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