Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.18
      1  1.17  cgd /*-
      2  1.17  cgd  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
      3  1.17  cgd  * All rights reserved.
      4  1.17  cgd  * (c) UNIX System Laboratories, Inc.
      5  1.17  cgd  * All or some portions of this file are derived from material licensed
      6  1.17  cgd  * to the University of California by American Telephone and Telegraph
      7  1.17  cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  1.17  cgd  * the permission of UNIX System Laboratories, Inc.
      9  1.17  cgd  *
     10  1.17  cgd  * Redistribution and use in source and binary forms, with or without
     11  1.17  cgd  * modification, are permitted provided that the following conditions
     12  1.17  cgd  * are met:
     13  1.17  cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.17  cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.17  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.17  cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.17  cgd  *    documentation and/or other materials provided with the distribution.
     18  1.17  cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.17  cgd  *    must display the following acknowledgement:
     20  1.17  cgd  *	This product includes software developed by the University of
     21  1.17  cgd  *	California, Berkeley and its contributors.
     22  1.17  cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.17  cgd  *    may be used to endorse or promote products derived from this software
     24  1.17  cgd  *    without specific prior written permission.
     25  1.17  cgd  *
     26  1.17  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.17  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.17  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.17  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.17  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.17  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.17  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.17  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.17  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.17  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.17  cgd  * SUCH DAMAGE.
     37  1.17  cgd  *
     38  1.17  cgd  *	from: @(#)kern_resource.c	7.13 (Berkeley) 5/9/91
     39  1.18  cgd  *	$Id: kern_resource.c,v 1.18 1994/05/18 05:12:39 cgd Exp $
     40  1.17  cgd  */
     41  1.17  cgd 
     42  1.17  cgd #include <sys/param.h>
     43  1.17  cgd #include <sys/systm.h>
     44  1.17  cgd #include <sys/kernel.h>
     45  1.17  cgd #include <sys/resourcevar.h>
     46  1.17  cgd #include <sys/malloc.h>
     47  1.17  cgd #include <sys/proc.h>
     48  1.17  cgd 
     49  1.17  cgd #include <vm/vm.h>
     50  1.17  cgd 
     51  1.17  cgd int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
     52  1.17  cgd 
     53  1.17  cgd /*
     54  1.17  cgd  * Resource controls and accounting.
     55  1.17  cgd  */
     56  1.17  cgd 
     57  1.17  cgd struct getpriority_args {
     58  1.17  cgd 	int	which;
     59  1.17  cgd 	int	who;
     60  1.17  cgd };
     61  1.17  cgd 
     62  1.17  cgd int
     63  1.17  cgd getpriority(curp, uap, retval)
     64  1.17  cgd 	struct proc *curp;
     65  1.17  cgd 	register struct getpriority_args *uap;
     66  1.17  cgd 	int *retval;
     67  1.17  cgd {
     68  1.17  cgd 	register struct proc *p;
     69  1.17  cgd 	register int low = PRIO_MAX + 1;
     70  1.17  cgd 
     71  1.17  cgd 	switch (uap->which) {
     72  1.17  cgd 
     73  1.17  cgd 	case PRIO_PROCESS:
     74  1.17  cgd 		if (uap->who == 0)
     75  1.17  cgd 			p = curp;
     76  1.17  cgd 		else
     77  1.17  cgd 			p = pfind(uap->who);
     78  1.17  cgd 		if (p == 0)
     79  1.17  cgd 			break;
     80  1.17  cgd 		low = p->p_nice;
     81  1.17  cgd 		break;
     82  1.17  cgd 
     83  1.17  cgd 	case PRIO_PGRP: {
     84  1.17  cgd 		register struct pgrp *pg;
     85  1.17  cgd 
     86  1.17  cgd 		if (uap->who == 0)
     87  1.17  cgd 			pg = curp->p_pgrp;
     88  1.17  cgd 		else if ((pg = pgfind(uap->who)) == NULL)
     89  1.17  cgd 			break;
     90  1.17  cgd 		for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
     91  1.17  cgd 			if (p->p_nice < low)
     92  1.17  cgd 				low = p->p_nice;
     93  1.17  cgd 		}
     94  1.17  cgd 		break;
     95  1.17  cgd 	}
     96  1.17  cgd 
     97  1.17  cgd 	case PRIO_USER:
     98  1.17  cgd 		if (uap->who == 0)
     99  1.17  cgd 			uap->who = curp->p_ucred->cr_uid;
    100  1.17  cgd 		for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
    101  1.17  cgd 			if (p->p_ucred->cr_uid == uap->who &&
    102  1.17  cgd 			    p->p_nice < low)
    103  1.17  cgd 				low = p->p_nice;
    104  1.17  cgd 		}
    105  1.17  cgd 		break;
    106  1.17  cgd 
    107  1.17  cgd 	default:
    108  1.17  cgd 		return (EINVAL);
    109  1.17  cgd 	}
    110  1.17  cgd 	if (low == PRIO_MAX + 1)
    111  1.17  cgd 		return (ESRCH);
    112  1.17  cgd 	*retval = low;
    113  1.17  cgd 	return (0);
    114  1.17  cgd }
    115  1.17  cgd 
    116  1.17  cgd struct setpriority_args {
    117  1.17  cgd 	int	which;
    118  1.17  cgd 	int	who;
    119  1.17  cgd 	int	prio;
    120  1.17  cgd };
    121  1.17  cgd 
    122  1.17  cgd /* ARGSUSED */
    123  1.17  cgd int
    124  1.17  cgd setpriority(curp, uap, retval)
    125  1.17  cgd 	struct proc *curp;
    126  1.17  cgd 	register struct setpriority_args *uap;
    127  1.17  cgd 	int *retval;
    128  1.17  cgd {
    129  1.17  cgd 	register struct proc *p;
    130  1.17  cgd 	int found = 0, error = 0;
    131  1.17  cgd 
    132  1.17  cgd 	switch (uap->which) {
    133  1.17  cgd 
    134  1.17  cgd 	case PRIO_PROCESS:
    135  1.17  cgd 		if (uap->who == 0)
    136  1.17  cgd 			p = curp;
    137  1.17  cgd 		else
    138  1.17  cgd 			p = pfind(uap->who);
    139  1.17  cgd 		if (p == 0)
    140  1.17  cgd 			break;
    141  1.17  cgd 		error = donice(curp, p, uap->prio);
    142  1.17  cgd 		found++;
    143  1.17  cgd 		break;
    144  1.17  cgd 
    145  1.17  cgd 	case PRIO_PGRP: {
    146  1.17  cgd 		register struct pgrp *pg;
    147  1.17  cgd 
    148  1.17  cgd 		if (uap->who == 0)
    149  1.17  cgd 			pg = curp->p_pgrp;
    150  1.17  cgd 		else if ((pg = pgfind(uap->who)) == NULL)
    151  1.17  cgd 			break;
    152  1.17  cgd 		for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
    153  1.17  cgd 			error = donice(curp, p, uap->prio);
    154  1.17  cgd 			found++;
    155  1.17  cgd 		}
    156  1.17  cgd 		break;
    157  1.17  cgd 	}
    158  1.17  cgd 
    159  1.17  cgd 	case PRIO_USER:
    160  1.17  cgd 		if (uap->who == 0)
    161  1.17  cgd 			uap->who = curp->p_ucred->cr_uid;
    162  1.17  cgd 		for (p = (struct proc *)allproc; p != NULL; p = p->p_next)
    163  1.17  cgd 			if (p->p_ucred->cr_uid == uap->who) {
    164  1.17  cgd 				error = donice(curp, p, uap->prio);
    165  1.17  cgd 				found++;
    166  1.17  cgd 			}
    167  1.17  cgd 		break;
    168  1.17  cgd 
    169  1.17  cgd 	default:
    170  1.17  cgd 		return (EINVAL);
    171  1.17  cgd 	}
    172  1.17  cgd 	if (found == 0)
    173  1.17  cgd 		return (ESRCH);
    174  1.17  cgd 	return (error);
    175  1.17  cgd }
    176  1.17  cgd 
    177  1.17  cgd int
    178  1.17  cgd donice(curp, chgp, n)
    179  1.17  cgd 	register struct proc *curp, *chgp;
    180  1.17  cgd 	register int n;
    181  1.17  cgd {
    182  1.17  cgd 	register struct pcred *pcred = curp->p_cred;
    183  1.17  cgd 
    184  1.17  cgd 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
    185  1.17  cgd 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
    186  1.17  cgd 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
    187  1.17  cgd 		return (EPERM);
    188  1.17  cgd 	if (n > PRIO_MAX)
    189  1.17  cgd 		n = PRIO_MAX;
    190  1.17  cgd 	if (n < PRIO_MIN)
    191  1.17  cgd 		n = PRIO_MIN;
    192  1.17  cgd 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
    193  1.17  cgd 		return (EACCES);
    194  1.17  cgd 	chgp->p_nice = n;
    195  1.17  cgd 	(void) resetpriority(chgp);
    196  1.17  cgd 	return (0);
    197  1.17  cgd }
    198  1.17  cgd 
    199  1.17  cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    200  1.17  cgd struct osetrlimit_args {
    201  1.17  cgd 	u_int	which;
    202  1.17  cgd 	struct	orlimit *lim;
    203  1.17  cgd };
    204  1.17  cgd /* ARGSUSED */
    205  1.17  cgd int
    206  1.17  cgd osetrlimit(p, uap, retval)
    207  1.17  cgd 	struct proc *p;
    208  1.17  cgd 	struct osetrlimit_args *uap;
    209  1.17  cgd 	int *retval;
    210  1.17  cgd {
    211  1.17  cgd 	struct orlimit olim;
    212  1.17  cgd 	struct rlimit lim;
    213  1.17  cgd 	int error;
    214  1.17  cgd 
    215  1.17  cgd 	if (error = copyin((caddr_t)uap->lim, (caddr_t)&olim, sizeof(olim)))
    216  1.17  cgd 		return (error);
    217  1.17  cgd 	lim.rlim_cur = olim.rlim_cur;
    218  1.17  cgd 	lim.rlim_max = olim.rlim_max;
    219  1.17  cgd 	return (dosetrlimit(p, uap->which, &lim));
    220  1.17  cgd }
    221  1.17  cgd 
    222  1.17  cgd struct ogetrlimit_args {
    223  1.17  cgd 	u_int	which;
    224  1.17  cgd 	struct	orlimit *rlp;
    225  1.17  cgd };
    226  1.17  cgd /* ARGSUSED */
    227  1.17  cgd int
    228  1.17  cgd ogetrlimit(p, uap, retval)
    229  1.17  cgd 	struct proc *p;
    230  1.17  cgd 	register struct ogetrlimit_args *uap;
    231  1.17  cgd 	int *retval;
    232  1.17  cgd {
    233  1.17  cgd 	struct orlimit olim;
    234  1.17  cgd 
    235  1.17  cgd 	if (uap->which >= RLIM_NLIMITS)
    236  1.17  cgd 		return (EINVAL);
    237  1.17  cgd 	olim.rlim_cur =  p->p_rlimit[uap->which].rlim_cur;
    238  1.17  cgd 	if (olim.rlim_cur == -1)
    239  1.17  cgd 		olim.rlim_cur = 0x7fffffff;
    240  1.17  cgd 	olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
    241  1.17  cgd 	if (olim.rlim_max == -1)
    242  1.17  cgd 		olim.rlim_max = 0x7fffffff;
    243  1.17  cgd 	return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
    244  1.17  cgd }
    245  1.17  cgd #endif
    246  1.17  cgd 
    247  1.17  cgd struct setrlimit_args {
    248  1.17  cgd 	u_int	which;
    249  1.17  cgd 	struct	rlimit *lim;
    250  1.17  cgd };
    251  1.17  cgd 
    252  1.17  cgd /* ARGSUSED */
    253  1.17  cgd int
    254  1.17  cgd setrlimit(p, uap, retval)
    255  1.17  cgd 	struct proc *p;
    256  1.17  cgd 	register struct setrlimit_args *uap;
    257  1.17  cgd 	int *retval;
    258  1.17  cgd {
    259  1.17  cgd 	struct rlimit lim;
    260  1.17  cgd 	int error;
    261  1.17  cgd 
    262  1.17  cgd 	if (error = copyin((caddr_t)uap->lim, (caddr_t)&lim, sizeof(lim)))
    263  1.17  cgd 		return (error);
    264  1.17  cgd 	return (dosetrlimit(p, uap->which, &lim));
    265  1.17  cgd }
    266  1.17  cgd 
    267  1.17  cgd int
    268  1.17  cgd dosetrlimit(p, which, limp)
    269  1.17  cgd 	struct proc *p;
    270  1.17  cgd 	u_int which;
    271  1.17  cgd 	struct rlimit *limp;
    272  1.17  cgd {
    273  1.17  cgd 	register struct rlimit *alimp;
    274  1.17  cgd 	extern int maxfdescs;
    275  1.17  cgd 	int error;
    276  1.17  cgd 
    277  1.17  cgd 	if (which >= RLIM_NLIMITS)
    278  1.17  cgd 		return (EINVAL);
    279  1.17  cgd 	alimp = &p->p_rlimit[which];
    280  1.17  cgd 	if (limp->rlim_cur > alimp->rlim_max ||
    281  1.17  cgd 	    limp->rlim_max > alimp->rlim_max)
    282  1.17  cgd 		if (error = suser(p->p_ucred, &p->p_acflag))
    283  1.17  cgd 			return (error);
    284  1.17  cgd 	if (limp->rlim_cur > limp->rlim_max)
    285  1.17  cgd 		limp->rlim_cur = limp->rlim_max;
    286  1.17  cgd 	if (p->p_limit->p_refcnt > 1 &&
    287  1.17  cgd 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    288  1.17  cgd 		p->p_limit->p_refcnt--;
    289  1.17  cgd 		p->p_limit = limcopy(p->p_limit);
    290  1.17  cgd 		alimp = &p->p_rlimit[which];
    291  1.17  cgd 	}
    292  1.17  cgd 
    293  1.17  cgd 	switch (which) {
    294  1.17  cgd 
    295  1.17  cgd 	case RLIMIT_DATA:
    296  1.17  cgd 		if (limp->rlim_cur > MAXDSIZ)
    297  1.17  cgd 			limp->rlim_cur = MAXDSIZ;
    298  1.17  cgd 		if (limp->rlim_max > MAXDSIZ)
    299  1.17  cgd 			limp->rlim_max = MAXDSIZ;
    300  1.17  cgd 		break;
    301  1.17  cgd 
    302  1.17  cgd 	case RLIMIT_NOFILE:
    303  1.17  cgd 		if (limp->rlim_cur > maxfdescs)
    304  1.17  cgd 			limp->rlim_cur = maxfdescs;
    305  1.17  cgd 		if (limp->rlim_max > maxfdescs)
    306  1.17  cgd 			limp->rlim_max = maxfdescs;
    307  1.17  cgd 		break;
    308  1.17  cgd 
    309  1.17  cgd 	case RLIMIT_STACK:
    310  1.17  cgd 		if (limp->rlim_cur > MAXSSIZ)
    311  1.17  cgd 			limp->rlim_cur = MAXSSIZ;
    312  1.17  cgd 		if (limp->rlim_max > MAXSSIZ)
    313  1.17  cgd 			limp->rlim_max = MAXSSIZ;
    314  1.17  cgd 		/*
    315  1.17  cgd 		 * Stack is allocated to the max at exec time with only
    316  1.17  cgd 		 * "rlim_cur" bytes accessible.  If stack limit is going
    317  1.17  cgd 		 * up make more accessible, if going down make inaccessible.
    318  1.17  cgd 		 */
    319  1.17  cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    320  1.17  cgd 			vm_offset_t addr;
    321  1.17  cgd 			vm_size_t size;
    322  1.17  cgd 			vm_prot_t prot;
    323  1.17  cgd 
    324  1.17  cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    325  1.17  cgd 				prot = VM_PROT_ALL;
    326  1.17  cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    327  1.17  cgd 				addr = USRSTACK - limp->rlim_cur;
    328  1.17  cgd 			} else {
    329  1.17  cgd 				prot = VM_PROT_NONE;
    330  1.17  cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    331  1.17  cgd 				addr = USRSTACK - alimp->rlim_cur;
    332  1.17  cgd 			}
    333  1.17  cgd 			addr = trunc_page(addr);
    334  1.17  cgd 			size = round_page(size);
    335  1.17  cgd 			(void) vm_map_protect(&p->p_vmspace->vm_map,
    336  1.17  cgd 					      addr, addr+size, prot, FALSE);
    337  1.17  cgd 		}
    338  1.17  cgd 		break;
    339  1.17  cgd 	}
    340  1.17  cgd 	*alimp = *limp;
    341  1.17  cgd 	return (0);
    342  1.17  cgd }
    343  1.17  cgd 
    344  1.17  cgd struct getrlimit_args {
    345  1.17  cgd 	u_int	which;
    346  1.17  cgd 	struct	rlimit *rlp;
    347  1.17  cgd };
    348  1.17  cgd 
    349  1.17  cgd /* ARGSUSED */
    350  1.17  cgd int
    351  1.17  cgd getrlimit(p, uap, retval)
    352  1.17  cgd 	struct proc *p;
    353  1.17  cgd 	register struct getrlimit_args *uap;
    354  1.17  cgd 	int *retval;
    355  1.17  cgd {
    356  1.17  cgd 
    357  1.17  cgd 	if (uap->which >= RLIM_NLIMITS)
    358  1.17  cgd 		return (EINVAL);
    359  1.17  cgd 	return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
    360  1.17  cgd 	    sizeof (struct rlimit)));
    361  1.17  cgd }
    362  1.17  cgd 
    363  1.17  cgd /*
    364  1.17  cgd  * Transform the running time and tick information in proc p into user,
    365  1.17  cgd  * system, and interrupt time usage.
    366  1.17  cgd  */
    367  1.17  cgd calcru(p, up, sp, ip)
    368  1.17  cgd 	register struct proc *p;
    369  1.17  cgd 	register struct timeval *up;
    370  1.17  cgd 	register struct timeval *sp;
    371  1.17  cgd 	register struct timeval *ip;
    372  1.17  cgd {
    373  1.17  cgd 	register u_quad_t u, st, ut, it, tot;
    374  1.17  cgd 	register u_long sec, usec;
    375  1.17  cgd 	register int s;
    376  1.17  cgd 	struct timeval tv;
    377  1.17  cgd 
    378  1.17  cgd 	s = splstatclock();
    379  1.17  cgd 	st = p->p_sticks;
    380  1.17  cgd 	ut = p->p_uticks;
    381  1.17  cgd 	it = p->p_iticks;
    382  1.17  cgd 	splx(s);
    383  1.17  cgd 
    384  1.17  cgd 	tot = st + ut + it;
    385  1.17  cgd 	if (tot == 0) {
    386  1.17  cgd 		up->tv_sec = up->tv_usec = 0;
    387  1.17  cgd 		sp->tv_sec = sp->tv_usec = 0;
    388  1.17  cgd 		if (ip != NULL)
    389  1.17  cgd 			ip->tv_sec = ip->tv_usec = 0;
    390  1.17  cgd 		return;
    391  1.17  cgd 	}
    392  1.17  cgd 
    393  1.17  cgd 	sec = p->p_rtime.tv_sec;
    394  1.17  cgd 	usec = p->p_rtime.tv_usec;
    395  1.17  cgd 	if (p == curproc) {
    396  1.17  cgd 		/*
    397  1.17  cgd 		 * Adjust for the current time slice.  This is actually fairly
    398  1.17  cgd 		 * important since the error here is on the order of a time
    399  1.17  cgd 		 * quantum, which is much greater than the sampling error.
    400  1.17  cgd 		 */
    401  1.17  cgd 		microtime(&tv);
    402  1.17  cgd 		sec += tv.tv_sec - runtime.tv_sec;
    403  1.17  cgd 		usec += tv.tv_usec - runtime.tv_usec;
    404  1.17  cgd 	}
    405  1.17  cgd 	u = sec * 1000000 + usec;
    406  1.17  cgd 	st = (u * st) / tot;
    407  1.17  cgd 	sp->tv_sec = st / 1000000;
    408  1.17  cgd 	sp->tv_usec = st % 1000000;
    409  1.17  cgd 	ut = (u * ut) / tot;
    410  1.17  cgd 	up->tv_sec = ut / 1000000;
    411  1.17  cgd 	up->tv_usec = ut % 1000000;
    412  1.17  cgd 	if (ip != NULL) {
    413  1.17  cgd 		it = (u * it) / tot;
    414  1.17  cgd 		ip->tv_sec = it / 1000000;
    415  1.17  cgd 		ip->tv_usec = it % 1000000;
    416  1.17  cgd 	}
    417  1.17  cgd }
    418  1.17  cgd 
    419  1.17  cgd struct getrusage_args {
    420  1.17  cgd 	int	who;
    421  1.17  cgd 	struct	rusage *rusage;
    422  1.17  cgd };
    423  1.17  cgd /* ARGSUSED */
    424  1.17  cgd int
    425  1.17  cgd getrusage(p, uap, retval)
    426  1.17  cgd 	register struct proc *p;
    427  1.17  cgd 	register struct getrusage_args *uap;
    428  1.17  cgd 	int *retval;
    429  1.17  cgd {
    430  1.17  cgd 	register struct rusage *rup;
    431  1.17  cgd 
    432  1.17  cgd 	switch (uap->who) {
    433  1.17  cgd 
    434  1.17  cgd 	case RUSAGE_SELF: {
    435  1.17  cgd 		rup = &p->p_stats->p_ru;
    436  1.17  cgd 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    437  1.17  cgd 		break;
    438  1.17  cgd 	}
    439  1.17  cgd 
    440  1.17  cgd 	case RUSAGE_CHILDREN:
    441  1.17  cgd 		rup = &p->p_stats->p_cru;
    442  1.17  cgd 		break;
    443  1.17  cgd 
    444  1.17  cgd 	default:
    445  1.17  cgd 		return (EINVAL);
    446  1.17  cgd 	}
    447  1.17  cgd 	return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
    448  1.17  cgd 	    sizeof (struct rusage)));
    449  1.17  cgd }
    450  1.17  cgd 
    451  1.17  cgd void
    452  1.17  cgd ruadd(ru, ru2)
    453  1.17  cgd 	register struct rusage *ru, *ru2;
    454  1.17  cgd {
    455  1.17  cgd 	register long *ip, *ip2;
    456  1.17  cgd 	register int i;
    457  1.17  cgd 
    458  1.17  cgd 	timevaladd(&ru->ru_utime, &ru2->ru_utime);
    459  1.17  cgd 	timevaladd(&ru->ru_stime, &ru2->ru_stime);
    460  1.17  cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    461  1.17  cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    462  1.17  cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    463  1.17  cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    464  1.17  cgd 		*ip++ += *ip2++;
    465  1.17  cgd }
    466  1.17  cgd 
    467  1.17  cgd /*
    468  1.17  cgd  * Make a copy of the plimit structure.
    469  1.17  cgd  * We share these structures copy-on-write after fork,
    470  1.17  cgd  * and copy when a limit is changed.
    471  1.17  cgd  */
    472  1.17  cgd struct plimit *
    473  1.17  cgd limcopy(lim)
    474  1.17  cgd 	struct plimit *lim;
    475  1.17  cgd {
    476  1.17  cgd 	register struct plimit *copy;
    477  1.17  cgd 
    478  1.17  cgd 	MALLOC(copy, struct plimit *, sizeof(struct plimit),
    479  1.17  cgd 	    M_SUBPROC, M_WAITOK);
    480  1.17  cgd 	bcopy(lim->pl_rlimit, copy->pl_rlimit,
    481  1.17  cgd 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    482  1.17  cgd 	copy->p_lflags = 0;
    483  1.17  cgd 	copy->p_refcnt = 1;
    484  1.17  cgd 	return (copy);
    485  1.17  cgd }
    486