Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.100.4.3
      1  1.100.4.3      elad /*	$NetBSD: kern_resource.c,v 1.100.4.3 2006/03/12 23:33:56 elad 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.72       agc  * 3. Neither the name of the University nor the names of its contributors
     21       1.17       cgd  *    may be used to endorse or promote products derived from this software
     22       1.17       cgd  *    without specific prior written permission.
     23       1.17       cgd  *
     24       1.17       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.17       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.17       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.17       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.17       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.17       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.17       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.17       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.17       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.17       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.17       cgd  * SUCH DAMAGE.
     35       1.17       cgd  *
     36       1.45      fvdl  *	@(#)kern_resource.c	8.8 (Berkeley) 2/14/95
     37       1.17       cgd  */
     38       1.61     lukem 
     39       1.61     lukem #include <sys/cdefs.h>
     40  1.100.4.3      elad __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.100.4.3 2006/03/12 23:33:56 elad Exp $");
     41       1.44       mrg 
     42       1.17       cgd #include <sys/param.h>
     43       1.22       cgd #include <sys/systm.h>
     44       1.17       cgd #include <sys/kernel.h>
     45       1.19       cgd #include <sys/file.h>
     46       1.17       cgd #include <sys/resourcevar.h>
     47       1.17       cgd #include <sys/malloc.h>
     48      1.100      yamt #include <sys/namei.h>
     49       1.49   thorpej #include <sys/pool.h>
     50       1.17       cgd #include <sys/proc.h>
     51       1.74    atatat #include <sys/sysctl.h>
     52       1.17       cgd 
     53       1.22       cgd #include <sys/mount.h>
     54       1.68   thorpej #include <sys/sa.h>
     55       1.22       cgd #include <sys/syscallargs.h>
     56       1.17       cgd 
     57       1.43       mrg #include <uvm/uvm_extern.h>
     58       1.43       mrg 
     59       1.17       cgd /*
     60       1.60       eeh  * Maximum process data and stack limits.
     61       1.60       eeh  * They are variables so they are 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.82      matt struct uihashhead *uihashtbl;
     67       1.82      matt u_long uihash;		/* size of hash table - 1 */
     68       1.88  christos struct simplelock uihashtbl_slock = SIMPLELOCK_INITIALIZER;
     69       1.82      matt 
     70       1.79  christos 
     71       1.60       eeh /*
     72       1.17       cgd  * Resource controls and accounting.
     73       1.17       cgd  */
     74       1.17       cgd 
     75       1.25       cgd int
     76       1.98   thorpej sys_getpriority(struct lwp *l, void *v, register_t *retval)
     77       1.30   thorpej {
     78       1.54  augustss 	struct sys_getpriority_args /* {
     79       1.22       cgd 		syscallarg(int) which;
     80       1.81    kleink 		syscallarg(id_t) who;
     81       1.30   thorpej 	} */ *uap = v;
     82       1.68   thorpej 	struct proc *curp = l->l_proc, *p;
     83       1.54  augustss 	int low = NZERO + PRIO_MAX + 1;
     84       1.17       cgd 
     85       1.22       cgd 	switch (SCARG(uap, which)) {
     86       1.17       cgd 
     87       1.17       cgd 	case PRIO_PROCESS:
     88       1.22       cgd 		if (SCARG(uap, who) == 0)
     89       1.17       cgd 			p = curp;
     90       1.17       cgd 		else
     91       1.22       cgd 			p = pfind(SCARG(uap, who));
     92       1.17       cgd 		if (p == 0)
     93       1.17       cgd 			break;
     94       1.17       cgd 		low = p->p_nice;
     95       1.17       cgd 		break;
     96       1.17       cgd 
     97       1.17       cgd 	case PRIO_PGRP: {
     98       1.54  augustss 		struct pgrp *pg;
     99       1.17       cgd 
    100       1.22       cgd 		if (SCARG(uap, who) == 0)
    101       1.17       cgd 			pg = curp->p_pgrp;
    102       1.22       cgd 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    103       1.17       cgd 			break;
    104       1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    105       1.17       cgd 			if (p->p_nice < low)
    106       1.17       cgd 				low = p->p_nice;
    107       1.17       cgd 		}
    108       1.17       cgd 		break;
    109       1.17       cgd 	}
    110       1.17       cgd 
    111       1.17       cgd 	case PRIO_USER:
    112       1.22       cgd 		if (SCARG(uap, who) == 0)
    113  1.100.4.1      elad 			SCARG(uap, who) = kauth_cred_geteuid(curp->p_cred);
    114       1.52   thorpej 		proclist_lock_read();
    115       1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    116  1.100.4.1      elad 			if (kauth_cred_geteuid(p->p_cred) == (uid_t) SCARG(uap, who) &&
    117       1.22       cgd 			    p->p_nice < low)
    118       1.17       cgd 				low = p->p_nice;
    119       1.64      matt 		}
    120       1.51   thorpej 		proclist_unlock_read();
    121       1.17       cgd 		break;
    122       1.17       cgd 
    123       1.17       cgd 	default:
    124       1.17       cgd 		return (EINVAL);
    125       1.17       cgd 	}
    126       1.37        ws 	if (low == NZERO + PRIO_MAX + 1)
    127       1.17       cgd 		return (ESRCH);
    128       1.37        ws 	*retval = low - NZERO;
    129       1.17       cgd 	return (0);
    130       1.17       cgd }
    131       1.17       cgd 
    132       1.17       cgd /* ARGSUSED */
    133       1.25       cgd int
    134       1.98   thorpej sys_setpriority(struct lwp *l, void *v, register_t *retval)
    135       1.30   thorpej {
    136       1.54  augustss 	struct sys_setpriority_args /* {
    137       1.22       cgd 		syscallarg(int) which;
    138       1.81    kleink 		syscallarg(id_t) who;
    139       1.22       cgd 		syscallarg(int) prio;
    140       1.30   thorpej 	} */ *uap = v;
    141       1.68   thorpej 	struct proc *curp = l->l_proc, *p;
    142       1.17       cgd 	int found = 0, error = 0;
    143       1.17       cgd 
    144       1.22       cgd 	switch (SCARG(uap, which)) {
    145       1.17       cgd 
    146       1.17       cgd 	case PRIO_PROCESS:
    147       1.22       cgd 		if (SCARG(uap, who) == 0)
    148       1.17       cgd 			p = curp;
    149       1.17       cgd 		else
    150       1.22       cgd 			p = pfind(SCARG(uap, who));
    151       1.17       cgd 		if (p == 0)
    152       1.17       cgd 			break;
    153       1.22       cgd 		error = donice(curp, p, SCARG(uap, prio));
    154       1.17       cgd 		found++;
    155       1.17       cgd 		break;
    156       1.17       cgd 
    157       1.17       cgd 	case PRIO_PGRP: {
    158       1.54  augustss 		struct pgrp *pg;
    159       1.87     perry 
    160       1.22       cgd 		if (SCARG(uap, who) == 0)
    161       1.17       cgd 			pg = curp->p_pgrp;
    162       1.22       cgd 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    163       1.17       cgd 			break;
    164       1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    165       1.22       cgd 			error = donice(curp, p, SCARG(uap, prio));
    166       1.17       cgd 			found++;
    167       1.17       cgd 		}
    168       1.17       cgd 		break;
    169       1.17       cgd 	}
    170       1.17       cgd 
    171       1.17       cgd 	case PRIO_USER:
    172       1.22       cgd 		if (SCARG(uap, who) == 0)
    173  1.100.4.1      elad 			SCARG(uap, who) = kauth_cred_geteuid(curp->p_cred);
    174       1.52   thorpej 		proclist_lock_read();
    175       1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    176  1.100.4.1      elad 			if (kauth_cred_geteuid(p->p_cred) == (uid_t) SCARG(uap, who)) {
    177       1.22       cgd 				error = donice(curp, p, SCARG(uap, prio));
    178       1.17       cgd 				found++;
    179       1.17       cgd 			}
    180       1.64      matt 		}
    181       1.51   thorpej 		proclist_unlock_read();
    182       1.17       cgd 		break;
    183       1.17       cgd 
    184       1.17       cgd 	default:
    185       1.17       cgd 		return (EINVAL);
    186       1.17       cgd 	}
    187       1.17       cgd 	if (found == 0)
    188       1.17       cgd 		return (ESRCH);
    189       1.17       cgd 	return (error);
    190       1.17       cgd }
    191       1.17       cgd 
    192       1.25       cgd int
    193       1.98   thorpej donice(struct proc *curp, struct proc *chgp, int n)
    194       1.17       cgd {
    195  1.100.4.1      elad 	kauth_cred_t cred = curp->p_cred;
    196       1.59   thorpej 	int s;
    197       1.17       cgd 
    198  1.100.4.1      elad 	if (kauth_cred_geteuid(cred) && kauth_cred_getuid(cred) &&
    199  1.100.4.1      elad 	    kauth_cred_geteuid(cred) != kauth_cred_geteuid(chgp->p_cred) &&
    200  1.100.4.1      elad 	    kauth_cred_getuid(cred) != kauth_cred_geteuid(chgp->p_cred))
    201       1.17       cgd 		return (EPERM);
    202       1.17       cgd 	if (n > PRIO_MAX)
    203       1.17       cgd 		n = PRIO_MAX;
    204       1.17       cgd 	if (n < PRIO_MIN)
    205       1.17       cgd 		n = PRIO_MIN;
    206       1.37        ws 	n += NZERO;
    207  1.100.4.2      elad 	if (n < chgp->p_nice && kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    208  1.100.4.1      elad 						  &curp->p_acflag))
    209       1.17       cgd 		return (EACCES);
    210       1.17       cgd 	chgp->p_nice = n;
    211       1.59   thorpej 	SCHED_LOCK(s);
    212       1.68   thorpej 	(void)resetprocpriority(chgp);
    213       1.59   thorpej 	SCHED_UNLOCK(s);
    214       1.17       cgd 	return (0);
    215       1.17       cgd }
    216       1.17       cgd 
    217       1.17       cgd /* ARGSUSED */
    218       1.25       cgd int
    219       1.98   thorpej sys_setrlimit(struct lwp *l, void *v, register_t *retval)
    220       1.30   thorpej {
    221       1.54  augustss 	struct sys_setrlimit_args /* {
    222       1.42   mycroft 		syscallarg(int) which;
    223       1.39       cgd 		syscallarg(const struct rlimit *) rlp;
    224       1.30   thorpej 	} */ *uap = v;
    225       1.68   thorpej 	struct proc *p = l->l_proc;
    226       1.42   mycroft 	int which = SCARG(uap, which);
    227       1.19       cgd 	struct rlimit alim;
    228       1.17       cgd 	int error;
    229       1.17       cgd 
    230       1.46     perry 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    231       1.33  christos 	if (error)
    232       1.17       cgd 		return (error);
    233       1.53    bouyer 	return (dosetrlimit(p, p->p_cred, which, &alim));
    234       1.17       cgd }
    235       1.17       cgd 
    236       1.17       cgd int
    237  1.100.4.1      elad dosetrlimit(struct proc *p, kauth_cred_t cred, int which, struct rlimit *limp)
    238       1.17       cgd {
    239       1.54  augustss 	struct rlimit *alimp;
    240       1.83        pk 	struct plimit *oldplim;
    241       1.17       cgd 	int error;
    242       1.17       cgd 
    243       1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    244       1.17       cgd 		return (EINVAL);
    245       1.38  matthias 
    246       1.38  matthias 	if (limp->rlim_cur < 0 || limp->rlim_max < 0)
    247       1.38  matthias 		return (EINVAL);
    248       1.38  matthias 
    249       1.17       cgd 	alimp = &p->p_rlimit[which];
    250       1.53    bouyer 	/* if we don't change the value, no need to limcopy() */
    251       1.53    bouyer 	if (limp->rlim_cur == alimp->rlim_cur &&
    252       1.53    bouyer 	    limp->rlim_max == alimp->rlim_max)
    253       1.53    bouyer 		return 0;
    254       1.53    bouyer 
    255       1.62  jdolecek 	if (limp->rlim_cur > limp->rlim_max) {
    256       1.62  jdolecek 		/*
    257       1.62  jdolecek 		 * This is programming error. According to SUSv2, we should
    258       1.62  jdolecek 		 * return error in this case.
    259       1.62  jdolecek 		 */
    260       1.62  jdolecek 		return (EINVAL);
    261       1.62  jdolecek 	}
    262       1.62  jdolecek 	if (limp->rlim_max > alimp->rlim_max
    263  1.100.4.2      elad 	    && (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    264  1.100.4.1      elad 					  &p->p_acflag)) != 0)
    265       1.17       cgd 			return (error);
    266       1.62  jdolecek 
    267       1.17       cgd 	if (p->p_limit->p_refcnt > 1 &&
    268       1.17       cgd 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    269       1.83        pk 		p->p_limit = limcopy(oldplim = p->p_limit);
    270       1.83        pk 		limfree(oldplim);
    271       1.17       cgd 		alimp = &p->p_rlimit[which];
    272       1.17       cgd 	}
    273       1.17       cgd 
    274       1.17       cgd 	switch (which) {
    275       1.17       cgd 
    276       1.17       cgd 	case RLIMIT_DATA:
    277       1.19       cgd 		if (limp->rlim_cur > maxdmap)
    278       1.19       cgd 			limp->rlim_cur = maxdmap;
    279       1.19       cgd 		if (limp->rlim_max > maxdmap)
    280       1.19       cgd 			limp->rlim_max = maxdmap;
    281       1.17       cgd 		break;
    282       1.17       cgd 
    283       1.17       cgd 	case RLIMIT_STACK:
    284       1.19       cgd 		if (limp->rlim_cur > maxsmap)
    285       1.19       cgd 			limp->rlim_cur = maxsmap;
    286       1.19       cgd 		if (limp->rlim_max > maxsmap)
    287       1.19       cgd 			limp->rlim_max = maxsmap;
    288       1.62  jdolecek 
    289       1.62  jdolecek 		/*
    290       1.62  jdolecek 		 * Return EINVAL if the new stack size limit is lower than
    291       1.62  jdolecek 		 * current usage. Otherwise, the process would get SIGSEGV the
    292       1.62  jdolecek 		 * moment it would try to access anything on it's current stack.
    293       1.62  jdolecek 		 * This conforms to SUSv2.
    294       1.62  jdolecek 		 */
    295       1.62  jdolecek 		if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
    296       1.62  jdolecek 		    || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE)
    297       1.62  jdolecek 			return (EINVAL);
    298       1.40     enami 
    299       1.17       cgd 		/*
    300       1.40     enami 		 * Stack is allocated to the max at exec time with
    301       1.40     enami 		 * only "rlim_cur" bytes accessible (In other words,
    302       1.40     enami 		 * allocates stack dividing two contiguous regions at
    303       1.40     enami 		 * "rlim_cur" bytes boundary).
    304       1.40     enami 		 *
    305       1.40     enami 		 * Since allocation is done in terms of page, roundup
    306       1.40     enami 		 * "rlim_cur" (otherwise, contiguous regions
    307       1.40     enami 		 * overlap).  If stack limit is going up make more
    308       1.40     enami 		 * accessible, if going down make inaccessible.
    309       1.17       cgd 		 */
    310       1.40     enami 		limp->rlim_cur = round_page(limp->rlim_cur);
    311       1.17       cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    312       1.48       eeh 			vaddr_t addr;
    313       1.48       eeh 			vsize_t size;
    314       1.17       cgd 			vm_prot_t prot;
    315       1.17       cgd 
    316       1.17       cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    317       1.73       chs 				prot = VM_PROT_READ | VM_PROT_WRITE;
    318       1.17       cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    319       1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    320       1.91      fvdl 				    limp->rlim_cur;
    321       1.17       cgd 			} else {
    322       1.17       cgd 				prot = VM_PROT_NONE;
    323       1.17       cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    324       1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    325       1.91      fvdl 				     alimp->rlim_cur;
    326       1.17       cgd 			}
    327       1.43       mrg 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    328       1.43       mrg 					      addr, addr+size, prot, FALSE);
    329       1.17       cgd 		}
    330       1.17       cgd 		break;
    331       1.19       cgd 
    332       1.19       cgd 	case RLIMIT_NOFILE:
    333       1.19       cgd 		if (limp->rlim_cur > maxfiles)
    334       1.19       cgd 			limp->rlim_cur = maxfiles;
    335       1.19       cgd 		if (limp->rlim_max > maxfiles)
    336       1.19       cgd 			limp->rlim_max = maxfiles;
    337       1.19       cgd 		break;
    338       1.19       cgd 
    339       1.19       cgd 	case RLIMIT_NPROC:
    340       1.19       cgd 		if (limp->rlim_cur > maxproc)
    341       1.19       cgd 			limp->rlim_cur = maxproc;
    342       1.19       cgd 		if (limp->rlim_max > maxproc)
    343       1.19       cgd 			limp->rlim_max = maxproc;
    344       1.19       cgd 		break;
    345       1.17       cgd 	}
    346       1.17       cgd 	*alimp = *limp;
    347       1.17       cgd 	return (0);
    348       1.17       cgd }
    349       1.17       cgd 
    350       1.17       cgd /* ARGSUSED */
    351       1.25       cgd int
    352       1.98   thorpej sys_getrlimit(struct lwp *l, void *v, register_t *retval)
    353       1.30   thorpej {
    354       1.54  augustss 	struct sys_getrlimit_args /* {
    355       1.42   mycroft 		syscallarg(int) which;
    356       1.22       cgd 		syscallarg(struct rlimit *) rlp;
    357       1.30   thorpej 	} */ *uap = v;
    358       1.68   thorpej 	struct proc *p = l->l_proc;
    359       1.42   mycroft 	int which = SCARG(uap, which);
    360       1.17       cgd 
    361       1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    362       1.17       cgd 		return (EINVAL);
    363       1.42   mycroft 	return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
    364       1.46     perry 	    sizeof(struct rlimit)));
    365       1.17       cgd }
    366       1.17       cgd 
    367       1.17       cgd /*
    368       1.17       cgd  * Transform the running time and tick information in proc p into user,
    369       1.17       cgd  * system, and interrupt time usage.
    370       1.17       cgd  */
    371       1.25       cgd void
    372       1.98   thorpej calcru(struct proc *p, struct timeval *up, struct timeval *sp,
    373       1.98   thorpej     struct timeval *ip)
    374       1.17       cgd {
    375       1.54  augustss 	u_quad_t u, st, ut, it, tot;
    376       1.70       dsl 	unsigned long sec;
    377       1.70       dsl 	long usec;
    378       1.54  augustss 	int s;
    379       1.17       cgd 	struct timeval tv;
    380       1.68   thorpej 	struct lwp *l;
    381       1.17       cgd 
    382       1.17       cgd 	s = splstatclock();
    383       1.17       cgd 	st = p->p_sticks;
    384       1.17       cgd 	ut = p->p_uticks;
    385       1.17       cgd 	it = p->p_iticks;
    386       1.17       cgd 	splx(s);
    387       1.17       cgd 
    388       1.17       cgd 	sec = p->p_rtime.tv_sec;
    389       1.17       cgd 	usec = p->p_rtime.tv_usec;
    390       1.70       dsl 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    391       1.68   thorpej 		if (l->l_stat == LSONPROC) {
    392       1.68   thorpej 			struct schedstate_percpu *spc;
    393       1.87     perry 
    394       1.68   thorpej 			KDASSERT(l->l_cpu != NULL);
    395       1.68   thorpej 			spc = &l->l_cpu->ci_schedstate;
    396       1.87     perry 
    397       1.68   thorpej 			/*
    398       1.68   thorpej 			 * Adjust for the current time slice.  This is
    399       1.68   thorpej 			 * actually fairly important since the error
    400       1.68   thorpej 			 * here is on the order of a time quantum,
    401       1.68   thorpej 			 * which is much greater than the sampling
    402       1.87     perry 			 * error.
    403       1.68   thorpej 			 */
    404       1.68   thorpej 			microtime(&tv);
    405       1.68   thorpej 			sec += tv.tv_sec - spc->spc_runtime.tv_sec;
    406       1.68   thorpej 			usec += tv.tv_usec - spc->spc_runtime.tv_usec;
    407       1.68   thorpej 		}
    408       1.17       cgd 	}
    409       1.69       dsl 
    410       1.69       dsl 	tot = st + ut + it;
    411       1.70       dsl 	u = sec * 1000000ull + usec;
    412       1.70       dsl 
    413       1.69       dsl 	if (tot == 0) {
    414       1.69       dsl 		/* No ticks, so can't use to share time out, split 50-50 */
    415       1.70       dsl 		st = ut = u / 2;
    416       1.70       dsl 	} else {
    417       1.70       dsl 		st = (u * st) / tot;
    418       1.70       dsl 		ut = (u * ut) / tot;
    419       1.69       dsl 	}
    420       1.17       cgd 	sp->tv_sec = st / 1000000;
    421       1.17       cgd 	sp->tv_usec = st % 1000000;
    422       1.17       cgd 	up->tv_sec = ut / 1000000;
    423       1.17       cgd 	up->tv_usec = ut % 1000000;
    424       1.17       cgd 	if (ip != NULL) {
    425       1.70       dsl 		if (it != 0)
    426       1.70       dsl 			it = (u * it) / tot;
    427       1.17       cgd 		ip->tv_sec = it / 1000000;
    428       1.17       cgd 		ip->tv_usec = it % 1000000;
    429       1.17       cgd 	}
    430       1.17       cgd }
    431       1.17       cgd 
    432       1.17       cgd /* ARGSUSED */
    433       1.25       cgd int
    434       1.98   thorpej sys_getrusage(struct lwp *l, void *v, register_t *retval)
    435       1.30   thorpej {
    436       1.54  augustss 	struct sys_getrusage_args /* {
    437       1.22       cgd 		syscallarg(int) who;
    438       1.22       cgd 		syscallarg(struct rusage *) rusage;
    439       1.30   thorpej 	} */ *uap = v;
    440       1.54  augustss 	struct rusage *rup;
    441       1.68   thorpej 	struct proc *p = l->l_proc;
    442       1.17       cgd 
    443       1.22       cgd 	switch (SCARG(uap, who)) {
    444       1.17       cgd 
    445       1.19       cgd 	case RUSAGE_SELF:
    446       1.17       cgd 		rup = &p->p_stats->p_ru;
    447       1.17       cgd 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    448       1.17       cgd 		break;
    449       1.17       cgd 
    450       1.17       cgd 	case RUSAGE_CHILDREN:
    451       1.17       cgd 		rup = &p->p_stats->p_cru;
    452       1.17       cgd 		break;
    453       1.17       cgd 
    454       1.17       cgd 	default:
    455       1.17       cgd 		return (EINVAL);
    456       1.17       cgd 	}
    457       1.46     perry 	return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
    458       1.17       cgd }
    459       1.17       cgd 
    460       1.25       cgd void
    461       1.98   thorpej ruadd(struct rusage *ru, struct rusage *ru2)
    462       1.17       cgd {
    463       1.54  augustss 	long *ip, *ip2;
    464       1.54  augustss 	int i;
    465       1.17       cgd 
    466       1.27   mycroft 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    467       1.27   mycroft 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    468       1.17       cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    469       1.17       cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    470       1.17       cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    471       1.17       cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    472       1.17       cgd 		*ip++ += *ip2++;
    473       1.17       cgd }
    474       1.17       cgd 
    475       1.17       cgd /*
    476       1.17       cgd  * Make a copy of the plimit structure.
    477       1.17       cgd  * We share these structures copy-on-write after fork,
    478       1.17       cgd  * and copy when a limit is changed.
    479       1.17       cgd  */
    480       1.17       cgd struct plimit *
    481       1.98   thorpej limcopy(struct plimit *lim)
    482       1.17       cgd {
    483       1.54  augustss 	struct plimit *newlim;
    484       1.83        pk 	size_t l = 0;
    485       1.83        pk 
    486       1.83        pk 	simple_lock(&lim->p_slock);
    487       1.83        pk 	if (lim->pl_corename != defcorename)
    488       1.83        pk 		l = strlen(lim->pl_corename) + 1;
    489       1.83        pk 	simple_unlock(&lim->p_slock);
    490       1.17       cgd 
    491       1.49   thorpej 	newlim = pool_get(&plimit_pool, PR_WAITOK);
    492       1.83        pk 	simple_lock_init(&newlim->p_slock);
    493       1.83        pk 	newlim->p_lflags = 0;
    494       1.83        pk 	newlim->p_refcnt = 1;
    495       1.83        pk 	newlim->pl_corename = (l != 0)
    496       1.83        pk 		? malloc(l, M_TEMP, M_WAITOK)
    497       1.83        pk 		: defcorename;
    498       1.83        pk 
    499       1.83        pk 	simple_lock(&lim->p_slock);
    500       1.47     perry 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    501       1.17       cgd 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    502       1.83        pk 
    503       1.83        pk 	if (l != 0)
    504       1.71    itojun 		strlcpy(newlim->pl_corename, lim->pl_corename, l);
    505       1.83        pk 	simple_unlock(&lim->p_slock);
    506       1.83        pk 
    507       1.32   mycroft 	return (newlim);
    508       1.32   mycroft }
    509       1.32   mycroft 
    510       1.32   mycroft void
    511       1.98   thorpej limfree(struct plimit *lim)
    512       1.32   mycroft {
    513       1.84  christos 	int n;
    514       1.85    kleink 
    515       1.83        pk 	simple_lock(&lim->p_slock);
    516       1.84  christos 	n = --lim->p_refcnt;
    517       1.83        pk 	simple_unlock(&lim->p_slock);
    518       1.83        pk 	if (n > 0)
    519       1.32   mycroft 		return;
    520       1.53    bouyer #ifdef DIAGNOSTIC
    521       1.83        pk 	if (n < 0)
    522       1.53    bouyer 		panic("limfree");
    523       1.53    bouyer #endif
    524       1.53    bouyer 	if (lim->pl_corename != defcorename)
    525       1.53    bouyer 		free(lim->pl_corename, M_TEMP);
    526       1.49   thorpej 	pool_put(&plimit_pool, lim);
    527       1.68   thorpej }
    528       1.68   thorpej 
    529       1.68   thorpej struct pstats *
    530       1.98   thorpej pstatscopy(struct pstats *ps)
    531       1.68   thorpej {
    532       1.87     perry 
    533       1.68   thorpej 	struct pstats *newps;
    534       1.68   thorpej 
    535       1.68   thorpej 	newps = pool_get(&pstats_pool, PR_WAITOK);
    536       1.68   thorpej 
    537       1.68   thorpej 	memset(&newps->pstat_startzero, 0,
    538       1.68   thorpej 	(unsigned) ((caddr_t)&newps->pstat_endzero -
    539       1.68   thorpej 		    (caddr_t)&newps->pstat_startzero));
    540       1.68   thorpej 	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
    541       1.68   thorpej 	((caddr_t)&newps->pstat_endcopy -
    542       1.68   thorpej 	 (caddr_t)&newps->pstat_startcopy));
    543       1.68   thorpej 
    544       1.68   thorpej 	return (newps);
    545       1.68   thorpej 
    546       1.68   thorpej }
    547       1.68   thorpej 
    548       1.68   thorpej void
    549       1.98   thorpej pstatsfree(struct pstats *ps)
    550       1.68   thorpej {
    551       1.68   thorpej 
    552       1.68   thorpej 	pool_put(&pstats_pool, ps);
    553       1.74    atatat }
    554       1.74    atatat 
    555       1.74    atatat /*
    556       1.74    atatat  * sysctl interface in five parts
    557       1.74    atatat  */
    558       1.74    atatat 
    559       1.74    atatat /*
    560       1.74    atatat  * a routine for sysctl proc subtree helpers that need to pick a valid
    561       1.74    atatat  * process by pid.
    562       1.74    atatat  */
    563       1.74    atatat static int
    564       1.74    atatat sysctl_proc_findproc(struct proc *p, struct proc **p2, pid_t pid)
    565       1.74    atatat {
    566       1.74    atatat 	struct proc *ptmp;
    567  1.100.4.3      elad 	int error = 0;
    568       1.74    atatat 
    569       1.74    atatat 	if (pid == PROC_CURPROC)
    570       1.74    atatat 		ptmp = p;
    571       1.74    atatat 	else if ((ptmp = pfind(pid)) == NULL)
    572       1.74    atatat 		error = ESRCH;
    573       1.74    atatat 	else {
    574       1.74    atatat 		/*
    575       1.74    atatat 		 * suid proc of ours or proc not ours
    576       1.74    atatat 		 */
    577  1.100.4.1      elad 		if (kauth_cred_getuid(p->p_cred) != kauth_cred_getuid(ptmp->p_cred) ||
    578  1.100.4.1      elad 		    kauth_cred_getuid(p->p_cred) != kauth_cred_getsvuid(ptmp->p_cred))
    579  1.100.4.2      elad 			error = kauth_authorize_generic(p->p_cred,
    580  1.100.4.1      elad 				    KAUTH_GENERIC_ISSUSER, &p->p_acflag);
    581       1.74    atatat 
    582       1.74    atatat 		/*
    583       1.74    atatat 		 * sgid proc has sgid back to us temporarily
    584       1.74    atatat 		 */
    585  1.100.4.1      elad 		else if (kauth_cred_getgid(ptmp->p_cred) != kauth_cred_getsvgid(ptmp->p_cred))
    586  1.100.4.2      elad 			error = kauth_authorize_generic(p->p_cred,
    587  1.100.4.1      elad 			    KAUTH_GENERIC_ISSUSER, &p->p_acflag);
    588       1.74    atatat 
    589       1.74    atatat 		/*
    590       1.74    atatat 		 * our rgid must be in target's group list (ie,
    591       1.74    atatat 		 * sub-processes started by a sgid process)
    592       1.74    atatat 		 */
    593       1.74    atatat 		else {
    594  1.100.4.3      elad 			int ismember = 0;
    595  1.100.4.1      elad 
    596  1.100.4.3      elad 			if (kauth_cred_ismember_gid(p->p_cred,
    597  1.100.4.3      elad 			    kauth_cred_getgid(ptmp->p_cred), &ismember) != 0 ||
    598  1.100.4.3      elad 			    !ismember) {
    599  1.100.4.2      elad 				error = kauth_authorize_generic(p->p_cred,
    600  1.100.4.3      elad 				    KAUTH_GENERIC_ISSUSER, &p->p_acflag);
    601  1.100.4.3      elad 			}
    602       1.74    atatat 		}
    603       1.74    atatat 	}
    604       1.74    atatat 
    605       1.74    atatat 	*p2 = ptmp;
    606       1.74    atatat 	return (error);
    607       1.74    atatat }
    608       1.74    atatat 
    609       1.74    atatat /*
    610       1.74    atatat  * sysctl helper routine for setting a process's specific corefile
    611       1.74    atatat  * name.  picks the process based on the given pid and checks the
    612       1.74    atatat  * correctness of the new value.
    613       1.74    atatat  */
    614       1.74    atatat static int
    615       1.74    atatat sysctl_proc_corename(SYSCTLFN_ARGS)
    616       1.74    atatat {
    617       1.74    atatat 	struct proc *ptmp, *p;
    618       1.83        pk 	struct plimit *lim;
    619       1.74    atatat 	int error = 0, len;
    620      1.100      yamt 	char *cname;
    621      1.100      yamt 	char *tmp;
    622       1.74    atatat 	struct sysctlnode node;
    623       1.74    atatat 
    624       1.74    atatat 	/*
    625       1.74    atatat 	 * is this all correct?
    626       1.74    atatat 	 */
    627       1.74    atatat 	if (namelen != 0)
    628       1.74    atatat 		return (EINVAL);
    629       1.74    atatat 	if (name[-1] != PROC_PID_CORENAME)
    630       1.74    atatat 		return (EINVAL);
    631       1.74    atatat 
    632       1.74    atatat 	/*
    633       1.74    atatat 	 * whom are we tweaking?
    634       1.74    atatat 	 */
    635       1.74    atatat 	p = l->l_proc;
    636       1.74    atatat 	error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
    637       1.74    atatat 	if (error)
    638       1.74    atatat 		return (error);
    639       1.74    atatat 
    640      1.100      yamt 	cname = PNBUF_GET();
    641       1.74    atatat 	/*
    642       1.74    atatat 	 * let them modify a temporary copy of the core name
    643       1.74    atatat 	 */
    644       1.74    atatat 	node = *rnode;
    645      1.100      yamt 	strlcpy(cname, ptmp->p_limit->pl_corename, MAXPATHLEN);
    646       1.74    atatat 	node.sysctl_data = cname;
    647       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    648       1.74    atatat 
    649       1.74    atatat 	/*
    650       1.74    atatat 	 * if that failed, or they have nothing new to say, or we've
    651       1.74    atatat 	 * heard it before...
    652       1.74    atatat 	 */
    653       1.74    atatat 	if (error || newp == NULL ||
    654      1.100      yamt 	    strcmp(cname, ptmp->p_limit->pl_corename) == 0) {
    655      1.100      yamt 		goto done;
    656      1.100      yamt 	}
    657       1.74    atatat 
    658       1.74    atatat 	/*
    659       1.74    atatat 	 * no error yet and cname now has the new core name in it.
    660       1.74    atatat 	 * let's see if it looks acceptable.  it must be either "core"
    661       1.74    atatat 	 * or end in ".core" or "/core".
    662       1.74    atatat 	 */
    663       1.74    atatat 	len = strlen(cname);
    664      1.100      yamt 	if (len < 4) {
    665      1.100      yamt 		error = EINVAL;
    666      1.100      yamt 	} else if (strcmp(cname + len - 4, "core") != 0) {
    667      1.100      yamt 		error = EINVAL;
    668      1.100      yamt 	} else if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.') {
    669      1.100      yamt 		error = EINVAL;
    670      1.100      yamt 	}
    671      1.100      yamt 	if (error != 0) {
    672      1.100      yamt 		goto done;
    673      1.100      yamt 	}
    674       1.74    atatat 
    675       1.74    atatat 	/*
    676       1.74    atatat 	 * hmm...looks good.  now...where do we put it?
    677       1.74    atatat 	 */
    678       1.74    atatat 	tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
    679      1.100      yamt 	if (tmp == NULL) {
    680      1.100      yamt 		error = ENOMEM;
    681      1.100      yamt 		goto done;
    682      1.100      yamt 	}
    683       1.74    atatat 	strlcpy(tmp, cname, len + 1);
    684       1.74    atatat 
    685       1.83        pk 	lim = ptmp->p_limit;
    686       1.83        pk 	if (lim->p_refcnt > 1 && (lim->p_lflags & PL_SHAREMOD) == 0) {
    687       1.83        pk 		ptmp->p_limit = limcopy(lim);
    688       1.83        pk 		limfree(lim);
    689       1.83        pk 		lim = ptmp->p_limit;
    690       1.83        pk 	}
    691       1.83        pk 	if (lim->pl_corename != defcorename)
    692       1.83        pk 		free(lim->pl_corename, M_TEMP);
    693       1.83        pk 	lim->pl_corename = tmp;
    694      1.100      yamt done:
    695      1.100      yamt 	PNBUF_PUT(cname);
    696      1.100      yamt 	return error;
    697       1.74    atatat }
    698       1.74    atatat 
    699       1.74    atatat /*
    700       1.74    atatat  * sysctl helper routine for checking/setting a process's stop flags,
    701       1.74    atatat  * one for fork and one for exec.
    702       1.74    atatat  */
    703       1.74    atatat static int
    704       1.74    atatat sysctl_proc_stop(SYSCTLFN_ARGS)
    705       1.74    atatat {
    706       1.74    atatat 	struct proc *p, *ptmp;
    707       1.74    atatat 	int i, f, error = 0;
    708       1.74    atatat 	struct sysctlnode node;
    709       1.74    atatat 
    710       1.74    atatat 	if (namelen != 0)
    711       1.74    atatat 		return (EINVAL);
    712       1.74    atatat 
    713       1.74    atatat 	p = l->l_proc;
    714       1.74    atatat 	error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
    715       1.74    atatat 	if (error)
    716       1.74    atatat 		return (error);
    717       1.74    atatat 
    718       1.74    atatat 	switch (rnode->sysctl_num) {
    719       1.74    atatat 	case PROC_PID_STOPFORK:
    720       1.74    atatat 		f = P_STOPFORK;
    721       1.74    atatat 		break;
    722       1.74    atatat 	case PROC_PID_STOPEXEC:
    723       1.74    atatat 		f = P_STOPEXEC;
    724       1.74    atatat 		break;
    725       1.74    atatat 	case PROC_PID_STOPEXIT:
    726       1.74    atatat 		f = P_STOPEXIT;
    727       1.74    atatat 		break;
    728       1.74    atatat 	default:
    729       1.74    atatat 		return (EINVAL);
    730       1.74    atatat 	}
    731       1.74    atatat 
    732       1.74    atatat 	i = (ptmp->p_flag & f) ? 1 : 0;
    733       1.74    atatat 	node = *rnode;
    734       1.74    atatat 	node.sysctl_data = &i;
    735       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    736       1.74    atatat 	if (error || newp == NULL)
    737       1.74    atatat 		return (error);
    738       1.74    atatat 
    739       1.74    atatat 	if (i)
    740       1.74    atatat 		ptmp->p_flag |= f;
    741       1.74    atatat 	else
    742       1.74    atatat 		ptmp->p_flag &= ~f;
    743       1.74    atatat 
    744       1.74    atatat 	return (0);
    745       1.74    atatat }
    746       1.74    atatat 
    747       1.74    atatat /*
    748       1.74    atatat  * sysctl helper routine for a process's rlimits as exposed by sysctl.
    749       1.74    atatat  */
    750       1.74    atatat static int
    751       1.74    atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
    752       1.74    atatat {
    753       1.74    atatat 	struct proc *ptmp, *p;
    754       1.74    atatat 	u_int limitno;
    755       1.74    atatat 	int which, error = 0;
    756       1.74    atatat         struct rlimit alim;
    757       1.74    atatat 	struct sysctlnode node;
    758       1.74    atatat 
    759       1.74    atatat 	if (namelen != 0)
    760       1.74    atatat 		return (EINVAL);
    761       1.74    atatat 
    762       1.74    atatat 	which = name[-1];
    763       1.74    atatat 	if (which != PROC_PID_LIMIT_TYPE_SOFT &&
    764       1.74    atatat 	    which != PROC_PID_LIMIT_TYPE_HARD)
    765       1.74    atatat 		return (EINVAL);
    766       1.74    atatat 
    767       1.74    atatat 	limitno = name[-2] - 1;
    768       1.74    atatat 	if (limitno >= RLIM_NLIMITS)
    769       1.74    atatat 		return (EINVAL);
    770       1.74    atatat 
    771       1.74    atatat 	if (name[-3] != PROC_PID_LIMIT)
    772       1.74    atatat 		return (EINVAL);
    773       1.74    atatat 
    774       1.74    atatat 	p = l->l_proc;
    775       1.74    atatat 	error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-4]);
    776       1.74    atatat 	if (error)
    777       1.74    atatat 		return (error);
    778       1.74    atatat 
    779       1.74    atatat 	node = *rnode;
    780       1.74    atatat 	memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
    781       1.74    atatat 	if (which == PROC_PID_LIMIT_TYPE_HARD)
    782       1.74    atatat 		node.sysctl_data = &alim.rlim_max;
    783       1.74    atatat 	else
    784       1.74    atatat 		node.sysctl_data = &alim.rlim_cur;
    785       1.74    atatat 
    786       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    787       1.74    atatat 	if (error || newp == NULL)
    788       1.74    atatat 		return (error);
    789       1.74    atatat 
    790       1.74    atatat 	return (dosetrlimit(ptmp, p->p_cred, limitno, &alim));
    791       1.74    atatat }
    792       1.74    atatat 
    793       1.74    atatat /*
    794       1.74    atatat  * and finally, the actually glue that sticks it to the tree
    795       1.74    atatat  */
    796       1.74    atatat SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
    797       1.74    atatat {
    798       1.74    atatat 
    799       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    800       1.76    atatat 		       CTLFLAG_PERMANENT,
    801       1.74    atatat 		       CTLTYPE_NODE, "proc", NULL,
    802       1.74    atatat 		       NULL, 0, NULL, 0,
    803       1.74    atatat 		       CTL_PROC, CTL_EOL);
    804       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    805       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
    806       1.78    atatat 		       CTLTYPE_NODE, "curproc",
    807       1.78    atatat 		       SYSCTL_DESCR("Per-process settings"),
    808       1.74    atatat 		       NULL, 0, NULL, 0,
    809       1.74    atatat 		       CTL_PROC, PROC_CURPROC, CTL_EOL);
    810       1.74    atatat 
    811       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    812       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY2|CTLFLAG_ANYWRITE,
    813       1.78    atatat 		       CTLTYPE_STRING, "corename",
    814       1.78    atatat 		       SYSCTL_DESCR("Core file name"),
    815       1.74    atatat 		       sysctl_proc_corename, 0, NULL, MAXPATHLEN,
    816       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
    817       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    818       1.76    atatat 		       CTLFLAG_PERMANENT,
    819       1.78    atatat 		       CTLTYPE_NODE, "rlimit",
    820       1.78    atatat 		       SYSCTL_DESCR("Process limits"),
    821       1.74    atatat 		       NULL, 0, NULL, 0,
    822       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
    823       1.74    atatat 
    824       1.74    atatat #define create_proc_plimit(s, n) do {					\
    825       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
    826       1.76    atatat 		       CTLFLAG_PERMANENT,				\
    827       1.78    atatat 		       CTLTYPE_NODE, s,					\
    828       1.78    atatat 		       SYSCTL_DESCR("Process " s " limits"),		\
    829       1.74    atatat 		       NULL, 0, NULL, 0,				\
    830       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
    831       1.74    atatat 		       CTL_EOL);					\
    832       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
    833       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
    834       1.78    atatat 		       CTLTYPE_QUAD, "soft",				\
    835       1.78    atatat 		       SYSCTL_DESCR("Process soft " s " limit"),	\
    836       1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
    837       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
    838       1.74    atatat 		       PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL);		\
    839       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
    840       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
    841       1.78    atatat 		       CTLTYPE_QUAD, "hard",				\
    842       1.78    atatat 		       SYSCTL_DESCR("Process hard " s " limit"),	\
    843       1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
    844       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
    845       1.74    atatat 		       PROC_PID_LIMIT_TYPE_HARD, CTL_EOL);		\
    846       1.74    atatat 	} while (0/*CONSTCOND*/)
    847       1.74    atatat 
    848       1.74    atatat 	create_proc_plimit("cputime",		PROC_PID_LIMIT_CPU);
    849       1.74    atatat 	create_proc_plimit("filesize",		PROC_PID_LIMIT_FSIZE);
    850       1.74    atatat 	create_proc_plimit("datasize",		PROC_PID_LIMIT_DATA);
    851       1.74    atatat 	create_proc_plimit("stacksize",		PROC_PID_LIMIT_STACK);
    852       1.74    atatat 	create_proc_plimit("coredumpsize",	PROC_PID_LIMIT_CORE);
    853       1.74    atatat 	create_proc_plimit("memoryuse",		PROC_PID_LIMIT_RSS);
    854       1.74    atatat 	create_proc_plimit("memorylocked",	PROC_PID_LIMIT_MEMLOCK);
    855       1.74    atatat 	create_proc_plimit("maxproc",		PROC_PID_LIMIT_NPROC);
    856       1.74    atatat 	create_proc_plimit("descriptors",	PROC_PID_LIMIT_NOFILE);
    857       1.79  christos 	create_proc_plimit("sbsize",		PROC_PID_LIMIT_SBSIZE);
    858       1.74    atatat 
    859       1.74    atatat #undef create_proc_plimit
    860       1.74    atatat 
    861       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    862       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
    863       1.78    atatat 		       CTLTYPE_INT, "stopfork",
    864       1.78    atatat 		       SYSCTL_DESCR("Stop process at fork(2)"),
    865       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
    866       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
    867       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    868       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
    869       1.78    atatat 		       CTLTYPE_INT, "stopexec",
    870       1.78    atatat 		       SYSCTL_DESCR("Stop process at execve(2)"),
    871       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
    872       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
    873       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    874       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
    875       1.78    atatat 		       CTLTYPE_INT, "stopexit",
    876       1.78    atatat 		       SYSCTL_DESCR("Stop process before completing exit"),
    877       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
    878       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
    879       1.17       cgd }
    880       1.79  christos 
    881       1.88  christos struct uidinfo *
    882       1.88  christos uid_find(uid_t uid)
    883       1.79  christos {
    884       1.79  christos 	struct uidinfo *uip;
    885       1.90  christos 	struct uidinfo *newuip = NULL;
    886       1.79  christos 	struct uihashhead *uipp;
    887       1.79  christos 
    888       1.79  christos 	uipp = UIHASH(uid);
    889       1.79  christos 
    890       1.90  christos again:
    891       1.89  christos 	simple_lock(&uihashtbl_slock);
    892       1.79  christos 	LIST_FOREACH(uip, uipp, ui_hash)
    893       1.88  christos 		if (uip->ui_uid == uid) {
    894       1.88  christos 			simple_unlock(&uihashtbl_slock);
    895       1.90  christos 			if (newuip)
    896       1.90  christos 				free(newuip, M_PROC);
    897       1.79  christos 			return uip;
    898       1.88  christos 		}
    899       1.79  christos 
    900       1.90  christos 	if (newuip == NULL) {
    901       1.90  christos 		simple_unlock(&uihashtbl_slock);
    902       1.90  christos 		newuip = malloc(sizeof(*uip), M_PROC, M_WAITOK | M_ZERO);
    903       1.90  christos 		goto again;
    904       1.90  christos 	}
    905       1.90  christos 	uip = newuip;
    906       1.89  christos 
    907       1.79  christos 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    908       1.79  christos 	uip->ui_uid = uid;
    909       1.94  christos 	simple_lock_init(&uip->ui_slock);
    910       1.88  christos 	simple_unlock(&uihashtbl_slock);
    911       1.89  christos 
    912       1.79  christos 	return uip;
    913       1.79  christos }
    914       1.79  christos 
    915       1.79  christos /*
    916       1.79  christos  * Change the count associated with number of processes
    917       1.79  christos  * a given user is using.
    918       1.79  christos  */
    919       1.79  christos int
    920       1.79  christos chgproccnt(uid_t uid, int diff)
    921       1.79  christos {
    922       1.79  christos 	struct uidinfo *uip;
    923       1.96  christos 	int s;
    924       1.79  christos 
    925       1.79  christos 	if (diff == 0)
    926       1.79  christos 		return 0;
    927       1.79  christos 
    928       1.88  christos 	uip = uid_find(uid);
    929       1.96  christos 	UILOCK(uip, s);
    930       1.88  christos 	uip->ui_proccnt += diff;
    931       1.88  christos 	KASSERT(uip->ui_proccnt >= 0);
    932       1.96  christos 	UIUNLOCK(uip, s);
    933       1.88  christos 	return uip->ui_proccnt;
    934       1.79  christos }
    935       1.79  christos 
    936       1.79  christos int
    937       1.97  christos chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
    938       1.79  christos {
    939       1.79  christos 	rlim_t nsb;
    940       1.96  christos 	int s;
    941       1.79  christos 
    942       1.96  christos 	UILOCK(uip, s);
    943       1.80      yamt 	nsb = uip->ui_sbsize + to - *hiwat;
    944       1.97  christos 	if (to > *hiwat && nsb > xmax) {
    945       1.96  christos 		UIUNLOCK(uip, s);
    946       1.95  christos 		splx(s);
    947       1.88  christos 		return 0;
    948       1.94  christos 	}
    949       1.79  christos 	*hiwat = to;
    950       1.79  christos 	uip->ui_sbsize = nsb;
    951       1.79  christos 	KASSERT(uip->ui_sbsize >= 0);
    952       1.96  christos 	UIUNLOCK(uip, s);
    953       1.88  christos 	return 1;
    954       1.79  christos }
    955