posix_spawnattr_setcwd_np()
Set the current working directory attribute in a spawn attributes object
Synopsis:
#include <spawn.h>
int posix_spawnattr_setcwd_np(
posix_spawnattr_t *attrp,
const int dirfd);
Arguments:
- attrp
- A pointer to the spawn attributes object that you want to modify.
- dirfd
- A file descriptor for the directory that you want to use as the child process's initial working directory, obtained by calling open(), specifying O_DIRECTORY in the flags.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
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(). This setting also takes effect before any file actions are made, including trying to open the executable.
If posix_spawn() is called after posix_spawnattr_setcwd_np(), you must provide a path relative to the new path chosen by posix_spawnattr_setcwd_np(). As a result, the child process will use the current working directory specified by posix_spawnattr_setcwd_np().
For more information about spawn attributes, see the entry for posix_spawn().
npin the name stands for
non-POSIX.
Returns:
- EOK
- Success.
- EINVAL
- The attrp pointer does not refer to a valid attribute structure.
Examples:
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;
}
Classification:
Safety: | |
---|---|
Cancellation point | No |
Signal handler | Yes |
Thread | Yes |