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