18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993, 1994 28269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 38269e767SBrooks Davis.\" 48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 68269e767SBrooks Davis.\" are met: 78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 88269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 108269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 118269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 138269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 148269e767SBrooks Davis.\" without specific prior written permission. 158269e767SBrooks Davis.\" 168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268269e767SBrooks Davis.\" SUCH DAMAGE. 278269e767SBrooks Davis.\" 28*d0675399SEdward Tomasz Napierala.Dd August 27, 2024 298269e767SBrooks Davis.Dt WAIT 2 308269e767SBrooks Davis.Os 318269e767SBrooks Davis.Sh NAME 328269e767SBrooks Davis.Nm wait , 338269e767SBrooks Davis.Nm waitid , 348269e767SBrooks Davis.Nm waitpid , 358269e767SBrooks Davis.Nm wait3 , 368269e767SBrooks Davis.Nm wait4 , 378269e767SBrooks Davis.Nm wait6 388269e767SBrooks Davis.Nd wait for processes to change status 398269e767SBrooks Davis.Sh LIBRARY 408269e767SBrooks Davis.Lb libc 418269e767SBrooks Davis.Sh SYNOPSIS 428269e767SBrooks Davis.In sys/wait.h 438269e767SBrooks Davis.Ft pid_t 448269e767SBrooks Davis.Fn wait "int *status" 458269e767SBrooks Davis.Ft pid_t 468269e767SBrooks Davis.Fn waitpid "pid_t wpid" "int *status" "int options" 478269e767SBrooks Davis.In signal.h 488269e767SBrooks Davis.Ft int 498269e767SBrooks Davis.Fn waitid "idtype_t idtype" "id_t id" "siginfo_t *info" "int options" 508269e767SBrooks Davis.In sys/time.h 518269e767SBrooks Davis.In sys/resource.h 528269e767SBrooks Davis.Ft pid_t 538269e767SBrooks Davis.Fn wait3 "int *status" "int options" "struct rusage *rusage" 548269e767SBrooks Davis.Ft pid_t 558269e767SBrooks Davis.Fn wait4 "pid_t wpid" "int *status" "int options" "struct rusage *rusage" 568269e767SBrooks Davis.Ft pid_t 578269e767SBrooks Davis.Fo wait6 588269e767SBrooks Davis.Fa "idtype_t idtype" "id_t id" 598269e767SBrooks Davis.Fa "int *status" 608269e767SBrooks Davis.Fa "int options" 618269e767SBrooks Davis.Fa "struct __wrusage *wrusage" 628269e767SBrooks Davis.Fa "siginfo_t *infop" 638269e767SBrooks Davis.Fc 648269e767SBrooks Davis.Sh DESCRIPTION 658269e767SBrooks DavisThe 668269e767SBrooks Davis.Fn wait 678269e767SBrooks Davisfunction suspends execution of its calling thread until 688269e767SBrooks Davis.Fa status 698269e767SBrooks Davisinformation is available for a child process 708269e767SBrooks Davisor a signal is received. 718269e767SBrooks DavisOn return from a successful 728269e767SBrooks Davis.Fn wait 738269e767SBrooks Daviscall, 748269e767SBrooks Davisthe 758269e767SBrooks Davis.Fa status 768269e767SBrooks Davisarea contains information about the process that reported a status change 778269e767SBrooks Davisas defined below. 788269e767SBrooks Davis.Pp 798269e767SBrooks DavisThe 808269e767SBrooks Davis.Fn wait4 818269e767SBrooks Davisand 828269e767SBrooks Davis.Fn wait6 838269e767SBrooks Davissystem calls provide a more general interface for programs 848269e767SBrooks Davisthat need to wait for specific child processes, 858269e767SBrooks Davisthat need resource utilization statistics accumulated by child processes, 868269e767SBrooks Davisor that require options. 878269e767SBrooks DavisThe other wait functions are implemented using either 888269e767SBrooks Davis.Fn wait4 898269e767SBrooks Davisor 908269e767SBrooks Davis.Fn wait6 . 918269e767SBrooks Davis.Pp 928269e767SBrooks DavisThe 938269e767SBrooks Davis.Fn wait6 948269e767SBrooks Davisfunction is the most general function in this family and its distinct 958269e767SBrooks Davisfeatures are: 968269e767SBrooks Davis.Pp 978269e767SBrooks DavisAll of the desired process statuses to be waited on must be explicitly 988269e767SBrooks Davisspecified in 998269e767SBrooks Davis.Fa options . 1008269e767SBrooks DavisThe 1018269e767SBrooks Davis.Fn wait , 1028269e767SBrooks Davis.Fn waitpid , 1038269e767SBrooks Davis.Fn wait3 , 1048269e767SBrooks Davisand 1058269e767SBrooks Davis.Fn wait4 1068269e767SBrooks Davisfunctions all implicitly wait for exited and trapped processes, 1078269e767SBrooks Davisbut the 1088269e767SBrooks Davis.Fn waitid 1098269e767SBrooks Davisand 1108269e767SBrooks Davis.Fn wait6 1118269e767SBrooks Davisfunctions require the corresponding 1128269e767SBrooks Davis.Dv WEXITED 1138269e767SBrooks Davisand 1148269e767SBrooks Davis.Dv WTRAPPED 1158269e767SBrooks Davisflags to be explicitly specified. 1168269e767SBrooks DavisThis allows waiting for processes which have experienced other 1178269e767SBrooks Davisstatus changes without having to also handle the exit status from 1188269e767SBrooks Davisterminated processes. 1198269e767SBrooks Davis.Pp 1208269e767SBrooks DavisThe 1218269e767SBrooks Davis.Fn wait6 1228269e767SBrooks Davisfunction accepts a 1238269e767SBrooks Davis.Fa wrusage 1248269e767SBrooks Davisargument which points to a structure defined as: 1258269e767SBrooks Davis.Bd -literal 1268269e767SBrooks Davisstruct __wrusage { 1278269e767SBrooks Davis struct rusage wru_self; 1288269e767SBrooks Davis struct rusage wru_children; 1298269e767SBrooks Davis}; 1308269e767SBrooks Davis.Ed 1318269e767SBrooks Davis.Pp 1328269e767SBrooks DavisThis allows the calling process to collect resource usage statistics 1338269e767SBrooks Davisfrom both its own child process as well as from its grand children. 1348269e767SBrooks DavisWhen no resource usage statistics are needed this pointer can be 1358269e767SBrooks Davis.Dv NULL . 1368269e767SBrooks Davis.Pp 1378269e767SBrooks DavisThe last argument 1388269e767SBrooks Davis.Fa infop 1398269e767SBrooks Davismust be either 1408269e767SBrooks Davis.Dv NULL 1418269e767SBrooks Davisor a pointer to a 1428269e767SBrooks Davis.Fa siginfo_t 1438269e767SBrooks Davisstructure. 1448269e767SBrooks DavisIf 1458269e767SBrooks Davis.Pf non- Dv NULL , 1468269e767SBrooks Davisthe structure is filled with the same data as for a 1478269e767SBrooks Davis.Dv SIGCHLD 1488269e767SBrooks Davissignal delivered when the process changed state. 1498269e767SBrooks Davis.Pp 1508269e767SBrooks DavisThe set of child processes to be queried is specified by the arguments 1518269e767SBrooks Davis.Fa idtype 1528269e767SBrooks Davisand 1538269e767SBrooks Davis.Fa id . 1548269e767SBrooks DavisThe separate 1558269e767SBrooks Davis.Fa idtype 1568269e767SBrooks Davisand 1578269e767SBrooks Davis.Fa id 1588269e767SBrooks Davisarguments support many other types of 1598269e767SBrooks Davisidentifiers in addition to process IDs and process group IDs. 1608269e767SBrooks Davis.Bl -bullet -offset indent 1618269e767SBrooks Davis.It 1628269e767SBrooks DavisIf 1638269e767SBrooks Davis.Fa idtype 1648269e767SBrooks Davisis 1658269e767SBrooks Davis.Dv P_PID , 1668269e767SBrooks Davis.Fn waitid 1678269e767SBrooks Davisand 1688269e767SBrooks Davis.Fn wait6 1698269e767SBrooks Daviswait for the child process with a process ID equal to 1708269e767SBrooks Davis.Dv (pid_t)id . 1718269e767SBrooks Davis.It 1728269e767SBrooks DavisIf 1738269e767SBrooks Davis.Fa idtype 1748269e767SBrooks Davisis 1758269e767SBrooks Davis.Dv P_PGID , 1768269e767SBrooks Davis.Fn waitid 1778269e767SBrooks Davisand 1788269e767SBrooks Davis.Fn wait6 1798269e767SBrooks Daviswait for the child process with a process group ID equal to 1808269e767SBrooks Davis.Dv (pid_t)id . 1818269e767SBrooks Davis.It 1828269e767SBrooks DavisIf 1838269e767SBrooks Davis.Fa idtype 1848269e767SBrooks Davisis 1858269e767SBrooks Davis.Dv P_ALL , 1868269e767SBrooks Davis.Fn waitid 1878269e767SBrooks Davisand 1888269e767SBrooks Davis.Fn wait6 1898269e767SBrooks Daviswait for any child process and the 1908269e767SBrooks Davis.Dv id 1918269e767SBrooks Davisis ignored. 1928269e767SBrooks Davis.It 1938269e767SBrooks DavisIf 1948269e767SBrooks Davis.Fa idtype 1958269e767SBrooks Davisis 1968269e767SBrooks Davis.Dv P_PID 1978269e767SBrooks Davisor 1988269e767SBrooks Davis.Dv P_PGID 1998269e767SBrooks Davisand the 2008269e767SBrooks Davis.Dv id 2018269e767SBrooks Davisis zero, 2028269e767SBrooks Davis.Fn waitid 2038269e767SBrooks Davisand 2048269e767SBrooks Davis.Fn wait6 2058269e767SBrooks Daviswait for any child process in the same process group as the caller. 2068269e767SBrooks Davis.El 2078269e767SBrooks Davis.Pp 2088269e767SBrooks DavisNon-standard identifier types supported by this 2098269e767SBrooks Davisimplementation of 2108269e767SBrooks Davis.Fn waitid 2118269e767SBrooks Davisand 2128269e767SBrooks Davis.Fn wait6 2138269e767SBrooks Davisare: 2148269e767SBrooks Davis.Bl -tag -width P_JAILID 2158269e767SBrooks Davis.It Dv P_UID 2168269e767SBrooks DavisWait for processes whose effective user ID is equal to 2178269e767SBrooks Davis.Dv (uid_t) Fa id . 2188269e767SBrooks Davis.It Dv P_GID 2198269e767SBrooks DavisWait for processes whose effective group ID is equal to 2208269e767SBrooks Davis.Dv (gid_t) Fa id . 2218269e767SBrooks Davis.It Dv P_SID 2228269e767SBrooks DavisWait for processes whose session ID is equal to 2238269e767SBrooks Davis.Fa id . 2248269e767SBrooks Davis.\" This is just how sessions work, not sure this needs to be documented here 2258269e767SBrooks DavisIf the child process started its own session, 2268269e767SBrooks Davisits session ID will be the same as its process ID. 2278269e767SBrooks DavisOtherwise the session ID of a child process will match the caller's session ID. 2288269e767SBrooks Davis.It Dv P_JAILID 2298269e767SBrooks DavisWaits for processes within a jail whose jail identifier is equal to 2308269e767SBrooks Davis.Fa id . 2318269e767SBrooks Davis.El 2328269e767SBrooks Davis.Pp 2338269e767SBrooks DavisFor the 2348269e767SBrooks Davis.Fn waitpid 2358269e767SBrooks Davisand 2368269e767SBrooks Davis.Fn wait4 2378269e767SBrooks Davisfunctions, the single 2388269e767SBrooks Davis.Fa wpid 2398269e767SBrooks Davisargument specifies the set of child processes for which to wait. 2408269e767SBrooks Davis.Bl -bullet -offset indent 2418269e767SBrooks Davis.It 2428269e767SBrooks DavisIf 2438269e767SBrooks Davis.Fa wpid 2448269e767SBrooks Davisis -1, the call waits for any child process. 2458269e767SBrooks Davis.It 2468269e767SBrooks DavisIf 2478269e767SBrooks Davis.Fa wpid 2488269e767SBrooks Davisis 0, 2498269e767SBrooks Davisthe call waits for any child process in the process group of the caller. 2508269e767SBrooks Davis.It 2518269e767SBrooks DavisIf 2528269e767SBrooks Davis.Fa wpid 2538269e767SBrooks Davisis greater than zero, the call waits for the process with process ID 2548269e767SBrooks Davis.Fa wpid . 2558269e767SBrooks Davis.It 2568269e767SBrooks DavisIf 2578269e767SBrooks Davis.Fa wpid 2588269e767SBrooks Davisis less than -1, the call waits for any process whose process group ID 2598269e767SBrooks Davisequals the absolute value of 2608269e767SBrooks Davis.Fa wpid . 2618269e767SBrooks Davis.El 2628269e767SBrooks Davis.Pp 2638269e767SBrooks DavisThe 2648269e767SBrooks Davis.Fa status 2658269e767SBrooks Davisargument is defined below. 2668269e767SBrooks Davis.Pp 2678269e767SBrooks DavisThe 2688269e767SBrooks Davis.Fa options 2698269e767SBrooks Davisargument contains the bitwise OR of any of the following options. 2708269e767SBrooks Davis.Bl -tag -width WCONTINUED 2718269e767SBrooks Davis.It Dv WCONTINUED 2728269e767SBrooks DavisReport the status of selected processes that 2738269e767SBrooks Davishave continued from a job control stop by receiving a 2748269e767SBrooks Davis.Dv SIGCONT 2758269e767SBrooks Davissignal. 2768269e767SBrooks Davis.It Dv WNOHANG 2778269e767SBrooks DavisDo not block when 2788269e767SBrooks Davisthere are no processes wishing to report status. 2798269e767SBrooks Davis.It Dv WUNTRACED 2808269e767SBrooks DavisReport the status of selected processes which are stopped due to a 2818269e767SBrooks Davis.Dv SIGTTIN , SIGTTOU , SIGTSTP , 2828269e767SBrooks Davisor 2838269e767SBrooks Davis.Dv SIGSTOP 2848269e767SBrooks Davissignal. 2858269e767SBrooks Davis.It Dv WSTOPPED 2868269e767SBrooks DavisAn alias for 2878269e767SBrooks Davis.Dv WUNTRACED . 2888269e767SBrooks Davis.It Dv WTRAPPED 2898269e767SBrooks DavisReport the status of selected processes which are being traced via 2908269e767SBrooks Davis.Xr ptrace 2 2918269e767SBrooks Davisand have trapped or reached a breakpoint. 2928269e767SBrooks DavisThis flag is implicitly set for the functions 2938269e767SBrooks Davis.Fn wait , 2948269e767SBrooks Davis.Fn waitpid , 2958269e767SBrooks Davis.Fn wait3 , 2968269e767SBrooks Davisand 2978269e767SBrooks Davis.Fn wait4 . 2988269e767SBrooks Davis.br 2998269e767SBrooks DavisFor the 3008269e767SBrooks Davis.Fn waitid 3018269e767SBrooks Davisand 3028269e767SBrooks Davis.Fn wait6 3038269e767SBrooks Davisfunctions, the flag has to be explicitly included in 3048269e767SBrooks Davis.Fa options 3058269e767SBrooks Davisif status reports from trapped processes are expected. 3068269e767SBrooks Davis.It Dv WEXITED 3078269e767SBrooks DavisReport the status of selected processes which have terminated. 3088269e767SBrooks DavisThis flag is implicitly set for the functions 3098269e767SBrooks Davis.Fn wait , 3108269e767SBrooks Davis.Fn waitpid , 3118269e767SBrooks Davis.Fn wait3 , 3128269e767SBrooks Davisand 3138269e767SBrooks Davis.Fn wait4 . 3148269e767SBrooks Davis.br 3158269e767SBrooks DavisFor the 3168269e767SBrooks Davis.Fn waitid 3178269e767SBrooks Davisand 3188269e767SBrooks Davis.Fn wait6 3198269e767SBrooks Davisfunctions, the flag has to be explicitly included in 3208269e767SBrooks Davis.Fa options 3218269e767SBrooks Davisif status reports from terminated processes are expected. 3228269e767SBrooks Davis.It Dv WNOWAIT 3238269e767SBrooks DavisKeep the process whose status is returned in a waitable state. 3248269e767SBrooks DavisThe process may be waited for again after this call completes. 3258269e767SBrooks Davis.El 3268269e767SBrooks Davis.sp 3278269e767SBrooks DavisFor the 3288269e767SBrooks Davis.Fn waitid 3298269e767SBrooks Davisand 3308269e767SBrooks Davis.Fn wait6 3318269e767SBrooks Davisfunctions, at least one of the options 3328269e767SBrooks Davis.Dv WEXITED , 3338269e767SBrooks Davis.Dv WUNTRACED , 3348269e767SBrooks Davis.Dv WSTOPPED , 3358269e767SBrooks Davis.Dv WTRAPPED , 3368269e767SBrooks Davisor 3378269e767SBrooks Davis.Dv WCONTINUED 3388269e767SBrooks Davismust be specified. 3398269e767SBrooks DavisOtherwise there will be no events for the call to report. 3408269e767SBrooks DavisTo avoid hanging indefinitely in such a case these functions 3418269e767SBrooks Davisreturn -1 with 3428269e767SBrooks Davis.Dv errno 3438269e767SBrooks Davisset to 3448269e767SBrooks Davis.Dv EINVAL . 3458269e767SBrooks Davis.Pp 3468269e767SBrooks DavisIf 3478269e767SBrooks Davis.Fa rusage 3488269e767SBrooks Davisis non-NULL, a summary of the resources used by the terminated 3498269e767SBrooks Davisprocess and all its children is returned. 3508269e767SBrooks Davis.Pp 3518269e767SBrooks DavisIf 3528269e767SBrooks Davis.Fa wrusage 3538269e767SBrooks Davisis non-NULL, separate summaries are returned for the resources used 3548269e767SBrooks Davisby the terminated process and the resources used by all its children. 3558269e767SBrooks Davis.Pp 3568269e767SBrooks DavisIf 3578269e767SBrooks Davis.Fa infop 3588269e767SBrooks Davisis non-NULL, a 3598269e767SBrooks Davis.Dv siginfo_t 3608269e767SBrooks Davisstructure is returned with the 3618269e767SBrooks Davis.Fa si_signo 3628269e767SBrooks Davisfield set to 3638269e767SBrooks Davis.Dv SIGCHLD 3648269e767SBrooks Davisand the 3658269e767SBrooks Davis.Fa si_pid 3668269e767SBrooks Davisfield set to the process ID of the process reporting status. 3678269e767SBrooks DavisFor the exited process, the 3688269e767SBrooks Davis.Fa si_status 3698269e767SBrooks Davisfield of the 3708269e767SBrooks Davis.Dv siginfo_t 3718269e767SBrooks Davisstructure contains the full 32 bit exit status passed to 3728269e767SBrooks Davis.Xr _exit 2 ; 3738269e767SBrooks Davisthe 3748269e767SBrooks Davis.Fa status 3758269e767SBrooks Davisargument of other calls only returns 8 lowest bits of the exit status. 3768269e767SBrooks Davis.Pp 3778269e767SBrooks DavisWhen the 3788269e767SBrooks Davis.Dv WNOHANG 3798269e767SBrooks Davisoption is specified and no processes 3808269e767SBrooks Daviswish to report status, 3818269e767SBrooks Davis.Fn waitid 3828269e767SBrooks Davissets the 3838269e767SBrooks Davis.Fa si_signo 3848269e767SBrooks Davisand 3858269e767SBrooks Davis.Fa si_pid 3868269e767SBrooks Davisfields in 3878269e767SBrooks Davis.Fa infop 3888269e767SBrooks Davisto zero. 3898269e767SBrooks DavisChecking these fields is the only way to know if a status change was reported. 3908269e767SBrooks Davis.Pp 3918269e767SBrooks DavisWhen the 3928269e767SBrooks Davis.Dv WNOHANG 3938269e767SBrooks Davisoption is specified and no processes 3948269e767SBrooks Daviswish to report status, 3958269e767SBrooks Davis.Fn wait4 3968269e767SBrooks Davisand 3978269e767SBrooks Davis.Fn wait6 3988269e767SBrooks Davisreturn a 3998269e767SBrooks Davisprocess id 4008269e767SBrooks Davisof 0. 4018269e767SBrooks Davis.Pp 4028269e767SBrooks DavisThe 4038269e767SBrooks Davis.Fn wait 4048269e767SBrooks Daviscall is the same as 4058269e767SBrooks Davis.Fn wait4 4068269e767SBrooks Daviswith a 4078269e767SBrooks Davis.Fa wpid 4088269e767SBrooks Davisvalue of -1, 4098269e767SBrooks Daviswith an 4108269e767SBrooks Davis.Fa options 4118269e767SBrooks Davisvalue of zero, 4128269e767SBrooks Davisand a 4138269e767SBrooks Davis.Fa rusage 4148269e767SBrooks Davisvalue of 4158269e767SBrooks Davis.Dv NULL . 4168269e767SBrooks DavisThe 4178269e767SBrooks Davis.Fn waitpid 4188269e767SBrooks Davisfunction is identical to 4198269e767SBrooks Davis.Fn wait4 4208269e767SBrooks Daviswith an 4218269e767SBrooks Davis.Fa rusage 4228269e767SBrooks Davisvalue of 4238269e767SBrooks Davis.Dv NULL . 4248269e767SBrooks DavisThe older 4258269e767SBrooks Davis.Fn wait3 4268269e767SBrooks Daviscall is the same as 4278269e767SBrooks Davis.Fn wait4 4288269e767SBrooks Daviswith a 4298269e767SBrooks Davis.Fa wpid 4308269e767SBrooks Davisvalue of -1. 4318269e767SBrooks DavisThe 4328269e767SBrooks Davis.Fn wait4 4338269e767SBrooks Davisfunction is identical to 4348269e767SBrooks Davis.Fn wait6 4358269e767SBrooks Daviswith the flags 4368269e767SBrooks Davis.Dv WEXITED 4378269e767SBrooks Davisand 4388269e767SBrooks Davis.Dv WTRAPPED 4398269e767SBrooks Davisset in 4408269e767SBrooks Davis.Fa options 4418269e767SBrooks Davisand 4428269e767SBrooks Davis.Fa infop 4438269e767SBrooks Davisset to 4448269e767SBrooks Davis.Dv NULL . 4458269e767SBrooks Davis.Pp 4468269e767SBrooks DavisThe following macros may be used to test the current status of the process. 4478269e767SBrooks DavisExactly one of the following four macros will evaluate to a non-zero 4488269e767SBrooks Davis.Pq true 4498269e767SBrooks Davisvalue: 4508269e767SBrooks Davis.Bl -tag -width Ds 4518269e767SBrooks Davis.It Fn WIFCONTINUED status 4528269e767SBrooks DavisTrue if the process has not terminated, and 4538269e767SBrooks Davishas continued after a job control stop. 4548269e767SBrooks DavisThis macro can be true only if the wait call specified the 4558269e767SBrooks Davis.Dv WCONTINUED 4568269e767SBrooks Davisoption. 4578269e767SBrooks Davis.It Fn WIFEXITED status 4588269e767SBrooks DavisTrue if the process terminated normally by a call to 4598269e767SBrooks Davis.Xr _exit 2 4608269e767SBrooks Davisor 4618269e767SBrooks Davis.Xr exit 3 . 4628269e767SBrooks Davis.It Fn WIFSIGNALED status 4638269e767SBrooks DavisTrue if the process terminated due to receipt of a signal. 4648269e767SBrooks Davis.It Fn WIFSTOPPED status 4658269e767SBrooks DavisTrue if the process has not terminated, but has stopped and can be restarted. 4668269e767SBrooks DavisThis macro can be true only if the wait call specified the 4678269e767SBrooks Davis.Dv WUNTRACED 4688269e767SBrooks Davisoption 4698269e767SBrooks Davisor if the child process is being traced (see 4708269e767SBrooks Davis.Xr ptrace 2 ) . 4718269e767SBrooks Davis.El 4728269e767SBrooks Davis.Pp 4738269e767SBrooks DavisDepending on the values of those macros, the following macros 4748269e767SBrooks Davisproduce the remaining status information about the child process: 4758269e767SBrooks Davis.Bl -tag -width Ds 4768269e767SBrooks Davis.It Fn WEXITSTATUS status 4778269e767SBrooks DavisIf 4788269e767SBrooks Davis.Fn WIFEXITED status 4798269e767SBrooks Davisis true, evaluates to the low-order 8 bits 4808269e767SBrooks Davisof the argument passed to 4818269e767SBrooks Davis.Xr _exit 2 4828269e767SBrooks Davisor 4838269e767SBrooks Davis.Xr exit 3 4848269e767SBrooks Davisby the child. 4858269e767SBrooks Davis.It Fn WTERMSIG status 4868269e767SBrooks DavisIf 4878269e767SBrooks Davis.Fn WIFSIGNALED status 4888269e767SBrooks Davisis true, evaluates to the number of the signal 4898269e767SBrooks Davisthat caused the termination of the process. 4908269e767SBrooks Davis.It Fn WCOREDUMP status 4918269e767SBrooks DavisIf 4928269e767SBrooks Davis.Fn WIFSIGNALED status 4938269e767SBrooks Davisis true, evaluates as true if the termination 4948269e767SBrooks Davisof the process was accompanied by the creation of a core file 4958269e767SBrooks Daviscontaining an image of the process when the signal was received. 4968269e767SBrooks Davis.It Fn WSTOPSIG status 4978269e767SBrooks DavisIf 4988269e767SBrooks Davis.Fn WIFSTOPPED status 4998269e767SBrooks Davisis true, evaluates to the number of the signal 5008269e767SBrooks Davisthat caused the process to stop. 5018269e767SBrooks Davis.El 5028269e767SBrooks Davis.Sh NOTES 5038269e767SBrooks DavisSee 5048269e767SBrooks Davis.Xr sigaction 2 5058269e767SBrooks Davisfor a list of termination signals. 5068269e767SBrooks DavisA status of 0 indicates normal termination. 5078269e767SBrooks Davis.Pp 5088269e767SBrooks DavisIf a parent process terminates without 5098269e767SBrooks Daviswaiting for all of its child processes to terminate, 5108269e767SBrooks Davisthe remaining child processes are re-assigned to the reaper 5118269e767SBrooks Davisof the exiting process as the parent, see 5128269e767SBrooks Davis.Xr procctl 2 5138269e767SBrooks Davis.Dv PROC_REAP_ACQUIRE . 5148269e767SBrooks DavisIf no specific reaper was assigned, the process with ID 1, the init process, 5158269e767SBrooks Davisbecomes the parent of the orphaned children by default. 5168269e767SBrooks Davis.Pp 5178269e767SBrooks DavisIf a signal is caught while any of the 5188269e767SBrooks Davis.Fn wait 5198269e767SBrooks Daviscalls are pending, 5208269e767SBrooks Davisthe call may be interrupted or restarted when the signal-catching routine 5218269e767SBrooks Davisreturns, 5228269e767SBrooks Davisdepending on the options in effect for the signal; 5238269e767SBrooks Davissee discussion of 5248269e767SBrooks Davis.Dv SA_RESTART 5258269e767SBrooks Davisin 5268269e767SBrooks Davis.Xr sigaction 2 . 5278269e767SBrooks Davis.Pp 5288269e767SBrooks DavisThe implementation queues one 5298269e767SBrooks Davis.Dv SIGCHLD 5308269e767SBrooks Davissignal for each child process whose 5318269e767SBrooks Davisstatus has changed; if 5328269e767SBrooks Davis.Fn wait 5338269e767SBrooks Davisreturns because the status of a child process is available, the pending 5348269e767SBrooks DavisSIGCHLD signal associated with the process ID of the child process will 5358269e767SBrooks Davisbe discarded. 5368269e767SBrooks DavisAny other pending 5378269e767SBrooks Davis.Dv SIGCHLD 5388269e767SBrooks Davissignals remain pending. 5398269e767SBrooks Davis.Pp 5408269e767SBrooks DavisIf 5418269e767SBrooks Davis.Dv SIGCHLD 5428269e767SBrooks Davisis blocked and 5438269e767SBrooks Davis.Fn wait 5448269e767SBrooks Davisreturns because the status of a child process is available, the pending 5458269e767SBrooks Davis.Dv SIGCHLD 5468269e767SBrooks Davissignal will be cleared unless another status of the child process 5478269e767SBrooks Davisis available. 5488269e767SBrooks Davis.Sh RETURN VALUES 5498269e767SBrooks DavisIf 5508269e767SBrooks Davis.Fn wait 5518269e767SBrooks Davisreturns due to a stopped, continued, 5528269e767SBrooks Davisor terminated child process, the process ID of the child 5538269e767SBrooks Davisis returned to the calling process. 5548269e767SBrooks DavisOtherwise, a value of \-1 5558269e767SBrooks Davisis returned and 5568269e767SBrooks Davis.Va errno 5578269e767SBrooks Davisis set to indicate the error. 5588269e767SBrooks Davis.Pp 5598269e767SBrooks DavisIf 5608269e767SBrooks Davis.Fn wait6 , 5618269e767SBrooks Davis.Fn wait4 , 5628269e767SBrooks Davis.Fn wait3 , 5638269e767SBrooks Davisor 5648269e767SBrooks Davis.Fn waitpid 5658269e767SBrooks Davisreturns due to a stopped, continued, 5668269e767SBrooks Davisor terminated child process, the process ID of the child 5678269e767SBrooks Davisis returned to the calling process. 5688269e767SBrooks DavisIf there are no children not previously awaited, 5698269e767SBrooks Davis-1 is returned with 5708269e767SBrooks Davis.Va errno 5718269e767SBrooks Davisset to 5728269e767SBrooks Davis.Er ECHILD . 5738269e767SBrooks DavisOtherwise, if 5748269e767SBrooks Davis.Dv WNOHANG 5758269e767SBrooks Davisis specified and there are 5768269e767SBrooks Davisno stopped, continued or exited children, 5778269e767SBrooks Davis0 is returned. 5788269e767SBrooks DavisIf an error is detected or a caught signal aborts the call, 5798269e767SBrooks Davisa value of -1 5808269e767SBrooks Davisis returned and 5818269e767SBrooks Davis.Va errno 5828269e767SBrooks Davisis set to indicate the error. 5838269e767SBrooks Davis.Pp 5848269e767SBrooks DavisIf 5858269e767SBrooks Davis.Fn waitid 5868269e767SBrooks Davisreturns because one or more processes have a state change to report, 5878269e767SBrooks Davis0 is returned. 5888269e767SBrooks DavisIf an error is detected, 5898269e767SBrooks Davisa value of -1 5908269e767SBrooks Davisis returned and 5918269e767SBrooks Davis.Va errno 5928269e767SBrooks Davisis set to indicate the error. 5938269e767SBrooks DavisIf 5948269e767SBrooks Davis.Dv WNOHANG 5958269e767SBrooks Davisis specified and there are 5968269e767SBrooks Davisno stopped, continued or exited children, 5978269e767SBrooks Davis0 is returned. 5988269e767SBrooks DavisThe 5998269e767SBrooks Davis.Fa si_signo 6008269e767SBrooks Davisand 6018269e767SBrooks Davis.Fa si_pid 6028269e767SBrooks Davisfields of 6038269e767SBrooks Davis.Fa infop 6048269e767SBrooks Davismust be checked against zero to determine if a process reported status. 6058269e767SBrooks Davis.Pp 6068269e767SBrooks DavisThe 6078269e767SBrooks Davis.Fn wait 608*d0675399SEdward Tomasz Napieralafamily of functions will only return a child process created with 6098269e767SBrooks Davis.Xr pdfork 2 610*d0675399SEdward Tomasz Napieralaif the calling process is not in 611*d0675399SEdward Tomasz Napierala.Xr capsicum 4 612*d0675399SEdward Tomasz Napieralacapability mode, and 613*d0675399SEdward Tomasz Napierala.Nm 614*d0675399SEdward Tomasz Napieralahas been explicitly given the child's process ID. 6158269e767SBrooks Davis.Sh ERRORS 6168269e767SBrooks DavisThe 6178269e767SBrooks Davis.Fn wait 6188269e767SBrooks Davisfunction 6198269e767SBrooks Daviswill fail and return immediately if: 6208269e767SBrooks Davis.Bl -tag -width Er 6218269e767SBrooks Davis.It Bq Er ECHILD 6228269e767SBrooks DavisThe calling process has no existing unwaited-for 6238269e767SBrooks Davischild processes. 6248269e767SBrooks Davis.It Bq Er ECHILD 6258269e767SBrooks DavisNo status from the terminated child process is available 6268269e767SBrooks Davisbecause the calling process has asked the system to discard 6278269e767SBrooks Davissuch status by ignoring the signal 6288269e767SBrooks Davis.Dv SIGCHLD 6298269e767SBrooks Davisor setting the flag 6308269e767SBrooks Davis.Dv SA_NOCLDWAIT 6318269e767SBrooks Davisfor that signal. 6328269e767SBrooks Davis.It Bq Er EFAULT 6338269e767SBrooks DavisThe 6348269e767SBrooks Davis.Fa status 6358269e767SBrooks Davisor 6368269e767SBrooks Davis.Fa rusage 6378269e767SBrooks Davisargument points to an illegal address. 6388269e767SBrooks Davis(May not be detected before exit of a child process.) 6398269e767SBrooks Davis.It Bq Er EINTR 6408269e767SBrooks DavisThe call was interrupted by a caught signal, 6418269e767SBrooks Davisor the signal did not have the 6428269e767SBrooks Davis.Dv SA_RESTART 6438269e767SBrooks Davisflag set. 6448269e767SBrooks Davis.It Bq Er EINVAL 6458269e767SBrooks DavisAn invalid value was specified for 6468269e767SBrooks Davis.Fa options , 6478269e767SBrooks Davisor 6488269e767SBrooks Davis.Fa idtype 6498269e767SBrooks Davisand 6508269e767SBrooks Davis.Fa id 6518269e767SBrooks Davisdo not specify a valid set of processes. 6528269e767SBrooks Davis.El 6538269e767SBrooks Davis.Sh SEE ALSO 6548269e767SBrooks Davis.Xr _exit 2 , 6558269e767SBrooks Davis.Xr procctl 2 , 6568269e767SBrooks Davis.Xr ptrace 2 , 6578269e767SBrooks Davis.Xr sigaction 2 , 6588269e767SBrooks Davis.Xr exit 3 , 6598269e767SBrooks Davis.Xr siginfo 3 6608269e767SBrooks Davis.Sh STANDARDS 6618269e767SBrooks DavisThe 6628269e767SBrooks Davis.Fn wait , 6638269e767SBrooks Davis.Fn waitpid , 6648269e767SBrooks Davisand 6658269e767SBrooks Davis.Fn waitid 6668269e767SBrooks Davisfunctions are defined by POSIX; 6678269e767SBrooks Davis.Fn wait6 , 6688269e767SBrooks Davis.Fn wait4 , 6698269e767SBrooks Davisand 6708269e767SBrooks Davis.Fn wait3 6718269e767SBrooks Davisare not specified by POSIX. 6728269e767SBrooks DavisThe 6738269e767SBrooks Davis.Fn WCOREDUMP 6748269e767SBrooks Davismacro 6758269e767SBrooks Davisis an extension to the POSIX interface. 6768269e767SBrooks Davis.Pp 6778269e767SBrooks DavisThe ability to use the 6788269e767SBrooks Davis.Dv WNOWAIT 6798269e767SBrooks Davisflag with 6808269e767SBrooks Davis.Fn waitpid 6818269e767SBrooks Davisis an extension; 6828269e767SBrooks Davis.Tn POSIX 6838269e767SBrooks Davisonly permits this flag with 6848269e767SBrooks Davis.Fn waitid . 6858269e767SBrooks Davis.Sh HISTORY 6868269e767SBrooks DavisThe 6878269e767SBrooks Davis.Fn wait 6888269e767SBrooks Davisfunction appeared in 6898269e767SBrooks Davis.At v1 . 690