xref: /titanic_41/usr/src/lib/libbc/libc/sys/common/wait.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  *      Compatibility lib for BSD's wait3() and wait4().
39  */
40 
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"
49 
50 /*
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  *      ...
60  *
61  */
62 
63 /*
64  * arguments to wait functions from SVR4
65  */
66 
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 */
71 
72 #define N_WUNTRACED       N_WSTOPPED /* for POSIX */
73 
74 #define N_WNOHANG         0100    /* non blocking form of wait    */
75 #define N_WNOWAIT         0200    /* non destructive form of wait */
76 
77 #define WCOREFLG	  0200
78 
79 /*
80  * SIGCLD signal codes from SVr4
81  */
82 
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
90 
91 /*
92  * id type from SVR4 procset.h
93  */
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;
105 
106 static void mapstatus(int *, int);
107 
108 int
109 wait(int *status)
110 {
111 	int ret, nstatus;
112 
113 	if ((int)status == -1) {
114 		errno = EFAULT;
115 		return (-1);
116 	}
117 
118 	ret = _wait(&nstatus);
119 	if (status)
120 		mapstatus(status, nstatus);
121 	return (ret);
122 }
123 
124 int
125 waitpid(int pid, int *status, int options)
126 {
127 	int noptions, ret;
128 	int nstatus;
129 
130 	if ((int)status == -1) {
131 		errno = EFAULT;
132 		return (-1);
133 	}
134 
135 	/*
136 	 * BSD's wait* routines only support WNOHANG & WUNTRACED
137 	 */
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 */
145 
146 	ret = _waitpid(pid, &nstatus, noptions);
147 
148 	if (status)
149 		mapstatus(status, nstatus);
150 
151 	return (ret);
152 }
153 
154 /*
155  * It would be -so- nice just to call _wait3 and mapstatus here.
156  */
157 int
158 wait3(int *status, int options, struct rusage *rp)
159 {
160 	return (wait4(0, status, options, rp));
161 }
162 
163 static int wstat(int, int);
164 
165 /*
166  * It would be -so- nice just to call _wait4 and mapstatus here.
167  */
168 int
169 wait4(int pid, int *status, int options, struct rusage *rp)
170 {
171         struct  tms     before_tms;
172         struct  tms     after_tms;
173         siginfo_t       info;
174         int             error;
175         int             noptions;
176 	idtype_t	idtype;
177 
178         if ((int)status == -1 || (int)rp == -1) {
179                 errno = EFAULT;
180                 return(-1);
181         }
182 
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() */
188 
189 	/*
190 	 * BSD's wait* routines only support WNOHANG & WUNTRACED
191 	 */
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 */
199 
200 	/*
201 	 * Emulate undocumented 4.x semantics for 1186845
202 	 */
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;
210 
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) */
215 
216                 if ((options & WNOHANG) && (info.si_pid == 0))
217                         return (0);     /* no child found */
218 
219 		if (rp) {
220 			if (times(&after_tms) < 0)
221 				return (-1);    /* errno already set by times() */
222 			/*
223 			 * The system/user time is an approximation only !!!
224 			 */
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);
231 		}
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() */
237         }
238 }
239 
240 
241 /*
242  * Convert the status code to old style wait status
243  */
244 static int
245 wstat(int code, int status)
246 {
247 	int stat = (status & 0377);
248 
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;
267         }
268         return (stat);
269 }
270 
271 static void
272 mapstatus(int *new, int old)
273 {
274 	int stat = old & 0xFF;
275 
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 */
288 	}
289 }
290