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 2019-03-06 "GNU" "Linux Programmer's Manual"
33 posix_spawn, posix_spawnp \- spawn a process
38 .BI "int posix_spawn(pid_t *" pid ", const char *" path ,
39 .BI " const posix_spawn_file_actions_t *" file_actions ,
40 .BI " const posix_spawnattr_t *" attrp ,
41 .BI " char *const " argv[] ", char *const " envp[] );
43 .BI "int posix_spawnp(pid_t *" pid ", const char *" file ,
44 .BI " const posix_spawn_file_actions_t *" file_actions ,
45 .BI " const posix_spawnattr_t *" attrp ,
46 .BI " char *const " argv[] ", char *const " envp[] );
53 functions are used to create a new child process that executes
55 These functions were specified by POSIX to provide a standardized method
56 of creating new processes on machines that lack the capability
60 These machines are generally small, embedded systems lacking MMU support.
66 functions provide the functionality of a combined
70 with some optional housekeeping steps in the child process before the
72 These functions are not meant to replace the
77 In fact, they provide only a subset of the functionality
78 that can be achieved by using the system calls.
80 The only difference between
84 is the manner in which they specify the file to be executed by
88 the executable file is specified as a pathname
89 (which can be absolute or relative).
92 the executable file is specified as a simple filename;
93 the system searches for this file in the list of directories specified by
95 (in the same way as for
97 For the remainder of this page, the discussion is phrased in terms of
99 with the understanding that
101 differs only on the point just described.
103 The remaining arguments to these two functions are as follows:
107 argument points to a buffer that is used to return the process ID
108 of the new child process.
113 .I "spawn file actions object"
114 that specifies file-related actions to be performed in the child
120 This object is initialized and populated before the
123 .BR posix_spawn_file_actions_init (3)
125 .BR posix_spawn_file_actions_* ()
130 argument points to an
131 .I attributes objects
132 that specifies various attributes of the created child process.
133 This object is initialized and populated before the
136 .BR posix_spawnattr_init (3)
138 .BR posix_spawnattr_* ()
145 arguments specify the argument list and environment for the program
146 that is executed in the child process, as for
149 Below, the functions are described in terms of a three-step process: the
153 step (executed in the child),
156 step (executed in the child).
160 function commences by calling
166 The PID of the new child process is placed in
170 function then returns control to the parent process.
172 Subsequently, the parent can use one of the system calls described in
174 to check the status of the child process.
175 If the child fails in any of the housekeeping steps described below,
176 or fails to execute the desired file,
177 it exits with a status of 127.
179 The child process is created using
183 when either of the following is true:
187 element of the attributes object pointed to by
189 contains the GNU-specific flag
190 .BR POSIX_SPAWN_USEVFORK ;
196 element of the attributes object pointed to by
198 does \fInot\fP contain
199 .BR POSIX_SPAWN_SETSIGMASK ,
200 .BR POSIX_SPAWN_SETSIGDEF ,
201 .BR POSIX_SPAWN_SETSCHEDPARAM ,
202 .BR POSIX_SPAWN_SETSCHEDULER ,
203 .BR POSIX_SPAWN_SETPGROUP ,
205 .BR POSIX_SPAWN_RESETIDS .
209 is used if the caller requests it,
210 or if there is no cleanup expected in the child before it
214 .SS pre-exec() step: housekeeping
219 a child process may need to perform a set of housekeeping actions.
224 functions support a small, well-defined set of system tasks that the child
225 process can accomplish before it executes the executable file.
226 These operations are controlled by the attributes object pointed to by
228 and the file actions object pointed to by
230 In the child, processing is done in the following sequence:
232 Process attribute actions: signal mask, signal default handlers,
233 scheduling algorithm and parameters,
234 process group, and effective user and group IDs
235 are changed as specified by the attributes object pointed to by
238 File actions, as specified in the
241 are performed in the order that they were specified using calls to the
242 .BR posix_spawn_file_actions_add* ()
245 File descriptors with the
249 All process attributes in the child,
250 other than those affected by attributes specified in the
253 and the file actions in the object pointed to by
255 will be affected as though the child was created with
257 and it executed the program with
260 The process attributes actions are defined by the attributes object
266 .BR posix_spawnattr_setflags (3))
267 controls the general actions that occur,
268 and other attributes in the object specify values
269 to be used during those actions.
271 The effects of the flags that may be specified in
275 .B POSIX_SPAWN_SETSIGMASK
276 Set the signal mask to the signal set specified in the
281 .\" .BR posix_spawnattr_setsigmask (3))
282 of the object pointed to by
285 .B POSIX_SPAWN_SETSIGMASK
286 flag is not set, then the child inherits the parent's signal mask.
288 .B POSIX_SPAWN_SETSIGDEF
289 Reset the disposition of all signals in the set specified in the
294 .\" .BR posix_spawnattr_setsigdefault (3))
295 of the object pointed to by
298 For the treatment of the dispositions of signals not specified in the
300 attribute, or the treatment when
301 .B POSIX_SPAWN_SETSIGDEF
302 is not specified, see
305 .B POSIX_SPAWN_SETSCHEDPARAM
306 .\" (POSIX_PRIORITY_SCHEDULING only)
307 If this flag is set, and the
308 .B POSIX_SPAWN_SETSCHEDULER
309 flag is not set, then set the scheduling parameters
310 to the parameters specified in the
315 .\" .BR posix_spawnattr_setschedparam (3))
316 of the object pointed to by
319 .B POSIX_SPAWN_SETSCHEDULER
320 Set the scheduling policy algorithm and parameters of the child,
324 The scheduling policy is set to the value specified in the
329 .\" .BR posix_spawnattr_setpolicy (3))
330 of the object pointed to by
333 The scheduling parameters are set to the value specified in the
338 .\" .BR posix_spawnattr_setschedparam (3))
339 of the object pointed to by
344 .B POSIX_SPAWN_SETSCHEDPARAM
346 .B POSIX_SPAWN_SETSCHEDPOLICY
347 flags are not specified,
348 the child inherits the corresponding scheduling attributes from the parent.
351 .B POSIX_SPAWN_RESETIDS
353 reset the effective UID and GID to the
354 real UID and GID of the parent process.
355 If this flag is not set,
356 then the child retains the effective UID and GID of the parent.
357 In either case, if the set-user-ID and set-group-ID permission
358 bits are enabled on the executable file, their effect will override
359 the setting of the effective UID and GID (se
362 .B POSIX_SPAWN_SETPGROUP
363 Set the process group to the value specified in the
368 .\" .BR posix_spawnattr_setpgroup (3))
369 of the object pointed to by
373 attribute has the value 0,
374 the child's process group ID is made the same as its process ID.
376 .B POSIX_SPAWN_SETPGROUP
377 flag is not set, the child inherits the parent's process group ID.
381 is NULL, then the default behaviors described above for each flag apply.
382 .\" mtk: I think we probably don't want to say the following, since it
383 .\" could lead people to do the wrong thing
384 .\" The POSIX standard tells you to call
385 .\" this function to de-initialize the attributes object pointed to by
387 .\" when you are done with it;
388 .\" however, on Linux systems this operation is a no-op.
392 argument specifies a sequence of file operations
393 that are performed in the child process after
394 the general processing described above, and before it performs the
398 is NULL, then no special action is taken, and standard
400 semantics apply--file descriptors open before the exec
401 remain open in the new process,
402 except those for which the
405 File locks remain in place.
409 is not NULL, then it contains an ordered set of requests to
415 These requests are added to the
418 .BR posix_spawn_file_actions_addopen (3),
419 .BR posix_spawn_file_actions_addclose (3),
421 .BR posix_spawn_file_actions_adddup2 (3).
422 The requested operations are performed in the order they were added to
424 .\" FIXME . I think the following is best placed in the
425 .\" posix_spawn_file_actions_adddup2(3) page, and a similar statement is
426 .\" also needed in posix_spawn_file_actions_addclose(3)
427 .\" Note that you can specify file descriptors in
428 .\" .I posix_spawn_file_actions_adddup2 (3)
429 .\" which would not be usable if you called
431 .\" at that time--i.e., file descriptors that are opened or
432 .\" closed by the earlier operations
434 .\" .I file_actions .
436 If any of the housekeeping actions fails
437 (due to bogus values being passed or other reasons why signal handling,
438 process scheduling, process group ID functions,
439 and file descriptor operations might fail),
440 the child process exits with exit value 127.
442 Once the child has successfully forked and performed
443 all requested pre-exec steps,
444 the child runs the requested executable.
446 The child process takes its environment from the
448 argument, which is interpreted as if it had been passed to
450 The arguments to the created process come from the
452 argument, which is processed as for
455 Upon successful completion,
459 place the PID of the child process in
462 If there is an error before or during the
464 then no child is created,
468 and these functions return an error number as described below.
470 Even when these functions return a success status,
471 the child process may still fail for a plethora of reasons related to its
472 pre-\fBexec\fR() initialization.
476 In all of these cases, the child process will exit with the exit value of 127.
482 functions fail only in the case where the underlying
486 call fails; in these cases, these functions return an error number,
487 which will be one of the errors described for
492 In addition, these functions fail if:
495 Function not supported on this system.
501 functions are available since glibc 2.2.
504 POSIX.1-2001, POSIX.1-2008.
505 .\" FIXME . This piece belongs in spawnattr_setflags(3)
507 .\" .B POSIX_SPAWN_USEVFORK
508 .\" flag is a GNU extension; the
510 .\" feature test macro must be defined (before including any header files)
511 .\" to obtain the definition of this constant.
513 The housekeeping activities in the child are controlled by
514 the objects pointed to by
516 (for non-file actions) and
518 In POSIX parlance, the
521 .I posix_spawn_file_actions_t
522 data types are referred to as objects,
523 and their elements are not specified by name.
524 Portable programs should initialize these objects using
525 only the POSIX-specified functions.
527 although these objects may be implemented as structures containing fields,
528 portable programs must avoid dependence on such implementation details.)
530 According to POSIX, it unspecified whether fork handlers established with
531 .BR pthread_atfork (3)
536 .\" Tested on glibc 2.12
537 fork handlers are called only if the child is created using
540 There is no "posix_fspawn" function (i.e., a function that is to
546 However, this functionality can be obtained by specifying the
548 argument as one of the files in the caller's
552 POSIX.1 says that when
553 .B POSIX_SPAWN_SETSCHEDULER
557 .B POSIX_SPAWN_SETSCHEDPARAM
558 (if present) is ignored.
559 However, before glibc 2.14, calls to
561 failed with an error if
562 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12052
563 .BR POSIX_SPAWN_SETSCHEDULER
564 was specified without also specifying
565 .BR POSIX_SPAWN_SETSCHEDPARAM .
567 The program below demonstrates the use of various functions in the
569 The program accepts command-line attributes that can be used
570 to create file actions and attributes objects.
571 The remaining command-line arguments are used as the executable name
572 and command-line arguments of the program that is executed in the child.
574 In the first run, the
576 command is executed in the child, and the
578 call employs no file actions or attributes objects.
584 Tue Feb 1 19:47:50 CEST 2011
585 Child status: exited, status=0
591 command-line option is used to create a file actions object that closes
592 standard output in the child.
595 fails when trying to perform output and exits with a status of 1.
599 $ \fB./a.out -c date\fP
601 date: write error: Bad file descriptor
602 Child status: exited, status=1
608 command-line option is used to create an attributes object that
609 specifies that all (blockable) signals in the child should be blocked.
610 Consequently, trying to kill child with the default signal sent by
614 fails, because that signal is blocked.
615 Therefore, to kill the child,
623 $ \fB./a.out -s sleep 60 &\fP
628 $ \fBkill -KILL 7638\fP
629 $ Child status: killed by signal 9
630 [1]+ Done ./a.out -s sleep 60
634 When we try to execute a nonexistent command in the child, the
636 fails and the child exits with a status of 127.
642 Child status: exited, status=127
656 #define errExit(msg) do { perror(msg); \e
657 exit(EXIT_FAILURE); } while (0)
659 #define errExitEN(en, msg) \e
660 do { errno = en; perror(msg); \e
661 exit(EXIT_FAILURE); } while (0)
666 main(int argc, char *argv[])
671 posix_spawnattr_t attr;
672 posix_spawnattr_t *attrp;
673 posix_spawn_file_actions_t file_actions;
674 posix_spawn_file_actions_t *file_actionsp;
676 /* Parse command\-line options, which can be used to specify an
677 attributes object and file actions object for the child. */
680 file_actionsp = NULL;
682 while ((opt = getopt(argc, argv, "sc")) != \-1) {
684 case \(aqc\(aq: /* \-c: close standard output in child */
686 /* Create a file actions object and add a "close"
689 s = posix_spawn_file_actions_init(&file_actions);
691 errExitEN(s, "posix_spawn_file_actions_init");
693 s = posix_spawn_file_actions_addclose(&file_actions,
696 errExitEN(s, "posix_spawn_file_actions_addclose");
698 file_actionsp = &file_actions;
701 case \(aqs\(aq: /* \-s: block all signals in child */
703 /* Create an attributes object and add a "set signal mask"
706 s = posix_spawnattr_init(&attr);
708 errExitEN(s, "posix_spawnattr_init");
709 s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
711 errExitEN(s, "posix_spawnattr_setflags");
714 s = posix_spawnattr_setsigmask(&attr, &mask);
716 errExitEN(s, "posix_spawnattr_setsigmask");
723 /* Spawn the child. The name of the program to execute and the
724 command\-line arguments are taken from the command\-line arguments
725 of this program. The environment of the program execed in the
726 child is made the same as the parent\(aqs environment. */
728 s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp,
729 &argv[optind], environ);
731 errExitEN(s, "posix_spawn");
733 /* Destroy any objects that we created earlier */
736 s = posix_spawnattr_destroy(attrp);
738 errExitEN(s, "posix_spawnattr_destroy");
741 if (file_actionsp != NULL) {
742 s = posix_spawn_file_actions_destroy(file_actionsp);
744 errExitEN(s, "posix_spawn_file_actions_destroy");
747 printf("PID of child: %ld\en", (long) child_pid);
749 /* Monitor status of the child until it terminates */
752 s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
756 printf("Child status: ");
757 if (WIFEXITED(status)) {
758 printf("exited, status=%d\en", WEXITSTATUS(status));
759 } else if (WIFSIGNALED(status)) {
760 printf("killed by signal %d\en", WTERMSIG(status));
761 } else if (WIFSTOPPED(status)) {
762 printf("stopped by signal %d\en", WSTOPSIG(status));
763 } else if (WIFCONTINUED(status)) {
764 printf("continued\en");
766 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
772 .nh \" Disable hyphenation
780 .BR sched_setparam (2),
781 .BR sched_setscheduler (2),
786 .BR posix_spawn_file_actions_addclose (3),
787 .BR posix_spawn_file_actions_adddup2 (3),
788 .BR posix_spawn_file_actions_addopen (3),
789 .BR posix_spawn_file_actions_destroy (3),
790 .BR posix_spawn_file_actions_init (3),
791 .BR posix_spawnattr_destroy (3),
792 .BR posix_spawnattr_getflags (3),
793 .BR posix_spawnattr_getpgroup (3),
794 .BR posix_spawnattr_getschedparam (3),
795 .BR posix_spawnattr_getschedpolicy (3),
796 .BR posix_spawnattr_getsigdefault (3),
797 .BR posix_spawnattr_getsigmask (3),
798 .BR posix_spawnattr_init (3),
799 .BR posix_spawnattr_setflags (3),
800 .BR posix_spawnattr_setpgroup (3),
801 .BR posix_spawnattr_setschedparam (3),
802 .BR posix_spawnattr_setschedpolicy (3),
803 .BR posix_spawnattr_setsigdefault (3),
804 .BR posix_spawnattr_setsigmask (3),
805 .BR pthread_atfork (3),
807 Base Definitions volume of POSIX.1-2001,
808 .I http://www.opengroup.org/unix/online.html