]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/wait.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)wait.2      8.2 (Berkeley) 4/19/94
29 .\" $FreeBSD$
30 .\"
31 .Dd November 10, 2012
32 .Dt WAIT 2
33 .Os
34 .Sh NAME
35 .Nm wait ,
36 .Nm waitid ,
37 .Nm waitpid ,
38 .Nm wait3 ,
39 .Nm wait4 ,
40 .Nm wait6
41 .Nd wait for processes to change status
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In sys/types.h
46 .In sys/wait.h
47 .Ft pid_t
48 .Fn wait "int *status"
49 .Ft pid_t
50 .Fn waitpid "pid_t wpid" "int *status" "int options"
51 .In sys/signal.h
52 .Ft int
53 .Fn waitid "idtype_t idtype" "id_t id" "siginfo_t *info" "int options"
54 .In sys/time.h
55 .In sys/resource.h
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 .Ft pid_t
61 .Fn wait6 "idtype_t idtype" "id_t id" "int *status" "int options" "struct __wrusage *wrusage" "siginfo_t *infop"
62 .Sh DESCRIPTION
63 The
64 .Fn wait
65 function suspends execution of its calling process until
66 .Fa status
67 information is available for a terminated child process,
68 or a signal is received.
69 On return from a successful
70 .Fn wait
71 call,
72 the
73 .Fa status
74 area contains termination information about the process that exited
75 as defined below.
76 The
77 .Fn wait
78 call is the same as
79 .Fn wait4
80 with a
81 .Fa wpid
82 value of -1,
83 with an
84 .Fa options
85 value of zero,
86 and a
87 .Fa rusage
88 value of zero.
89 .Pp
90 The
91 .Fn wait4
92 system call provides a more general interface for programs
93 that need to wait for certain child processes,
94 that need resource utilization statistics accumulated by child processes,
95 or that require options.
96 .Pp
97 The broadest interface of all functions in this family is
98 .Fn wait6
99 which is otherwise very much like
100 .Fn wait4
101 but with a few very important distinctions.
102 To wait for exited processes, the option flag
103 .Dv WEXITED
104 need to be explicitly specified.
105 This allows for waiting for processes which have experienced other
106 status changes without having to handle also the exit status from
107 the terminated processes.
108 Instead of the traditional
109 .Dv rusage
110 argument, a pointer to a new structure
111 .Bd -literal
112 struct __wrusage {
113         struct rusage   wru_self;
114         struct rusage   wru_children;
115 };
116 .Ed
117 can be passed.
118 This allows the calling process to collect resource usage statistics
119 from both its own child process as well as from its grand children.
120 When no resource usage statistics are needed this pointer can be
121 .Dv NULL .
122 The last argument
123 .Fa infop
124 must be either
125 .Dv NULL
126 or a pointer to a
127 .Fa siginfo_t
128 structure.
129 When specified, the structure is filled the same as for
130 .Dv SIGNCHLD
131 signal, delivered at the process state change.
132 .br
133 The process, which state is queried, is specified by two arguments
134 .Fa idtype
135 and
136 .Fa id .
137 The separate
138 .Fa idtype
139 and
140 .Fa id
141 arguments allows to support many other types of
142 IDs  as well in addition to PID and PGID.
143 .Bl -bullet -offset indent
144 .It
145 If
146 .Fa idtype
147 is
148 .Dv P_PID ,
149 .Fn waitid
150 and
151 .Fn wait6
152 wait for the child process with a process ID equal to
153 .Dv (pid_t)id .
154 .It
155 If
156 .Fa idtype
157 is
158 .Dv P_PGID ,
159 .Fn waitid
160 and
161 .Fn wait6
162 wait for the child process with a process group ID equal to
163 .Dv (pid_t)id .
164 .It
165 If
166 .Fa idtype
167 is
168 .Dv P_ALL ,
169 .Fn waitid
170 and
171 .Fn wait6
172 wait for any child process and the
173 .Dv id
174 is ignored.
175 .It
176 If
177 .Fa idtype
178 is
179 .Dv P_PID
180 or
181 .Dv P_PGID
182 and the
183 .Dv id
184 is zero,
185 .Fn waitid
186 and
187 .Fn wait6
188 wait for any child process in the same process group as the caller.
189 .El
190 .Pp
191 Non-standard specifiers for the process to wait for, supported by this
192 implementation of
193 .Fn waitid
194 and
195 .Fn wait6 ,
196 are:
197 .Bl -bullet -offset indent
198 .It
199 The
200 .Fa idtype
201 value
202 .Dv P_UID
203 waits for processes which effective UID is equal to
204 .Dv (uid_t)id .
205 .It
206 The
207 .Fa idtype
208 value
209 .Dv P_GID
210 waits for processes which effective GID is equal to
211 .Dv (gid_t)id .
212 .It
213 The
214 .Fa idtype
215 value
216 .Dv P_SID
217 waits for processes which session ID is equal to
218 .Dv id .
219 In case the child process started its own new session,
220 SID will be the same as its own PID.
221 Otherwise the SID of a child process will match the caller's SID.
222 .It
223 The
224 .Fa idtype
225 value
226 .Dv P_JAILID
227 waits for processes within a jail which jail identifier is equal
228 to
229 .Dv id .
230 .El
231 .Pp
232 For
233 .Fn wait ,
234 .Fn wait3 ,
235 and
236 .Fn wait4
237 functions, the single
238 .Fa wpid
239 argument specifies the set of child processes for which to wait.
240 .Bl -bullet -offset indent
241 .It
242 If
243 .Fa wpid
244 is -1, the call waits for any child process.
245 .It
246 If
247 .Fa wpid
248 is 0,
249 the call waits for any child process in the process group of the caller.
250 .It
251 If
252 .Fa wpid
253 is greater than zero, the call waits for the process with process id
254 .Fa wpid .
255 .It
256 If
257 .Fa wpid
258 is less than -1, the call waits for any process whose process group id
259 equals the absolute value of
260 .Fa wpid .
261 .El
262 .Pp
263 The
264 .Fa status
265 argument is defined below.
266 .Pp
267 The
268 .Fa options
269 argument contains the bitwise OR of any of the following options.
270 .Bl -tag -width Ds
271 .It Dv WCONTINUED
272 indicates that children of the current process that
273 have continued from a job control stop, by receiving a
274 .Dv SIGCONT
275 signal, should also have their status reported.
276 .It Dv WNOHANG
277 is used to indicate that the call should not block when
278 there are no processes wishing to report status.
279 .It Dv WUNTRACED
280 indicates that children of the current process which are stopped
281 due to a
282 .Dv SIGTTIN , SIGTTOU , SIGTSTP ,
283 or
284 .Dv SIGSTOP
285 signal shall have their status reported.
286 .It Dv WSTOPPED
287 is an alias for
288 .Dv WUNTRACED .
289 .It Dv WTRAPPED
290 allows waiting for processes which have trapped or reached a breakpoint.
291 .It Dv WEXITED
292 indicates that the caller is wants to receive status reports from
293 terminated processes.
294 This flag is implicitly set for the functions
295 .Fn wait ,
296 .Fn waitpid ,
297 .Fn wait3 ,
298 and
299 .Fn wait4 .
300 .br
301 For the
302 .Fn waitid
303 and
304 .Fn wait6
305 functions, the flag has to be explicitly included in the
306 .Fa options ,
307 if status reports from terminated processes are expected.
308 .It Dv WNOWAIT
309 keeps the process whose status is returned in a waitable state.
310 The process may be waited for again after this call completes.
311 .El
312 .sp
313 For the
314 .Fn waitid
315 and
316 .Fn wait6
317 functions, at least one of the options
318 .Dv WEXITED ,
319 .Dv WUNTRACED ,
320 .Dv WSTOPPED ,
321 .Dv WTRAPPED ,
322 or
323 .Dv WCONTINUED
324 must be specified.
325 Otherwise there will be no events for the call to report.
326 To avoid hanging indefinitely in such a case these functions
327 return -1 with
328 .Dv errno
329 set to
330 .Dv EINVAL .
331 .Pp
332 If
333 .Fa rusage
334 is non-NULL, a summary of the resources used by the terminated
335 process and all its children is returned.
336 .Pp
337 If
338 .Fa wrusage
339 argument is non-NULL, a resource usage statistics
340 from both its own child process as well as from its grand children
341 is returned.
342 .Pp
343 If
344 .Fa infop
345 is non-NULL, it must point to a
346 .Dv siginfo_t
347 structure which is filled on return such that the
348 .Dv si_signo
349 field is always
350 .Dv SIGCHLD
351 and the field
352 .Dv si_pid
353 if be non-zero, if there is a status change to report.
354 If there are no status changes to report and WNOHANG is applied,
355 both of these fields are returned zero.
356 When using the
357 .Fn waitid
358 function with the
359 .Dv WNOHANG
360 option set, checking these fields is the only way to know whether
361 there were any status changes to report, because the return value
362 from
363 .Fn waitid
364 is be zero as it is for any successful return from
365 .Fn waitid .
366 .Pp
367 When the
368 .Dv WNOHANG
369 option is specified and no processes
370 wish to report status,
371 .Fn wait4
372 returns a
373 process id
374 of 0.
375 .Pp
376 The
377 .Fn waitpid
378 function is identical to
379 .Fn wait4
380 with an
381 .Fa rusage
382 value of zero.
383 The older
384 .Fn wait3
385 call is the same as
386 .Fn wait4
387 with a
388 .Fa wpid
389 value of -1.
390 The
391 .Fn wait6
392 call, with the bits
393 .Dv WEXITED
394 and
395 .Dv WTRAPPED
396 set in the
397 .Fa options
398 and with
399 .Fa infop
400 set to
401 .Dv NULL ,
402 is similar to
403 .Fn wait4 .
404 .Pp
405 The following macros may be used to test the manner of exit of the process.
406 One of the first four macros will evaluate to a non-zero (true) value:
407 .Bl -tag -width Ds
408 .It Fn WIFCONTINUED status
409 True if the process has not terminated, and
410 has continued after a job control stop.
411 This macro can be true only if the wait call specified the
412 .Dv WCONTINUED
413 option).
414 .It Fn WIFEXITED status
415 True if the process terminated normally by a call to
416 .Xr _exit 2
417 or
418 .Xr exit 3 .
419 .It Fn WIFSIGNALED status
420 True if the process terminated due to receipt of a signal.
421 .It Fn WIFSTOPPED status
422 True if the process has not terminated, but has stopped and can be restarted.
423 This macro can be true only if the wait call specified the
424 .Dv WUNTRACED
425 option
426 or if the child process is being traced (see
427 .Xr ptrace 2 ) .
428 .El
429 .Pp
430 Depending on the values of those macros, the following macros
431 produce the remaining status information about the child process:
432 .Bl -tag -width Ds
433 .It Fn WEXITSTATUS status
434 If
435 .Fn WIFEXITED status
436 is true, evaluates to the low-order 8 bits
437 of the argument passed to
438 .Xr _exit 2
439 or
440 .Xr exit 3
441 by the child.
442 .It Fn WTERMSIG status
443 If
444 .Fn WIFSIGNALED status
445 is true, evaluates to the number of the signal
446 that caused the termination of the process.
447 .It Fn WCOREDUMP status
448 If
449 .Fn WIFSIGNALED status
450 is true, evaluates as true if the termination
451 of the process was accompanied by the creation of a core file
452 containing an image of the process when the signal was received.
453 .It Fn WSTOPSIG status
454 If
455 .Fn WIFSTOPPED status
456 is true, evaluates to the number of the signal
457 that caused the process to stop.
458 .El
459 .Sh NOTES
460 See
461 .Xr sigaction 2
462 for a list of termination signals.
463 A status of 0 indicates normal termination.
464 .Pp
465 If a parent process terminates without
466 waiting for all of its child processes to terminate,
467 the remaining child processes are assigned the parent
468 process 1 ID (the init process ID).
469 .Pp
470 If a signal is caught while any of the
471 .Fn wait
472 calls are pending,
473 the call may be interrupted or restarted when the signal-catching routine
474 returns,
475 depending on the options in effect for the signal;
476 see discussion of
477 .Dv SA_RESTART
478 in
479 .Xr sigaction 2 .
480 .Pp
481 The implementation queues one
482 .Dv SIGCHLD
483 signal for each child process whose
484 status has changed, if
485 .Fn wait
486 returns because the status of a child process is available, the pending
487 SIGCHLD signal associated with the process ID of the child process will
488 be discarded.
489 Any other pending
490 .Dv SIGCHLD
491 signals remain pending.
492 .Pp
493 If
494 .Dv SIGCHLD
495 is blocked,
496 .Fn wait
497 returns because the status of a child process is available, the pending
498 .Dv SIGCHLD
499 signal will be cleared unless another status of the child process
500 is available.
501 .Sh RETURN VALUES
502 If
503 .Fn wait
504 returns due to a stopped, continued,
505 or terminated child process, the process ID of the child
506 is returned to the calling process.
507 Otherwise, a value of \-1
508 is returned and
509 .Va errno
510 is set to indicate the error.
511 .Pp
512 If
513 .Fn wait6 ,
514 .Fn wait4 ,
515 .Fn wait3 ,
516 or
517 .Fn waitpid
518 returns due to a stopped, continued,
519 or terminated child process, the process ID of the child
520 is returned to the calling process.
521 If there are no children not previously awaited,
522 -1 is returned with
523 .Va errno
524 set to
525 .Er ECHILD .
526 Otherwise, if
527 .Dv WNOHANG
528 is specified and there are
529 no stopped, continued or exited children,
530 0 is returned.
531 If an error is detected or a caught signal aborts the call,
532 a value of -1
533 is returned and
534 .Va errno
535 is set to indicate the error.
536 .Pp
537 If
538 .Fn waitid
539 returns because one or more processes have a state change to report,
540 0 is returned.
541 To indicate an error, -1 will be returned and
542 .Dv errno
543 set to an appropriate value.
544 If
545 .Dv WNOHANG
546 was used, 0 can be returned indicating no error, but no processes
547 may have changed state either, if si_signo and/or si_pid are zero.
548 .Sh ERRORS
549 The
550 .Fn wait
551 function
552 will fail and return immediately if:
553 .Bl -tag -width Er
554 .It Bq Er ECHILD
555 The calling process has no existing unwaited-for
556 child processes.
557 .It Bq Er ECHILD
558 No status from the terminated child process is available
559 because the calling process has asked the system to discard
560 such status by ignoring the signal
561 .Dv SIGCHLD
562 or setting the flag
563 .Dv SA_NOCLDWAIT
564 for that signal.
565 .It Bq Er EFAULT
566 The
567 .Fa status
568 or
569 .Fa rusage
570 argument points to an illegal address.
571 (May not be detected before exit of a child process.)
572 .It Bq Er EINTR
573 The call was interrupted by a caught signal,
574 or the signal did not have the
575 .Dv SA_RESTART
576 flag set.
577 .It Bq Er EINVAL
578 An invalid value was specified for
579 .Fa options ,
580 or
581 .Fa idtype
582 and
583 .Fa id
584 do not specify a valid set of processes.
585 .El
586 .Sh SEE ALSO
587 .Xr _exit 2 ,
588 .Xr ptrace 2 ,
589 .Xr sigaction 2 ,
590 .Xr exit 3 ,
591 .Xr siginfo 3
592 .Sh STANDARDS
593 The
594 .Fn wait ,
595 .Fn waitpid ,
596 and
597 .Fn waitid
598 functions are defined by POSIX;
599 .Fn wait6 ,
600 .Fn wait4 ,
601 and
602 .Fn wait3
603 are not specified by POSIX.
604 The
605 .Fn WCOREDUMP
606 macro
607 and the ability to restart a pending
608 .Fn wait
609 call are extensions to the POSIX interface.
610 .Sh HISTORY
611 The
612 .Fn wait
613 function appeared in
614 .At v6 .