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