xref: /openbsd-src/sys/kern/kern_resource.c (revision d852dfbc8b4ab857193322be8c11189b8ba06b74)
1 /*	$OpenBSD: kern_resource.c,v 1.12 2000/05/05 08:34:18 art Exp $	*/
2 /*	$NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)kern_resource.c	8.5 (Berkeley) 1/21/94
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/resourcevar.h>
49 #include <sys/malloc.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 #if defined(UVM)
58 #include <uvm/uvm_extern.h>
59 #endif
60 
61 /*
62  * Resource controls and accounting.
63  */
64 
65 int
66 sys_getpriority(curp, v, retval)
67 	struct proc *curp;
68 	void *v;
69 	register_t *retval;
70 {
71 	register struct sys_getpriority_args /* {
72 		syscallarg(int) which;
73 		syscallarg(int) who;
74 	} */ *uap = v;
75 	register struct proc *p;
76 	register int low = NZERO + PRIO_MAX + 1;
77 
78 	switch (SCARG(uap, which)) {
79 
80 	case PRIO_PROCESS:
81 		if (SCARG(uap, who) == 0)
82 			p = curp;
83 		else
84 			p = pfind(SCARG(uap, who));
85 		if (p == 0)
86 			break;
87 		low = p->p_nice;
88 		break;
89 
90 	case PRIO_PGRP: {
91 		register struct pgrp *pg;
92 
93 		if (SCARG(uap, who) == 0)
94 			pg = curp->p_pgrp;
95 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
96 			break;
97 		for (p = pg->pg_members.lh_first; p != 0; 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 = LIST_FIRST(&allproc); p; p = LIST_NEXT(p, p_list))
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 = LIST_FIRST(&allproc); p; p = LIST_NEXT(p, p_list))
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(u_int) which;
215 		syscallarg(struct rlimit *) rlp;
216 	} */ *uap = v;
217 	struct rlimit alim;
218 	int error;
219 
220 	error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim,
221 		       sizeof (struct rlimit));
222 	if (error)
223 		return (error);
224 	return (dosetrlimit(p, SCARG(uap, which), &alim));
225 }
226 
227 int
228 dosetrlimit(p, which, limp)
229 	struct proc *p;
230 	u_int which;
231 	struct rlimit *limp;
232 {
233 	register struct rlimit *alimp;
234 	extern unsigned maxdmap, maxsmap;
235 	int error;
236 
237 	if (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 		 * Stack is allocated to the max at exec time with only
273 		 * "rlim_cur" bytes accessible.  If stack limit is going
274 		 * up make more accessible, if going down make inaccessible.
275 		 */
276 		if (limp->rlim_cur != alimp->rlim_cur) {
277 			vaddr_t addr;
278 			vsize_t size;
279 			vm_prot_t prot;
280 
281 			if (limp->rlim_cur > alimp->rlim_cur) {
282 				prot = VM_PROT_ALL;
283 				size = limp->rlim_cur - alimp->rlim_cur;
284 #ifdef MACHINE_STACK_GROWS_UP
285 				addr = USRSTACK + alimp->rlim_cur;
286 #else
287 				addr = USRSTACK - limp->rlim_cur;
288 #endif
289 			} else {
290 				prot = VM_PROT_NONE;
291 				size = alimp->rlim_cur - limp->rlim_cur;
292 #ifdef MACHINE_STACK_GROWS_UP
293 				addr = USRSTACK + limp->rlim_cur;
294 #else
295 				addr = USRSTACK - alimp->rlim_cur;
296 #endif
297 			}
298 			addr = trunc_page(addr);
299 			size = round_page(size);
300 #if defined(UVM)
301 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
302 					      addr, addr+size, prot, FALSE);
303 #else
304 			(void) vm_map_protect(&p->p_vmspace->vm_map,
305 					      addr, addr+size, prot, FALSE);
306 #endif
307 		}
308 		break;
309 
310 	case RLIMIT_NOFILE:
311 		if (limp->rlim_cur > maxfiles)
312 			limp->rlim_cur = maxfiles;
313 		if (limp->rlim_max > maxfiles)
314 			limp->rlim_max = maxfiles;
315 		break;
316 
317 	case RLIMIT_NPROC:
318 		if (limp->rlim_cur > maxproc)
319 			limp->rlim_cur = maxproc;
320 		if (limp->rlim_max > maxproc)
321 			limp->rlim_max = maxproc;
322 		break;
323 	}
324 	*alimp = *limp;
325 	return (0);
326 }
327 
328 /* ARGSUSED */
329 int
330 sys_getrlimit(p, v, retval)
331 	struct proc *p;
332 	void *v;
333 	register_t *retval;
334 {
335 	register struct sys_getrlimit_args /* {
336 		syscallarg(u_int) which;
337 		syscallarg(struct rlimit *) rlp;
338 	} */ *uap = v;
339 
340 	if (SCARG(uap, which) >= RLIM_NLIMITS)
341 		return (EINVAL);
342 	return (copyout((caddr_t)&p->p_rlimit[SCARG(uap, which)],
343 	    (caddr_t)SCARG(uap, rlp), sizeof (struct rlimit)));
344 }
345 
346 /*
347  * Transform the running time and tick information in proc p into user,
348  * system, and interrupt time usage.
349  */
350 void
351 calcru(p, up, sp, ip)
352 	register struct proc *p;
353 	register struct timeval *up;
354 	register struct timeval *sp;
355 	register struct timeval *ip;
356 {
357 	register u_quad_t u, st, ut, it, tot;
358 	register long sec, usec;
359 	register int s;
360 	struct timeval tv;
361 
362 	s = splstatclock();
363 	st = p->p_sticks;
364 	ut = p->p_uticks;
365 	it = p->p_iticks;
366 	splx(s);
367 
368 	tot = st + ut + it;
369 	if (tot == 0) {
370 		up->tv_sec = up->tv_usec = 0;
371 		sp->tv_sec = sp->tv_usec = 0;
372 		if (ip != NULL)
373 			ip->tv_sec = ip->tv_usec = 0;
374 		return;
375 	}
376 
377 	sec = p->p_rtime.tv_sec;
378 	usec = p->p_rtime.tv_usec;
379 	if (p == curproc) {
380 		/*
381 		 * Adjust for the current time slice.  This is actually fairly
382 		 * important since the error here is on the order of a time
383 		 * quantum, which is much greater than the sampling error.
384 		 */
385 		microtime(&tv);
386 		sec += tv.tv_sec - runtime.tv_sec;
387 		usec += tv.tv_usec - runtime.tv_usec;
388 	}
389 	u = (u_quad_t) sec * 1000000 + usec;
390 	st = (u * st) / tot;
391 	sp->tv_sec = st / 1000000;
392 	sp->tv_usec = st % 1000000;
393 	ut = (u * ut) / tot;
394 	up->tv_sec = ut / 1000000;
395 	up->tv_usec = ut % 1000000;
396 	if (ip != NULL) {
397 		it = (u * it) / tot;
398 		ip->tv_sec = it / 1000000;
399 		ip->tv_usec = it % 1000000;
400 	}
401 }
402 
403 /* ARGSUSED */
404 int
405 sys_getrusage(p, v, retval)
406 	register struct proc *p;
407 	void *v;
408 	register_t *retval;
409 {
410 	register struct sys_getrusage_args /* {
411 		syscallarg(int) who;
412 		syscallarg(struct rusage *) rusage;
413 	} */ *uap = v;
414 	register struct rusage *rup;
415 
416 	switch (SCARG(uap, who)) {
417 
418 	case RUSAGE_SELF:
419 		rup = &p->p_stats->p_ru;
420 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
421 		break;
422 
423 	case RUSAGE_CHILDREN:
424 		rup = &p->p_stats->p_cru;
425 		break;
426 
427 	default:
428 		return (EINVAL);
429 	}
430 	return (copyout((caddr_t)rup, (caddr_t)SCARG(uap, rusage),
431 	    sizeof (struct rusage)));
432 }
433 
434 void
435 ruadd(ru, ru2)
436 	register struct rusage *ru, *ru2;
437 {
438 	register long *ip, *ip2;
439 	register int i;
440 
441 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
442 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
443 	if (ru->ru_maxrss < ru2->ru_maxrss)
444 		ru->ru_maxrss = ru2->ru_maxrss;
445 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
446 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
447 		*ip++ += *ip2++;
448 }
449 
450 /*
451  * Make a copy of the plimit structure.
452  * We share these structures copy-on-write after fork,
453  * and copy when a limit is changed.
454  */
455 struct plimit *
456 limcopy(lim)
457 	struct plimit *lim;
458 {
459 	register struct plimit *newlim;
460 
461 	MALLOC(newlim, struct plimit *, sizeof(struct plimit),
462 	    M_SUBPROC, M_WAITOK);
463 	bcopy(lim->pl_rlimit, newlim->pl_rlimit,
464 	    sizeof(struct rlimit) * RLIM_NLIMITS);
465 	newlim->p_lflags = 0;
466 	newlim->p_refcnt = 1;
467 	return (newlim);
468 }
469 
470 void
471 limfree(lim)
472 	struct plimit *lim;
473 {
474 
475 	if (--lim->p_refcnt > 0)
476 		return;
477 	FREE(lim, M_SUBPROC);
478 }
479