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