xref: /openbsd/lib/libc/sys/wait.2 (revision 960f8fbd)
1.\"	$OpenBSD: wait.2,v 1.5 1999/02/27 21:57:00 deraadt Exp $
2.\"	$NetBSD: wait.2,v 1.6 1995/02/27 12:39:37 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993, 1994
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)wait.2	8.2 (Berkeley) 4/19/94
36.\"
37.Dd April 19, 1994
38.Dt WAIT 2
39.Os BSD 4
40.Sh NAME
41.Nm wait ,
42.Nm waitpid ,
43.Nm wait4 ,
44.Nm wait3
45.Nd wait for process termination
46.Sh SYNOPSIS
47.Fd #include <sys/types.h>
48.Fd #include <sys/wait.h>
49.Ft pid_t
50.Fn wait "int *status"
51.Ft pid_t
52.Fn waitpid "pid_t wpid" "int *status" "int options"
53.Fd #include <sys/time.h>
54.Fd #include <sys/resource.h>
55.Ft pid_t
56.Fn wait3 "int *status" "int options" "struct rusage *rusage"
57.Ft pid_t
58.Fn wait4 "pid_t wpid" "int *status" "int options" "struct rusage *rusage"
59.Sh DESCRIPTION
60The
61.Fn wait
62function suspends execution of its calling process until
63.Fa status
64information is available for a terminated child process,
65or a signal is received.
66On return from a successful
67.Fn wait
68call,
69the
70.Fa status
71area contains termination information about the process that exited
72as defined below.
73.Pp
74The
75.Fn wait4
76call provides a more general interface for programs
77that need to wait for certain child processes,
78that need resource utilization statistics accumulated by child processes,
79or that require options.
80The other wait functions are implemented using
81.Fn wait4 .
82.Pp
83The
84.Fa wpid
85parameter specifies the set of child processes for which to wait.
86If
87.Fa wpid
88is -1, the call waits for any child process.
89If
90.Fa wpid
91is 0,
92the call waits for any child process in the process group of the caller.
93If
94.Fa wpid
95is greater than zero, the call waits for the process with process id
96.Fa wpid .
97If
98.Fa wpid
99is less than -1, the call waits for any process whose process group id
100equals the absolute value of
101.Fa wpid .
102.Pp
103The
104.Fa status
105parameter is defined below.  The
106.Fa options
107parameter contains the bitwise OR of any of the following options.
108The
109.Dv WNOHANG
110option
111is used to indicate that the call should not block if
112there are no processes that wish to report status.
113If the
114.Dv WUNTRACED
115option is set,
116children of the current process that are stopped
117due to a
118.Dv SIGTTIN , SIGTTOU , SIGTSTP ,
119or
120.Dv SIGSTOP
121signal also have
122their status reported.
123.Pp
124If
125.Fa rusage
126is non-zero, a summary of the resources used by the terminated
127process and all its
128children is returned (this information is currently not available
129for stopped processes).
130.Pp
131When the
132.Dv WNOHANG
133option is specified and no processes
134wish to report status,
135.Fn wait4
136returns a
137process id
138of 0.
139.Pp
140The
141.Fn waitpid
142call is identical to
143.Fn wait4
144with an
145.Fa rusage
146value of zero.
147The older
148.Fn wait3
149call is the same as
150.Fn wait4
151with a
152.Fa wpid
153value of -1.
154.Pp
155The following macros may be used to test the manner of exit of the process.
156One of the first three macros will evaluate to a non-zero (true) value:
157.Bl -tag -width Ds
158.It Fn WIFEXITED status
159True if the process terminated normally by a call to
160.Xr _exit 2
161or
162.Xr exit 3 .
163.It Fn WIFSIGNALED status
164True if the process terminated due to receipt of a signal.
165.It Fn WIFSTOPPED status
166True if the process has not terminated, but has stopped and can be restarted.
167This macro can be true only if the wait call specified the
168.Dv WUNTRACED
169option
170or if the child process is being traced (see
171.Xr ptrace 2 ) .
172.El
173.Pp
174Depending on the values of those macros, the following macros
175produce the remaining status information about the child process:
176.Bl -tag -width Ds
177.It Fn WEXITSTATUS status
178If
179.Fn WIFEXITED status
180is true, evaluates to the low-order 8 bits
181of the argument passed to
182.Xr _exit 2
183or
184.Xr exit 3
185by the child.
186.It Fn WTERMSIG status
187If
188.Fn WIFSIGNALED status
189is true, evaluates to the number of the signal
190that caused the termination of the process.
191.It Fn WCOREDUMP status
192If
193.Fn WIFSIGNALED status
194is true, evaluates as true if the termination
195of the process was accompanied by the creation of a core file
196containing an image of the process when the signal was received.
197.It Fn WSTOPSIG status
198If
199.Fn WIFSTOPPED status
200is true, evaluates to the number of the signal
201that caused the process to stop.
202.El
203.Sh NOTES
204See
205.Xr sigaction 2
206for a list of termination signals.
207A status of 0 indicates normal termination.
208.Pp
209If a parent process terminates without
210waiting for all of its child processes to terminate,
211the remaining child processes are assigned the parent
212process 1 ID (the init process ID).
213.Pp
214If a signal is caught while any of the
215.Fn wait
216calls is pending,
217the call may be interrupted or restarted when the signal-catching routine
218returns,
219depending on the options in effect for the signal;
220see
221.Xr intro 2 ,
222System call restart.
223.Sh RETURN VALUES
224If
225.Fn wait
226returns due to a stopped
227or terminated child process, the process ID of the child
228is returned to the calling process.  Otherwise, a value of -1
229is returned and
230.Va errno
231is set to indicate the error.
232.Pp
233If
234.Fn wait4 ,
235.Fn wait3
236or
237.Fn waitpid
238returns due to a stopped
239or terminated child process, the process ID of the child
240is returned to the calling process.
241If there are no children not previously awaited,
242-1 is returned with
243.Va errno
244set to
245.Bq Er ECHILD .
246Otherwise, if
247.Dv WNOHANG
248is specified and there are
249no stopped or exited children,
2500 is returned.
251If an error is detected or a caught signal aborts the call,
252a value of -1
253is returned and
254.Va errno
255is set to indicate the error.
256.Sh ERRORS
257.Fn wait
258will fail and return immediately if:
259.Bl -tag -width Er
260.It Bq Er ECHILD
261The calling process has no existing unwaited-for
262child processes.
263.It Bq Er EFAULT
264The
265.Fa status
266or
267.Fa rusage
268arguments point to an illegal address.
269(May not be detected before exit of a child process.)
270.It Bq Er EINTR
271The call was interrupted by a caught signal,
272or the signal did not have the
273.Dv SA_RESTART
274flag set.
275.It Bq Er EINVAL
276Invalid or underfined flags were passed in the
277.Fa options
278argument.
279.El
280.Sh STANDARDS
281The
282.Fn wait
283and
284.Fn waitpid
285functions are defined by POSIX;
286.Fn wait4
287and
288.Fn wait3
289are not specified by POSIX.
290The
291.Fn WCOREDUMP
292macro
293and the ability to restart a pending
294.Fn wait
295call are extensions to the POSIX interface.
296.Sh SEE ALSO
297.Xr _exit 2 ,
298.Xr exit 3 ,
299.Xr sigaction 2
300.Sh HISTORY
301A
302.Fn wait
303function call appeared in
304.At v6 .
305