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.20 2004/05/03 16:06:26 joerg 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>
55 #include <sys/lockf.h>
58 #include <vm/vm_param.h>
61 #include <vm/vm_map.h>
63 #include <sys/thread2.h>
65 static int donice (struct proc *chgp, int n);
67 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
68 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
69 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
70 static u_long uihash; /* size of hash table - 1 */
72 static struct uidinfo *uicreate (uid_t uid);
73 static struct uidinfo *uilookup (uid_t uid);
76 * Resource controls and accounting.
80 getpriority(struct getpriority_args *uap)
82 struct proc *curp = curproc;
84 int low = PRIO_MAX + 1;
94 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
105 else if ((pg = pgfind(uap->who)) == NULL)
107 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
108 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
115 uap->who = curp->p_ucred->cr_uid;
116 FOREACH_PROC_IN_SYSTEM(p)
117 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
118 p->p_ucred->cr_uid == uap->who &&
126 if (low == PRIO_MAX + 1)
128 uap->sysmsg_result = low;
134 setpriority(struct setpriority_args *uap)
136 struct proc *curp = curproc;
138 int found = 0, error = 0;
140 switch (uap->which) {
149 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
151 error = donice(p, uap->prio);
161 else if ((pg = pgfind(uap->who)) == NULL)
163 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
164 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
165 error = donice(p, uap->prio);
173 uap->who = curp->p_ucred->cr_uid;
174 FOREACH_PROC_IN_SYSTEM(p)
175 if (p->p_ucred->cr_uid == uap->who &&
176 PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
177 error = donice(p, uap->prio);
191 donice(struct proc *chgp, int n)
193 struct proc *curp = curproc;
194 struct ucred *cr = curp->p_ucred;
196 if (cr->cr_uid && cr->cr_ruid &&
197 cr->cr_uid != chgp->p_ucred->cr_uid &&
198 cr->cr_ruid != chgp->p_ucred->cr_uid)
204 if (n < chgp->p_nice && suser_cred(cr, 0))
207 (void)resetpriority(chgp);
212 * Set realtime priority
216 rtprio(struct rtprio_args *uap)
218 struct proc *curp = curproc;
220 struct ucred *cr = curp->p_ucred;
224 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
236 switch (uap->function) {
238 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
240 if (cr->cr_uid && cr->cr_ruid &&
241 cr->cr_uid != p->p_ucred->cr_uid &&
242 cr->cr_ruid != p->p_ucred->cr_uid)
244 /* disallow setting rtprio in most cases if not superuser */
245 if (suser_cred(cr, 0)) {
246 /* can't set someone else's */
249 /* can't set realtime priority */
251 * Realtime priority has to be restricted for reasons which should be
252 * obvious. However, for idle priority, there is a potential for
253 * system deadlock if an idleprio process gains a lock on a resource
254 * that other processes need (and the idleprio process can't run
255 * due to a CPU-bound normal process). Fix me! XXX
257 if (RTP_PRIO_IS_REALTIME(rtp.type))
264 case RTP_PRIO_REALTIME:
265 case RTP_PRIO_NORMAL:
267 if (rtp.prio > RTP_PRIO_MAX)
281 setrlimit(struct __setrlimit_args *uap)
286 error = copyin(uap->rlp, &alim, sizeof(alim));
290 error = kern_setrlimit(uap->which, &alim);
296 kern_setrlimit(u_int which, struct rlimit *limp)
298 struct proc *p = curproc;
299 struct rlimit *alimp;
302 if (which >= RLIM_NLIMITS)
304 alimp = &p->p_rlimit[which];
307 * Preserve historical bugs by treating negative limits as unsigned.
309 if (limp->rlim_cur < 0)
310 limp->rlim_cur = RLIM_INFINITY;
311 if (limp->rlim_max < 0)
312 limp->rlim_max = RLIM_INFINITY;
314 if (limp->rlim_cur > alimp->rlim_max ||
315 limp->rlim_max > alimp->rlim_max)
316 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
318 if (limp->rlim_cur > limp->rlim_max)
319 limp->rlim_cur = limp->rlim_max;
320 if (p->p_limit->p_refcnt > 1 &&
321 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
322 p->p_limit->p_refcnt--;
323 p->p_limit = limcopy(p->p_limit);
324 alimp = &p->p_rlimit[which];
330 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
331 p->p_limit->p_cpulimit = RLIM_INFINITY;
333 p->p_limit->p_cpulimit =
334 (rlim_t)1000000 * limp->rlim_cur;
337 if (limp->rlim_cur > maxdsiz)
338 limp->rlim_cur = maxdsiz;
339 if (limp->rlim_max > maxdsiz)
340 limp->rlim_max = maxdsiz;
344 if (limp->rlim_cur > maxssiz)
345 limp->rlim_cur = maxssiz;
346 if (limp->rlim_max > maxssiz)
347 limp->rlim_max = maxssiz;
349 * Stack is allocated to the max at exec time with only
350 * "rlim_cur" bytes accessible. If stack limit is going
351 * up make more accessible, if going down make inaccessible.
353 if (limp->rlim_cur != alimp->rlim_cur) {
358 if (limp->rlim_cur > alimp->rlim_cur) {
360 size = limp->rlim_cur - alimp->rlim_cur;
361 addr = USRSTACK - limp->rlim_cur;
364 size = alimp->rlim_cur - limp->rlim_cur;
365 addr = USRSTACK - alimp->rlim_cur;
367 addr = trunc_page(addr);
368 size = round_page(size);
369 (void) vm_map_protect(&p->p_vmspace->vm_map,
370 addr, addr+size, prot, FALSE);
375 if (limp->rlim_cur > maxfilesperproc)
376 limp->rlim_cur = maxfilesperproc;
377 if (limp->rlim_max > maxfilesperproc)
378 limp->rlim_max = maxfilesperproc;
382 if (limp->rlim_cur > maxprocperuid)
383 limp->rlim_cur = maxprocperuid;
384 if (limp->rlim_max > maxprocperuid)
385 limp->rlim_max = maxprocperuid;
386 if (limp->rlim_cur < 1)
388 if (limp->rlim_max < 1)
391 case RLIMIT_POSIXLOCKS:
392 if (limp->rlim_cur > maxposixlocksperuid)
393 limp->rlim_cur = maxposixlocksperuid;
394 if (limp->rlim_max > maxposixlocksperuid)
395 limp->rlim_max = maxposixlocksperuid;
403 * The rlimit indexed by which is returned in the second argument.
406 kern_getrlimit(u_int which, struct rlimit *limp)
408 struct thread *td = curthread;
409 struct proc *p = td->td_proc;
411 if (which >= RLIM_NLIMITS)
414 *limp = p->p_rlimit[which];
420 getrlimit(struct __getrlimit_args *uap)
425 error = kern_getrlimit(uap->which, &lim);
428 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
433 * Transform the running time and tick information in proc p into user,
434 * system, and interrupt time usage.
436 * Since we are limited to statclock tick granularity this is a statisical
437 * calculation which will be correct over the long haul, but should not be
438 * expected to measure fine grained deltas.
441 calcru(struct proc *p, struct timeval *up, struct timeval *sp,
444 struct thread *td = p->p_thread;
447 * Calculate at the statclock level. YYY if the thread is owned by
448 * another cpu we need to forward the request to the other cpu, or
449 * have a token to interlock the information.
452 up->tv_sec = td->td_uticks / 1000000;
453 up->tv_usec = td->td_uticks % 1000000;
454 sp->tv_sec = td->td_sticks / 1000000;
455 sp->tv_usec = td->td_sticks % 1000000;
457 ip->tv_sec = td->td_iticks / 1000000;
458 ip->tv_usec = td->td_iticks % 1000000;
465 getrusage(struct getrusage_args *uap)
467 struct proc *p = curproc;
473 rup = &p->p_stats->p_ru;
474 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
477 case RUSAGE_CHILDREN:
478 rup = &p->p_stats->p_cru;
484 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
485 sizeof (struct rusage)));
489 ruadd(struct rusage *ru, struct rusage *ru2)
494 timevaladd(&ru->ru_utime, &ru2->ru_utime);
495 timevaladd(&ru->ru_stime, &ru2->ru_stime);
496 if (ru->ru_maxrss < ru2->ru_maxrss)
497 ru->ru_maxrss = ru2->ru_maxrss;
498 ip = &ru->ru_first; ip2 = &ru2->ru_first;
499 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
504 * Make a copy of the plimit structure.
505 * We share these structures copy-on-write after fork,
506 * and copy when a limit is changed.
509 limcopy(struct plimit *lim)
513 MALLOC(copy, struct plimit *, sizeof(struct plimit),
514 M_SUBPROC, M_WAITOK);
515 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));
522 * Find the uidinfo structure for a uid. This structure is used to
523 * track the total resource consumption (process count, socket buffer
524 * size, etc.) for the uid and impose limits.
529 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
532 static struct uidinfo *
535 struct uihashhead *uipp;
539 LIST_FOREACH(uip, uipp, ui_hash) {
540 if (uip->ui_uid == uid)
546 static struct uidinfo *
549 struct uidinfo *uip, *norace;
552 * Allocate space and check for a race
554 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
555 norace = uilookup(uid);
556 if (norace != NULL) {
557 FREE(uip, M_UIDINFO);
562 * Initialize structure and enter it into the hash table
564 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
569 uip->ui_posixlocks = 0;
570 varsymset_init(&uip->ui_varsymset, NULL);
587 uifree(struct uidinfo *uip)
589 if (uip->ui_sbsize != 0)
590 /* XXX no %qd in kernel. Truncate. */
591 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
592 uip->ui_uid, (long)uip->ui_sbsize);
593 if (uip->ui_proccnt != 0)
594 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
595 uip->ui_uid, uip->ui_proccnt);
596 LIST_REMOVE(uip, ui_hash);
597 varsymset_clean(&uip->ui_varsymset);
598 FREE(uip, M_UIDINFO);
602 uihold(struct uidinfo *uip)
605 KKASSERT(uip->ui_ref > 0);
609 uidrop(struct uidinfo *uip)
611 KKASSERT(uip->ui_ref > 0);
612 if (--uip->ui_ref == 0)
617 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
624 * Change the count associated with number of processes
625 * a given user is using. When 'max' is 0, don't enforce a limit
628 chgproccnt(struct uidinfo *uip, int diff, int max)
630 /* don't allow them to exceed max, but allow subtraction */
631 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
633 uip->ui_proccnt += diff;
634 if (uip->ui_proccnt < 0)
635 printf("negative proccnt for uid = %d\n", uip->ui_uid);
640 * Change the total socket buffer size a user has used.
643 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
649 new = uip->ui_sbsize + to - *hiwat;
650 /* don't allow them to exceed max, but allow subtraction */
651 if (to > *hiwat && new > max) {
655 uip->ui_sbsize = new;
657 if (uip->ui_sbsize < 0)
658 printf("negative sbsize for uid = %d\n", uip->ui_uid);