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