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
42 #include "opt_compat.h"
43 #include "opt_rlimit.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
49 #include <sys/resourcevar.h>
50 #include <sys/malloc.h>
55 #include <vm/vm_param.h>
58 #include <vm/vm_map.h>
60 static int donice __P((struct proc *curp, struct proc *chgp, int n));
61 /* dosetrlimit non-static: Needed by SysVR4 emulator */
62 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
65 * Resource controls and accounting.
68 #ifndef _SYS_SYSPROTO_H_
69 struct getpriority_args {
75 getpriority(curp, uap)
77 register struct getpriority_args *uap;
79 register struct proc *p;
80 register int low = PRIO_MAX + 1;
95 register struct pgrp *pg;
99 else if ((pg = pgfind(uap->who)) == NULL)
101 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
110 uap->who = curp->p_ucred->cr_uid;
111 LIST_FOREACH(p, &allproc, p_list)
112 if (p->p_ucred->cr_uid == uap->who &&
120 if (low == PRIO_MAX + 1)
122 curp->p_retval[0] = low;
126 #ifndef _SYS_SYSPROTO_H_
127 struct setpriority_args {
135 setpriority(curp, uap)
137 register struct setpriority_args *uap;
139 register struct proc *p;
140 int found = 0, error = 0;
142 switch (uap->which) {
151 error = donice(curp, p, uap->prio);
156 register struct pgrp *pg;
160 else if ((pg = pgfind(uap->who)) == NULL)
162 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
163 error = donice(curp, p, uap->prio);
171 uap->who = curp->p_ucred->cr_uid;
172 LIST_FOREACH(p, &allproc, p_list)
173 if (p->p_ucred->cr_uid == uap->who) {
174 error = donice(curp, p, uap->prio);
188 donice(curp, chgp, n)
189 register struct proc *curp, *chgp;
192 register struct pcred *pcred = curp->p_cred;
194 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
195 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
196 pcred->p_ruid != chgp->p_ucred->cr_uid)
202 if (n < chgp->p_nice && suser(curp))
205 (void)resetpriority(chgp);
209 /* rtprio system call */
210 #ifndef _SYS_SYSPROTO_H_
219 * Set realtime priority
226 register struct rtprio_args *uap;
228 register struct proc *p;
229 register struct pcred *pcred = curp->p_cred;
233 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
245 switch (uap->function) {
247 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
249 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
250 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid &&
251 pcred->p_ruid != p->p_ucred->cr_uid)
253 /* disallow setting rtprio in most cases if not superuser */
255 /* can't set someone else's */
258 /* can't set realtime priority */
260 * Realtime priority has to be restricted for reasons which should be
261 * obvious. However, for idle priority, there is a potential for
262 * system deadlock if an idleprio process gains a lock on a resource
263 * that other processes need (and the idleprio process can't run
264 * due to a CPU-bound normal process). Fix me! XXX
267 if (RTP_PRIO_IS_REALTIME(rtp.type))
269 if (rtp.type != RTP_PRIO_NORMAL)
276 case RTP_PRIO_REALTIME:
277 case RTP_PRIO_NORMAL:
279 if (rtp.prio > RTP_PRIO_MAX)
292 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
293 #ifndef _SYS_SYSPROTO_H_
294 struct osetrlimit_args {
303 register struct osetrlimit_args *uap;
310 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
312 lim.rlim_cur = olim.rlim_cur;
313 lim.rlim_max = olim.rlim_max;
314 return (dosetrlimit(p, uap->which, &lim));
317 #ifndef _SYS_SYSPROTO_H_
318 struct ogetrlimit_args {
327 register struct ogetrlimit_args *uap;
331 if (uap->which >= RLIM_NLIMITS)
333 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
334 if (olim.rlim_cur == -1)
335 olim.rlim_cur = 0x7fffffff;
336 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
337 if (olim.rlim_max == -1)
338 olim.rlim_max = 0x7fffffff;
339 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
341 #endif /* COMPAT_43 || COMPAT_SUNOS */
343 #ifndef _SYS_SYSPROTO_H_
344 struct __setrlimit_args {
353 register struct __setrlimit_args *uap;
359 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
361 return (dosetrlimit(p, uap->which, &alim));
365 dosetrlimit(p, which, limp)
370 register struct rlimit *alimp;
373 if (which >= RLIM_NLIMITS)
375 alimp = &p->p_rlimit[which];
378 * Preserve historical bugs by treating negative limits as unsigned.
380 if (limp->rlim_cur < 0)
381 limp->rlim_cur = RLIM_INFINITY;
382 if (limp->rlim_max < 0)
383 limp->rlim_max = RLIM_INFINITY;
385 if (limp->rlim_cur > alimp->rlim_max ||
386 limp->rlim_max > alimp->rlim_max)
387 if ((error = suser_xxx(0, p, PRISON_ROOT)))
389 if (limp->rlim_cur > limp->rlim_max)
390 limp->rlim_cur = limp->rlim_max;
391 if (p->p_limit->p_refcnt > 1 &&
392 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
393 p->p_limit->p_refcnt--;
394 p->p_limit = limcopy(p->p_limit);
395 alimp = &p->p_rlimit[which];
401 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
402 p->p_limit->p_cpulimit = RLIM_INFINITY;
404 p->p_limit->p_cpulimit =
405 (rlim_t)1000000 * limp->rlim_cur;
408 if (limp->rlim_cur > MAXDSIZ)
409 limp->rlim_cur = MAXDSIZ;
410 if (limp->rlim_max > MAXDSIZ)
411 limp->rlim_max = MAXDSIZ;
415 if (limp->rlim_cur > MAXSSIZ)
416 limp->rlim_cur = MAXSSIZ;
417 if (limp->rlim_max > MAXSSIZ)
418 limp->rlim_max = MAXSSIZ;
420 * Stack is allocated to the max at exec time with only
421 * "rlim_cur" bytes accessible. If stack limit is going
422 * up make more accessible, if going down make inaccessible.
424 if (limp->rlim_cur != alimp->rlim_cur) {
429 if (limp->rlim_cur > alimp->rlim_cur) {
431 size = limp->rlim_cur - alimp->rlim_cur;
432 addr = USRSTACK - limp->rlim_cur;
435 size = alimp->rlim_cur - limp->rlim_cur;
436 addr = USRSTACK - alimp->rlim_cur;
438 addr = trunc_page(addr);
439 size = round_page(size);
440 (void) vm_map_protect(&p->p_vmspace->vm_map,
441 addr, addr+size, prot, FALSE);
446 if (limp->rlim_cur > maxfilesperproc)
447 limp->rlim_cur = maxfilesperproc;
448 if (limp->rlim_max > maxfilesperproc)
449 limp->rlim_max = maxfilesperproc;
453 if (limp->rlim_cur > maxprocperuid)
454 limp->rlim_cur = maxprocperuid;
455 if (limp->rlim_max > maxprocperuid)
456 limp->rlim_max = maxprocperuid;
463 #ifndef _SYS_SYSPROTO_H_
464 struct __getrlimit_args {
473 register struct __getrlimit_args *uap;
476 if (uap->which >= RLIM_NLIMITS)
478 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
479 sizeof (struct rlimit)));
483 * Transform the running time and tick information in proc p into user,
484 * system, and interrupt time usage.
487 calcru(p, up, sp, ip)
493 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */
494 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu;
498 /* XXX: why spl-protect ? worst case is an off-by-one report */
513 if (p->p_oncpu != 0xff) {
518 * Adjust for the current time slice. This is actually fairly
519 * important since the error here is on the order of a time
520 * quantum, which is much greater than the sampling error.
523 if (timevalcmp(&tv, &switchtime, <))
524 printf("microuptime() went backwards (%ld.%06ld -> %ld,%06ld)\n",
525 switchtime.tv_sec, switchtime.tv_usec,
526 tv.tv_sec, tv.tv_usec);
528 tu += (tv.tv_usec - switchtime.tv_usec) +
529 (tv.tv_sec - switchtime.tv_sec) * (int64_t)1000000;
531 ptu = p->p_stats->p_uu + p->p_stats->p_su + p->p_stats->p_iu;
532 if (tu < ptu || (int64_t)tu < 0) {
533 /* XXX no %qd in kernel. Truncate. */
534 printf("calcru: negative time of %ld usec for pid %d (%s)\n",
535 (long)tu, p->p_pid, p->p_comm);
544 /* Enforce monotonicity. */
545 if (uu < p->p_stats->p_uu || su < p->p_stats->p_su ||
546 iu < p->p_stats->p_iu) {
547 if (uu < p->p_stats->p_uu)
548 uu = p->p_stats->p_uu;
549 else if (uu + p->p_stats->p_su + p->p_stats->p_iu > tu)
550 uu = tu - p->p_stats->p_su - p->p_stats->p_iu;
552 su = p->p_stats->p_su;
554 su = ((tu - uu) * st) / (st + it);
555 if (su < p->p_stats->p_su)
556 su = p->p_stats->p_su;
557 else if (uu + su + p->p_stats->p_iu > tu)
558 su = tu - uu - p->p_stats->p_iu;
560 KASSERT(uu + su + p->p_stats->p_iu <= tu,
561 ("calcru: monotonisation botch 1"));
563 KASSERT(iu >= p->p_stats->p_iu,
564 ("calcru: monotonisation botch 2"));
566 p->p_stats->p_uu = uu;
567 p->p_stats->p_su = su;
568 p->p_stats->p_iu = iu;
570 up->tv_sec = uu / 1000000;
571 up->tv_usec = uu % 1000000;
572 sp->tv_sec = su / 1000000;
573 sp->tv_usec = su % 1000000;
575 ip->tv_sec = iu / 1000000;
576 ip->tv_usec = iu % 1000000;
580 #ifndef _SYS_SYSPROTO_H_
581 struct getrusage_args {
583 struct rusage *rusage;
589 register struct proc *p;
590 register struct getrusage_args *uap;
592 register struct rusage *rup;
597 rup = &p->p_stats->p_ru;
598 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
601 case RUSAGE_CHILDREN:
602 rup = &p->p_stats->p_cru;
608 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
609 sizeof (struct rusage)));
614 register struct rusage *ru, *ru2;
616 register long *ip, *ip2;
619 timevaladd(&ru->ru_utime, &ru2->ru_utime);
620 timevaladd(&ru->ru_stime, &ru2->ru_stime);
621 if (ru->ru_maxrss < ru2->ru_maxrss)
622 ru->ru_maxrss = ru2->ru_maxrss;
623 ip = &ru->ru_first; ip2 = &ru2->ru_first;
624 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
629 * Make a copy of the plimit structure.
630 * We share these structures copy-on-write after fork,
631 * and copy when a limit is changed.
637 register struct plimit *copy;
639 MALLOC(copy, struct plimit *, sizeof(struct plimit),
640 M_SUBPROC, M_WAITOK);
641 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));