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