]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/posix_spawn.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / posix_spawn.3
CommitLineData
a5458d49 1.\" Copyright (c) 2009 Bill O. Gallmeister (bgallmeister@gmail.com)
10069ed0 2.\" and Copyright 2010 Michael Kerrisk <mtk.manpages@gmail.com>
a5458d49 3.\"
68df07dd 4.\" %%%LICENSE_START(VERBATIM)
a5458d49
BG
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.
8.\"
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.
13.\"
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
20.\" professionally.
21.\"
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
68df07dd 24.\" %%%LICENSE_END
a5458d49
BG
25.\"
26.\" References consulted:
27.\" Linux glibc source code
10069ed0
MK
28.\" POSIX 1003.1-2004 documentation
29.\" (http://www.opengroup.org/onlinepubs/009695399)
a5458d49 30.\"
4b8c67d9 31.TH POSIX_SPAWN 3 2017-09-15 "GNU" "Linux Programmer's Manual"
a5458d49
BG
32.SH NAME
33posix_spawn, posix_spawnp \- spawn a process
34.SH SYNOPSIS
35.nf
36.B #include <spawn.h>
f90f031e 37.PP
a5458d49
BG
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[] );
f90f031e 42.PP
a5458d49
BG
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[] );
47.fi
a5458d49 48.SH DESCRIPTION
10069ed0
MK
49The
50.BR posix_spawn ()
a5458d49
BG
51and
52.BR posix_spawnp ()
10069ed0
MK
53functions are used to create a new child process that executes
54a specified file.
55These functions were specified by POSIX to provide a standardized method
56of creating new processes on machines that lack the capability
a5458d49 57to support the
10069ed0 58.BR fork (2)
a5458d49
BG
59system call.
60These machines are generally small, embedded systems lacking MMU support.
847e0d88 61.PP
10069ed0
MK
62The
63.BR posix_spawn ()
a5458d49
BG
64and
65.BR posix_spawnp ()
10069ed0
MK
66functions provide the functionality of a combined
67.BR fork (2)
a5458d49 68and
10069ed0 69.BR exec (3),
a5458d49 70with some optional housekeeping steps in the child process before the
10069ed0
MK
71.BR exec (3).
72These functions are not meant to replace the
73.BR fork (2)
74and
75.BR execve (2)
76system calls.
77In fact, they provide only a subset of the functionality
78that can be achieved by using the system calls.
847e0d88 79.PP
10069ed0
MK
80The only difference between
81.BR posix_spawn ()
82and
83.BR posix_spawnp ()
84is the manner in which they specify the file to be executed by
85the child process.
86With
87.BR posix_spawn (),
88the executable file is specified as a pathname
89(which can be absolute or relative).
90With
91.BR posix_spawnp (),
92the executable file is specified as a simple filename;
93the system searches for this file in the list of directories specified by
94.BR PATH
95(in the same way as for
96.BR execvp (3)).
97For the remainder of this page, the discussion is phrased in terms of
98.BR posix_spawn (),
99with the understanding that
cf3ecd87 100.BR posix_spawnp ()
10069ed0 101differs only on the point just described.
847e0d88 102.PP
10069ed0
MK
103The remaining arguments to these two functions are as follows:
104.IP * 3
105The
a5458d49 106.I pid
10069ed0
MK
107argument points to a buffer that is used to return the process ID
108of the new child process.
109.IP *
110The
111.I file_actions
112argument points to a
113.I "spawn file actions object"
114that specifies file-related actions to be performed in the child
115between the
116.BR fork (2)
117and
118.BR exec (3)
119steps.
120This object is initialized and populated before the
121.BR posix_spawn ()
122call using
123.BR posix_spawn_file_actions_init (3)
124and the
125.BR posix_spawn_file_actions_* ()
126functions.
a5458d49 127.IP *
10069ed0
MK
128The
129.I attrp
130argument points to an
131.I attributes objects
132that specifies various attributes of the created child process.
133This object is initialized and populated before the
134.BR posix_spawn ()
135call using
136.BR posix_spawnattr_init (3)
137and the
138.BR posix_spawnattr_* ()
139functions.
140.IP *
141The
142.I argv
143and
144.I envp
145arguments specify the argument list and environment for the program
146that is executed in the child process, as for
147.BR execve (2).
148.PP
149Below, the functions are described in terms of a three-step process: the
150.BR fork()
151step, the
152.RB pre- exec ()
153step (executed in the child),
154and the
155.BR exec ()
156step (executed in the child).
157.SS fork() step
158The
159.BR posix_spawn ()
160function commences by calling
161.BR fork (2),
162or possibly
163.BR vfork (2)
164(see below).
847e0d88 165.PP
10069ed0
MK
166The PID of the new child process is placed in
167.IR *pid .
168The
169.BR posix_spawn ()
170function then returns control to the parent process.
847e0d88 171.PP
10069ed0
MK
172Subsequently, the parent can use one of the system calls described in
173.BR wait (2)
174to check the status of the child process.
175If the child fails in any of the housekeeping steps described below,
176or fails to execute the desired file,
177it exits with a status of 127.
847e0d88 178.PP
10069ed0
MK
179The child process is created using
180.BR vfork (2)
181instead of
182.BR fork (2)
183when either of the following is true:
184.IP * 3
a5458d49
BG
185the
186.I spawn-flags
10069ed0
MK
187element of the attributes object pointed to by
188.I attrp
189contains the GNU-specific flag
190.BR POSIX_SPAWN_USEVFORK ;
191or
a5458d49
BG
192.IP *
193.I file_actions
10069ed0
MK
194is NULL and the
195.I spawn-flags
196element of the attributes object pointed to by
197.I attrp
198does \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 ,
204or
205.BR POSIX_SPAWN_RESETIDS .
dd3568a1 206.PP
10069ed0
MK
207In other words,
208.BR vfork (2)
209is used if the caller requests it,
210or if there is no cleanup expected in the child before it
211.BR exec (3)s
a5458d49 212the requested file.
847e0d88 213.PP
10069ed0 214.SS pre-exec() step: housekeeping
a5458d49 215In between the
10069ed0 216.BR fork (2)
a5458d49 217and the
10069ed0
MK
218.BR exec (3),
219a child process may need to perform a set of housekeeping actions.
220The
221.BR posix_spawn ()
a5458d49
BG
222and
223.BR posix_spawnp ()
10069ed0
MK
224functions support a small, well-defined set of system tasks that the child
225process can accomplish before it executes the executable file.
226These operations are controlled by the attributes object pointed to by
227.IR attrp
228and the file actions object pointed to by
82b43a76 229.IR file_actions .
a5458d49 230In the child, processing is done in the following sequence:
10069ed0
MK
231.IP 1. 3
232Process attribute actions: signal mask, signal default handlers,
233scheduling algorithm and parameters,
234process group, and effective user and group IDs
235are changed as specified by the attributes object pointed to by
236.IR attrp .
237.IP 2.
a5458d49
BG
238File actions, as specified in the
239.I file_actions
10069ed0
MK
240argument,
241are performed in the order that they were specified using calls to the
242.BR posix_spawn_file_actions_add* ()
243functions.
244.IP 3.
245File descriptors with the
246.B FD_CLOEXEC
247flag set are closed.
dd3568a1 248.PP
10069ed0
MK
249All process attributes in the child,
250other than those affected by attributes specified in the
251object pointed to by
252.IR attrp
253and the file actions in the object pointed to by
254.IR file_actions ,
255will be affected as though the child was created with
256.BR fork (2)
257and it executed the program with
258.BR execve (2).
847e0d88 259.PP
10069ed0
MK
260The process attributes actions are defined by the attributes object
261pointed to by
262.IR attrp .
263The
a5458d49 264.I spawn-flags
10069ed0
MK
265attribute (set using
266.BR posix_spawnattr_setflags (3))
267controls the general actions that occur,
268and other attributes in the object specify values
269to be used during those actions.
dd3568a1 270.PP
10069ed0
MK
271The effects of the flags that may be specified in
272.IR spawn-flags
273are as follows:
274.TP 8
275.B POSIX_SPAWN_SETSIGMASK
276Set the signal mask to the signal set specified in the
a5458d49 277.I spawn-sigmask
82b43a76
MK
278attribute
279.\" FIXME .
280.\" (see
281.\" .BR posix_spawnattr_setsigmask (3))
10069ed0
MK
282of the object pointed to by
283.IR attrp .
284If the
285.B POSIX_SPAWN_SETSIGMASK
286flag is not set, then the child inherits the parent's signal mask.
287.TP
288.B POSIX_SPAWN_SETSIGDEF
289Reset the disposition of all signals in the set specified in the
a5458d49 290.I spawn-sigdefault
82b43a76
MK
291attribute
292.\" FIXME .
293.\" (see
294.\" .BR posix_spawnattr_setsigdefault (3))
10069ed0
MK
295of the object pointed to by
296.IR attrp
297to the default.
298For the treatment of the dispositions of signals not specified in the
299.I spawn-sigdefault
300attribute, or the treatment when
301.B POSIX_SPAWN_SETSIGDEF
302is not specified, see
303.BR execve (2).
304.TP
305.B POSIX_SPAWN_SETSCHEDPARAM
306.\" (POSIX_PRIORITY_SCHEDULING only)
307If this flag is set, and the
308.B POSIX_SPAWN_SETSCHEDULER
309flag is not set, then set the scheduling parameters
a5458d49
BG
310to the parameters specified in the
311.I spawn-schedparam
82b43a76
MK
312attribute
313.\" FIXME .
314.\" (see
315.\" .BR posix_spawnattr_setschedparam (3))
10069ed0
MK
316of the object pointed to by
317.IR attrp .
318.TP
319.B POSIX_SPAWN_SETSCHEDULER
320Set the scheduling policy algorithm and parameters of the child,
321as follows:
322.RS
323.IP * 3
324The scheduling policy is set to the value specified in the
a5458d49 325.I spawn-schedpolicy
82b43a76
MK
326attribute
327.\" FIXME .
328.\" (see
329.\" .BR posix_spawnattr_setpolicy (3))
10069ed0
MK
330of the object pointed to by
331.IR attrp .
10069ed0 332.IP *
82b43a76 333The scheduling parameters are set to the value specified in the
10069ed0 334.I spawn-schedparam
82b43a76
MK
335attribute
336.\" FIXME .
337.\" (see
338.\" .BR posix_spawnattr_setschedparam (3))
10069ed0 339of the object pointed to by
82b43a76
MK
340.IR attrp
341(but see BUGS).
dd3568a1 342.PP
10069ed0
MK
343If the
344.B POSIX_SPAWN_SETSCHEDPARAM
a5458d49 345and
10069ed0
MK
346.B POSIX_SPAWN_SETSCHEDPOLICY
347flags are not specified,
348the child inherits the corresponding scheduling attributes from the parent.
10069ed0
MK
349.RE
350.TP
351.B POSIX_SPAWN_RESETIDS
352If this flag is set,
353reset the effective UID and GID to the
354real UID and GID of the parent process.
355If this flag is not set,
356then the child retains the effective UID and GID of the parent.
357In either case, if the set-user-ID and set-group-ID permission
358bits are enabled on the executable file, their effect will override
359the setting of the effective UID and GID (se
360.BR execve (2)).
361.TP
362.B POSIX_SPAWN_SETPGROUP
363Set the process group to the value specified in the
364.I spawn-pgroup
82b43a76
MK
365attribute
366.\" FIXME .
367.\" (see
368.\" .BR posix_spawnattr_setpgroup (3))
10069ed0
MK
369of the object pointed to by
370.IR attrp .
371If the
372.I spawn-pgroup
373attribute has the value 0,
374the child's process group ID is made the same as its process ID.
375If the
376.B POSIX_SPAWN_SETPGROUP
377flag is not set, the child inherits the parent's process group ID.
dd3568a1 378.PP
10069ed0
MK
379If
380.I attrp
381is 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
386.\" .I attrp
387.\" when you are done with it;
388.\" however, on Linux systems this operation is a no-op.
847e0d88 389.PP
10069ed0 390The
a5458d49 391.I file_actions
10069ed0
MK
392argument specifies a sequence of file operations
393that are performed in the child process after
394the general processing described above, and before it performs the
395.BR exec (3).
a5458d49
BG
396If
397.I file_actions
398is NULL, then no special action is taken, and standard
10069ed0
MK
399.BR exec (3)
400semantics apply--file descriptors open before the exec
401remain open in the new process,
402except those for which the
403.B FD_CLOEXEC
404flag has been set.
405File locks remain in place.
dd3568a1 406.PP
a5458d49
BG
407If
408.I file_actions
10069ed0
MK
409is not NULL, then it contains an ordered set of requests to
410.BR open (2),
411.BR close (2),
a5458d49 412and
10069ed0 413.BR dup2 (2)
a5458d49
BG
414files.
415These requests are added to the
416.I file_actions
417by
10069ed0
MK
418.BR posix_spawn_file_actions_addopen (3),
419.BR posix_spawn_file_actions_addclose (3),
a5458d49 420and
10069ed0 421.BR posix_spawn_file_actions_adddup2 (3).
a5458d49 422The requested operations are performed in the order they were added to
10069ed0 423.IR file_actions .
82b43a76 424.\" FIXME . I think the following is best placed in the
10069ed0
MK
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
430.\" .BR dup2 (2)
431.\" at that time--i.e., file descriptors that are opened or
432.\" closed by the earlier operations
433.\" added to
434.\" .I file_actions .
847e0d88 435.PP
10069ed0
MK
436If any of the housekeeping actions fails
437(due to bogus values being passed or other reasons why signal handling,
438process scheduling, process group ID functions,
439and file descriptor operations might fail),
440the child process exits with exit value 127.
441.SS exec() step
442Once the child has successfully forked and performed
443all requested pre-exec steps,
a5458d49 444the child runs the requested executable.
dd3568a1 445.PP
a5458d49
BG
446The child process takes its environment from the
447.I envp
10069ed0
MK
448argument, which is interpreted as if it had been passed to
449.BR execve (2).
a5458d49
BG
450The arguments to the created process come from the
451.I argv
10069ed0
MK
452argument, which is processed as for
453.BR execve (2).
a5458d49 454.SH RETURN VALUE
a5458d49
BG
455Upon successful completion,
456.BR posix_spawn ()
457and
458.BR posix_spawnp ()
10069ed0
MK
459place the PID of the child process in
460.IR pid ,
a5458d49 461and return 0.
10069ed0
MK
462If there is an error before or during the
463.BR fork (2),
464then no child is created,
465the contents of
466.IR *pid
467are unspecified,
468and these functions return an error number as described below.
847e0d88 469.PP
10069ed0
MK
470Even when these functions return a success status,
471the child process may still fail for a plethora of reasons related to its
472pre-\fBexec\fR() initialization.
473In addition, the
474.BR exec (3)
475may fail.
476In all of these cases, the child process will exit with the exit value of 127.
a5458d49 477.SH ERRORS
10069ed0 478The
a5458d49
BG
479.BR posix_spawn ()
480and
481.BR posix_spawnp ()
10069ed0
MK
482functions fail only in the case where the underlying
483.BR fork (2)
a5458d49 484or
10069ed0
MK
485.BR vfork (2)
486call fails; in these cases, these functions return an error number,
487which will be one of the errors described for
488.BR fork (2)
a5458d49 489or
10069ed0 490.BR vfork (2).
dd3568a1 491.PP
10069ed0 492In addition, these functions fail if:
a5458d49
BG
493.TP
494.B ENOSYS
495Function not supported on this system.
9c78ec5b
MK
496.SH VERSIONS
497The
498.BR posix_spawn ()
499and
500.BR posix_spawnp ()
501functions are available since glibc 2.2.
502.SH CONFORMING TO
dd3568a1 503.PP
9c78ec5b
MK
504POSIX.1-2001, POSIX.1-2008.
505.\" FIXME . This piece belongs in spawnattr_setflags(3)
506.\" The
507.\" .B POSIX_SPAWN_USEVFORK
508.\" flag is a GNU extension; the
509.\" .B _GNU_SOURCE
510.\" feature test macro must be defined (before including any header files)
511.\" to obtain the definition of this constant.
a5458d49 512.SH NOTES
10069ed0
MK
513The housekeeping activities in the child are controlled by
514the objects pointed to by
515.I attrp
516(for non-file actions) and
517.I file_actions
518In POSIX parlance, the
a5458d49 519.I posix_spawnattr_t
10069ed0
MK
520and
521.I posix_spawn_file_actions_t
522data types are referred to as objects,
523and their elements are not specified by name.
524Portable programs should initialize these objects using
525only the POSIX-specified functions.
526(In other words,
527although these objects may be implemented as structures containing fields,
528portable programs must avoid dependence on such implementation details.)
847e0d88 529.PP
10069ed0
MK
530According to POSIX, it unspecified whether fork handlers established with
531.BR pthread_atfork (3)
532are called when
533.BR posix_spawn ()
534is invoked.
535On glibc,
536.\" Tested on glibc 2.12
537fork handlers are called only if the child is created using
538.BR fork (2).
847e0d88 539.PP
53562047
MK
540There is no "posix_fspawn" function (i.e., a function that is to
541.BR posix_spawn ()
542as
543.BR fexecve (3)
544is to
545.BR execve (2)).
546However, this functionality can be obtained by specifying the
547.I path
548argument as one of the files in the caller's
549.IR /proc/self/fd
550directory.
82b43a76
MK
551.SH BUGS
552POSIX.1 says that when
553.B POSIX_SPAWN_SETSCHEDULER
554is specified in
555.IR spawn-flags ,
556then the
557.B POSIX_SPAWN_SETSCHEDPARAM
558(if present) is ignored.
559However, before glibc 2.14, calls to
560.BR posix_spawn ()
561failed with an error if
562.\" http://sourceware.org/bugzilla/show_bug.cgi?id=12052
563.BR POSIX_SPAWN_SETSCHEDULER
564was specified without also specifying
565.BR POSIX_SPAWN_SETSCHEDPARAM .
10069ed0
MK
566.SH EXAMPLE
567The program below demonstrates the use of various functions in the
568POSIX spawn API.
569The program accepts command-line attributes that can be used
570to create file actions and attributes objects.
571The remaining command-line arguments are used as the executable name
572and command-line arguments of the program that is executed in the child.
847e0d88 573.PP
10069ed0
MK
574In the first run, the
575.BR date (1)
576command is executed in the child, and the
577.BR posix_spawn ()
578call employs no file actions or attributes objects.
bdd915e2 579.PP
10069ed0 580.in +4
bdd915e2 581.EX
10069ed0
MK
582$ \fB./a.out date\fP
583PID of child: 7634
584Tue Feb 1 19:47:50 CEST 2011
585Child status: exited, status=0
bdd915e2 586.EE
10069ed0 587.in
bdd915e2 588.PP
10069ed0
MK
589In the next run, the
590.I \-c
591command-line option is used to create a file actions object that closes
592standard output in the child.
593Consequently,
594.BR date (1)
595fails when trying to perform output and exits with a status of 1.
bdd915e2 596.PP
10069ed0 597.in +4
bdd915e2 598.EX
10069ed0
MK
599$ \fB./a.out -c date\fP
600PID of child: 7636
601date: write error: Bad file descriptor
602Child status: exited, status=1
bdd915e2 603.EE
10069ed0 604.in
bdd915e2 605.PP
10069ed0
MK
606In the next run, the
607.I \-s
608command-line option is used to create an attributes object that
609specifies that all (blockable) signals in the child should be blocked.
610Consequently, trying to kill child with the default signal sent by
611.BR kill (1)
612(i.e.,
613.BR SIGTERM )
614fails, because that signal is blocked.
615Therefore, to kill the child,
616.BR SIGKILL
617is necessary
618.RB ( SIGKILL
619can't be blocked).
bdd915e2 620.PP
10069ed0 621.in +4
bdd915e2 622.EX
10069ed0
MK
623$ \fB./a.out -s sleep 60 &\fP
624[1] 7637
625$ PID of child: 7638
626
627$ \fBkill 7638\fP
628$ \fBkill -KILL 7638\fP
629$ Child status: killed by signal 9
630[1]+ Done ./a.out -s sleep 60
bdd915e2 631.EE
10069ed0 632.in
bdd915e2 633.PP
10069ed0
MK
634When we try to execute a nonexistent command in the child, the
635.BR exec (3)
636fails and the child exits with a status of 127.
bdd915e2 637.PP
10069ed0 638.in +4
bdd915e2 639.EX
10069ed0
MK
640$ \fB./a.out xxxxx
641PID of child: 10190
642Child status: exited, status=127
bdd915e2 643.EE
10069ed0 644.in
7b5e3ad5 645.SS Program source
c7885256 646\&
e7d0bb47 647.EX
10069ed0
MK
648#include <spawn.h>
649#include <stdio.h>
650#include <unistd.h>
651#include <stdlib.h>
652#include <string.h>
653#include <wait.h>
654#include <errno.h>
655
656#define errExit(msg) do { perror(msg); \\
657 exit(EXIT_FAILURE); } while (0)
658
659#define errExitEN(en, msg) \\
660 do { errno = en; perror(msg); \\
661 exit(EXIT_FAILURE); } while (0)
662
663char **environ;
664
665int
666main(int argc, char *argv[])
667{
668 pid_t child_pid;
669 int s, opt, status;
670 sigset_t mask;
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;
675
676 /* Parse command\-line options, which can be used to specify an
677 attributes object and file actions object for the child. */
678
679 attrp = NULL;
680 file_actionsp = NULL;
681
682 while ((opt = getopt(argc, argv, "sc")) != \-1) {
683 switch (opt) {
684 case \(aqc\(aq: /* \-c: close standard output in child */
685
686 /* Create a file actions object and add a "close"
687 action to it */
688
689 s = posix_spawn_file_actions_init(&file_actions);
690 if (s != 0)
691 errExitEN(s, "posix_spawn_file_actions_init");
692
693 s = posix_spawn_file_actions_addclose(&file_actions,
694 STDOUT_FILENO);
695 if (s != 0)
696 errExitEN(s, "posix_spawn_file_actions_addclose");
697
698 file_actionsp = &file_actions;
699 break;
700
701 case \(aqs\(aq: /* \-s: block all signals in child */
702
703 /* Create an attributes object and add a "set signal mask"
704 action to it */
705
706 s = posix_spawnattr_init(&attr);
707 if (s != 0)
708 errExitEN(s, "posix_spawnattr_init");
709 s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
710 if (s != 0)
711 errExitEN(s, "posix_spawnattr_setflags");
712
713 sigfillset(&mask);
714 s = posix_spawnattr_setsigmask(&attr, &mask);
715 if (s != 0)
716 errExitEN(s, "posix_spawnattr_setsigmask");
717
718 attrp = &attr;
719 break;
720 }
721 }
722
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. */
727
728 s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp,
729 &argv[optind], environ);
730 if (s != 0)
731 errExitEN(s, "posix_spawn");
732
733 /* Destroy any objects that we created earlier */
734
735 if (attrp != NULL) {
736 s = posix_spawnattr_destroy(attrp);
737 if (s != 0)
738 errExitEN(s, "posix_spawnattr_destroy");
739 }
740
741 if (file_actionsp != NULL) {
742 s = posix_spawn_file_actions_destroy(file_actionsp);
743 if (s != 0)
744 errExitEN(s, "posix_spawn_file_actions_destroy");
745 }
746
747 printf("PID of child: %ld\\n", (long) child_pid);
748
749 /* Monitor status of the child until it terminates */
750
751 do {
752 s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
753 if (s == \-1)
754 errExit("waitpid");
755
756 printf("Child status: ");
757 if (WIFEXITED(status)) {
758 printf("exited, status=%d\\n", WEXITSTATUS(status));
759 } else if (WIFSIGNALED(status)) {
760 printf("killed by signal %d\\n", WTERMSIG(status));
761 } else if (WIFSTOPPED(status)) {
762 printf("stopped by signal %d\\n", WSTOPSIG(status));
763 } else if (WIFCONTINUED(status)) {
764 printf("continued\\n");
765 }
766 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
767
768 exit(EXIT_SUCCESS);
769}
e7d0bb47 770.EE
a5458d49 771.SH SEE ALSO
10069ed0
MK
772.nh \" Disable hyphenation
773.ad l
a5458d49
BG
774.BR close (2),
775.BR dup2 (2),
776.BR execl (2),
777.BR execlp (2),
778.BR fork (2),
779.BR open (2),
780.BR sched_setparam (2),
781.BR sched_setscheduler (2),
782.BR setpgid (2),
783.BR setuid (2),
784.BR sigaction (2),
10069ed0
MK
785.BR sigprocmask (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),
a5458d49
BG
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),
a5458d49
BG
805.BR pthread_atfork (3),
806.BR <spawn.h>,
10069ed0 807Base Definitions volume of POSIX.1-2001,
a5458d49 808.I http://www.opengroup.org/unix/online.html