10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate * under license from the Regents of the University of California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * Compatibility lib for BSD's wait3() and wait4().
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/time.h>
440Sstevel@tonic-gate #include <sys/times.h>
450Sstevel@tonic-gate #include <sys/wait.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/resource.h>
480Sstevel@tonic-gate #include "signalmap.h"
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * Since sysV does not support rusage as in BSD, an approximate approach
520Sstevel@tonic-gate * is:
530Sstevel@tonic-gate * ...
540Sstevel@tonic-gate * call times
550Sstevel@tonic-gate * call waitid
560Sstevel@tonic-gate * if ( a child is found )
570Sstevel@tonic-gate * call times again
580Sstevel@tonic-gate * rusage ~= diff in the 2 times call
590Sstevel@tonic-gate * ...
600Sstevel@tonic-gate *
610Sstevel@tonic-gate */
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * arguments to wait functions from SVR4
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
670Sstevel@tonic-gate #define N_WEXITED 0001 /* wait for processes that have exite */
680Sstevel@tonic-gate #define N_WTRAPPED 0002 /* wait for processes stopped while tracing */
690Sstevel@tonic-gate #define N_WSTOPPED 0004 /* wait for processes stopped by signals */
700Sstevel@tonic-gate #define N_WCONTINUED 0010 /* wait for processes continued */
710Sstevel@tonic-gate
720Sstevel@tonic-gate #define N_WUNTRACED N_WSTOPPED /* for POSIX */
730Sstevel@tonic-gate
740Sstevel@tonic-gate #define N_WNOHANG 0100 /* non blocking form of wait */
750Sstevel@tonic-gate #define N_WNOWAIT 0200 /* non destructive form of wait */
760Sstevel@tonic-gate
770Sstevel@tonic-gate #define WCOREFLG 0200
780Sstevel@tonic-gate
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate * SIGCLD signal codes from SVr4
810Sstevel@tonic-gate */
820Sstevel@tonic-gate
830Sstevel@tonic-gate #define CLD_EXITED 1 /* child has exited */
840Sstevel@tonic-gate #define CLD_KILLED 2 /* child was killed */
850Sstevel@tonic-gate #define CLD_DUMPED 3 /* child has coredumped */
860Sstevel@tonic-gate #define CLD_TRAPPED 4 /* traced child has stopped */
870Sstevel@tonic-gate #define CLD_STOPPED 5 /* child has stopped on signal */
880Sstevel@tonic-gate #define CLD_CONTINUED 6 /* stopped child has continued */
890Sstevel@tonic-gate #define NSIGCLD 6
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * id type from SVR4 procset.h
930Sstevel@tonic-gate */
940Sstevel@tonic-gate typedef enum idtype {
950Sstevel@tonic-gate P_PID, /* A process identifier. */
960Sstevel@tonic-gate P_PPID, /* A parent process identifier. */
970Sstevel@tonic-gate P_PGID, /* A process group (job control group) */
980Sstevel@tonic-gate /* identifier. */
990Sstevel@tonic-gate P_SID, /* A session identifier. */
1000Sstevel@tonic-gate P_CID, /* A scheduling class identifier. */
1010Sstevel@tonic-gate P_UID, /* A user identifier. */
1020Sstevel@tonic-gate P_GID, /* A group identifier. */
1030Sstevel@tonic-gate P_ALL /* All processes. */
1040Sstevel@tonic-gate } idtype_t;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate static void mapstatus(int *, int);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate int
wait(int * status)1090Sstevel@tonic-gate wait(int *status)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate int ret, nstatus;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if ((int)status == -1) {
1140Sstevel@tonic-gate errno = EFAULT;
1150Sstevel@tonic-gate return (-1);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate ret = _wait(&nstatus);
1190Sstevel@tonic-gate if (status)
1200Sstevel@tonic-gate mapstatus(status, nstatus);
1210Sstevel@tonic-gate return (ret);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate int
waitpid(int pid,int * status,int options)1250Sstevel@tonic-gate waitpid(int pid, int *status, int options)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate int noptions, ret;
1280Sstevel@tonic-gate int nstatus;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if ((int)status == -1) {
1310Sstevel@tonic-gate errno = EFAULT;
1320Sstevel@tonic-gate return (-1);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * BSD's wait* routines only support WNOHANG & WUNTRACED
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED))
1390Sstevel@tonic-gate return (EINVAL);
1400Sstevel@tonic-gate noptions = (N_WEXITED|N_WTRAPPED);
1410Sstevel@tonic-gate if (options & WNOHANG)
1420Sstevel@tonic-gate noptions |= N_WNOHANG;
1430Sstevel@tonic-gate if (options & WUNTRACED)
1440Sstevel@tonic-gate noptions |= N_WUNTRACED; /* == N_WSTOPPED */
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate ret = _waitpid(pid, &nstatus, noptions);
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if (status)
1490Sstevel@tonic-gate mapstatus(status, nstatus);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate return (ret);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate * It would be -so- nice just to call _wait3 and mapstatus here.
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate int
wait3(int * status,int options,struct rusage * rp)1580Sstevel@tonic-gate wait3(int *status, int options, struct rusage *rp)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate return (wait4(0, status, options, rp));
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate static int wstat(int, int);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * It would be -so- nice just to call _wait4 and mapstatus here.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate int
wait4(int pid,int * status,int options,struct rusage * rp)1690Sstevel@tonic-gate wait4(int pid, int *status, int options, struct rusage *rp)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate struct tms before_tms;
1720Sstevel@tonic-gate struct tms after_tms;
1730Sstevel@tonic-gate siginfo_t info;
1740Sstevel@tonic-gate int error;
1750Sstevel@tonic-gate int noptions;
1760Sstevel@tonic-gate idtype_t idtype;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if ((int)status == -1 || (int)rp == -1) {
1790Sstevel@tonic-gate errno = EFAULT;
1800Sstevel@tonic-gate return(-1);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (rp)
1840Sstevel@tonic-gate memset(rp, 0, sizeof(struct rusage));
1850Sstevel@tonic-gate memset(&info, 0, sizeof (siginfo_t));
1860Sstevel@tonic-gate if (times(&before_tms) < 0)
1870Sstevel@tonic-gate return (-1); /* errno is set by times() */
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * BSD's wait* routines only support WNOHANG & WUNTRACED
1910Sstevel@tonic-gate */
1920Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED))
1930Sstevel@tonic-gate return (EINVAL);
1940Sstevel@tonic-gate noptions = N_WEXITED | N_WTRAPPED;
1950Sstevel@tonic-gate if (options & WNOHANG)
1960Sstevel@tonic-gate noptions |= N_WNOHANG;
1970Sstevel@tonic-gate if (options & WUNTRACED)
1980Sstevel@tonic-gate noptions |= N_WUNTRACED; /* == N_WSTOPPED */
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * Emulate undocumented 4.x semantics for 1186845
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate if (pid < 0) {
2040Sstevel@tonic-gate pid = -pid;
2050Sstevel@tonic-gate idtype = P_PGID;
2060Sstevel@tonic-gate } else if (pid == 0)
2070Sstevel@tonic-gate idtype = P_ALL;
2080Sstevel@tonic-gate else
2090Sstevel@tonic-gate idtype = P_PID;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate error = _waitid(idtype, pid, &info, noptions);
2120Sstevel@tonic-gate if (error == 0) {
2130Sstevel@tonic-gate long diffu; /* difference in usertime (ticks) */
2140Sstevel@tonic-gate long diffs; /* difference in systemtime (ticks) */
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if ((options & WNOHANG) && (info.si_pid == 0))
2170Sstevel@tonic-gate return (0); /* no child found */
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (rp) {
2200Sstevel@tonic-gate if (times(&after_tms) < 0)
2210Sstevel@tonic-gate return (-1); /* errno already set by times() */
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * The system/user time is an approximation only !!!
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate diffu = after_tms.tms_cutime - before_tms.tms_cutime;
2260Sstevel@tonic-gate diffs = after_tms.tms_cstime - before_tms.tms_cstime;
2270Sstevel@tonic-gate rp->ru_utime.tv_sec = diffu / HZ;
2280Sstevel@tonic-gate rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ);
2290Sstevel@tonic-gate rp->ru_stime.tv_sec = diffs / HZ;
2300Sstevel@tonic-gate rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate if (status)
2330Sstevel@tonic-gate *status = wstat(info.si_code, info.si_status);
2340Sstevel@tonic-gate return (info.si_pid);
2350Sstevel@tonic-gate } else {
2360Sstevel@tonic-gate return (-1); /* error number is set by waitid() */
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate * Convert the status code to old style wait status
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate static int
wstat(int code,int status)2450Sstevel@tonic-gate wstat(int code, int status)
2460Sstevel@tonic-gate {
247*722Smuffin int stat = (status & 0377);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate switch (code) {
2500Sstevel@tonic-gate case CLD_EXITED:
2510Sstevel@tonic-gate stat <<= 8;
2520Sstevel@tonic-gate break;
2530Sstevel@tonic-gate case CLD_KILLED:
2540Sstevel@tonic-gate stat = maptooldsig(stat);
2550Sstevel@tonic-gate if (code == CLD_DUMPED)
2560Sstevel@tonic-gate stat |= WCOREFLG;
2570Sstevel@tonic-gate break;
2580Sstevel@tonic-gate case CLD_DUMPED:
2590Sstevel@tonic-gate stat |= WCOREFLG;
2600Sstevel@tonic-gate break;
2610Sstevel@tonic-gate case CLD_TRAPPED:
2620Sstevel@tonic-gate case CLD_STOPPED:
2630Sstevel@tonic-gate stat = maptooldsig(stat);
2640Sstevel@tonic-gate stat <<= 8;
2650Sstevel@tonic-gate stat |= _WSTOPPED;
2660Sstevel@tonic-gate break;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate return (stat);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate static void
mapstatus(int * new,int old)2720Sstevel@tonic-gate mapstatus(int *new, int old)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate int stat = old & 0xFF;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate switch(stat) {
2770Sstevel@tonic-gate case _WSTOPPED:
2780Sstevel@tonic-gate *new = maptooldsig(stat >> 8);
2790Sstevel@tonic-gate *new = (stat << 8) | _WSTOPPED;
2800Sstevel@tonic-gate break;
2810Sstevel@tonic-gate case 0:
2820Sstevel@tonic-gate *new = old;
2830Sstevel@tonic-gate break;
2840Sstevel@tonic-gate default:
2850Sstevel@tonic-gate *new = maptooldsig(old & 0x7F);
2860Sstevel@tonic-gate if (old & 0x80)
2870Sstevel@tonic-gate *new |= 0x80; /* set WCOREFLG */
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate }
290