xref: /netbsd/lib/libc/sys/wait.2 (revision 6550d01e)
1.\"	$NetBSD: wait.2,v 1.27 2010/04/03 15:43:46 jruoho 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 3, 2010
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.
101.Pp
102The
103.Fa options
104parameter contains the bitwise OR of any of the following options:
105.Bl -tag -width WUNTRACED
106.It Dv WNOHANG
107This option is used to indicate that the call should not block if
108there are no processes that wish to report status.
109.It Dv WUNTRACED
110If this option is set, children of the current process that are stopped
111due to a
112.Dv SIGTTIN , SIGTTOU , SIGTSTP ,
113or
114.Dv SIGSTOP
115signal also have their status reported.
116.It Dv WALTSIG
117If this option is specified, the call will wait only for processes that
118are configured to post a signal other than
119.Dv SIGCHLD
120when they exit.
121If
122.Dv WALTSIG
123is not specified, the call will wait only for processes that
124are configured to post
125.Dv SIGCHLD .
126.It Dv __WCLONE
127This is an alias for
128.Dv WALTSIG .
129It is provided for compatibility with the Linux
130.Xr clone 2
131API.
132.It Dv WALLSIG
133If this option is specified, the call will wait for all children regardless
134of what exit signal they post.
135.It Dv __WALL
136This is an alias for
137.Dv WALLSIG .
138It is provided for compatibility with the Linux
139.Xr clone 2
140API .
141.El
142.Pp
143If
144.Fa rusage
145is non-zero, a summary of the resources used by the terminated
146process and all its
147children is returned (this information is currently not available
148for stopped processes).
149.Pp
150When the
151.Dv WNOHANG
152option is specified and no processes
153wish to report status,
154.Fn wait4
155returns a
156process id
157of 0.
158.Pp
159The
160.Fn waitpid
161call is identical to
162.Fn wait4
163with an
164.Fa rusage
165value of zero.
166The older
167.Fn wait3
168call is the same as
169.Fn wait4
170with a
171.Fa wpid
172value of \-1.
173.Pp
174The following macros may be used to test the manner of exit of the process.
175Note that these macros expect the
176.Fa status
177value itself, not a pointer to the
178.Fa status
179value.
180One of the first three macros will evaluate to a non-zero (true) value:
181.Bl -tag -width Ds
182.It Fn WIFEXITED status
183True if the process terminated normally by a call to
184.Xr _exit 2
185or
186.Xr exit 3 .
187.It Fn WIFSIGNALED status
188True if the process terminated due to receipt of a signal.
189.It Fn WIFSTOPPED status
190True if the process has not terminated, but has stopped and can be restarted.
191This macro can be true only if the wait call specified the
192.Dv WUNTRACED
193option
194or if the child process is being traced (see
195.Xr ptrace 2 ) .
196.El
197.Pp
198Depending on the values of those macros, the following macros
199produce the remaining status information about the child process:
200.Bl -tag -width Ds
201.It Fn WEXITSTATUS status
202If
203.Fn WIFEXITED status
204is true, evaluates to the low-order 8 bits
205of the argument passed to
206.Xr _exit 2
207or
208.Xr exit 3
209by the child.
210.It Fn WTERMSIG status
211If
212.Fn WIFSIGNALED status
213is true, evaluates to the number of the signal
214that caused the termination of the process.
215.It Fn WCOREDUMP status
216If
217.Fn WIFSIGNALED status
218is true, evaluates as true if the termination
219of the process was accompanied by the creation of a core file
220containing an image of the process when the signal was received.
221.It Fn WSTOPSIG status
222If
223.Fn WIFSTOPPED status
224is true, evaluates to the number of the signal
225that caused the process to stop.
226.El
227.Sh NOTES
228See
229.Xr sigaction 2
230for a list of termination signals.
231A status of 0 indicates normal termination.
232.Pp
233If a parent process terminates without
234waiting for all of its child processes to terminate,
235the remaining child processes are assigned the parent
236process 1 ID (the init process ID).
237.Pp
238If a signal is caught while any of the
239.Fn wait
240calls is pending,
241the call may be interrupted or restarted when the signal-catching routine
242returns,
243depending on the options in effect for the signal;
244see
245.Xr intro 2 ,
246System call restart.
247.Sh RETURN VALUES
248If
249.Fn wait
250returns due to a stopped
251or terminated child process, the process ID of the child
252is returned to the calling process.
253Otherwise, a value of \-1 is returned and
254.Va errno
255is set to indicate the error.
256.Pp
257If
258.Fn wait4 ,
259.Fn wait3
260or
261.Fn waitpid
262returns due to a stopped
263or terminated child process, the process ID of the child
264is returned to the calling process.
265If there are no children not previously awaited,
266\-1 is returned with
267.Va errno
268set to
269.Bq Er ECHILD .
270Otherwise, if
271.Dv WNOHANG
272is specified and there are
273no stopped or exited children, 0 is returned.
274If an error is detected or a caught signal aborts the call,
275a value of \-1 is returned and
276.Va errno
277is set to indicate the error.
278.Sh ERRORS
279.Fn wait
280will fail and return immediately if:
281.Bl -tag -width Er
282.It Bq Er ECHILD
283The calling process has no existing unwaited-for child processes.
284.It Bq Er EFAULT
285The
286.Fa status
287or
288.Fa rusage
289arguments point to an illegal address.
290(May not be detected before exit of a child process.)
291.It Bq Er EINTR
292The call was interrupted by a caught signal,
293or the signal did not have the
294.Dv SA_RESTART
295flag set.
296.El
297.Pp
298In addition,
299.Fn wait3 ,
300.Fn wait4 ,
301and
302.Fn waitpid
303will fail and return immediately if:
304.Bl -tag -width Er
305.It Bq Er EINVAL
306An invalid value was specified for
307.Fa options .
308.El
309.Sh SEE ALSO
310.Xr _exit 2 ,
311.Xr sigaction 2
312.Sh STANDARDS
313The
314.Fn wait
315and
316.Fn waitpid
317functions conform to
318.St -p1003.1-90 ;
319the
320.Fn wait3
321function conforms to
322.St -xpg4 ;
323.Fn wait4
324is an extension.
325The
326.Fn WCOREDUMP
327macro
328and the ability to restart a pending
329.Fn wait
330call are extensions to the POSIX interface.
331.Sh HISTORY
332A
333.Fn wait
334function call appeared in
335.At v2 .
336