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