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