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.7 2003/06/30 19:50:31 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 *chgp, int n));
62 /* dosetrlimit non-static: Needed by SysVR4 emulator */
63 int dosetrlimit __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(struct getpriority_args *uap)
86 struct proc *curp = curproc;
88 int low = PRIO_MAX + 1;
98 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
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_ucred, p->p_ucred) && p->p_nice < low))
119 uap->who = curp->p_ucred->cr_uid;
120 LIST_FOREACH(p, &allproc, p_list)
121 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
122 p->p_ucred->cr_uid == uap->who &&
130 if (low == PRIO_MAX + 1)
132 curp->p_retval[0] = low;
136 #ifndef _SYS_SYSPROTO_H_
137 struct setpriority_args {
145 setpriority(struct setpriority_args *uap)
147 struct proc *curp = curproc;
149 int found = 0, error = 0;
151 switch (uap->which) {
160 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
162 error = donice(p, uap->prio);
168 register struct pgrp *pg;
172 else if ((pg = pgfind(uap->who)) == NULL)
174 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
175 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
176 error = donice(p, uap->prio);
184 uap->who = curp->p_ucred->cr_uid;
185 LIST_FOREACH(p, &allproc, p_list)
186 if (p->p_ucred->cr_uid == uap->who &&
187 PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
188 error = donice(p, uap->prio);
202 donice(struct proc *chgp, int n)
204 struct proc *curp = curproc;
205 struct ucred *cr = curp->p_ucred;
207 if (cr->cr_uid && cr->cr_ruid &&
208 cr->cr_uid != chgp->p_ucred->cr_uid &&
209 cr->cr_ruid != chgp->p_ucred->cr_uid)
215 if (n < chgp->p_nice && suser_cred(cr, 0))
218 (void)resetpriority(chgp);
222 /* rtprio system call */
223 #ifndef _SYS_SYSPROTO_H_
232 * Set realtime priority
237 rtprio(register struct rtprio_args *uap)
239 struct proc *curp = curproc;
241 struct ucred *cr = curp->p_ucred;
245 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
257 switch (uap->function) {
259 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
261 if (cr->cr_uid && cr->cr_ruid &&
262 cr->cr_uid != p->p_ucred->cr_uid &&
263 cr->cr_ruid != p->p_ucred->cr_uid)
265 /* disallow setting rtprio in most cases if not superuser */
266 if (suser_cred(cr, 0)) {
267 /* can't set someone else's */
270 /* can't set realtime priority */
272 * Realtime priority has to be restricted for reasons which should be
273 * obvious. However, for idle priority, there is a potential for
274 * system deadlock if an idleprio process gains a lock on a resource
275 * that other processes need (and the idleprio process can't run
276 * due to a CPU-bound normal process). Fix me! XXX
278 if (RTP_PRIO_IS_REALTIME(rtp.type))
285 case RTP_PRIO_REALTIME:
286 case RTP_PRIO_NORMAL:
288 if (rtp.prio > RTP_PRIO_MAX)
301 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
302 #ifndef _SYS_SYSPROTO_H_
303 struct osetrlimit_args {
310 osetrlimit(struct osetrlimit_args *uap)
317 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
319 lim.rlim_cur = olim.rlim_cur;
320 lim.rlim_max = olim.rlim_max;
321 return (dosetrlimit(uap->which, &lim));
324 #ifndef _SYS_SYSPROTO_H_
325 struct ogetrlimit_args {
332 ogetrlimit(struct ogetrlimit_args *uap)
334 struct proc *p = curproc;
337 if (uap->which >= RLIM_NLIMITS)
339 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
340 if (olim.rlim_cur == -1)
341 olim.rlim_cur = 0x7fffffff;
342 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
343 if (olim.rlim_max == -1)
344 olim.rlim_max = 0x7fffffff;
345 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
347 #endif /* COMPAT_43 || COMPAT_SUNOS */
349 #ifndef _SYS_SYSPROTO_H_
350 struct __setrlimit_args {
357 setrlimit(struct __setrlimit_args *uap)
363 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
365 return (dosetrlimit(uap->which, &alim));
369 dosetrlimit(u_int which, struct rlimit *limp)
371 struct proc *p = curproc;
372 struct rlimit *alimp;
375 if (which >= RLIM_NLIMITS)
377 alimp = &p->p_rlimit[which];
380 * Preserve historical bugs by treating negative limits as unsigned.
382 if (limp->rlim_cur < 0)
383 limp->rlim_cur = RLIM_INFINITY;
384 if (limp->rlim_max < 0)
385 limp->rlim_max = RLIM_INFINITY;
387 if (limp->rlim_cur > alimp->rlim_max ||
388 limp->rlim_max > alimp->rlim_max)
389 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
391 if (limp->rlim_cur > limp->rlim_max)
392 limp->rlim_cur = limp->rlim_max;
393 if (p->p_limit->p_refcnt > 1 &&
394 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
395 p->p_limit->p_refcnt--;
396 p->p_limit = limcopy(p->p_limit);
397 alimp = &p->p_rlimit[which];
403 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
404 p->p_limit->p_cpulimit = RLIM_INFINITY;
406 p->p_limit->p_cpulimit =
407 (rlim_t)1000000 * limp->rlim_cur;
410 if (limp->rlim_cur > maxdsiz)
411 limp->rlim_cur = maxdsiz;
412 if (limp->rlim_max > maxdsiz)
413 limp->rlim_max = maxdsiz;
417 if (limp->rlim_cur > maxssiz)
418 limp->rlim_cur = maxssiz;
419 if (limp->rlim_max > maxssiz)
420 limp->rlim_max = maxssiz;
422 * Stack is allocated to the max at exec time with only
423 * "rlim_cur" bytes accessible. If stack limit is going
424 * up make more accessible, if going down make inaccessible.
426 if (limp->rlim_cur != alimp->rlim_cur) {
431 if (limp->rlim_cur > alimp->rlim_cur) {
433 size = limp->rlim_cur - alimp->rlim_cur;
434 addr = USRSTACK - limp->rlim_cur;
437 size = alimp->rlim_cur - limp->rlim_cur;
438 addr = USRSTACK - alimp->rlim_cur;
440 addr = trunc_page(addr);
441 size = round_page(size);
442 (void) vm_map_protect(&p->p_vmspace->vm_map,
443 addr, addr+size, prot, FALSE);
448 if (limp->rlim_cur > maxfilesperproc)
449 limp->rlim_cur = maxfilesperproc;
450 if (limp->rlim_max > maxfilesperproc)
451 limp->rlim_max = maxfilesperproc;
455 if (limp->rlim_cur > maxprocperuid)
456 limp->rlim_cur = maxprocperuid;
457 if (limp->rlim_max > maxprocperuid)
458 limp->rlim_max = maxprocperuid;
459 if (limp->rlim_cur < 1)
461 if (limp->rlim_max < 1)
469 #ifndef _SYS_SYSPROTO_H_
470 struct __getrlimit_args {
477 getrlimit(struct __getrlimit_args *uap)
479 struct proc *p = curproc;
481 if (uap->which >= RLIM_NLIMITS)
483 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
484 sizeof (struct rlimit)));
488 * Transform the running time and tick information in proc p into user,
489 * system, and interrupt time usage.
491 * Since we are limited to statclock tick granularity this is a statisical
492 * calculation which will be correct over the long haul, but should not be
493 * expected to measure fine grained deltas.
496 calcru(p, up, sp, ip)
502 struct thread *td = p->p_thread;
506 * Calculate at the statclock level. YYY if the thread is owned by
507 * another cpu we need to forward the request to the other cpu, or
508 * have a token to interlock the information.
511 up->tv_sec = td->td_uticks / 1000000;
512 up->tv_usec = td->td_uticks % 1000000;
513 sp->tv_sec = td->td_sticks / 1000000;
514 sp->tv_usec = td->td_sticks % 1000000;
516 ip->tv_sec = td->td_iticks / 1000000;
517 ip->tv_usec = td->td_iticks % 1000000;
522 #ifndef _SYS_SYSPROTO_H_
523 struct getrusage_args {
525 struct rusage *rusage;
530 getrusage(struct getrusage_args *uap)
532 struct proc *p = curproc;
538 rup = &p->p_stats->p_ru;
539 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
542 case RUSAGE_CHILDREN:
543 rup = &p->p_stats->p_cru;
549 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
550 sizeof (struct rusage)));
555 register struct rusage *ru, *ru2;
557 register long *ip, *ip2;
560 timevaladd(&ru->ru_utime, &ru2->ru_utime);
561 timevaladd(&ru->ru_stime, &ru2->ru_stime);
562 if (ru->ru_maxrss < ru2->ru_maxrss)
563 ru->ru_maxrss = ru2->ru_maxrss;
564 ip = &ru->ru_first; ip2 = &ru2->ru_first;
565 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
570 * Make a copy of the plimit structure.
571 * We share these structures copy-on-write after fork,
572 * and copy when a limit is changed.
578 register struct plimit *copy;
580 MALLOC(copy, struct plimit *, sizeof(struct plimit),
581 M_SUBPROC, M_WAITOK);
582 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
589 * Find the uidinfo structure for a uid. This structure is used to
590 * track the total resource consumption (process count, socket buffer
591 * size, etc.) for the uid and impose limits.
596 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
599 static struct uidinfo *
603 struct uihashhead *uipp;
607 LIST_FOREACH(uip, uipp, ui_hash)
608 if (uip->ui_uid == uid)
614 static struct uidinfo *
618 struct uidinfo *uip, *norace;
620 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_NOWAIT);
622 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
624 * if we M_WAITOK we must look afterwards or risk
627 norace = uilookup(uid);
628 if (norace != NULL) {
629 FREE(uip, M_UIDINFO);
633 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
659 if (--uip->ui_ref == 0) {
660 if (uip->ui_sbsize != 0)
661 /* XXX no %qd in kernel. Truncate. */
662 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
663 uip->ui_uid, (long)uip->ui_sbsize);
664 if (uip->ui_proccnt != 0)
665 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
666 uip->ui_uid, uip->ui_proccnt);
667 LIST_REMOVE(uip, ui_hash);
668 FREE(uip, M_UIDINFO);
675 * Change the count associated with number of processes
676 * a given user is using. When 'max' is 0, don't enforce a limit
679 chgproccnt(uip, diff, max)
684 /* don't allow them to exceed max, but allow subtraction */
685 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
687 uip->ui_proccnt += diff;
688 if (uip->ui_proccnt < 0)
689 printf("negative proccnt for uid = %d\n", uip->ui_uid);
694 * Change the total socket buffer size a user has used.
697 chgsbsize(uip, hiwat, to, max)
707 new = uip->ui_sbsize + to - *hiwat;
708 /* don't allow them to exceed max, but allow subtraction */
709 if (to > *hiwat && new > max) {
713 uip->ui_sbsize = new;
715 if (uip->ui_sbsize < 0)
716 printf("negative sbsize for uid = %d\n", uip->ui_uid);