| Updated: October 28, 2024 |
Set the current working directory attribute in a spawn attributes object
#include <spawn.h>
int posix_spawnattr_setcwd_np(
posix_spawnattr_t *attrp,
const int dirfd);
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The posix_spawnattr_setcwd_np() function sets the current working directory attribute in the given spawn attributes object. You must have already initialized the spawn attributes object by calling posix_spawnattr_init().
This setting takes effect only if the POSIX_SPAWN_SETCWD flag is set in the spawn attributes; to set this flag, call posix_spawnattr_setxflags().
For more information about spawn attributes, see the entry for posix_spawn().
posix_spawnattr_t spawnattr;
if (posix_spawnattr_init(&spawnattr) != 0) {
perror("posix_spawnattr_init");
return 1;
}
char *dir_to_open = "/proc/boot";
dirfd = open(dir_to_open, O_RDONLY | O_DIRECTORY);
if (dirfd < 0) {
perror("open");
return 1;
}
if (posix_spawnattr_setxflags(&spawnattr, POSIX_SPAWN_SETCWD) != 0) {
perror("posix_spawnattr_setxflags");
return 1;
}
if (posix_spawnattr_setcwd_np(&spawnattr, dirfd) != 0) {
perror("posix_spawnattr_setcwd_np");
return 1;
}
err = posix_spawn(&pid, argv[0], NULL, &spawnattr, child_argv,
child_envp);
if (err != 0) {
perror("posix_spawn");
return 1;
}
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | Yes |
| Signal handler | Yes |
| Thread | Yes |