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