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.2 2003/06/17 04:28:41 dillon 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 __P((struct proc *curp, struct proc *chgp, int n));
62 /* dosetrlimit non-static: Needed by SysVR4 emulator */
63 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
65 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
66 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
67 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
68 static u_long uihash; /* size of hash table - 1 */
70 static struct uidinfo *uicreate __P((uid_t uid));
71 static struct uidinfo *uilookup __P((uid_t uid));
74 * Resource controls and accounting.
77 #ifndef _SYS_SYSPROTO_H_
78 struct getpriority_args {
84 getpriority(curp, uap)
86 register struct getpriority_args *uap;
88 register struct proc *p;
89 register int low = PRIO_MAX + 1;
100 if (!PRISON_CHECK(curp, p))
106 register struct pgrp *pg;
110 else if ((pg = pgfind(uap->who)) == NULL)
112 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
113 if ((PRISON_CHECK(curp, p) && p->p_nice < low))
121 uap->who = curp->p_ucred->cr_uid;
122 LIST_FOREACH(p, &allproc, p_list)
123 if (PRISON_CHECK(curp, p) &&
124 p->p_ucred->cr_uid == uap->who &&
132 if (low == PRIO_MAX + 1)
134 curp->p_retval[0] = low;
138 #ifndef _SYS_SYSPROTO_H_
139 struct setpriority_args {
147 setpriority(curp, uap)
149 register struct setpriority_args *uap;
151 register struct proc *p;
152 int found = 0, error = 0;
154 switch (uap->which) {
163 if (!PRISON_CHECK(curp, p))
165 error = donice(curp, p, uap->prio);
170 register struct pgrp *pg;
174 else if ((pg = pgfind(uap->who)) == NULL)
176 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
177 if (PRISON_CHECK(curp, p)) {
178 error = donice(curp, p, uap->prio);
187 uap->who = curp->p_ucred->cr_uid;
188 LIST_FOREACH(p, &allproc, p_list)
189 if (p->p_ucred->cr_uid == uap->who &&
190 PRISON_CHECK(curp, p)) {
191 error = donice(curp, p, uap->prio);
205 donice(curp, chgp, n)
206 register struct proc *curp, *chgp;
209 register struct pcred *pcred = curp->p_cred;
211 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
212 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
213 pcred->p_ruid != chgp->p_ucred->cr_uid)
219 if (n < chgp->p_nice && suser(curp))
222 (void)resetpriority(chgp);
226 /* rtprio system call */
227 #ifndef _SYS_SYSPROTO_H_
236 * Set realtime priority
243 register struct rtprio_args *uap;
245 register struct proc *p;
246 register struct pcred *pcred = curp->p_cred;
250 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
262 switch (uap->function) {
264 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
266 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
267 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid &&
268 pcred->p_ruid != p->p_ucred->cr_uid)
270 /* disallow setting rtprio in most cases if not superuser */
272 /* can't set someone else's */
275 /* can't set realtime priority */
277 * Realtime priority has to be restricted for reasons which should be
278 * obvious. However, for idle priority, there is a potential for
279 * system deadlock if an idleprio process gains a lock on a resource
280 * that other processes need (and the idleprio process can't run
281 * due to a CPU-bound normal process). Fix me! XXX
284 if (RTP_PRIO_IS_REALTIME(rtp.type))
286 if (rtp.type != RTP_PRIO_NORMAL)
293 case RTP_PRIO_REALTIME:
294 case RTP_PRIO_NORMAL:
296 if (rtp.prio > RTP_PRIO_MAX)
309 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
310 #ifndef _SYS_SYSPROTO_H_
311 struct osetrlimit_args {
320 register struct osetrlimit_args *uap;
327 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
329 lim.rlim_cur = olim.rlim_cur;
330 lim.rlim_max = olim.rlim_max;
331 return (dosetrlimit(p, uap->which, &lim));
334 #ifndef _SYS_SYSPROTO_H_
335 struct ogetrlimit_args {
344 register struct ogetrlimit_args *uap;
348 if (uap->which >= RLIM_NLIMITS)
350 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
351 if (olim.rlim_cur == -1)
352 olim.rlim_cur = 0x7fffffff;
353 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
354 if (olim.rlim_max == -1)
355 olim.rlim_max = 0x7fffffff;
356 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
358 #endif /* COMPAT_43 || COMPAT_SUNOS */
360 #ifndef _SYS_SYSPROTO_H_
361 struct __setrlimit_args {
370 register struct __setrlimit_args *uap;
376 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
378 return (dosetrlimit(p, uap->which, &alim));
382 dosetrlimit(p, which, limp)
387 register struct rlimit *alimp;
390 if (which >= RLIM_NLIMITS)
392 alimp = &p->p_rlimit[which];
395 * Preserve historical bugs by treating negative limits as unsigned.
397 if (limp->rlim_cur < 0)
398 limp->rlim_cur = RLIM_INFINITY;
399 if (limp->rlim_max < 0)
400 limp->rlim_max = RLIM_INFINITY;
402 if (limp->rlim_cur > alimp->rlim_max ||
403 limp->rlim_max > alimp->rlim_max)
404 if ((error = suser_xxx(0, p, PRISON_ROOT)))
406 if (limp->rlim_cur > limp->rlim_max)
407 limp->rlim_cur = limp->rlim_max;
408 if (p->p_limit->p_refcnt > 1 &&
409 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
410 p->p_limit->p_refcnt--;
411 p->p_limit = limcopy(p->p_limit);
412 alimp = &p->p_rlimit[which];
418 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
419 p->p_limit->p_cpulimit = RLIM_INFINITY;
421 p->p_limit->p_cpulimit =
422 (rlim_t)1000000 * limp->rlim_cur;
425 if (limp->rlim_cur > maxdsiz)
426 limp->rlim_cur = maxdsiz;
427 if (limp->rlim_max > maxdsiz)
428 limp->rlim_max = maxdsiz;
432 if (limp->rlim_cur > maxssiz)
433 limp->rlim_cur = maxssiz;
434 if (limp->rlim_max > maxssiz)
435 limp->rlim_max = maxssiz;
437 * Stack is allocated to the max at exec time with only
438 * "rlim_cur" bytes accessible. If stack limit is going
439 * up make more accessible, if going down make inaccessible.
441 if (limp->rlim_cur != alimp->rlim_cur) {
446 if (limp->rlim_cur > alimp->rlim_cur) {
448 size = limp->rlim_cur - alimp->rlim_cur;
449 addr = USRSTACK - limp->rlim_cur;
452 size = alimp->rlim_cur - limp->rlim_cur;
453 addr = USRSTACK - alimp->rlim_cur;
455 addr = trunc_page(addr);
456 size = round_page(size);
457 (void) vm_map_protect(&p->p_vmspace->vm_map,
458 addr, addr+size, prot, FALSE);
463 if (limp->rlim_cur > maxfilesperproc)
464 limp->rlim_cur = maxfilesperproc;
465 if (limp->rlim_max > maxfilesperproc)
466 limp->rlim_max = maxfilesperproc;
470 if (limp->rlim_cur > maxprocperuid)
471 limp->rlim_cur = maxprocperuid;
472 if (limp->rlim_max > maxprocperuid)
473 limp->rlim_max = maxprocperuid;
474 if (limp->rlim_cur < 1)
476 if (limp->rlim_max < 1)
484 #ifndef _SYS_SYSPROTO_H_
485 struct __getrlimit_args {
494 register struct __getrlimit_args *uap;
497 if (uap->which >= RLIM_NLIMITS)
499 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
500 sizeof (struct rlimit)));
504 * Transform the running time and tick information in proc p into user,
505 * system, and interrupt time usage.
508 calcru(p, up, sp, ip)
514 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */
515 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu;
519 /* XXX: why spl-protect ? worst case is an off-by-one report */
535 * Adjust for the current time slice. This is actually fairly
536 * important since the error here is on the order of a time
537 * quantum, which is much greater than the sampling error.
540 if (timevalcmp(&tv, &switchtime, <))
541 printf("microuptime() went backwards (%ld.%06ld -> %ld.%06ld)\n",
542 switchtime.tv_sec, switchtime.tv_usec,
543 tv.tv_sec, tv.tv_usec);
545 tu += (tv.tv_usec - switchtime.tv_usec) +
546 (tv.tv_sec - switchtime.tv_sec) * (int64_t)1000000;
548 ptu = p->p_uu + p->p_su + p->p_iu;
549 if (tu < ptu || (int64_t)tu < 0) {
550 /* XXX no %qd in kernel. Truncate. */
551 printf("calcru: negative time of %ld usec for pid %d (%s)\n",
552 (long)tu, p->p_pid, p->p_comm);
561 /* Enforce monotonicity. */
562 if (uu < p->p_uu || su < p->p_su || iu < p->p_iu) {
565 else if (uu + p->p_su + p->p_iu > tu)
566 uu = tu - p->p_su - p->p_iu;
570 su = ((tu - uu) * st) / (st + it);
573 else if (uu + su + p->p_iu > tu)
574 su = tu - uu - p->p_iu;
576 KASSERT(uu + su + p->p_iu <= tu,
577 ("calcru: monotonisation botch 1"));
579 KASSERT(iu >= p->p_iu,
580 ("calcru: monotonisation botch 2"));
586 up->tv_sec = uu / 1000000;
587 up->tv_usec = uu % 1000000;
588 sp->tv_sec = su / 1000000;
589 sp->tv_usec = su % 1000000;
591 ip->tv_sec = iu / 1000000;
592 ip->tv_usec = iu % 1000000;
596 #ifndef _SYS_SYSPROTO_H_
597 struct getrusage_args {
599 struct rusage *rusage;
605 register struct proc *p;
606 register struct getrusage_args *uap;
608 register struct rusage *rup;
613 rup = &p->p_stats->p_ru;
614 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
617 case RUSAGE_CHILDREN:
618 rup = &p->p_stats->p_cru;
624 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
625 sizeof (struct rusage)));
630 register struct rusage *ru, *ru2;
632 register long *ip, *ip2;
635 timevaladd(&ru->ru_utime, &ru2->ru_utime);
636 timevaladd(&ru->ru_stime, &ru2->ru_stime);
637 if (ru->ru_maxrss < ru2->ru_maxrss)
638 ru->ru_maxrss = ru2->ru_maxrss;
639 ip = &ru->ru_first; ip2 = &ru2->ru_first;
640 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
645 * Make a copy of the plimit structure.
646 * We share these structures copy-on-write after fork,
647 * and copy when a limit is changed.
653 register struct plimit *copy;
655 MALLOC(copy, struct plimit *, sizeof(struct plimit),
656 M_SUBPROC, M_WAITOK);
657 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
664 * Find the uidinfo structure for a uid. This structure is used to
665 * track the total resource consumption (process count, socket buffer
666 * size, etc.) for the uid and impose limits.
671 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
674 static struct uidinfo *
678 struct uihashhead *uipp;
682 LIST_FOREACH(uip, uipp, ui_hash)
683 if (uip->ui_uid == uid)
689 static struct uidinfo *
693 struct uidinfo *uip, *norace;
695 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_NOWAIT);
697 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
699 * if we M_WAITOK we must look afterwards or risk
702 norace = uilookup(uid);
703 if (norace != NULL) {
704 FREE(uip, M_UIDINFO);
708 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
734 if (--uip->ui_ref == 0) {
735 if (uip->ui_sbsize != 0)
736 /* XXX no %qd in kernel. Truncate. */
737 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
738 uip->ui_uid, (long)uip->ui_sbsize);
739 if (uip->ui_proccnt != 0)
740 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
741 uip->ui_uid, uip->ui_proccnt);
742 LIST_REMOVE(uip, ui_hash);
743 FREE(uip, M_UIDINFO);
750 * Change the count associated with number of processes
751 * a given user is using. When 'max' is 0, don't enforce a limit
754 chgproccnt(uip, diff, max)
759 /* don't allow them to exceed max, but allow subtraction */
760 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
762 uip->ui_proccnt += diff;
763 if (uip->ui_proccnt < 0)
764 printf("negative proccnt for uid = %d\n", uip->ui_uid);
769 * Change the total socket buffer size a user has used.
772 chgsbsize(uip, hiwat, to, max)
782 new = uip->ui_sbsize + to - *hiwat;
783 /* don't allow them to exceed max, but allow subtraction */
784 if (to > *hiwat && new > max) {
788 uip->ui_sbsize = new;
790 if (uip->ui_sbsize < 0)
791 printf("negative sbsize for uid = %d\n", uip->ui_uid);