wait.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
2187N/A/*
2187N/A * CDDL HEADER START
2187N/A *
2187N/A * The contents of this file are subject to the terms of the
2187N/A * Common Development and Distribution License, Version 1.0 only
2187N/A * (the "License"). You may not use this file except in compliance
2187N/A * with the License.
2187N/A *
2187N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2187N/A * or http://www.opensolaris.org/os/licensing.
2187N/A * See the License for the specific language governing permissions
2187N/A * and limitations under the License.
2187N/A *
2187N/A * When distributing Covered Code, include this CDDL HEADER in each
2187N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2187N/A * If applicable, add the following below this CDDL HEADER, with the
2187N/A * fields enclosed by brackets "[]" replaced with your own identifying
2187N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2187N/A *
2187N/A * CDDL HEADER END
2187N/A */
2187N/A/*
2187N/A * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
2187N/A * Use is subject to license terms.
2187N/A */
2187N/A
2187N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2187N/A/* All Rights Reserved */
2187N/A
2754N/A/*
2187N/A * Portions of this source code were derived from Berkeley 4.3 BSD
2187N/A * under license from the Regents of the University of California.
2520N/A */
2625N/A
2187N/A#pragma ident "%Z%%M% %I% %E% SMI"
2520N/A
2187N/A/*
2520N/A * Compatibility lib for BSD's wait3() and wait4().
2488N/A */
2187N/A
2187N/A#include <errno.h>
2520N/A#include <sys/types.h>
2754N/A#include <sys/time.h>
2488N/A#include <sys/times.h>
2520N/A#include <sys/wait.h>
2520N/A#include <sys/param.h>
2614N/A#include <sys/resource.h>
2658N/A#include "signalmap.h"
2187N/A
2488N/A/*
2187N/A * Since sysV does not support rusage as in BSD, an approximate approach
2187N/A * is:
2520N/A * ...
2520N/A * call times
2604N/A * call waitid
2604N/A * if ( a child is found )
2604N/A * call times again
2658N/A * rusage ~= diff in the 2 times call
2658N/A * ...
2658N/A *
2488N/A */
2187N/A
2187N/Aextern int errno;
2187N/A
2187N/A/*
2187N/A * arguments to wait functions from SVR4
2187N/A */
2187N/A
2187N/A#define N_WEXITED 0001 /* wait for processes that have exite */
2554N/A#define N_WTRAPPED 0002 /* wait for processes stopped while tracing */
2554N/A#define N_WSTOPPED 0004 /* wait for processes stopped by signals */
2554N/A#define N_WCONTINUED 0010 /* wait for processes continued */
2554N/A
2554N/A#define N_WUNTRACED N_WSTOPPED /* for POSIX */
2554N/A
2554N/A#define N_WNOHANG 0100 /* non blocking form of wait */
2604N/A#define N_WNOWAIT 0200 /* non destructive form of wait */
2604N/A
2604N/A#define WCOREFLG 0200
2604N/A
2520N/A/*
2520N/A * SIGCLD signal codes from SVr4
2604N/A */
2520N/A
2520N/A#define CLD_EXITED 1 /* child has exited */
2658N/A#define CLD_KILLED 2 /* child was killed */
2658N/A#define CLD_DUMPED 3 /* child has coredumped */
2658N/A#define CLD_TRAPPED 4 /* traced child has stopped */
2658N/A#define CLD_STOPPED 5 /* child has stopped on signal */
2658N/A#define CLD_CONTINUED 6 /* stopped child has continued */
2658N/A#define NSIGCLD 6
2658N/A
2658N/A/*
2658N/A * id type from SVR4 procset.h
2658N/A */
2658N/Atypedef enum idtype {
2658N/A P_PID, /* A process identifier. */
2658N/A P_PPID, /* A parent process identifier. */
2658N/A P_PGID, /* A process group (job control group) */
2658N/A /* identifier. */
2520N/A P_SID, /* A session identifier. */
2520N/A P_CID, /* A scheduling class identifier. */
2520N/A P_UID, /* A user identifier. */
2520N/A P_GID, /* A group identifier. */
2520N/A P_ALL /* All processes. */
2520N/A} idtype_t;
2520N/A
2520N/Astatic void mapstatus(int *, int);
2520N/A
2520N/Aint
2520N/Await(int *status)
2520N/A{
2520N/A int ret, nstatus;
2520N/A
2520N/A if ((int)status == -1) {
2520N/A errno = EFAULT;
2520N/A return (-1);
2520N/A }
2520N/A
2520N/A ret = _wait(&nstatus);
2520N/A if (status)
2520N/A mapstatus(status, nstatus);
2520N/A return (ret);
2520N/A}
2520N/A
2625N/Aint
2625N/Awaitpid(int pid, int *status, int options)
2625N/A{
2554N/A int noptions, ret;
2625N/A int nstatus;
2625N/A
2625N/A if ((int)status == -1) {
2625N/A errno = EFAULT;
2520N/A return (-1);
2520N/A }
2754N/A
2754N/A /*
2754N/A * BSD's wait* routines only support WNOHANG & WUNTRACED
2754N/A */
2754N/A if (options & ~(WNOHANG|WUNTRACED))
2754N/A return (EINVAL);
2754N/A noptions = (N_WEXITED|N_WTRAPPED);
2754N/A if (options & WNOHANG)
2754N/A noptions |= N_WNOHANG;
2754N/A if (options & WUNTRACED)
2754N/A noptions |= N_WUNTRACED; /* == N_WSTOPPED */
2754N/A
2754N/A ret = _waitpid(pid, &nstatus, noptions);
2754N/A
2604N/A if (status)
2604N/A mapstatus(status, nstatus);
2604N/A
2604N/A return (ret);
2604N/A}
2604N/A
2604N/A/*
2625N/A * It would be -so- nice just to call _wait3 and mapstatus here.
2658N/A */
2658N/Aint
2658N/Await3(int *status, int options, struct rusage *rp)
2658N/A{
2658N/A return (wait4(0, status, options, rp));
2658N/A}
2658N/A
2658N/Astatic int wstat(int, int);
2658N/A
2658N/A/*
2658N/A * It would be -so- nice just to call _wait4 and mapstatus here.
2658N/A */
2658N/Aint
2658N/Await4(int pid, int *status, int options, struct rusage *rp)
2658N/A{
2658N/A struct tms before_tms;
2658N/A struct tms after_tms;
2658N/A siginfo_t info;
2658N/A int error;
2658N/A int noptions;
2658N/A idtype_t idtype;
2658N/A
2658N/A if ((int)status == -1 || (int)rp == -1) {
2658N/A errno = EFAULT;
2658N/A return(-1);
2658N/A }
2658N/A
2658N/A if (rp)
2658N/A memset(rp, 0, sizeof(struct rusage));
2658N/A memset(&info, 0, sizeof (siginfo_t));
2658N/A if (times(&before_tms) < 0)
2658N/A return (-1); /* errno is set by times() */
2658N/A
2658N/A /*
2658N/A * BSD's wait* routines only support WNOHANG & WUNTRACED
2658N/A */
2520N/A if (options & ~(WNOHANG|WUNTRACED))
2520N/A return (EINVAL);
2520N/A noptions = N_WEXITED | N_WTRAPPED;
2520N/A if (options & WNOHANG)
2520N/A noptions |= N_WNOHANG;
2520N/A if (options & WUNTRACED)
2520N/A noptions |= N_WUNTRACED; /* == N_WSTOPPED */
2520N/A
2520N/A /*
2520N/A * Emulate undocumented 4.x semantics for 1186845
2520N/A */
2520N/A if (pid < 0) {
2520N/A pid = -pid;
2520N/A idtype = P_PGID;
2520N/A } else if (pid == 0)
2554N/A idtype = P_ALL;
2520N/A else
2520N/A idtype = P_PID;
2520N/A
2520N/A error = _waitid(idtype, pid, &info, noptions);
2520N/A if (error == 0) {
2520N/A long diffu; /* difference in usertime (ticks) */
2658N/A long diffs; /* difference in systemtime (ticks) */
2658N/A
2658N/A if ((options & WNOHANG) && (info.si_pid == 0))
2658N/A return (0); /* no child found */
2658N/A
2658N/A if (rp) {
2658N/A if (times(&after_tms) < 0)
2658N/A return (-1); /* errno already set by times() */
2658N/A /*
2658N/A * The system/user time is an approximation only !!!
2658N/A */
2658N/A diffu = after_tms.tms_cutime - before_tms.tms_cutime;
2658N/A diffs = after_tms.tms_cstime - before_tms.tms_cstime;
2658N/A rp->ru_utime.tv_sec = diffu / HZ;
2658N/A rp->ru_utime.tv_usec = (diffu % HZ) * (1000000 / HZ);
2658N/A rp->ru_stime.tv_sec = diffs / HZ;
2658N/A rp->ru_stime.tv_usec = (diffs % HZ) * (1000000 / HZ);
2658N/A }
2658N/A if (status)
2658N/A *status = wstat(info.si_code, info.si_status);
2658N/A return (info.si_pid);
2658N/A } else {
2658N/A return (-1); /* error number is set by waitid() */
2658N/A }
2658N/A}
2658N/A
2658N/A
2658N/A/*
2658N/A * Convert the status code to old style wait status
2658N/A */
2658N/Astatic int
2658N/Awstat(int code, int status)
2658N/A{
2658N/A register stat = (status & 0377);
2658N/A
2658N/A switch (code) {
2658N/A case CLD_EXITED:
2658N/A stat <<= 8;
2658N/A break;
2658N/A case CLD_KILLED:
2658N/A stat = maptooldsig(stat);
2658N/A if (code == CLD_DUMPED)
2658N/A stat |= WCOREFLG;
2658N/A break;
2658N/A case CLD_DUMPED:
2658N/A stat |= WCOREFLG;
2658N/A break;
2658N/A case CLD_TRAPPED:
2658N/A case CLD_STOPPED:
2658N/A stat = maptooldsig(stat);
2658N/A stat <<= 8;
2658N/A stat |= _WSTOPPED;
2658N/A break;
2658N/A }
2520N/A return (stat);
2520N/A}
2520N/A
2520N/Astatic void
2520N/Amapstatus(int *new, int old)
2520N/A{
2520N/A int stat = old & 0xFF;
2604N/A
2604N/A switch(stat) {
2520N/A case _WSTOPPED:
2554N/A *new = maptooldsig(stat >> 8);
2554N/A *new = (stat << 8) | _WSTOPPED;
2554N/A break;
2554N/A case 0:
2554N/A *new = old;
2554N/A break;
2754N/A default:
2554N/A *new = maptooldsig(old & 0x7F);
2520N/A if (old & 0x80)
2520N/A *new |= 0x80; /* set WCOREFLG */
2520N/A }
2520N/A}
2520N/A