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