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 $
42 #include "opt_compat.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
49 #include <sys/resourcevar.h>
50 #include <sys/malloc.h>
55 #include <vm/vm_param.h>
58 #include <vm/vm_map.h>
60 static int donice __P((struct proc *curp, struct proc *chgp, int n));
61 /* dosetrlimit non-static: Needed by SysVR4 emulator */
62 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
64 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
65 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
66 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
67 static u_long uihash; /* size of hash table - 1 */
69 static struct uidinfo *uicreate __P((uid_t uid));
70 static struct uidinfo *uilookup __P((uid_t uid));
73 * Resource controls and accounting.
76 #ifndef _SYS_SYSPROTO_H_
77 struct getpriority_args {
83 getpriority(curp, uap)
85 register struct getpriority_args *uap;
87 register struct proc *p;
88 register int low = PRIO_MAX + 1;
99 if (!PRISON_CHECK(curp, p))
105 register struct pgrp *pg;
109 else if ((pg = pgfind(uap->who)) == NULL)
111 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
112 if ((PRISON_CHECK(curp, p) && p->p_nice < low))
120 uap->who = curp->p_ucred->cr_uid;
121 LIST_FOREACH(p, &allproc, p_list)
122 if (PRISON_CHECK(curp, p) &&
123 p->p_ucred->cr_uid == uap->who &&
131 if (low == PRIO_MAX + 1)
133 curp->p_retval[0] = low;
137 #ifndef _SYS_SYSPROTO_H_
138 struct setpriority_args {
146 setpriority(curp, uap)
148 register struct setpriority_args *uap;
150 register struct proc *p;
151 int found = 0, error = 0;
153 switch (uap->which) {
162 if (!PRISON_CHECK(curp, p))
164 error = donice(curp, p, uap->prio);
169 register struct pgrp *pg;
173 else if ((pg = pgfind(uap->who)) == NULL)
175 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
176 if (PRISON_CHECK(curp, p)) {
177 error = donice(curp, p, uap->prio);
186 uap->who = curp->p_ucred->cr_uid;
187 LIST_FOREACH(p, &allproc, p_list)
188 if (p->p_ucred->cr_uid == uap->who &&
189 PRISON_CHECK(curp, p)) {
190 error = donice(curp, p, uap->prio);
204 donice(curp, chgp, n)
205 register struct proc *curp, *chgp;
208 register struct pcred *pcred = curp->p_cred;
210 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
211 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
212 pcred->p_ruid != chgp->p_ucred->cr_uid)
218 if (n < chgp->p_nice && suser(curp))
221 (void)resetpriority(chgp);
225 /* rtprio system call */
226 #ifndef _SYS_SYSPROTO_H_
235 * Set realtime priority
242 register struct rtprio_args *uap;
244 register struct proc *p;
245 register struct pcred *pcred = curp->p_cred;
249 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
261 switch (uap->function) {
263 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
265 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
266 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid &&
267 pcred->p_ruid != p->p_ucred->cr_uid)
269 /* disallow setting rtprio in most cases if not superuser */
271 /* can't set someone else's */
274 /* can't set realtime priority */
276 * Realtime priority has to be restricted for reasons which should be
277 * obvious. However, for idle priority, there is a potential for
278 * system deadlock if an idleprio process gains a lock on a resource
279 * that other processes need (and the idleprio process can't run
280 * due to a CPU-bound normal process). Fix me! XXX
283 if (RTP_PRIO_IS_REALTIME(rtp.type))
285 if (rtp.type != RTP_PRIO_NORMAL)
292 case RTP_PRIO_REALTIME:
293 case RTP_PRIO_NORMAL:
295 if (rtp.prio > RTP_PRIO_MAX)
308 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
309 #ifndef _SYS_SYSPROTO_H_
310 struct osetrlimit_args {
319 register struct osetrlimit_args *uap;
326 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
328 lim.rlim_cur = olim.rlim_cur;
329 lim.rlim_max = olim.rlim_max;
330 return (dosetrlimit(p, uap->which, &lim));
333 #ifndef _SYS_SYSPROTO_H_
334 struct ogetrlimit_args {
343 register struct ogetrlimit_args *uap;
347 if (uap->which >= RLIM_NLIMITS)
349 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
350 if (olim.rlim_cur == -1)
351 olim.rlim_cur = 0x7fffffff;
352 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
353 if (olim.rlim_max == -1)
354 olim.rlim_max = 0x7fffffff;
355 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
357 #endif /* COMPAT_43 || COMPAT_SUNOS */
359 #ifndef _SYS_SYSPROTO_H_
360 struct __setrlimit_args {
369 register struct __setrlimit_args *uap;
375 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
377 return (dosetrlimit(p, uap->which, &alim));
381 dosetrlimit(p, which, limp)
386 register struct rlimit *alimp;
389 if (which >= RLIM_NLIMITS)
391 alimp = &p->p_rlimit[which];
394 * Preserve historical bugs by treating negative limits as unsigned.
396 if (limp->rlim_cur < 0)
397 limp->rlim_cur = RLIM_INFINITY;
398 if (limp->rlim_max < 0)
399 limp->rlim_max = RLIM_INFINITY;
401 if (limp->rlim_cur > alimp->rlim_max ||
402 limp->rlim_max > alimp->rlim_max)
403 if ((error = suser_xxx(0, p, PRISON_ROOT)))
405 if (limp->rlim_cur > limp->rlim_max)
406 limp->rlim_cur = limp->rlim_max;
407 if (p->p_limit->p_refcnt > 1 &&
408 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
409 p->p_limit->p_refcnt--;
410 p->p_limit = limcopy(p->p_limit);
411 alimp = &p->p_rlimit[which];
417 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
418 p->p_limit->p_cpulimit = RLIM_INFINITY;
420 p->p_limit->p_cpulimit =
421 (rlim_t)1000000 * limp->rlim_cur;
424 if (limp->rlim_cur > maxdsiz)
425 limp->rlim_cur = maxdsiz;
426 if (limp->rlim_max > maxdsiz)
427 limp->rlim_max = maxdsiz;
431 if (limp->rlim_cur > maxssiz)
432 limp->rlim_cur = maxssiz;
433 if (limp->rlim_max > maxssiz)
434 limp->rlim_max = maxssiz;
436 * Stack is allocated to the max at exec time with only
437 * "rlim_cur" bytes accessible. If stack limit is going
438 * up make more accessible, if going down make inaccessible.
440 if (limp->rlim_cur != alimp->rlim_cur) {
445 if (limp->rlim_cur > alimp->rlim_cur) {
447 size = limp->rlim_cur - alimp->rlim_cur;
448 addr = USRSTACK - limp->rlim_cur;
451 size = alimp->rlim_cur - limp->rlim_cur;
452 addr = USRSTACK - alimp->rlim_cur;
454 addr = trunc_page(addr);
455 size = round_page(size);
456 (void) vm_map_protect(&p->p_vmspace->vm_map,
457 addr, addr+size, prot, FALSE);
462 if (limp->rlim_cur > maxfilesperproc)
463 limp->rlim_cur = maxfilesperproc;
464 if (limp->rlim_max > maxfilesperproc)
465 limp->rlim_max = maxfilesperproc;
469 if (limp->rlim_cur > maxprocperuid)
470 limp->rlim_cur = maxprocperuid;
471 if (limp->rlim_max > maxprocperuid)
472 limp->rlim_max = maxprocperuid;
473 if (limp->rlim_cur < 1)
475 if (limp->rlim_max < 1)
483 #ifndef _SYS_SYSPROTO_H_
484 struct __getrlimit_args {
493 register struct __getrlimit_args *uap;
496 if (uap->which >= RLIM_NLIMITS)
498 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
499 sizeof (struct rlimit)));
503 * Transform the running time and tick information in proc p into user,
504 * system, and interrupt time usage.
507 calcru(p, up, sp, ip)
513 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */
514 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu;
518 /* XXX: why spl-protect ? worst case is an off-by-one report */
534 * Adjust for the current time slice. This is actually fairly
535 * important since the error here is on the order of a time
536 * quantum, which is much greater than the sampling error.
539 if (timevalcmp(&tv, &switchtime, <))
540 printf("microuptime() went backwards (%ld.%06ld -> %ld.%06ld)\n",
541 switchtime.tv_sec, switchtime.tv_usec,
542 tv.tv_sec, tv.tv_usec);
544 tu += (tv.tv_usec - switchtime.tv_usec) +
545 (tv.tv_sec - switchtime.tv_sec) * (int64_t)1000000;
547 ptu = p->p_uu + p->p_su + p->p_iu;
548 if (tu < ptu || (int64_t)tu < 0) {
549 /* XXX no %qd in kernel. Truncate. */
550 printf("calcru: negative time of %ld usec for pid %d (%s)\n",
551 (long)tu, p->p_pid, p->p_comm);
560 /* Enforce monotonicity. */
561 if (uu < p->p_uu || su < p->p_su || iu < p->p_iu) {
564 else if (uu + p->p_su + p->p_iu > tu)
565 uu = tu - p->p_su - p->p_iu;
569 su = ((tu - uu) * st) / (st + it);
572 else if (uu + su + p->p_iu > tu)
573 su = tu - uu - p->p_iu;
575 KASSERT(uu + su + p->p_iu <= tu,
576 ("calcru: monotonisation botch 1"));
578 KASSERT(iu >= p->p_iu,
579 ("calcru: monotonisation botch 2"));
585 up->tv_sec = uu / 1000000;
586 up->tv_usec = uu % 1000000;
587 sp->tv_sec = su / 1000000;
588 sp->tv_usec = su % 1000000;
590 ip->tv_sec = iu / 1000000;
591 ip->tv_usec = iu % 1000000;
595 #ifndef _SYS_SYSPROTO_H_
596 struct getrusage_args {
598 struct rusage *rusage;
604 register struct proc *p;
605 register struct getrusage_args *uap;
607 register struct rusage *rup;
612 rup = &p->p_stats->p_ru;
613 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
616 case RUSAGE_CHILDREN:
617 rup = &p->p_stats->p_cru;
623 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
624 sizeof (struct rusage)));
629 register struct rusage *ru, *ru2;
631 register long *ip, *ip2;
634 timevaladd(&ru->ru_utime, &ru2->ru_utime);
635 timevaladd(&ru->ru_stime, &ru2->ru_stime);
636 if (ru->ru_maxrss < ru2->ru_maxrss)
637 ru->ru_maxrss = ru2->ru_maxrss;
638 ip = &ru->ru_first; ip2 = &ru2->ru_first;
639 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
644 * Make a copy of the plimit structure.
645 * We share these structures copy-on-write after fork,
646 * and copy when a limit is changed.
652 register struct plimit *copy;
654 MALLOC(copy, struct plimit *, sizeof(struct plimit),
655 M_SUBPROC, M_WAITOK);
656 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
663 * Find the uidinfo structure for a uid. This structure is used to
664 * track the total resource consumption (process count, socket buffer
665 * size, etc.) for the uid and impose limits.
670 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
673 static struct uidinfo *
677 struct uihashhead *uipp;
681 LIST_FOREACH(uip, uipp, ui_hash)
682 if (uip->ui_uid == uid)
688 static struct uidinfo *
692 struct uidinfo *uip, *norace;
694 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_NOWAIT);
696 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
698 * if we M_WAITOK we must look afterwards or risk
701 norace = uilookup(uid);
702 if (norace != NULL) {
703 FREE(uip, M_UIDINFO);
707 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
733 if (--uip->ui_ref == 0) {
734 if (uip->ui_sbsize != 0)
735 /* XXX no %qd in kernel. Truncate. */
736 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
737 uip->ui_uid, (long)uip->ui_sbsize);
738 if (uip->ui_proccnt != 0)
739 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
740 uip->ui_uid, uip->ui_proccnt);
741 LIST_REMOVE(uip, ui_hash);
742 FREE(uip, M_UIDINFO);
749 * Change the count associated with number of processes
750 * a given user is using. When 'max' is 0, don't enforce a limit
753 chgproccnt(uip, diff, max)
758 /* don't allow them to exceed max, but allow subtraction */
759 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
761 uip->ui_proccnt += diff;
762 if (uip->ui_proccnt < 0)
763 printf("negative proccnt for uid = %d\n", uip->ui_uid);
768 * Change the total socket buffer size a user has used.
771 chgsbsize(uip, hiwat, to, max)
781 new = uip->ui_sbsize + to - *hiwat;
782 /* don't allow them to exceed max, but allow subtraction */
783 if (to > *hiwat && new > max) {
787 uip->ui_sbsize = new;
789 if (uip->ui_sbsize < 0)
790 printf("negative sbsize for uid = %d\n", uip->ui_uid);