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.19 2004/04/10 20:55:23 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/kern_syscall.h>
50 #include <sys/kernel.h>
51 #include <sys/resourcevar.h>
52 #include <sys/malloc.h>
57 #include <vm/vm_param.h>
60 #include <vm/vm_map.h>
62 #include <sys/thread2.h>
64 static int donice (struct proc *chgp, int n);
66 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
67 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
68 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
69 static u_long uihash; /* size of hash table - 1 */
71 static struct uidinfo *uicreate (uid_t uid);
72 static struct uidinfo *uilookup (uid_t uid);
75 * Resource controls and accounting.
79 getpriority(struct getpriority_args *uap)
81 struct proc *curp = curproc;
83 int low = PRIO_MAX + 1;
93 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
104 else if ((pg = pgfind(uap->who)) == NULL)
106 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
107 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
114 uap->who = curp->p_ucred->cr_uid;
115 FOREACH_PROC_IN_SYSTEM(p)
116 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
117 p->p_ucred->cr_uid == uap->who &&
125 if (low == PRIO_MAX + 1)
127 uap->sysmsg_result = low;
133 setpriority(struct setpriority_args *uap)
135 struct proc *curp = curproc;
137 int found = 0, error = 0;
139 switch (uap->which) {
148 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
150 error = donice(p, uap->prio);
160 else if ((pg = pgfind(uap->who)) == NULL)
162 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
163 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
164 error = donice(p, uap->prio);
172 uap->who = curp->p_ucred->cr_uid;
173 FOREACH_PROC_IN_SYSTEM(p)
174 if (p->p_ucred->cr_uid == uap->who &&
175 PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
176 error = donice(p, uap->prio);
190 donice(struct proc *chgp, int n)
192 struct proc *curp = curproc;
193 struct ucred *cr = curp->p_ucred;
195 if (cr->cr_uid && cr->cr_ruid &&
196 cr->cr_uid != chgp->p_ucred->cr_uid &&
197 cr->cr_ruid != chgp->p_ucred->cr_uid)
203 if (n < chgp->p_nice && suser_cred(cr, 0))
206 (void)resetpriority(chgp);
211 * Set realtime priority
215 rtprio(struct rtprio_args *uap)
217 struct proc *curp = curproc;
219 struct ucred *cr = curp->p_ucred;
223 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
235 switch (uap->function) {
237 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
239 if (cr->cr_uid && cr->cr_ruid &&
240 cr->cr_uid != p->p_ucred->cr_uid &&
241 cr->cr_ruid != p->p_ucred->cr_uid)
243 /* disallow setting rtprio in most cases if not superuser */
244 if (suser_cred(cr, 0)) {
245 /* can't set someone else's */
248 /* can't set realtime priority */
250 * Realtime priority has to be restricted for reasons which should be
251 * obvious. However, for idle priority, there is a potential for
252 * system deadlock if an idleprio process gains a lock on a resource
253 * that other processes need (and the idleprio process can't run
254 * due to a CPU-bound normal process). Fix me! XXX
256 if (RTP_PRIO_IS_REALTIME(rtp.type))
263 case RTP_PRIO_REALTIME:
264 case RTP_PRIO_NORMAL:
266 if (rtp.prio > RTP_PRIO_MAX)
280 setrlimit(struct __setrlimit_args *uap)
285 error = copyin(uap->rlp, &alim, sizeof(alim));
289 error = kern_setrlimit(uap->which, &alim);
295 kern_setrlimit(u_int which, struct rlimit *limp)
297 struct proc *p = curproc;
298 struct rlimit *alimp;
301 if (which >= RLIM_NLIMITS)
303 alimp = &p->p_rlimit[which];
306 * Preserve historical bugs by treating negative limits as unsigned.
308 if (limp->rlim_cur < 0)
309 limp->rlim_cur = RLIM_INFINITY;
310 if (limp->rlim_max < 0)
311 limp->rlim_max = RLIM_INFINITY;
313 if (limp->rlim_cur > alimp->rlim_max ||
314 limp->rlim_max > alimp->rlim_max)
315 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
317 if (limp->rlim_cur > limp->rlim_max)
318 limp->rlim_cur = limp->rlim_max;
319 if (p->p_limit->p_refcnt > 1 &&
320 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
321 p->p_limit->p_refcnt--;
322 p->p_limit = limcopy(p->p_limit);
323 alimp = &p->p_rlimit[which];
329 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
330 p->p_limit->p_cpulimit = RLIM_INFINITY;
332 p->p_limit->p_cpulimit =
333 (rlim_t)1000000 * limp->rlim_cur;
336 if (limp->rlim_cur > maxdsiz)
337 limp->rlim_cur = maxdsiz;
338 if (limp->rlim_max > maxdsiz)
339 limp->rlim_max = maxdsiz;
343 if (limp->rlim_cur > maxssiz)
344 limp->rlim_cur = maxssiz;
345 if (limp->rlim_max > maxssiz)
346 limp->rlim_max = maxssiz;
348 * Stack is allocated to the max at exec time with only
349 * "rlim_cur" bytes accessible. If stack limit is going
350 * up make more accessible, if going down make inaccessible.
352 if (limp->rlim_cur != alimp->rlim_cur) {
357 if (limp->rlim_cur > alimp->rlim_cur) {
359 size = limp->rlim_cur - alimp->rlim_cur;
360 addr = USRSTACK - limp->rlim_cur;
363 size = alimp->rlim_cur - limp->rlim_cur;
364 addr = USRSTACK - alimp->rlim_cur;
366 addr = trunc_page(addr);
367 size = round_page(size);
368 (void) vm_map_protect(&p->p_vmspace->vm_map,
369 addr, addr+size, prot, FALSE);
374 if (limp->rlim_cur > maxfilesperproc)
375 limp->rlim_cur = maxfilesperproc;
376 if (limp->rlim_max > maxfilesperproc)
377 limp->rlim_max = maxfilesperproc;
381 if (limp->rlim_cur > maxprocperuid)
382 limp->rlim_cur = maxprocperuid;
383 if (limp->rlim_max > maxprocperuid)
384 limp->rlim_max = maxprocperuid;
385 if (limp->rlim_cur < 1)
387 if (limp->rlim_max < 1)
396 * The rlimit indexed by which is returned in the second argument.
399 kern_getrlimit(u_int which, struct rlimit *limp)
401 struct thread *td = curthread;
402 struct proc *p = td->td_proc;
404 if (which >= RLIM_NLIMITS)
407 *limp = p->p_rlimit[which];
413 getrlimit(struct __getrlimit_args *uap)
418 error = kern_getrlimit(uap->which, &lim);
421 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
426 * Transform the running time and tick information in proc p into user,
427 * system, and interrupt time usage.
429 * Since we are limited to statclock tick granularity this is a statisical
430 * calculation which will be correct over the long haul, but should not be
431 * expected to measure fine grained deltas.
434 calcru(struct proc *p, struct timeval *up, struct timeval *sp,
437 struct thread *td = p->p_thread;
440 * Calculate at the statclock level. YYY if the thread is owned by
441 * another cpu we need to forward the request to the other cpu, or
442 * have a token to interlock the information.
445 up->tv_sec = td->td_uticks / 1000000;
446 up->tv_usec = td->td_uticks % 1000000;
447 sp->tv_sec = td->td_sticks / 1000000;
448 sp->tv_usec = td->td_sticks % 1000000;
450 ip->tv_sec = td->td_iticks / 1000000;
451 ip->tv_usec = td->td_iticks % 1000000;
458 getrusage(struct getrusage_args *uap)
460 struct proc *p = curproc;
466 rup = &p->p_stats->p_ru;
467 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
470 case RUSAGE_CHILDREN:
471 rup = &p->p_stats->p_cru;
477 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
478 sizeof (struct rusage)));
482 ruadd(struct rusage *ru, struct rusage *ru2)
487 timevaladd(&ru->ru_utime, &ru2->ru_utime);
488 timevaladd(&ru->ru_stime, &ru2->ru_stime);
489 if (ru->ru_maxrss < ru2->ru_maxrss)
490 ru->ru_maxrss = ru2->ru_maxrss;
491 ip = &ru->ru_first; ip2 = &ru2->ru_first;
492 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
497 * Make a copy of the plimit structure.
498 * We share these structures copy-on-write after fork,
499 * and copy when a limit is changed.
502 limcopy(struct plimit *lim)
506 MALLOC(copy, struct plimit *, sizeof(struct plimit),
507 M_SUBPROC, M_WAITOK);
508 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
515 * Find the uidinfo structure for a uid. This structure is used to
516 * track the total resource consumption (process count, socket buffer
517 * size, etc.) for the uid and impose limits.
522 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
525 static struct uidinfo *
528 struct uihashhead *uipp;
532 LIST_FOREACH(uip, uipp, ui_hash) {
533 if (uip->ui_uid == uid)
539 static struct uidinfo *
542 struct uidinfo *uip, *norace;
545 * Allocate space and check for a race
547 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
548 norace = uilookup(uid);
549 if (norace != NULL) {
550 FREE(uip, M_UIDINFO);
555 * Initialize structure and enter it into the hash table
557 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
562 varsymset_init(&uip->ui_varsymset, NULL);
579 uifree(struct uidinfo *uip)
581 if (uip->ui_sbsize != 0)
582 /* XXX no %qd in kernel. Truncate. */
583 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
584 uip->ui_uid, (long)uip->ui_sbsize);
585 if (uip->ui_proccnt != 0)
586 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
587 uip->ui_uid, uip->ui_proccnt);
588 LIST_REMOVE(uip, ui_hash);
589 varsymset_clean(&uip->ui_varsymset);
590 FREE(uip, M_UIDINFO);
594 uihold(struct uidinfo *uip)
597 KKASSERT(uip->ui_ref > 0);
601 uidrop(struct uidinfo *uip)
603 KKASSERT(uip->ui_ref > 0);
604 if (--uip->ui_ref == 0)
609 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
616 * Change the count associated with number of processes
617 * a given user is using. When 'max' is 0, don't enforce a limit
620 chgproccnt(struct uidinfo *uip, int diff, int max)
622 /* don't allow them to exceed max, but allow subtraction */
623 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
625 uip->ui_proccnt += diff;
626 if (uip->ui_proccnt < 0)
627 printf("negative proccnt for uid = %d\n", uip->ui_uid);
632 * Change the total socket buffer size a user has used.
635 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
641 new = uip->ui_sbsize + to - *hiwat;
642 /* don't allow them to exceed max, but allow subtraction */
643 if (to > *hiwat && new > max) {
647 uip->ui_sbsize = new;
649 if (uip->ui_sbsize < 0)
650 printf("negative sbsize for uid = %d\n", uip->ui_uid);