There has been a recent (very recent, like within the past few hours) proposal to add the ability to do a chdir() (or fchdir()) to posix_spawn
There are more changes than what are included here, but the rest are mostly all just boilerplate changes to support this addition. If anyone has any comments, I'm happy to pass them along, or you can directly (this is issue 1208 if anyone wants to comment directly.) kre [I have made some whitespace and line wrapping changes to this text] posix_spawn_file_actions_addchdir( ) NAME posix_spawn_file_actions_addchdir, posix_spawn_file_actions_addfchdir - add chdir or fchdir action to spawn file actions object (ADVANCED REALTIME) SYNOPSIS #include <spawn.h> int posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t *restrict file_actions, const char *restrict path); int posix_spawn_file_actions_addfchdir(posix_spawn_file_actions_t *file_actions, int filedes); DESCRIPTION The posix_spawn_file_actions_addchdir( ) function shall add a chdir action to the object referenced by file_actions that shall cause the working directory to be set to path (as if chdir(path) had been called) when a new process is spawned using this file actions object. A relative path shall be interpreted in relation to the working directory determined by any prior actions. The string described by path shall be copied by the posix_spawn_file_actions_addchdir( ) function. The posix_spawn_file_actions_addfchdir( ) function shall add a fchdir action to the object referenced by file_actions that shall cause the working directory to be set to filedes (as if fchdir(filedes) had been called) when a new process is spawned using this file actions object. A spawn file actions object is as defined in posix_spawn_file_actions_addclose( ). File actions are performed in a new process created by posix_spawn( ) or posix_spawnp( ) in the same order that they were added to the file actions object. Thus, the execution of an addopen action that was created by a call to posix_spawn_file_actions_addopen( ) that specifies a relative path will be affected by the execution of a chdir or fchdir action that was created by a previous call to posix_spawn_file_actions_addchdir( ) or posix_spawn_file_actions_addfchdir( ). Likewise, a relative path passed to posix_spawn( ) will be affected by the last chdir or fchdir action in the file action list. RETURN VALUE Upon successful completion, these functions shall return zero; otherwise, an error number shall be returned to indicate the error. ERRORS The posix_spawn_file_actions_addfchdir( ) function shall fail if: [EBADF] The value specified by fildes is negative. These functions shall fail if: [ENOMEM] Insufficient memory exists to add to the spawn file actions object. It shall not be considered an error for the path or fildes argument passed to these functions to specify a path or file descriptor for which the specified operation could not be performed at the time of the call. Any such error will be detected when the associated file actions object is later used during a posix_spawn( ) or posix_spawnp( ) operation. These functions may fail if: [EINVAL] The value specified by file_actions is invalid. EXAMPLES None. APPLICATION USAGE The posix_spawn_file_actions_addchdir( ) and posix_spawn_file_actions_addfchdir( ) functions are part of the Spawn option and need not be provided on all implementations. Changing the working directory of a child process can be useful when invoking utilities such as pax. Furthermore, the ability to add fchdir actions to posix_spawn( ) gives the caller as much control over relative pathnames processed in the context of the child as it would otherwise have using openat( ), since all file actions are processed in sequence in the context of the child at a point where the child process is still single-threaded. Without chdir or fchdir actions, changing the working directory of the child would require a shim utility (some implementations provide env -C /new/path program args... as an extension, but the standard does not require this extension), or else temporarily changing the working directory in the parent process prior to calling posix_spawn( ) (but this requires locking in a multi-threaded process, to ensure that no other thread is impacted by the temporary change to global state). RATIONALE Refer to the RATIONALE section in posix_spawn_file_actions_addclose( ). FUTURE DIRECTIONS None. SEE ALSO chdir( ), fchdir( ), posix_spawn( ), posix_spawn_file_actions_addclose( ), posix_spawn_file_actions_destroy( ). XBD <spawn.h> CHANGE HISTORY First released in Issue 8. Derived from Solaris posix_spawn_file_actions_addchdir_np.