sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libast / common / comp / waitpid.c
blob1b09472709c3e103e25d07ebb115fb62e91c033b
1 /***********************************************************************
2 * *
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 *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 * POSIX waitpid()
26 * pid < -1 WUNTRACED may not be fully supported
27 * process group specifics ignored by non-{waitpid,wait4}
30 #include <ast.h>
31 #include <wait.h>
33 #if _lib_waitpid
35 NoN(waitpid)
37 #else
39 #if _lib_wait4
41 struct rusage;
43 extern int wait4(int, int*, int, struct rusage*);
45 pid_t
46 waitpid(pid_t pid, int* status, int flags)
48 return(wait4(pid, status, flags, NiL));
51 #else
53 #undef SIGCLD
55 #if _lib_wait3
57 extern int wait3(int*, int, struct rusage*);
59 #else
61 #if _lib_wait2
63 #define wait3(s,f,u) wait2(s,f)
65 extern int wait2(int*, int);
67 #else
69 #include <sig.h>
71 #define wait3(s,f,u) wait(s)
73 static int caught;
75 static void
76 catch(sig)
77 int sig;
79 NoP(sig);
80 caught = 1;
83 #endif
85 #endif
87 #include <error.h>
89 struct zombie
91 struct zombie* next;
92 int status;
93 pid_t pid;
96 pid_t
97 waitpid(pid_t pid, int* status, int flags)
99 register struct zombie* zp;
100 register struct zombie* pp;
101 register int p;
102 int s;
103 #if !_lib_wait2 && !_lib_wait3
104 #if !defined(SIGCLD)
105 int n;
106 int oerrno;
107 #endif
108 Sig_handler_t handler;
109 #endif
111 static struct zombie* zombies;
113 pp = 0;
114 zp = zombies;
115 while (zp)
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;
122 pid = zp->pid;
123 free(zp);
124 return(pid);
127 if (pid > 0 && kill(pid, 0) < 0) return(-1);
128 for (;;)
130 #if !_lib_wait2 && !_lib_wait3
131 #if !defined(SIGCLD)
132 oerrno = errno;
133 #endif
134 if (flags & WNOHANG)
136 caught = 0;
137 #if defined(SIGCLD)
138 handler = signal(SIGCLD, catch);
139 if (!caught)
141 signal(SIGCLD, handler);
142 return(0);
144 #else
145 #if defined(SIGALRM)
146 handler = signal(SIGALRM, catch);
147 n = alarm(1);
148 #endif
149 #endif
151 #endif
152 p = wait3(&s, flags, NiL);
153 #if !_lib_wait3
154 #if !_lib_wait2
155 #if defined(SIGCLD)
156 if (flags & WNOHANG) signal(SIGCLD, handler);
157 #else
158 #if defined(SIGALRM)
159 if (flags & WNOHANG)
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)
167 errno = oerrno;
168 p = 0;
169 s = 0;
171 #endif
172 #endif
173 #else
174 if (p == -1 && errno == EINVAL && (flags & ~WNOHANG))
175 p = wait3(&s, flags & WNOHANG, NiL);
176 #endif
177 #endif
178 if (p <= 0)
180 if (p == 0 && status) *status = s;
181 return(p);
183 if (pid <= 0 || p == pid)
185 if (status) *status = s;
186 return(p);
188 if (!(zp = newof(0, struct zombie, 1, 0))) return(-1);
189 zp->pid = p;
190 zp->status = s;
191 zp->next = zombies;
192 zombies = zp;
194 /*NOTREACHED*/
197 #endif
199 #endif