Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_wait.c revision 1.2.10.1
      1  1.2.10.1  lukem /*	$NetBSD: netbsd32_wait.c,v 1.2.10.1 2002/07/29 15:39:18 lukem Exp $	*/
      2       1.1    mrg 
      3       1.1    mrg /*
      4       1.1    mrg  * Copyright (c) 1998, 2001 Matthew R. Green
      5       1.1    mrg  * All rights reserved.
      6       1.1    mrg  *
      7       1.1    mrg  * Redistribution and use in source and binary forms, with or without
      8       1.1    mrg  * modification, are permitted provided that the following conditions
      9       1.1    mrg  * are met:
     10       1.1    mrg  * 1. Redistributions of source code must retain the above copyright
     11       1.1    mrg  *    notice, this list of conditions and the following disclaimer.
     12       1.1    mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    mrg  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    mrg  *    documentation and/or other materials provided with the distribution.
     15       1.1    mrg  * 3. The name of the author may not be used to endorse or promote products
     16       1.1    mrg  *    derived from this software without specific prior written permission.
     17       1.1    mrg  *
     18       1.1    mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1    mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1    mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1    mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1    mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23       1.1    mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24       1.1    mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25       1.1    mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26       1.1    mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1    mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1    mrg  * SUCH DAMAGE.
     29       1.1    mrg  */
     30       1.2  lukem 
     31       1.2  lukem #include <sys/cdefs.h>
     32       1.2  lukem __KERNEL_RCSID(0, "$NetBSD: netbsd32_wait.c,v 1.2.10.1 2002/07/29 15:39:18 lukem Exp $");
     33       1.1    mrg 
     34       1.1    mrg #include <sys/param.h>
     35       1.1    mrg #include <sys/systm.h>
     36       1.1    mrg #include <sys/mount.h>
     37       1.1    mrg #include <sys/time.h>
     38       1.1    mrg #include <sys/wait.h>
     39       1.1    mrg #include <sys/ptrace.h>
     40       1.1    mrg #include <sys/resourcevar.h>
     41       1.1    mrg #include <sys/vnode.h>
     42       1.1    mrg #include <sys/pool.h>
     43       1.1    mrg #include <sys/proc.h>
     44       1.1    mrg 
     45       1.1    mrg #include <compat/netbsd32/netbsd32.h>
     46       1.1    mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     47       1.1    mrg #include <compat/netbsd32/netbsd32_conv.h>
     48       1.1    mrg 
     49       1.1    mrg int
     50       1.1    mrg netbsd32_wait4(q, v, retval)
     51       1.1    mrg 	struct proc *q;
     52       1.1    mrg 	void *v;
     53       1.1    mrg 	register_t *retval;
     54       1.1    mrg {
     55       1.1    mrg 	struct netbsd32_wait4_args /* {
     56       1.1    mrg 		syscallarg(int) pid;
     57       1.1    mrg 		syscallarg(netbsd32_intp) status;
     58       1.1    mrg 		syscallarg(int) options;
     59       1.1    mrg 		syscallarg(netbsd32_rusagep_t) rusage;
     60       1.1    mrg 	} */ *uap = v;
     61       1.1    mrg 	struct netbsd32_rusage ru32;
     62       1.1    mrg 	int nfound;
     63       1.1    mrg 	struct proc *p, *t;
     64       1.1    mrg 	int status, error;
     65       1.1    mrg 
     66       1.1    mrg 	if (SCARG(uap, pid) == 0)
     67       1.1    mrg 		SCARG(uap, pid) = -q->p_pgid;
     68       1.1    mrg 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
     69       1.1    mrg 		return (EINVAL);
     70       1.1    mrg 
     71       1.1    mrg loop:
     72       1.1    mrg 	nfound = 0;
     73       1.1    mrg 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
     74       1.1    mrg 		if (SCARG(uap, pid) != WAIT_ANY &&
     75       1.1    mrg 		    p->p_pid != SCARG(uap, pid) &&
     76       1.1    mrg 		    p->p_pgid != -SCARG(uap, pid))
     77       1.1    mrg 			continue;
     78       1.1    mrg 		nfound++;
     79       1.1    mrg 		if (p->p_stat == SZOMB) {
     80       1.1    mrg 			retval[0] = p->p_pid;
     81       1.1    mrg 
     82       1.1    mrg 			if (SCARG(uap, status)) {
     83       1.1    mrg 				status = p->p_xstat;	/* convert to int */
     84       1.1    mrg 				error = copyout((caddr_t)&status,
     85       1.1    mrg 						(caddr_t)(u_long)SCARG(uap, status),
     86       1.1    mrg 						sizeof(status));
     87       1.1    mrg 				if (error)
     88       1.1    mrg 					return (error);
     89       1.1    mrg 			}
     90       1.1    mrg 			if (SCARG(uap, rusage)) {
     91       1.1    mrg 				netbsd32_from_rusage(p->p_ru, &ru32);
     92       1.1    mrg 				if ((error = copyout((caddr_t)&ru32,
     93       1.1    mrg 						     (caddr_t)(u_long)SCARG(uap, rusage),
     94       1.1    mrg 						     sizeof(struct netbsd32_rusage))))
     95       1.1    mrg 					return (error);
     96       1.1    mrg 			}
     97       1.1    mrg 			/*
     98       1.1    mrg 			 * If we got the child via ptrace(2) or procfs, and
     99       1.1    mrg 			 * the parent is different (meaning the process was
    100       1.1    mrg 			 * attached, rather than run as a child), then we need
    101       1.1    mrg 			 * to give it back to the old parent, and send the
    102       1.1    mrg 			 * parent a SIGCHLD.  The rest of the cleanup will be
    103       1.1    mrg 			 * done when the old parent waits on the child.
    104       1.1    mrg 			 */
    105  1.2.10.1  lukem 			if ((p->p_flag & P_TRACED) && p->p_opptr != p->p_pptr){
    106  1.2.10.1  lukem 				t = p->p_opptr;
    107       1.1    mrg 				proc_reparent(p, t ? t : initproc);
    108  1.2.10.1  lukem 				p->p_opptr = NULL;
    109       1.1    mrg 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    110       1.1    mrg 				psignal(p->p_pptr, SIGCHLD);
    111       1.1    mrg 				wakeup((caddr_t)p->p_pptr);
    112       1.1    mrg 				return (0);
    113       1.1    mrg 			}
    114       1.1    mrg 			p->p_xstat = 0;
    115       1.1    mrg 			ruadd(&q->p_stats->p_cru, p->p_ru);
    116       1.1    mrg 			pool_put(&rusage_pool, p->p_ru);
    117       1.1    mrg 
    118       1.1    mrg 			/*
    119       1.1    mrg 			 * Finally finished with old proc entry.
    120       1.1    mrg 			 * Unlink it from its process group and free it.
    121       1.1    mrg 			 */
    122       1.1    mrg 			leavepgrp(p);
    123       1.1    mrg 
    124       1.1    mrg 			LIST_REMOVE(p, p_list);	/* off zombproc */
    125       1.1    mrg 
    126       1.1    mrg 			LIST_REMOVE(p, p_sibling);
    127       1.1    mrg 
    128       1.1    mrg 			/*
    129       1.1    mrg 			 * Decrement the count of procs running with this uid.
    130       1.1    mrg 			 */
    131       1.1    mrg 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    132       1.1    mrg 
    133       1.1    mrg 			/*
    134       1.1    mrg 			 * Free up credentials.
    135       1.1    mrg 			 */
    136       1.1    mrg 			if (--p->p_cred->p_refcnt == 0) {
    137       1.1    mrg 				crfree(p->p_cred->pc_ucred);
    138       1.1    mrg 				pool_put(&pcred_pool, p->p_cred);
    139       1.1    mrg 			}
    140       1.1    mrg 
    141       1.1    mrg 			/*
    142       1.1    mrg 			 * Release reference to text vnode
    143       1.1    mrg 			 */
    144       1.1    mrg 			if (p->p_textvp)
    145       1.1    mrg 				vrele(p->p_textvp);
    146       1.1    mrg 
    147       1.1    mrg 			pool_put(&proc_pool, p);
    148       1.1    mrg 			nprocs--;
    149       1.1    mrg 			return (0);
    150       1.1    mrg 		}
    151       1.1    mrg 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    152       1.1    mrg 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    153       1.1    mrg 			p->p_flag |= P_WAITED;
    154       1.1    mrg 			retval[0] = p->p_pid;
    155       1.1    mrg 
    156       1.1    mrg 			if (SCARG(uap, status)) {
    157       1.1    mrg 				status = W_STOPCODE(p->p_xstat);
    158       1.1    mrg 				error = copyout((caddr_t)&status,
    159       1.1    mrg 				    (caddr_t)(u_long)SCARG(uap, status),
    160       1.1    mrg 				    sizeof(status));
    161       1.1    mrg 			} else
    162       1.1    mrg 				error = 0;
    163       1.1    mrg 			return (error);
    164       1.1    mrg 		}
    165       1.1    mrg 	}
    166       1.1    mrg 	if (nfound == 0)
    167       1.1    mrg 		return (ECHILD);
    168       1.1    mrg 	if (SCARG(uap, options) & WNOHANG) {
    169       1.1    mrg 		retval[0] = 0;
    170       1.1    mrg 		return (0);
    171       1.1    mrg 	}
    172       1.1    mrg 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    173       1.1    mrg 		return (error);
    174       1.1    mrg 	goto loop;
    175       1.1    mrg }
    176       1.1    mrg 
    177       1.1    mrg int
    178       1.1    mrg netbsd32_getrusage(p, v, retval)
    179       1.1    mrg 	struct proc *p;
    180       1.1    mrg 	void *v;
    181       1.1    mrg 	register_t *retval;
    182       1.1    mrg {
    183       1.1    mrg 	struct netbsd32_getrusage_args /* {
    184       1.1    mrg 		syscallarg(int) who;
    185       1.1    mrg 		syscallarg(netbsd32_rusagep_t) rusage;
    186       1.1    mrg 	} */ *uap = v;
    187       1.1    mrg 	struct rusage *rup;
    188       1.1    mrg 	struct netbsd32_rusage ru;
    189       1.1    mrg 
    190       1.1    mrg 	switch (SCARG(uap, who)) {
    191       1.1    mrg 
    192       1.1    mrg 	case RUSAGE_SELF:
    193       1.1    mrg 		rup = &p->p_stats->p_ru;
    194       1.1    mrg 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    195       1.1    mrg 		break;
    196       1.1    mrg 
    197       1.1    mrg 	case RUSAGE_CHILDREN:
    198       1.1    mrg 		rup = &p->p_stats->p_cru;
    199       1.1    mrg 		break;
    200       1.1    mrg 
    201       1.1    mrg 	default:
    202       1.1    mrg 		return (EINVAL);
    203       1.1    mrg 	}
    204       1.1    mrg 	netbsd32_from_rusage(rup, &ru);
    205       1.1    mrg 	return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
    206       1.1    mrg }
    207