2 .\" Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
3 .\" Portions Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
4 .\" 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/.
5 .\" 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
6 .\" are reprinted and reproduced in electronic form in the Sun OS 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
7 .\" 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.
8 .\" This notice shall appear on any product containing this material.
9 .\" 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.
10 .\" 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.
11 .\" 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]
12 .\" Copyright 2011 by Delphix. All rights reserved.
13 .TH POSIX_SPAWN 3C "Feb 20, 2009"
15 posix_spawn, posix_spawnp \- spawn a process
21 \fBint\fR \fBposix_spawn\fR(\fBpid_t *restrict\fR \fIpid\fR, \fBconst char *restrict\fR \fIpath\fR,
22 \fBconst posix_spawn_file_actions_t *\fR\fIfile_actions\fR,
23 \fBconst posix_spawnattr_t *restrict\fR \fIattrp\fR,
24 \fBchar *const\fR \fIargv\fR[restrict], \fBchar *const\fR \fIenvp\fR[restrict]);
29 \fBint\fR \fBposix_spawnp\fR(\fBpid_t *restrict\fR \fIpid\fR, \fBconst char *restrict\fR \fIfile\fR,
30 \fBconst posix_spawn_file_actions_t *\fR\fIfile_actions\fR,
31 \fBconst posix_spawnattr_t *restrict\fR \fIattrp\fR,
32 \fBchar *const\fR \fIargv\fR[restrict], \fBchar *const\fR \fIenvp\fR[restrict]);
38 The \fBposix_spawn()\fR and \fBposix_spawnp()\fR functions create a new process
39 (child process) from the specified process image. The new process image is
40 constructed from a regular executable file called the new process image file.
43 When a C program is executed as the result of this call, it is entered as a C
44 language function call as follows:
48 int main(int \fIargc\fR, char *\fIargv\fR[]);
54 where \fIargc\fR is the argument count and \fIargv\fR is an array of character
55 pointers to the arguments themselves. In addition, the following variable
59 extern char **environ;
65 is initialized as a pointer to an array of character pointers to the
69 The argument \fIargv\fR is an array of character pointers to null-terminated
70 strings. The last member of this array is a null pointer and is not counted in
71 \fIargc\fR. These strings constitute the argument list available to the new
72 process image. The value in \fIargv\fR[0] should point to a filename that is
73 associated with the process image being started by the \fBposix_spawn()\fR or
74 \fBposix_spawnp()\fR function.
77 The argument \fIenvp\fR is an array of character pointers to null-terminated
78 strings. These strings constitute the environment for the new process image.
79 The environment array is terminated by a null pointer.
82 The number of bytes available for the child process's combined argument and
83 environment lists is {\fBARG_MAX\fR}, counting all character pointers, the
84 strings they point to, the trailing null bytes in the strings, and the
85 list-terminating null pointers. There is no additional system overhead included
89 The \fIpath\fR argument to \fBposix_spawn()\fR is a pathname that identifies
90 the new process image file to execute.
93 The \fIfile\fR parameter to \fBposix_spawnp()\fR is used to construct a
94 pathname that identifies the new process image file. If the file parameter
95 contains a slash character, the file parameter is used as the pathname for the
96 new process image file. Otherwise, the path prefix for this file is obtained by
97 a search of the directories passed as the environment variable \fBPATH\fR. If
98 this environment variable is not defined, the results of the search are
99 implementation-defined.
102 If \fIfile_actions\fR is a null pointer, then file descriptors open in the
103 calling process remain open in the child process, except for those whose
104 close-on-exec flag \fBFD_CLOEXEC\fR is set (see \fBfcntl\fR(2)). For those
105 file descriptors that remain open, all attributes of the corresponding open
106 file descriptions, including file locks (see \fBfcntl\fR(2)), remain unchanged.
109 If \fIfile_actions\fR is not \fINULL\fR, then the file descriptors open in the
110 child process are those open in the calling process as modified by the spawn
111 file actions object pointed to by \fIfile_actions\fR and the \fBFD_CLOEXEC\fR
112 flag of each remaining open file descriptor after the spawn file actions have
113 been processed. The effective order of processing the spawn file actions are:
117 The set of open file descriptors for the child process are initially the
118 same set as is open for the calling process. All attributes of the
119 corresponding open file descriptions, including file locks (see
120 \fBfcntl\fR(2)), remain unchanged.
125 The signal mask, signal default or ignore actions, and the effective user
126 and group IDs for the child process are changed as specified in the attributes
127 object referenced by \fIattrp\fR.
132 The file actions specified by the spawn file actions object are performed in
133 the order in which they were added to the spawn file actions object.
138 Any file descriptor that has its \fBFD_CLOEXEC\fR flag set (see
139 \fBfcntl\fR(2)) is closed.
143 The \fBposix_spawnattr_t\fR spawn attributes object type is defined in
144 <\fBspawn.h\fR>. It contains at least the attributes defined below.
147 If the \fBPOSIX_SPAWN_SETPGROUP\fR flag is set in the \fIspawn-flags\fR
148 attribute of the object referenced by \fIattrp\fR, and the \fIspawn-pgroup\fR
149 attribute of the same object is non-zero, then the child's process group is as
150 specified in the \fIspawn-pgroup\fR attribute of the object referenced by
154 As a special case, if the \fBPOSIX_SPAWN_SETPGROUP\fR flag is set in the
155 \fIspawn-flags\fR attribute of the object referenced by \fIattrp\fR, and the
156 \fIspawn-pgroup\fR attribute of the same object is set to zero, then the child
157 will be in a new process group with a process group ID equal to its process ID.
160 If the \fBPOSIX_SPAWN_SETPGROUP\fR flag is not set in the \fIspawn-flags\fR
161 attribute of the object referenced by \fIattrp\fR, the new child process
162 inherits the parent's process group.
165 If the \fBPOSIX_SPAWN_SETSCHEDPARAM\fR flag is set in the \fIspawn-flags\fR
166 attribute of the object referenced by \fIattrp\fR, but
167 \fBPOSIX_SPAWN_SETSCHEDULER\fR is not set, the new process image initially has
168 the scheduling policy of the calling process with the scheduling parameters
169 specified in the \fIspawn-schedparam\fR attribute of the object referenced by
173 If the \fBPOSIX_SPAWN_SETSCHEDULER\fR flag is set in \fIspawn-flags\fR
174 attribute of the object referenced by \fIattrp\fR (regardless of the setting of
175 the \fBPOSIX_SPAWN_SETSCHEDPARAM\fR flag), the new process image initially has
176 the scheduling policy specified in the \fIspawn-schedpolicy\fR attribute of the
177 object referenced by \fIattrp\fR and the scheduling parameters specified in the
178 \fIspawn-schedparam\fR attribute of the same object.
181 The \fBPOSIX_SPAWN_RESETIDS\fR flag in the \fIspawn-flags\fR attribute of the
182 object referenced by \fIattrp\fR governs the effective user ID of the child
183 process. If this flag is not set, the child process inherits the parent
184 process's effective user ID. If this flag is set, the child process's effective
185 user ID is reset to the parent's real user ID. In either case, if the
186 set-user-ID mode bit of the new process image file is set, the effective user
187 ID of the child process becomes that file's owner ID before the new process
188 image begins execution. If this flag is set, the child process's effective user
189 ID is reset to the parent's real user ID. In either case, if the set-user-ID
190 mode bit of the new process image file is set, the effective user ID of the
191 child process becomes that file's owner ID before the new process image begins
195 The \fBPOSIX_SPAWN_RESETIDS\fR flag in the \fIspawn-flags\fR attribute of the
196 object referenced by \fIattrp\fR also governs the effective group ID of the
197 child process. If this flag is not set, the child process inherits the parent
198 process's effective group ID. If this flag is set, the child process's
199 effective group ID is reset to the parent's real group ID. In either case, if
200 the set-group-ID mode bit of the new process image file is set, the effective
201 group ID of the child process becomes that file's group ID before the new
202 process image begins execution.
205 If the \fBPOSIX_SPAWN_SETSIGMASK\fR flag is set in the \fIspawn-flags\fR
206 attribute of the object referenced by \fIattrp\fR, the child process initially
207 has the signal mask specified in the \fIspawn-sigmask\fR attribute of the
208 object referenced by \fIattrp\fR.
211 If the \fBPOSIX_SPAWN_SETSIGDEF\fR flag is set in the \fIspawn-flags\fR
212 attribute of the object referenced by \fIattrp\fR, the signals specified in the
213 \fIspawn-sigdefault\fR attribute of the same object is set to their default
214 actions in the child process.
217 If the \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flag is set in the spawn-flags attribute
218 of the object referenced by \fIattrp\fR, the signals specified in the
219 \fIspawn-sigignore\fR attribute of the same object are set to be ignored in the
223 If both \fBPOSIX_SPAWN_SETSIGDEF\fR and \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flags
224 are set in the spawn-flags attribute of the object referenced by \fIattrp\fR,
225 the actions for \fBPOSIX_SPAWN_SETSIGDEF\fR take precedence over the actions
226 for \fBPOSIX_SPAWN_SETSIGIGN_NP\fR.
229 If the \fBPOSIX_SPAWN_NOSIGCHLD_NP\fR flag is set in the \fIspawn-flags\fR
230 attribute of the object referenced by \fIattrp\fR, no \fBSIGCHLD\fR signal will
231 be posted to the parent process when the child process terminates, regardless
232 of the disposition of the \fBSIGCHLD\fR signal in the parent. \fBSIGCHLD\fR
233 signals are still possible for job control stop and continue actions if the
234 parent has requested them.
237 If the \fBPOSIX_SPAWN_WAITPID_NP\fR flag is set in the \fIspawn-flags\fR
238 attribute of the object referenced by \fIattrp\fR, no wait-for-multiple-pids
239 operation by the parent, as in \fBwait()\fR, \fBwaitid\fR(\fBP_ALL\fR), or
240 \fBwaitid\fR(\fBP_PGID\fR), will succeed in reaping the child, and the child
241 will not be reaped automatically due the disposition of the \fBSIGCHLD\fR
242 signal being set to be ignored in the parent. Only a specific wait for the
243 child, as in \fBwaitid\fR(\fBP_PID\fR, \fBpid\fR), is allowed and it is
244 required, else when the child exits it will remain a zombie until the parent
248 If the \fBPOSIX_SPAWN_NOEXECERR_NP\fR flag is set in the spawn-flags attribute
249 of the object referenced by \fIattrp\fR, and if the specified process image
250 file cannot be executed, then the \fBposix_spawn()\fR and \fBposix_spawnp()\fR
251 functions do not fail with one of the \fBexec\fR(2) error codes, as is normal,
252 but rather return successfully having created a child process that exits
253 immediately with exit status 127. This flag permits \fBsystem\fR(3C) and
254 \fBpopen\fR(3C) to be implemented with \fBposix_spawn()\fR and still conform
255 strictly to their POSIX specifications.
258 Signals set to be caught or set to the default action in the calling process
259 are set to the default action in the child process, unless the
260 \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flag is set in the spawn-flags attribute of the
261 object referenced by \fIattrp\fR and the signals are specified in the
262 \fIspawn-sigignore\fR attribute of the same object.
265 Except for \fBSIGCHLD\fR, signals set to be ignored by the calling process
266 image are set to be ignored by the child process, unless otherwise specified by
267 the \fBPOSIX_SPAWN_SETSIGDEF\fR flag being set in the \fIspawn-flags\fR
268 attribute of the object referenced by \fIattrp\fR and the signals being
269 indicated in the \fIspawn-sigdefault\fR attribute of the object referenced by
273 If the \fBSIGCHLD\fR signal is set to be ignored by the calling process, it is
274 unspecified whether the \fBSIGCHLD\fR signal is set to be ignored or to the
275 default action in the child process, unless otherwise specified by the
276 \fBPOSIX_SPAWN_SETSIGDEF\fR flag being set in the \fIspawn-flags\fR attribute
277 of the object referenced by \fIattrp\fR and the \fBSIGCHLD\fR signal being
278 indicated in the \fIspawn-sigdefault\fR attribute of the object referenced by
282 If the value of the \fIattrp\fR pointer is \fINULL\fR, then the default values
286 All process attributes, other than those influenced by the attributes set in
287 the object referenced by \fIattrp\fR as specified above or by the file
288 descriptor manipulations specified in \fIfile_actions\fR appear in the new
289 process image as though \fBfork()\fR had been called to create a child process
290 and then a member of the \fBexec\fR family of functions had been called by the
291 child process to execute the new process image.
294 The fork handlers are not run when \fBposix_spawn()\fR or \fBposix_spawnp()\fR
299 Upon successful completion, \fBposix_spawn()\fR and \fBposix_spawnp()\fR return
300 the process ID of the child process to the parent process in the variable
301 pointed to by a non-null \fIpid\fR argument, and return zero as the function
302 return value. Otherwise, no child process is created, the value stored into the
303 variable pointed to by a non-null \fIpid\fR is unspecified, and an error number
304 is returned as the function return value to indicate the error. If the
305 \fIpid\fR argument is a null pointer, the process ID of the child is not
306 returned to the caller.
310 The \fBposix_spawn()\fR and \fBposix_spawnp()\fR functions will fail if:
317 The value specified by \fIfile_actions\fR or \fIattrp\fR is invalid.
322 If \fBposix_spawn()\fR or \fBposix_spawnp()\fR fails for any of the reasons
323 that would cause \fBfork()\fR or one of the \fBexec\fR family of functions to
324 fail, an error value is returned as described by \fBfork\fR(2) and
325 \fBexec\fR(2), respectively
328 If \fBPOSIX_SPAWN_SETPGROUP\fR is set in the \fIspawn-flags\fR attribute of the
329 object referenced by \fIattrp\fR, and \fBposix_spawn()\fR or
330 \fBposix_spawnp()\fR fails while changing the child's process group, an error
331 value is returned as described by \fBsetpgid\fR(2).
334 If \fBPOSIX_SPAWN_SETSCHEDPARAM\fR is set and \fBPOSIX_SPAWN_SETSCHEDULER\fR is
335 not set in the \fIspawn-flags\fR attribute of the object referenced by
336 \fIattrp\fR, then if \fBposix_spawn()\fR or \fBposix_spawnp()\fR fails for any
337 of the reasons that would cause \fBsched_setparam()\fR to fail, an error value
338 is returned as described by \fBsched_setparam\fR(3C).
341 If \fBPOSIX_SPAWN_SETSCHEDULER\fR is set in the \fIspawn-flags\fR attribute of
342 the object referenced by \fIattrp\fR, and if \fBposix_spawn()\fR or
343 \fBposix_spawnp()\fR fails for any of the reasons that would cause
344 \fBsched_setscheduler()\fR to fail, an error value is returned as described by
345 \fBsched_setscheduler\fR(3C).
348 If the \fIfile_actions\fR argument is not \fINULL\fR and specifies any
349 \fBclose()\fR, \fBdup2()\fR, or \fBopen()\fR actions to be performed, and if
350 \fBposix_spawn()\fR or \fBposix_spawnp()\fR fails for any of the reasons that
351 would cause \fBclose()\fR, \fBdup2()\fR, or \fBopen()\fR to fail, an error
352 value is returned as described by \fBclose\fR(2), \fBdup2\fR(3C), or
353 \fBopen\fR(2), respectively. An open file action might, by itself, result in
354 any of the errors described by \fBclose()\fR or \fBdup2()\fR, in addition to
355 those described by \fBopen()\fR.
358 If a \fBclose\fR(2) operation is specified to be performed for a file
359 descriptor that is not open at the time of the call to \fBposix_spawn()\fR or
360 \fBposix_spawnp()\fR, the action does not cause \fBposix_spawn()\fR or
361 \fBposix_spawnp()\fR to fail.
365 See \fBattributes\fR(5) for descriptions of the following attributes:
373 ATTRIBUTE TYPE ATTRIBUTE VALUE
375 Interface Stability Committed
379 Standard See \fBstandards\fR(5).
385 \fBalarm\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBdup\fR(2), \fBexec\fR(2),
386 \fBexit\fR(2), \fBfcntl\fR(2), \fBfork\fR(2), \fBkill\fR(2), \fBopen\fR(2),
387 \fBsetpgid\fR(2), \fBsetuid\fR(2), \fBstat\fR(2), \fBtimes\fR(2),
388 \fBdup2\fR(3C), \fBpopen\fR(3C), \fBposix_spawn_file_actions_addclose\fR(3C),
389 \fBposix_spawn_file_actions_adddup2\fR(3C),
390 \fBposix_spawn_file_actions_addopen\fR(3C),
391 \fBposix_spawn_file_actions_destroy\fR(3C),
392 \fBposix_spawn_file_actions_init\fR(3C),
393 \fBposix_spawn_pipe_np\fR(3C),
394 \fBposix_spawnattr_destroy\fR(3C),
395 \fBposix_spawnattr_getflags\fR(3C), \fBposix_spawnattr_getpgroup\fR(3C),
396 \fBposix_spawnattr_getschedparam\fR(3C),
397 \fBposix_spawnattr_getschedpolicy\fR(3C),
398 \fBposix_spawnattr_getsigdefault\fR(3C),
399 \fBposix_spawnattr_getsigignore_np\fR(3C),
400 \fBposix_spawnattr_getsigmask\fR(3C), \fBposix_spawnattr_init\fR(3C),
401 \fBposix_spawnattr_setflags\fR(3C), \fBposix_spawnattr_setpgroup\fR(3C),
402 \fBposix_spawnattr_setschedparam\fR(3C),
403 \fBposix_spawnattr_setschedpolicy\fR(3C),
404 \fBposix_spawnattr_setsigdefault\fR(3C),
405 \fBposix_spawnattr_setsigignore_np\fR(3C),
406 \fBposix_spawnattr_setsigmask\fR(3C), \fBsched_setparam\fR(3C),
407 \fBsched_setscheduler\fR(3C), \fBsystem\fR(3C), \fBwait\fR(3C),
408 \fBattributes\fR(5), \fBstandards\fR(5)
412 The SUSv3 POSIX standard (The Open Group Base Specifications Issue 6, IEEE Std
413 1003.1-2001) permits the \fBposix_spawn()\fR and \fBposix_spawnp()\fR functions
414 to return successfully before some of the above-described errors are detected,
415 allowing the child process to fail instead:
419 \&... if the error occurs after the calling process
420 successfully returns, the child process exits with
427 With the one exception of when the \fBPOSIX_SPAWN_NOEXECERR_NP\fR flag is
428 passed in the attributes structure, this behavior is not present in the Solaris
429 implementation. Any error that occurs before the new process image is
430 successfully constructed causes the \fBposix_spawn()\fR and
431 \fBposix_spawnp()\fR functions to return the corresponding non-zero error value
432 without creating a child process.
435 The \fBPOSIX_SPAWN_NOSIGCHLD_NP\fR, \fBPOSIX_SPAWN_WAITPID_NP\fR,
436 \fBPOSIX_SPAWN_NOEXECERR_NP\fR, and \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flags and
437 the \fBposix_spawnattr_getsigignore_np()\fR and
438 \fBposix_spawnattr_setsigignore_np()\fR functions are non-portable Solaris
439 extensions to the \fBposix_spawn()\fR and \fBposix_spawnp()\fR interfaces.