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>
54 #include <vm/vm_param.h>
57 #include <vm/vm_map.h>
59 static int donice __P((struct proc *curp, struct proc *chgp, int n));
60 /* dosetrlimit non-static: Needed by SysVR4 emulator */
61 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
64 * Resource controls and accounting.
67 #ifndef _SYS_SYSPROTO_H_
68 struct getpriority_args {
74 getpriority(curp, uap)
76 register struct getpriority_args *uap;
78 register struct proc *p;
79 register int low = PRIO_MAX + 1;
94 register struct pgrp *pg;
98 else if ((pg = pgfind(uap->who)) == NULL)
100 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
109 uap->who = curp->p_ucred->cr_uid;
110 LIST_FOREACH(p, &allproc, p_list)
111 if (p->p_ucred->cr_uid == uap->who &&
119 if (low == PRIO_MAX + 1)
121 curp->p_retval[0] = low;
125 #ifndef _SYS_SYSPROTO_H_
126 struct setpriority_args {
134 setpriority(curp, uap)
136 register struct setpriority_args *uap;
138 register struct proc *p;
139 int found = 0, error = 0;
141 switch (uap->which) {
150 error = donice(curp, p, uap->prio);
155 register struct pgrp *pg;
159 else if ((pg = pgfind(uap->who)) == NULL)
161 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
162 error = donice(curp, p, uap->prio);
170 uap->who = curp->p_ucred->cr_uid;
171 LIST_FOREACH(p, &allproc, p_list)
172 if (p->p_ucred->cr_uid == uap->who) {
173 error = donice(curp, p, uap->prio);
187 donice(curp, chgp, n)
188 register struct proc *curp, *chgp;
191 register struct pcred *pcred = curp->p_cred;
193 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
194 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
195 pcred->p_ruid != chgp->p_ucred->cr_uid)
201 if (n < chgp->p_nice && suser(curp))
204 (void)resetpriority(chgp);
208 /* rtprio system call */
209 #ifndef _SYS_SYSPROTO_H_
218 * Set realtime priority
225 register struct rtprio_args *uap;
227 register struct proc *p;
228 register struct pcred *pcred = curp->p_cred;
232 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
244 switch (uap->function) {
246 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
248 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
249 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid &&
250 pcred->p_ruid != p->p_ucred->cr_uid)
252 /* disallow setting rtprio in most cases if not superuser */
254 /* can't set someone else's */
257 /* can't set realtime priority */
259 * Realtime priority has to be restricted for reasons which should be
260 * obvious. However, for idle priority, there is a potential for
261 * system deadlock if an idleprio process gains a lock on a resource
262 * that other processes need (and the idleprio process can't run
263 * due to a CPU-bound normal process). Fix me! XXX
266 if (RTP_PRIO_IS_REALTIME(rtp.type))
268 if (rtp.type != RTP_PRIO_NORMAL)
275 case RTP_PRIO_REALTIME:
276 case RTP_PRIO_NORMAL:
278 if (rtp.prio > RTP_PRIO_MAX)
291 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
292 #ifndef _SYS_SYSPROTO_H_
293 struct osetrlimit_args {
302 register struct osetrlimit_args *uap;
309 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
311 lim.rlim_cur = olim.rlim_cur;
312 lim.rlim_max = olim.rlim_max;
313 return (dosetrlimit(p, uap->which, &lim));
316 #ifndef _SYS_SYSPROTO_H_
317 struct ogetrlimit_args {
326 register struct ogetrlimit_args *uap;
330 if (uap->which >= RLIM_NLIMITS)
332 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
333 if (olim.rlim_cur == -1)
334 olim.rlim_cur = 0x7fffffff;
335 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
336 if (olim.rlim_max == -1)
337 olim.rlim_max = 0x7fffffff;
338 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
340 #endif /* COMPAT_43 || COMPAT_SUNOS */
342 #ifndef _SYS_SYSPROTO_H_
343 struct __setrlimit_args {
352 register struct __setrlimit_args *uap;
358 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
360 return (dosetrlimit(p, uap->which, &alim));
364 dosetrlimit(p, which, limp)
369 register struct rlimit *alimp;
372 if (which >= RLIM_NLIMITS)
374 alimp = &p->p_rlimit[which];
377 * Preserve historical bugs by treating negative limits as unsigned.
379 if (limp->rlim_cur < 0)
380 limp->rlim_cur = RLIM_INFINITY;
381 if (limp->rlim_max < 0)
382 limp->rlim_max = RLIM_INFINITY;
384 if (limp->rlim_cur > alimp->rlim_max ||
385 limp->rlim_max > alimp->rlim_max)
386 if ((error = suser_xxx(0, p, PRISON_ROOT)))
388 if (limp->rlim_cur > limp->rlim_max)
389 limp->rlim_cur = limp->rlim_max;
390 if (p->p_limit->p_refcnt > 1 &&
391 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
392 p->p_limit->p_refcnt--;
393 p->p_limit = limcopy(p->p_limit);
394 alimp = &p->p_rlimit[which];
400 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
401 p->p_limit->p_cpulimit = RLIM_INFINITY;
403 p->p_limit->p_cpulimit =
404 (rlim_t)1000000 * limp->rlim_cur;
407 if (limp->rlim_cur > MAXDSIZ)
408 limp->rlim_cur = MAXDSIZ;
409 if (limp->rlim_max > MAXDSIZ)
410 limp->rlim_max = MAXDSIZ;
414 if (limp->rlim_cur > MAXSSIZ)
415 limp->rlim_cur = MAXSSIZ;
416 if (limp->rlim_max > MAXSSIZ)
417 limp->rlim_max = MAXSSIZ;
419 * Stack is allocated to the max at exec time with only
420 * "rlim_cur" bytes accessible. If stack limit is going
421 * up make more accessible, if going down make inaccessible.
423 if (limp->rlim_cur != alimp->rlim_cur) {
428 if (limp->rlim_cur > alimp->rlim_cur) {
430 size = limp->rlim_cur - alimp->rlim_cur;
431 addr = USRSTACK - limp->rlim_cur;
434 size = alimp->rlim_cur - limp->rlim_cur;
435 addr = USRSTACK - alimp->rlim_cur;
437 addr = trunc_page(addr);
438 size = round_page(size);
439 (void) vm_map_protect(&p->p_vmspace->vm_map,
440 addr, addr+size, prot, FALSE);
445 if (limp->rlim_cur > maxfilesperproc)
446 limp->rlim_cur = maxfilesperproc;
447 if (limp->rlim_max > maxfilesperproc)
448 limp->rlim_max = maxfilesperproc;
452 if (limp->rlim_cur > maxprocperuid)
453 limp->rlim_cur = maxprocperuid;
454 if (limp->rlim_max > maxprocperuid)
455 limp->rlim_max = maxprocperuid;
462 #ifndef _SYS_SYSPROTO_H_
463 struct __getrlimit_args {
472 register struct __getrlimit_args *uap;
475 if (uap->which >= RLIM_NLIMITS)
477 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
478 sizeof (struct rlimit)));
482 * Transform the running time and tick information in proc p into user,
483 * system, and interrupt time usage.
486 calcru(p, up, sp, ip)
492 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */
493 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu;
497 /* XXX: why spl-protect ? worst case is an off-by-one report */
512 if (p->p_oncpu != 0xff) {
517 * Adjust for the current time slice. This is actually fairly
518 * important since the error here is on the order of a time
519 * quantum, which is much greater than the sampling error.
522 tu += (tv.tv_usec - switchtime.tv_usec) +
523 (tv.tv_sec - switchtime.tv_sec) * (int64_t)1000000;
525 ptu = p->p_stats->p_uu + p->p_stats->p_su + p->p_stats->p_iu;
526 if (tu < ptu || (int64_t)tu < 0) {
527 /* XXX no %qd in kernel. Truncate. */
528 printf("calcru: negative time of %ld usec for pid %d (%s)\n",
529 (long)tu, p->p_pid, p->p_comm);
538 /* Enforce monotonicity. */
539 if (uu < p->p_stats->p_uu || su < p->p_stats->p_su ||
540 iu < p->p_stats->p_iu) {
541 if (uu < p->p_stats->p_uu)
542 uu = p->p_stats->p_uu;
543 else if (uu + p->p_stats->p_su + p->p_stats->p_iu > tu)
544 uu = tu - p->p_stats->p_su - p->p_stats->p_iu;
546 su = p->p_stats->p_su;
548 su = ((tu - uu) * st) / (st + it);
549 if (su < p->p_stats->p_su)
550 su = p->p_stats->p_su;
551 else if (uu + su + p->p_stats->p_iu > tu)
552 su = tu - uu - p->p_stats->p_iu;
554 KASSERT(uu + su + p->p_stats->p_iu <= tu,
555 ("calcru: monotonisation botch 1"));
557 KASSERT(iu >= p->p_stats->p_iu,
558 ("calcru: monotonisation botch 2"));
560 p->p_stats->p_uu = uu;
561 p->p_stats->p_su = su;
562 p->p_stats->p_iu = iu;
564 up->tv_sec = uu / 1000000;
565 up->tv_usec = uu % 1000000;
566 sp->tv_sec = su / 1000000;
567 sp->tv_usec = su % 1000000;
569 ip->tv_sec = iu / 1000000;
570 ip->tv_usec = iu % 1000000;
574 #ifndef _SYS_SYSPROTO_H_
575 struct getrusage_args {
577 struct rusage *rusage;
583 register struct proc *p;
584 register struct getrusage_args *uap;
586 register struct rusage *rup;
591 rup = &p->p_stats->p_ru;
592 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
595 case RUSAGE_CHILDREN:
596 rup = &p->p_stats->p_cru;
602 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
603 sizeof (struct rusage)));
608 register struct rusage *ru, *ru2;
610 register long *ip, *ip2;
613 timevaladd(&ru->ru_utime, &ru2->ru_utime);
614 timevaladd(&ru->ru_stime, &ru2->ru_stime);
615 if (ru->ru_maxrss < ru2->ru_maxrss)
616 ru->ru_maxrss = ru2->ru_maxrss;
617 ip = &ru->ru_first; ip2 = &ru2->ru_first;
618 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
623 * Make a copy of the plimit structure.
624 * We share these structures copy-on-write after fork,
625 * and copy when a limit is changed.
631 register struct plimit *copy;
633 MALLOC(copy, struct plimit *, sizeof(struct plimit),
634 M_SUBPROC, M_WAITOK);
635 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));