1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
26 * pid < -1 WUNTRACED may not be fully supported
27 * process group specifics ignored by non-{waitpid,wait4}
43 extern int wait4(int, int*, int, struct rusage
*);
46 waitpid(pid_t pid
, int* status
, int flags
)
48 return(wait4(pid
, status
, flags
, NiL
));
57 extern int wait3(int*, int, struct rusage
*);
63 #define wait3(s,f,u) wait2(s,f)
65 extern int wait2(int*, int);
71 #define wait3(s,f,u) wait(s)
97 waitpid(pid_t pid
, int* status
, int flags
)
99 register struct zombie
* zp
;
100 register struct zombie
* pp
;
103 #if !_lib_wait2 && !_lib_wait3
108 Sig_handler_t handler
;
111 static struct zombie
* zombies
;
117 if (zp
->pid
>= 0 && (zp
->pid
== pid
|| pid
<= 0))
119 if (pp
) pp
->next
= zp
->next
;
120 else zombies
= zp
->next
;
121 if (status
) *status
= zp
->status
;
127 if (pid
> 0 && kill(pid
, 0) < 0) return(-1);
130 #if !_lib_wait2 && !_lib_wait3
138 handler
= signal(SIGCLD
, catch);
141 signal(SIGCLD
, handler
);
146 handler
= signal(SIGALRM
, catch);
152 p
= wait3(&s
, flags
, NiL
);
156 if (flags
& WNOHANG
) signal(SIGCLD
, handler
);
161 if (n
== 0 && !caught
|| n
== 1) alarm(n
);
162 else if (n
> 1) alarm(n
- caught
);
163 signal(SIGALRM
, handler
);
165 if (p
== -1 && errno
== EINTR
)
174 if (p
== -1 && errno
== EINVAL
&& (flags
& ~WNOHANG
))
175 p
= wait3(&s
, flags
& WNOHANG
, NiL
);
180 if (p
== 0 && status
) *status
= s
;
183 if (pid
<= 0 || p
== pid
)
185 if (status
) *status
= s
;
188 if (!(zp
= newof(0, struct zombie
, 1, 0))) return(-1);