posix_spawnattr_setcwd_np()

Updated: April 19, 2023

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().

For more information about spawn attributes, see the entry for posix_spawn().

Note: This function is a QNX Neutrino extension. The “np” in the name stands for “non-POSIX.”

Returns:

EOK
Success.
EINVAL
An argument was invalid.

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:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes