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