17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 1995 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 327c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * Compatibility lib for BSD's wait3() and wait4(). 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <errno.h> 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/time.h> 447c478bd9Sstevel@tonic-gate #include <sys/times.h> 457c478bd9Sstevel@tonic-gate #include <sys/wait.h> 467c478bd9Sstevel@tonic-gate #include <sys/param.h> 477c478bd9Sstevel@tonic-gate #include <sys/resource.h> 487c478bd9Sstevel@tonic-gate #include "signalmap.h" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * Since sysV does not support rusage as in BSD, an approximate approach 527c478bd9Sstevel@tonic-gate * is: 537c478bd9Sstevel@tonic-gate * ... 547c478bd9Sstevel@tonic-gate * call times 557c478bd9Sstevel@tonic-gate * call waitid 567c478bd9Sstevel@tonic-gate * if ( a child is found ) 577c478bd9Sstevel@tonic-gate * call times again 587c478bd9Sstevel@tonic-gate * rusage ~= diff in the 2 times call 597c478bd9Sstevel@tonic-gate * ... 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * arguments to wait functions from SVR4 657c478bd9Sstevel@tonic-gate */ 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #define N_WEXITED 0001 /* wait for processes that have exite */ 687c478bd9Sstevel@tonic-gate #define N_WTRAPPED 0002 /* wait for processes stopped while tracing */ 697c478bd9Sstevel@tonic-gate #define N_WSTOPPED 0004 /* wait for processes stopped by signals */ 707c478bd9Sstevel@tonic-gate #define N_WCONTINUED 0010 /* wait for processes continued */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define N_WUNTRACED N_WSTOPPED /* for POSIX */ 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define N_WNOHANG 0100 /* non blocking form of wait */ 757c478bd9Sstevel@tonic-gate #define N_WNOWAIT 0200 /* non destructive form of wait */ 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #define WCOREFLG 0200 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* 807c478bd9Sstevel@tonic-gate * SIGCLD signal codes from SVr4 817c478bd9Sstevel@tonic-gate */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #define CLD_EXITED 1 /* child has exited */ 847c478bd9Sstevel@tonic-gate #define CLD_KILLED 2 /* child was killed */ 857c478bd9Sstevel@tonic-gate #define CLD_DUMPED 3 /* child has coredumped */ 867c478bd9Sstevel@tonic-gate #define CLD_TRAPPED 4 /* traced child has stopped */ 877c478bd9Sstevel@tonic-gate #define CLD_STOPPED 5 /* child has stopped on signal */ 887c478bd9Sstevel@tonic-gate #define CLD_CONTINUED 6 /* stopped child has continued */ 897c478bd9Sstevel@tonic-gate #define NSIGCLD 6 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* 927c478bd9Sstevel@tonic-gate * id type from SVR4 procset.h 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate typedef enum idtype { 957c478bd9Sstevel@tonic-gate P_PID, /* A process identifier. */ 967c478bd9Sstevel@tonic-gate P_PPID, /* A parent process identifier. */ 977c478bd9Sstevel@tonic-gate P_PGID, /* A process group (job control group) */ 987c478bd9Sstevel@tonic-gate /* identifier. */ 997c478bd9Sstevel@tonic-gate P_SID, /* A session identifier. */ 1007c478bd9Sstevel@tonic-gate P_CID, /* A scheduling class identifier. */ 1017c478bd9Sstevel@tonic-gate P_UID, /* A user identifier. */ 1027c478bd9Sstevel@tonic-gate P_GID, /* A group identifier. */ 1037c478bd9Sstevel@tonic-gate P_ALL /* All processes. */ 1047c478bd9Sstevel@tonic-gate } idtype_t; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate static void mapstatus(int *, int); 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate int 1097c478bd9Sstevel@tonic-gate wait(int *status) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate int ret, nstatus; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if ((int)status == -1) { 1147c478bd9Sstevel@tonic-gate errno = EFAULT; 1157c478bd9Sstevel@tonic-gate return (-1); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate ret = _wait(&nstatus); 1197c478bd9Sstevel@tonic-gate if (status) 1207c478bd9Sstevel@tonic-gate mapstatus(status, nstatus); 1217c478bd9Sstevel@tonic-gate return (ret); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate int 1257c478bd9Sstevel@tonic-gate waitpid(int pid, int *status, int options) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate int noptions, ret; 1287c478bd9Sstevel@tonic-gate int nstatus; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate if ((int)status == -1) { 1317c478bd9Sstevel@tonic-gate errno = EFAULT; 1327c478bd9Sstevel@tonic-gate return (-1); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * BSD's wait* routines only support WNOHANG & WUNTRACED 1377c478bd9Sstevel@tonic-gate */ 1387c478bd9Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED)) 1397c478bd9Sstevel@tonic-gate return (EINVAL); 1407c478bd9Sstevel@tonic-gate noptions = (N_WEXITED|N_WTRAPPED); 1417c478bd9Sstevel@tonic-gate if (options & WNOHANG) 1427c478bd9Sstevel@tonic-gate noptions |= N_WNOHANG; 1437c478bd9Sstevel@tonic-gate if (options & WUNTRACED) 1447c478bd9Sstevel@tonic-gate noptions |= N_WUNTRACED; /* == N_WSTOPPED */ 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate ret = _waitpid(pid, &nstatus, noptions); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate if (status) 1497c478bd9Sstevel@tonic-gate mapstatus(status, nstatus); 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate return (ret); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * It would be -so- nice just to call _wait3 and mapstatus here. 1567c478bd9Sstevel@tonic-gate */ 1577c478bd9Sstevel@tonic-gate int 1587c478bd9Sstevel@tonic-gate wait3(int *status, int options, struct rusage *rp) 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate return (wait4(0, status, options, rp)); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate static int wstat(int, int); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * It would be -so- nice just to call _wait4 and mapstatus here. 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate int 1697c478bd9Sstevel@tonic-gate wait4(int pid, int *status, int options, struct rusage *rp) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate struct tms before_tms; 1727c478bd9Sstevel@tonic-gate struct tms after_tms; 1737c478bd9Sstevel@tonic-gate siginfo_t info; 1747c478bd9Sstevel@tonic-gate int error; 1757c478bd9Sstevel@tonic-gate int noptions; 1767c478bd9Sstevel@tonic-gate idtype_t idtype; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate if ((int)status == -1 || (int)rp == -1) { 1797c478bd9Sstevel@tonic-gate errno = EFAULT; 1807c478bd9Sstevel@tonic-gate return(-1); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (rp) 1847c478bd9Sstevel@tonic-gate memset(rp, 0, sizeof(struct rusage)); 1857c478bd9Sstevel@tonic-gate memset(&info, 0, sizeof (siginfo_t)); 1867c478bd9Sstevel@tonic-gate if (times(&before_tms) < 0) 1877c478bd9Sstevel@tonic-gate return (-1); /* errno is set by times() */ 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* 1907c478bd9Sstevel@tonic-gate * BSD's wait* routines only support WNOHANG & WUNTRACED 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate if (options & ~(WNOHANG|WUNTRACED)) 1937c478bd9Sstevel@tonic-gate return (EINVAL); 1947c478bd9Sstevel@tonic-gate noptions = N_WEXITED | N_WTRAPPED; 1957c478bd9Sstevel@tonic-gate if (options & WNOHANG) 1967c478bd9Sstevel@tonic-gate noptions |= N_WNOHANG; 1977c478bd9Sstevel@tonic-gate if (options & WUNTRACED) 1987c478bd9Sstevel@tonic-gate noptions |= N_WUNTRACED; /* == N_WSTOPPED */ 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * Emulate undocumented 4.x semantics for 1186845 2027c478bd9Sstevel@tonic-gate */ 2037c478bd9Sstevel@tonic-gate if (pid < 0) { 2047c478bd9Sstevel@tonic-gate pid = -pid; 2057c478bd9Sstevel@tonic-gate idtype = P_PGID; 2067c478bd9Sstevel@tonic-gate } else if (pid == 0) 2077c478bd9Sstevel@tonic-gate idtype = P_ALL; 2087c478bd9Sstevel@tonic-gate else 2097c478bd9Sstevel@tonic-gate idtype = P_PID; 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate error = _waitid(idtype, pid, &info, noptions); 2127c478bd9Sstevel@tonic-gate if (error == 0) { 2137c478bd9Sstevel@tonic-gate long diffu; /* difference in usertime (ticks) */ 2147c478bd9Sstevel@tonic-gate long diffs; /* difference in systemtime (ticks) */ 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if ((options & WNOHANG) && (info.si_pid == 0)) 2177c478bd9Sstevel@tonic-gate return (0); /* no child found */ 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate if (rp) { 2207c478bd9Sstevel@tonic-gate if (times(&after_tms) < 0) 2217c478bd9Sstevel@tonic-gate return (-1); /* errno already set by times() */ 2227c478bd9Sstevel@tonic-gate /* 2237c478bd9Sstevel@tonic-gate * The system/user time is an approximation only !!! 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate diffu = after_tms.tms_cutime - before_tms.tms_cutime; 2267c478bd9Sstevel@tonic-gate diffs = after_tms.tms_cstime - before_tms.tms_cstime; 2277c478bd9Sstevel@tonic-gate rp->ru_utime.tv_sec = diffu / HZ; 2287c478bd9Sstevel@tonic-gate rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ); 2297c478bd9Sstevel@tonic-gate rp->ru_stime.tv_sec = diffs / HZ; 2307c478bd9Sstevel@tonic-gate rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate if (status) 2337c478bd9Sstevel@tonic-gate *status = wstat(info.si_code, info.si_status); 2347c478bd9Sstevel@tonic-gate return (info.si_pid); 2357c478bd9Sstevel@tonic-gate } else { 2367c478bd9Sstevel@tonic-gate return (-1); /* error number is set by waitid() */ 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * Convert the status code to old style wait status 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate static int 2457c478bd9Sstevel@tonic-gate wstat(int code, int status) 2467c478bd9Sstevel@tonic-gate { 247*5d54f3d8Smuffin int stat = (status & 0377); 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate switch (code) { 2507c478bd9Sstevel@tonic-gate case CLD_EXITED: 2517c478bd9Sstevel@tonic-gate stat <<= 8; 2527c478bd9Sstevel@tonic-gate break; 2537c478bd9Sstevel@tonic-gate case CLD_KILLED: 2547c478bd9Sstevel@tonic-gate stat = maptooldsig(stat); 2557c478bd9Sstevel@tonic-gate if (code == CLD_DUMPED) 2567c478bd9Sstevel@tonic-gate stat |= WCOREFLG; 2577c478bd9Sstevel@tonic-gate break; 2587c478bd9Sstevel@tonic-gate case CLD_DUMPED: 2597c478bd9Sstevel@tonic-gate stat |= WCOREFLG; 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate case CLD_TRAPPED: 2627c478bd9Sstevel@tonic-gate case CLD_STOPPED: 2637c478bd9Sstevel@tonic-gate stat = maptooldsig(stat); 2647c478bd9Sstevel@tonic-gate stat <<= 8; 2657c478bd9Sstevel@tonic-gate stat |= _WSTOPPED; 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate return (stat); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate static void 2727c478bd9Sstevel@tonic-gate mapstatus(int *new, int old) 2737c478bd9Sstevel@tonic-gate { 2747c478bd9Sstevel@tonic-gate int stat = old & 0xFF; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate switch(stat) { 2777c478bd9Sstevel@tonic-gate case _WSTOPPED: 2787c478bd9Sstevel@tonic-gate *new = maptooldsig(stat >> 8); 2797c478bd9Sstevel@tonic-gate *new = (stat << 8) | _WSTOPPED; 2807c478bd9Sstevel@tonic-gate break; 2817c478bd9Sstevel@tonic-gate case 0: 2827c478bd9Sstevel@tonic-gate *new = old; 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate default: 2857c478bd9Sstevel@tonic-gate *new = maptooldsig(old & 0x7F); 2867c478bd9Sstevel@tonic-gate if (old & 0x80) 2877c478bd9Sstevel@tonic-gate *new |= 0x80; /* set WCOREFLG */ 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate } 290