Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.60.2.2
      1 /*	$NetBSD: kern_resource.c,v 1.60.2.2 2001/11/14 19:16:37 nathanw 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kern_resource.c	8.8 (Berkeley) 2/14/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.60.2.2 2001/11/14 19:16:37 nathanw Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/file.h>
     50 #include <sys/resourcevar.h>
     51 #include <sys/malloc.h>
     52 #include <sys/pool.h>
     53 #include <sys/lwp.h>
     54 #include <sys/proc.h>
     55 
     56 #include <sys/mount.h>
     57 #include <sys/syscallargs.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 /*
     62  * Maximum process data and stack limits.
     63  * They are variables so they are patchable.
     64  *
     65  * XXXX Do we really need them to be patchable?
     66  */
     67 rlim_t maxdmap = MAXDSIZ;
     68 rlim_t maxsmap = MAXSSIZ;
     69 
     70 /*
     71  * Resource controls and accounting.
     72  */
     73 
     74 int
     75 sys_getpriority(l, v, retval)
     76 	struct lwp *l;
     77 	void *v;
     78 	register_t *retval;
     79 {
     80 	struct sys_getpriority_args /* {
     81 		syscallarg(int) which;
     82 		syscallarg(int) who;
     83 	} */ *uap = v;
     84 	struct proc *curp = l->l_proc, *p;
     85 	int low = NZERO + PRIO_MAX + 1;
     86 
     87 	switch (SCARG(uap, which)) {
     88 
     89 	case PRIO_PROCESS:
     90 		if (SCARG(uap, who) == 0)
     91 			p = curp;
     92 		else
     93 			p = pfind(SCARG(uap, who));
     94 		if (p == 0)
     95 			break;
     96 		low = p->p_nice;
     97 		break;
     98 
     99 	case PRIO_PGRP: {
    100 		struct pgrp *pg;
    101 
    102 		if (SCARG(uap, who) == 0)
    103 			pg = curp->p_pgrp;
    104 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    105 			break;
    106 		for (p = pg->pg_members.lh_first; p != 0;
    107 		     p = p->p_pglist.le_next) {
    108 			if (p->p_nice < low)
    109 				low = p->p_nice;
    110 		}
    111 		break;
    112 	}
    113 
    114 	case PRIO_USER:
    115 		if (SCARG(uap, who) == 0)
    116 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    117 		proclist_lock_read();
    118 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    119 			if (p->p_ucred->cr_uid == SCARG(uap, who) &&
    120 			    p->p_nice < low)
    121 				low = p->p_nice;
    122 		proclist_unlock_read();
    123 		break;
    124 
    125 	default:
    126 		return (EINVAL);
    127 	}
    128 	if (low == NZERO + PRIO_MAX + 1)
    129 		return (ESRCH);
    130 	*retval = low - NZERO;
    131 	return (0);
    132 }
    133 
    134 /* ARGSUSED */
    135 int
    136 sys_setpriority(l, v, retval)
    137 	struct lwp *l;
    138 	void *v;
    139 	register_t *retval;
    140 {
    141 	struct sys_setpriority_args /* {
    142 		syscallarg(int) which;
    143 		syscallarg(int) who;
    144 		syscallarg(int) prio;
    145 	} */ *uap = v;
    146 	struct proc *curp = l->l_proc, *p;
    147 	int found = 0, error = 0;
    148 
    149 	switch (SCARG(uap, which)) {
    150 
    151 	case PRIO_PROCESS:
    152 		if (SCARG(uap, who) == 0)
    153 			p = curp;
    154 		else
    155 			p = pfind(SCARG(uap, who));
    156 		if (p == 0)
    157 			break;
    158 		error = donice(curp, p, SCARG(uap, prio));
    159 		found++;
    160 		break;
    161 
    162 	case PRIO_PGRP: {
    163 		struct pgrp *pg;
    164 
    165 		if (SCARG(uap, who) == 0)
    166 			pg = curp->p_pgrp;
    167 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    168 			break;
    169 		for (p = pg->pg_members.lh_first; p != 0;
    170 		    p = p->p_pglist.le_next) {
    171 			error = donice(curp, p, SCARG(uap, prio));
    172 			found++;
    173 		}
    174 		break;
    175 	}
    176 
    177 	case PRIO_USER:
    178 		if (SCARG(uap, who) == 0)
    179 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    180 		proclist_lock_read();
    181 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    182 			if (p->p_ucred->cr_uid == SCARG(uap, who)) {
    183 				error = donice(curp, p, SCARG(uap, prio));
    184 				found++;
    185 			}
    186 		proclist_unlock_read();
    187 		break;
    188 
    189 	default:
    190 		return (EINVAL);
    191 	}
    192 	if (found == 0)
    193 		return (ESRCH);
    194 	return (error);
    195 }
    196 
    197 int
    198 donice(curp, chgp, n)
    199 	struct proc *curp, *chgp;
    200 	int n;
    201 {
    202 	struct pcred *pcred = curp->p_cred;
    203 	int s;
    204 
    205 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
    206 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
    207 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
    208 		return (EPERM);
    209 	if (n > PRIO_MAX)
    210 		n = PRIO_MAX;
    211 	if (n < PRIO_MIN)
    212 		n = PRIO_MIN;
    213 	n += NZERO;
    214 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
    215 		return (EACCES);
    216 	chgp->p_nice = n;
    217 	SCHED_LOCK(s);
    218 	(void)resetprocpriority(chgp);
    219 	SCHED_UNLOCK(s);
    220 	return (0);
    221 }
    222 
    223 /* ARGSUSED */
    224 int
    225 sys_setrlimit(l, v, retval)
    226 	struct lwp *l;
    227 	void *v;
    228 	register_t *retval;
    229 {
    230 	struct sys_setrlimit_args /* {
    231 		syscallarg(int) which;
    232 		syscallarg(const struct rlimit *) rlp;
    233 	} */ *uap = v;
    234 	struct proc *p = l->l_proc;
    235 	int which = SCARG(uap, which);
    236 	struct rlimit alim;
    237 	int error;
    238 
    239 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    240 	if (error)
    241 		return (error);
    242 	return (dosetrlimit(p, p->p_cred, which, &alim));
    243 }
    244 
    245 int
    246 dosetrlimit(p, cred, which, limp)
    247 	struct proc *p;
    248 	struct  pcred *cred;
    249 	int which;
    250 	struct rlimit *limp;
    251 {
    252 	struct rlimit *alimp;
    253 	struct plimit *newplim;
    254 	int error;
    255 
    256 	if ((u_int)which >= RLIM_NLIMITS)
    257 		return (EINVAL);
    258 
    259 	if (limp->rlim_cur < 0 || limp->rlim_max < 0)
    260 		return (EINVAL);
    261 
    262 	alimp = &p->p_rlimit[which];
    263 	/* if we don't change the value, no need to limcopy() */
    264 	if (limp->rlim_cur == alimp->rlim_cur &&
    265 	    limp->rlim_max == alimp->rlim_max)
    266 		return 0;
    267 
    268 	if (limp->rlim_cur > alimp->rlim_max ||
    269 	    limp->rlim_max > alimp->rlim_max)
    270 		if ((error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
    271 			return (error);
    272 	if (limp->rlim_cur > limp->rlim_max)
    273 		limp->rlim_cur = limp->rlim_max;
    274 	if (p->p_limit->p_refcnt > 1 &&
    275 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    276 		newplim = limcopy(p->p_limit);
    277 		limfree(p->p_limit);
    278 		p->p_limit = newplim;
    279 		alimp = &p->p_rlimit[which];
    280 	}
    281 
    282 	switch (which) {
    283 
    284 	case RLIMIT_DATA:
    285 		if (limp->rlim_cur > maxdmap)
    286 			limp->rlim_cur = maxdmap;
    287 		if (limp->rlim_max > maxdmap)
    288 			limp->rlim_max = maxdmap;
    289 		break;
    290 
    291 	case RLIMIT_STACK:
    292 		if (limp->rlim_cur > maxsmap)
    293 			limp->rlim_cur = maxsmap;
    294 		if (limp->rlim_max > maxsmap)
    295 			limp->rlim_max = maxsmap;
    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_ALL;
    316 				size = limp->rlim_cur - alimp->rlim_cur;
    317 				addr = USRSTACK - limp->rlim_cur;
    318 			} else {
    319 				prot = VM_PROT_NONE;
    320 				size = alimp->rlim_cur - limp->rlim_cur;
    321 				addr = USRSTACK - alimp->rlim_cur;
    322 			}
    323 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    324 					      addr, addr+size, prot, FALSE);
    325 		}
    326 		break;
    327 
    328 	case RLIMIT_NOFILE:
    329 		if (limp->rlim_cur > maxfiles)
    330 			limp->rlim_cur = maxfiles;
    331 		if (limp->rlim_max > maxfiles)
    332 			limp->rlim_max = maxfiles;
    333 		break;
    334 
    335 	case RLIMIT_NPROC:
    336 		if (limp->rlim_cur > maxproc)
    337 			limp->rlim_cur = maxproc;
    338 		if (limp->rlim_max > maxproc)
    339 			limp->rlim_max = maxproc;
    340 		break;
    341 	}
    342 	*alimp = *limp;
    343 	return (0);
    344 }
    345 
    346 /* ARGSUSED */
    347 int
    348 sys_getrlimit(l, v, retval)
    349 	struct lwp *l;
    350 	void *v;
    351 	register_t *retval;
    352 {
    353 	struct sys_getrlimit_args /* {
    354 		syscallarg(int) which;
    355 		syscallarg(struct rlimit *) rlp;
    356 	} */ *uap = v;
    357 	struct proc *p = l->l_proc;
    358 	int which = SCARG(uap, which);
    359 
    360 	if ((u_int)which >= RLIM_NLIMITS)
    361 		return (EINVAL);
    362 	return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
    363 	    sizeof(struct rlimit)));
    364 }
    365 
    366 /*
    367  * Transform the running time and tick information in proc p into user,
    368  * system, and interrupt time usage.
    369  */
    370 void
    371 calcru(p, up, sp, ip)
    372 	struct proc *p;
    373 	struct timeval *up;
    374 	struct timeval *sp;
    375 	struct timeval *ip;
    376 {
    377 	u_quad_t u, st, ut, it, tot;
    378 	long sec, usec;
    379 	int s;
    380 	struct timeval tv;
    381 	struct lwp *l;
    382 
    383 	s = splstatclock();
    384 	st = p->p_sticks;
    385 	ut = p->p_uticks;
    386 	it = p->p_iticks;
    387 	splx(s);
    388 
    389 	tot = st + ut + it;
    390 	if (tot == 0) {
    391 		up->tv_sec = up->tv_usec = 0;
    392 		sp->tv_sec = sp->tv_usec = 0;
    393 		if (ip != NULL)
    394 			ip->tv_sec = ip->tv_usec = 0;
    395 		return;
    396 	}
    397 
    398 	sec = p->p_rtime.tv_sec;
    399 	usec = p->p_rtime.tv_usec;
    400 	for (l = LIST_FIRST(&p->p_lwps); l != NULL;
    401 	     l = LIST_NEXT(l, l_sibling)) {
    402 		if (l->l_stat == LSONPROC) {
    403 			struct schedstate_percpu *spc;
    404 
    405 			KDASSERT(l->l_cpu != NULL);
    406 			spc = &l->l_cpu->ci_schedstate;
    407 
    408 			/*
    409 			 * Adjust for the current time slice.  This is
    410 			 * actually fairly important since the error
    411 			 * here is on the order of a time quantum,
    412 			 * which is much greater than the sampling
    413 			 * error.
    414 			 */
    415 			microtime(&tv);
    416 			sec += tv.tv_sec - spc->spc_runtime.tv_sec;
    417 			usec += tv.tv_usec - spc->spc_runtime.tv_usec;
    418 
    419 			break;
    420 		}
    421 	}
    422 	u = (u_quad_t) sec * 1000000 + usec;
    423 	st = (u * st) / tot;
    424 	sp->tv_sec = st / 1000000;
    425 	sp->tv_usec = st % 1000000;
    426 	ut = (u * ut) / tot;
    427 	up->tv_sec = ut / 1000000;
    428 	up->tv_usec = ut % 1000000;
    429 	if (ip != NULL) {
    430 		it = (u * it) / tot;
    431 		ip->tv_sec = it / 1000000;
    432 		ip->tv_usec = it % 1000000;
    433 	}
    434 }
    435 
    436 /* ARGSUSED */
    437 int
    438 sys_getrusage(l, v, retval)
    439 	struct lwp *l;
    440 	void *v;
    441 	register_t *retval;
    442 {
    443 	struct sys_getrusage_args /* {
    444 		syscallarg(int) who;
    445 		syscallarg(struct rusage *) rusage;
    446 	} */ *uap = v;
    447 	struct rusage *rup;
    448 	struct proc *p = l->l_proc;
    449 
    450 	switch (SCARG(uap, who)) {
    451 
    452 	case RUSAGE_SELF:
    453 		rup = &p->p_stats->p_ru;
    454 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    455 		break;
    456 
    457 	case RUSAGE_CHILDREN:
    458 		rup = &p->p_stats->p_cru;
    459 		break;
    460 
    461 	default:
    462 		return (EINVAL);
    463 	}
    464 	return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
    465 }
    466 
    467 void
    468 ruadd(ru, ru2)
    469 	struct rusage *ru, *ru2;
    470 {
    471 	long *ip, *ip2;
    472 	int i;
    473 
    474 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    475 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    476 	if (ru->ru_maxrss < ru2->ru_maxrss)
    477 		ru->ru_maxrss = ru2->ru_maxrss;
    478 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    479 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    480 		*ip++ += *ip2++;
    481 }
    482 
    483 /*
    484  * Make a copy of the plimit structure.
    485  * We share these structures copy-on-write after fork,
    486  * and copy when a limit is changed.
    487  */
    488 struct plimit *
    489 limcopy(lim)
    490 	struct plimit *lim;
    491 {
    492 	struct plimit *newlim;
    493 
    494 	newlim = pool_get(&plimit_pool, PR_WAITOK);
    495 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    496 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    497 	if (lim->pl_corename == defcorename) {
    498 		newlim->pl_corename = defcorename;
    499 	} else {
    500 		newlim->pl_corename = malloc(strlen(lim->pl_corename)+1,
    501 		    M_TEMP, M_WAITOK);
    502 		strcpy(newlim->pl_corename, lim->pl_corename);
    503 	}
    504 	newlim->p_lflags = 0;
    505 	newlim->p_refcnt = 1;
    506 	return (newlim);
    507 }
    508 
    509 void
    510 limfree(lim)
    511 	struct plimit *lim;
    512 {
    513 
    514 	if (--lim->p_refcnt > 0)
    515 		return;
    516 #ifdef DIAGNOSTIC
    517 	if (lim->p_refcnt < 0)
    518 		panic("limfree");
    519 #endif
    520 	if (lim->pl_corename != defcorename)
    521 		free(lim->pl_corename, M_TEMP);
    522 	pool_put(&plimit_pool, lim);
    523 }
    524 
    525 struct pstats *
    526 pstatscopy(ps)
    527 	struct pstats *ps;
    528 {
    529 
    530 	struct pstats *newps;
    531 
    532 	newps = pool_get(&pstats_pool, PR_WAITOK);
    533 
    534 	memset(&newps->pstat_startzero, 0,
    535 	(unsigned) ((caddr_t)&newps->pstat_endzero -
    536 		    (caddr_t)&newps->pstat_startzero));
    537 	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
    538 	((caddr_t)&newps->pstat_endcopy -
    539 	 (caddr_t)&newps->pstat_startcopy));
    540 
    541 	return (newps);
    542 
    543 }
    544 
    545 void
    546 pstatsfree(ps)
    547 	struct pstats *ps;
    548 {
    549 
    550 	pool_put(&pstats_pool, ps);
    551 }
    552