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
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]
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().
42 #include <sys/types.h>
44 #include <sys/times.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
56 * if ( a child is found )
58 * rusage ~= diff in the 2 times call
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 */
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 */
92 * id type from SVR4 procset.h
95 P_PID
, /* A process identifier. */
96 P_PPID
, /* A parent process identifier. */
97 P_PGID
, /* A process group (job control group) */
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. */
106 static void mapstatus(int *, int);
113 if ((int)status
== -1) {
118 ret
= _wait(&nstatus
);
120 mapstatus(status
, nstatus
);
125 waitpid(int pid
, int *status
, int options
)
130 if ((int)status
== -1) {
136 * BSD's wait* routines only support WNOHANG & WUNTRACED
138 if (options
& ~(WNOHANG
|WUNTRACED
))
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
);
149 mapstatus(status
, nstatus
);
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
;
178 if ((int)status
== -1 || (int)rp
== -1) {
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
))
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
211 error
= _waitid(idtype
, pid
, &info
, noptions
);
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 */
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
);
233 *status
= wstat(info
.si_code
, info
.si_status
);
234 return (info
.si_pid
);
236 return (-1); /* error number is set by waitid() */
242 * Convert the status code to old style wait status
245 wstat(int code
, int status
)
247 int stat
= (status
& 0377);
254 stat
= maptooldsig(stat
);
255 if (code
== CLD_DUMPED
)
263 stat
= maptooldsig(stat
);
272 mapstatus(int *new, int old
)
274 int stat
= old
& 0xFF;
278 *new = maptooldsig(stat
>> 8);
279 *new = (stat
<< 8) | _WSTOPPED
;
285 *new = maptooldsig(old
& 0x7F);
287 *new |= 0x80; /* set WCOREFLG */