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