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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_resource.c,v 1.55.2.5 2001/11/03 01:41:08 ps Exp $
40 * $DragonFly: src/sys/kern/kern_resource.c,v 1.14 2003/08/26 21:09:02 rob Exp $
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
49 #include <sys/kernel.h>
50 #include <sys/resourcevar.h>
51 #include <sys/malloc.h>
56 #include <vm/vm_param.h>
59 #include <vm/vm_map.h>
61 static int donice (struct proc *chgp, int n);
63 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
64 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
65 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
66 static u_long uihash; /* size of hash table - 1 */
68 static struct uidinfo *uicreate (uid_t uid);
69 static struct uidinfo *uilookup (uid_t uid);
72 * Resource controls and accounting.
76 getpriority(struct getpriority_args *uap)
78 struct proc *curp = curproc;
80 int low = PRIO_MAX + 1;
90 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
101 else if ((pg = pgfind(uap->who)) == NULL)
103 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
104 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
111 uap->who = curp->p_ucred->cr_uid;
112 FOREACH_PROC_IN_SYSTEM(p)
113 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
114 p->p_ucred->cr_uid == uap->who &&
122 if (low == PRIO_MAX + 1)
124 uap->sysmsg_result = low;
130 setpriority(struct setpriority_args *uap)
132 struct proc *curp = curproc;
134 int found = 0, error = 0;
136 switch (uap->which) {
145 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
147 error = donice(p, uap->prio);
157 else if ((pg = pgfind(uap->who)) == NULL)
159 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
160 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
161 error = donice(p, uap->prio);
169 uap->who = curp->p_ucred->cr_uid;
170 FOREACH_PROC_IN_SYSTEM(p)
171 if (p->p_ucred->cr_uid == uap->who &&
172 PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
173 error = donice(p, uap->prio);
187 donice(struct proc *chgp, int n)
189 struct proc *curp = curproc;
190 struct ucred *cr = curp->p_ucred;
192 if (cr->cr_uid && cr->cr_ruid &&
193 cr->cr_uid != chgp->p_ucred->cr_uid &&
194 cr->cr_ruid != chgp->p_ucred->cr_uid)
200 if (n < chgp->p_nice && suser_cred(cr, 0))
203 (void)resetpriority(chgp);
208 * Set realtime priority
212 rtprio(struct rtprio_args *uap)
214 struct proc *curp = curproc;
216 struct ucred *cr = curp->p_ucred;
220 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
232 switch (uap->function) {
234 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
236 if (cr->cr_uid && cr->cr_ruid &&
237 cr->cr_uid != p->p_ucred->cr_uid &&
238 cr->cr_ruid != p->p_ucred->cr_uid)
240 /* disallow setting rtprio in most cases if not superuser */
241 if (suser_cred(cr, 0)) {
242 /* can't set someone else's */
245 /* can't set realtime priority */
247 * Realtime priority has to be restricted for reasons which should be
248 * obvious. However, for idle priority, there is a potential for
249 * system deadlock if an idleprio process gains a lock on a resource
250 * that other processes need (and the idleprio process can't run
251 * due to a CPU-bound normal process). Fix me! XXX
253 if (RTP_PRIO_IS_REALTIME(rtp.type))
260 case RTP_PRIO_REALTIME:
261 case RTP_PRIO_NORMAL:
263 if (rtp.prio > RTP_PRIO_MAX)
276 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
279 osetrlimit(struct osetrlimit_args *uap)
286 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
288 lim.rlim_cur = olim.rlim_cur;
289 lim.rlim_max = olim.rlim_max;
290 return (dosetrlimit(uap->which, &lim));
295 ogetrlimit(struct ogetrlimit_args *uap)
297 struct proc *p = curproc;
300 if (uap->which >= RLIM_NLIMITS)
302 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
303 if (olim.rlim_cur == -1)
304 olim.rlim_cur = 0x7fffffff;
305 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
306 if (olim.rlim_max == -1)
307 olim.rlim_max = 0x7fffffff;
308 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
310 #endif /* COMPAT_43 || COMPAT_SUNOS */
314 setrlimit(struct __setrlimit_args *uap)
320 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
322 return (dosetrlimit(uap->which, &alim));
326 dosetrlimit(u_int which, struct rlimit *limp)
328 struct proc *p = curproc;
329 struct rlimit *alimp;
332 if (which >= RLIM_NLIMITS)
334 alimp = &p->p_rlimit[which];
337 * Preserve historical bugs by treating negative limits as unsigned.
339 if (limp->rlim_cur < 0)
340 limp->rlim_cur = RLIM_INFINITY;
341 if (limp->rlim_max < 0)
342 limp->rlim_max = RLIM_INFINITY;
344 if (limp->rlim_cur > alimp->rlim_max ||
345 limp->rlim_max > alimp->rlim_max)
346 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
348 if (limp->rlim_cur > limp->rlim_max)
349 limp->rlim_cur = limp->rlim_max;
350 if (p->p_limit->p_refcnt > 1 &&
351 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
352 p->p_limit->p_refcnt--;
353 p->p_limit = limcopy(p->p_limit);
354 alimp = &p->p_rlimit[which];
360 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
361 p->p_limit->p_cpulimit = RLIM_INFINITY;
363 p->p_limit->p_cpulimit =
364 (rlim_t)1000000 * limp->rlim_cur;
367 if (limp->rlim_cur > maxdsiz)
368 limp->rlim_cur = maxdsiz;
369 if (limp->rlim_max > maxdsiz)
370 limp->rlim_max = maxdsiz;
374 if (limp->rlim_cur > maxssiz)
375 limp->rlim_cur = maxssiz;
376 if (limp->rlim_max > maxssiz)
377 limp->rlim_max = maxssiz;
379 * Stack is allocated to the max at exec time with only
380 * "rlim_cur" bytes accessible. If stack limit is going
381 * up make more accessible, if going down make inaccessible.
383 if (limp->rlim_cur != alimp->rlim_cur) {
388 if (limp->rlim_cur > alimp->rlim_cur) {
390 size = limp->rlim_cur - alimp->rlim_cur;
391 addr = USRSTACK - limp->rlim_cur;
394 size = alimp->rlim_cur - limp->rlim_cur;
395 addr = USRSTACK - alimp->rlim_cur;
397 addr = trunc_page(addr);
398 size = round_page(size);
399 (void) vm_map_protect(&p->p_vmspace->vm_map,
400 addr, addr+size, prot, FALSE);
405 if (limp->rlim_cur > maxfilesperproc)
406 limp->rlim_cur = maxfilesperproc;
407 if (limp->rlim_max > maxfilesperproc)
408 limp->rlim_max = maxfilesperproc;
412 if (limp->rlim_cur > maxprocperuid)
413 limp->rlim_cur = maxprocperuid;
414 if (limp->rlim_max > maxprocperuid)
415 limp->rlim_max = maxprocperuid;
416 if (limp->rlim_cur < 1)
418 if (limp->rlim_max < 1)
428 getrlimit(struct __getrlimit_args *uap)
430 struct proc *p = curproc;
432 if (uap->which >= RLIM_NLIMITS)
434 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
435 sizeof (struct rlimit)));
439 * Transform the running time and tick information in proc p into user,
440 * system, and interrupt time usage.
442 * Since we are limited to statclock tick granularity this is a statisical
443 * calculation which will be correct over the long haul, but should not be
444 * expected to measure fine grained deltas.
447 calcru(p, up, sp, ip)
453 struct thread *td = p->p_thread;
457 * Calculate at the statclock level. YYY if the thread is owned by
458 * another cpu we need to forward the request to the other cpu, or
459 * have a token to interlock the information.
462 up->tv_sec = td->td_uticks / 1000000;
463 up->tv_usec = td->td_uticks % 1000000;
464 sp->tv_sec = td->td_sticks / 1000000;
465 sp->tv_usec = td->td_sticks % 1000000;
467 ip->tv_sec = td->td_iticks / 1000000;
468 ip->tv_usec = td->td_iticks % 1000000;
475 getrusage(struct getrusage_args *uap)
477 struct proc *p = curproc;
483 rup = &p->p_stats->p_ru;
484 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
487 case RUSAGE_CHILDREN:
488 rup = &p->p_stats->p_cru;
494 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
495 sizeof (struct rusage)));
500 struct rusage *ru, *ru2;
505 timevaladd(&ru->ru_utime, &ru2->ru_utime);
506 timevaladd(&ru->ru_stime, &ru2->ru_stime);
507 if (ru->ru_maxrss < ru2->ru_maxrss)
508 ru->ru_maxrss = ru2->ru_maxrss;
509 ip = &ru->ru_first; ip2 = &ru2->ru_first;
510 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
515 * Make a copy of the plimit structure.
516 * We share these structures copy-on-write after fork,
517 * and copy when a limit is changed.
525 MALLOC(copy, struct plimit *, sizeof(struct plimit),
526 M_SUBPROC, M_WAITOK);
527 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
534 * Find the uidinfo structure for a uid. This structure is used to
535 * track the total resource consumption (process count, socket buffer
536 * size, etc.) for the uid and impose limits.
541 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
544 static struct uidinfo *
548 struct uihashhead *uipp;
552 LIST_FOREACH(uip, uipp, ui_hash)
553 if (uip->ui_uid == uid)
559 static struct uidinfo *
563 struct uidinfo *uip, *norace;
565 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_NOWAIT);
567 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
569 * if we M_WAITOK we must look afterwards or risk
572 norace = uilookup(uid);
573 if (norace != NULL) {
574 FREE(uip, M_UIDINFO);
578 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
604 if (--uip->ui_ref == 0) {
605 if (uip->ui_sbsize != 0)
606 /* XXX no %qd in kernel. Truncate. */
607 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
608 uip->ui_uid, (long)uip->ui_sbsize);
609 if (uip->ui_proccnt != 0)
610 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
611 uip->ui_uid, uip->ui_proccnt);
612 LIST_REMOVE(uip, ui_hash);
613 FREE(uip, M_UIDINFO);
620 * Change the count associated with number of processes
621 * a given user is using. When 'max' is 0, don't enforce a limit
624 chgproccnt(uip, diff, max)
629 /* don't allow them to exceed max, but allow subtraction */
630 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
632 uip->ui_proccnt += diff;
633 if (uip->ui_proccnt < 0)
634 printf("negative proccnt for uid = %d\n", uip->ui_uid);
639 * Change the total socket buffer size a user has used.
642 chgsbsize(uip, hiwat, to, max)
652 new = uip->ui_sbsize + to - *hiwat;
653 /* don't allow them to exceed max, but allow subtraction */
654 if (to > *hiwat && new > max) {
658 uip->ui_sbsize = new;
660 if (uip->ui_sbsize < 0)
661 printf("negative sbsize for uid = %d\n", uip->ui_uid);