1 .\" Copyright (c) 2009 Bill O. Gallmeister (bgallmeister@gmail.com)
2 .\" and Copyright 2010 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux glibc source code
8 .\" POSIX 1003.1-2004 documentation
9 .\" (http://www.opengroup.org/onlinepubs/009695399)
11 .TH posix_spawn 3 (date) "Linux man-pages (unreleased)"
13 posix_spawn, posix_spawnp \- spawn a process
16 .RI ( libc ", " \-lc )
21 .BI "int posix_spawn(pid_t *restrict " pid ", const char *restrict " path ,
22 .BI " const posix_spawn_file_actions_t *restrict " file_actions ,
23 .BI " const posix_spawnattr_t *restrict " attrp ,
24 .BI " char *const " argv [restrict],
25 .BI " char *const " envp [restrict]);
26 .BI "int posix_spawnp(pid_t *restrict " pid ", const char *restrict " file ,
27 .BI " const posix_spawn_file_actions_t *restrict " file_actions ,
28 .BI " const posix_spawnattr_t *restrict " attrp ,
29 .BI " char *const " argv [restrict],
30 .BI " char *const " envp [restrict]);
37 functions are used to create a new child process that executes
39 These functions were specified by POSIX to provide a standardized method
40 of creating new processes on machines that lack the capability
44 These machines are generally small, embedded systems lacking MMU support.
50 functions provide the functionality of a combined
54 with some optional housekeeping steps in the child process before the
56 These functions are not meant to replace the
61 In fact, they provide only a subset of the functionality
62 that can be achieved by using the system calls.
64 The only difference between
68 is the manner in which they specify the file to be executed by
72 the executable file is specified as a pathname
73 (which can be absolute or relative).
76 the executable file is specified as a simple filename;
77 the system searches for this file in the list of directories specified by
79 (in the same way as for
81 For the remainder of this page, the discussion is phrased in terms of
83 with the understanding that
85 differs only on the point just described.
87 The remaining arguments to these two functions are as follows:
90 points to a buffer that is used to return the process ID
91 of the new child process.
95 .I "spawn file actions object"
96 that specifies file-related actions to be performed in the child
102 This object is initialized and populated before the
105 .BR posix_spawn_file_actions_init (3)
107 .BR posix_spawn_file_actions_* ()
112 .I attributes objects
113 that specifies various attributes of the created child process.
114 This object is initialized and populated before the
117 .BR posix_spawnattr_init (3)
119 .BR posix_spawnattr_* ()
125 specify the argument list and environment for the program
126 that is executed in the child process, as for
129 Below, the functions are described in terms of a three-step process: the
133 step (executed in the child),
136 step (executed in the child).
138 Since glibc 2.24, the
140 function commences by calling
147 Older implementations use
153 The PID of the new child process is placed in
157 function then returns control to the parent process.
159 Subsequently, the parent can use one of the system calls described in
161 to check the status of the child process.
162 If the child fails in any of the housekeeping steps described below,
163 or fails to execute the desired file,
164 it exits with a status of 127.
166 Before glibc 2.24, the child process is created using
170 when either of the following is true:
174 element of the attributes object pointed to by
176 contains the GNU-specific flag
177 .BR POSIX_SPAWN_USEVFORK ;
183 element of the attributes object pointed to by
185 does \fInot\fP contain
186 .BR POSIX_SPAWN_SETSIGMASK ,
187 .BR POSIX_SPAWN_SETSIGDEF ,
188 .BR POSIX_SPAWN_SETSCHEDPARAM ,
189 .BR POSIX_SPAWN_SETSCHEDULER ,
190 .BR POSIX_SPAWN_SETPGROUP ,
192 .BR POSIX_SPAWN_RESETIDS .
196 is used if the caller requests it,
197 or if there is no cleanup expected in the child before it
200 .SS pre-exec() step: housekeeping
205 steps, a child process may need to perform a set of housekeeping actions.
210 functions support a small, well-defined set of system tasks that the child
211 process can accomplish before it executes the executable file.
212 These operations are controlled by the attributes object pointed to by
214 and the file actions object pointed to by
216 In the child, processing is done in the following sequence:
218 Process attribute actions: signal mask, signal default handlers,
219 scheduling algorithm and parameters,
220 process group, and effective user and group IDs
221 are changed as specified by the attributes object pointed to by
224 File actions, as specified in the
227 are performed in the order that they were specified using calls to the
228 .BR posix_spawn_file_actions_add* ()
231 File descriptors with the
235 All process attributes in the child,
236 other than those affected by attributes specified in the
239 and the file actions in the object pointed to by
241 will be affected as though the child was created with
243 and it executed the program with
246 The process attributes actions are defined by the attributes object
252 .BR posix_spawnattr_setflags (3))
253 controls the general actions that occur,
254 and other attributes in the object specify values
255 to be used during those actions.
257 The effects of the flags that may be specified in
261 .B POSIX_SPAWN_SETSIGMASK
262 Set the signal mask to the signal set specified in the
267 .\" .BR posix_spawnattr_setsigmask (3))
268 of the object pointed to by
271 .B POSIX_SPAWN_SETSIGMASK
272 flag is not set, then the child inherits the parent's signal mask.
274 .B POSIX_SPAWN_SETSIGDEF
275 Reset the disposition of all signals in the set specified in the
280 .\" .BR posix_spawnattr_setsigdefault (3))
281 of the object pointed to by
284 For the treatment of the dispositions of signals not specified in the
286 attribute, or the treatment when
287 .B POSIX_SPAWN_SETSIGDEF
288 is not specified, see
291 .B POSIX_SPAWN_SETSCHEDPARAM
292 .\" (POSIX_PRIORITY_SCHEDULING only)
293 If this flag is set, and the
294 .B POSIX_SPAWN_SETSCHEDULER
295 flag is not set, then set the scheduling parameters
296 to the parameters specified in the
301 .\" .BR posix_spawnattr_setschedparam (3))
302 of the object pointed to by
305 .B POSIX_SPAWN_SETSCHEDULER
306 Set the scheduling policy algorithm and parameters of the child,
310 The scheduling policy is set to the value specified in the
315 .\" .BR posix_spawnattr_setpolicy (3))
316 of the object pointed to by
319 The scheduling parameters are set to the value specified in the
324 .\" .BR posix_spawnattr_setschedparam (3))
325 of the object pointed to by
330 .B POSIX_SPAWN_SETSCHEDPARAM
332 .B POSIX_SPAWN_SETSCHEDPOLICY
333 flags are not specified,
334 the child inherits the corresponding scheduling attributes from the parent.
337 .B POSIX_SPAWN_RESETIDS
339 reset the effective UID and GID to the
340 real UID and GID of the parent process.
341 If this flag is not set,
342 then the child retains the effective UID and GID of the parent.
343 In either case, if the set-user-ID and set-group-ID permission
344 bits are enabled on the executable file, their effect will override
345 the setting of the effective UID and GID (se
348 .B POSIX_SPAWN_SETPGROUP
349 Set the process group to the value specified in the
354 .\" .BR posix_spawnattr_setpgroup (3))
355 of the object pointed to by
359 attribute has the value 0,
360 the child's process group ID is made the same as its process ID.
362 .B POSIX_SPAWN_SETPGROUP
363 flag is not set, the child inherits the parent's process group ID.
365 .B POSIX_SPAWN_USEVFORK
366 Since glibc 2.24, this flag has no effect.
367 On older implementations, setting this flag forces the
375 feature test macro must be defined to obtain the definition of this constant.
377 .BR POSIX_SPAWN_SETSID " (since glibc 2.26)"
379 the child process shall create a new session and become the session leader.
380 The child process shall also become the process group leader of the new process
381 group in the session (see
385 feature test macro must be defined to obtain the definition of this constant.
386 .\" This flag has been accepted in POSIX, see:
387 .\" http://austingroupbugs.net/view.php?id=1044
388 .\" and has been implemented since glibc 2.26
389 .\" commit daeb1fa2e1b33323e719015f5f546988bd4cc73b
393 is NULL, then the default behaviors described above for each flag apply.
394 .\" mtk: I think we probably don't want to say the following, since it
395 .\" could lead people to do the wrong thing
396 .\" The POSIX standard tells you to call
397 .\" this function to de-initialize the attributes object pointed to by
399 .\" when you are done with it;
400 .\" however, on Linux systems this operation is a no-op.
404 argument specifies a sequence of file operations
405 that are performed in the child process after
406 the general processing described above, and before it performs the
410 is NULL, then no special action is taken, and standard
412 semantics apply\[em]file descriptors open before the exec
413 remain open in the new process,
414 except those for which the
417 File locks remain in place.
421 is not NULL, then it contains an ordered set of requests to
427 These requests are added to the
430 .BR posix_spawn_file_actions_addopen (3),
431 .BR posix_spawn_file_actions_addclose (3),
433 .BR posix_spawn_file_actions_adddup2 (3).
434 The requested operations are performed in the order they were added to
436 .\" FIXME . I think the following is best placed in the
437 .\" posix_spawn_file_actions_adddup2(3) page, and a similar statement is
438 .\" also needed in posix_spawn_file_actions_addclose(3)
439 .\" Note that you can specify file descriptors in
440 .\" .I posix_spawn_file_actions_adddup2 (3)
441 .\" which would not be usable if you called
443 .\" at that time--i.e., file descriptors that are opened or
444 .\" closed by the earlier operations
446 .\" .I file_actions .
448 If any of the housekeeping actions fails
449 (due to bogus values being passed or other reasons why signal handling,
450 process scheduling, process group ID functions,
451 and file descriptor operations might fail),
452 the child process exits with exit value 127.
454 Once the child has successfully forked and performed
455 all requested pre-exec steps,
456 the child runs the requested executable.
458 The child process takes its environment from the
460 argument, which is interpreted as if it had been passed to
462 The arguments to the created process come from the
464 argument, which is processed as for
467 Upon successful completion,
471 place the PID of the child process in
474 If there is an error during the
477 then no child is created,
481 and these functions return an error number as described below.
483 Even when these functions return a success status,
484 the child process may still fail for a plethora of reasons related to its
485 pre-\fBexec\fR() initialization.
489 In all of these cases, the child process will exit with the exit value of 127.
495 functions fail only in the case where the underlying
500 call fails; in these cases, these functions return an error number,
501 which will be one of the errors described for
507 In addition, these functions fail if:
510 Function not supported on this system.
516 .\" FIXME . This piece belongs in spawnattr_setflags(3)
518 .\" .B POSIX_SPAWN_USEVFORK
519 .\" flag is a GNU extension; the
521 .\" feature test macro must be defined (before including any header files)
522 .\" to obtain the definition of this constant.
524 The housekeeping activities in the child are controlled by
525 the objects pointed to by
527 (for non-file actions) and
529 In POSIX parlance, the
532 .I posix_spawn_file_actions_t
533 data types are referred to as objects,
534 and their elements are not specified by name.
535 Portable programs should initialize these objects using
536 only the POSIX-specified functions.
538 although these objects may be implemented as structures containing fields,
539 portable programs must avoid dependence on such implementation details.)
541 According to POSIX, it is unspecified whether fork handlers established with
542 .BR pthread_atfork (3)
546 Since glibc 2.24, the fork handlers are not executed in any case.
547 .\" Tested on glibc 2.12
548 On older implementations,
549 fork handlers are called only if the child is created using
552 There is no "posix_fspawn" function (i.e., a function that is to
558 However, this functionality can be obtained by specifying the
560 argument as one of the files in the caller's
564 POSIX.1 says that when
565 .B POSIX_SPAWN_SETSCHEDULER
569 .B POSIX_SPAWN_SETSCHEDPARAM
570 (if present) is ignored.
571 However, before glibc 2.14, calls to
573 failed with an error if
574 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12052
575 .B POSIX_SPAWN_SETSCHEDULER
576 was specified without also specifying
577 .BR POSIX_SPAWN_SETSCHEDPARAM .
579 The program below demonstrates the use of various functions in the
581 The program accepts command-line attributes that can be used
582 to create file actions and attributes objects.
583 The remaining command-line arguments are used as the executable name
584 and command-line arguments of the program that is executed in the child.
586 In the first run, the
588 command is executed in the child, and the
590 call employs no file actions or attributes objects.
596 Tue Feb 1 19:47:50 CEST 2011
597 Child status: exited, status=0
603 command-line option is used to create a file actions object that closes
604 standard output in the child.
607 fails when trying to perform output and exits with a status of 1.
611 $ \fB./a.out \-c date\fP
613 date: write error: Bad file descriptor
614 Child status: exited, status=1
620 command-line option is used to create an attributes object that
621 specifies that all (blockable) signals in the child should be blocked.
622 Consequently, trying to kill child with the default signal sent by
626 fails, because that signal is blocked.
627 Therefore, to kill the child,
635 $ \fB./a.out \-s sleep 60 &\fP
640 $ \fBkill \-KILL 7638\fP
641 $ Child status: killed by signal 9
642 [1]+ Done ./a.out \-s sleep 60
646 When we try to execute a nonexistent command in the child, the
648 fails and the child exits with a status of 127.
654 Child status: exited, status=127
659 .\" SRC BEGIN (posix_spawn.c)
670 #define errExit(msg) do { perror(msg); \e
671 exit(EXIT_FAILURE); } while (0)
673 #define errExitEN(en, msg) \e
674 do { errno = en; perror(msg); \e
675 exit(EXIT_FAILURE); } while (0)
680 main(int argc, char *argv[])
685 posix_spawnattr_t attr;
686 posix_spawnattr_t *attrp;
687 posix_spawn_file_actions_t file_actions;
688 posix_spawn_file_actions_t *file_actionsp;
690 /* Parse command\-line options, which can be used to specify an
691 attributes object and file actions object for the child. */
694 file_actionsp = NULL;
696 while ((opt = getopt(argc, argv, "sc")) != \-1) {
698 case \[aq]c\[aq]: /* \-c: close standard output in child */
700 /* Create a file actions object and add a "close"
703 s = posix_spawn_file_actions_init(&file_actions);
705 errExitEN(s, "posix_spawn_file_actions_init");
707 s = posix_spawn_file_actions_addclose(&file_actions,
710 errExitEN(s, "posix_spawn_file_actions_addclose");
712 file_actionsp = &file_actions;
715 case \[aq]s\[aq]: /* \-s: block all signals in child */
717 /* Create an attributes object and add a "set signal mask"
720 s = posix_spawnattr_init(&attr);
722 errExitEN(s, "posix_spawnattr_init");
723 s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
725 errExitEN(s, "posix_spawnattr_setflags");
728 s = posix_spawnattr_setsigmask(&attr, &mask);
730 errExitEN(s, "posix_spawnattr_setsigmask");
737 /* Spawn the child. The name of the program to execute and the
738 command\-line arguments are taken from the command\-line arguments
739 of this program. The environment of the program execed in the
740 child is made the same as the parent\[aq]s environment. */
742 s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp,
743 &argv[optind], environ);
745 errExitEN(s, "posix_spawn");
747 /* Destroy any objects that we created earlier. */
750 s = posix_spawnattr_destroy(attrp);
752 errExitEN(s, "posix_spawnattr_destroy");
755 if (file_actionsp != NULL) {
756 s = posix_spawn_file_actions_destroy(file_actionsp);
758 errExitEN(s, "posix_spawn_file_actions_destroy");
761 printf("PID of child: %jd\en", (intmax_t) child_pid);
763 /* Monitor status of the child until it terminates. */
766 s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
770 printf("Child status: ");
771 if (WIFEXITED(status)) {
772 printf("exited, status=%d\en", WEXITSTATUS(status));
773 } else if (WIFSIGNALED(status)) {
774 printf("killed by signal %d\en", WTERMSIG(status));
775 } else if (WIFSTOPPED(status)) {
776 printf("stopped by signal %d\en", WSTOPSIG(status));
777 } else if (WIFCONTINUED(status)) {
778 printf("continued\en");
780 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
787 .nh \" Disable hyphenation
795 .BR sched_setparam (2),
796 .BR sched_setscheduler (2),
801 .BR posix_spawn_file_actions_addclose (3),
802 .BR posix_spawn_file_actions_adddup2 (3),
803 .BR posix_spawn_file_actions_addopen (3),
804 .BR posix_spawn_file_actions_destroy (3),
805 .BR posix_spawn_file_actions_init (3),
806 .BR posix_spawnattr_destroy (3),
807 .BR posix_spawnattr_getflags (3),
808 .BR posix_spawnattr_getpgroup (3),
809 .BR posix_spawnattr_getschedparam (3),
810 .BR posix_spawnattr_getschedpolicy (3),
811 .BR posix_spawnattr_getsigdefault (3),
812 .BR posix_spawnattr_getsigmask (3),
813 .BR posix_spawnattr_init (3),
814 .BR posix_spawnattr_setflags (3),
815 .BR posix_spawnattr_setpgroup (3),
816 .BR posix_spawnattr_setschedparam (3),
817 .BR posix_spawnattr_setschedpolicy (3),
818 .BR posix_spawnattr_setsigdefault (3),
819 .BR posix_spawnattr_setsigmask (3),
820 .BR pthread_atfork (3),
822 Base Definitions volume of POSIX.1-2001,
823 .I http://www.opengroup.org/unix/online.html