1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1995 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 32*0Sstevel@tonic-gate * under license from the Regents of the University of California. 33*0Sstevel@tonic-gate */ 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * Compatibility lib for BSD's wait3() and wait4(). 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include <errno.h> 42*0Sstevel@tonic-gate #include <sys/types.h> 43*0Sstevel@tonic-gate #include <sys/time.h> 44*0Sstevel@tonic-gate #include <sys/times.h> 45*0Sstevel@tonic-gate #include <sys/wait.h> 46*0Sstevel@tonic-gate #include <sys/param.h> 47*0Sstevel@tonic-gate #include <sys/resource.h> 48*0Sstevel@tonic-gate #include "signalmap.h" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate /* 51*0Sstevel@tonic-gate * Since sysV does not support rusage as in BSD, an approximate approach 52*0Sstevel@tonic-gate * is: 53*0Sstevel@tonic-gate * ... 54*0Sstevel@tonic-gate * call times 55*0Sstevel@tonic-gate * call waitid 56*0Sstevel@tonic-gate * if ( a child is found ) 57*0Sstevel@tonic-gate * call times again 58*0Sstevel@tonic-gate * rusage ~= diff in the 2 times call 59*0Sstevel@tonic-gate * ... 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate */ 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate extern int errno; 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate /* 66*0Sstevel@tonic-gate * arguments to wait functions from SVR4 67*0Sstevel@tonic-gate */ 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate #define N_WEXITED 0001 /* wait for processes that have exite */ 70*0Sstevel@tonic-gate #define N_WTRAPPED 0002 /* wait for processes stopped while tracing */ 71*0Sstevel@tonic-gate #define N_WSTOPPED 0004 /* wait for processes stopped by signals */ 72*0Sstevel@tonic-gate #define N_WCONTINUED 0010 /* wait for processes continued */ 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate #define N_WUNTRACED N_WSTOPPED /* for POSIX */ 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate #define N_WNOHANG 0100 /* non blocking form of wait */ 77*0Sstevel@tonic-gate #define N_WNOWAIT 0200 /* non destructive form of wait */ 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate #define WCOREFLG 0200 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate /* 82*0Sstevel@tonic-gate * SIGCLD signal codes from SVr4 83*0Sstevel@tonic-gate */ 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate #define CLD_EXITED 1 /* child has exited */ 86*0Sstevel@tonic-gate #define CLD_KILLED 2 /* child was killed */ 87*0Sstevel@tonic-gate #define CLD_DUMPED 3 /* child has coredumped */ 88*0Sstevel@tonic-gate #define CLD_TRAPPED 4 /* traced child has stopped */ 89*0Sstevel@tonic-gate #define CLD_STOPPED 5 /* child has stopped on signal */ 90*0Sstevel@tonic-gate #define CLD_CONTINUED 6 /* stopped child has continued */ 91*0Sstevel@tonic-gate #define NSIGCLD 6 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * id type from SVR4 procset.h 95*0Sstevel@tonic-gate */ 96*0Sstevel@tonic-gate typedef enum idtype { 97*0Sstevel@tonic-gate P_PID, /* A process identifier. */ 98*0Sstevel@tonic-gate P_PPID, /* A parent process identifier. */ 99*0Sstevel@tonic-gate P_PGID, /* A process group (job control group) */ 100*0Sstevel@tonic-gate /* identifier. */ 101*0Sstevel@tonic-gate P_SID, /* A session identifier. */ 102*0Sstevel@tonic-gate P_CID, /* A scheduling class identifier. */ 103*0Sstevel@tonic-gate P_UID, /* A user identifier. */ 104*0Sstevel@tonic-gate P_GID, /* A group identifier. */ 105*0Sstevel@tonic-gate P_ALL /* All processes. */ 106*0Sstevel@tonic-gate } idtype_t; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate static void mapstatus(int *, int); 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate int 111*0Sstevel@tonic-gate wait(int *status) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate int ret, nstatus; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate if ((int)status == -1) { 116*0Sstevel@tonic-gate errno = EFAULT; 117*0Sstevel@tonic-gate return (-1); 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate ret = _wait(&nstatus); 121*0Sstevel@tonic-gate if (status) 122*0Sstevel@tonic-gate mapstatus(status, nstatus); 123*0Sstevel@tonic-gate return (ret); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate int 127*0Sstevel@tonic-gate waitpid(int pid, int *status, int options) 128*0Sstevel@tonic-gate { 129*0Sstevel@tonic-gate int noptions, ret; 130*0Sstevel@tonic-gate int nstatus; 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate if ((int)status == -1) { 133*0Sstevel@tonic-gate errno = EFAULT; 134*0Sstevel@tonic-gate return (-1); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /* 138*0Sstevel@tonic-gate * BSD's wait* routines only support WNOHANG & WUNTRACED 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED)) 141*0Sstevel@tonic-gate return (EINVAL); 142*0Sstevel@tonic-gate noptions = (N_WEXITED|N_WTRAPPED); 143*0Sstevel@tonic-gate if (options & WNOHANG) 144*0Sstevel@tonic-gate noptions |= N_WNOHANG; 145*0Sstevel@tonic-gate if (options & WUNTRACED) 146*0Sstevel@tonic-gate noptions |= N_WUNTRACED; /* == N_WSTOPPED */ 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate ret = _waitpid(pid, &nstatus, noptions); 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate if (status) 151*0Sstevel@tonic-gate mapstatus(status, nstatus); 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate return (ret); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate /* 157*0Sstevel@tonic-gate * It would be -so- nice just to call _wait3 and mapstatus here. 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate int 160*0Sstevel@tonic-gate wait3(int *status, int options, struct rusage *rp) 161*0Sstevel@tonic-gate { 162*0Sstevel@tonic-gate return (wait4(0, status, options, rp)); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate static int wstat(int, int); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* 168*0Sstevel@tonic-gate * It would be -so- nice just to call _wait4 and mapstatus here. 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate int 171*0Sstevel@tonic-gate wait4(int pid, int *status, int options, struct rusage *rp) 172*0Sstevel@tonic-gate { 173*0Sstevel@tonic-gate struct tms before_tms; 174*0Sstevel@tonic-gate struct tms after_tms; 175*0Sstevel@tonic-gate siginfo_t info; 176*0Sstevel@tonic-gate int error; 177*0Sstevel@tonic-gate int noptions; 178*0Sstevel@tonic-gate idtype_t idtype; 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate if ((int)status == -1 || (int)rp == -1) { 181*0Sstevel@tonic-gate errno = EFAULT; 182*0Sstevel@tonic-gate return(-1); 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate if (rp) 186*0Sstevel@tonic-gate memset(rp, 0, sizeof(struct rusage)); 187*0Sstevel@tonic-gate memset(&info, 0, sizeof (siginfo_t)); 188*0Sstevel@tonic-gate if (times(&before_tms) < 0) 189*0Sstevel@tonic-gate return (-1); /* errno is set by times() */ 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate /* 192*0Sstevel@tonic-gate * BSD's wait* routines only support WNOHANG & WUNTRACED 193*0Sstevel@tonic-gate */ 194*0Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED)) 195*0Sstevel@tonic-gate return (EINVAL); 196*0Sstevel@tonic-gate noptions = N_WEXITED | N_WTRAPPED; 197*0Sstevel@tonic-gate if (options & WNOHANG) 198*0Sstevel@tonic-gate noptions |= N_WNOHANG; 199*0Sstevel@tonic-gate if (options & WUNTRACED) 200*0Sstevel@tonic-gate noptions |= N_WUNTRACED; /* == N_WSTOPPED */ 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate /* 203*0Sstevel@tonic-gate * Emulate undocumented 4.x semantics for 1186845 204*0Sstevel@tonic-gate */ 205*0Sstevel@tonic-gate if (pid < 0) { 206*0Sstevel@tonic-gate pid = -pid; 207*0Sstevel@tonic-gate idtype = P_PGID; 208*0Sstevel@tonic-gate } else if (pid == 0) 209*0Sstevel@tonic-gate idtype = P_ALL; 210*0Sstevel@tonic-gate else 211*0Sstevel@tonic-gate idtype = P_PID; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate error = _waitid(idtype, pid, &info, noptions); 214*0Sstevel@tonic-gate if (error == 0) { 215*0Sstevel@tonic-gate long diffu; /* difference in usertime (ticks) */ 216*0Sstevel@tonic-gate long diffs; /* difference in systemtime (ticks) */ 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate if ((options & WNOHANG) && (info.si_pid == 0)) 219*0Sstevel@tonic-gate return (0); /* no child found */ 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate if (rp) { 222*0Sstevel@tonic-gate if (times(&after_tms) < 0) 223*0Sstevel@tonic-gate return (-1); /* errno already set by times() */ 224*0Sstevel@tonic-gate /* 225*0Sstevel@tonic-gate * The system/user time is an approximation only !!! 226*0Sstevel@tonic-gate */ 227*0Sstevel@tonic-gate diffu = after_tms.tms_cutime - before_tms.tms_cutime; 228*0Sstevel@tonic-gate diffs = after_tms.tms_cstime - before_tms.tms_cstime; 229*0Sstevel@tonic-gate rp->ru_utime.tv_sec = diffu / HZ; 230*0Sstevel@tonic-gate rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ); 231*0Sstevel@tonic-gate rp->ru_stime.tv_sec = diffs / HZ; 232*0Sstevel@tonic-gate rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate if (status) 235*0Sstevel@tonic-gate *status = wstat(info.si_code, info.si_status); 236*0Sstevel@tonic-gate return (info.si_pid); 237*0Sstevel@tonic-gate } else { 238*0Sstevel@tonic-gate return (-1); /* error number is set by waitid() */ 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate /* 244*0Sstevel@tonic-gate * Convert the status code to old style wait status 245*0Sstevel@tonic-gate */ 246*0Sstevel@tonic-gate static int 247*0Sstevel@tonic-gate wstat(int code, int status) 248*0Sstevel@tonic-gate { 249*0Sstevel@tonic-gate register stat = (status & 0377); 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate switch (code) { 252*0Sstevel@tonic-gate case CLD_EXITED: 253*0Sstevel@tonic-gate stat <<= 8; 254*0Sstevel@tonic-gate break; 255*0Sstevel@tonic-gate case CLD_KILLED: 256*0Sstevel@tonic-gate stat = maptooldsig(stat); 257*0Sstevel@tonic-gate if (code == CLD_DUMPED) 258*0Sstevel@tonic-gate stat |= WCOREFLG; 259*0Sstevel@tonic-gate break; 260*0Sstevel@tonic-gate case CLD_DUMPED: 261*0Sstevel@tonic-gate stat |= WCOREFLG; 262*0Sstevel@tonic-gate break; 263*0Sstevel@tonic-gate case CLD_TRAPPED: 264*0Sstevel@tonic-gate case CLD_STOPPED: 265*0Sstevel@tonic-gate stat = maptooldsig(stat); 266*0Sstevel@tonic-gate stat <<= 8; 267*0Sstevel@tonic-gate stat |= _WSTOPPED; 268*0Sstevel@tonic-gate break; 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate return (stat); 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate static void 274*0Sstevel@tonic-gate mapstatus(int *new, int old) 275*0Sstevel@tonic-gate { 276*0Sstevel@tonic-gate int stat = old & 0xFF; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate switch(stat) { 279*0Sstevel@tonic-gate case _WSTOPPED: 280*0Sstevel@tonic-gate *new = maptooldsig(stat >> 8); 281*0Sstevel@tonic-gate *new = (stat << 8) | _WSTOPPED; 282*0Sstevel@tonic-gate break; 283*0Sstevel@tonic-gate case 0: 284*0Sstevel@tonic-gate *new = old; 285*0Sstevel@tonic-gate break; 286*0Sstevel@tonic-gate default: 287*0Sstevel@tonic-gate *new = maptooldsig(old & 0x7F); 288*0Sstevel@tonic-gate if (old & 0x80) 289*0Sstevel@tonic-gate *new |= 0x80; /* set WCOREFLG */ 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate } 292