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