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