Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.22
      1  1.22      cgd /*	$NetBSD: kern_resource.c,v 1.22 1994/10/20 04:22:54 cgd 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.20      cgd  *	@(#)kern_resource.c	8.5 (Berkeley) 1/21/94
     41  1.17      cgd  */
     42  1.17      cgd 
     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.17      cgd #include <sys/proc.h>
     50  1.17      cgd 
     51  1.22      cgd #include <sys/mount.h>
     52  1.22      cgd #include <sys/syscallargs.h>
     53  1.22      cgd 
     54  1.17      cgd #include <vm/vm.h>
     55  1.17      cgd 
     56  1.17      cgd /*
     57  1.17      cgd  * Resource controls and accounting.
     58  1.17      cgd  */
     59  1.17      cgd 
     60  1.17      cgd getpriority(curp, uap, retval)
     61  1.17      cgd 	struct proc *curp;
     62  1.22      cgd 	register struct getpriority_args /* {
     63  1.22      cgd 		syscallarg(int) which;
     64  1.22      cgd 		syscallarg(int) who;
     65  1.22      cgd 	} */ *uap;
     66  1.22      cgd 	register_t *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.22      cgd 	switch (SCARG(uap, which)) {
     72  1.17      cgd 
     73  1.17      cgd 	case PRIO_PROCESS:
     74  1.22      cgd 		if (SCARG(uap, who) == 0)
     75  1.17      cgd 			p = curp;
     76  1.17      cgd 		else
     77  1.22      cgd 			p = pfind(SCARG(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.22      cgd 		if (SCARG(uap, who) == 0)
     87  1.17      cgd 			pg = curp->p_pgrp;
     88  1.22      cgd 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
     89  1.17      cgd 			break;
     90  1.21  mycroft 		for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
     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.22      cgd 		if (SCARG(uap, who) == 0)
     99  1.22      cgd 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    100  1.21  mycroft 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    101  1.22      cgd 			if (p->p_ucred->cr_uid == SCARG(uap, who) &&
    102  1.22      cgd 			    p->p_nice < low)
    103  1.17      cgd 				low = p->p_nice;
    104  1.17      cgd 		break;
    105  1.17      cgd 
    106  1.17      cgd 	default:
    107  1.17      cgd 		return (EINVAL);
    108  1.17      cgd 	}
    109  1.17      cgd 	if (low == PRIO_MAX + 1)
    110  1.17      cgd 		return (ESRCH);
    111  1.17      cgd 	*retval = low;
    112  1.17      cgd 	return (0);
    113  1.17      cgd }
    114  1.17      cgd 
    115  1.17      cgd /* ARGSUSED */
    116  1.17      cgd setpriority(curp, uap, retval)
    117  1.17      cgd 	struct proc *curp;
    118  1.22      cgd 	register struct setpriority_args /* {
    119  1.22      cgd 		syscallarg(int) which;
    120  1.22      cgd 		syscallarg(int) who;
    121  1.22      cgd 		syscallarg(int) prio;
    122  1.22      cgd 	} */ *uap;
    123  1.22      cgd 	register_t *retval;
    124  1.17      cgd {
    125  1.17      cgd 	register struct proc *p;
    126  1.17      cgd 	int found = 0, error = 0;
    127  1.17      cgd 
    128  1.22      cgd 	switch (SCARG(uap, which)) {
    129  1.17      cgd 
    130  1.17      cgd 	case PRIO_PROCESS:
    131  1.22      cgd 		if (SCARG(uap, who) == 0)
    132  1.17      cgd 			p = curp;
    133  1.17      cgd 		else
    134  1.22      cgd 			p = pfind(SCARG(uap, who));
    135  1.17      cgd 		if (p == 0)
    136  1.17      cgd 			break;
    137  1.22      cgd 		error = donice(curp, p, SCARG(uap, prio));
    138  1.17      cgd 		found++;
    139  1.17      cgd 		break;
    140  1.17      cgd 
    141  1.17      cgd 	case PRIO_PGRP: {
    142  1.17      cgd 		register struct pgrp *pg;
    143  1.17      cgd 
    144  1.22      cgd 		if (SCARG(uap, who) == 0)
    145  1.17      cgd 			pg = curp->p_pgrp;
    146  1.22      cgd 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    147  1.17      cgd 			break;
    148  1.22      cgd 		for (p = pg->pg_members.lh_first; p != 0;
    149  1.22      cgd 		    p = p->p_pglist.le_next) {
    150  1.22      cgd 			error = donice(curp, p, SCARG(uap, prio));
    151  1.17      cgd 			found++;
    152  1.17      cgd 		}
    153  1.17      cgd 		break;
    154  1.17      cgd 	}
    155  1.17      cgd 
    156  1.17      cgd 	case PRIO_USER:
    157  1.22      cgd 		if (SCARG(uap, who) == 0)
    158  1.22      cgd 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    159  1.21  mycroft 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    160  1.22      cgd 			if (p->p_ucred->cr_uid == SCARG(uap, who)) {
    161  1.22      cgd 				error = donice(curp, p, SCARG(uap, prio));
    162  1.17      cgd 				found++;
    163  1.17      cgd 			}
    164  1.17      cgd 		break;
    165  1.17      cgd 
    166  1.17      cgd 	default:
    167  1.17      cgd 		return (EINVAL);
    168  1.17      cgd 	}
    169  1.17      cgd 	if (found == 0)
    170  1.17      cgd 		return (ESRCH);
    171  1.17      cgd 	return (error);
    172  1.17      cgd }
    173  1.17      cgd 
    174  1.17      cgd donice(curp, chgp, n)
    175  1.17      cgd 	register struct proc *curp, *chgp;
    176  1.17      cgd 	register int n;
    177  1.17      cgd {
    178  1.17      cgd 	register struct pcred *pcred = curp->p_cred;
    179  1.17      cgd 
    180  1.17      cgd 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
    181  1.17      cgd 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
    182  1.17      cgd 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
    183  1.17      cgd 		return (EPERM);
    184  1.17      cgd 	if (n > PRIO_MAX)
    185  1.17      cgd 		n = PRIO_MAX;
    186  1.17      cgd 	if (n < PRIO_MIN)
    187  1.17      cgd 		n = PRIO_MIN;
    188  1.17      cgd 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
    189  1.17      cgd 		return (EACCES);
    190  1.17      cgd 	chgp->p_nice = n;
    191  1.19      cgd 	(void)resetpriority(chgp);
    192  1.17      cgd 	return (0);
    193  1.17      cgd }
    194  1.17      cgd 
    195  1.17      cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    196  1.17      cgd /* ARGSUSED */
    197  1.22      cgd compat_43_setrlimit(p, uap, retval)
    198  1.17      cgd 	struct proc *p;
    199  1.22      cgd 	struct compat_43_setrlimit_args /* {
    200  1.22      cgd 		syscallarg(u_int) which;
    201  1.22      cgd 		syscallarg(struct ogetrlimit *) rlp;
    202  1.22      cgd 	} */ *uap;
    203  1.22      cgd 	register_t *retval;
    204  1.17      cgd {
    205  1.17      cgd 	struct orlimit olim;
    206  1.17      cgd 	struct rlimit lim;
    207  1.17      cgd 	int error;
    208  1.17      cgd 
    209  1.22      cgd 	if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&olim,
    210  1.22      cgd 	    sizeof (struct orlimit)))
    211  1.17      cgd 		return (error);
    212  1.17      cgd 	lim.rlim_cur = olim.rlim_cur;
    213  1.17      cgd 	lim.rlim_max = olim.rlim_max;
    214  1.22      cgd 	return (dosetrlimit(p, SCARG(uap, which), &lim));
    215  1.17      cgd }
    216  1.17      cgd 
    217  1.17      cgd /* ARGSUSED */
    218  1.22      cgd compat_43_getrlimit(p, uap, retval)
    219  1.17      cgd 	struct proc *p;
    220  1.22      cgd 	register struct compat_43_getrlimit_args /* {
    221  1.22      cgd 		syscallarg(u_int) which;
    222  1.22      cgd 		syscallarg(struct ogetrlimit *) rlp;
    223  1.22      cgd 	} */ *uap;
    224  1.22      cgd 	register_t *retval;
    225  1.17      cgd {
    226  1.17      cgd 	struct orlimit olim;
    227  1.17      cgd 
    228  1.22      cgd 	if (SCARG(uap, which) >= RLIM_NLIMITS)
    229  1.17      cgd 		return (EINVAL);
    230  1.22      cgd 	olim.rlim_cur = p->p_rlimit[SCARG(uap, which)].rlim_cur;
    231  1.17      cgd 	if (olim.rlim_cur == -1)
    232  1.17      cgd 		olim.rlim_cur = 0x7fffffff;
    233  1.22      cgd 	olim.rlim_max = p->p_rlimit[SCARG(uap, which)].rlim_max;
    234  1.17      cgd 	if (olim.rlim_max == -1)
    235  1.17      cgd 		olim.rlim_max = 0x7fffffff;
    236  1.22      cgd 	return (copyout((caddr_t)&olim, (caddr_t)SCARG(uap, rlp),
    237  1.22      cgd 	    sizeof(olim)));
    238  1.17      cgd }
    239  1.19      cgd #endif /* COMPAT_43 || COMPAT_SUNOS */
    240  1.17      cgd 
    241  1.17      cgd /* ARGSUSED */
    242  1.17      cgd setrlimit(p, uap, retval)
    243  1.17      cgd 	struct proc *p;
    244  1.22      cgd 	register struct setrlimit_args /* {
    245  1.22      cgd 		syscallarg(u_int) which;
    246  1.22      cgd 		syscallarg(struct rlimit *) rlp;
    247  1.22      cgd 	} */ *uap;
    248  1.22      cgd 	register_t *retval;
    249  1.17      cgd {
    250  1.19      cgd 	struct rlimit alim;
    251  1.17      cgd 	int error;
    252  1.17      cgd 
    253  1.22      cgd 	if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim,
    254  1.22      cgd 	    sizeof (struct rlimit)))
    255  1.17      cgd 		return (error);
    256  1.22      cgd 	return (dosetrlimit(p, SCARG(uap, which), &alim));
    257  1.17      cgd }
    258  1.17      cgd 
    259  1.17      cgd int
    260  1.17      cgd dosetrlimit(p, which, limp)
    261  1.17      cgd 	struct proc *p;
    262  1.17      cgd 	u_int which;
    263  1.17      cgd 	struct rlimit *limp;
    264  1.17      cgd {
    265  1.17      cgd 	register struct rlimit *alimp;
    266  1.19      cgd 	extern unsigned maxdmap, maxsmap;
    267  1.17      cgd 	int error;
    268  1.17      cgd 
    269  1.17      cgd 	if (which >= RLIM_NLIMITS)
    270  1.17      cgd 		return (EINVAL);
    271  1.17      cgd 	alimp = &p->p_rlimit[which];
    272  1.19      cgd 	if (limp->rlim_cur > alimp->rlim_max ||
    273  1.17      cgd 	    limp->rlim_max > alimp->rlim_max)
    274  1.17      cgd 		if (error = suser(p->p_ucred, &p->p_acflag))
    275  1.17      cgd 			return (error);
    276  1.17      cgd 	if (limp->rlim_cur > limp->rlim_max)
    277  1.17      cgd 		limp->rlim_cur = limp->rlim_max;
    278  1.17      cgd 	if (p->p_limit->p_refcnt > 1 &&
    279  1.17      cgd 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    280  1.17      cgd 		p->p_limit->p_refcnt--;
    281  1.17      cgd 		p->p_limit = limcopy(p->p_limit);
    282  1.17      cgd 		alimp = &p->p_rlimit[which];
    283  1.17      cgd 	}
    284  1.17      cgd 
    285  1.17      cgd 	switch (which) {
    286  1.17      cgd 
    287  1.17      cgd 	case RLIMIT_DATA:
    288  1.19      cgd 		if (limp->rlim_cur > maxdmap)
    289  1.19      cgd 			limp->rlim_cur = maxdmap;
    290  1.19      cgd 		if (limp->rlim_max > maxdmap)
    291  1.19      cgd 			limp->rlim_max = maxdmap;
    292  1.17      cgd 		break;
    293  1.17      cgd 
    294  1.17      cgd 	case RLIMIT_STACK:
    295  1.19      cgd 		if (limp->rlim_cur > maxsmap)
    296  1.19      cgd 			limp->rlim_cur = maxsmap;
    297  1.19      cgd 		if (limp->rlim_max > maxsmap)
    298  1.19      cgd 			limp->rlim_max = maxsmap;
    299  1.17      cgd 		/*
    300  1.17      cgd 		 * Stack is allocated to the max at exec time with only
    301  1.17      cgd 		 * "rlim_cur" bytes accessible.  If stack limit is going
    302  1.17      cgd 		 * up make more accessible, if going down make inaccessible.
    303  1.17      cgd 		 */
    304  1.17      cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    305  1.17      cgd 			vm_offset_t addr;
    306  1.17      cgd 			vm_size_t size;
    307  1.17      cgd 			vm_prot_t prot;
    308  1.17      cgd 
    309  1.17      cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    310  1.17      cgd 				prot = VM_PROT_ALL;
    311  1.17      cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    312  1.17      cgd 				addr = USRSTACK - limp->rlim_cur;
    313  1.17      cgd 			} else {
    314  1.17      cgd 				prot = VM_PROT_NONE;
    315  1.17      cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    316  1.17      cgd 				addr = USRSTACK - alimp->rlim_cur;
    317  1.17      cgd 			}
    318  1.17      cgd 			addr = trunc_page(addr);
    319  1.17      cgd 			size = round_page(size);
    320  1.17      cgd 			(void) vm_map_protect(&p->p_vmspace->vm_map,
    321  1.17      cgd 					      addr, addr+size, prot, FALSE);
    322  1.17      cgd 		}
    323  1.17      cgd 		break;
    324  1.19      cgd 
    325  1.19      cgd 	case RLIMIT_NOFILE:
    326  1.19      cgd 		if (limp->rlim_cur > maxfiles)
    327  1.19      cgd 			limp->rlim_cur = maxfiles;
    328  1.19      cgd 		if (limp->rlim_max > maxfiles)
    329  1.19      cgd 			limp->rlim_max = maxfiles;
    330  1.19      cgd 		break;
    331  1.19      cgd 
    332  1.19      cgd 	case RLIMIT_NPROC:
    333  1.19      cgd 		if (limp->rlim_cur > maxproc)
    334  1.19      cgd 			limp->rlim_cur = maxproc;
    335  1.19      cgd 		if (limp->rlim_max > maxproc)
    336  1.19      cgd 			limp->rlim_max = maxproc;
    337  1.19      cgd 		break;
    338  1.17      cgd 	}
    339  1.17      cgd 	*alimp = *limp;
    340  1.17      cgd 	return (0);
    341  1.17      cgd }
    342  1.17      cgd 
    343  1.17      cgd /* ARGSUSED */
    344  1.17      cgd getrlimit(p, uap, retval)
    345  1.17      cgd 	struct proc *p;
    346  1.22      cgd 	register struct getrlimit_args /* {
    347  1.22      cgd 		syscallarg(u_int) which;
    348  1.22      cgd 		syscallarg(struct rlimit *) rlp;
    349  1.22      cgd 	} */ *uap;
    350  1.22      cgd 	register_t *retval;
    351  1.17      cgd {
    352  1.17      cgd 
    353  1.22      cgd 	if (SCARG(uap, which) >= RLIM_NLIMITS)
    354  1.17      cgd 		return (EINVAL);
    355  1.22      cgd 	return (copyout((caddr_t)&p->p_rlimit[SCARG(uap, which)],
    356  1.22      cgd 	    (caddr_t)SCARG(uap, rlp), sizeof (struct rlimit)));
    357  1.17      cgd }
    358  1.17      cgd 
    359  1.17      cgd /*
    360  1.17      cgd  * Transform the running time and tick information in proc p into user,
    361  1.17      cgd  * system, and interrupt time usage.
    362  1.17      cgd  */
    363  1.17      cgd calcru(p, up, sp, ip)
    364  1.17      cgd 	register struct proc *p;
    365  1.17      cgd 	register struct timeval *up;
    366  1.17      cgd 	register struct timeval *sp;
    367  1.17      cgd 	register struct timeval *ip;
    368  1.17      cgd {
    369  1.17      cgd 	register u_quad_t u, st, ut, it, tot;
    370  1.17      cgd 	register u_long sec, usec;
    371  1.17      cgd 	register int s;
    372  1.17      cgd 	struct timeval tv;
    373  1.17      cgd 
    374  1.17      cgd 	s = splstatclock();
    375  1.17      cgd 	st = p->p_sticks;
    376  1.17      cgd 	ut = p->p_uticks;
    377  1.17      cgd 	it = p->p_iticks;
    378  1.17      cgd 	splx(s);
    379  1.17      cgd 
    380  1.17      cgd 	tot = st + ut + it;
    381  1.17      cgd 	if (tot == 0) {
    382  1.17      cgd 		up->tv_sec = up->tv_usec = 0;
    383  1.17      cgd 		sp->tv_sec = sp->tv_usec = 0;
    384  1.17      cgd 		if (ip != NULL)
    385  1.17      cgd 			ip->tv_sec = ip->tv_usec = 0;
    386  1.17      cgd 		return;
    387  1.17      cgd 	}
    388  1.17      cgd 
    389  1.17      cgd 	sec = p->p_rtime.tv_sec;
    390  1.17      cgd 	usec = p->p_rtime.tv_usec;
    391  1.17      cgd 	if (p == curproc) {
    392  1.17      cgd 		/*
    393  1.17      cgd 		 * Adjust for the current time slice.  This is actually fairly
    394  1.17      cgd 		 * important since the error here is on the order of a time
    395  1.17      cgd 		 * quantum, which is much greater than the sampling error.
    396  1.17      cgd 		 */
    397  1.17      cgd 		microtime(&tv);
    398  1.17      cgd 		sec += tv.tv_sec - runtime.tv_sec;
    399  1.17      cgd 		usec += tv.tv_usec - runtime.tv_usec;
    400  1.17      cgd 	}
    401  1.17      cgd 	u = sec * 1000000 + usec;
    402  1.17      cgd 	st = (u * st) / tot;
    403  1.17      cgd 	sp->tv_sec = st / 1000000;
    404  1.17      cgd 	sp->tv_usec = st % 1000000;
    405  1.17      cgd 	ut = (u * ut) / tot;
    406  1.17      cgd 	up->tv_sec = ut / 1000000;
    407  1.17      cgd 	up->tv_usec = ut % 1000000;
    408  1.17      cgd 	if (ip != NULL) {
    409  1.17      cgd 		it = (u * it) / tot;
    410  1.17      cgd 		ip->tv_sec = it / 1000000;
    411  1.17      cgd 		ip->tv_usec = it % 1000000;
    412  1.17      cgd 	}
    413  1.17      cgd }
    414  1.17      cgd 
    415  1.17      cgd /* ARGSUSED */
    416  1.17      cgd getrusage(p, uap, retval)
    417  1.17      cgd 	register struct proc *p;
    418  1.22      cgd 	register struct getrusage_args /* {
    419  1.22      cgd 		syscallarg(int) who;
    420  1.22      cgd 		syscallarg(struct rusage *) rusage;
    421  1.22      cgd 	} */ *uap;
    422  1.22      cgd 	register_t *retval;
    423  1.17      cgd {
    424  1.17      cgd 	register struct rusage *rup;
    425  1.17      cgd 
    426  1.22      cgd 	switch (SCARG(uap, who)) {
    427  1.17      cgd 
    428  1.19      cgd 	case RUSAGE_SELF:
    429  1.17      cgd 		rup = &p->p_stats->p_ru;
    430  1.17      cgd 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    431  1.17      cgd 		break;
    432  1.17      cgd 
    433  1.17      cgd 	case RUSAGE_CHILDREN:
    434  1.17      cgd 		rup = &p->p_stats->p_cru;
    435  1.17      cgd 		break;
    436  1.17      cgd 
    437  1.17      cgd 	default:
    438  1.17      cgd 		return (EINVAL);
    439  1.17      cgd 	}
    440  1.22      cgd 	return (copyout((caddr_t)rup, (caddr_t)SCARG(uap, rusage),
    441  1.17      cgd 	    sizeof (struct rusage)));
    442  1.17      cgd }
    443  1.17      cgd 
    444  1.17      cgd ruadd(ru, ru2)
    445  1.17      cgd 	register struct rusage *ru, *ru2;
    446  1.17      cgd {
    447  1.17      cgd 	register long *ip, *ip2;
    448  1.17      cgd 	register int i;
    449  1.17      cgd 
    450  1.17      cgd 	timevaladd(&ru->ru_utime, &ru2->ru_utime);
    451  1.17      cgd 	timevaladd(&ru->ru_stime, &ru2->ru_stime);
    452  1.17      cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    453  1.17      cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    454  1.17      cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    455  1.17      cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    456  1.17      cgd 		*ip++ += *ip2++;
    457  1.17      cgd }
    458  1.17      cgd 
    459  1.17      cgd /*
    460  1.17      cgd  * Make a copy of the plimit structure.
    461  1.17      cgd  * We share these structures copy-on-write after fork,
    462  1.17      cgd  * and copy when a limit is changed.
    463  1.17      cgd  */
    464  1.17      cgd struct plimit *
    465  1.17      cgd limcopy(lim)
    466  1.17      cgd 	struct plimit *lim;
    467  1.17      cgd {
    468  1.17      cgd 	register struct plimit *copy;
    469  1.17      cgd 
    470  1.17      cgd 	MALLOC(copy, struct plimit *, sizeof(struct plimit),
    471  1.17      cgd 	    M_SUBPROC, M_WAITOK);
    472  1.17      cgd 	bcopy(lim->pl_rlimit, copy->pl_rlimit,
    473  1.17      cgd 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    474  1.17      cgd 	copy->p_lflags = 0;
    475  1.17      cgd 	copy->p_refcnt = 1;
    476  1.17      cgd 	return (copy);
    477  1.17      cgd }
    478