Add pselect syscall.
[dragonfly.git] / lib / libc / sys / wait.2
1 .\" Copyright (c) 1980, 1991, 1993, 1994
2 .\"     The Regents of the University of California.  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 .\"     @(#)wait.2      8.2 (Berkeley) 4/19/94
33 .\" $FreeBSD: src/lib/libc/sys/wait.2,v 1.6.2.6 2001/12/14 18:34:02 ru Exp $
34 .\" $DragonFly: src/lib/libc/sys/wait.2,v 1.4 2007/08/30 20:58:35 pavalos Exp $
35 .\"
36 .Dd August 30, 2007
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 .In sys/types.h
49 .In sys/wait.h
50 .Ft pid_t
51 .Fn wait "int *status"
52 .In sys/time.h
53 .In sys/resource.h
54 .Ft pid_t
55 .Fn waitpid "pid_t wpid" "int *status" "int options"
56 .Ft pid_t
57 .Fn wait3 "int *status" "int options" "struct rusage *rusage"
58 .Ft pid_t
59 .Fn wait4 "pid_t wpid" "int *status" "int options" "struct rusage *rusage"
60 .Sh DESCRIPTION
61 The
62 .Fn wait
63 function suspends execution of its calling process until
64 .Fa status
65 information is available for a terminated child process,
66 or a signal is received.
67 On return from a successful
68 .Fn wait
69 call,
70 the
71 .Fa status
72 area contains termination information about the process that exited
73 as defined below.
74 .Pp
75 The
76 .Fn wait4
77 call provides a more general interface for programs
78 that need to wait for certain child processes,
79 that need resource utilization statistics accumulated by child processes,
80 or that require options.
81 The other wait functions are implemented using
82 .Fn wait4 .
83 .Pp
84 The
85 .Fa wpid
86 parameter specifies the set of child processes for which to wait.
87 If
88 .Fa wpid
89 is -1, the call waits for any child process.
90 If
91 .Fa wpid
92 is 0,
93 the call waits for any child process in the process group of the caller.
94 If
95 .Fa wpid
96 is greater than zero, the call waits for the process with process id
97 .Fa wpid .
98 If
99 .Fa wpid
100 is less than -1, the call waits for any process whose process group id
101 equals the absolute value of
102 .Fa wpid .
103 .Pp
104 The
105 .Fa status
106 parameter is defined below.  The
107 .Fa options
108 parameter contains the bitwise OR of any of the following options.
109 The
110 .Dv WCONTINUED
111 option indicates that children of the current process that
112 have continued from a job control stop, by receiving a
113 .Dv SIGCONT
114 signal, should also have their status reported.
115 The
116 .Dv WNOHANG
117 option
118 is used to indicate that the call should not block if
119 there are no processes that wish to report status.
120 If the
121 .Dv WUNTRACED
122 option is set,
123 children of the current process that are stopped
124 due to a
125 .Dv SIGTTIN , SIGTTOU , SIGTSTP ,
126 or
127 .Dv SIGSTOP
128 signal also have
129 their status reported.
130 .Pp
131 If
132 .Fa rusage
133 is non-zero, a summary of the resources used by the terminated
134 process and all its
135 children is returned (this information is currently not available
136 for stopped processes).
137 .Pp
138 When the
139 .Dv WNOHANG
140 option is specified and no processes
141 wish to report status,
142 .Fn wait4
143 returns a
144 process id
145 of 0.
146 .Pp
147 The
148 .Fn waitpid
149 call is identical to
150 .Fn wait4
151 with an
152 .Fa rusage
153 value of zero.
154 The older
155 .Fn wait3
156 call is the same as
157 .Fn wait4
158 with a
159 .Fa wpid
160 value of -1.
161 .Pp
162 The following macros may be used to test the manner of exit of the process.
163 One of the first three macros will evaluate to a non-zero (true) value:
164 .Bl -tag -width Ds
165 .It Fn WIFEXITED status
166 True if the process terminated normally by a call to
167 .Xr _exit 2
168 or
169 .Xr exit 3 .
170 .It Fn WIFSIGNALED status
171 True if the process terminated due to receipt of a signal.
172 .It Fn WIFSTOPPED status
173 True if the process has not terminated, but has stopped and can be restarted.
174 This macro can be true only if the wait call specified the
175 .Dv WUNTRACED
176 option
177 or if the child process is being traced (see
178 .Xr ptrace 2 ) .
179 .El
180 .Pp
181 Depending on the values of those macros, the following macros
182 produce the remaining status information about the child process:
183 .Bl -tag -width Ds
184 .It Fn WIFCONTINUED status
185 True if the process has not terminated, and
186 has continued after a job control stop.
187 This macro can be true only if the wait call specified the
188 .Dv WCONTINUED
189 option).
190 .It Fn WEXITSTATUS status
191 If
192 .Fn WIFEXITED status
193 is true, evaluates to the low-order 8 bits
194 of the argument passed to
195 .Xr _exit 2
196 or
197 .Xr exit 3
198 by the child.
199 .It Fn WTERMSIG status
200 If
201 .Fn WIFSIGNALED status
202 is true, evaluates to the number of the signal
203 that caused the termination of the process.
204 .It Fn WCOREDUMP status
205 If
206 .Fn WIFSIGNALED status
207 is true, evaluates as true if the termination
208 of the process was accompanied by the creation of a core file
209 containing an image of the process when the signal was received.
210 .It Fn WSTOPSIG status
211 If
212 .Fn WIFSTOPPED status
213 is true, evaluates to the number of the signal
214 that caused the process to stop.
215 .El
216 .Sh NOTES
217 See
218 .Xr sigaction 2
219 for a list of termination signals.
220 A status of 0 indicates normal termination.
221 .Pp
222 If a parent process terminates without
223 waiting for all of its child processes to terminate,
224 the remaining child processes are assigned the parent
225 process 1 ID (the init process ID).
226 .Pp
227 If a signal is caught while any of the
228 .Fn wait
229 calls are pending,
230 the call may be interrupted or restarted when the signal-catching routine
231 returns,
232 depending on the options in effect for the signal;
233 see
234 .Xr intro 2 ,
235 System call restart.
236 .Sh RETURN VALUES
237 If
238 .Fn wait
239 returns due to a stopped
240 or terminated child process, the process ID of the child
241 is returned to the calling process.  Otherwise, a value of -1
242 is returned and
243 .Va errno
244 is set to indicate the error.
245 .Pp
246 If
247 .Fn wait4 ,
248 .Fn wait3 ,
249 or
250 .Fn waitpid
251 returns due to a stopped
252 or terminated child process, the process ID of the child
253 is returned to the calling process.
254 If there are no children not previously awaited,
255 -1 is returned with
256 .Va errno
257 set to
258 .Er ECHILD .
259 Otherwise, if
260 .Dv WNOHANG
261 is specified and there are
262 no stopped or exited children,
263 0 is returned.
264 If an error is detected or a caught signal aborts the call,
265 a value of -1
266 is returned and
267 .Va errno
268 is set to indicate the error.
269 .Sh ERRORS
270 .Fn Wait
271 will fail and return immediately if:
272 .Bl -tag -width Er
273 .It Bq Er ECHILD
274 The calling process has no existing unwaited-for
275 child processes.
276 .It Bq Er EFAULT
277 The
278 .Fa status
279 or
280 .Fa rusage
281 arguments point to an illegal address.
282 (May not be detected before exit of a child process.)
283 .It Bq Er EINTR
284 The call was interrupted by a caught signal,
285 or the signal did not have the
286 .Dv SA_RESTART
287 flag set.
288 .El
289 .Sh SEE ALSO
290 .Xr ptrace 2 ,
291 .Xr sigaction 2 ,
292 .Xr _exit 2 ,
293 .Xr exit 3
294 .Sh STANDARDS
295 The
296 .Fn wait
297 and
298 .Fn waitpid
299 functions are defined by POSIX;
300 .Fn wait4
301 and
302 .Fn wait3
303 are not specified by POSIX.
304 The
305 .Fn WCOREDUMP
306 macro
307 and the ability to restart a pending
308 .Fn wait
309 call are extensions to the POSIX interface.
310 .Sh HISTORY
311 A
312 .Fn wait
313 function call appeared in
314 .At v6 .