xref: /netbsd-src/lib/libc/sys/wait.2 (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1.\"	$NetBSD: wait.2,v 1.36 2016/06/30 17:00:55 abhinav 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 June 27, 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.Fn wait6
90function is the most general function in this family and its distinct
91features are:
92.Pp
93All of the desired process statuses to be waited on must be explicitly
94specified in
95.Fa options .
96The
97.Fn wait ,
98.Fn waitpid ,
99.Fn wait3 ,
100and
101.Fn wait4
102functions all implicitly wait for exited and trapped processes,
103but the
104.Fn waitid
105and
106.Fn wait6
107functions require the corresponding
108.Dv WEXITED
109and
110.Dv WTRAPPED
111flags to be explicitly specified.
112This allows waiting for processes which have experienced other
113status changes without having to also handle the exit status from
114terminated processes.
115.Pp
116The
117.Fn wait6
118function accepts a
119.Fa wrusage
120argument which points to a structure defined as:
121.Bd -literal
122struct wrusage {
123	struct rusage   wru_self;
124	struct rusage   wru_children;
125};
126.Ed
127.Pp
128This allows the calling process to collect resource usage statistics
129from both its own child process as well as from its grand children.
130When no resource usage statistics are needed this pointer can be
131.Dv NULL .
132.Pp
133The last argument
134.Fa infop
135must be either
136.Dv NULL
137or a pointer to a
138.Fa siginfo_t
139structure.
140If
141.Pf non- Dv NULL ,
142the structure is filled with the same data as for a
143.Dv SIGCHLD
144signal delivered when the process changed state.
145.Pp
146The set of child processes to be queried is specified by the arguments
147.Fa idtype
148and
149.Fa id .
150The separate
151.Fa idtype
152and
153.Fa id
154arguments support many other types of
155identifiers in addition to process IDs and process group IDs.
156.Bl -bullet -offset indent
157.It
158If
159.Fa idtype
160is
161.Dv P_PID ,
162.Fn waitid
163and
164.Fn wait6
165wait for the child process with a process ID equal to
166.Dv (pid_t)id .
167.It
168If
169.Fa idtype
170is
171.Dv P_PGID ,
172.Fn waitid
173and
174.Fn wait6
175wait for the child process with a process group ID equal to
176.Dv (pid_t)id .
177.It
178If
179.Fa idtype
180is
181.Dv P_ALL ,
182.Fn waitid
183and
184.Fn wait6
185wait for any child process and the
186.Dv id
187is ignored.
188.It
189If
190.Fa idtype
191is
192.Dv P_PID
193or
194.Dv P_PGID
195and the
196.Dv id
197is zero,
198.Fn waitid
199and
200.Fn wait6
201wait for any child process in the same process group as the caller.
202.El
203.Pp
204Non-standard identifier types supported by this
205implementation of
206.Fn waitid
207and
208.Fn wait6
209are:
210.Bl -tag -width P_JAILID
211.It Dv P_UID
212Wait for processes whose effective user ID is equal to
213.Dv (uid_t) Fa id .
214.It Dv P_GID
215Wait for processes whose effective group ID is equal to
216.Dv (gid_t) Fa id .
217.It Dv P_SID
218Wait for processes whose session ID is equal to
219.Fa id .
220.\" This is just how sessions work, not sure this needs to be documented here
221If the child process started its own session,
222its session ID will be the same as its process ID.
223Otherwise the session ID of a child process will match the caller's session ID.
224.\" .It Dv P_JAILID
225.\" Waits for processes within a jail whose jail identifier is equal to
226.\" .Fa id .
227.El
228.Pp
229For the
230.Fn waitpid
231and
232.Fn wait4
233functions, the single
234.Fa wpid
235argument specifies the set of child processes for which to wait.
236The following symbolic constants are defined in
237.In sys/wait.h
238.Bd -literal -offset indent
239#define WAIT_ANY        (-1)    /* any process */
240#define WAIT_MYPGRP     0       /* any process in my process group */
241.Ed
242.Bl -bullet -offset indent
243.It
244If
245.Fa wpid
246is
247.Dv WAIT_ANY ,
248the call waits for any child process.
249.It
250If
251.Fa wpid
252is
253.Dv WAIT_MYPGRP ,
254the call waits for any child process in the process group of the caller.
255.It
256If
257.Fa wpid
258is greater than zero, the call waits for the process with process ID
259.Fa wpid .
260.It
261If
262.Fa wpid
263is less than \-1, the call waits for any process whose process group ID
264equals the absolute value of
265.Fa wpid .
266.El
267.Pp
268The
269.Fa status
270argument is defined below.
271.Pp
272The
273.Fa options
274argument contains the bitwise OR of any of the following options.
275.Bl -tag -width WCONTINUED
276.It Dv WALLSIG
277If this option is specified, the call will wait for all children regardless
278of what exit signal they post.
279.It Dv WALTSIG
280If this option is specified, the call will wait only for processes that
281are configured to post a signal other than
282.Dv SIGCHLD
283when they exit.
284If
285.Dv WALTSIG
286is not specified, the call will wait only for processes that
287are configured to post
288.Dv SIGCHLD .
289.It Dv WCONTINUED
290Report the status of selected processes that
291have continued from a job control stop by receiving a
292.Dv SIGCONT
293signal.
294.It Dv WEXITED
295Report the status of selected processes which have terminated.
296This flag is implicitly set for the functions
297.Fn wait ,
298.Fn waitpid ,
299.Fn wait3 ,
300and
301.Fn wait4 .
302.br
303For the
304.Fn waitid
305and
306.Fn wait6
307functions, the flag has to be explicitly included in
308.Fa options
309if status reports from terminated processes are expected.
310.It Dv WNOHANG
311Do not block when
312there are no processes wishing to report status.
313.It Dv WNOWAIT
314Keep the process whose status is returned in a waitable state.
315The process may be waited for again after this call completes.
316.It Dv WSTOPPED
317An alias for
318.Dv WUNTRACED .
319.It Dv WTRAPPED
320Report the status of selected processes which are being traced via
321.Xr ptrace 2
322and have trapped or reached a breakpoint.
323This flag is implicitly set for the functions
324.Fn wait ,
325.Fn waitpid ,
326.Fn wait3 ,
327and
328.Fn wait4 .
329.br
330For the
331.Fn waitid
332and
333.Fn wait6
334functions, the flag has to be explicitly included in
335.Fa options
336if status reports from trapped processes are expected.
337.It Dv WUNTRACED
338Report the status of selected processes which are stopped due to a
339.Dv SIGTTIN , SIGTTOU , SIGTSTP ,
340or
341.Dv SIGSTOP
342signal.
343.It Dv __WALL
344This is an alias for
345.Dv WALLSIG .
346It is provided for compatibility with the Linux
347.Xr clone 2
348API .
349.It Dv __WCLONE
350This is an alias for
351.Dv WALTSIG .
352It is provided for compatibility with the Linux
353.Xr clone 2
354API.
355.El
356.sp
357For the
358.Fn waitid
359and
360.Fn wait6
361functions, at least one of the options
362.Dv WEXITED ,
363.Dv WUNTRACED ,
364.Dv WSTOPPED ,
365.Dv WTRAPPED ,
366or
367.Dv WCONTINUED
368must be specified.
369Otherwise there will be no events for the call to report.
370To avoid hanging indefinitely in such a case these functions
371return \-1 with
372.Dv errno
373set to
374.Dv EINVAL .
375.Pp
376If
377.Fa rusage
378is
379.Pf non- Dv NULL ,
380a summary of the resources used by the terminated
381process and all its children is returned.
382.Pp
383If
384.Fa wrusage
385is
386.Pf non- Dv NULL ,
387separate summaries are returned for the resources used
388by the terminated process and the resources used by all its children.
389.Pp
390If
391.Fa infop
392is
393.Pf non- Dv NULL ,
394a
395.Dv siginfo_t
396structure is returned with the
397.Fa si_signo
398field set to
399.Dv SIGCHLD
400and the
401.Fa si_pid
402field set to the process ID of the process reporting status.
403For the exited process, the
404.Fa si_status
405field of the
406.Dv siginfo_t
407structure contains the full 32 bit exit status passed to
408.Xr _exit 2 ;
409the
410.Fa status
411argument of other calls only returns 8 lowest bits of the exit status.
412.Pp
413When the
414.Dv WNOHANG
415option is specified and no processes
416wish to report status,
417.Fn waitid
418sets the
419.Fa si_signo
420and
421.Fa si_pid
422fields in
423.Fa infop
424to zero.
425Checking these fields is the only way to know if a status change was reported.
426.Pp
427When the
428.Dv WNOHANG
429option is specified and no processes
430wish to report status,
431.Fn wait4
432returns a
433process id
434of 0.
435.Pp
436The
437.Fn waitpid
438call is identical to
439.Fn wait4
440with an
441.Fa rusage
442value of zero.
443The older
444.Fn wait3
445call is the same as
446.Fn wait4
447with a
448.Fa wpid
449value of \-1.
450.Pp
451The following macros may be used to test the manner of exit of the process.
452Note that these macros expect the
453.Fa status
454value itself, not a pointer to the
455.Fa status
456value.
457One of the first three macros will evaluate to a non-zero (true) value:
458.Bl -tag -width Ds
459.It Fn WIFEXITED status
460True if the process terminated normally by a call to
461.Xr _exit 2
462or
463.Xr exit 3 .
464.It Fn WIFSIGNALED status
465True if the process terminated due to receipt of a signal.
466.It Fn WIFSTOPPED status
467True if the process has not terminated, but has stopped and can be restarted.
468This macro can be true only if the wait call specified the
469.Dv WUNTRACED
470option
471or if the child process is being traced (see
472.Xr ptrace 2 ) .
473.It Fn WIFCONTINUED status
474True if the process has not terminated, but has been continued via the
475delivery of the
476.Dv SIGCONT
477signal.
478This macro can be true only if the wait call specified the
479.Dv WCONTINUED
480option.
481.El
482.Pp
483Depending on the values of those macros, the following macros
484produce the remaining status information about the child process:
485.Bl -tag -width Ds
486.It Fn WEXITSTATUS status
487If
488.Fn WIFEXITED status
489is true, evaluates to the low-order 8 bits
490of the argument passed to
491.Xr _exit 2
492or
493.Xr exit 3
494by the child.
495.It Fn WTERMSIG status
496If
497.Fn WIFSIGNALED status
498is true, evaluates to the number of the signal
499that caused the termination of the process.
500.It Fn WCOREDUMP status
501If
502.Fn WIFSIGNALED status
503is true, evaluates as true if the termination
504of the process was accompanied by the creation of a core file
505containing an image of the process when the signal was received.
506.It Fn WSTOPSIG status
507If
508.Fn WIFSTOPPED status
509is true, evaluates to the number of the signal
510that caused the process to stop.
511.El
512.Sh NOTES
513See
514.Xr sigaction 2
515for a list of termination signals.
516A status of 0 indicates normal termination.
517.Pp
518If a parent process terminates without
519waiting for all of its child processes to terminate,
520the remaining child processes are assigned the parent
521process 1 ID (the init process ID).
522.Pp
523If a signal is caught while any of the
524.Fn wait
525calls is pending,
526the call may be interrupted or restarted when the signal-catching routine
527returns,
528depending on the options in effect for the signal;
529see
530.Xr siginterrupt 3 .
531.Sh RETURN VALUES
532If
533.Fn wait
534returns due to a stopped
535or terminated child process, the process ID of the child
536is returned to the calling process.
537Otherwise, a value of \-1 is returned and
538.Va errno
539is set to indicate the error.
540.Pp
541If
542.Fn wait6 ,
543.Fn wait4 ,
544.Fn wait3
545or
546.Fn waitpid
547returns due to a stopped
548or terminated child process, the process ID of the child
549is returned to the calling process.
550If there are no children not previously awaited,
551\-1 is returned with
552.Va errno
553set to
554.Bq Er ECHILD .
555Otherwise, if
556.Dv WNOHANG
557is specified and there are
558no stopped or exited children, 0 is returned.
559If an error is detected or a caught signal aborts the call,
560a value of \-1 is returned and
561.Va errno
562is set to indicate the error.
563.Pp
564If
565.Fn waitid
566returns because one or more processes have a state change to report,
5670 is returned.
568If an error is detected,
569a value of -1
570is returned and
571.Va errno
572is set to indicate the error.
573If
574.Dv WNOHANG
575is specified and there are
576no stopped, continued or exited children,
5770 is returned.
578The
579.Fa si_signo
580and
581.Fa si_pid
582fields of
583.Fa infop
584must be checked against zero to determine if a process reported status.
585.Sh ERRORS
586.Fn wait
587will fail and return immediately if:
588.Bl -tag -width Er
589.It Bq Er ECHILD
590The calling process has no existing unwaited-for child processes; or
591no status from the terminated child process is available
592because the calling process has asked the system to discard
593such status by ignoring the signal
594.Dv SIGCHLD
595or setting the flag
596.Dv SA_NOCLDWAIT
597for that signal.
598.It Bq Er EFAULT
599The
600.Fa status
601or
602.Fa rusage
603arguments point to an illegal address.
604(May not be detected before exit of a child process.)
605.It Bq Er EINTR
606The call was interrupted by a caught signal,
607or the signal did not have the
608.Dv SA_RESTART
609flag set.
610.El
611.Pp
612In addition,
613.Fn wait6 ,
614.Fn wait3 ,
615.Fn wait4 ,
616.Fn waitid ,
617and
618.Fn waitpid
619will fail and return immediately if:
620.Bl -tag -width Er
621.It Bq Er EINVAL
622An invalid value was specified for
623.Fa options .
624.El
625.Sh SEE ALSO
626.Xr _exit 2 ,
627.Xr ptrace 2 ,
628.Xr sigaction 2 ,
629.Xr siginfo 2 ,
630.Xr exit 3 ,
631.Xr siginterrupt 3
632.Sh STANDARDS
633The
634.Fn wait
635and
636.Fn waitpid
637functions conform to
638.St -p1003.1-90 ;
639the
640.Fn waitid
641function conforms to
642.St -p1003.1-2004 ;
643the
644.Fn wait3
645function conforms to
646.St -xpg4 ;
647.Fn wait4
648is an extension.
649The
650.Fn WCOREDUMP
651macro
652and the ability to restart a pending
653.Fn wait
654call are extensions to the POSIX interface.
655.Sh HISTORY
656A
657.Fn wait
658function call appeared in
659.At v2 .
660