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