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