Home | History | Annotate | Line # | Download | only in kern
kern_resource.c revision 1.27
      1 /*	$NetBSD: kern_resource.c,v 1.27 1995/03/21 13:33:51 mycroft 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.5 (Berkeley) 1/21/94
     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/proc.h>
     50 
     51 #include <sys/mount.h>
     52 #include <sys/syscallargs.h>
     53 
     54 #include <vm/vm.h>
     55 
     56 int	donice __P((struct proc *curp, struct proc *chgp, int n));
     57 int	dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
     58 
     59 /*
     60  * Resource controls and accounting.
     61  */
     62 
     63 int
     64 getpriority(curp, uap, retval)
     65 	struct proc *curp;
     66 	register struct getpriority_args /* {
     67 		syscallarg(int) which;
     68 		syscallarg(int) who;
     69 	} */ *uap;
     70 	register_t *retval;
     71 {
     72 	register struct proc *p;
     73 	register int low = PRIO_MAX + 1;
     74 
     75 	switch (SCARG(uap, which)) {
     76 
     77 	case PRIO_PROCESS:
     78 		if (SCARG(uap, who) == 0)
     79 			p = curp;
     80 		else
     81 			p = pfind(SCARG(uap, who));
     82 		if (p == 0)
     83 			break;
     84 		low = p->p_nice;
     85 		break;
     86 
     87 	case PRIO_PGRP: {
     88 		register struct pgrp *pg;
     89 
     90 		if (SCARG(uap, who) == 0)
     91 			pg = curp->p_pgrp;
     92 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
     93 			break;
     94 		for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
     95 			if (p->p_nice < low)
     96 				low = p->p_nice;
     97 		}
     98 		break;
     99 	}
    100 
    101 	case PRIO_USER:
    102 		if (SCARG(uap, who) == 0)
    103 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    104 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    105 			if (p->p_ucred->cr_uid == SCARG(uap, who) &&
    106 			    p->p_nice < low)
    107 				low = p->p_nice;
    108 		break;
    109 
    110 	default:
    111 		return (EINVAL);
    112 	}
    113 	if (low == PRIO_MAX + 1)
    114 		return (ESRCH);
    115 	*retval = low;
    116 	return (0);
    117 }
    118 
    119 /* ARGSUSED */
    120 int
    121 setpriority(curp, uap, retval)
    122 	struct proc *curp;
    123 	register struct setpriority_args /* {
    124 		syscallarg(int) which;
    125 		syscallarg(int) who;
    126 		syscallarg(int) prio;
    127 	} */ *uap;
    128 	register_t *retval;
    129 {
    130 	register struct proc *p;
    131 	int found = 0, error = 0;
    132 
    133 	switch (SCARG(uap, which)) {
    134 
    135 	case PRIO_PROCESS:
    136 		if (SCARG(uap, who) == 0)
    137 			p = curp;
    138 		else
    139 			p = pfind(SCARG(uap, who));
    140 		if (p == 0)
    141 			break;
    142 		error = donice(curp, p, SCARG(uap, prio));
    143 		found++;
    144 		break;
    145 
    146 	case PRIO_PGRP: {
    147 		register struct pgrp *pg;
    148 
    149 		if (SCARG(uap, who) == 0)
    150 			pg = curp->p_pgrp;
    151 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
    152 			break;
    153 		for (p = pg->pg_members.lh_first; p != 0;
    154 		    p = p->p_pglist.le_next) {
    155 			error = donice(curp, p, SCARG(uap, prio));
    156 			found++;
    157 		}
    158 		break;
    159 	}
    160 
    161 	case PRIO_USER:
    162 		if (SCARG(uap, who) == 0)
    163 			SCARG(uap, who) = curp->p_ucred->cr_uid;
    164 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
    165 			if (p->p_ucred->cr_uid == SCARG(uap, who)) {
    166 				error = donice(curp, p, SCARG(uap, prio));
    167 				found++;
    168 			}
    169 		break;
    170 
    171 	default:
    172 		return (EINVAL);
    173 	}
    174 	if (found == 0)
    175 		return (ESRCH);
    176 	return (error);
    177 }
    178 
    179 int
    180 donice(curp, chgp, n)
    181 	register struct proc *curp, *chgp;
    182 	register int n;
    183 {
    184 	register struct pcred *pcred = curp->p_cred;
    185 
    186 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
    187 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
    188 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
    189 		return (EPERM);
    190 	if (n > PRIO_MAX)
    191 		n = PRIO_MAX;
    192 	if (n < PRIO_MIN)
    193 		n = PRIO_MIN;
    194 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
    195 		return (EACCES);
    196 	chgp->p_nice = n;
    197 	(void)resetpriority(chgp);
    198 	return (0);
    199 }
    200 
    201 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4) \
    202     || defined(COMPAT_LINUX)
    203 /* ARGSUSED */
    204 int
    205 compat_43_setrlimit(p, uap, retval)
    206 	struct proc *p;
    207 	struct compat_43_setrlimit_args /* {
    208 		syscallarg(u_int) which;
    209 		syscallarg(struct ogetrlimit *) rlp;
    210 	} */ *uap;
    211 	register_t *retval;
    212 {
    213 	struct orlimit olim;
    214 	struct rlimit lim;
    215 	int error;
    216 
    217 	if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&olim,
    218 	    sizeof (struct orlimit)))
    219 		return (error);
    220 	lim.rlim_cur = olim.rlim_cur;
    221 	lim.rlim_max = olim.rlim_max;
    222 	return (dosetrlimit(p, SCARG(uap, which), &lim));
    223 }
    224 
    225 /* ARGSUSED */
    226 int
    227 compat_43_getrlimit(p, uap, retval)
    228 	struct proc *p;
    229 	register struct compat_43_getrlimit_args /* {
    230 		syscallarg(u_int) which;
    231 		syscallarg(struct ogetrlimit *) rlp;
    232 	} */ *uap;
    233 	register_t *retval;
    234 {
    235 	struct orlimit olim;
    236 
    237 	if (SCARG(uap, which) >= RLIM_NLIMITS)
    238 		return (EINVAL);
    239 	olim.rlim_cur = p->p_rlimit[SCARG(uap, which)].rlim_cur;
    240 	if (olim.rlim_cur == -1)
    241 		olim.rlim_cur = 0x7fffffff;
    242 	olim.rlim_max = p->p_rlimit[SCARG(uap, which)].rlim_max;
    243 	if (olim.rlim_max == -1)
    244 		olim.rlim_max = 0x7fffffff;
    245 	return (copyout((caddr_t)&olim, (caddr_t)SCARG(uap, rlp),
    246 	    sizeof(olim)));
    247 }
    248 #endif /* COMPAT_43 || COMPAT_SUNOS || COMPAT_SVR4 || COMPAT_LINUX */
    249 
    250 /* ARGSUSED */
    251 int
    252 setrlimit(p, uap, retval)
    253 	struct proc *p;
    254 	register struct setrlimit_args /* {
    255 		syscallarg(u_int) which;
    256 		syscallarg(struct rlimit *) rlp;
    257 	} */ *uap;
    258 	register_t *retval;
    259 {
    260 	struct rlimit alim;
    261 	int error;
    262 
    263 	if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim,
    264 	    sizeof (struct rlimit)))
    265 		return (error);
    266 	return (dosetrlimit(p, SCARG(uap, which), &alim));
    267 }
    268 
    269 int
    270 dosetrlimit(p, which, limp)
    271 	struct proc *p;
    272 	u_int which;
    273 	struct rlimit *limp;
    274 {
    275 	register struct rlimit *alimp;
    276 	extern unsigned maxdmap, maxsmap;
    277 	int error;
    278 
    279 	if (which >= RLIM_NLIMITS)
    280 		return (EINVAL);
    281 	alimp = &p->p_rlimit[which];
    282 	if (limp->rlim_cur > alimp->rlim_max ||
    283 	    limp->rlim_max > alimp->rlim_max)
    284 		if (error = suser(p->p_ucred, &p->p_acflag))
    285 			return (error);
    286 	if (limp->rlim_cur > limp->rlim_max)
    287 		limp->rlim_cur = limp->rlim_max;
    288 	if (p->p_limit->p_refcnt > 1 &&
    289 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    290 		p->p_limit->p_refcnt--;
    291 		p->p_limit = limcopy(p->p_limit);
    292 		alimp = &p->p_rlimit[which];
    293 	}
    294 
    295 	switch (which) {
    296 
    297 	case RLIMIT_DATA:
    298 		if (limp->rlim_cur > maxdmap)
    299 			limp->rlim_cur = maxdmap;
    300 		if (limp->rlim_max > maxdmap)
    301 			limp->rlim_max = maxdmap;
    302 		break;
    303 
    304 	case RLIMIT_STACK:
    305 		if (limp->rlim_cur > maxsmap)
    306 			limp->rlim_cur = maxsmap;
    307 		if (limp->rlim_max > maxsmap)
    308 			limp->rlim_max = maxsmap;
    309 		/*
    310 		 * Stack is allocated to the max at exec time with only
    311 		 * "rlim_cur" bytes accessible.  If stack limit is going
    312 		 * up make more accessible, if going down make inaccessible.
    313 		 */
    314 		if (limp->rlim_cur != alimp->rlim_cur) {
    315 			vm_offset_t addr;
    316 			vm_size_t size;
    317 			vm_prot_t prot;
    318 
    319 			if (limp->rlim_cur > alimp->rlim_cur) {
    320 				prot = VM_PROT_ALL;
    321 				size = limp->rlim_cur - alimp->rlim_cur;
    322 				addr = USRSTACK - limp->rlim_cur;
    323 			} else {
    324 				prot = VM_PROT_NONE;
    325 				size = alimp->rlim_cur - limp->rlim_cur;
    326 				addr = USRSTACK - alimp->rlim_cur;
    327 			}
    328 			addr = trunc_page(addr);
    329 			size = round_page(size);
    330 			(void) vm_map_protect(&p->p_vmspace->vm_map,
    331 					      addr, addr+size, prot, FALSE);
    332 		}
    333 		break;
    334 
    335 	case RLIMIT_NOFILE:
    336 		if (limp->rlim_cur > maxfiles)
    337 			limp->rlim_cur = maxfiles;
    338 		if (limp->rlim_max > maxfiles)
    339 			limp->rlim_max = maxfiles;
    340 		break;
    341 
    342 	case RLIMIT_NPROC:
    343 		if (limp->rlim_cur > maxproc)
    344 			limp->rlim_cur = maxproc;
    345 		if (limp->rlim_max > maxproc)
    346 			limp->rlim_max = maxproc;
    347 		break;
    348 	}
    349 	*alimp = *limp;
    350 	return (0);
    351 }
    352 
    353 /* ARGSUSED */
    354 int
    355 getrlimit(p, uap, retval)
    356 	struct proc *p;
    357 	register struct getrlimit_args /* {
    358 		syscallarg(u_int) which;
    359 		syscallarg(struct rlimit *) rlp;
    360 	} */ *uap;
    361 	register_t *retval;
    362 {
    363 
    364 	if (SCARG(uap, which) >= RLIM_NLIMITS)
    365 		return (EINVAL);
    366 	return (copyout((caddr_t)&p->p_rlimit[SCARG(uap, which)],
    367 	    (caddr_t)SCARG(uap, rlp), sizeof (struct rlimit)));
    368 }
    369 
    370 /*
    371  * Transform the running time and tick information in proc p into user,
    372  * system, and interrupt time usage.
    373  */
    374 void
    375 calcru(p, up, sp, ip)
    376 	register struct proc *p;
    377 	register struct timeval *up;
    378 	register struct timeval *sp;
    379 	register struct timeval *ip;
    380 {
    381 	register u_quad_t u, st, ut, it, tot;
    382 	register u_long sec, usec;
    383 	register int s;
    384 	struct timeval tv;
    385 
    386 	s = splstatclock();
    387 	st = p->p_sticks;
    388 	ut = p->p_uticks;
    389 	it = p->p_iticks;
    390 	splx(s);
    391 
    392 	tot = st + ut + it;
    393 	if (tot == 0) {
    394 		up->tv_sec = up->tv_usec = 0;
    395 		sp->tv_sec = sp->tv_usec = 0;
    396 		if (ip != NULL)
    397 			ip->tv_sec = ip->tv_usec = 0;
    398 		return;
    399 	}
    400 
    401 	sec = p->p_rtime.tv_sec;
    402 	usec = p->p_rtime.tv_usec;
    403 	if (p == curproc) {
    404 		/*
    405 		 * Adjust for the current time slice.  This is actually fairly
    406 		 * important since the error here is on the order of a time
    407 		 * quantum, which is much greater than the sampling error.
    408 		 */
    409 		microtime(&tv);
    410 		sec += tv.tv_sec - runtime.tv_sec;
    411 		usec += tv.tv_usec - runtime.tv_usec;
    412 	}
    413 	u = sec * 1000000 + usec;
    414 	st = (u * st) / tot;
    415 	sp->tv_sec = st / 1000000;
    416 	sp->tv_usec = st % 1000000;
    417 	ut = (u * ut) / tot;
    418 	up->tv_sec = ut / 1000000;
    419 	up->tv_usec = ut % 1000000;
    420 	if (ip != NULL) {
    421 		it = (u * it) / tot;
    422 		ip->tv_sec = it / 1000000;
    423 		ip->tv_usec = it % 1000000;
    424 	}
    425 }
    426 
    427 /* ARGSUSED */
    428 int
    429 getrusage(p, uap, retval)
    430 	register struct proc *p;
    431 	register struct getrusage_args /* {
    432 		syscallarg(int) who;
    433 		syscallarg(struct rusage *) rusage;
    434 	} */ *uap;
    435 	register_t *retval;
    436 {
    437 	register struct rusage *rup;
    438 
    439 	switch (SCARG(uap, who)) {
    440 
    441 	case RUSAGE_SELF:
    442 		rup = &p->p_stats->p_ru;
    443 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
    444 		break;
    445 
    446 	case RUSAGE_CHILDREN:
    447 		rup = &p->p_stats->p_cru;
    448 		break;
    449 
    450 	default:
    451 		return (EINVAL);
    452 	}
    453 	return (copyout((caddr_t)rup, (caddr_t)SCARG(uap, rusage),
    454 	    sizeof (struct rusage)));
    455 }
    456 
    457 void
    458 ruadd(ru, ru2)
    459 	register struct rusage *ru, *ru2;
    460 {
    461 	register long *ip, *ip2;
    462 	register 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(lim)
    480 	struct plimit *lim;
    481 {
    482 	register struct plimit *copy;
    483 
    484 	MALLOC(copy, struct plimit *, sizeof(struct plimit),
    485 	    M_SUBPROC, M_WAITOK);
    486 	bcopy(lim->pl_rlimit, copy->pl_rlimit,
    487 	    sizeof(struct rlimit) * RLIM_NLIMITS);
    488 	copy->p_lflags = 0;
    489 	copy->p_refcnt = 1;
    490 	return (copy);
    491 }
    492