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