xref: /illumos-gate/usr/src/man/man3c/posix_spawn.3c (revision bbf21555)

Sun Microsystems, Inc. gratefully acknowledges The Open Group for
permission to reproduce portions of its copyrighted documentation.
Original documentation from The Open Group can be obtained online at
http://www.opengroup.org/bookstore/.

The Institute of Electrical and Electronics Engineers and The Open
Group, have given us permission to reprint portions of their
documentation.

In the following statement, the phrase ``this text'' refers to portions
of the system documentation.

Portions of this text are reprinted and reproduced in electronic form
in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
Standard for Information Technology -- Portable Operating System
Interface (POSIX), The Open Group Base Specifications Issue 6,
Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
Engineers, Inc and The Open Group. In the event of any discrepancy
between these versions and the original IEEE and The Open Group
Standard, the original IEEE and The Open Group Standard is the referee
document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html.

This notice shall appear on any product containing this material.

The contents of this file are subject to the terms of the
Common Development and Distribution License (the "License").
You may not use this file except in compliance with the License.

You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
or http://www.opensolaris.org/os/licensing.
See the License for the specific language governing permissions
and limitations under the License.

When distributing Covered Code, include this CDDL HEADER in each
file and include the License file at usr/src/OPENSOLARIS.LICENSE.
If applicable, add the following below this CDDL HEADER, with the
fields enclosed by brackets "[]" replaced with your own identifying
information: Portions Copyright [yyyy] [name of copyright owner]


Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved.
Portions Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
Copyright 2011 by Delphix. All rights reserved.

POSIX_SPAWN 3C "Feb 20, 2009"
NAME
posix_spawn, posix_spawnp - spawn a process
SYNOPSIS

#include <spawn.h>

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], char *const envp[restrict]);

int posix_spawnp(pid_t *restrict pid, const char *restrict file,
 const posix_spawn_file_actions_t *file_actions,
 const posix_spawnattr_t *restrict attrp,
 char *const argv[restrict], char *const envp[restrict]);
DESCRIPTION

The posix_spawn() and posix_spawnp() functions create a new process (child process) from the specified process image. The new process image is constructed from a regular executable file called the new process image file.

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 is not 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 is 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's combined argument and environment lists is {ARG_MAX}, counting all character pointers, the strings they point to, the trailing null bytes in the strings, and the list-terminating null pointers. There is no additional system overhead 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 is used as the pathname for the new process image file. Otherwise, the path prefix for this file is obtained by a search of the directories passed as the environment variable PATH. If this environment variable is not defined, the results of the search are implementation-defined.

If file_actions is a null pointer, then file descriptors open in the calling process remain open in the child process, except for those whose close-on-exec flag FD_CLOEXEC is set (see fcntl(2)). For those file descriptors that remain open, all attributes of the corresponding open file descriptions, including file locks (see fcntl(2)), remain unchanged.

If file_actions is not NULL, then the file descriptors open in the child process are 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 are:

1. The set of open file descriptors for the child process are initially the same set as is open for the calling process. All attributes of the corresponding open file descriptions, including file locks (see fcntl(2)), remain unchanged.

2. The signal mask, signal default or ignore actions, and the effective user and group IDs for the child process are changed as specified in the attributes object referenced by attrp.

3. The file actions specified by the spawn file actions object are 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(2)) is closed.

The posix_spawnattr_t spawn attributes object type is defined in <spawn.h>. It contains 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 is 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 is not set in the spawn-flags attribute of the object referenced by attrp, the new child process inherits 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 is not set, the new process image initially has 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 spawn-flags attribute of the object referenced by attrp (regardless of the setting of the POSIX_SPAWN_SETSCHEDPARAM flag), the new process image initially has 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 is not set, the child process inherits the parent process's effective user ID. If this flag is set, the child process's effective user ID is 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 becomes that file's owner ID before the new process image begins execution. If this flag is set, the child process's effective user ID is 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 becomes 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 is not set, the child process inherits the parent process's effective group ID. If this flag is set, the child process's effective group ID is 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 becomes 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 initially has 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 is set to their default actions in the child process.

If the POSIX_SPAWN_SETSIGIGN_NP flag is set in the spawn-flags attribute of the object referenced by attrp, the signals specified in the spawn-sigignore attribute of the same object are set to be ignored in the child process.

If both POSIX_SPAWN_SETSIGDEF and POSIX_SPAWN_SETSIGIGN_NP flags are set in the spawn-flags attribute of the object referenced by attrp, the actions for POSIX_SPAWN_SETSIGDEF take precedence over the actions for POSIX_SPAWN_SETSIGIGN_NP.

If the POSIX_SPAWN_NOSIGCHLD_NP flag is set in the spawn-flags attribute of the object referenced by attrp, no SIGCHLD signal will be posted to the parent process when the child process terminates, regardless of the disposition of the SIGCHLD signal in the parent. SIGCHLD signals are still possible for job control stop and continue actions if the parent has requested them.

If the POSIX_SPAWN_WAITPID_NP flag is set in the spawn-flags attribute of the object referenced by attrp, no wait-for-multiple-pids operation by the parent, as in wait(), waitid(P_ALL), or waitid(P_PGID), will succeed in reaping the child, and the child will not be reaped automatically due the disposition of the SIGCHLD signal being set to be ignored in the parent. Only a specific wait for the child, as in waitid(P_PID, pid), is allowed and it is required, else when the child exits it will remain a zombie until the parent exits.

If the POSIX_SPAWN_NOEXECERR_NP flag is set in the spawn-flags attribute of the object referenced by attrp, and if the specified process image file cannot be executed, then the posix_spawn() and posix_spawnp() functions do not fail with one of the exec(2) error codes, as is normal, but rather return successfully having created a child process that exits immediately with exit status 127. This flag permits system(3C) and popen(3C) to be implemented with posix_spawn() and still conform strictly to their POSIX specifications.

Signals set to be caught or set to the default action in the calling process are set to the default action in the child process, unless the POSIX_SPAWN_SETSIGIGN_NP flag is set in the spawn-flags attribute of the object referenced by attrp and the signals are specified in the spawn-sigignore attribute of the same object.

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 is 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 SIGCHLD signal 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.

The fork handlers are not run when posix_spawn() or posix_spawnp() is called.

RETURN VALUES

Upon successful completion, posix_spawn() and posix_spawnp() return the process ID of the child process to the parent process in the variable pointed to by a non-null pid argument, and return 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 is not returned to the caller.

ERRORS

The posix_spawn() and posix_spawnp() functions will fail if: EINVAL

The value specified by file_actions or attrp is invalid.

If posix_spawn() or posix_spawnp() fails for any of the reasons that would cause fork() or one of the exec family of functions to fail, an error value is returned as described by fork(2) and exec(2), respectively

If POSIX_SPAWN_SETPGROUP is set in the spawn-flags attribute of the object referenced by attrp, and posix_spawn() or posix_spawnp() fails while changing the child's process group, an error value is returned as described by setpgid(2).

If POSIX_SPAWN_SETSCHEDPARAM is set and POSIX_SPAWN_SETSCHEDULER is not set in the spawn-flags attribute of the object referenced by attrp, then if posix_spawn() or posix_spawnp() fails for any of the reasons that would cause sched_setparam() to fail, an error value is returned as described by sched_setparam(3C).

If POSIX_SPAWN_SETSCHEDULER is set in the spawn-flags attribute of the object referenced by attrp, and if posix_spawn() or posix_spawnp() fails for any of the reasons that would cause sched_setscheduler() to fail, an error value is returned as described by sched_setscheduler(3C).

If the file_actions argument is not NULL and specifies any close(), dup2(), or open() actions to be performed, and if posix_spawn() or posix_spawnp() fails for any of the reasons that would cause close(), dup2(), or open() to fail, an error value is returned as described by close(2), dup2(3C), or open(2), respectively. An open file action might, by itself, result in any of the errors described by close() or dup2(), in addition to those described by open().

If a close(2) operation is specified to be performed for a file descriptor that is not open at the time of the call to posix_spawn() or posix_spawnp(), the action does not cause posix_spawn() or posix_spawnp() to fail.

ATTRIBUTES

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE ATTRIBUTE VALUE
Interface Stability Committed
MT-Level MT-Safe
Standard See standards(7).
SEE ALSO

alarm (2), chmod (2), close (2), dup (2), exec (2), exit (2), fcntl (2), fork (2), kill (2), open (2), setpgid (2), setuid (2), stat (2), times (2), dup2 (3C), popen (3C), posix_spawn_file_actions_addclose (3C), posix_spawn_file_actions_adddup2 (3C), posix_spawn_file_actions_addopen (3C), posix_spawn_file_actions_destroy (3C), posix_spawn_file_actions_init (3C), posix_spawn_pipe_np (3C), posix_spawnattr_destroy (3C), posix_spawnattr_getflags (3C), posix_spawnattr_getpgroup (3C), posix_spawnattr_getschedparam (3C), posix_spawnattr_getschedpolicy (3C), posix_spawnattr_getsigdefault (3C), posix_spawnattr_getsigignore_np (3C), posix_spawnattr_getsigmask (3C), posix_spawnattr_init (3C), posix_spawnattr_setflags (3C), posix_spawnattr_setpgroup (3C), posix_spawnattr_setschedparam (3C), posix_spawnattr_setschedpolicy (3C), posix_spawnattr_setsigdefault (3C), posix_spawnattr_setsigignore_np (3C), posix_spawnattr_setsigmask (3C), sched_setparam (3C), sched_setscheduler (3C), system (3C), wait (3C), attributes (7), standards (7)

NOTES

The SUSv3 POSIX standard (The Open Group Base Specifications Issue 6, IEEE Std 1003.1-2001) permits the posix_spawn() and posix_spawnp() functions to return successfully before some of the above-described errors are detected, allowing the child process to fail instead:

... if the error occurs after the calling process
successfully returns, the child process exits with
exit status 127.

With the one exception of when the POSIX_SPAWN_NOEXECERR_NP flag is passed in the attributes structure, this behavior is not present in the Solaris implementation. Any error that occurs before the new process image is successfully constructed causes the posix_spawn() and posix_spawnp() functions to return the corresponding non-zero error value without creating a child process.

The POSIX_SPAWN_NOSIGCHLD_NP, POSIX_SPAWN_WAITPID_NP, POSIX_SPAWN_NOEXECERR_NP, and POSIX_SPAWN_SETSIGIGN_NP flags and the posix_spawnattr_getsigignore_np() and posix_spawnattr_setsigignore_np() functions are non-portable Solaris extensions to the posix_spawn() and posix_spawnp() interfaces.