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