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