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