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