xref: /illumos-gate/usr/src/ucblib/libucb/port/sys/wait4.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Compatibility lib for SunOS's wait4().
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/times.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
50*7c478bd9Sstevel@tonic-gate #include <unistd.h>
51*7c478bd9Sstevel@tonic-gate #include <string.h>
52*7c478bd9Sstevel@tonic-gate #include <errno.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * Since sysV does not support rusage as in BSD, an approximate approach
56*7c478bd9Sstevel@tonic-gate  * is:
57*7c478bd9Sstevel@tonic-gate  *      ...
58*7c478bd9Sstevel@tonic-gate  *      call times
59*7c478bd9Sstevel@tonic-gate  *      call waitid
60*7c478bd9Sstevel@tonic-gate  *      if ( a child is found )
61*7c478bd9Sstevel@tonic-gate  *              call times again
62*7c478bd9Sstevel@tonic-gate  *              rusage ~= diff in the 2 times call
63*7c478bd9Sstevel@tonic-gate  *      ...
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * forward declaration
68*7c478bd9Sstevel@tonic-gate  */
69*7c478bd9Sstevel@tonic-gate static int wstat(int, int);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate pid_t
72*7c478bd9Sstevel@tonic-gate wait4(pid_t pid, int *status, int options, struct rusage *rp)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	struct  tms	before_tms;
75*7c478bd9Sstevel@tonic-gate 	struct  tms	after_tms;
76*7c478bd9Sstevel@tonic-gate 	siginfo_t	info;
77*7c478bd9Sstevel@tonic-gate 	int		error;
78*7c478bd9Sstevel@tonic-gate 	int 		noptions;
79*7c478bd9Sstevel@tonic-gate 	idtype_t	idtype;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if ((long)status == -1L || (long)rp == -1L) {
82*7c478bd9Sstevel@tonic-gate 		errno = EFAULT;
83*7c478bd9Sstevel@tonic-gate 		return (-1);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (rp)
87*7c478bd9Sstevel@tonic-gate 		(void) memset(rp, 0, sizeof (struct rusage));
88*7c478bd9Sstevel@tonic-gate 	(void) memset(&info, 0, sizeof (siginfo_t));
89*7c478bd9Sstevel@tonic-gate 	if (times(&before_tms) < 0)
90*7c478bd9Sstevel@tonic-gate 		return (-1);	/* errno is set by times() */
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * SunOS's wait4() only supports WNOHANG & WUNTRACED
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 	if (options & ~(WNOHANG|WUNTRACED))
96*7c478bd9Sstevel@tonic-gate 		return (EINVAL);	/* used to be done by the kernel */
97*7c478bd9Sstevel@tonic-gate 	noptions = options | WEXITED | WTRAPPED;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/*
100*7c478bd9Sstevel@tonic-gate 	 * Emulate undocumented 4.x semantics for 1186845
101*7c478bd9Sstevel@tonic-gate 	 */
102*7c478bd9Sstevel@tonic-gate 	if (pid < 0) {
103*7c478bd9Sstevel@tonic-gate 		pid = -pid;
104*7c478bd9Sstevel@tonic-gate 		idtype = P_PGID;
105*7c478bd9Sstevel@tonic-gate 	} else if (pid == 0)
106*7c478bd9Sstevel@tonic-gate 		idtype = P_ALL;
107*7c478bd9Sstevel@tonic-gate 	else
108*7c478bd9Sstevel@tonic-gate 		idtype = P_PID;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	error = waitid(idtype, pid, &info, noptions);
111*7c478bd9Sstevel@tonic-gate 	if (error == 0) {
112*7c478bd9Sstevel@tonic-gate 		clock_t	diffu;  /* difference in usertime (ticks) */
113*7c478bd9Sstevel@tonic-gate 		clock_t	diffs;  /* difference in systemtime (ticks) */
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		if ((options & WNOHANG) && (info.si_pid == 0))
116*7c478bd9Sstevel@tonic-gate 			return (0);	/* no child found */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 		if (rp) {
119*7c478bd9Sstevel@tonic-gate 			if (times(&after_tms) < 0)
120*7c478bd9Sstevel@tonic-gate 				return (-1);    /* errno set by times() */
121*7c478bd9Sstevel@tonic-gate 			/*
122*7c478bd9Sstevel@tonic-gate 			 * The system/user time is an approximation only !!!
123*7c478bd9Sstevel@tonic-gate 			 */
124*7c478bd9Sstevel@tonic-gate 			diffu = after_tms.tms_cutime - before_tms.tms_cutime;
125*7c478bd9Sstevel@tonic-gate 			diffs = after_tms.tms_cstime - before_tms.tms_cstime;
126*7c478bd9Sstevel@tonic-gate 			rp->ru_utime.tv_sec = diffu / HZ;
127*7c478bd9Sstevel@tonic-gate 			rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ);
128*7c478bd9Sstevel@tonic-gate 			rp->ru_stime.tv_sec = diffs/ HZ;
129*7c478bd9Sstevel@tonic-gate 			rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ);
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		if (status)
132*7c478bd9Sstevel@tonic-gate 			*status = wstat(info.si_code, info.si_status);
133*7c478bd9Sstevel@tonic-gate 		return (info.si_pid);
134*7c478bd9Sstevel@tonic-gate 	} else {
135*7c478bd9Sstevel@tonic-gate 		return (-1);		/* error number is set by waitid() */
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * Convert the status code to old style wait status
141*7c478bd9Sstevel@tonic-gate  */
142*7c478bd9Sstevel@tonic-gate static int
143*7c478bd9Sstevel@tonic-gate wstat(int code, int status)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate 	int stat = (status & 0377);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	switch (code) {
148*7c478bd9Sstevel@tonic-gate 	case CLD_EXITED:
149*7c478bd9Sstevel@tonic-gate 		stat <<= 8;
150*7c478bd9Sstevel@tonic-gate 		break;
151*7c478bd9Sstevel@tonic-gate 	case CLD_DUMPED:
152*7c478bd9Sstevel@tonic-gate 		stat |= WCOREFLG;
153*7c478bd9Sstevel@tonic-gate 		break;
154*7c478bd9Sstevel@tonic-gate 	case CLD_KILLED:
155*7c478bd9Sstevel@tonic-gate 		break;
156*7c478bd9Sstevel@tonic-gate 	case CLD_TRAPPED:
157*7c478bd9Sstevel@tonic-gate 	case CLD_STOPPED:
158*7c478bd9Sstevel@tonic-gate 		stat <<= 8;
159*7c478bd9Sstevel@tonic-gate 		stat |= WSTOPFLG;
160*7c478bd9Sstevel@tonic-gate 		break;
161*7c478bd9Sstevel@tonic-gate 	case CLD_CONTINUED:
162*7c478bd9Sstevel@tonic-gate 		stat = WCONTFLG;
163*7c478bd9Sstevel@tonic-gate 		break;
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 	return (stat);
166*7c478bd9Sstevel@tonic-gate }
167