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