xref: /titanic_44/usr/src/lib/libbc/libc/sys/common/wait.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 extern int errno;
64 
65 /*
66  * arguments to wait functions from SVR4
67  */
68 
69 #define N_WEXITED         0001    /* wait for processes that have exite   */
70 #define N_WTRAPPED        0002    /* wait for processes stopped while tracing */
71 #define N_WSTOPPED        0004    /* wait for processes stopped by signals */
72 #define N_WCONTINUED      0010    /* wait for processes continued */
73 
74 #define N_WUNTRACED       N_WSTOPPED /* for POSIX */
75 
76 #define N_WNOHANG         0100    /* non blocking form of wait    */
77 #define N_WNOWAIT         0200    /* non destructive form of wait */
78 
79 #define WCOREFLG	  0200
80 
81 /*
82  * SIGCLD signal codes from SVr4
83  */
84 
85 #define CLD_EXITED      1       /* child has exited */
86 #define CLD_KILLED      2       /* child was killed */
87 #define CLD_DUMPED      3       /* child has coredumped */
88 #define CLD_TRAPPED     4       /* traced child has stopped */
89 #define CLD_STOPPED     5       /* child has stopped on signal */
90 #define CLD_CONTINUED   6       /* stopped child has continued */
91 #define NSIGCLD         6
92 
93 /*
94  * id type from SVR4 procset.h
95  */
96 typedef enum idtype {
97         P_PID,          /* A process identifier.                */
98         P_PPID,         /* A parent process identifier.         */
99         P_PGID,         /* A process group (job control group)  */
100                         /* identifier.                          */
101         P_SID,          /* A session identifier.                */
102         P_CID,          /* A scheduling class identifier.       */
103         P_UID,          /* A user identifier.                   */
104         P_GID,          /* A group identifier.                  */
105         P_ALL           /* All processes.                       */
106 } idtype_t;
107 
108 static void mapstatus(int *, int);
109 
110 int
111 wait(int *status)
112 {
113 	int ret, nstatus;
114 
115 	if ((int)status == -1) {
116 		errno = EFAULT;
117 		return (-1);
118 	}
119 
120 	ret = _wait(&nstatus);
121 	if (status)
122 		mapstatus(status, nstatus);
123 	return (ret);
124 }
125 
126 int
127 waitpid(int pid, int *status, int options)
128 {
129 	int noptions, ret;
130 	int nstatus;
131 
132 	if ((int)status == -1) {
133 		errno = EFAULT;
134 		return (-1);
135 	}
136 
137 	/*
138 	 * BSD's wait* routines only support WNOHANG & WUNTRACED
139 	 */
140 	if (options & ~(WNOHANG|WUNTRACED))
141 		return (EINVAL);
142 	noptions = (N_WEXITED|N_WTRAPPED);
143 	if (options & WNOHANG)
144 		noptions |= N_WNOHANG;
145 	if (options & WUNTRACED)
146 		noptions |= N_WUNTRACED;	/* == N_WSTOPPED */
147 
148 	ret = _waitpid(pid, &nstatus, noptions);
149 
150 	if (status)
151 		mapstatus(status, nstatus);
152 
153 	return (ret);
154 }
155 
156 /*
157  * It would be -so- nice just to call _wait3 and mapstatus here.
158  */
159 int
160 wait3(int *status, int options, struct rusage *rp)
161 {
162 	return (wait4(0, status, options, rp));
163 }
164 
165 static int wstat(int, int);
166 
167 /*
168  * It would be -so- nice just to call _wait4 and mapstatus here.
169  */
170 int
171 wait4(int pid, int *status, int options, struct rusage *rp)
172 {
173         struct  tms     before_tms;
174         struct  tms     after_tms;
175         siginfo_t       info;
176         int             error;
177         int             noptions;
178 	idtype_t	idtype;
179 
180         if ((int)status == -1 || (int)rp == -1) {
181                 errno = EFAULT;
182                 return(-1);
183         }
184 
185         if (rp)
186                 memset(rp, 0, sizeof(struct rusage));
187 	memset(&info, 0, sizeof (siginfo_t));
188         if (times(&before_tms) < 0)
189                 return (-1);            /* errno is set by times() */
190 
191 	/*
192 	 * BSD's wait* routines only support WNOHANG & WUNTRACED
193 	 */
194 	if (options & ~(WNOHANG|WUNTRACED))
195 		return (EINVAL);
196 	noptions = N_WEXITED | N_WTRAPPED;
197 	if (options & WNOHANG)
198 		noptions |= N_WNOHANG;
199 	if (options & WUNTRACED)
200 		noptions |= N_WUNTRACED;	/* == N_WSTOPPED */
201 
202 	/*
203 	 * Emulate undocumented 4.x semantics for 1186845
204 	 */
205 	if (pid < 0) {
206 		pid = -pid;
207 		idtype = P_PGID;
208 	} else if (pid == 0)
209 		idtype = P_ALL;
210 	else
211 		idtype = P_PID;
212 
213         error = _waitid(idtype, pid, &info, noptions);
214         if (error == 0) {
215                 long diffu;  /* difference in usertime (ticks) */
216                 long diffs;  /* difference in systemtime (ticks) */
217 
218                 if ((options & WNOHANG) && (info.si_pid == 0))
219                         return (0);     /* no child found */
220 
221 		if (rp) {
222 			if (times(&after_tms) < 0)
223 				return (-1);    /* errno already set by times() */
224 			/*
225 			 * The system/user time is an approximation only !!!
226 			 */
227 			diffu = after_tms.tms_cutime - before_tms.tms_cutime;
228 			diffs = after_tms.tms_cstime - before_tms.tms_cstime;
229                 	rp->ru_utime.tv_sec = diffu / HZ;
230                 	rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ);
231                 	rp->ru_stime.tv_sec = diffs / HZ;
232                 	rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ);
233 		}
234                 if (status)
235                         *status = wstat(info.si_code, info.si_status);
236                 return (info.si_pid);
237          } else {
238                 return (-1);            /* error number is set by waitid() */
239         }
240 }
241 
242 
243 /*
244  * Convert the status code to old style wait status
245  */
246 static int
247 wstat(int code, int status)
248 {
249         register stat = (status & 0377);
250 
251         switch (code) {
252 	case CLD_EXITED:
253 		stat <<= 8;
254 		break;
255 	case CLD_KILLED:
256 		stat = maptooldsig(stat);
257 		if (code == CLD_DUMPED)
258 			stat |= WCOREFLG;
259 		break;
260 	case CLD_DUMPED:
261 		stat |= WCOREFLG;
262 		break;
263 	case CLD_TRAPPED:
264 	case CLD_STOPPED:
265 		stat = maptooldsig(stat);
266 		stat <<= 8;
267 		stat |= _WSTOPPED;
268 		break;
269         }
270         return (stat);
271 }
272 
273 static void
274 mapstatus(int *new, int old)
275 {
276 	int stat = old & 0xFF;
277 
278 	switch(stat) {
279 	case _WSTOPPED:
280 		*new = maptooldsig(stat >> 8);
281 		*new = (stat << 8) | _WSTOPPED;
282 		break;
283 	case 0:
284 		*new = old;
285 		break;
286 	default:
287 		*new = maptooldsig(old & 0x7F);
288 		if (old & 0x80)
289 			*new |= 0x80;		/* set WCOREFLG */
290 	}
291 }
292