1 /* Copyright (C) 1991-1994,1996-2001,2003,2004,2005,2007,2009,2010,2011,2012
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 /*
20  *	POSIX Standard: 3.2.1 Wait for Process Termination	<sys/wait.h>
21  */
22 
23 #ifndef	_SYS_WAIT_H
24 #define	_SYS_WAIT_H	1
25 
26 #include <features.h>
27 
28 __BEGIN_DECLS
29 
30 #include <signal.h>
31 
32 /* These macros could also be defined in <stdlib.h>.  */
33 #if !defined _STDLIB_H || (!defined __USE_XOPEN && !defined __USE_XOPEN2K8)
34 /* This will define the `W*' macros for the flag
35    bits to `waitpid', `wait3', and `wait4'.  */
36 # include <bits/waitflags.h>
37 
38 # ifdef	__USE_BSD
39 
40 /* Lots of hair to allow traditional BSD use of `union wait'
41    as well as POSIX.1 use of `int' for the status word.  */
42 
43 #  if defined __GNUC__ && !defined __cplusplus
44 #   define __WAIT_INT(status) \
45   (__extension__ (((union { __typeof(status) __in; int __i; }) \
46 		   { .__in = (status) }).__i))
47 #  else
48 #   define __WAIT_INT(status)	(*(const int *) &(status))
49 #  endif
50 
51 /* This is the type of the argument to `wait'.  The funky union
52    causes redeclarations with either `int *' or `union wait *' to be
53    allowed without complaint.  __WAIT_STATUS_DEFN is the type used in
54    the actual function definitions.  */
55 
56 #  if !defined __GNUC__ || __GNUC__ < 2 || defined __cplusplus
57 #   define __WAIT_STATUS	void *
58 #   define __WAIT_STATUS_DEFN	void *
59 #  else
60 /* This works in GCC 2.6.1 and later.  */
61 typedef union
62   {
63     union wait *__uptr;
64     int *__iptr;
65   } __WAIT_STATUS __attribute__ ((__transparent_union__));
66 #   define __WAIT_STATUS_DEFN	int *
67 #  endif
68 
69 # else /* Don't use BSD.  */
70 
71 #  define __WAIT_INT(status)	(status)
72 #  define __WAIT_STATUS		int *
73 #  define __WAIT_STATUS_DEFN	int *
74 
75 # endif /* Use BSD.  */
76 
77 /* This will define all the `__W*' macros.  */
78 # include <bits/waitstatus.h>
79 
80 # define WEXITSTATUS(status)	__WEXITSTATUS (__WAIT_INT (status))
81 # define WTERMSIG(status)	__WTERMSIG (__WAIT_INT (status))
82 # define WSTOPSIG(status)	__WSTOPSIG (__WAIT_INT (status))
83 # define WIFEXITED(status)	__WIFEXITED (__WAIT_INT (status))
84 # define WIFSIGNALED(status)	__WIFSIGNALED (__WAIT_INT (status))
85 # define WIFSTOPPED(status)	__WIFSTOPPED (__WAIT_INT (status))
86 # ifdef __WIFCONTINUED
87 #  define WIFCONTINUED(status)	__WIFCONTINUED (__WAIT_INT (status))
88 # endif
89 #endif	/* <stdlib.h> not included.  */
90 
91 #ifdef	__USE_BSD
92 # define WCOREFLAG		__WCOREFLAG
93 # define WCOREDUMP(status)	__WCOREDUMP (__WAIT_INT (status))
94 # define W_EXITCODE(ret, sig)	__W_EXITCODE (ret, sig)
95 # define W_STOPCODE(sig)	__W_STOPCODE (sig)
96 #endif
97 
98 /* The following values are used by the `waitid' function.  */
99 #if defined __USE_SVID || defined __USE_XOPEN || defined __USE_XOPEN2K8
100 typedef enum
101 {
102   P_ALL,		/* Wait for any child.  */
103   P_PID,		/* Wait for specified process.  */
104   P_PGID		/* Wait for members of process group.  */
105 } idtype_t;
106 #endif
107 
108 
109 /* Wait for a child to die.  When one does, put its status in *STAT_LOC
110    and return its process ID.  For errors, return (pid_t) -1.
111 
112    This function is a cancellation point and therefore not marked with
113    __THROW.  */
114 extern __pid_t wait (__WAIT_STATUS __stat_loc);
115 
116 #ifdef	__USE_BSD
117 /* Special values for the PID argument to `waitpid' and `wait4'.  */
118 # define WAIT_ANY	(-1)	/* Any process.  */
119 # define WAIT_MYPGRP	0	/* Any process in my process group.  */
120 #endif
121 
122 /* Wait for a child matching PID to die.
123    If PID is greater than 0, match any process whose process ID is PID.
124    If PID is (pid_t) -1, match any process.
125    If PID is (pid_t) 0, match any process with the
126    same process group as the current process.
127    If PID is less than -1, match any process whose
128    process group is the absolute value of PID.
129    If the WNOHANG bit is set in OPTIONS, and that child
130    is not already dead, return (pid_t) 0.  If successful,
131    return PID and store the dead child's status in STAT_LOC.
132    Return (pid_t) -1 for errors.  If the WUNTRACED bit is
133    set in OPTIONS, return status for stopped children; otherwise don't.
134 
135    This function is a cancellation point and therefore not marked with
136    __THROW.  */
137 extern __pid_t waitpid (__pid_t __pid, int *__stat_loc, int __options);
138 
139 #if defined __USE_SVID || defined __USE_XOPEN || defined __USE_XOPEN2K8
140 # ifndef __id_t_defined
141 #  include <bits/types.h>
142 typedef __id_t id_t;
143 #  define __id_t_defined
144 # endif
145 
146 # define __need_siginfo_t
147 # include <bits/siginfo.h>
148 
149 /* Wait for a childing matching IDTYPE and ID to change the status and
150    place appropriate information in *INFOP.
151    If IDTYPE is P_PID, match any process whose process ID is ID.
152    If IDTYPE is P_PGID, match any process whose process group is ID.
153    If IDTYPE is P_ALL, match any process.
154    If the WNOHANG bit is set in OPTIONS, and that child
155    is not already dead, clear *INFOP and return 0.  If successful, store
156    exit code and status in *INFOP.
157 
158    This function is a cancellation point and therefore not marked with
159    __THROW.  */
160 extern int waitid (idtype_t __idtype, __id_t __id, siginfo_t *__infop,
161 		   int __options);
162 #endif
163 
164 #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
165 /* This being here makes the prototypes valid whether or not
166    we have already included <sys/resource.h> to define `struct rusage'.  */
167 struct rusage;
168 
169 /* Wait for a child to exit.  When one does, put its status in *STAT_LOC and
170    return its process ID.  For errors return (pid_t) -1.  If USAGE is not
171    nil, store information about the child's resource usage there.  If the
172    WUNTRACED bit is set in OPTIONS, return status for stopped children;
173    otherwise don't.  */
174 extern __pid_t wait3 (__WAIT_STATUS __stat_loc, int __options,
175 		      struct rusage * __usage) __THROWNL;
176 #endif
177 
178 #ifdef __USE_BSD
179 /* PID is like waitpid.  Other args are like wait3.  */
180 extern __pid_t wait4 (__pid_t __pid, __WAIT_STATUS __stat_loc, int __options,
181 		      struct rusage *__usage) __THROWNL;
182 #endif /* Use BSD.  */
183 
184 
185 __END_DECLS
186 
187 #endif /* sys/wait.h  */
188