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