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