xref: /openbsd-src/sys/kern/kern_resource.c (revision 012ea299774669efba934275e67c4a60442e6b8f)
1 /*	$OpenBSD: kern_resource.c,v 1.27 2004/06/13 21:49:26 niklas 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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_resource.c	8.5 (Berkeley) 1/21/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file.h>
44 #include <sys/resourcevar.h>
45 #include <sys/pool.h>
46 #include <sys/proc.h>
47 #include <sys/sched.h>
48 
49 #include <sys/mount.h>
50 #include <sys/syscallargs.h>
51 
52 #include <uvm/uvm_extern.h>
53 
54 /*
55  * Patchable maximum data and stack limits.
56  */
57 rlim_t maxdmap = MAXDSIZ;
58 rlim_t maxsmap = MAXSSIZ;
59 
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(id_t) 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; p = p->p_pglist.le_next) {
97 			if (p->p_nice < low)
98 				low = p->p_nice;
99 		}
100 		break;
101 	}
102 
103 	case PRIO_USER:
104 		if (SCARG(uap, who) == 0)
105 			SCARG(uap, who) = curp->p_ucred->cr_uid;
106 		for (p = LIST_FIRST(&allproc); p; p = LIST_NEXT(p, p_list))
107 			if (p->p_ucred->cr_uid == SCARG(uap, who) &&
108 			    p->p_nice < low)
109 				low = p->p_nice;
110 		break;
111 
112 	default:
113 		return (EINVAL);
114 	}
115 	if (low == NZERO + PRIO_MAX + 1)
116 		return (ESRCH);
117 	*retval = low - NZERO;
118 	return (0);
119 }
120 
121 /* ARGSUSED */
122 int
123 sys_setpriority(curp, v, retval)
124 	struct proc *curp;
125 	void *v;
126 	register_t *retval;
127 {
128 	register struct sys_setpriority_args /* {
129 		syscallarg(int) which;
130 		syscallarg(id_t) who;
131 		syscallarg(int) prio;
132 	} */ *uap = v;
133 	register struct proc *p;
134 	int found = 0, error = 0;
135 
136 	switch (SCARG(uap, which)) {
137 
138 	case PRIO_PROCESS:
139 		if (SCARG(uap, who) == 0)
140 			p = curp;
141 		else
142 			p = pfind(SCARG(uap, who));
143 		if (p == 0)
144 			break;
145 		error = donice(curp, p, SCARG(uap, prio));
146 		found++;
147 		break;
148 
149 	case PRIO_PGRP: {
150 		register struct pgrp *pg;
151 
152 		if (SCARG(uap, who) == 0)
153 			pg = curp->p_pgrp;
154 		else if ((pg = pgfind(SCARG(uap, who))) == NULL)
155 			break;
156 		for (p = pg->pg_members.lh_first; p != 0;
157 		    p = p->p_pglist.le_next) {
158 			error = donice(curp, p, SCARG(uap, prio));
159 			found++;
160 		}
161 		break;
162 	}
163 
164 	case PRIO_USER:
165 		if (SCARG(uap, who) == 0)
166 			SCARG(uap, who) = curp->p_ucred->cr_uid;
167 		for (p = LIST_FIRST(&allproc); p; p = LIST_NEXT(p, p_list))
168 			if (p->p_ucred->cr_uid == SCARG(uap, who)) {
169 				error = donice(curp, p, SCARG(uap, prio));
170 				found++;
171 			}
172 		break;
173 
174 	default:
175 		return (EINVAL);
176 	}
177 	if (found == 0)
178 		return (ESRCH);
179 	return (error);
180 }
181 
182 int
183 donice(curp, chgp, n)
184 	register struct proc *curp, *chgp;
185 	register int n;
186 {
187 	register struct pcred *pcred = curp->p_cred;
188 	int s;
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(curp, 0))
200 		return (EACCES);
201 	chgp->p_nice = n;
202 	SCHED_LOCK(s);
203 	(void)resetpriority(chgp);
204 	SCHED_UNLOCK(s);
205 	return (0);
206 }
207 
208 /* ARGSUSED */
209 int
210 sys_setrlimit(p, v, retval)
211 	struct proc *p;
212 	void *v;
213 	register_t *retval;
214 {
215 	register struct sys_setrlimit_args /* {
216 		syscallarg(int) which;
217 		syscallarg(const struct rlimit *) rlp;
218 	} */ *uap = v;
219 	struct rlimit alim;
220 	int error;
221 
222 	error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim,
223 		       sizeof (struct rlimit));
224 	if (error)
225 		return (error);
226 	return (dosetrlimit(p, SCARG(uap, which), &alim));
227 }
228 
229 int
230 dosetrlimit(p, which, limp)
231 	struct proc *p;
232 	u_int which;
233 	struct rlimit *limp;
234 {
235 	struct rlimit *alimp;
236 	rlim_t maxlim;
237 	int error;
238 
239 	if (which >= RLIM_NLIMITS)
240 		return (EINVAL);
241 
242 	alimp = &p->p_rlimit[which];
243 	if (limp->rlim_cur > alimp->rlim_max ||
244 	    limp->rlim_max > alimp->rlim_max)
245 		if ((error = suser(p, 0)) != 0)
246 			return (error);
247 	if (p->p_limit->p_refcnt > 1 &&
248 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
249 		p->p_limit->p_refcnt--;
250 		p->p_limit = limcopy(p->p_limit);
251 		alimp = &p->p_rlimit[which];
252 	}
253 
254 	switch (which) {
255 	case RLIMIT_DATA:
256 		maxlim = maxdmap;
257 		break;
258 	case RLIMIT_STACK:
259 		maxlim = maxsmap;
260 		break;
261 	case RLIMIT_NOFILE:
262 		maxlim = maxfiles;
263 		break;
264 	case RLIMIT_NPROC:
265 		maxlim = maxproc;
266 		break;
267 	default:
268 		maxlim = RLIM_INFINITY;
269 		break;
270 	}
271 
272 	if (limp->rlim_max > maxlim)
273 		limp->rlim_max = maxlim;
274 	if (limp->rlim_cur > limp->rlim_max)
275 		limp->rlim_cur = limp->rlim_max;
276 
277 	if (which == RLIMIT_STACK) {
278 		/*
279 		 * Stack is allocated to the max at exec time with only
280 		 * "rlim_cur" bytes accessible.  If stack limit is going
281 		 * up make more accessible, if going down make inaccessible.
282 		 */
283 		if (limp->rlim_cur != alimp->rlim_cur) {
284 			vaddr_t addr;
285 			vsize_t size;
286 			vm_prot_t prot;
287 
288 			if (limp->rlim_cur > alimp->rlim_cur) {
289 				prot = VM_PROT_READ|VM_PROT_WRITE;
290 				size = limp->rlim_cur - alimp->rlim_cur;
291 #ifdef MACHINE_STACK_GROWS_UP
292 				addr = USRSTACK + alimp->rlim_cur;
293 #else
294 				addr = USRSTACK - limp->rlim_cur;
295 #endif
296 			} else {
297 				prot = VM_PROT_NONE;
298 				size = alimp->rlim_cur - limp->rlim_cur;
299 #ifdef MACHINE_STACK_GROWS_UP
300 				addr = USRSTACK + limp->rlim_cur;
301 #else
302 				addr = USRSTACK - alimp->rlim_cur;
303 #endif
304 			}
305 			addr = trunc_page(addr);
306 			size = round_page(size);
307 			(void) uvm_map_protect(&p->p_vmspace->vm_map,
308 					      addr, addr+size, prot, FALSE);
309 		}
310 	}
311 
312 	*alimp = *limp;
313 	return (0);
314 }
315 
316 /* ARGSUSED */
317 int
318 sys_getrlimit(p, v, retval)
319 	struct proc *p;
320 	void *v;
321 	register_t *retval;
322 {
323 	register struct sys_getrlimit_args /* {
324 		syscallarg(int) which;
325 		syscallarg(struct rlimit *) rlp;
326 	} */ *uap = v;
327 
328 	if (SCARG(uap, which) < 0 || SCARG(uap, which) >= RLIM_NLIMITS)
329 		return (EINVAL);
330 	return (copyout((caddr_t)&p->p_rlimit[SCARG(uap, which)],
331 	    (caddr_t)SCARG(uap, rlp), sizeof (struct rlimit)));
332 }
333 
334 /*
335  * Transform the running time and tick information in proc p into user,
336  * system, and interrupt time usage.
337  */
338 void
339 calcru(p, up, sp, ip)
340 	struct proc *p;
341 	struct timeval *up;
342 	struct timeval *sp;
343 	struct timeval *ip;
344 {
345 	u_quad_t st, ut, it;
346 	int freq;
347 	int s;
348 
349 	s = splstatclock();
350 	st = p->p_sticks;
351 	ut = p->p_uticks;
352 	it = p->p_iticks;
353 	splx(s);
354 
355 	if (st + ut + it == 0) {
356 		timerclear(up);
357 		timerclear(sp);
358 		if (ip != NULL)
359 			timerclear(ip);
360 		return;
361 	}
362 
363 	freq = stathz ? stathz : hz;
364 
365 	st = st * 1000000 / freq;
366 	sp->tv_sec = st / 1000000;
367 	sp->tv_usec = st % 1000000;
368 	ut = ut * 1000000 / freq;
369 	up->tv_sec = ut / 1000000;
370 	up->tv_usec = ut % 1000000;
371 	if (ip != NULL) {
372 		it = it * 1000000 / freq;
373 		ip->tv_sec = it / 1000000;
374 		ip->tv_usec = it % 1000000;
375 	}
376 }
377 
378 /* ARGSUSED */
379 int
380 sys_getrusage(p, v, retval)
381 	register struct proc *p;
382 	void *v;
383 	register_t *retval;
384 {
385 	register struct sys_getrusage_args /* {
386 		syscallarg(int) who;
387 		syscallarg(struct rusage *) rusage;
388 	} */ *uap = v;
389 	register struct rusage *rup;
390 
391 	switch (SCARG(uap, who)) {
392 
393 	case RUSAGE_SELF:
394 		rup = &p->p_stats->p_ru;
395 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
396 		break;
397 
398 	case RUSAGE_CHILDREN:
399 		rup = &p->p_stats->p_cru;
400 		break;
401 
402 	default:
403 		return (EINVAL);
404 	}
405 	return (copyout((caddr_t)rup, (caddr_t)SCARG(uap, rusage),
406 	    sizeof (struct rusage)));
407 }
408 
409 void
410 ruadd(ru, ru2)
411 	register struct rusage *ru, *ru2;
412 {
413 	register long *ip, *ip2;
414 	register int i;
415 
416 	timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
417 	timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
418 	if (ru->ru_maxrss < ru2->ru_maxrss)
419 		ru->ru_maxrss = ru2->ru_maxrss;
420 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
421 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
422 		*ip++ += *ip2++;
423 }
424 
425 struct pool plimit_pool;
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(struct plimit *lim)
434 {
435 	struct plimit *newlim;
436 	static int initialized;
437 
438 	if (!initialized) {
439 		pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0,
440 		    "plimitpl", &pool_allocator_nointr);
441 		initialized = 1;
442 	}
443 
444 	newlim = pool_get(&plimit_pool, PR_WAITOK);
445 	bcopy(lim->pl_rlimit, newlim->pl_rlimit,
446 	    sizeof(struct rlimit) * RLIM_NLIMITS);
447 	newlim->p_lflags = 0;
448 	newlim->p_refcnt = 1;
449 	return (newlim);
450 }
451 
452 void
453 limfree(struct plimit *lim)
454 {
455 	if (--lim->p_refcnt > 0)
456 		return;
457 	pool_put(&plimit_pool, lim);
458 }
459