Add an “open a file” action to a spawn file action object
#include <spawn.h> int posix_spawn_file_actions_addopen( posix_spawn_file_actions_t *_Restrict fact_p, int new_fd, const char *_Restrict path, int oflags, mode_t omode);
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The posix_spawn_file_actions_addopen() function adds an open action to the object referenced by fact_p that causes the file named by path to be opened (as if open(path, oflag, mode) had been called, and the returned file descriptor, if not already new_fd, had been changed to new_fd) when a new process is spawned using this file actions object. If new_fd was already an open file descriptor, it will be closed before the new file is opened.
Files that need to be opened for use by the spawned process can alternatively be handled either by having the calling process open them before the posix_spawn() or posix_spawnp() call (and close them after), or by passing filenames to the spawned process (in argv) so that it may open them itself.
Note that the spawn open file action provides to posix_spawn() and posix_spawnp() the same capability that the shell redirection operators provide to system(), only without the intervening execution of a shell; for example:
system ("myprog <file1 3<file2");
If the calling process needs to open one or more files for access by the spawned process, but has insufficient spare file descriptors, then the open action is necessary to allow the open() to occur in the context of the child process after other file descriptors have been closed (that must remain open in the parent). Additionally, if a parent is executed from a file having a “set-user-id” mode bit set and the POSIX_SPAWN_RESETIDS flag is set in the spawn attributes (see posix_spawn()), a file created within the parent process will (possibly incorrectly) have the parent's effective user ID as its owner, whereas a file created with an open() action during posix_spawn() or posix_spawnp() will have the parent's real ID as its owner; an open action by the parent process may successfully open a file to which the real user should not have access, or fail to open a file to which the real user should have access.
It isn't considered an error if the new_fd argument specifies a file descriptor for which the specified operation couldn't be performed at the time of the call. This type of error isn't detected when the associated file actions object is later used during a posix_spawn() or posix_spawnp() operation.
POSIX 1003.1 SPN
posix_spawn(), posix_spawn_file_actions_addclose(), posix_spawn_file_actions_adddup2(), posix_spawn_file_actions_destroy(), posix_spawn_file_actions_init(), posix_spawnattr_addpartid(), posix_spawnattr_addpartition(), posix_spawnattr_destroy(), posix_spawnattr_getcred(), posix_spawnattr_getflags(), posix_spawnattr_getnode(), posix_spawnattr_getpartid(), posix_spawnattr_getpgroup(), posix_spawnattr_getrunmask(), posix_spawnattr_getschedparam(), posix_spawnattr_getschedpolicy(), posix_spawnattr_getsigdefault(), posix_spawnattr_getsigignore(), posix_spawnattr_getsigmask(), posix_spawnattr_getstackmax(), posix_spawnattr_getxflags(), posix_spawnattr_init(), posix_spawnattr_setcred(), posix_spawnattr_setflags(), posix_spawnattr_setnode(), posix_spawnattr_setpgroup(), posix_spawnattr_setrunmask(), posix_spawnattr_setschedparam(), posix_spawnattr_setschedpolicy(), posix_spawnattr_setsigdefault(), posix_spawnattr_setsigignore(), posix_spawnattr_setsigmask(), posix_spawnattr_setstackmax(), posix_spawnattr_setxflags(), posix_spawnp()