posix_spawn()

Spawn a process

Synopsis:

#include <sys/spawn.h>

/* If using C and gcc version 2.95, use:

int posix_spawn(pid_t *_Restrict pid,
        const char *_Restrict path,
        const posix_spawn_file_actions_t *file_actions,
        const posix_spawnattr_t *_Restrict attrp,
        char *const argv[],
        char *const envp[]);

/* If using C++ and gcc higher than version 2.95, use:

int posix_spawn(pid_t *_Restrict pid,
        const char *_Restrict path,
        const posix_spawn_file_actions_t *file_actions,
        const posix_spawnattr_t *_Restrict attrp,
        char *const argv[_Restrict_arr],
        char *const envp[_Restrict_arr])

Arguments:

argv
A pointer to an argument vector. The value in argv[0] should represent the filename of the program being loaded, but can be NULL if no arguments are being passed. The last member of argv must be a NULL pointer. The value of argv can't be NULL.
attrp
A pointer to a spawn attributes object. If the value of the attrp pointer is NULL, then the default values are used.
envp
A pointer to an array of character pointers, each pointing to a string defining an environment variable. The array is terminated with a NULL pointer. Each pointer points to a character string of the form:
variable=value
 

that's used to define an environment variable. If the value of envp is NULL, then the child process inherits the environment of the parent process.

file_actions
The spawn file actions object is pointed to by file_actions parameter.
path
The full path to the executable. Use posix_spawnp() to search for the executable to spawn.
pid
The process ID.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The posix_spawn() function creates a new process (a child process) from the specified process image. The new image is constructed from a regular executable file called the new process image file. This means that the only difference between posix_spawnp() and posix_spawn() is that posix_spawnp() accepts the name of an executable without its full path having been specified. Therefore, all this function needs to do is to locate file and then prefix it with its location, as required. posix_spawn() can then be called to perform the actual work.

When a C program is executed as the result of this call, it is entered as a C-language function call as follows:

int main(int argc, char *argv[]);

where argc is the argument count and argv is an array of character pointers to the arguments themselves.

In addition, the following variable: extern char **environ; is initialized as a pointer to an array of character pointers to the environment strings. The argument argv is an array of character pointers to NULL-terminated strings. The last member of this array is a NULL pointer and isn't counted in argc. These strings constitute the argument list available to the new process image. The value in argv[0] should point to a filename that's associated with the process image being started by the posix_spawn() or posix_spawnp() function.

The argument envp is an array of character pointers to NULL-terminated strings. These strings constitute the environment for the new process image. The environment array is terminated by a NULL pointer. The number of bytes available for the child process' combined argument and environment lists is {ARG_MAX}. The implementation shall specify in the system documentation (see the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 2, Conformance) whether any list overhead, such as length words, NULL terminators, pointers, or alignment bytes, are included in this total. The path argument to posix_spawn() is a pathname that identifies the new process image file to execute.

The file parameter to posix_spawnp() is used to construct a pathname that identifies the new process image file. If the file parameter contains a slash character, the file parameter will be used as the pathname for the new process image file. Otherwise, the path prefix for this file will be obtained by a search of the directories passed as the environment variable PATH (see the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 8, Environment Variables.) If this environment variable isn't defined, the results of the search are implementation-defined.

If file_actions is a NULL pointer, then the file descriptors open in the calling process will remain open in the child process, except for those whose close-on-exec flag FD_CLOEXEC is set (see fcntl()). For those file descriptors that remain open, all attributes for the corresponding open file descriptions, including file locks (see fcntl()), will remain unchanged. If file_actions is not NULL, then the file descriptors open in the child process will be those open in the calling process as modified by the spawn file actions object pointed to by file_actions, and the FD_CLOEXEC flag of each remaining open file descriptor after the spawn file actions have been processed. The effective order of processing the spawn file actions will be:

  1. The set of open file descriptors for the child process will initially be the same set as is open for the calling process. All attributes of the corresponding open file descriptions, including file locks (see fcntl()), will remain unchanged.
  2. The signal mask, signal default actions, and the effective user and group IDs for the child process will be changed as specified in the attributes object referenced by attrp.
  3. The file actions specified by the spawn file actions object will be performed in the order in which they were added to the spawn file actions object.
  4. Any file descriptor that has its FD_CLOEXEC flag set (see fcntl()) will be closed.

The posix_spawnattr_t spawn attributes object type is defined in <spawn.h>. It will contain at least the attributes defined below.

If the POSIX_SPAWN_SETPGROUP flag is set in the spawn-flags attribute of the object referenced by attrp, and the spawn-pgroup attribute of the same object is non-zero, then the child's process group will be as specified in the spawn-pgroup attribute of the object referenced by attrp.

As a special case, if the POSIX_SPAWN_SETPGROUP flag is set in the spawn-flags attribute of the object referenced by attrp, and the spawn-pgroup attribute of the same object is set to zero, then the child will be in a new process group with a process group ID equal to its process ID. If the POSIX_SPAWN_SETPGROUP flag isn't set in the spawn-flags attribute of the object referenced by attrp, the new child process will inherit the parent's process group.

If the POSIX_SPAWN_SETSCHEDPARAM flag is set in the spawn-flags attribute of the object referenced by attrp, but POSIX_SPAWN_SETSCHEDULER isn't set, the new process image will initially have the scheduling policy of the calling process with the scheduling parameters specified in the spawn-schedparam attribute of the object referenced by attrp.

If the POSIX_SPAWN_SETSCHEDULER flag is set in the spawn-flags attribute of the object referenced by attrp (regardless of the setting of the POSIX_SPAWN_SETSCHEDPARAM flag), the new process image will initially have the scheduling policy specified in the spawn-schedpolicy attribute of the object referenced by attrp and the scheduling parameters specified in the spawn-schedparam attribute of the same object.

The POSIX_SPAWN_RESETIDS flag in the spawn-flags attribute of the object referenced by attrp governs the effective user ID of the child process. If this flag isn't set, the child process will inherit the parent process' effective user ID. If this flag is set, the child process' effective user ID will be reset to the parent' s real user ID. In either case, if the set-user-ID mode bit of the new process image file is set, the effective user ID of the child process will become that file's owner ID before the new process image begins execution.

The POSIX_SPAWN_RESETIDS flag in the spawn-flags attribute of the object referenced by attrp also governs the effective group ID of the child process. If this flag isn't set, the child process will inherit the parent process' effective group ID. If this flag is set, the child process' effective group ID will be reset to the parent's real group ID. In either case, if the set-group-ID mode bit of the new process image file is set, the effective group ID of the child process will become that file's group ID before the new process image begins execution.

If the POSIX_SPAWN_SETSIGMASK flag is set in the spawn-flags attribute of the object referenced by attrp, the child process will initially have the signal mask specified in the spawn-sigmask attribute of the object referenced by attrp.

If the POSIX_SPAWN_SETSIGDEF flag is set in the spawn-flags attribute of the object referenced by attrp, the signals specified in the spawn-sigdefault attribute of the same object will be set to their default actions in the child process. Signals set to the default action in the parent process will be set to the default action in the child process.

Signals set to be caught by the calling process are set to the default action in the child process.

Except for SIGCHLD, signals set to be ignored by the calling process image are set to be ignored by the child process, unless otherwise specified by the POSIX_SPAWN_SETSIGDEF flag being set in the spawn-flags attribute of the object referenced by attrp and the signals being indicated in the spawn-sigdefault attribute of the object referenced by attrp.

If the SIGCHLD signal is set to be ignored by the calling process, it's unspecified whether the SIGCHLD signal is set to be ignored or to the default action in the child process, unless otherwise specified by the POSIX_SPAWN_SETSIGDEF flag being set in the spawn_flags attribute of the object referenced by attrp and the SIGCHLDsignal being indicated in the spawn_sigdefault attribute of the object referenced by attrp.

If the value of the attrp pointer is NULL, then the default values are used.

All process attributes - other than those influenced by the attributes set in the object referenced by attrp as specified above or by the file descriptor manipulations specified in file_actions - appear in the new process image as though fork() had been called to create a child process, and then a member of the exec() family of functions had been called by the child process to execute the new process image.

It is implementation-defined whether the fork() handlers are run when posix_spawn() or posix_spawnp() is called.

Returns:

Upon successful completion, posix_spawn() returns the process ID of the child process to the parent process in the variable pointed to by a non-NULL pid argument, and returns zero as the function return value. Otherwise, no child process is created, the value stored into the variable pointed to by a non-NULL pid is unspecified, and an error number is returned as the function return value to indicate the error. If the pid argument is a NULL pointer, the process ID of the child isn't returned to the caller.

Errors:

EINVAL
For any invalid parameter. An invalid argument was provided including an improperly initialized posix_spawnattr_t, posix_spawn_file_actions_t object, or if more than one PATH= is present in the environment.
EIO
An internal error occurred in the library.
ENOENT
The path argument couldn't be found. The path must be the full path to the executable; otherwise, ENOENT will be returned. Use posix_spawn() if you want to go on a search for the executable to spawn.
ENOMEM
The memory required to create the message to send to procnto couldn't be allocated memory to create the new process and its associated data structures couldn't be allocated. For partitions, the partition ID couldn't be added to the attributes object.
EOK
Success.
errno
Any error returned by a stat() on path.
ETXTBSY
The text file that you're trying to execute is busy (e.g. it might be open for writing).

When Adaptive Partitioning modules are also included in the image, the following error codes can be returned:

EACCES
The spawned program doesn't have permission to associate with the specified partitions.
EEXIST
Either one of the following errors have occurred:
ENOMEM
The posix_spawnattr_t object specifies a memory partition that doesn't exist. The new process was unable to associate with one or more memory partitions to be inherited from the parent process.

For EINVAL, posix_spawn() might fail if:

Classification:

POSIX 1003.1 RTS

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

See also:

posix_spawn_file_actions_addclose(), posix_spawn_file_actions_adddup2(), posix_spawn_file_actions_addopen(), 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_setschedparam(), posix_spawnattr_setrunmask(), posix_spawnattr_setschedpolicy(), posix_spawnattr_setsigdefault(), posix_spawnattr_setsigignore(), posix_spawnattr_setstackmax(), posix_spawnattr_setstackmax(), posix_spawnattr_setxflags(), posix_spawnp()