8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbc / libc / sys / common / wait.c
blob36f06ef44f48a4960f7c90856e45ff1ba41dfb6e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * Compatibility lib for BSD's wait3() and wait4().
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/times.h>
45 #include <sys/wait.h>
46 #include <sys/param.h>
47 #include <sys/resource.h>
48 #include "signalmap.h"
51 * Since sysV does not support rusage as in BSD, an approximate approach
52 * is:
53 * ...
54 * call times
55 * call waitid
56 * if ( a child is found )
57 * call times again
58 * rusage ~= diff in the 2 times call
59 * ...
64 * arguments to wait functions from SVR4
67 #define N_WEXITED 0001 /* wait for processes that have exite */
68 #define N_WTRAPPED 0002 /* wait for processes stopped while tracing */
69 #define N_WSTOPPED 0004 /* wait for processes stopped by signals */
70 #define N_WCONTINUED 0010 /* wait for processes continued */
72 #define N_WUNTRACED N_WSTOPPED /* for POSIX */
74 #define N_WNOHANG 0100 /* non blocking form of wait */
75 #define N_WNOWAIT 0200 /* non destructive form of wait */
77 #define WCOREFLG 0200
80 * SIGCLD signal codes from SVr4
83 #define CLD_EXITED 1 /* child has exited */
84 #define CLD_KILLED 2 /* child was killed */
85 #define CLD_DUMPED 3 /* child has coredumped */
86 #define CLD_TRAPPED 4 /* traced child has stopped */
87 #define CLD_STOPPED 5 /* child has stopped on signal */
88 #define CLD_CONTINUED 6 /* stopped child has continued */
89 #define NSIGCLD 6
91 /*
92 * id type from SVR4 procset.h
94 typedef enum idtype {
95 P_PID, /* A process identifier. */
96 P_PPID, /* A parent process identifier. */
97 P_PGID, /* A process group (job control group) */
98 /* identifier. */
99 P_SID, /* A session identifier. */
100 P_CID, /* A scheduling class identifier. */
101 P_UID, /* A user identifier. */
102 P_GID, /* A group identifier. */
103 P_ALL /* All processes. */
104 } idtype_t;
106 static void mapstatus(int *, int);
109 wait(int *status)
111 int ret, nstatus;
113 if ((int)status == -1) {
114 errno = EFAULT;
115 return (-1);
118 ret = _wait(&nstatus);
119 if (status)
120 mapstatus(status, nstatus);
121 return (ret);
125 waitpid(int pid, int *status, int options)
127 int noptions, ret;
128 int nstatus;
130 if ((int)status == -1) {
131 errno = EFAULT;
132 return (-1);
136 * BSD's wait* routines only support WNOHANG & WUNTRACED
138 if (options & ~(WNOHANG|WUNTRACED))
139 return (EINVAL);
140 noptions = (N_WEXITED|N_WTRAPPED);
141 if (options & WNOHANG)
142 noptions |= N_WNOHANG;
143 if (options & WUNTRACED)
144 noptions |= N_WUNTRACED; /* == N_WSTOPPED */
146 ret = _waitpid(pid, &nstatus, noptions);
148 if (status)
149 mapstatus(status, nstatus);
151 return (ret);
155 * It would be -so- nice just to call _wait3 and mapstatus here.
158 wait3(int *status, int options, struct rusage *rp)
160 return (wait4(0, status, options, rp));
163 static int wstat(int, int);
166 * It would be -so- nice just to call _wait4 and mapstatus here.
169 wait4(int pid, int *status, int options, struct rusage *rp)
171 struct tms before_tms;
172 struct tms after_tms;
173 siginfo_t info;
174 int error;
175 int noptions;
176 idtype_t idtype;
178 if ((int)status == -1 || (int)rp == -1) {
179 errno = EFAULT;
180 return(-1);
183 if (rp)
184 memset(rp, 0, sizeof(struct rusage));
185 memset(&info, 0, sizeof (siginfo_t));
186 if (times(&before_tms) < 0)
187 return (-1); /* errno is set by times() */
190 * BSD's wait* routines only support WNOHANG & WUNTRACED
192 if (options & ~(WNOHANG|WUNTRACED))
193 return (EINVAL);
194 noptions = N_WEXITED | N_WTRAPPED;
195 if (options & WNOHANG)
196 noptions |= N_WNOHANG;
197 if (options & WUNTRACED)
198 noptions |= N_WUNTRACED; /* == N_WSTOPPED */
201 * Emulate undocumented 4.x semantics for 1186845
203 if (pid < 0) {
204 pid = -pid;
205 idtype = P_PGID;
206 } else if (pid == 0)
207 idtype = P_ALL;
208 else
209 idtype = P_PID;
211 error = _waitid(idtype, pid, &info, noptions);
212 if (error == 0) {
213 long diffu; /* difference in usertime (ticks) */
214 long diffs; /* difference in systemtime (ticks) */
216 if ((options & WNOHANG) && (info.si_pid == 0))
217 return (0); /* no child found */
219 if (rp) {
220 if (times(&after_tms) < 0)
221 return (-1); /* errno already set by times() */
223 * The system/user time is an approximation only !!!
225 diffu = after_tms.tms_cutime - before_tms.tms_cutime;
226 diffs = after_tms.tms_cstime - before_tms.tms_cstime;
227 rp->ru_utime.tv_sec = diffu / HZ;
228 rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ);
229 rp->ru_stime.tv_sec = diffs / HZ;
230 rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ);
232 if (status)
233 *status = wstat(info.si_code, info.si_status);
234 return (info.si_pid);
235 } else {
236 return (-1); /* error number is set by waitid() */
242 * Convert the status code to old style wait status
244 static int
245 wstat(int code, int status)
247 int stat = (status & 0377);
249 switch (code) {
250 case CLD_EXITED:
251 stat <<= 8;
252 break;
253 case CLD_KILLED:
254 stat = maptooldsig(stat);
255 if (code == CLD_DUMPED)
256 stat |= WCOREFLG;
257 break;
258 case CLD_DUMPED:
259 stat |= WCOREFLG;
260 break;
261 case CLD_TRAPPED:
262 case CLD_STOPPED:
263 stat = maptooldsig(stat);
264 stat <<= 8;
265 stat |= _WSTOPPED;
266 break;
268 return (stat);
271 static void
272 mapstatus(int *new, int old)
274 int stat = old & 0xFF;
276 switch(stat) {
277 case _WSTOPPED:
278 *new = maptooldsig(stat >> 8);
279 *new = (stat << 8) | _WSTOPPED;
280 break;
281 case 0:
282 *new = old;
283 break;
284 default:
285 *new = maptooldsig(old & 0x7F);
286 if (old & 0x80)
287 *new |= 0x80; /* set WCOREFLG */