Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.60.2.1
      1 /*	$NetBSD: kern_resource.c,v 1.60.2.1 2001/03/05 22:49:41 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/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/file.h>
     47 #include <sys/resourcevar.h>
     48 #include <sys/malloc.h>
     49 #include <sys/pool.h>
     50 #include <sys/lwp.h>
     51 #include <sys/proc.h>
     52 
     53 #include <sys/mount.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  * XXXX Do we really need them to be patchable?
     63  */
     64 rlim_t maxdmap = MAXDSIZ;
     65 rlim_t maxsmap = MAXSSIZ;
     66 
     67 /*
     68  * Resource controls and accounting.
     69  */
     70 
     71 int
     72 sys_getpriority(l, v, retval)
     73 	struct lwp *l;
     74 	void *v;
     75 	register_t *retval;
     76 {
     77 	struct sys_getpriority_args /* {
     78 		syscallarg(int) which;
     79 		syscallarg(int) who;
     80 	} */ *uap = v;
     81 	struct proc *curp = l->l_proc, *p;
     82 	int low = NZERO + PRIO_MAX + 1;
     83 
     84 	switch (SCARG(uap, which)) {
     85 
     86 	case PRIO_PROCESS:
     87 		if (SCARG(uap, who) == 0)
     88 			p = curp;
     89 		else
     90 			p = pfind(SCARG(uap, who));
     91 		if (p == 0)
     92 			break;
     93 		low = p->p_nice;
     94 		break;
     95 
     96 	case PRIO_PGRP: {
     97 		struct pgrp *pg;
     98 
     99 		if (SCARG(uap, who) == 0)
    100 			pg = curp->p_pgrp;
    101 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    102 			break;
    103 		for (p = pg->pg_members.lh_first; p != 0;
    104 		     p = p->p_pglist.le_next) {
    105 			if (p->p_nice < low)
    106 				low = p->p_nice;
    107 		}
    108 		break;
    109 	}
    110 
    111 	case PRIO_USER:
    112 		if (SCARG(uap, who) == 0)
    113 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    114 		proclist_lock_read();
    115 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    116 			if (p->p_ucred->cr_uid == SCARG(uap, who) &&
    117 			    p->p_nice < low)
    118 				low = p->p_nice;
    119 		proclist_unlock_read();
    120 		break;
    121 
    122 	default:
    123 		return (EINVAL);
    124 	}
    125 	if (low == NZERO + PRIO_MAX + 1)
    126 		return (ESRCH);
    127 	*retval = low - NZERO;
    128 	return (0);
    129 }
    130 
    131 /* ARGSUSED */
    132 int
    133 sys_setpriority(l, v, retval)
    134 	struct lwp *l;
    135 	void *v;
    136 	register_t *retval;
    137 {
    138 	struct sys_setpriority_args /* {
    139 		syscallarg(int) which;
    140 		syscallarg(int) who;
    141 		syscallarg(int) prio;
    142 	} */ *uap = v;
    143 	struct proc *curp = l->l_proc, *p;
    144 	int found = 0, error = 0;
    145 
    146 	switch (SCARG(uap, which)) {
    147 
    148 	case PRIO_PROCESS:
    149 		if (SCARG(uap, who) == 0)
    150 			p = curp;
    151 		else
    152 			p = pfind(SCARG(uap, who));
    153 		if (p == 0)
    154 			break;
    155 		error = donice(curp, p, SCARG(uap, prio));
    156 		found++;
    157 		break;
    158 
    159 	case PRIO_PGRP: {
    160 		struct pgrp *pg;
    161 
    162 		if (SCARG(uap, who) == 0)
    163 			pg = curp->p_pgrp;
    164 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    165 			break;
    166 		for (p = pg->pg_members.lh_first; p != 0;
    167 		    p = p->p_pglist.le_next) {
    168 			error = donice(curp, p, SCARG(uap, prio));
    169 			found++;
    170 		}
    171 		break;
    172 	}
    173 
    174 	case PRIO_USER:
    175 		if (SCARG(uap, who) == 0)
    176 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    177 		proclist_lock_read();
    178 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    179 			if (p->p_ucred->cr_uid == SCARG(uap, who)) {
    180 				error = donice(curp, p, SCARG(uap, prio));
    181 				found++;
    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(curp, chgp, n)
    196 	struct proc *curp, *chgp;
    197 	int n;
    198 {
    199 	struct pcred *pcred = curp->p_cred;
    200 	int s;
    201 
    202 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
    203 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
    204 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
    205 		return (EPERM);
    206 	if (n > PRIO_MAX)
    207 		n = PRIO_MAX;
    208 	if (n < PRIO_MIN)
    209 		n = PRIO_MIN;
    210 	n += NZERO;
    211 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
    212 		return (EACCES);
    213 	chgp->p_nice = n;
    214 	SCHED_LOCK(s);
    215 	(void)resetprocpriority(chgp);
    216 	SCHED_UNLOCK(s);
    217 	return (0);
    218 }
    219 
    220 /* ARGSUSED */
    221 int
    222 sys_setrlimit(l, v, retval)
    223 	struct lwp *l;
    224 	void *v;
    225 	register_t *retval;
    226 {
    227 	struct sys_setrlimit_args /* {
    228 		syscallarg(int) which;
    229 		syscallarg(const struct rlimit *) rlp;
    230 	} */ *uap = v;
    231 	struct proc *p = l->l_proc;
    232 	int which = SCARG(uap, which);
    233 	struct rlimit alim;
    234 	int error;
    235 
    236 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
    237 	if (error)
    238 		return (error);
    239 	return (dosetrlimit(p, p->p_cred, which, &alim));
    240 }
    241 
    242 int
    243 dosetrlimit(p, cred, which, limp)
    244 	struct proc *p;
    245 	struct  pcred *cred;
    246 	int which;
    247 	struct rlimit *limp;
    248 {
    249 	struct rlimit *alimp;
    250 	struct plimit *newplim;
    251 	int error;
    252 
    253 	if ((u_int)which >= RLIM_NLIMITS)
    254 		return (EINVAL);
    255 
    256 	if (limp->rlim_cur < 0 || limp->rlim_max < 0)
    257 		return (EINVAL);
    258 
    259 	alimp = &p->p_rlimit[which];
    260 	/* if we don't change the value, no need to limcopy() */
    261 	if (limp->rlim_cur == alimp->rlim_cur &&
    262 	    limp->rlim_max == alimp->rlim_max)
    263 		return 0;
    264 
    265 	if (limp->rlim_cur > alimp->rlim_max ||
    266 	    limp->rlim_max > alimp->rlim_max)
    267 		if ((error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
    268 			return (error);
    269 	if (limp->rlim_cur > limp->rlim_max)
    270 		limp->rlim_cur = limp->rlim_max;
    271 	if (p->p_limit->p_refcnt > 1 &&
    272 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    273 		newplim = limcopy(p->p_limit);
    274 		limfree(p->p_limit);
    275 		p->p_limit = newplim;
    276 		alimp = &p->p_rlimit[which];
    277 	}
    278 
    279 	switch (which) {
    280 
    281 	case RLIMIT_DATA:
    282 		if (limp->rlim_cur > maxdmap)
    283 			limp->rlim_cur = maxdmap;
    284 		if (limp->rlim_max > maxdmap)
    285 			limp->rlim_max = maxdmap;
    286 		break;
    287 
    288 	case RLIMIT_STACK:
    289 		if (limp->rlim_cur > maxsmap)
    290 			limp->rlim_cur = maxsmap;
    291 		if (limp->rlim_max > maxsmap)
    292 			limp->rlim_max = maxsmap;
    293 
    294 		/*
    295 		 * Stack is allocated to the max at exec time with
    296 		 * only "rlim_cur" bytes accessible (In other words,
    297 		 * allocates stack dividing two contiguous regions at
    298 		 * "rlim_cur" bytes boundary).
    299 		 *
    300 		 * Since allocation is done in terms of page, roundup
    301 		 * "rlim_cur" (otherwise, contiguous regions
    302 		 * overlap).  If stack limit is going up make more
    303 		 * accessible, if going down make inaccessible.
    304 		 */
    305 		limp->rlim_cur = round_page(limp->rlim_cur);
    306 		if (limp->rlim_cur != alimp->rlim_cur) {
    307 			vaddr_t addr;
    308 			vsize_t size;
    309 			vm_prot_t prot;
    310 
    311 			if (limp->rlim_cur > alimp->rlim_cur) {
    312 				prot = VM_PROT_ALL;
    313 				size = limp->rlim_cur - alimp->rlim_cur;
    314 				addr = USRSTACK - limp->rlim_cur;
    315 			} else {
    316 				prot = VM_PROT_NONE;
    317 				size = alimp->rlim_cur - limp->rlim_cur;
    318 				addr = USRSTACK - alimp->rlim_cur;
    319 			}
    320 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
    321 					      addr, addr+size, prot, FALSE);
    322 		}
    323 		break;
    324 
    325 	case RLIMIT_NOFILE:
    326 		if (limp->rlim_cur > maxfiles)
    327 			limp->rlim_cur = maxfiles;
    328 		if (limp->rlim_max > maxfiles)
    329 			limp->rlim_max = maxfiles;
    330 		break;
    331 
    332 	case RLIMIT_NPROC:
    333 		if (limp->rlim_cur > maxproc)
    334 			limp->rlim_cur = maxproc;
    335 		if (limp->rlim_max > maxproc)
    336 			limp->rlim_max = maxproc;
    337 		break;
    338 	}
    339 	*alimp = *limp;
    340 	return (0);
    341 }
    342 
    343 /* ARGSUSED */
    344 int
    345 sys_getrlimit(l, v, retval)
    346 	struct lwp *l;
    347 	void *v;
    348 	register_t *retval;
    349 {
    350 	struct sys_getrlimit_args /* {
    351 		syscallarg(int) which;
    352 		syscallarg(struct rlimit *) rlp;
    353 	} */ *uap = v;
    354 	struct proc *p = l->l_proc;
    355 	int which = SCARG(uap, which);
    356 
    357 	if ((u_int)which >= RLIM_NLIMITS)
    358 		return (EINVAL);
    359 	return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
    360 	    sizeof(struct rlimit)));
    361 }
    362 
    363 /*
    364  * Transform the running time and tick information in proc p into user,
    365  * system, and interrupt time usage.
    366  */
    367 void
    368 calcru(p, up, sp, ip)
    369 	struct proc *p;
    370 	struct timeval *up;
    371 	struct timeval *sp;
    372 	struct timeval *ip;
    373 {
    374 	u_quad_t u, st, ut, it, tot;
    375 	long sec, 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 	tot = st + ut + it;
    387 	if (tot == 0) {
    388 		up->tv_sec = up->tv_usec = 0;
    389 		sp->tv_sec = sp->tv_usec = 0;
    390 		if (ip != NULL)
    391 			ip->tv_sec = ip->tv_usec = 0;
    392 		return;
    393 	}
    394 
    395 	sec = p->p_rtime.tv_sec;
    396 	usec = p->p_rtime.tv_usec;
    397 	for (l = LIST_FIRST(&p->p_lwps); l != NULL;
    398 	     l = LIST_NEXT(l, l_sibling)) {
    399 		if (l->l_stat == LSONPROC) {
    400 			struct schedstate_percpu *spc;
    401 
    402 			KDASSERT(l->l_cpu != NULL);
    403 			spc = &l->l_cpu->ci_schedstate;
    404 
    405 			/*
    406 			 * Adjust for the current time slice.  This is
    407 			 * actually fairly important since the error
    408 			 * here is on the order of a time quantum,
    409 			 * which is much greater than the sampling
    410 			 * error.
    411 			 */
    412 			microtime(&tv);
    413 			sec += tv.tv_sec - spc->spc_runtime.tv_sec;
    414 			usec += tv.tv_usec - spc->spc_runtime.tv_usec;
    415 
    416 			break;
    417 		}
    418 	}
    419 	u = (u_quad_t) sec * 1000000 + usec;
    420 	st = (u * st) / tot;
    421 	sp->tv_sec = st / 1000000;
    422 	sp->tv_usec = st % 1000000;
    423 	ut = (u * ut) / tot;
    424 	up->tv_sec = ut / 1000000;
    425 	up->tv_usec = ut % 1000000;
    426 	if (ip != NULL) {
    427 		it = (u * it) / tot;
    428 		ip->tv_sec = it / 1000000;
    429 		ip->tv_usec = it % 1000000;
    430 	}
    431 }
    432 
    433 /* ARGSUSED */
    434 int
    435 sys_getrusage(l, v, retval)
    436 	struct lwp *l;
    437 	void *v;
    438 	register_t *retval;
    439 {
    440 	struct sys_getrusage_args /* {
    441 		syscallarg(int) who;
    442 		syscallarg(struct rusage *) rusage;
    443 	} */ *uap = v;
    444 	struct rusage *rup;
    445 	struct proc *p = l->l_proc;
    446 
    447 	switch (SCARG(uap, who)) {
    448 
    449 	case RUSAGE_SELF:
    450 		rup = &p->p_stats->p_ru;
    451 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    452 		break;
    453 
    454 	case RUSAGE_CHILDREN:
    455 		rup = &p->p_stats->p_cru;
    456 		break;
    457 
    458 	default:
    459 		return (EINVAL);
    460 	}
    461 	return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
    462 }
    463 
    464 void
    465 ruadd(ru, ru2)
    466 	struct rusage *ru, *ru2;
    467 {
    468 	long *ip, *ip2;
    469 	int i;
    470 
    471 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    472 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    473 	if (ru->ru_maxrss < ru2->ru_maxrss)
    474 		ru->ru_maxrss = ru2->ru_maxrss;
    475 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
    476 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
    477 		*ip++ += *ip2++;
    478 }
    479 
    480 /*
    481  * Make a copy of the plimit structure.
    482  * We share these structures copy-on-write after fork,
    483  * and copy when a limit is changed.
    484  */
    485 struct plimit *
    486 limcopy(lim)
    487 	struct plimit *lim;
    488 {
    489 	struct plimit *newlim;
    490 
    491 	newlim = pool_get(&plimit_pool, PR_WAITOK);
    492 	memcpy(newlim->pl_rlimit, lim->pl_rlimit,
    493 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    494 	if (lim->pl_corename == defcorename) {
    495 		newlim->pl_corename = defcorename;
    496 	} else {
    497 		newlim->pl_corename = malloc(strlen(lim->pl_corename)+1,
    498 		    M_TEMP, M_WAITOK);
    499 		strcpy(newlim->pl_corename, lim->pl_corename);
    500 	}
    501 	newlim->p_lflags = 0;
    502 	newlim->p_refcnt = 1;
    503 	return (newlim);
    504 }
    505 
    506 void
    507 limfree(lim)
    508 	struct plimit *lim;
    509 {
    510 
    511 	if (--lim->p_refcnt > 0)
    512 		return;
    513 #ifdef DIAGNOSTIC
    514 	if (lim->p_refcnt < 0)
    515 		panic("limfree");
    516 #endif
    517 	if (lim->pl_corename != defcorename)
    518 		free(lim->pl_corename, M_TEMP);
    519 	pool_put(&plimit_pool, lim);
    520 }
    521 
    522 struct pstats *
    523 pstatscopy(ps)
    524 	struct pstats *ps;
    525 {
    526 
    527 	struct pstats *newps;
    528 
    529 	newps = pool_get(&pstats_pool, PR_WAITOK);
    530 
    531 	memset(&newps->pstat_startzero, 0,
    532 	(unsigned) ((caddr_t)&newps->pstat_endzero -
    533 		    (caddr_t)&newps->pstat_startzero));
    534 	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
    535 	((caddr_t)&newps->pstat_endcopy -
    536 	 (caddr_t)&newps->pstat_startcopy));
    537 
    538 	return (newps);
    539 
    540 }
    541 
    542 void
    543 pstatsfree(ps)
    544 	struct pstats *ps;
    545 {
    546 
    547 	pool_put(&pstats_pool, ps);
    548 }
    549