1 .\" Copyright (c) 2009 Bill O. Gallmeister (bgallmeister@gmail.com)
2 .\" and Copyright 2010 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References consulted:
27 .\" Linux glibc source code
28 .\" POSIX 1003.1-2004 documentation
29 .\" (http://www.opengroup.org/onlinepubs/009695399)
31 .TH POSIX_SPAWN 3 2021-03-22 "GNU" "Linux Programmer's Manual"
33 posix_spawn, posix_spawnp \- spawn a process
38 .BI "int posix_spawn(pid_t *restrict " pid ", const char *restrict " path ,
39 .BI " const posix_spawn_file_actions_t *restrict " file_actions ,
40 .BI " const posix_spawnattr_t *restrict " attrp ,
41 .BI " char *const " argv [restrict],
42 .BI " char *const " envp [restrict]);
43 .BI "int posix_spawnp(pid_t *restrict " pid ", const char *restrict " file ,
44 .BI " const posix_spawn_file_actions_t *restrict " file_actions ,
45 .BI " const posix_spawnattr_t *restrict " attrp ,
46 .BI " char *const " argv [restrict],
47 .BI " char *const " envp [restrict]);
54 functions are used to create a new child process that executes
56 These functions were specified by POSIX to provide a standardized method
57 of creating new processes on machines that lack the capability
61 These machines are generally small, embedded systems lacking MMU support.
67 functions provide the functionality of a combined
71 with some optional housekeeping steps in the child process before the
73 These functions are not meant to replace the
78 In fact, they provide only a subset of the functionality
79 that can be achieved by using the system calls.
81 The only difference between
85 is the manner in which they specify the file to be executed by
89 the executable file is specified as a pathname
90 (which can be absolute or relative).
93 the executable file is specified as a simple filename;
94 the system searches for this file in the list of directories specified by
96 (in the same way as for
98 For the remainder of this page, the discussion is phrased in terms of
100 with the understanding that
102 differs only on the point just described.
104 The remaining arguments to these two functions are as follows:
108 argument points to a buffer that is used to return the process ID
109 of the new child process.
114 .I "spawn file actions object"
115 that specifies file-related actions to be performed in the child
121 This object is initialized and populated before the
124 .BR posix_spawn_file_actions_init (3)
126 .BR posix_spawn_file_actions_* ()
131 argument points to an
132 .I attributes objects
133 that specifies various attributes of the created child process.
134 This object is initialized and populated before the
137 .BR posix_spawnattr_init (3)
139 .BR posix_spawnattr_* ()
146 arguments specify the argument list and environment for the program
147 that is executed in the child process, as for
150 Below, the functions are described in terms of a three-step process: the
154 step (executed in the child),
157 step (executed in the child).
159 Since glibc 2.24, the
161 function commences by calling
168 Older implementations use
174 The PID of the new child process is placed in
178 function then returns control to the parent process.
180 Subsequently, the parent can use one of the system calls described in
182 to check the status of the child process.
183 If the child fails in any of the housekeeping steps described below,
184 or fails to execute the desired file,
185 it exits with a status of 127.
187 Before glibc 2.24, the child process is created using
191 when either of the following is true:
195 element of the attributes object pointed to by
197 contains the GNU-specific flag
198 .BR POSIX_SPAWN_USEVFORK ;
204 element of the attributes object pointed to by
206 does \fInot\fP contain
207 .BR POSIX_SPAWN_SETSIGMASK ,
208 .BR POSIX_SPAWN_SETSIGDEF ,
209 .BR POSIX_SPAWN_SETSCHEDPARAM ,
210 .BR POSIX_SPAWN_SETSCHEDULER ,
211 .BR POSIX_SPAWN_SETPGROUP ,
213 .BR POSIX_SPAWN_RESETIDS .
217 is used if the caller requests it,
218 or if there is no cleanup expected in the child before it
221 .SS pre-exec() step: housekeeping
226 steps, a child process may need to perform a set of housekeeping actions.
231 functions support a small, well-defined set of system tasks that the child
232 process can accomplish before it executes the executable file.
233 These operations are controlled by the attributes object pointed to by
235 and the file actions object pointed to by
237 In the child, processing is done in the following sequence:
239 Process attribute actions: signal mask, signal default handlers,
240 scheduling algorithm and parameters,
241 process group, and effective user and group IDs
242 are changed as specified by the attributes object pointed to by
245 File actions, as specified in the
248 are performed in the order that they were specified using calls to the
249 .BR posix_spawn_file_actions_add* ()
252 File descriptors with the
256 All process attributes in the child,
257 other than those affected by attributes specified in the
260 and the file actions in the object pointed to by
262 will be affected as though the child was created with
264 and it executed the program with
267 The process attributes actions are defined by the attributes object
273 .BR posix_spawnattr_setflags (3))
274 controls the general actions that occur,
275 and other attributes in the object specify values
276 to be used during those actions.
278 The effects of the flags that may be specified in
282 .B POSIX_SPAWN_SETSIGMASK
283 Set the signal mask to the signal set specified in the
288 .\" .BR posix_spawnattr_setsigmask (3))
289 of the object pointed to by
292 .B POSIX_SPAWN_SETSIGMASK
293 flag is not set, then the child inherits the parent's signal mask.
295 .B POSIX_SPAWN_SETSIGDEF
296 Reset the disposition of all signals in the set specified in the
301 .\" .BR posix_spawnattr_setsigdefault (3))
302 of the object pointed to by
305 For the treatment of the dispositions of signals not specified in the
307 attribute, or the treatment when
308 .B POSIX_SPAWN_SETSIGDEF
309 is not specified, see
312 .B POSIX_SPAWN_SETSCHEDPARAM
313 .\" (POSIX_PRIORITY_SCHEDULING only)
314 If this flag is set, and the
315 .B POSIX_SPAWN_SETSCHEDULER
316 flag is not set, then set the scheduling parameters
317 to the parameters specified in the
322 .\" .BR posix_spawnattr_setschedparam (3))
323 of the object pointed to by
326 .B POSIX_SPAWN_SETSCHEDULER
327 Set the scheduling policy algorithm and parameters of the child,
331 The scheduling policy is set to the value specified in the
336 .\" .BR posix_spawnattr_setpolicy (3))
337 of the object pointed to by
340 The scheduling parameters are set to the value specified in the
345 .\" .BR posix_spawnattr_setschedparam (3))
346 of the object pointed to by
351 .B POSIX_SPAWN_SETSCHEDPARAM
353 .B POSIX_SPAWN_SETSCHEDPOLICY
354 flags are not specified,
355 the child inherits the corresponding scheduling attributes from the parent.
358 .B POSIX_SPAWN_RESETIDS
360 reset the effective UID and GID to the
361 real UID and GID of the parent process.
362 If this flag is not set,
363 then the child retains the effective UID and GID of the parent.
364 In either case, if the set-user-ID and set-group-ID permission
365 bits are enabled on the executable file, their effect will override
366 the setting of the effective UID and GID (se
369 .B POSIX_SPAWN_SETPGROUP
370 Set the process group to the value specified in the
375 .\" .BR posix_spawnattr_setpgroup (3))
376 of the object pointed to by
380 attribute has the value 0,
381 the child's process group ID is made the same as its process ID.
383 .B POSIX_SPAWN_SETPGROUP
384 flag is not set, the child inherits the parent's process group ID.
386 .B POSIX_SPAWN_USEVFORK
387 Since glibc 2.24, this flag has no effect.
388 On older implementations, setting this flag forces the
396 feature test macro must be defined to obtain the definition of this constant.
398 .BR POSIX_SPAWN_SETSID " (since glibc 2.26)"
400 the child process shall create a new session and become the session leader.
401 The child process shall also become the process group leader of the new process
402 group in the session (see
406 feature test macro must be defined to obtain the definition of this constant.
407 .\" This flag has been accepted in POSIX, see:
408 .\" http://austingroupbugs.net/view.php?id=1044
409 .\" and has been implemented in glibc since version 2.26
410 .\" commit daeb1fa2e1b33323e719015f5f546988bd4cc73b
414 is NULL, then the default behaviors described above for each flag apply.
415 .\" mtk: I think we probably don't want to say the following, since it
416 .\" could lead people to do the wrong thing
417 .\" The POSIX standard tells you to call
418 .\" this function to de-initialize the attributes object pointed to by
420 .\" when you are done with it;
421 .\" however, on Linux systems this operation is a no-op.
425 argument specifies a sequence of file operations
426 that are performed in the child process after
427 the general processing described above, and before it performs the
431 is NULL, then no special action is taken, and standard
433 semantics apply\(emfile descriptors open before the exec
434 remain open in the new process,
435 except those for which the
438 File locks remain in place.
442 is not NULL, then it contains an ordered set of requests to
448 These requests are added to the
451 .BR posix_spawn_file_actions_addopen (3),
452 .BR posix_spawn_file_actions_addclose (3),
454 .BR posix_spawn_file_actions_adddup2 (3).
455 The requested operations are performed in the order they were added to
457 .\" FIXME . I think the following is best placed in the
458 .\" posix_spawn_file_actions_adddup2(3) page, and a similar statement is
459 .\" also needed in posix_spawn_file_actions_addclose(3)
460 .\" Note that you can specify file descriptors in
461 .\" .I posix_spawn_file_actions_adddup2 (3)
462 .\" which would not be usable if you called
464 .\" at that time--i.e., file descriptors that are opened or
465 .\" closed by the earlier operations
467 .\" .I file_actions .
469 If any of the housekeeping actions fails
470 (due to bogus values being passed or other reasons why signal handling,
471 process scheduling, process group ID functions,
472 and file descriptor operations might fail),
473 the child process exits with exit value 127.
475 Once the child has successfully forked and performed
476 all requested pre-exec steps,
477 the child runs the requested executable.
479 The child process takes its environment from the
481 argument, which is interpreted as if it had been passed to
483 The arguments to the created process come from the
485 argument, which is processed as for
488 Upon successful completion,
492 place the PID of the child process in
495 If there is an error during the
498 then no child is created,
502 and these functions return an error number as described below.
504 Even when these functions return a success status,
505 the child process may still fail for a plethora of reasons related to its
506 pre-\fBexec\fR() initialization.
510 In all of these cases, the child process will exit with the exit value of 127.
516 functions fail only in the case where the underlying
521 call fails; in these cases, these functions return an error number,
522 which will be one of the errors described for
528 In addition, these functions fail if:
531 Function not supported on this system.
537 functions are available since glibc 2.2.
539 POSIX.1-2001, POSIX.1-2008.
540 .\" FIXME . This piece belongs in spawnattr_setflags(3)
542 .\" .B POSIX_SPAWN_USEVFORK
543 .\" flag is a GNU extension; the
545 .\" feature test macro must be defined (before including any header files)
546 .\" to obtain the definition of this constant.
548 The housekeeping activities in the child are controlled by
549 the objects pointed to by
551 (for non-file actions) and
553 In POSIX parlance, the
556 .I posix_spawn_file_actions_t
557 data types are referred to as objects,
558 and their elements are not specified by name.
559 Portable programs should initialize these objects using
560 only the POSIX-specified functions.
562 although these objects may be implemented as structures containing fields,
563 portable programs must avoid dependence on such implementation details.)
565 According to POSIX, it is unspecified whether fork handlers established with
566 .BR pthread_atfork (3)
570 Since glibc 2.24, the fork handlers are not executed in any case.
571 .\" Tested on glibc 2.12
572 On older implementations,
573 fork handlers are called only if the child is created using
576 There is no "posix_fspawn" function (i.e., a function that is to
582 However, this functionality can be obtained by specifying the
584 argument as one of the files in the caller's
588 POSIX.1 says that when
589 .B POSIX_SPAWN_SETSCHEDULER
593 .B POSIX_SPAWN_SETSCHEDPARAM
594 (if present) is ignored.
595 However, before glibc 2.14, calls to
597 failed with an error if
598 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12052
599 .BR POSIX_SPAWN_SETSCHEDULER
600 was specified without also specifying
601 .BR POSIX_SPAWN_SETSCHEDPARAM .
603 The program below demonstrates the use of various functions in the
605 The program accepts command-line attributes that can be used
606 to create file actions and attributes objects.
607 The remaining command-line arguments are used as the executable name
608 and command-line arguments of the program that is executed in the child.
610 In the first run, the
612 command is executed in the child, and the
614 call employs no file actions or attributes objects.
620 Tue Feb 1 19:47:50 CEST 2011
621 Child status: exited, status=0
627 command-line option is used to create a file actions object that closes
628 standard output in the child.
631 fails when trying to perform output and exits with a status of 1.
635 $ \fB./a.out \-c date\fP
637 date: write error: Bad file descriptor
638 Child status: exited, status=1
644 command-line option is used to create an attributes object that
645 specifies that all (blockable) signals in the child should be blocked.
646 Consequently, trying to kill child with the default signal sent by
650 fails, because that signal is blocked.
651 Therefore, to kill the child,
659 $ \fB./a.out \-s sleep 60 &\fP
664 $ \fBkill \-KILL 7638\fP
665 $ Child status: killed by signal 9
666 [1]+ Done ./a.out \-s sleep 60
670 When we try to execute a nonexistent command in the child, the
672 fails and the child exits with a status of 127.
678 Child status: exited, status=127
693 #define errExit(msg) do { perror(msg); \e
694 exit(EXIT_FAILURE); } while (0)
696 #define errExitEN(en, msg) \e
697 do { errno = en; perror(msg); \e
698 exit(EXIT_FAILURE); } while (0)
703 main(int argc, char *argv[])
708 posix_spawnattr_t attr;
709 posix_spawnattr_t *attrp;
710 posix_spawn_file_actions_t file_actions;
711 posix_spawn_file_actions_t *file_actionsp;
713 /* Parse command\-line options, which can be used to specify an
714 attributes object and file actions object for the child. */
717 file_actionsp = NULL;
719 while ((opt = getopt(argc, argv, "sc")) != \-1) {
721 case \(aqc\(aq: /* \-c: close standard output in child */
723 /* Create a file actions object and add a "close"
726 s = posix_spawn_file_actions_init(&file_actions);
728 errExitEN(s, "posix_spawn_file_actions_init");
730 s = posix_spawn_file_actions_addclose(&file_actions,
733 errExitEN(s, "posix_spawn_file_actions_addclose");
735 file_actionsp = &file_actions;
738 case \(aqs\(aq: /* \-s: block all signals in child */
740 /* Create an attributes object and add a "set signal mask"
743 s = posix_spawnattr_init(&attr);
745 errExitEN(s, "posix_spawnattr_init");
746 s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
748 errExitEN(s, "posix_spawnattr_setflags");
751 s = posix_spawnattr_setsigmask(&attr, &mask);
753 errExitEN(s, "posix_spawnattr_setsigmask");
760 /* Spawn the child. The name of the program to execute and the
761 command\-line arguments are taken from the command\-line arguments
762 of this program. The environment of the program execed in the
763 child is made the same as the parent\(aqs environment. */
765 s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp,
766 &argv[optind], environ);
768 errExitEN(s, "posix_spawn");
770 /* Destroy any objects that we created earlier. */
773 s = posix_spawnattr_destroy(attrp);
775 errExitEN(s, "posix_spawnattr_destroy");
778 if (file_actionsp != NULL) {
779 s = posix_spawn_file_actions_destroy(file_actionsp);
781 errExitEN(s, "posix_spawn_file_actions_destroy");
784 printf("PID of child: %jd\en", (intmax_t) child_pid);
786 /* Monitor status of the child until it terminates. */
789 s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
793 printf("Child status: ");
794 if (WIFEXITED(status)) {
795 printf("exited, status=%d\en", WEXITSTATUS(status));
796 } else if (WIFSIGNALED(status)) {
797 printf("killed by signal %d\en", WTERMSIG(status));
798 } else if (WIFSTOPPED(status)) {
799 printf("stopped by signal %d\en", WSTOPSIG(status));
800 } else if (WIFCONTINUED(status)) {
801 printf("continued\en");
803 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
809 .nh \" Disable hyphenation
817 .BR sched_setparam (2),
818 .BR sched_setscheduler (2),
823 .BR posix_spawn_file_actions_addclose (3),
824 .BR posix_spawn_file_actions_adddup2 (3),
825 .BR posix_spawn_file_actions_addopen (3),
826 .BR posix_spawn_file_actions_destroy (3),
827 .BR posix_spawn_file_actions_init (3),
828 .BR posix_spawnattr_destroy (3),
829 .BR posix_spawnattr_getflags (3),
830 .BR posix_spawnattr_getpgroup (3),
831 .BR posix_spawnattr_getschedparam (3),
832 .BR posix_spawnattr_getschedpolicy (3),
833 .BR posix_spawnattr_getsigdefault (3),
834 .BR posix_spawnattr_getsigmask (3),
835 .BR posix_spawnattr_init (3),
836 .BR posix_spawnattr_setflags (3),
837 .BR posix_spawnattr_setpgroup (3),
838 .BR posix_spawnattr_setschedparam (3),
839 .BR posix_spawnattr_setschedpolicy (3),
840 .BR posix_spawnattr_setsigdefault (3),
841 .BR posix_spawnattr_setsigmask (3),
842 .BR pthread_atfork (3),
844 Base Definitions volume of POSIX.1-2001,
845 .I http://www.opengroup.org/unix/online.html