Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.59
      1  1.59   thorpej /*	$NetBSD: kern_resource.c,v 1.59 2000/08/20 21:50:11 thorpej 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.17       cgd 
     55  1.43       mrg #include <uvm/uvm_extern.h>
     56  1.43       mrg 
     57  1.17       cgd /*
     58  1.17       cgd  * Resource controls and accounting.
     59  1.17       cgd  */
     60  1.17       cgd 
     61  1.25       cgd int
     62  1.31   mycroft sys_getpriority(curp, v, retval)
     63  1.17       cgd 	struct proc *curp;
     64  1.30   thorpej 	void *v;
     65  1.30   thorpej 	register_t *retval;
     66  1.30   thorpej {
     67  1.54  augustss 	struct sys_getpriority_args /* {
     68  1.22       cgd 		syscallarg(int) which;
     69  1.22       cgd 		syscallarg(int) who;
     70  1.30   thorpej 	} */ *uap = v;
     71  1.54  augustss 	struct proc *p;
     72  1.54  augustss 	int low = NZERO + PRIO_MAX + 1;
     73  1.17       cgd 
     74  1.22       cgd 	switch (SCARG(uap, which)) {
     75  1.17       cgd 
     76  1.17       cgd 	case PRIO_PROCESS:
     77  1.22       cgd 		if (SCARG(uap, who) == 0)
     78  1.17       cgd 			p = curp;
     79  1.17       cgd 		else
     80  1.22       cgd 			p = pfind(SCARG(uap, who));
     81  1.17       cgd 		if (p == 0)
     82  1.17       cgd 			break;
     83  1.17       cgd 		low = p->p_nice;
     84  1.17       cgd 		break;
     85  1.17       cgd 
     86  1.17       cgd 	case PRIO_PGRP: {
     87  1.54  augustss 		struct pgrp *pg;
     88  1.17       cgd 
     89  1.22       cgd 		if (SCARG(uap, who) == 0)
     90  1.17       cgd 			pg = curp->p_pgrp;
     91  1.22       cgd 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
     92  1.17       cgd 			break;
     93  1.45      fvdl 		for (p = pg->pg_members.lh_first; p != 0;
     94  1.45      fvdl 		     p = p->p_pglist.le_next) {
     95  1.17       cgd 			if (p->p_nice < low)
     96  1.17       cgd 				low = p->p_nice;
     97  1.17       cgd 		}
     98  1.17       cgd 		break;
     99  1.17       cgd 	}
    100  1.17       cgd 
    101  1.17       cgd 	case PRIO_USER:
    102  1.22       cgd 		if (SCARG(uap, who) == 0)
    103  1.22       cgd 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    104  1.52   thorpej 		proclist_lock_read();
    105  1.21   mycroft 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    106  1.22       cgd 			if (p->p_ucred->cr_uid == SCARG(uap, who) &&
    107  1.22       cgd 			    p->p_nice < low)
    108  1.17       cgd 				low = p->p_nice;
    109  1.51   thorpej 		proclist_unlock_read();
    110  1.17       cgd 		break;
    111  1.17       cgd 
    112  1.17       cgd 	default:
    113  1.17       cgd 		return (EINVAL);
    114  1.17       cgd 	}
    115  1.37        ws 	if (low == NZERO + PRIO_MAX + 1)
    116  1.17       cgd 		return (ESRCH);
    117  1.37        ws 	*retval = low - NZERO;
    118  1.17       cgd 	return (0);
    119  1.17       cgd }
    120  1.17       cgd 
    121  1.17       cgd /* ARGSUSED */
    122  1.25       cgd int
    123  1.31   mycroft sys_setpriority(curp, v, retval)
    124  1.17       cgd 	struct proc *curp;
    125  1.30   thorpej 	void *v;
    126  1.30   thorpej 	register_t *retval;
    127  1.30   thorpej {
    128  1.54  augustss 	struct sys_setpriority_args /* {
    129  1.22       cgd 		syscallarg(int) which;
    130  1.22       cgd 		syscallarg(int) who;
    131  1.22       cgd 		syscallarg(int) prio;
    132  1.30   thorpej 	} */ *uap = v;
    133  1.54  augustss 	struct proc *p;
    134  1.17       cgd 	int found = 0, error = 0;
    135  1.17       cgd 
    136  1.22       cgd 	switch (SCARG(uap, which)) {
    137  1.17       cgd 
    138  1.17       cgd 	case PRIO_PROCESS:
    139  1.22       cgd 		if (SCARG(uap, who) == 0)
    140  1.17       cgd 			p = curp;
    141  1.17       cgd 		else
    142  1.22       cgd 			p = pfind(SCARG(uap, who));
    143  1.17       cgd 		if (p == 0)
    144  1.17       cgd 			break;
    145  1.22       cgd 		error = donice(curp, p, SCARG(uap, prio));
    146  1.17       cgd 		found++;
    147  1.17       cgd 		break;
    148  1.17       cgd 
    149  1.17       cgd 	case PRIO_PGRP: {
    150  1.54  augustss 		struct pgrp *pg;
    151  1.17       cgd 
    152  1.22       cgd 		if (SCARG(uap, who) == 0)
    153  1.17       cgd 			pg = curp->p_pgrp;
    154  1.22       cgd 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    155  1.17       cgd 			break;
    156  1.22       cgd 		for (p = pg->pg_members.lh_first; p != 0;
    157  1.22       cgd 		    p = p->p_pglist.le_next) {
    158  1.22       cgd 			error = donice(curp, p, SCARG(uap, prio));
    159  1.17       cgd 			found++;
    160  1.17       cgd 		}
    161  1.17       cgd 		break;
    162  1.17       cgd 	}
    163  1.17       cgd 
    164  1.17       cgd 	case PRIO_USER:
    165  1.22       cgd 		if (SCARG(uap, who) == 0)
    166  1.22       cgd 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    167  1.52   thorpej 		proclist_lock_read();
    168  1.21   mycroft 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    169  1.22       cgd 			if (p->p_ucred->cr_uid == SCARG(uap, who)) {
    170  1.22       cgd 				error = donice(curp, p, SCARG(uap, prio));
    171  1.17       cgd 				found++;
    172  1.17       cgd 			}
    173  1.51   thorpej 		proclist_unlock_read();
    174  1.17       cgd 		break;
    175  1.17       cgd 
    176  1.17       cgd 	default:
    177  1.17       cgd 		return (EINVAL);
    178  1.17       cgd 	}
    179  1.17       cgd 	if (found == 0)
    180  1.17       cgd 		return (ESRCH);
    181  1.17       cgd 	return (error);
    182  1.17       cgd }
    183  1.17       cgd 
    184  1.25       cgd int
    185  1.17       cgd donice(curp, chgp, n)
    186  1.54  augustss 	struct proc *curp, *chgp;
    187  1.54  augustss 	int n;
    188  1.17       cgd {
    189  1.54  augustss 	struct pcred *pcred = curp->p_cred;
    190  1.59   thorpej 	int s;
    191  1.17       cgd 
    192  1.17       cgd 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
    193  1.17       cgd 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
    194  1.17       cgd 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
    195  1.17       cgd 		return (EPERM);
    196  1.17       cgd 	if (n > PRIO_MAX)
    197  1.17       cgd 		n = PRIO_MAX;
    198  1.17       cgd 	if (n < PRIO_MIN)
    199  1.17       cgd 		n = PRIO_MIN;
    200  1.37        ws 	n += NZERO;
    201  1.17       cgd 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
    202  1.17       cgd 		return (EACCES);
    203  1.17       cgd 	chgp->p_nice = n;
    204  1.59   thorpej 	SCHED_LOCK(s);
    205  1.19       cgd 	(void)resetpriority(chgp);
    206  1.59   thorpej 	SCHED_UNLOCK(s);
    207  1.17       cgd 	return (0);
    208  1.17       cgd }
    209  1.17       cgd 
    210  1.17       cgd /* ARGSUSED */
    211  1.25       cgd int
    212  1.31   mycroft sys_setrlimit(p, v, retval)
    213  1.17       cgd 	struct proc *p;
    214  1.30   thorpej 	void *v;
    215  1.30   thorpej 	register_t *retval;
    216  1.30   thorpej {
    217  1.54  augustss 	struct sys_setrlimit_args /* {
    218  1.42   mycroft 		syscallarg(int) which;
    219  1.39       cgd 		syscallarg(const struct rlimit *) rlp;
    220  1.30   thorpej 	} */ *uap = v;
    221  1.42   mycroft 	int which = SCARG(uap, which);
    222  1.19       cgd 	struct rlimit alim;
    223  1.17       cgd 	int error;
    224  1.17       cgd 
    225  1.46     perry 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    226  1.33  christos 	if (error)
    227  1.17       cgd 		return (error);
    228  1.53    bouyer 	return (dosetrlimit(p, p->p_cred, which, &alim));
    229  1.17       cgd }
    230  1.17       cgd 
    231  1.17       cgd int
    232  1.53    bouyer dosetrlimit(p, cred, which, limp)
    233  1.17       cgd 	struct proc *p;
    234  1.53    bouyer 	struct  pcred *cred;
    235  1.42   mycroft 	int which;
    236  1.17       cgd 	struct rlimit *limp;
    237  1.17       cgd {
    238  1.54  augustss 	struct rlimit *alimp;
    239  1.19       cgd 	extern unsigned maxdmap, maxsmap;
    240  1.53    bouyer 	struct plimit *newplim;
    241  1.17       cgd 	int error;
    242  1.17       cgd 
    243  1.42   mycroft 	if ((u_int)which >= RLIM_NLIMITS)
    244  1.17       cgd 		return (EINVAL);
    245  1.38  matthias 
    246  1.38  matthias 	if (limp->rlim_cur < 0 || limp->rlim_max < 0)
    247  1.38  matthias 		return (EINVAL);
    248  1.38  matthias 
    249  1.17       cgd 	alimp = &p->p_rlimit[which];
    250  1.53    bouyer 	/* if we don't change the value, no need to limcopy() */
    251  1.53    bouyer 	if (limp->rlim_cur == alimp->rlim_cur &&
    252  1.53    bouyer 	    limp->rlim_max == alimp->rlim_max)
    253  1.53    bouyer 		return 0;
    254  1.53    bouyer 
    255  1.19       cgd 	if (limp->rlim_cur > alimp->rlim_max ||
    256  1.17       cgd 	    limp->rlim_max > alimp->rlim_max)
    257  1.53    bouyer 		if ((error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
    258  1.17       cgd 			return (error);
    259  1.17       cgd 	if (limp->rlim_cur > limp->rlim_max)
    260  1.17       cgd 		limp->rlim_cur = limp->rlim_max;
    261  1.17       cgd 	if (p->p_limit->p_refcnt > 1 &&
    262  1.17       cgd 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    263  1.53    bouyer 		newplim = limcopy(p->p_limit);
    264  1.53    bouyer 		limfree(p->p_limit);
    265  1.53    bouyer 		p->p_limit = newplim;
    266  1.17       cgd 		alimp = &p->p_rlimit[which];
    267  1.17       cgd 	}
    268  1.17       cgd 
    269  1.17       cgd 	switch (which) {
    270  1.17       cgd 
    271  1.17       cgd 	case RLIMIT_DATA:
    272  1.19       cgd 		if (limp->rlim_cur > maxdmap)
    273  1.19       cgd 			limp->rlim_cur = maxdmap;
    274  1.19       cgd 		if (limp->rlim_max > maxdmap)
    275  1.19       cgd 			limp->rlim_max = maxdmap;
    276  1.17       cgd 		break;
    277  1.17       cgd 
    278  1.17       cgd 	case RLIMIT_STACK:
    279  1.19       cgd 		if (limp->rlim_cur > maxsmap)
    280  1.19       cgd 			limp->rlim_cur = maxsmap;
    281  1.19       cgd 		if (limp->rlim_max > maxsmap)
    282  1.19       cgd 			limp->rlim_max = maxsmap;
    283  1.40     enami 
    284  1.17       cgd 		/*
    285  1.40     enami 		 * Stack is allocated to the max at exec time with
    286  1.40     enami 		 * only "rlim_cur" bytes accessible (In other words,
    287  1.40     enami 		 * allocates stack dividing two contiguous regions at
    288  1.40     enami 		 * "rlim_cur" bytes boundary).
    289  1.40     enami 		 *
    290  1.40     enami 		 * Since allocation is done in terms of page, roundup
    291  1.40     enami 		 * "rlim_cur" (otherwise, contiguous regions
    292  1.40     enami 		 * overlap).  If stack limit is going up make more
    293  1.40     enami 		 * accessible, if going down make inaccessible.
    294  1.17       cgd 		 */
    295  1.40     enami 		limp->rlim_cur = round_page(limp->rlim_cur);
    296  1.17       cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    297  1.48       eeh 			vaddr_t addr;
    298  1.48       eeh 			vsize_t size;
    299  1.17       cgd 			vm_prot_t prot;
    300  1.17       cgd 
    301  1.17       cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    302  1.17       cgd 				prot = VM_PROT_ALL;
    303  1.17       cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    304  1.17       cgd 				addr = USRSTACK - limp->rlim_cur;
    305  1.17       cgd 			} else {
    306  1.17       cgd 				prot = VM_PROT_NONE;
    307  1.17       cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    308  1.17       cgd 				addr = USRSTACK - alimp->rlim_cur;
    309  1.17       cgd 			}
    310  1.43       mrg 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    311  1.43       mrg 					      addr, addr+size, prot, FALSE);
    312  1.17       cgd 		}
    313  1.17       cgd 		break;
    314  1.19       cgd 
    315  1.19       cgd 	case RLIMIT_NOFILE:
    316  1.19       cgd 		if (limp->rlim_cur > maxfiles)
    317  1.19       cgd 			limp->rlim_cur = maxfiles;
    318  1.19       cgd 		if (limp->rlim_max > maxfiles)
    319  1.19       cgd 			limp->rlim_max = maxfiles;
    320  1.19       cgd 		break;
    321  1.19       cgd 
    322  1.19       cgd 	case RLIMIT_NPROC:
    323  1.19       cgd 		if (limp->rlim_cur > maxproc)
    324  1.19       cgd 			limp->rlim_cur = maxproc;
    325  1.19       cgd 		if (limp->rlim_max > maxproc)
    326  1.19       cgd 			limp->rlim_max = maxproc;
    327  1.19       cgd 		break;
    328  1.17       cgd 	}
    329  1.17       cgd 	*alimp = *limp;
    330  1.17       cgd 	return (0);
    331  1.17       cgd }
    332  1.17       cgd 
    333  1.17       cgd /* ARGSUSED */
    334  1.25       cgd int
    335  1.31   mycroft sys_getrlimit(p, v, retval)
    336  1.17       cgd 	struct proc *p;
    337  1.30   thorpej 	void *v;
    338  1.30   thorpej 	register_t *retval;
    339  1.30   thorpej {
    340  1.54  augustss 	struct sys_getrlimit_args /* {
    341  1.42   mycroft 		syscallarg(int) which;
    342  1.22       cgd 		syscallarg(struct rlimit *) rlp;
    343  1.30   thorpej 	} */ *uap = v;
    344  1.42   mycroft 	int which = SCARG(uap, which);
    345  1.17       cgd 
    346  1.42   mycroft 	if ((u_int)which >= RLIM_NLIMITS)
    347  1.17       cgd 		return (EINVAL);
    348  1.42   mycroft 	return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
    349  1.46     perry 	    sizeof(struct rlimit)));
    350  1.17       cgd }
    351  1.17       cgd 
    352  1.17       cgd /*
    353  1.17       cgd  * Transform the running time and tick information in proc p into user,
    354  1.17       cgd  * system, and interrupt time usage.
    355  1.17       cgd  */
    356  1.25       cgd void
    357  1.17       cgd calcru(p, up, sp, ip)
    358  1.54  augustss 	struct proc *p;
    359  1.54  augustss 	struct timeval *up;
    360  1.54  augustss 	struct timeval *sp;
    361  1.54  augustss 	struct timeval *ip;
    362  1.17       cgd {
    363  1.54  augustss 	u_quad_t u, st, ut, it, tot;
    364  1.54  augustss 	long sec, usec;
    365  1.54  augustss 	int s;
    366  1.17       cgd 	struct timeval tv;
    367  1.17       cgd 
    368  1.17       cgd 	s = splstatclock();
    369  1.17       cgd 	st = p->p_sticks;
    370  1.17       cgd 	ut = p->p_uticks;
    371  1.17       cgd 	it = p->p_iticks;
    372  1.17       cgd 	splx(s);
    373  1.17       cgd 
    374  1.17       cgd 	tot = st + ut + it;
    375  1.17       cgd 	if (tot == 0) {
    376  1.17       cgd 		up->tv_sec = up->tv_usec = 0;
    377  1.17       cgd 		sp->tv_sec = sp->tv_usec = 0;
    378  1.17       cgd 		if (ip != NULL)
    379  1.17       cgd 			ip->tv_sec = ip->tv_usec = 0;
    380  1.17       cgd 		return;
    381  1.17       cgd 	}
    382  1.17       cgd 
    383  1.17       cgd 	sec = p->p_rtime.tv_sec;
    384  1.17       cgd 	usec = p->p_rtime.tv_usec;
    385  1.55   thorpej 	if (p->p_stat == SONPROC) {
    386  1.57   thorpej 		struct schedstate_percpu *spc;
    387  1.57   thorpej 
    388  1.57   thorpej 		KDASSERT(p->p_cpu != NULL);
    389  1.57   thorpej 		spc = &p->p_cpu->ci_schedstate;
    390  1.56   thorpej 
    391  1.56   thorpej 		/*
    392  1.17       cgd 		 * Adjust for the current time slice.  This is actually fairly
    393  1.17       cgd 		 * important since the error here is on the order of a time
    394  1.17       cgd 		 * quantum, which is much greater than the sampling error.
    395  1.17       cgd 		 */
    396  1.17       cgd 		microtime(&tv);
    397  1.56   thorpej 		sec += tv.tv_sec - spc->spc_runtime.tv_sec;
    398  1.56   thorpej 		usec += tv.tv_usec - spc->spc_runtime.tv_usec;
    399  1.17       cgd 	}
    400  1.35       jtc 	u = (u_quad_t) sec * 1000000 + usec;
    401  1.17       cgd 	st = (u * st) / tot;
    402  1.17       cgd 	sp->tv_sec = st / 1000000;
    403  1.17       cgd 	sp->tv_usec = st % 1000000;
    404  1.17       cgd 	ut = (u * ut) / tot;
    405  1.17       cgd 	up->tv_sec = ut / 1000000;
    406  1.17       cgd 	up->tv_usec = ut % 1000000;
    407  1.17       cgd 	if (ip != NULL) {
    408  1.17       cgd 		it = (u * it) / tot;
    409  1.17       cgd 		ip->tv_sec = it / 1000000;
    410  1.17       cgd 		ip->tv_usec = it % 1000000;
    411  1.17       cgd 	}
    412  1.17       cgd }
    413  1.17       cgd 
    414  1.17       cgd /* ARGSUSED */
    415  1.25       cgd int
    416  1.31   mycroft sys_getrusage(p, v, retval)
    417  1.54  augustss 	struct proc *p;
    418  1.30   thorpej 	void *v;
    419  1.30   thorpej 	register_t *retval;
    420  1.30   thorpej {
    421  1.54  augustss 	struct sys_getrusage_args /* {
    422  1.22       cgd 		syscallarg(int) who;
    423  1.22       cgd 		syscallarg(struct rusage *) rusage;
    424  1.30   thorpej 	} */ *uap = v;
    425  1.54  augustss 	struct rusage *rup;
    426  1.17       cgd 
    427  1.22       cgd 	switch (SCARG(uap, who)) {
    428  1.17       cgd 
    429  1.19       cgd 	case RUSAGE_SELF:
    430  1.17       cgd 		rup = &p->p_stats->p_ru;
    431  1.17       cgd 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    432  1.17       cgd 		break;
    433  1.17       cgd 
    434  1.17       cgd 	case RUSAGE_CHILDREN:
    435  1.17       cgd 		rup = &p->p_stats->p_cru;
    436  1.17       cgd 		break;
    437  1.17       cgd 
    438  1.17       cgd 	default:
    439  1.17       cgd 		return (EINVAL);
    440  1.17       cgd 	}
    441  1.46     perry 	return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
    442  1.17       cgd }
    443  1.17       cgd 
    444  1.25       cgd void
    445  1.17       cgd ruadd(ru, ru2)
    446  1.54  augustss 	struct rusage *ru, *ru2;
    447  1.17       cgd {
    448  1.54  augustss 	long *ip, *ip2;
    449  1.54  augustss 	int i;
    450  1.17       cgd 
    451  1.27   mycroft 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    452  1.27   mycroft 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    453  1.17       cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    454  1.17       cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    455  1.17       cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    456  1.17       cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    457  1.17       cgd 		*ip++ += *ip2++;
    458  1.17       cgd }
    459  1.17       cgd 
    460  1.17       cgd /*
    461  1.17       cgd  * Make a copy of the plimit structure.
    462  1.17       cgd  * We share these structures copy-on-write after fork,
    463  1.17       cgd  * and copy when a limit is changed.
    464  1.17       cgd  */
    465  1.17       cgd struct plimit *
    466  1.17       cgd limcopy(lim)
    467  1.17       cgd 	struct plimit *lim;
    468  1.17       cgd {
    469  1.54  augustss 	struct plimit *newlim;
    470  1.17       cgd 
    471  1.49   thorpej 	newlim = pool_get(&plimit_pool, PR_WAITOK);
    472  1.47     perry 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    473  1.17       cgd 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    474  1.53    bouyer 	if (lim->pl_corename == defcorename) {
    475  1.53    bouyer 		newlim->pl_corename = defcorename;
    476  1.53    bouyer 	} else {
    477  1.53    bouyer 		newlim->pl_corename = malloc(strlen(lim->pl_corename)+1,
    478  1.53    bouyer 		    M_TEMP, M_WAITOK);
    479  1.53    bouyer 		strcpy(newlim->pl_corename, lim->pl_corename);
    480  1.53    bouyer 	}
    481  1.32   mycroft 	newlim->p_lflags = 0;
    482  1.32   mycroft 	newlim->p_refcnt = 1;
    483  1.32   mycroft 	return (newlim);
    484  1.32   mycroft }
    485  1.32   mycroft 
    486  1.32   mycroft void
    487  1.32   mycroft limfree(lim)
    488  1.32   mycroft 	struct plimit *lim;
    489  1.32   mycroft {
    490  1.32   mycroft 
    491  1.32   mycroft 	if (--lim->p_refcnt > 0)
    492  1.32   mycroft 		return;
    493  1.53    bouyer #ifdef DIAGNOSTIC
    494  1.53    bouyer 	if (lim->p_refcnt < 0)
    495  1.53    bouyer 		panic("limfree");
    496  1.53    bouyer #endif
    497  1.53    bouyer 	if (lim->pl_corename != defcorename)
    498  1.53    bouyer 		free(lim->pl_corename, M_TEMP);
    499  1.49   thorpej 	pool_put(&plimit_pool, lim);
    500  1.17       cgd }
    501