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