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 2021-03-22 "GNU" "Linux Programmer's Manual"
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:
91 argument points to a buffer that is used to return the process ID
92 of the new child process.
97 .I "spawn file actions object"
98 that specifies file-related actions to be performed in the child
104 This object is initialized and populated before the
107 .BR posix_spawn_file_actions_init (3)
109 .BR posix_spawn_file_actions_* ()
114 argument points to an
115 .I attributes objects
116 that specifies various attributes of the created child process.
117 This object is initialized and populated before the
120 .BR posix_spawnattr_init (3)
122 .BR posix_spawnattr_* ()
129 arguments specify the argument list and environment for the program
130 that is executed in the child process, as for
133 Below, the functions are described in terms of a three-step process: the
137 step (executed in the child),
140 step (executed in the child).
142 Since glibc 2.24, the
144 function commences by calling
151 Older implementations use
157 The PID of the new child process is placed in
161 function then returns control to the parent process.
163 Subsequently, the parent can use one of the system calls described in
165 to check the status of the child process.
166 If the child fails in any of the housekeeping steps described below,
167 or fails to execute the desired file,
168 it exits with a status of 127.
170 Before glibc 2.24, the child process is created using
174 when either of the following is true:
178 element of the attributes object pointed to by
180 contains the GNU-specific flag
181 .BR POSIX_SPAWN_USEVFORK ;
187 element of the attributes object pointed to by
189 does \fInot\fP contain
190 .BR POSIX_SPAWN_SETSIGMASK ,
191 .BR POSIX_SPAWN_SETSIGDEF ,
192 .BR POSIX_SPAWN_SETSCHEDPARAM ,
193 .BR POSIX_SPAWN_SETSCHEDULER ,
194 .BR POSIX_SPAWN_SETPGROUP ,
196 .BR POSIX_SPAWN_RESETIDS .
200 is used if the caller requests it,
201 or if there is no cleanup expected in the child before it
204 .SS pre-exec() step: housekeeping
209 steps, a child process may need to perform a set of housekeeping actions.
214 functions support a small, well-defined set of system tasks that the child
215 process can accomplish before it executes the executable file.
216 These operations are controlled by the attributes object pointed to by
218 and the file actions object pointed to by
220 In the child, processing is done in the following sequence:
222 Process attribute actions: signal mask, signal default handlers,
223 scheduling algorithm and parameters,
224 process group, and effective user and group IDs
225 are changed as specified by the attributes object pointed to by
228 File actions, as specified in the
231 are performed in the order that they were specified using calls to the
232 .BR posix_spawn_file_actions_add* ()
235 File descriptors with the
239 All process attributes in the child,
240 other than those affected by attributes specified in the
243 and the file actions in the object pointed to by
245 will be affected as though the child was created with
247 and it executed the program with
250 The process attributes actions are defined by the attributes object
256 .BR posix_spawnattr_setflags (3))
257 controls the general actions that occur,
258 and other attributes in the object specify values
259 to be used during those actions.
261 The effects of the flags that may be specified in
265 .B POSIX_SPAWN_SETSIGMASK
266 Set the signal mask to the signal set specified in the
271 .\" .BR posix_spawnattr_setsigmask (3))
272 of the object pointed to by
275 .B POSIX_SPAWN_SETSIGMASK
276 flag is not set, then the child inherits the parent's signal mask.
278 .B POSIX_SPAWN_SETSIGDEF
279 Reset the disposition of all signals in the set specified in the
284 .\" .BR posix_spawnattr_setsigdefault (3))
285 of the object pointed to by
288 For the treatment of the dispositions of signals not specified in the
290 attribute, or the treatment when
291 .B POSIX_SPAWN_SETSIGDEF
292 is not specified, see
295 .B POSIX_SPAWN_SETSCHEDPARAM
296 .\" (POSIX_PRIORITY_SCHEDULING only)
297 If this flag is set, and the
298 .B POSIX_SPAWN_SETSCHEDULER
299 flag is not set, then set the scheduling parameters
300 to the parameters specified in the
305 .\" .BR posix_spawnattr_setschedparam (3))
306 of the object pointed to by
309 .B POSIX_SPAWN_SETSCHEDULER
310 Set the scheduling policy algorithm and parameters of the child,
314 The scheduling policy is set to the value specified in the
319 .\" .BR posix_spawnattr_setpolicy (3))
320 of the object pointed to by
323 The scheduling parameters are set to the value specified in the
328 .\" .BR posix_spawnattr_setschedparam (3))
329 of the object pointed to by
334 .B POSIX_SPAWN_SETSCHEDPARAM
336 .B POSIX_SPAWN_SETSCHEDPOLICY
337 flags are not specified,
338 the child inherits the corresponding scheduling attributes from the parent.
341 .B POSIX_SPAWN_RESETIDS
343 reset the effective UID and GID to the
344 real UID and GID of the parent process.
345 If this flag is not set,
346 then the child retains the effective UID and GID of the parent.
347 In either case, if the set-user-ID and set-group-ID permission
348 bits are enabled on the executable file, their effect will override
349 the setting of the effective UID and GID (se
352 .B POSIX_SPAWN_SETPGROUP
353 Set the process group to the value specified in the
358 .\" .BR posix_spawnattr_setpgroup (3))
359 of the object pointed to by
363 attribute has the value 0,
364 the child's process group ID is made the same as its process ID.
366 .B POSIX_SPAWN_SETPGROUP
367 flag is not set, the child inherits the parent's process group ID.
369 .B POSIX_SPAWN_USEVFORK
370 Since glibc 2.24, this flag has no effect.
371 On older implementations, setting this flag forces the
379 feature test macro must be defined to obtain the definition of this constant.
381 .BR POSIX_SPAWN_SETSID " (since glibc 2.26)"
383 the child process shall create a new session and become the session leader.
384 The child process shall also become the process group leader of the new process
385 group in the session (see
389 feature test macro must be defined to obtain the definition of this constant.
390 .\" This flag has been accepted in POSIX, see:
391 .\" http://austingroupbugs.net/view.php?id=1044
392 .\" and has been implemented in glibc since version 2.26
393 .\" commit daeb1fa2e1b33323e719015f5f546988bd4cc73b
397 is NULL, then the default behaviors described above for each flag apply.
398 .\" mtk: I think we probably don't want to say the following, since it
399 .\" could lead people to do the wrong thing
400 .\" The POSIX standard tells you to call
401 .\" this function to de-initialize the attributes object pointed to by
403 .\" when you are done with it;
404 .\" however, on Linux systems this operation is a no-op.
408 argument specifies a sequence of file operations
409 that are performed in the child process after
410 the general processing described above, and before it performs the
414 is NULL, then no special action is taken, and standard
416 semantics apply\(emfile descriptors open before the exec
417 remain open in the new process,
418 except those for which the
421 File locks remain in place.
425 is not NULL, then it contains an ordered set of requests to
431 These requests are added to the
434 .BR posix_spawn_file_actions_addopen (3),
435 .BR posix_spawn_file_actions_addclose (3),
437 .BR posix_spawn_file_actions_adddup2 (3).
438 The requested operations are performed in the order they were added to
440 .\" FIXME . I think the following is best placed in the
441 .\" posix_spawn_file_actions_adddup2(3) page, and a similar statement is
442 .\" also needed in posix_spawn_file_actions_addclose(3)
443 .\" Note that you can specify file descriptors in
444 .\" .I posix_spawn_file_actions_adddup2 (3)
445 .\" which would not be usable if you called
447 .\" at that time--i.e., file descriptors that are opened or
448 .\" closed by the earlier operations
450 .\" .I file_actions .
452 If any of the housekeeping actions fails
453 (due to bogus values being passed or other reasons why signal handling,
454 process scheduling, process group ID functions,
455 and file descriptor operations might fail),
456 the child process exits with exit value 127.
458 Once the child has successfully forked and performed
459 all requested pre-exec steps,
460 the child runs the requested executable.
462 The child process takes its environment from the
464 argument, which is interpreted as if it had been passed to
466 The arguments to the created process come from the
468 argument, which is processed as for
471 Upon successful completion,
475 place the PID of the child process in
478 If there is an error during the
481 then no child is created,
485 and these functions return an error number as described below.
487 Even when these functions return a success status,
488 the child process may still fail for a plethora of reasons related to its
489 pre-\fBexec\fR() initialization.
493 In all of these cases, the child process will exit with the exit value of 127.
499 functions fail only in the case where the underlying
504 call fails; in these cases, these functions return an error number,
505 which will be one of the errors described for
511 In addition, these functions fail if:
514 Function not supported on this system.
520 functions are available since glibc 2.2.
522 POSIX.1-2001, POSIX.1-2008.
523 .\" FIXME . This piece belongs in spawnattr_setflags(3)
525 .\" .B POSIX_SPAWN_USEVFORK
526 .\" flag is a GNU extension; the
528 .\" feature test macro must be defined (before including any header files)
529 .\" to obtain the definition of this constant.
531 The housekeeping activities in the child are controlled by
532 the objects pointed to by
534 (for non-file actions) and
536 In POSIX parlance, the
539 .I posix_spawn_file_actions_t
540 data types are referred to as objects,
541 and their elements are not specified by name.
542 Portable programs should initialize these objects using
543 only the POSIX-specified functions.
545 although these objects may be implemented as structures containing fields,
546 portable programs must avoid dependence on such implementation details.)
548 According to POSIX, it is unspecified whether fork handlers established with
549 .BR pthread_atfork (3)
553 Since glibc 2.24, the fork handlers are not executed in any case.
554 .\" Tested on glibc 2.12
555 On older implementations,
556 fork handlers are called only if the child is created using
559 There is no "posix_fspawn" function (i.e., a function that is to
565 However, this functionality can be obtained by specifying the
567 argument as one of the files in the caller's
571 POSIX.1 says that when
572 .B POSIX_SPAWN_SETSCHEDULER
576 .B POSIX_SPAWN_SETSCHEDPARAM
577 (if present) is ignored.
578 However, before glibc 2.14, calls to
580 failed with an error if
581 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12052
582 .BR POSIX_SPAWN_SETSCHEDULER
583 was specified without also specifying
584 .BR POSIX_SPAWN_SETSCHEDPARAM .
586 The program below demonstrates the use of various functions in the
588 The program accepts command-line attributes that can be used
589 to create file actions and attributes objects.
590 The remaining command-line arguments are used as the executable name
591 and command-line arguments of the program that is executed in the child.
593 In the first run, the
595 command is executed in the child, and the
597 call employs no file actions or attributes objects.
603 Tue Feb 1 19:47:50 CEST 2011
604 Child status: exited, status=0
610 command-line option is used to create a file actions object that closes
611 standard output in the child.
614 fails when trying to perform output and exits with a status of 1.
618 $ \fB./a.out \-c date\fP
620 date: write error: Bad file descriptor
621 Child status: exited, status=1
627 command-line option is used to create an attributes object that
628 specifies that all (blockable) signals in the child should be blocked.
629 Consequently, trying to kill child with the default signal sent by
633 fails, because that signal is blocked.
634 Therefore, to kill the child,
642 $ \fB./a.out \-s sleep 60 &\fP
647 $ \fBkill \-KILL 7638\fP
648 $ Child status: killed by signal 9
649 [1]+ Done ./a.out \-s sleep 60
653 When we try to execute a nonexistent command in the child, the
655 fails and the child exits with a status of 127.
661 Child status: exited, status=127
676 #define errExit(msg) do { perror(msg); \e
677 exit(EXIT_FAILURE); } while (0)
679 #define errExitEN(en, msg) \e
680 do { errno = en; perror(msg); \e
681 exit(EXIT_FAILURE); } while (0)
686 main(int argc, char *argv[])
691 posix_spawnattr_t attr;
692 posix_spawnattr_t *attrp;
693 posix_spawn_file_actions_t file_actions;
694 posix_spawn_file_actions_t *file_actionsp;
696 /* Parse command\-line options, which can be used to specify an
697 attributes object and file actions object for the child. */
700 file_actionsp = NULL;
702 while ((opt = getopt(argc, argv, "sc")) != \-1) {
704 case \(aqc\(aq: /* \-c: close standard output in child */
706 /* Create a file actions object and add a "close"
709 s = posix_spawn_file_actions_init(&file_actions);
711 errExitEN(s, "posix_spawn_file_actions_init");
713 s = posix_spawn_file_actions_addclose(&file_actions,
716 errExitEN(s, "posix_spawn_file_actions_addclose");
718 file_actionsp = &file_actions;
721 case \(aqs\(aq: /* \-s: block all signals in child */
723 /* Create an attributes object and add a "set signal mask"
726 s = posix_spawnattr_init(&attr);
728 errExitEN(s, "posix_spawnattr_init");
729 s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
731 errExitEN(s, "posix_spawnattr_setflags");
734 s = posix_spawnattr_setsigmask(&attr, &mask);
736 errExitEN(s, "posix_spawnattr_setsigmask");
743 /* Spawn the child. The name of the program to execute and the
744 command\-line arguments are taken from the command\-line arguments
745 of this program. The environment of the program execed in the
746 child is made the same as the parent\(aqs environment. */
748 s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp,
749 &argv[optind], environ);
751 errExitEN(s, "posix_spawn");
753 /* Destroy any objects that we created earlier. */
756 s = posix_spawnattr_destroy(attrp);
758 errExitEN(s, "posix_spawnattr_destroy");
761 if (file_actionsp != NULL) {
762 s = posix_spawn_file_actions_destroy(file_actionsp);
764 errExitEN(s, "posix_spawn_file_actions_destroy");
767 printf("PID of child: %jd\en", (intmax_t) child_pid);
769 /* Monitor status of the child until it terminates. */
772 s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
776 printf("Child status: ");
777 if (WIFEXITED(status)) {
778 printf("exited, status=%d\en", WEXITSTATUS(status));
779 } else if (WIFSIGNALED(status)) {
780 printf("killed by signal %d\en", WTERMSIG(status));
781 } else if (WIFSTOPPED(status)) {
782 printf("stopped by signal %d\en", WSTOPSIG(status));
783 } else if (WIFCONTINUED(status)) {
784 printf("continued\en");
786 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
792 .nh \" Disable hyphenation
800 .BR sched_setparam (2),
801 .BR sched_setscheduler (2),
806 .BR posix_spawn_file_actions_addclose (3),
807 .BR posix_spawn_file_actions_adddup2 (3),
808 .BR posix_spawn_file_actions_addopen (3),
809 .BR posix_spawn_file_actions_destroy (3),
810 .BR posix_spawn_file_actions_init (3),
811 .BR posix_spawnattr_destroy (3),
812 .BR posix_spawnattr_getflags (3),
813 .BR posix_spawnattr_getpgroup (3),
814 .BR posix_spawnattr_getschedparam (3),
815 .BR posix_spawnattr_getschedpolicy (3),
816 .BR posix_spawnattr_getsigdefault (3),
817 .BR posix_spawnattr_getsigmask (3),
818 .BR posix_spawnattr_init (3),
819 .BR posix_spawnattr_setflags (3),
820 .BR posix_spawnattr_setpgroup (3),
821 .BR posix_spawnattr_setschedparam (3),
822 .BR posix_spawnattr_setschedpolicy (3),
823 .BR posix_spawnattr_setsigdefault (3),
824 .BR posix_spawnattr_setsigmask (3),
825 .BR pthread_atfork (3),
827 Base Definitions volume of POSIX.1-2001,
828 .I http://www.opengroup.org/unix/online.html