xref: /freebsd/sys/kern/kern_resource.c (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_resource.c	8.5 (Berkeley) 1/21/94
39  * $Id$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/file.h>
46 #include <sys/resourcevar.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49 
50 #include <vm/vm.h>
51 
52 /*
53  * Resource controls and accounting.
54  */
55 
56 struct getpriority_args {
57 	int	which;
58 	int	who;
59 };
60 int
61 getpriority(curp, uap, retval)
62 	struct proc *curp;
63 	register struct getpriority_args *uap;
64 	int *retval;
65 {
66 	register struct proc *p;
67 	register int low = PRIO_MAX + 1;
68 
69 	switch (uap->which) {
70 
71 	case PRIO_PROCESS:
72 		if (uap->who == 0)
73 			p = curp;
74 		else
75 			p = pfind(uap->who);
76 		if (p == 0)
77 			break;
78 		low = p->p_nice;
79 		break;
80 
81 	case PRIO_PGRP: {
82 		register struct pgrp *pg;
83 
84 		if (uap->who == 0)
85 			pg = curp->p_pgrp;
86 		else if ((pg = pgfind(uap->who)) == NULL)
87 			break;
88 		for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
89 			if (p->p_nice < low)
90 				low = p->p_nice;
91 		}
92 		break;
93 	}
94 
95 	case PRIO_USER:
96 		if (uap->who == 0)
97 			uap->who = curp->p_ucred->cr_uid;
98 		for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
99 			if (p->p_ucred->cr_uid == uap->who &&
100 			    p->p_nice < low)
101 				low = p->p_nice;
102 		}
103 		break;
104 
105 	default:
106 		return (EINVAL);
107 	}
108 	if (low == PRIO_MAX + 1)
109 		return (ESRCH);
110 	*retval = low;
111 	return (0);
112 }
113 
114 struct setpriority_args {
115 	int	which;
116 	int	who;
117 	int	prio;
118 };
119 /* ARGSUSED */
120 int
121 setpriority(curp, uap, retval)
122 	struct proc *curp;
123 	register struct setpriority_args *uap;
124 	int *retval;
125 {
126 	register struct proc *p;
127 	int found = 0, error = 0;
128 
129 	switch (uap->which) {
130 
131 	case PRIO_PROCESS:
132 		if (uap->who == 0)
133 			p = curp;
134 		else
135 			p = pfind(uap->who);
136 		if (p == 0)
137 			break;
138 		error = donice(curp, p, uap->prio);
139 		found++;
140 		break;
141 
142 	case PRIO_PGRP: {
143 		register struct pgrp *pg;
144 
145 		if (uap->who == 0)
146 			pg = curp->p_pgrp;
147 		else if ((pg = pgfind(uap->who)) == NULL)
148 			break;
149 		for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
150 			error = donice(curp, p, uap->prio);
151 			found++;
152 		}
153 		break;
154 	}
155 
156 	case PRIO_USER:
157 		if (uap->who == 0)
158 			uap->who = curp->p_ucred->cr_uid;
159 		for (p = (struct proc *)allproc; p != NULL; p = p->p_next)
160 			if (p->p_ucred->cr_uid == uap->who) {
161 				error = donice(curp, p, 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 int
175 donice(curp, chgp, n)
176 	register struct proc *curp, *chgp;
177 	register int n;
178 {
179 	register struct pcred *pcred = curp->p_cred;
180 
181 	if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
182 	    pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
183 	    pcred->p_ruid != chgp->p_ucred->cr_uid)
184 		return (EPERM);
185 	if (n > PRIO_MAX)
186 		n = PRIO_MAX;
187 	if (n < PRIO_MIN)
188 		n = PRIO_MIN;
189 	if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
190 		return (EACCES);
191 	chgp->p_nice = n;
192 	(void)resetpriority(chgp);
193 	return (0);
194 }
195 
196 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
197 struct setrlimit_args {
198 	u_int	which;
199 	struct	orlimit *lim;
200 };
201 /* ARGSUSED */
202 int
203 osetrlimit(p, uap, retval)
204 	struct proc *p;
205 	register struct setrlimit_args *uap;
206 	int *retval;
207 {
208 	struct orlimit olim;
209 	struct rlimit lim;
210 	int error;
211 
212 	if (error =
213 	    copyin((caddr_t)uap->lim, (caddr_t)&olim, sizeof (struct orlimit)))
214 		return (error);
215 	lim.rlim_cur = olim.rlim_cur;
216 	lim.rlim_max = olim.rlim_max;
217 	return (dosetrlimit(p, uap->which, &lim));
218 }
219 
220 struct getrlimit_args {
221 	u_int	which;
222 	struct	orlimit *rlp;
223 };
224 /* ARGSUSED */
225 int
226 ogetrlimit(p, uap, retval)
227 	struct proc *p;
228 	register struct getrlimit_args *uap;
229 	int *retval;
230 {
231 	struct orlimit olim;
232 
233 	if (uap->which >= RLIM_NLIMITS)
234 		return (EINVAL);
235 	olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
236 	if (olim.rlim_cur == -1)
237 		olim.rlim_cur = 0x7fffffff;
238 	olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
239 	if (olim.rlim_max == -1)
240 		olim.rlim_max = 0x7fffffff;
241 	return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
242 }
243 #endif /* COMPAT_43 || COMPAT_SUNOS */
244 
245 struct __setrlimit_args {
246 	u_int	which;
247 	struct	rlimit *lim;
248 };
249 /* ARGSUSED */
250 int
251 setrlimit(p, uap, retval)
252 	struct proc *p;
253 	register struct __setrlimit_args *uap;
254 	int *retval;
255 {
256 	struct rlimit alim;
257 	int error;
258 
259 	if (error =
260 	    copyin((caddr_t)uap->lim, (caddr_t)&alim, sizeof (struct rlimit)))
261 		return (error);
262 	return (dosetrlimit(p, uap->which, &alim));
263 }
264 
265 int
266 dosetrlimit(p, which, limp)
267 	struct proc *p;
268 	u_int which;
269 	struct rlimit *limp;
270 {
271 	register struct rlimit *alimp;
272 	int error;
273 
274 	if (which >= RLIM_NLIMITS)
275 		return (EINVAL);
276 	alimp = &p->p_rlimit[which];
277 	if (limp->rlim_cur > alimp->rlim_max ||
278 	    limp->rlim_max > alimp->rlim_max)
279 		if (error = suser(p->p_ucred, &p->p_acflag))
280 			return (error);
281 	if (limp->rlim_cur > limp->rlim_max)
282 		limp->rlim_cur = limp->rlim_max;
283 	if (p->p_limit->p_refcnt > 1 &&
284 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
285 		p->p_limit->p_refcnt--;
286 		p->p_limit = limcopy(p->p_limit);
287 		alimp = &p->p_rlimit[which];
288 	}
289 
290 	switch (which) {
291 
292 	case RLIMIT_DATA:
293 		if (limp->rlim_cur > MAXDSIZ)
294 			limp->rlim_cur = MAXDSIZ;
295 		if (limp->rlim_max > MAXDSIZ)
296 			limp->rlim_max = MAXDSIZ;
297 		break;
298 
299 	case RLIMIT_STACK:
300 		if (limp->rlim_cur > MAXSSIZ)
301 			limp->rlim_cur = MAXSSIZ;
302 		if (limp->rlim_max > MAXSSIZ)
303 			limp->rlim_max = MAXSSIZ;
304 		/*
305 		 * Stack is allocated to the max at exec time with only
306 		 * "rlim_cur" bytes accessible.  If stack limit is going
307 		 * up make more accessible, if going down make inaccessible.
308 		 */
309 		if (limp->rlim_cur != alimp->rlim_cur) {
310 			vm_offset_t addr;
311 			vm_size_t size;
312 			vm_prot_t prot;
313 
314 			if (limp->rlim_cur > alimp->rlim_cur) {
315 				prot = VM_PROT_ALL;
316 				size = limp->rlim_cur - alimp->rlim_cur;
317 				addr = USRSTACK - limp->rlim_cur;
318 			} else {
319 				prot = VM_PROT_NONE;
320 				size = alimp->rlim_cur - limp->rlim_cur;
321 				addr = USRSTACK - alimp->rlim_cur;
322 			}
323 			addr = trunc_page(addr);
324 			size = round_page(size);
325 			(void) vm_map_protect(&p->p_vmspace->vm_map,
326 					      addr, addr+size, prot, FALSE);
327 		}
328 		break;
329 
330 	case RLIMIT_NOFILE:
331 		if (limp->rlim_cur > maxfiles)
332 			limp->rlim_cur = maxfiles;
333 		if (limp->rlim_max > maxfiles)
334 			limp->rlim_max = maxfiles;
335 		break;
336 
337 	case RLIMIT_NPROC:
338 		if (limp->rlim_cur > maxproc)
339 			limp->rlim_cur = maxproc;
340 		if (limp->rlim_max > maxproc)
341 			limp->rlim_max = maxproc;
342 		break;
343 	}
344 	*alimp = *limp;
345 	return (0);
346 }
347 
348 struct __getrlimit_args {
349 	u_int	which;
350 	struct	rlimit *rlp;
351 };
352 /* ARGSUSED */
353 int
354 getrlimit(p, uap, retval)
355 	struct proc *p;
356 	register struct __getrlimit_args *uap;
357 	int *retval;
358 {
359 
360 	if (uap->which >= RLIM_NLIMITS)
361 		return (EINVAL);
362 	return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
363 	    sizeof (struct rlimit)));
364 }
365 
366 /*
367  * Transform the running time and tick information in proc p into user,
368  * system, and interrupt time usage.
369  */
370 void
371 calcru(p, up, sp, ip)
372 	register struct proc *p;
373 	register struct timeval *up;
374 	register struct timeval *sp;
375 	register struct timeval *ip;
376 {
377 	register u_quad_t u, st, ut, it, tot;
378 	register u_long sec, usec;
379 	register int s;
380 	struct timeval tv;
381 
382 	s = splstatclock();
383 	st = p->p_sticks;
384 	ut = p->p_uticks;
385 	it = p->p_iticks;
386 	splx(s);
387 
388 	tot = st + ut + it;
389 	if (tot == 0) {
390 		up->tv_sec = up->tv_usec = 0;
391 		sp->tv_sec = sp->tv_usec = 0;
392 		if (ip != NULL)
393 			ip->tv_sec = ip->tv_usec = 0;
394 		return;
395 	}
396 
397 	sec = p->p_rtime.tv_sec;
398 	usec = p->p_rtime.tv_usec;
399 	if (p == curproc) {
400 		/*
401 		 * Adjust for the current time slice.  This is actually fairly
402 		 * important since the error here is on the order of a time
403 		 * quantum, which is much greater than the sampling error.
404 		 */
405 		microtime(&tv);
406 		sec += tv.tv_sec - runtime.tv_sec;
407 		usec += tv.tv_usec - runtime.tv_usec;
408 	}
409 	u = sec * 1000000 + usec;
410 	st = (u * st) / tot;
411 	sp->tv_sec = st / 1000000;
412 	sp->tv_usec = st % 1000000;
413 	ut = (u * ut) / tot;
414 	up->tv_sec = ut / 1000000;
415 	up->tv_usec = ut % 1000000;
416 	if (ip != NULL) {
417 		it = (u * it) / tot;
418 		ip->tv_sec = it / 1000000;
419 		ip->tv_usec = it % 1000000;
420 	}
421 }
422 
423 struct getrusage_args {
424 	int	who;
425 	struct	rusage *rusage;
426 };
427 /* ARGSUSED */
428 int
429 getrusage(p, uap, retval)
430 	register struct proc *p;
431 	register struct getrusage_args *uap;
432 	int *retval;
433 {
434 	register struct rusage *rup;
435 
436 	switch (uap->who) {
437 
438 	case RUSAGE_SELF:
439 		rup = &p->p_stats->p_ru;
440 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
441 		break;
442 
443 	case RUSAGE_CHILDREN:
444 		rup = &p->p_stats->p_cru;
445 		break;
446 
447 	default:
448 		return (EINVAL);
449 	}
450 	return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
451 	    sizeof (struct rusage)));
452 }
453 
454 void
455 ruadd(ru, ru2)
456 	register struct rusage *ru, *ru2;
457 {
458 	register long *ip, *ip2;
459 	register int i;
460 
461 	timevaladd(&ru->ru_utime, &ru2->ru_utime);
462 	timevaladd(&ru->ru_stime, &ru2->ru_stime);
463 	if (ru->ru_maxrss < ru2->ru_maxrss)
464 		ru->ru_maxrss = ru2->ru_maxrss;
465 	ip = &ru->ru_first; ip2 = &ru2->ru_first;
466 	for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
467 		*ip++ += *ip2++;
468 }
469 
470 /*
471  * Make a copy of the plimit structure.
472  * We share these structures copy-on-write after fork,
473  * and copy when a limit is changed.
474  */
475 struct plimit *
476 limcopy(lim)
477 	struct plimit *lim;
478 {
479 	register struct plimit *copy;
480 
481 	MALLOC(copy, struct plimit *, sizeof(struct plimit),
482 	    M_SUBPROC, M_WAITOK);
483 	bcopy(lim->pl_rlimit, copy->pl_rlimit,
484 	    sizeof(struct rlimit) * RLIM_NLIMITS);
485 	copy->p_lflags = 0;
486 	copy->p_refcnt = 1;
487 	return (copy);
488 }
489