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.29 2006/12/23 00:35:04 swildner 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.
79 struct getpriority_info {
84 static int getpriority_callback(struct proc *p, void *data);
87 sys_getpriority(struct getpriority_args *uap)
89 struct getpriority_info info;
90 struct proc *curp = curproc;
92 int low = PRIO_MAX + 1;
102 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
113 else if ((pg = pgfind(uap->who)) == NULL)
115 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
116 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
123 uap->who = curp->p_ucred->cr_uid;
126 allproc_scan(getpriority_callback, &info);
133 if (low == PRIO_MAX + 1)
135 uap->sysmsg_result = low;
140 * Figure out the current lowest nice priority for processes owned
141 * by the specified user.
145 getpriority_callback(struct proc *p, void *data)
147 struct getpriority_info *info = data;
149 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
150 p->p_ucred->cr_uid == info->who &&
151 p->p_nice < info->low) {
152 info->low = p->p_nice;
157 struct setpriority_info {
164 static int setpriority_callback(struct proc *p, void *data);
167 sys_setpriority(struct setpriority_args *uap)
169 struct setpriority_info info;
170 struct proc *curp = curproc;
172 int found = 0, error = 0;
174 switch (uap->which) {
182 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
184 error = donice(p, uap->prio);
194 else if ((pg = pgfind(uap->who)) == NULL)
196 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
197 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
198 error = donice(p, uap->prio);
206 uap->who = curp->p_ucred->cr_uid;
207 info.prio = uap->prio;
211 allproc_scan(setpriority_callback, &info);
226 setpriority_callback(struct proc *p, void *data)
228 struct setpriority_info *info = data;
231 if (p->p_ucred->cr_uid == info->who &&
232 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
233 error = donice(p, info->prio);
242 donice(struct proc *chgp, int n)
244 struct proc *curp = curproc;
245 struct ucred *cr = curp->p_ucred;
247 if (cr->cr_uid && cr->cr_ruid &&
248 cr->cr_uid != chgp->p_ucred->cr_uid &&
249 cr->cr_ruid != chgp->p_ucred->cr_uid)
255 if (n < chgp->p_nice && suser_cred(cr, 0))
258 chgp->p_usched->resetpriority(&chgp->p_lwp);
263 * Set realtime priority
267 sys_rtprio(struct rtprio_args *uap)
269 struct proc *curp = curproc;
271 struct ucred *cr = curp->p_ucred;
275 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
287 switch (uap->function) {
289 return (copyout(&p->p_lwp.lwp_rtprio, uap->rtp, sizeof(struct rtprio)));
291 if (cr->cr_uid && cr->cr_ruid &&
292 cr->cr_uid != p->p_ucred->cr_uid &&
293 cr->cr_ruid != p->p_ucred->cr_uid)
295 /* disallow setting rtprio in most cases if not superuser */
296 if (suser_cred(cr, 0)) {
297 /* can't set someone else's */
300 /* can't set realtime priority */
302 * Realtime priority has to be restricted for reasons which should be
303 * obvious. However, for idle priority, there is a potential for
304 * system deadlock if an idleprio process gains a lock on a resource
305 * that other processes need (and the idleprio process can't run
306 * due to a CPU-bound normal process). Fix me! XXX
308 if (RTP_PRIO_IS_REALTIME(rtp.type))
315 case RTP_PRIO_REALTIME:
316 case RTP_PRIO_NORMAL:
318 if (rtp.prio > RTP_PRIO_MAX)
320 p->p_lwp.lwp_rtprio = rtp;
332 sys_setrlimit(struct __setrlimit_args *uap)
337 error = copyin(uap->rlp, &alim, sizeof(alim));
341 error = kern_setrlimit(uap->which, &alim);
347 sys_getrlimit(struct __getrlimit_args *uap)
352 error = kern_getrlimit(uap->which, &lim);
355 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
360 * Transform the running time and tick information in proc p into user,
361 * system, and interrupt time usage.
363 * Since we are limited to statclock tick granularity this is a statisical
364 * calculation which will be correct over the long haul, but should not be
365 * expected to measure fine grained deltas.
368 calcru(struct proc *p, struct timeval *up, struct timeval *sp,
371 struct thread *td = p->p_thread;
374 * Calculate at the statclock level. YYY if the thread is owned by
375 * another cpu we need to forward the request to the other cpu, or
376 * have a token to interlock the information.
379 up->tv_sec = td->td_uticks / 1000000;
380 up->tv_usec = td->td_uticks % 1000000;
381 sp->tv_sec = td->td_sticks / 1000000;
382 sp->tv_usec = td->td_sticks % 1000000;
384 ip->tv_sec = td->td_iticks / 1000000;
385 ip->tv_usec = td->td_iticks % 1000000;
392 sys_getrusage(struct getrusage_args *uap)
394 struct proc *p = curproc;
400 rup = &p->p_stats->p_ru;
401 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
404 case RUSAGE_CHILDREN:
405 rup = &p->p_stats->p_cru;
411 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
412 sizeof (struct rusage)));
416 ruadd(struct rusage *ru, struct rusage *ru2)
421 timevaladd(&ru->ru_utime, &ru2->ru_utime);
422 timevaladd(&ru->ru_stime, &ru2->ru_stime);
423 if (ru->ru_maxrss < ru2->ru_maxrss)
424 ru->ru_maxrss = ru2->ru_maxrss;
425 ip = &ru->ru_first; ip2 = &ru2->ru_first;
426 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
431 * Find the uidinfo structure for a uid. This structure is used to
432 * track the total resource consumption (process count, socket buffer
433 * size, etc.) for the uid and impose limits.
438 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
441 static struct uidinfo *
444 struct uihashhead *uipp;
448 LIST_FOREACH(uip, uipp, ui_hash) {
449 if (uip->ui_uid == uid)
455 static struct uidinfo *
458 struct uidinfo *uip, *norace;
461 * Allocate space and check for a race
463 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
464 norace = uilookup(uid);
465 if (norace != NULL) {
466 FREE(uip, M_UIDINFO);
471 * Initialize structure and enter it into the hash table
473 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
478 uip->ui_posixlocks = 0;
479 varsymset_init(&uip->ui_varsymset, NULL);
496 uifree(struct uidinfo *uip)
498 if (uip->ui_sbsize != 0)
499 /* XXX no %qd in kernel. Truncate. */
500 kprintf("freeing uidinfo: uid = %d, sbsize = %ld\n",
501 uip->ui_uid, (long)uip->ui_sbsize);
502 if (uip->ui_proccnt != 0)
503 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
504 uip->ui_uid, uip->ui_proccnt);
505 LIST_REMOVE(uip, ui_hash);
506 varsymset_clean(&uip->ui_varsymset);
507 FREE(uip, M_UIDINFO);
511 uihold(struct uidinfo *uip)
514 KKASSERT(uip->ui_ref > 0);
518 uidrop(struct uidinfo *uip)
520 KKASSERT(uip->ui_ref > 0);
521 if (--uip->ui_ref == 0)
526 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
533 * Change the count associated with number of processes
534 * a given user is using. When 'max' is 0, don't enforce a limit
537 chgproccnt(struct uidinfo *uip, int diff, int max)
539 /* don't allow them to exceed max, but allow subtraction */
540 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0)
542 uip->ui_proccnt += diff;
543 if (uip->ui_proccnt < 0)
544 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
549 * Change the total socket buffer size a user has used.
552 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
557 new = uip->ui_sbsize + to - *hiwat;
558 /* don't allow them to exceed max, but allow subtraction */
559 if (to > *hiwat && new > max) {
563 uip->ui_sbsize = new;
565 if (uip->ui_sbsize < 0)
566 kprintf("negative sbsize for uid = %d\n", uip->ui_uid);