1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1995-1997, by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /* All Rights Reserved */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate /* Portions Copyright(c) 1988, Sun Microsystems Inc. */
32*0Sstevel@tonic-gate /* All Rights Reserved */
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate /*LINTLIBRARY*/
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * Compatibility lib for SunOS's wait4().
40*0Sstevel@tonic-gate */
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/time.h>
44*0Sstevel@tonic-gate #include <sys/times.h>
45*0Sstevel@tonic-gate #include <sys/wait.h>
46*0Sstevel@tonic-gate #include <sys/siginfo.h>
47*0Sstevel@tonic-gate #include <sys/procset.h>
48*0Sstevel@tonic-gate #include <sys/param.h>
49*0Sstevel@tonic-gate #include <sys/resource.h>
50*0Sstevel@tonic-gate #include <unistd.h>
51*0Sstevel@tonic-gate #include <string.h>
52*0Sstevel@tonic-gate #include <errno.h>
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate * Since sysV does not support rusage as in BSD, an approximate approach
56*0Sstevel@tonic-gate * is:
57*0Sstevel@tonic-gate * ...
58*0Sstevel@tonic-gate * call times
59*0Sstevel@tonic-gate * call waitid
60*0Sstevel@tonic-gate * if ( a child is found )
61*0Sstevel@tonic-gate * call times again
62*0Sstevel@tonic-gate * rusage ~= diff in the 2 times call
63*0Sstevel@tonic-gate * ...
64*0Sstevel@tonic-gate */
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate * forward declaration
68*0Sstevel@tonic-gate */
69*0Sstevel@tonic-gate static int wstat(int, int);
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate pid_t
wait4(pid_t pid,int * status,int options,struct rusage * rp)72*0Sstevel@tonic-gate wait4(pid_t pid, int *status, int options, struct rusage *rp)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate struct tms before_tms;
75*0Sstevel@tonic-gate struct tms after_tms;
76*0Sstevel@tonic-gate siginfo_t info;
77*0Sstevel@tonic-gate int error;
78*0Sstevel@tonic-gate int noptions;
79*0Sstevel@tonic-gate idtype_t idtype;
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate if ((long)status == -1L || (long)rp == -1L) {
82*0Sstevel@tonic-gate errno = EFAULT;
83*0Sstevel@tonic-gate return (-1);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate if (rp)
87*0Sstevel@tonic-gate (void) memset(rp, 0, sizeof (struct rusage));
88*0Sstevel@tonic-gate (void) memset(&info, 0, sizeof (siginfo_t));
89*0Sstevel@tonic-gate if (times(&before_tms) < 0)
90*0Sstevel@tonic-gate return (-1); /* errno is set by times() */
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate * SunOS's wait4() only supports WNOHANG & WUNTRACED
94*0Sstevel@tonic-gate */
95*0Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED))
96*0Sstevel@tonic-gate return (EINVAL); /* used to be done by the kernel */
97*0Sstevel@tonic-gate noptions = options | WEXITED | WTRAPPED;
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate * Emulate undocumented 4.x semantics for 1186845
101*0Sstevel@tonic-gate */
102*0Sstevel@tonic-gate if (pid < 0) {
103*0Sstevel@tonic-gate pid = -pid;
104*0Sstevel@tonic-gate idtype = P_PGID;
105*0Sstevel@tonic-gate } else if (pid == 0)
106*0Sstevel@tonic-gate idtype = P_ALL;
107*0Sstevel@tonic-gate else
108*0Sstevel@tonic-gate idtype = P_PID;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate error = waitid(idtype, pid, &info, noptions);
111*0Sstevel@tonic-gate if (error == 0) {
112*0Sstevel@tonic-gate clock_t diffu; /* difference in usertime (ticks) */
113*0Sstevel@tonic-gate clock_t diffs; /* difference in systemtime (ticks) */
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate if ((options & WNOHANG) && (info.si_pid == 0))
116*0Sstevel@tonic-gate return (0); /* no child found */
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate if (rp) {
119*0Sstevel@tonic-gate if (times(&after_tms) < 0)
120*0Sstevel@tonic-gate return (-1); /* errno set by times() */
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate * The system/user time is an approximation only !!!
123*0Sstevel@tonic-gate */
124*0Sstevel@tonic-gate diffu = after_tms.tms_cutime - before_tms.tms_cutime;
125*0Sstevel@tonic-gate diffs = after_tms.tms_cstime - before_tms.tms_cstime;
126*0Sstevel@tonic-gate rp->ru_utime.tv_sec = diffu / HZ;
127*0Sstevel@tonic-gate rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ);
128*0Sstevel@tonic-gate rp->ru_stime.tv_sec = diffs/ HZ;
129*0Sstevel@tonic-gate rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate if (status)
132*0Sstevel@tonic-gate *status = wstat(info.si_code, info.si_status);
133*0Sstevel@tonic-gate return (info.si_pid);
134*0Sstevel@tonic-gate } else {
135*0Sstevel@tonic-gate return (-1); /* error number is set by waitid() */
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate * Convert the status code to old style wait status
141*0Sstevel@tonic-gate */
142*0Sstevel@tonic-gate static int
wstat(int code,int status)143*0Sstevel@tonic-gate wstat(int code, int status)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate int stat = (status & 0377);
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate switch (code) {
148*0Sstevel@tonic-gate case CLD_EXITED:
149*0Sstevel@tonic-gate stat <<= 8;
150*0Sstevel@tonic-gate break;
151*0Sstevel@tonic-gate case CLD_DUMPED:
152*0Sstevel@tonic-gate stat |= WCOREFLG;
153*0Sstevel@tonic-gate break;
154*0Sstevel@tonic-gate case CLD_KILLED:
155*0Sstevel@tonic-gate break;
156*0Sstevel@tonic-gate case CLD_TRAPPED:
157*0Sstevel@tonic-gate case CLD_STOPPED:
158*0Sstevel@tonic-gate stat <<= 8;
159*0Sstevel@tonic-gate stat |= WSTOPFLG;
160*0Sstevel@tonic-gate break;
161*0Sstevel@tonic-gate case CLD_CONTINUED:
162*0Sstevel@tonic-gate stat = WCONTFLG;
163*0Sstevel@tonic-gate break;
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate return (stat);
166*0Sstevel@tonic-gate }
167