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