xref: /netbsd-src/lib/libc/sys/wait.2 (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1.\"	$NetBSD: wait.2,v 1.35 2016/04/29 13:17:09 christos Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993, 1994
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)wait.2	8.2 (Berkeley) 4/19/94
31.\"
32.Dd April 28, 2016
33.Dt WAIT 2
34.Os
35.Sh NAME
36.Nm wait ,
37.Nm waitid ,
38.Nm waitpid ,
39.Nm wait6 ,
40.Nm wait4 ,
41.Nm wait3
42.Nd wait for process termination
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In sys/wait.h
47.Ft pid_t
48.Fn wait "int *status"
49.Ft pid_t
50.Fn waitpid "pid_t wpid" "int *status" "int options"
51.Ft int
52.Fn waitid "idtype_t idtype" "id_t id" "siginfo_t *info" "int options"
53.In sys/resource.h
54.Ft pid_t
55.Fn wait3 "int *status" "int options" "struct rusage *rusage"
56.Ft pid_t
57.Fn wait4 "pid_t wpid" "int *status" "int options" "struct rusage *rusage"
58.Ft pid_t
59.Fn wait6 "idtype_t idtype" "id_t id" "int *status" "int options" "struct wrusage *wrusage" "siginfo_t *infop"
60.Sh DESCRIPTION
61The
62.Fn wait
63function suspends execution of its calling process until
64.Fa status
65information is available for a terminated child process,
66or a signal is received.
67On return from a successful
68.Fn wait
69call,
70the
71.Fa status
72area contains termination information about the process that exited
73as defined below.
74.Pp
75The
76.Fn wait4
77and
78.Fn wait6
79call provides a more general interface for programs
80that need to wait for certain child processes,
81that need resource utilization statistics accumulated by child processes,
82or that require options.
83The other wait functions are implemented using
84.Fn wait4 .
85or
86.Fn wait6 .
87.Pp
88The
89.Fa wpid
90parameter specifies the set of child processes for which to wait.
91If
92.Fa wpid
93is \-1, the call waits for any child process.
94If
95.Fa wpid
96is 0,
97the call waits for any child process in the process group of the caller.
98If
99.Fa wpid
100is greater than zero, the call waits for the process with process id
101.Fa wpid .
102If
103.Fa wpid
104is less than \-1, the call waits for any process whose process group id
105equals the absolute value of
106.Fa wpid .
107.Pp
108The
109.Fa status
110parameter is defined below.
111.Pp
112If
113.Fa rusage
114is non-zero, a summary of the resources used by the terminated
115process and all its
116children is returned (this information is currently not available
117for stopped processes).
118.Pp
119The
120.Fn wait6
121function is the most general function in this family and its distinct
122features are:
123.Pp
124All of the desired process statuses to be waited on must be explicitly
125specified in
126.Fa options .
127The
128.Fn wait ,
129.Fn waitpid ,
130.Fn wait3 ,
131and
132.Fn wait4
133functions all implicitly wait for exited and trapped processes,
134but the
135.Fn waitid
136and
137.Fn wait6
138functions require the corresponding
139.Dv WEXITED
140and
141.Dv WTRAPPED
142flags to be explicitly specified.
143This allows waiting for processes which have experienced other
144status changes without having to also handle the exit status from
145terminated processes.
146.Pp
147The
148.Fn wait6
149function accepts a
150.Fa wrusage
151argument which points to a structure defined as:
152.Bd -literal
153struct wrusage {
154	struct rusage   wru_self;
155	struct rusage   wru_children;
156};
157.Ed
158.Pp
159This allows the calling process to collect resource usage statistics
160from both its own child process as well as from its grand children.
161When no resource usage statistics are needed this pointer can be
162.Dv NULL .
163.Pp
164The last argument
165.Fa infop
166must be either
167.Dv NULL
168or a pointer to a
169.Fa siginfo_t
170structure.
171If
172.Pf non- Dv NULL ,
173the structure is filled with the same data as for a
174.Dv SIGCHLD
175signal delivered when the process changed state.
176.Pp
177The set of child processes to be queried is specified by the arguments
178.Fa idtype
179and
180.Fa id .
181The separate
182.Fa idtype
183and
184.Fa id
185arguments support many other types of
186identifiers in addition to process IDs and process group IDs.
187.Bl -bullet -offset indent
188.It
189If
190.Fa idtype
191is
192.Dv P_PID ,
193.Fn waitid
194and
195.Fn wait6
196wait for the child process with a process ID equal to
197.Dv (pid_t)id .
198.It
199If
200.Fa idtype
201is
202.Dv P_PGID ,
203.Fn waitid
204and
205.Fn wait6
206wait for the child process with a process group ID equal to
207.Dv (pid_t)id .
208.It
209If
210.Fa idtype
211is
212.Dv P_ALL ,
213.Fn waitid
214and
215.Fn wait6
216wait for any child process and the
217.Dv id
218is ignored.
219.It
220If
221.Fa idtype
222is
223.Dv P_PID
224or
225.Dv P_PGID
226and the
227.Dv id
228is zero,
229.Fn waitid
230and
231.Fn wait6
232wait for any child process in the same process group as the caller.
233.El
234.Pp
235Non-standard identifier types supported by this
236implementation of
237.Fn waitid
238and
239.Fn wait6
240are:
241.Bl -tag -width P_JAILID
242.It Dv P_UID
243Wait for processes whose effective user ID is equal to
244.Dv (uid_t) Fa id .
245.It Dv P_GID
246Wait for processes whose effective group ID is equal to
247.Dv (gid_t) Fa id .
248.It Dv P_SID
249Wait for processes whose session ID is equal to
250.Fa id .
251.\" This is just how sessions work, not sure this needs to be documented here
252If the child process started its own session,
253its session ID will be the same as its process ID.
254Otherwise the session ID of a child process will match the caller's session ID.
255.\" .It Dv P_JAILID
256.\" Waits for processes within a jail whose jail identifier is equal to
257.\" .Fa id .
258.El
259.Pp
260For the
261.Fn waitpid
262and
263.Fn wait4
264functions, the single
265.Fa wpid
266argument specifies the set of child processes for which to wait.
267.Bl -bullet -offset indent
268.It
269If
270.Fa wpid
271is \-1, the call waits for any child process.
272.It
273If
274.Fa wpid
275is 0,
276the call waits for any child process in the process group of the caller.
277.It
278If
279.Fa wpid
280is greater than zero, the call waits for the process with process ID
281.Fa wpid .
282.It
283If
284.Fa wpid
285is less than \-1, the call waits for any process whose process group ID
286equals the absolute value of
287.Fa wpid .
288.El
289.Pp
290The
291.Fa status
292argument is defined below.
293.Pp
294The
295.Fa options
296argument contains the bitwise OR of any of the following options.
297.Bl -tag -width WCONTINUED
298.It Dv WALLSIG
299If this option is specified, the call will wait for all children regardless
300of what exit signal they post.
301.It Dv WALTSIG
302If this option is specified, the call will wait only for processes that
303are configured to post a signal other than
304.Dv SIGCHLD
305when they exit.
306If
307.Dv WALTSIG
308is not specified, the call will wait only for processes that
309are configured to post
310.Dv SIGCHLD .
311.It Dv WCONTINUED
312Report the status of selected processes that
313have continued from a job control stop by receiving a
314.Dv SIGCONT
315signal.
316.It Dv WEXITED
317Report the status of selected processes which have terminated.
318This flag is implicitly set for the functions
319.Fn wait ,
320.Fn waitpid ,
321.Fn wait3 ,
322and
323.Fn wait4 .
324.br
325For the
326.Fn waitid
327and
328.Fn wait6
329functions, the flag has to be explicitly included in
330.Fa options
331if status reports from terminated processes are expected.
332.It Dv WNOHANG
333Do not block when
334there are no processes wishing to report status.
335.It Dv WNOWAIT
336Keep the process whose status is returned in a waitable state.
337The process may be waited for again after this call completes.
338.It Dv WSTOPPED
339An alias for
340.Dv WUNTRACED .
341.It Dv WTRAPPED
342Report the status of selected processes which are being traced via
343.Xr ptrace 2
344and have trapped or reached a breakpoint.
345This flag is implicitly set for the functions
346.Fn wait ,
347.Fn waitpid ,
348.Fn wait3 ,
349and
350.Fn wait4 .
351.br
352For the
353.Fn waitid
354and
355.Fn wait6
356functions, the flag has to be explicitly included in
357.Fa options
358if status reports from trapped processes are expected.
359.It Dv WUNTRACED
360Report the status of selected processes which are stopped due to a
361.Dv SIGTTIN , SIGTTOU , SIGTSTP ,
362or
363.Dv SIGSTOP
364signal.
365.It Dv __WALL
366This is an alias for
367.Dv WALLSIG .
368It is provided for compatibility with the Linux
369.Xr clone 2
370API .
371.It Dv __WCLONE
372This is an alias for
373.Dv WALTSIG .
374It is provided for compatibility with the Linux
375.Xr clone 2
376API.
377.El
378.sp
379For the
380.Fn waitid
381and
382.Fn wait6
383functions, at least one of the options
384.Dv WEXITED ,
385.Dv WUNTRACED ,
386.Dv WSTOPPED ,
387.Dv WTRAPPED ,
388or
389.Dv WCONTINUED
390must be specified.
391Otherwise there will be no events for the call to report.
392To avoid hanging indefinitely in such a case these functions
393return \-1 with
394.Dv errno
395set to
396.Dv EINVAL .
397.Pp
398If
399.Fa rusage
400is
401.Pf non- Dv NULL ,
402a summary of the resources used by the terminated
403process and all its children is returned.
404.Pp
405If
406.Fa wrusage
407is
408.Pf non- Dv NULL ,
409separate summaries are returned for the resources used
410by the terminated process and the resources used by all its children.
411.Pp
412If
413.Fa infop
414is
415.Pf non- Dv NULL ,
416a
417.Dv siginfo_t
418structure is returned with the
419.Fa si_signo
420field set to
421.Dv SIGCHLD
422and the
423.Fa si_pid
424field set to the process ID of the process reporting status.
425For the exited process, the
426.Fa si_status
427field of the
428.Dv siginfo_t
429structure contains the full 32 bit exit status passed to
430.Xr _exit 2 ;
431the
432.Fa status
433argument of other calls only returns 8 lowest bits of the exit status.
434.Pp
435When the
436.Dv WNOHANG
437option is specified and no processes
438wish to report status,
439.Fn waitid
440sets the
441.Fa si_signo
442and
443.Fa si_pid
444fields in
445.Fa infop
446to zero.
447Checking these fields is the only way to know if a status change was reported.
448.Pp
449When the
450.Dv WNOHANG
451option is specified and no processes
452wish to report status,
453.Fn wait4
454returns a
455process id
456of 0.
457.Pp
458The
459.Fn waitpid
460call is identical to
461.Fn wait4
462with an
463.Fa rusage
464value of zero.
465The older
466.Fn wait3
467call is the same as
468.Fn wait4
469with a
470.Fa wpid
471value of \-1.
472.Pp
473The following macros may be used to test the manner of exit of the process.
474Note that these macros expect the
475.Fa status
476value itself, not a pointer to the
477.Fa status
478value.
479One of the first three macros will evaluate to a non-zero (true) value:
480.Bl -tag -width Ds
481.It Fn WIFEXITED status
482True if the process terminated normally by a call to
483.Xr _exit 2
484or
485.Xr exit 3 .
486.It Fn WIFSIGNALED status
487True if the process terminated due to receipt of a signal.
488.It Fn WIFSTOPPED status
489True if the process has not terminated, but has stopped and can be restarted.
490This macro can be true only if the wait call specified the
491.Dv WUNTRACED
492option
493or if the child process is being traced (see
494.Xr ptrace 2 ) .
495.It Fn WIFCONTINUED status
496True if the process has not terminated, but has been continued via the
497delivery of the
498.Dv SIGCONT
499signal.
500This macro can be true only if the wait call specified the
501.Dv WCONTINUED
502option.
503.El
504.Pp
505Depending on the values of those macros, the following macros
506produce the remaining status information about the child process:
507.Bl -tag -width Ds
508.It Fn WEXITSTATUS status
509If
510.Fn WIFEXITED status
511is true, evaluates to the low-order 8 bits
512of the argument passed to
513.Xr _exit 2
514or
515.Xr exit 3
516by the child.
517.It Fn WTERMSIG status
518If
519.Fn WIFSIGNALED status
520is true, evaluates to the number of the signal
521that caused the termination of the process.
522.It Fn WCOREDUMP status
523If
524.Fn WIFSIGNALED status
525is true, evaluates as true if the termination
526of the process was accompanied by the creation of a core file
527containing an image of the process when the signal was received.
528.It Fn WSTOPSIG status
529If
530.Fn WIFSTOPPED status
531is true, evaluates to the number of the signal
532that caused the process to stop.
533.El
534.Sh NOTES
535See
536.Xr sigaction 2
537for a list of termination signals.
538A status of 0 indicates normal termination.
539.Pp
540If a parent process terminates without
541waiting for all of its child processes to terminate,
542the remaining child processes are assigned the parent
543process 1 ID (the init process ID).
544.Pp
545If a signal is caught while any of the
546.Fn wait
547calls is pending,
548the call may be interrupted or restarted when the signal-catching routine
549returns,
550depending on the options in effect for the signal;
551see
552.Xr intro 2 ,
553System call restart.
554.Sh RETURN VALUES
555If
556.Fn wait
557returns due to a stopped
558or terminated child process, the process ID of the child
559is returned to the calling process.
560Otherwise, a value of \-1 is returned and
561.Va errno
562is set to indicate the error.
563.Pp
564If
565.Fn wait6 ,
566.Fn wait4 ,
567.Fn wait3
568or
569.Fn waitpid
570returns due to a stopped
571or terminated child process, the process ID of the child
572is returned to the calling process.
573If there are no children not previously awaited,
574\-1 is returned with
575.Va errno
576set to
577.Bq Er ECHILD .
578Otherwise, if
579.Dv WNOHANG
580is specified and there are
581no stopped or exited children, 0 is returned.
582If an error is detected or a caught signal aborts the call,
583a value of \-1 is returned and
584.Va errno
585is set to indicate the error.
586.Pp
587If
588.Fn waitid
589returns because one or more processes have a state change to report,
5900 is returned.
591If an error is detected,
592a value of -1
593is returned and
594.Va errno
595is set to indicate the error.
596If
597.Dv WNOHANG
598is specified and there are
599no stopped, continued or exited children,
6000 is returned.
601The
602.Fa si_signo
603and
604.Fa si_pid
605fields of
606.Fa infop
607must be checked against zero to determine if a process reported status.
608.Sh ERRORS
609.Fn wait
610will fail and return immediately if:
611.Bl -tag -width Er
612.It Bq Er ECHILD
613The calling process has no existing unwaited-for child processes; or
614no status from the terminated child process is available
615because the calling process has asked the system to discard
616such status by ignoring the signal
617.Dv SIGCHLD
618or setting the flag
619.Dv SA_NOCLDWAIT
620for that signal.
621.It Bq Er EFAULT
622The
623.Fa status
624or
625.Fa rusage
626arguments point to an illegal address.
627(May not be detected before exit of a child process.)
628.It Bq Er EINTR
629The call was interrupted by a caught signal,
630or the signal did not have the
631.Dv SA_RESTART
632flag set.
633.El
634.Pp
635In addition,
636.Fn wait6 ,
637.Fn wait3 ,
638.Fn wait4 ,
639.Fn waitid ,
640and
641.Fn waitpid
642will fail and return immediately if:
643.Bl -tag -width Er
644.It Bq Er EINVAL
645An invalid value was specified for
646.Fa options .
647.El
648.Sh SEE ALSO
649.Xr _exit 2 ,
650.Xr ptrace 2 ,
651.Xr sigaction 2 ,
652.Xr siginfo 2
653.Sh STANDARDS
654The
655.Fn wait
656and
657.Fn waitpid
658functions conform to
659.St -p1003.1-90 ;
660the
661.Fn waitid
662function conforms to
663.St -p1003.1-2004 ;
664the
665.Fn wait3
666function conforms to
667.St -xpg4 ;
668.Fn wait4
669is an extension.
670The
671.Fn WCOREDUMP
672macro
673and the ability to restart a pending
674.Fn wait
675call are extensions to the POSIX interface.
676.Sh HISTORY
677A
678.Fn wait
679function call appeared in
680.At v2 .
681