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