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