Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.98.2.10
      1  1.98.2.10      yamt /*	$NetBSD: kern_resource.c,v 1.98.2.10 2008/02/27 08:36:55 yamt 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.98.2.10      yamt __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.98.2.10 2008/02/27 08:36:55 yamt 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.98.2.9      yamt #include <sys/kmem.h>
     49   1.98.2.1      yamt #include <sys/namei.h>
     50       1.49   thorpej #include <sys/pool.h>
     51       1.17       cgd #include <sys/proc.h>
     52       1.74    atatat #include <sys/sysctl.h>
     53   1.98.2.8      yamt #include <sys/timevar.h>
     54   1.98.2.1      yamt #include <sys/kauth.h>
     55   1.98.2.7      yamt #include <sys/atomic.h>
     56       1.22       cgd #include <sys/mount.h>
     57       1.22       cgd #include <sys/syscallargs.h>
     58       1.17       cgd 
     59       1.43       mrg #include <uvm/uvm_extern.h>
     60       1.43       mrg 
     61       1.17       cgd /*
     62       1.60       eeh  * Maximum process data and stack limits.
     63       1.60       eeh  * They are variables so they are patchable.
     64       1.60       eeh  */
     65       1.60       eeh rlim_t maxdmap = MAXDSIZ;
     66       1.60       eeh rlim_t maxsmap = MAXSSIZ;
     67       1.60       eeh 
     68       1.82      matt struct uihashhead *uihashtbl;
     69       1.82      matt u_long uihash;		/* size of hash table - 1 */
     70   1.98.2.4      yamt kmutex_t uihashtbl_lock;
     71       1.79  christos 
     72   1.98.2.8      yamt static pool_cache_t plimit_cache;
     73   1.98.2.8      yamt static pool_cache_t pstats_cache;
     74   1.98.2.8      yamt 
     75   1.98.2.8      yamt void
     76   1.98.2.8      yamt resource_init(void)
     77   1.98.2.8      yamt {
     78   1.98.2.8      yamt 
     79   1.98.2.8      yamt 	plimit_cache = pool_cache_init(sizeof(struct plimit), 0, 0, 0,
     80   1.98.2.8      yamt 	    "plimitpl", NULL, IPL_NONE, NULL, NULL, NULL);
     81   1.98.2.8      yamt 	pstats_cache = pool_cache_init(sizeof(struct pstats), 0, 0, 0,
     82   1.98.2.8      yamt 	    "pstatspl", NULL, IPL_NONE, NULL, NULL, NULL);
     83   1.98.2.8      yamt }
     84   1.98.2.8      yamt 
     85       1.60       eeh /*
     86       1.17       cgd  * Resource controls and accounting.
     87       1.17       cgd  */
     88       1.17       cgd 
     89       1.25       cgd int
     90   1.98.2.8      yamt sys_getpriority(struct lwp *l, const struct sys_getpriority_args *uap, register_t *retval)
     91       1.30   thorpej {
     92   1.98.2.8      yamt 	/* {
     93       1.22       cgd 		syscallarg(int) which;
     94       1.81    kleink 		syscallarg(id_t) who;
     95   1.98.2.8      yamt 	} */
     96       1.68   thorpej 	struct proc *curp = l->l_proc, *p;
     97       1.54  augustss 	int low = NZERO + PRIO_MAX + 1;
     98   1.98.2.3      yamt 	int who = SCARG(uap, who);
     99       1.17       cgd 
    100   1.98.2.4      yamt 	mutex_enter(&proclist_lock);
    101       1.22       cgd 	switch (SCARG(uap, which)) {
    102       1.17       cgd 	case PRIO_PROCESS:
    103   1.98.2.3      yamt 		if (who == 0)
    104       1.17       cgd 			p = curp;
    105       1.17       cgd 		else
    106   1.98.2.3      yamt 			p = p_find(who, PFIND_LOCKED);
    107   1.98.2.3      yamt 		if (p != NULL)
    108   1.98.2.3      yamt 			low = p->p_nice;
    109       1.17       cgd 		break;
    110       1.17       cgd 
    111       1.17       cgd 	case PRIO_PGRP: {
    112       1.54  augustss 		struct pgrp *pg;
    113       1.17       cgd 
    114   1.98.2.3      yamt 		if (who == 0)
    115       1.17       cgd 			pg = curp->p_pgrp;
    116   1.98.2.3      yamt 		else if ((pg = pg_find(who, PFIND_LOCKED)) == NULL)
    117       1.17       cgd 			break;
    118       1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    119       1.17       cgd 			if (p->p_nice < low)
    120       1.17       cgd 				low = p->p_nice;
    121       1.17       cgd 		}
    122       1.17       cgd 		break;
    123       1.17       cgd 	}
    124       1.17       cgd 
    125       1.17       cgd 	case PRIO_USER:
    126   1.98.2.3      yamt 		if (who == 0)
    127   1.98.2.3      yamt 			who = (int)kauth_cred_geteuid(l->l_cred);
    128       1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    129   1.98.2.3      yamt 			mutex_enter(&p->p_mutex);
    130   1.98.2.2      yamt 			if (kauth_cred_geteuid(p->p_cred) ==
    131   1.98.2.3      yamt 			    (uid_t)who && p->p_nice < low)
    132       1.17       cgd 				low = p->p_nice;
    133   1.98.2.3      yamt 			mutex_exit(&p->p_mutex);
    134       1.64      matt 		}
    135       1.17       cgd 		break;
    136       1.17       cgd 
    137       1.17       cgd 	default:
    138   1.98.2.4      yamt 		mutex_exit(&proclist_lock);
    139       1.17       cgd 		return (EINVAL);
    140       1.17       cgd 	}
    141   1.98.2.4      yamt 	mutex_exit(&proclist_lock);
    142   1.98.2.3      yamt 
    143       1.37        ws 	if (low == NZERO + PRIO_MAX + 1)
    144       1.17       cgd 		return (ESRCH);
    145       1.37        ws 	*retval = low - NZERO;
    146       1.17       cgd 	return (0);
    147       1.17       cgd }
    148       1.17       cgd 
    149       1.17       cgd /* ARGSUSED */
    150       1.25       cgd int
    151   1.98.2.8      yamt sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap, register_t *retval)
    152       1.30   thorpej {
    153   1.98.2.8      yamt 	/* {
    154       1.22       cgd 		syscallarg(int) which;
    155       1.81    kleink 		syscallarg(id_t) who;
    156       1.22       cgd 		syscallarg(int) prio;
    157   1.98.2.8      yamt 	} */
    158       1.68   thorpej 	struct proc *curp = l->l_proc, *p;
    159       1.17       cgd 	int found = 0, error = 0;
    160   1.98.2.3      yamt 	int who = SCARG(uap, who);
    161       1.17       cgd 
    162   1.98.2.4      yamt 	mutex_enter(&proclist_lock);
    163       1.22       cgd 	switch (SCARG(uap, which)) {
    164       1.17       cgd 	case PRIO_PROCESS:
    165   1.98.2.3      yamt 		if (who == 0)
    166       1.17       cgd 			p = curp;
    167       1.17       cgd 		else
    168   1.98.2.3      yamt 			p = p_find(who, PFIND_LOCKED);
    169   1.98.2.3      yamt 		if (p != 0) {
    170   1.98.2.3      yamt 			mutex_enter(&p->p_mutex);
    171   1.98.2.3      yamt 			error = donice(l, p, SCARG(uap, prio));
    172   1.98.2.3      yamt 			mutex_exit(&p->p_mutex);
    173   1.98.2.3      yamt 		}
    174       1.17       cgd 		found++;
    175       1.17       cgd 		break;
    176       1.17       cgd 
    177       1.17       cgd 	case PRIO_PGRP: {
    178       1.54  augustss 		struct pgrp *pg;
    179       1.87     perry 
    180   1.98.2.3      yamt 		if (who == 0)
    181       1.17       cgd 			pg = curp->p_pgrp;
    182   1.98.2.3      yamt 		else if ((pg = pg_find(who, PFIND_LOCKED)) == NULL)
    183       1.17       cgd 			break;
    184       1.64      matt 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    185   1.98.2.3      yamt 			mutex_enter(&p->p_mutex);
    186   1.98.2.2      yamt 			error = donice(l, p, SCARG(uap, prio));
    187   1.98.2.3      yamt 			mutex_exit(&p->p_mutex);
    188       1.17       cgd 			found++;
    189       1.17       cgd 		}
    190       1.17       cgd 		break;
    191       1.17       cgd 	}
    192       1.17       cgd 
    193       1.17       cgd 	case PRIO_USER:
    194   1.98.2.3      yamt 		if (who == 0)
    195   1.98.2.3      yamt 			who = (int)kauth_cred_geteuid(l->l_cred);
    196       1.86      yamt 		PROCLIST_FOREACH(p, &allproc) {
    197   1.98.2.3      yamt 			mutex_enter(&p->p_mutex);
    198   1.98.2.2      yamt 			if (kauth_cred_geteuid(p->p_cred) ==
    199   1.98.2.2      yamt 			    (uid_t)SCARG(uap, who)) {
    200   1.98.2.2      yamt 				error = donice(l, p, SCARG(uap, prio));
    201       1.17       cgd 				found++;
    202       1.17       cgd 			}
    203   1.98.2.3      yamt 			mutex_exit(&p->p_mutex);
    204       1.64      matt 		}
    205       1.17       cgd 		break;
    206       1.17       cgd 
    207       1.17       cgd 	default:
    208   1.98.2.3      yamt 		error = EINVAL;
    209   1.98.2.3      yamt 		break;
    210       1.17       cgd 	}
    211   1.98.2.4      yamt 	mutex_exit(&proclist_lock);
    212       1.17       cgd 	if (found == 0)
    213       1.17       cgd 		return (ESRCH);
    214       1.17       cgd 	return (error);
    215       1.17       cgd }
    216       1.17       cgd 
    217   1.98.2.3      yamt /*
    218   1.98.2.3      yamt  * Renice a process.
    219   1.98.2.3      yamt  *
    220   1.98.2.3      yamt  * Call with the target process' credentials locked.
    221   1.98.2.3      yamt  */
    222       1.25       cgd int
    223   1.98.2.2      yamt donice(struct lwp *l, struct proc *chgp, int n)
    224       1.17       cgd {
    225   1.98.2.2      yamt 	kauth_cred_t cred = l->l_cred;
    226   1.98.2.3      yamt 	int onice;
    227   1.98.2.3      yamt 
    228   1.98.2.4      yamt 	KASSERT(mutex_owned(&chgp->p_mutex));
    229       1.17       cgd 
    230       1.17       cgd 	if (n > PRIO_MAX)
    231       1.17       cgd 		n = PRIO_MAX;
    232       1.17       cgd 	if (n < PRIO_MIN)
    233       1.17       cgd 		n = PRIO_MIN;
    234       1.37        ws 	n += NZERO;
    235   1.98.2.3      yamt 	onice = chgp->p_nice;
    236   1.98.2.3      yamt 	onice = chgp->p_nice;
    237   1.98.2.3      yamt 
    238   1.98.2.3      yamt   again:
    239   1.98.2.3      yamt 	if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp,
    240   1.98.2.3      yamt 	    KAUTH_ARG(n), NULL, NULL))
    241       1.17       cgd 		return (EACCES);
    242   1.98.2.6      yamt 	mutex_spin_enter(&chgp->p_smutex);
    243   1.98.2.3      yamt 	if (onice != chgp->p_nice) {
    244   1.98.2.6      yamt 		mutex_spin_exit(&chgp->p_smutex);
    245   1.98.2.3      yamt 		goto again;
    246   1.98.2.3      yamt 	}
    247   1.98.2.4      yamt 	sched_nice(chgp, n);
    248   1.98.2.6      yamt 	mutex_spin_exit(&chgp->p_smutex);
    249       1.17       cgd 	return (0);
    250       1.17       cgd }
    251       1.17       cgd 
    252       1.17       cgd /* ARGSUSED */
    253       1.25       cgd int
    254   1.98.2.8      yamt sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap, register_t *retval)
    255       1.30   thorpej {
    256   1.98.2.8      yamt 	/* {
    257       1.42   mycroft 		syscallarg(int) which;
    258       1.39       cgd 		syscallarg(const struct rlimit *) rlp;
    259   1.98.2.8      yamt 	} */
    260       1.42   mycroft 	int which = SCARG(uap, which);
    261       1.19       cgd 	struct rlimit alim;
    262       1.17       cgd 	int error;
    263       1.17       cgd 
    264       1.46     perry 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    265       1.33  christos 	if (error)
    266       1.17       cgd 		return (error);
    267   1.98.2.2      yamt 	return (dosetrlimit(l, l->l_proc, which, &alim));
    268       1.17       cgd }
    269       1.17       cgd 
    270       1.17       cgd int
    271   1.98.2.2      yamt dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp)
    272       1.17       cgd {
    273       1.54  augustss 	struct rlimit *alimp;
    274       1.17       cgd 	int error;
    275       1.17       cgd 
    276       1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    277       1.17       cgd 		return (EINVAL);
    278       1.38  matthias 
    279       1.38  matthias 	if (limp->rlim_cur < 0 || limp->rlim_max < 0)
    280       1.38  matthias 		return (EINVAL);
    281       1.38  matthias 
    282       1.62  jdolecek 	if (limp->rlim_cur > limp->rlim_max) {
    283       1.62  jdolecek 		/*
    284       1.62  jdolecek 		 * This is programming error. According to SUSv2, we should
    285       1.62  jdolecek 		 * return error in this case.
    286       1.62  jdolecek 		 */
    287       1.62  jdolecek 		return (EINVAL);
    288       1.62  jdolecek 	}
    289   1.98.2.5      yamt 
    290   1.98.2.5      yamt 	alimp = &p->p_rlimit[which];
    291   1.98.2.5      yamt 	/* if we don't change the value, no need to limcopy() */
    292   1.98.2.5      yamt 	if (limp->rlim_cur == alimp->rlim_cur &&
    293   1.98.2.5      yamt 	    limp->rlim_max == alimp->rlim_max)
    294   1.98.2.5      yamt 		return 0;
    295   1.98.2.5      yamt 
    296   1.98.2.3      yamt 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
    297   1.98.2.9      yamt 	    p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which));
    298   1.98.2.2      yamt 	if (error)
    299   1.98.2.5      yamt 		return (error);
    300       1.62  jdolecek 
    301   1.98.2.5      yamt 	lim_privatise(p, false);
    302   1.98.2.5      yamt 	/* p->p_limit is now unchangeable */
    303   1.98.2.5      yamt 	alimp = &p->p_rlimit[which];
    304       1.17       cgd 
    305       1.17       cgd 	switch (which) {
    306       1.17       cgd 
    307       1.17       cgd 	case RLIMIT_DATA:
    308       1.19       cgd 		if (limp->rlim_cur > maxdmap)
    309       1.19       cgd 			limp->rlim_cur = maxdmap;
    310       1.19       cgd 		if (limp->rlim_max > maxdmap)
    311       1.19       cgd 			limp->rlim_max = maxdmap;
    312       1.17       cgd 		break;
    313       1.17       cgd 
    314       1.17       cgd 	case RLIMIT_STACK:
    315       1.19       cgd 		if (limp->rlim_cur > maxsmap)
    316       1.19       cgd 			limp->rlim_cur = maxsmap;
    317       1.19       cgd 		if (limp->rlim_max > maxsmap)
    318       1.19       cgd 			limp->rlim_max = maxsmap;
    319       1.62  jdolecek 
    320       1.62  jdolecek 		/*
    321       1.62  jdolecek 		 * Return EINVAL if the new stack size limit is lower than
    322       1.62  jdolecek 		 * current usage. Otherwise, the process would get SIGSEGV the
    323       1.62  jdolecek 		 * moment it would try to access anything on it's current stack.
    324       1.62  jdolecek 		 * This conforms to SUSv2.
    325       1.62  jdolecek 		 */
    326       1.62  jdolecek 		if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
    327   1.98.2.3      yamt 		    || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
    328       1.62  jdolecek 			return (EINVAL);
    329   1.98.2.3      yamt 		}
    330       1.40     enami 
    331       1.17       cgd 		/*
    332       1.40     enami 		 * Stack is allocated to the max at exec time with
    333       1.40     enami 		 * only "rlim_cur" bytes accessible (In other words,
    334       1.40     enami 		 * allocates stack dividing two contiguous regions at
    335       1.40     enami 		 * "rlim_cur" bytes boundary).
    336       1.40     enami 		 *
    337       1.40     enami 		 * Since allocation is done in terms of page, roundup
    338       1.40     enami 		 * "rlim_cur" (otherwise, contiguous regions
    339       1.40     enami 		 * overlap).  If stack limit is going up make more
    340       1.40     enami 		 * accessible, if going down make inaccessible.
    341       1.17       cgd 		 */
    342       1.40     enami 		limp->rlim_cur = round_page(limp->rlim_cur);
    343       1.17       cgd 		if (limp->rlim_cur != alimp->rlim_cur) {
    344       1.48       eeh 			vaddr_t addr;
    345       1.48       eeh 			vsize_t size;
    346       1.17       cgd 			vm_prot_t prot;
    347       1.17       cgd 
    348       1.17       cgd 			if (limp->rlim_cur > alimp->rlim_cur) {
    349       1.73       chs 				prot = VM_PROT_READ | VM_PROT_WRITE;
    350       1.17       cgd 				size = limp->rlim_cur - alimp->rlim_cur;
    351       1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    352       1.91      fvdl 				    limp->rlim_cur;
    353       1.17       cgd 			} else {
    354       1.17       cgd 				prot = VM_PROT_NONE;
    355       1.17       cgd 				size = alimp->rlim_cur - limp->rlim_cur;
    356       1.91      fvdl 				addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
    357       1.91      fvdl 				     alimp->rlim_cur;
    358       1.17       cgd 			}
    359       1.43       mrg 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    360   1.98.2.3      yamt 			    addr, addr+size, prot, false);
    361       1.17       cgd 		}
    362       1.17       cgd 		break;
    363       1.19       cgd 
    364       1.19       cgd 	case RLIMIT_NOFILE:
    365       1.19       cgd 		if (limp->rlim_cur > maxfiles)
    366       1.19       cgd 			limp->rlim_cur = maxfiles;
    367       1.19       cgd 		if (limp->rlim_max > maxfiles)
    368       1.19       cgd 			limp->rlim_max = maxfiles;
    369       1.19       cgd 		break;
    370       1.19       cgd 
    371       1.19       cgd 	case RLIMIT_NPROC:
    372       1.19       cgd 		if (limp->rlim_cur > maxproc)
    373       1.19       cgd 			limp->rlim_cur = maxproc;
    374       1.19       cgd 		if (limp->rlim_max > maxproc)
    375       1.19       cgd 			limp->rlim_max = maxproc;
    376       1.19       cgd 		break;
    377       1.17       cgd 	}
    378   1.98.2.5      yamt 
    379   1.98.2.5      yamt 	mutex_enter(&p->p_limit->pl_lock);
    380       1.17       cgd 	*alimp = *limp;
    381   1.98.2.5      yamt 	mutex_exit(&p->p_limit->pl_lock);
    382       1.17       cgd 	return (0);
    383       1.17       cgd }
    384       1.17       cgd 
    385       1.17       cgd /* ARGSUSED */
    386       1.25       cgd int
    387   1.98.2.8      yamt sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap, register_t *retval)
    388       1.30   thorpej {
    389   1.98.2.8      yamt 	/* {
    390       1.42   mycroft 		syscallarg(int) which;
    391       1.22       cgd 		syscallarg(struct rlimit *) rlp;
    392   1.98.2.8      yamt 	} */
    393       1.68   thorpej 	struct proc *p = l->l_proc;
    394       1.42   mycroft 	int which = SCARG(uap, which);
    395   1.98.2.4      yamt 	struct rlimit rl;
    396       1.17       cgd 
    397       1.67    itojun 	if ((u_int)which >= RLIM_NLIMITS)
    398       1.17       cgd 		return (EINVAL);
    399   1.98.2.4      yamt 
    400   1.98.2.4      yamt 	mutex_enter(&p->p_mutex);
    401   1.98.2.4      yamt 	memcpy(&rl, &p->p_rlimit[which], sizeof(rl));
    402   1.98.2.4      yamt 	mutex_exit(&p->p_mutex);
    403   1.98.2.4      yamt 
    404   1.98.2.4      yamt 	return copyout(&rl, SCARG(uap, rlp), sizeof(rl));
    405       1.17       cgd }
    406       1.17       cgd 
    407       1.17       cgd /*
    408       1.17       cgd  * Transform the running time and tick information in proc p into user,
    409       1.17       cgd  * system, and interrupt time usage.
    410   1.98.2.3      yamt  *
    411   1.98.2.3      yamt  * Should be called with p->p_smutex held unless called from exit1().
    412       1.17       cgd  */
    413       1.25       cgd void
    414       1.98   thorpej calcru(struct proc *p, struct timeval *up, struct timeval *sp,
    415   1.98.2.3      yamt     struct timeval *ip, struct timeval *rp)
    416       1.17       cgd {
    417   1.98.2.8      yamt 	uint64_t u, st, ut, it, tot;
    418       1.68   thorpej 	struct lwp *l;
    419   1.98.2.8      yamt 	struct bintime tm;
    420   1.98.2.8      yamt 	struct timeval tv;
    421       1.17       cgd 
    422   1.98.2.3      yamt 	mutex_spin_enter(&p->p_stmutex);
    423       1.17       cgd 	st = p->p_sticks;
    424       1.17       cgd 	ut = p->p_uticks;
    425       1.17       cgd 	it = p->p_iticks;
    426   1.98.2.3      yamt 	mutex_spin_exit(&p->p_stmutex);
    427       1.17       cgd 
    428   1.98.2.8      yamt 	tm = p->p_rtime;
    429   1.98.2.3      yamt 
    430       1.70       dsl 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    431   1.98.2.3      yamt 		lwp_lock(l);
    432   1.98.2.8      yamt 		bintime_add(&tm, &l->l_rtime);
    433   1.98.2.5      yamt 		if ((l->l_flag & LW_RUNNING) != 0) {
    434   1.98.2.8      yamt 			struct bintime diff;
    435       1.68   thorpej 			/*
    436       1.68   thorpej 			 * Adjust for the current time slice.  This is
    437       1.68   thorpej 			 * actually fairly important since the error
    438       1.68   thorpej 			 * here is on the order of a time quantum,
    439       1.68   thorpej 			 * which is much greater than the sampling
    440       1.87     perry 			 * error.
    441       1.68   thorpej 			 */
    442   1.98.2.8      yamt 			binuptime(&diff);
    443   1.98.2.8      yamt 			bintime_sub(&diff, &l->l_stime);
    444   1.98.2.8      yamt 			bintime_add(&tm, &diff);
    445       1.68   thorpej 		}
    446   1.98.2.3      yamt 		lwp_unlock(l);
    447       1.17       cgd 	}
    448       1.69       dsl 
    449       1.69       dsl 	tot = st + ut + it;
    450   1.98.2.8      yamt 	bintime2timeval(&tm, &tv);
    451   1.98.2.8      yamt 	u = (uint64_t)tv.tv_sec * 1000000ul + tv.tv_usec;
    452       1.70       dsl 
    453       1.69       dsl 	if (tot == 0) {
    454       1.69       dsl 		/* No ticks, so can't use to share time out, split 50-50 */
    455       1.70       dsl 		st = ut = u / 2;
    456       1.70       dsl 	} else {
    457       1.70       dsl 		st = (u * st) / tot;
    458       1.70       dsl 		ut = (u * ut) / tot;
    459       1.69       dsl 	}
    460   1.98.2.3      yamt 	if (sp != NULL) {
    461   1.98.2.3      yamt 		sp->tv_sec = st / 1000000;
    462   1.98.2.3      yamt 		sp->tv_usec = st % 1000000;
    463   1.98.2.3      yamt 	}
    464   1.98.2.3      yamt 	if (up != NULL) {
    465   1.98.2.3      yamt 		up->tv_sec = ut / 1000000;
    466   1.98.2.3      yamt 		up->tv_usec = ut % 1000000;
    467   1.98.2.3      yamt 	}
    468       1.17       cgd 	if (ip != NULL) {
    469       1.70       dsl 		if (it != 0)
    470       1.70       dsl 			it = (u * it) / tot;
    471       1.17       cgd 		ip->tv_sec = it / 1000000;
    472       1.17       cgd 		ip->tv_usec = it % 1000000;
    473       1.17       cgd 	}
    474   1.98.2.3      yamt 	if (rp != NULL) {
    475   1.98.2.8      yamt 		*rp = tv;
    476   1.98.2.3      yamt 	}
    477       1.17       cgd }
    478       1.17       cgd 
    479       1.17       cgd /* ARGSUSED */
    480       1.25       cgd int
    481   1.98.2.8      yamt sys_getrusage(struct lwp *l, const struct sys_getrusage_args *uap, register_t *retval)
    482       1.30   thorpej {
    483   1.98.2.8      yamt 	/* {
    484       1.22       cgd 		syscallarg(int) who;
    485       1.22       cgd 		syscallarg(struct rusage *) rusage;
    486   1.98.2.8      yamt 	} */
    487   1.98.2.4      yamt 	struct rusage ru;
    488       1.68   thorpej 	struct proc *p = l->l_proc;
    489       1.17       cgd 
    490       1.22       cgd 	switch (SCARG(uap, who)) {
    491       1.19       cgd 	case RUSAGE_SELF:
    492   1.98.2.3      yamt 		mutex_enter(&p->p_smutex);
    493   1.98.2.4      yamt 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
    494   1.98.2.4      yamt 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
    495   1.98.2.3      yamt 		mutex_exit(&p->p_smutex);
    496       1.17       cgd 		break;
    497       1.17       cgd 
    498       1.17       cgd 	case RUSAGE_CHILDREN:
    499   1.98.2.4      yamt 		mutex_enter(&p->p_smutex);
    500   1.98.2.4      yamt 		memcpy(&ru, &p->p_stats->p_cru, sizeof(ru));
    501   1.98.2.4      yamt 		mutex_exit(&p->p_smutex);
    502       1.17       cgd 		break;
    503       1.17       cgd 
    504       1.17       cgd 	default:
    505   1.98.2.4      yamt 		return EINVAL;
    506       1.17       cgd 	}
    507   1.98.2.4      yamt 
    508   1.98.2.4      yamt 	return copyout(&ru, SCARG(uap, rusage), sizeof(ru));
    509       1.17       cgd }
    510       1.17       cgd 
    511       1.25       cgd void
    512       1.98   thorpej ruadd(struct rusage *ru, struct rusage *ru2)
    513       1.17       cgd {
    514       1.54  augustss 	long *ip, *ip2;
    515       1.54  augustss 	int i;
    516       1.17       cgd 
    517       1.27   mycroft 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    518       1.27   mycroft 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    519       1.17       cgd 	if (ru->ru_maxrss < ru2->ru_maxrss)
    520       1.17       cgd 		ru->ru_maxrss = ru2->ru_maxrss;
    521       1.17       cgd 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    522       1.17       cgd 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    523       1.17       cgd 		*ip++ += *ip2++;
    524       1.17       cgd }
    525       1.17       cgd 
    526       1.17       cgd /*
    527       1.17       cgd  * Make a copy of the plimit structure.
    528       1.17       cgd  * We share these structures copy-on-write after fork,
    529       1.17       cgd  * and copy when a limit is changed.
    530   1.98.2.3      yamt  *
    531   1.98.2.5      yamt  * Unfortunately (due to PL_SHAREMOD) it is possibly for the structure
    532   1.98.2.5      yamt  * we are copying to change beneath our feet!
    533       1.17       cgd  */
    534       1.17       cgd struct plimit *
    535   1.98.2.5      yamt lim_copy(struct plimit *lim)
    536       1.17       cgd {
    537   1.98.2.5      yamt 	struct plimit *newlim;
    538   1.98.2.3      yamt 	char *corename;
    539   1.98.2.5      yamt 	size_t alen, len;
    540       1.83        pk 
    541   1.98.2.8      yamt 	newlim = pool_cache_get(plimit_cache, PR_WAITOK);
    542   1.98.2.5      yamt 	mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE);
    543   1.98.2.5      yamt 	newlim->pl_flags = 0;
    544   1.98.2.5      yamt 	newlim->pl_refcnt = 1;
    545   1.98.2.5      yamt 	newlim->pl_sv_limit = NULL;
    546   1.98.2.5      yamt 
    547   1.98.2.5      yamt 	mutex_enter(&lim->pl_lock);
    548   1.98.2.5      yamt 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    549   1.98.2.5      yamt 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    550       1.83        pk 
    551   1.98.2.5      yamt 	alen = 0;
    552   1.98.2.5      yamt 	corename = NULL;
    553   1.98.2.3      yamt 	for (;;) {
    554   1.98.2.5      yamt 		if (lim->pl_corename == defcorename) {
    555   1.98.2.5      yamt 			newlim->pl_corename = defcorename;
    556   1.98.2.5      yamt 			break;
    557   1.98.2.5      yamt 		}
    558   1.98.2.5      yamt 		len = strlen(lim->pl_corename) + 1;
    559   1.98.2.5      yamt 		if (len <= alen) {
    560   1.98.2.5      yamt 			newlim->pl_corename = corename;
    561   1.98.2.5      yamt 			memcpy(corename, lim->pl_corename, len);
    562   1.98.2.5      yamt 			corename = NULL;
    563   1.98.2.5      yamt 			break;
    564   1.98.2.5      yamt 		}
    565   1.98.2.5      yamt 		mutex_exit(&lim->pl_lock);
    566   1.98.2.5      yamt 		if (corename != NULL)
    567   1.98.2.5      yamt 			free(corename, M_TEMP);
    568   1.98.2.5      yamt 		alen = len;
    569   1.98.2.5      yamt 		corename = malloc(alen, M_TEMP, M_WAITOK);
    570   1.98.2.5      yamt 		mutex_enter(&lim->pl_lock);
    571   1.98.2.5      yamt 	}
    572   1.98.2.5      yamt 	mutex_exit(&lim->pl_lock);
    573   1.98.2.5      yamt 	if (corename != NULL)
    574   1.98.2.5      yamt 		free(corename, M_TEMP);
    575   1.98.2.5      yamt 	return newlim;
    576   1.98.2.5      yamt }
    577   1.98.2.3      yamt 
    578   1.98.2.5      yamt void
    579   1.98.2.5      yamt lim_addref(struct plimit *lim)
    580   1.98.2.5      yamt {
    581   1.98.2.7      yamt 	atomic_inc_uint(&lim->pl_refcnt);
    582   1.98.2.5      yamt }
    583   1.98.2.3      yamt 
    584   1.98.2.5      yamt /*
    585   1.98.2.5      yamt  * Give a process it's own private plimit structure.
    586   1.98.2.5      yamt  * This will only be shared (in fork) if modifications are to be shared.
    587   1.98.2.5      yamt  */
    588   1.98.2.5      yamt void
    589   1.98.2.5      yamt lim_privatise(struct proc *p, bool set_shared)
    590   1.98.2.5      yamt {
    591   1.98.2.5      yamt 	struct plimit *lim, *newlim;
    592   1.98.2.5      yamt 
    593   1.98.2.5      yamt 	lim = p->p_limit;
    594   1.98.2.5      yamt 	if (lim->pl_flags & PL_WRITEABLE) {
    595   1.98.2.5      yamt 		if (set_shared)
    596   1.98.2.5      yamt 			lim->pl_flags |= PL_SHAREMOD;
    597   1.98.2.5      yamt 		return;
    598   1.98.2.3      yamt 	}
    599       1.83        pk 
    600   1.98.2.5      yamt 	if (set_shared && lim->pl_flags & PL_SHAREMOD)
    601   1.98.2.5      yamt 		return;
    602   1.98.2.5      yamt 
    603   1.98.2.5      yamt 	newlim = lim_copy(lim);
    604   1.98.2.5      yamt 
    605   1.98.2.5      yamt 	mutex_enter(&p->p_mutex);
    606   1.98.2.5      yamt 	if (p->p_limit->pl_flags & PL_WRITEABLE) {
    607   1.98.2.5      yamt 		/* Someone crept in while we were busy */
    608   1.98.2.5      yamt 		mutex_exit(&p->p_mutex);
    609   1.98.2.5      yamt 		limfree(newlim);
    610   1.98.2.5      yamt 		if (set_shared)
    611   1.98.2.5      yamt 			p->p_limit->pl_flags |= PL_SHAREMOD;
    612   1.98.2.5      yamt 		return;
    613   1.98.2.5      yamt 	}
    614   1.98.2.5      yamt 
    615   1.98.2.5      yamt 	/*
    616   1.98.2.5      yamt 	 * Since most accesses to p->p_limit aren't locked, we must not
    617   1.98.2.5      yamt 	 * delete the old limit structure yet.
    618   1.98.2.5      yamt 	 */
    619   1.98.2.5      yamt 	newlim->pl_sv_limit = p->p_limit;
    620   1.98.2.5      yamt 	newlim->pl_flags |= PL_WRITEABLE;
    621   1.98.2.5      yamt 	if (set_shared)
    622   1.98.2.5      yamt 		newlim->pl_flags |= PL_SHAREMOD;
    623   1.98.2.5      yamt 	p->p_limit = newlim;
    624   1.98.2.5      yamt 	mutex_exit(&p->p_mutex);
    625       1.32   mycroft }
    626       1.32   mycroft 
    627       1.32   mycroft void
    628       1.98   thorpej limfree(struct plimit *lim)
    629       1.32   mycroft {
    630   1.98.2.5      yamt 	struct plimit *sv_lim;
    631       1.85    kleink 
    632   1.98.2.5      yamt 	do {
    633   1.98.2.7      yamt 		if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0)
    634   1.98.2.5      yamt 			return;
    635   1.98.2.5      yamt 		if (lim->pl_corename != defcorename)
    636   1.98.2.5      yamt 			free(lim->pl_corename, M_TEMP);
    637   1.98.2.5      yamt 		sv_lim = lim->pl_sv_limit;
    638   1.98.2.5      yamt 		mutex_destroy(&lim->pl_lock);
    639   1.98.2.8      yamt 		pool_cache_put(plimit_cache, lim);
    640   1.98.2.5      yamt 	} while ((lim = sv_lim) != NULL);
    641       1.68   thorpej }
    642       1.68   thorpej 
    643       1.68   thorpej struct pstats *
    644       1.98   thorpej pstatscopy(struct pstats *ps)
    645       1.68   thorpej {
    646       1.87     perry 
    647       1.68   thorpej 	struct pstats *newps;
    648       1.68   thorpej 
    649   1.98.2.8      yamt 	newps = pool_cache_get(pstats_cache, PR_WAITOK);
    650       1.68   thorpej 
    651       1.68   thorpej 	memset(&newps->pstat_startzero, 0,
    652   1.98.2.4      yamt 	(unsigned) ((char *)&newps->pstat_endzero -
    653   1.98.2.4      yamt 		    (char *)&newps->pstat_startzero));
    654       1.68   thorpej 	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
    655   1.98.2.4      yamt 	((char *)&newps->pstat_endcopy -
    656   1.98.2.4      yamt 	 (char *)&newps->pstat_startcopy));
    657       1.68   thorpej 
    658       1.68   thorpej 	return (newps);
    659       1.68   thorpej 
    660       1.68   thorpej }
    661       1.68   thorpej 
    662       1.68   thorpej void
    663       1.98   thorpej pstatsfree(struct pstats *ps)
    664       1.68   thorpej {
    665       1.68   thorpej 
    666   1.98.2.8      yamt 	pool_cache_put(pstats_cache, ps);
    667       1.74    atatat }
    668       1.74    atatat 
    669       1.74    atatat /*
    670       1.74    atatat  * sysctl interface in five parts
    671       1.74    atatat  */
    672       1.74    atatat 
    673       1.74    atatat /*
    674       1.74    atatat  * a routine for sysctl proc subtree helpers that need to pick a valid
    675       1.74    atatat  * process by pid.
    676       1.74    atatat  */
    677       1.74    atatat static int
    678   1.98.2.2      yamt sysctl_proc_findproc(struct lwp *l, struct proc **p2, pid_t pid)
    679       1.74    atatat {
    680       1.74    atatat 	struct proc *ptmp;
    681   1.98.2.1      yamt 	int error = 0;
    682       1.74    atatat 
    683       1.74    atatat 	if (pid == PROC_CURPROC)
    684   1.98.2.2      yamt 		ptmp = l->l_proc;
    685       1.74    atatat 	else if ((ptmp = pfind(pid)) == NULL)
    686       1.74    atatat 		error = ESRCH;
    687       1.74    atatat 
    688       1.74    atatat 	*p2 = ptmp;
    689       1.74    atatat 	return (error);
    690       1.74    atatat }
    691       1.74    atatat 
    692       1.74    atatat /*
    693       1.74    atatat  * sysctl helper routine for setting a process's specific corefile
    694       1.74    atatat  * name.  picks the process based on the given pid and checks the
    695       1.74    atatat  * correctness of the new value.
    696       1.74    atatat  */
    697       1.74    atatat static int
    698       1.74    atatat sysctl_proc_corename(SYSCTLFN_ARGS)
    699       1.74    atatat {
    700   1.98.2.2      yamt 	struct proc *ptmp;
    701       1.83        pk 	struct plimit *lim;
    702       1.74    atatat 	int error = 0, len;
    703   1.98.2.1      yamt 	char *cname;
    704   1.98.2.5      yamt 	char *ocore;
    705   1.98.2.1      yamt 	char *tmp;
    706       1.74    atatat 	struct sysctlnode node;
    707       1.74    atatat 
    708       1.74    atatat 	/*
    709       1.74    atatat 	 * is this all correct?
    710       1.74    atatat 	 */
    711       1.74    atatat 	if (namelen != 0)
    712       1.74    atatat 		return (EINVAL);
    713       1.74    atatat 	if (name[-1] != PROC_PID_CORENAME)
    714       1.74    atatat 		return (EINVAL);
    715       1.74    atatat 
    716       1.74    atatat 	/*
    717       1.74    atatat 	 * whom are we tweaking?
    718       1.74    atatat 	 */
    719   1.98.2.2      yamt 	error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]);
    720   1.98.2.2      yamt 	if (error)
    721   1.98.2.2      yamt 		return (error);
    722   1.98.2.2      yamt 
    723   1.98.2.9      yamt 	/* XXX-elad */
    724   1.98.2.9      yamt 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    725   1.98.2.9      yamt 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    726       1.74    atatat 	if (error)
    727       1.74    atatat 		return (error);
    728       1.74    atatat 
    729   1.98.2.9      yamt 	if (newp == NULL) {
    730   1.98.2.9      yamt 		error = kauth_authorize_process(l->l_cred,
    731   1.98.2.9      yamt 		    KAUTH_PROCESS_CORENAME, ptmp,
    732   1.98.2.9      yamt 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
    733   1.98.2.9      yamt 		if (error)
    734   1.98.2.9      yamt 			return (error);
    735   1.98.2.9      yamt 	}
    736   1.98.2.9      yamt 
    737       1.74    atatat 	/*
    738       1.74    atatat 	 * let them modify a temporary copy of the core name
    739       1.74    atatat 	 */
    740   1.98.2.5      yamt 	cname = PNBUF_GET();
    741   1.98.2.5      yamt 	lim = ptmp->p_limit;
    742   1.98.2.5      yamt 	mutex_enter(&lim->pl_lock);
    743   1.98.2.5      yamt 	strlcpy(cname, lim->pl_corename, MAXPATHLEN);
    744   1.98.2.5      yamt 	mutex_exit(&lim->pl_lock);
    745   1.98.2.5      yamt 
    746       1.74    atatat 	node = *rnode;
    747       1.74    atatat 	node.sysctl_data = cname;
    748       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    749       1.74    atatat 
    750       1.74    atatat 	/*
    751       1.74    atatat 	 * if that failed, or they have nothing new to say, or we've
    752       1.74    atatat 	 * heard it before...
    753       1.74    atatat 	 */
    754   1.98.2.5      yamt 	if (error || newp == NULL)
    755   1.98.2.5      yamt 		goto done;
    756   1.98.2.5      yamt 	lim = ptmp->p_limit;
    757   1.98.2.5      yamt 	mutex_enter(&lim->pl_lock);
    758   1.98.2.5      yamt 	error = strcmp(cname, lim->pl_corename);
    759   1.98.2.5      yamt 	mutex_exit(&lim->pl_lock);
    760   1.98.2.5      yamt 	if (error == 0)
    761   1.98.2.5      yamt 		/* Unchanged */
    762   1.98.2.1      yamt 		goto done;
    763       1.74    atatat 
    764   1.98.2.2      yamt 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CORENAME,
    765   1.98.2.9      yamt 	    ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cname, NULL);
    766   1.98.2.2      yamt 	if (error)
    767   1.98.2.2      yamt 		return (error);
    768   1.98.2.2      yamt 
    769       1.74    atatat 	/*
    770       1.74    atatat 	 * no error yet and cname now has the new core name in it.
    771       1.74    atatat 	 * let's see if it looks acceptable.  it must be either "core"
    772       1.74    atatat 	 * or end in ".core" or "/core".
    773       1.74    atatat 	 */
    774       1.74    atatat 	len = strlen(cname);
    775   1.98.2.1      yamt 	if (len < 4) {
    776   1.98.2.1      yamt 		error = EINVAL;
    777   1.98.2.1      yamt 	} else if (strcmp(cname + len - 4, "core") != 0) {
    778   1.98.2.1      yamt 		error = EINVAL;
    779   1.98.2.1      yamt 	} else if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.') {
    780   1.98.2.1      yamt 		error = EINVAL;
    781   1.98.2.1      yamt 	}
    782   1.98.2.1      yamt 	if (error != 0) {
    783   1.98.2.1      yamt 		goto done;
    784   1.98.2.1      yamt 	}
    785       1.74    atatat 
    786       1.74    atatat 	/*
    787       1.74    atatat 	 * hmm...looks good.  now...where do we put it?
    788       1.74    atatat 	 */
    789       1.74    atatat 	tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
    790   1.98.2.1      yamt 	if (tmp == NULL) {
    791   1.98.2.1      yamt 		error = ENOMEM;
    792   1.98.2.1      yamt 		goto done;
    793   1.98.2.1      yamt 	}
    794   1.98.2.5      yamt 	memcpy(tmp, cname, len + 1);
    795       1.74    atatat 
    796   1.98.2.5      yamt 	lim_privatise(ptmp, false);
    797       1.83        pk 	lim = ptmp->p_limit;
    798   1.98.2.5      yamt 	mutex_enter(&lim->pl_lock);
    799   1.98.2.5      yamt 	ocore = lim->pl_corename;
    800       1.83        pk 	lim->pl_corename = tmp;
    801   1.98.2.5      yamt 	mutex_exit(&lim->pl_lock);
    802   1.98.2.5      yamt 	if (ocore != defcorename)
    803   1.98.2.5      yamt 		free(ocore, M_TEMP);
    804   1.98.2.5      yamt 
    805   1.98.2.1      yamt done:
    806   1.98.2.1      yamt 	PNBUF_PUT(cname);
    807   1.98.2.1      yamt 	return error;
    808       1.74    atatat }
    809       1.74    atatat 
    810       1.74    atatat /*
    811       1.74    atatat  * sysctl helper routine for checking/setting a process's stop flags,
    812       1.74    atatat  * one for fork and one for exec.
    813       1.74    atatat  */
    814       1.74    atatat static int
    815       1.74    atatat sysctl_proc_stop(SYSCTLFN_ARGS)
    816       1.74    atatat {
    817   1.98.2.2      yamt 	struct proc *ptmp;
    818       1.74    atatat 	int i, f, error = 0;
    819       1.74    atatat 	struct sysctlnode node;
    820       1.74    atatat 
    821       1.74    atatat 	if (namelen != 0)
    822       1.74    atatat 		return (EINVAL);
    823       1.74    atatat 
    824   1.98.2.2      yamt 	error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]);
    825   1.98.2.2      yamt 	if (error)
    826   1.98.2.2      yamt 		return (error);
    827   1.98.2.2      yamt 
    828   1.98.2.9      yamt 	/* XXX-elad */
    829   1.98.2.9      yamt 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    830   1.98.2.9      yamt 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    831       1.74    atatat 	if (error)
    832       1.74    atatat 		return (error);
    833       1.74    atatat 
    834       1.74    atatat 	switch (rnode->sysctl_num) {
    835       1.74    atatat 	case PROC_PID_STOPFORK:
    836   1.98.2.3      yamt 		f = PS_STOPFORK;
    837       1.74    atatat 		break;
    838       1.74    atatat 	case PROC_PID_STOPEXEC:
    839   1.98.2.3      yamt 		f = PS_STOPEXEC;
    840       1.74    atatat 		break;
    841       1.74    atatat 	case PROC_PID_STOPEXIT:
    842   1.98.2.3      yamt 		f = PS_STOPEXIT;
    843       1.74    atatat 		break;
    844       1.74    atatat 	default:
    845       1.74    atatat 		return (EINVAL);
    846       1.74    atatat 	}
    847       1.74    atatat 
    848       1.74    atatat 	i = (ptmp->p_flag & f) ? 1 : 0;
    849       1.74    atatat 	node = *rnode;
    850       1.74    atatat 	node.sysctl_data = &i;
    851       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    852       1.74    atatat 	if (error || newp == NULL)
    853       1.74    atatat 		return (error);
    854       1.74    atatat 
    855   1.98.2.3      yamt 	mutex_enter(&ptmp->p_smutex);
    856   1.98.2.2      yamt 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
    857   1.98.2.2      yamt 	    ptmp, KAUTH_ARG(f), NULL, NULL);
    858   1.98.2.2      yamt 	if (error)
    859   1.98.2.2      yamt 		return (error);
    860       1.74    atatat 	if (i)
    861   1.98.2.3      yamt 		ptmp->p_sflag |= f;
    862       1.74    atatat 	else
    863   1.98.2.3      yamt 		ptmp->p_sflag &= ~f;
    864   1.98.2.3      yamt 	mutex_exit(&ptmp->p_smutex);
    865       1.74    atatat 
    866       1.74    atatat 	return (0);
    867       1.74    atatat }
    868       1.74    atatat 
    869       1.74    atatat /*
    870       1.74    atatat  * sysctl helper routine for a process's rlimits as exposed by sysctl.
    871       1.74    atatat  */
    872       1.74    atatat static int
    873       1.74    atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
    874       1.74    atatat {
    875   1.98.2.2      yamt 	struct proc *ptmp;
    876       1.74    atatat 	u_int limitno;
    877       1.74    atatat 	int which, error = 0;
    878       1.74    atatat         struct rlimit alim;
    879       1.74    atatat 	struct sysctlnode node;
    880       1.74    atatat 
    881       1.74    atatat 	if (namelen != 0)
    882       1.74    atatat 		return (EINVAL);
    883       1.74    atatat 
    884       1.74    atatat 	which = name[-1];
    885       1.74    atatat 	if (which != PROC_PID_LIMIT_TYPE_SOFT &&
    886       1.74    atatat 	    which != PROC_PID_LIMIT_TYPE_HARD)
    887       1.74    atatat 		return (EINVAL);
    888       1.74    atatat 
    889       1.74    atatat 	limitno = name[-2] - 1;
    890       1.74    atatat 	if (limitno >= RLIM_NLIMITS)
    891       1.74    atatat 		return (EINVAL);
    892       1.74    atatat 
    893       1.74    atatat 	if (name[-3] != PROC_PID_LIMIT)
    894       1.74    atatat 		return (EINVAL);
    895       1.74    atatat 
    896   1.98.2.2      yamt 	error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-4]);
    897   1.98.2.2      yamt 	if (error)
    898   1.98.2.2      yamt 		return (error);
    899   1.98.2.2      yamt 
    900   1.98.2.9      yamt 	/* XXX-elad */
    901   1.98.2.9      yamt 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
    902   1.98.2.9      yamt 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    903       1.74    atatat 	if (error)
    904       1.74    atatat 		return (error);
    905       1.74    atatat 
    906   1.98.2.9      yamt 	/* Check if we can view limits. */
    907   1.98.2.9      yamt 	if (newp == NULL) {
    908   1.98.2.9      yamt 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
    909   1.98.2.9      yamt 		    ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
    910   1.98.2.9      yamt 		    KAUTH_ARG(which));
    911   1.98.2.9      yamt 		if (error)
    912   1.98.2.9      yamt 			return (error);
    913   1.98.2.9      yamt 	}
    914   1.98.2.9      yamt 
    915       1.74    atatat 	node = *rnode;
    916       1.74    atatat 	memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
    917       1.74    atatat 	if (which == PROC_PID_LIMIT_TYPE_HARD)
    918       1.74    atatat 		node.sysctl_data = &alim.rlim_max;
    919       1.74    atatat 	else
    920       1.74    atatat 		node.sysctl_data = &alim.rlim_cur;
    921       1.74    atatat 
    922       1.74    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    923       1.74    atatat 	if (error || newp == NULL)
    924       1.74    atatat 		return (error);
    925       1.74    atatat 
    926   1.98.2.2      yamt 	return (dosetrlimit(l, ptmp, limitno, &alim));
    927       1.74    atatat }
    928       1.74    atatat 
    929       1.74    atatat /*
    930       1.74    atatat  * and finally, the actually glue that sticks it to the tree
    931       1.74    atatat  */
    932       1.74    atatat SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
    933       1.74    atatat {
    934       1.74    atatat 
    935       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    936       1.76    atatat 		       CTLFLAG_PERMANENT,
    937       1.74    atatat 		       CTLTYPE_NODE, "proc", NULL,
    938       1.74    atatat 		       NULL, 0, NULL, 0,
    939       1.74    atatat 		       CTL_PROC, CTL_EOL);
    940       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    941       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
    942       1.78    atatat 		       CTLTYPE_NODE, "curproc",
    943       1.78    atatat 		       SYSCTL_DESCR("Per-process settings"),
    944       1.74    atatat 		       NULL, 0, NULL, 0,
    945       1.74    atatat 		       CTL_PROC, PROC_CURPROC, CTL_EOL);
    946       1.74    atatat 
    947       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    948   1.98.2.2      yamt 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
    949       1.78    atatat 		       CTLTYPE_STRING, "corename",
    950       1.78    atatat 		       SYSCTL_DESCR("Core file name"),
    951       1.74    atatat 		       sysctl_proc_corename, 0, NULL, MAXPATHLEN,
    952       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
    953       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    954       1.76    atatat 		       CTLFLAG_PERMANENT,
    955       1.78    atatat 		       CTLTYPE_NODE, "rlimit",
    956       1.78    atatat 		       SYSCTL_DESCR("Process limits"),
    957       1.74    atatat 		       NULL, 0, NULL, 0,
    958       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
    959       1.74    atatat 
    960       1.74    atatat #define create_proc_plimit(s, n) do {					\
    961       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
    962       1.76    atatat 		       CTLFLAG_PERMANENT,				\
    963       1.78    atatat 		       CTLTYPE_NODE, s,					\
    964       1.78    atatat 		       SYSCTL_DESCR("Process " s " limits"),		\
    965       1.74    atatat 		       NULL, 0, NULL, 0,				\
    966       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
    967       1.74    atatat 		       CTL_EOL);					\
    968       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
    969       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
    970       1.78    atatat 		       CTLTYPE_QUAD, "soft",				\
    971       1.78    atatat 		       SYSCTL_DESCR("Process soft " s " limit"),	\
    972       1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
    973       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
    974       1.74    atatat 		       PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL);		\
    975       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,				\
    976       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
    977       1.78    atatat 		       CTLTYPE_QUAD, "hard",				\
    978       1.78    atatat 		       SYSCTL_DESCR("Process hard " s " limit"),	\
    979       1.74    atatat 		       sysctl_proc_plimit, 0, NULL, 0,			\
    980       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n,	\
    981       1.74    atatat 		       PROC_PID_LIMIT_TYPE_HARD, CTL_EOL);		\
    982       1.74    atatat 	} while (0/*CONSTCOND*/)
    983       1.74    atatat 
    984       1.74    atatat 	create_proc_plimit("cputime",		PROC_PID_LIMIT_CPU);
    985       1.74    atatat 	create_proc_plimit("filesize",		PROC_PID_LIMIT_FSIZE);
    986       1.74    atatat 	create_proc_plimit("datasize",		PROC_PID_LIMIT_DATA);
    987       1.74    atatat 	create_proc_plimit("stacksize",		PROC_PID_LIMIT_STACK);
    988       1.74    atatat 	create_proc_plimit("coredumpsize",	PROC_PID_LIMIT_CORE);
    989       1.74    atatat 	create_proc_plimit("memoryuse",		PROC_PID_LIMIT_RSS);
    990       1.74    atatat 	create_proc_plimit("memorylocked",	PROC_PID_LIMIT_MEMLOCK);
    991       1.74    atatat 	create_proc_plimit("maxproc",		PROC_PID_LIMIT_NPROC);
    992       1.74    atatat 	create_proc_plimit("descriptors",	PROC_PID_LIMIT_NOFILE);
    993       1.79  christos 	create_proc_plimit("sbsize",		PROC_PID_LIMIT_SBSIZE);
    994       1.74    atatat 
    995       1.74    atatat #undef create_proc_plimit
    996       1.74    atatat 
    997       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    998       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
    999       1.78    atatat 		       CTLTYPE_INT, "stopfork",
   1000       1.78    atatat 		       SYSCTL_DESCR("Stop process at fork(2)"),
   1001       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1002       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
   1003       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1004       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1005       1.78    atatat 		       CTLTYPE_INT, "stopexec",
   1006       1.78    atatat 		       SYSCTL_DESCR("Stop process at execve(2)"),
   1007       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1008       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
   1009       1.76    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1010       1.76    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
   1011       1.78    atatat 		       CTLTYPE_INT, "stopexit",
   1012       1.78    atatat 		       SYSCTL_DESCR("Stop process before completing exit"),
   1013       1.74    atatat 		       sysctl_proc_stop, 0, NULL, 0,
   1014       1.74    atatat 		       CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
   1015       1.17       cgd }
   1016       1.79  christos 
   1017   1.98.2.4      yamt void
   1018   1.98.2.4      yamt uid_init(void)
   1019   1.98.2.4      yamt {
   1020   1.98.2.4      yamt 
   1021   1.98.2.4      yamt 	/*
   1022   1.98.2.4      yamt 	 * XXXSMP This could be at IPL_SOFTNET, but for now we want
   1023   1.98.2.4      yamt 	 * to to be deadlock free, so it must be at IPL_VM.
   1024   1.98.2.4      yamt 	 */
   1025   1.98.2.7      yamt 	mutex_init(&uihashtbl_lock, MUTEX_DEFAULT, IPL_VM);
   1026   1.98.2.4      yamt 
   1027   1.98.2.4      yamt 	/*
   1028   1.98.2.4      yamt 	 * Ensure that uid 0 is always in the user hash table, as
   1029   1.98.2.4      yamt 	 * sbreserve() expects it available from interrupt context.
   1030   1.98.2.4      yamt 	 */
   1031   1.98.2.4      yamt 	(void)uid_find(0);
   1032   1.98.2.4      yamt }
   1033   1.98.2.4      yamt 
   1034       1.88  christos struct uidinfo *
   1035       1.88  christos uid_find(uid_t uid)
   1036       1.79  christos {
   1037       1.79  christos 	struct uidinfo *uip;
   1038       1.90  christos 	struct uidinfo *newuip = NULL;
   1039       1.79  christos 	struct uihashhead *uipp;
   1040       1.79  christos 
   1041       1.79  christos 	uipp = UIHASH(uid);
   1042       1.79  christos 
   1043       1.90  christos again:
   1044   1.98.2.4      yamt 	mutex_enter(&uihashtbl_lock);
   1045       1.79  christos 	LIST_FOREACH(uip, uipp, ui_hash)
   1046       1.88  christos 		if (uip->ui_uid == uid) {
   1047   1.98.2.4      yamt 			mutex_exit(&uihashtbl_lock);
   1048   1.98.2.4      yamt 			if (newuip) {
   1049   1.98.2.4      yamt 				mutex_destroy(&newuip->ui_lock);
   1050   1.98.2.9      yamt 				kmem_free(newuip, sizeof(*newuip));
   1051   1.98.2.4      yamt 			}
   1052       1.79  christos 			return uip;
   1053       1.88  christos 		}
   1054       1.90  christos 	if (newuip == NULL) {
   1055   1.98.2.4      yamt 		mutex_exit(&uihashtbl_lock);
   1056   1.98.2.4      yamt 		/* Must not be called from interrupt context. */
   1057   1.98.2.9      yamt 		newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP);
   1058   1.98.2.5      yamt 		/* XXX this could be IPL_SOFTNET */
   1059   1.98.2.7      yamt 		mutex_init(&newuip->ui_lock, MUTEX_DEFAULT, IPL_VM);
   1060       1.90  christos 		goto again;
   1061       1.90  christos 	}
   1062       1.90  christos 	uip = newuip;
   1063       1.89  christos 
   1064       1.79  christos 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
   1065       1.79  christos 	uip->ui_uid = uid;
   1066   1.98.2.4      yamt 	mutex_exit(&uihashtbl_lock);
   1067       1.89  christos 
   1068       1.79  christos 	return uip;
   1069       1.79  christos }
   1070       1.79  christos 
   1071       1.79  christos /*
   1072       1.79  christos  * Change the count associated with number of processes
   1073       1.79  christos  * a given user is using.
   1074       1.79  christos  */
   1075       1.79  christos int
   1076       1.79  christos chgproccnt(uid_t uid, int diff)
   1077       1.79  christos {
   1078       1.79  christos 	struct uidinfo *uip;
   1079       1.79  christos 
   1080       1.88  christos 	uip = uid_find(uid);
   1081   1.98.2.4      yamt 	mutex_enter(&uip->ui_lock);
   1082       1.88  christos 	uip->ui_proccnt += diff;
   1083       1.88  christos 	KASSERT(uip->ui_proccnt >= 0);
   1084   1.98.2.4      yamt 	mutex_exit(&uip->ui_lock);
   1085       1.88  christos 	return uip->ui_proccnt;
   1086       1.79  christos }
   1087       1.79  christos 
   1088       1.79  christos int
   1089       1.97  christos chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
   1090       1.79  christos {
   1091       1.79  christos 	rlim_t nsb;
   1092       1.79  christos 
   1093   1.98.2.4      yamt 	mutex_enter(&uip->ui_lock);
   1094       1.80      yamt 	nsb = uip->ui_sbsize + to - *hiwat;
   1095       1.97  christos 	if (to > *hiwat && nsb > xmax) {
   1096   1.98.2.4      yamt 		mutex_exit(&uip->ui_lock);
   1097       1.88  christos 		return 0;
   1098       1.94  christos 	}
   1099       1.79  christos 	*hiwat = to;
   1100       1.79  christos 	uip->ui_sbsize = nsb;
   1101       1.79  christos 	KASSERT(uip->ui_sbsize >= 0);
   1102   1.98.2.4      yamt 	mutex_exit(&uip->ui_lock);
   1103       1.88  christos 	return 1;
   1104       1.79  christos }
   1105