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 * $Id: kern_resource.c,v 1.41 1999/02/25 11:03:08 bde Exp $
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>
48 #include <sys/kernel.h>
50 #include <sys/resourcevar.h>
51 #include <sys/malloc.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_prot.h>
59 #include <vm/vm_map.h>
61 static int donice __P((struct proc *curp, struct proc *chgp, int n));
62 /* dosetrlimit non-static: Needed by SysVR4 emulator */
63 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp));
66 * Resource controls and accounting.
69 #ifndef _SYS_SYSPROTO_H_
70 struct getpriority_args {
76 getpriority(curp, uap)
78 register struct getpriority_args *uap;
80 register struct proc *p;
81 register int low = PRIO_MAX + 1;
96 register struct pgrp *pg;
100 else if ((pg = pgfind(uap->who)) == NULL)
102 for (p = pg->pg_members.lh_first; p != 0;
103 p = p->p_pglist.le_next) {
112 uap->who = curp->p_ucred->cr_uid;
113 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
114 if (p->p_ucred->cr_uid == uap->who &&
122 if (low == PRIO_MAX + 1)
124 curp->p_retval[0] = low;
128 #ifndef _SYS_SYSPROTO_H_
129 struct setpriority_args {
137 setpriority(curp, uap)
139 register struct setpriority_args *uap;
141 register struct proc *p;
142 int found = 0, error = 0;
144 switch (uap->which) {
153 error = donice(curp, p, uap->prio);
158 register struct pgrp *pg;
162 else if ((pg = pgfind(uap->who)) == NULL)
164 for (p = pg->pg_members.lh_first; p != 0;
165 p = p->p_pglist.le_next) {
166 error = donice(curp, p, uap->prio);
174 uap->who = curp->p_ucred->cr_uid;
175 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
176 if (p->p_ucred->cr_uid == uap->who) {
177 error = donice(curp, p, uap->prio);
191 donice(curp, chgp, n)
192 register struct proc *curp, *chgp;
195 register struct pcred *pcred = curp->p_cred;
197 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
198 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
199 pcred->p_ruid != chgp->p_ucred->cr_uid)
205 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
208 (void)resetpriority(chgp);
212 /* rtprio system call */
213 #ifndef _SYS_SYSPROTO_H_
222 * Set realtime priority
229 register struct rtprio_args *uap;
231 register struct proc *p;
232 register struct pcred *pcred = curp->p_cred;
236 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
248 switch (uap->function) {
250 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
252 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
253 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid &&
254 pcred->p_ruid != p->p_ucred->cr_uid)
256 /* disallow setting rtprio in most cases if not superuser */
257 if (suser(pcred->pc_ucred, &curp->p_acflag)) {
258 /* can't set someone else's */
261 /* can't set realtime priority */
263 * Realtime priority has to be restricted for reasons which should be
264 * obvious. However, for idle priority, there is a potential for
265 * system deadlock if an idleprio process gains a lock on a resource
266 * that other processes need (and the idleprio process can't run
267 * due to a CPU-bound normal process). Fix me! XXX
270 if (RTP_PRIO_IS_REALTIME(rtp.type))
272 if (rtp.type != RTP_PRIO_NORMAL)
279 case RTP_PRIO_REALTIME:
280 case RTP_PRIO_NORMAL:
282 if (rtp.prio > RTP_PRIO_MAX)
295 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
296 #ifndef _SYS_SYSPROTO_H_
297 struct osetrlimit_args {
306 register struct osetrlimit_args *uap;
313 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
315 lim.rlim_cur = olim.rlim_cur;
316 lim.rlim_max = olim.rlim_max;
317 return (dosetrlimit(p, uap->which, &lim));
320 #ifndef _SYS_SYSPROTO_H_
321 struct ogetrlimit_args {
330 register struct ogetrlimit_args *uap;
334 if (uap->which >= RLIM_NLIMITS)
336 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
337 if (olim.rlim_cur == -1)
338 olim.rlim_cur = 0x7fffffff;
339 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
340 if (olim.rlim_max == -1)
341 olim.rlim_max = 0x7fffffff;
342 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
344 #endif /* COMPAT_43 || COMPAT_SUNOS */
346 #ifndef _SYS_SYSPROTO_H_
347 struct __setrlimit_args {
356 register struct __setrlimit_args *uap;
362 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
364 return (dosetrlimit(p, uap->which, &alim));
368 dosetrlimit(p, which, limp)
373 register struct rlimit *alimp;
376 if (which >= RLIM_NLIMITS)
378 alimp = &p->p_rlimit[which];
381 * Preserve historical bugs by treating negative limits as unsigned.
383 if (limp->rlim_cur < 0)
384 limp->rlim_cur = RLIM_INFINITY;
385 if (limp->rlim_max < 0)
386 limp->rlim_max = RLIM_INFINITY;
388 if (limp->rlim_cur > alimp->rlim_max ||
389 limp->rlim_max > alimp->rlim_max)
390 if ((error = suser(p->p_ucred, &p->p_acflag)))
392 if (limp->rlim_cur > limp->rlim_max)
393 limp->rlim_cur = limp->rlim_max;
394 if (p->p_limit->p_refcnt > 1 &&
395 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
396 p->p_limit->p_refcnt--;
397 p->p_limit = limcopy(p->p_limit);
398 alimp = &p->p_rlimit[which];
404 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
405 p->p_limit->p_cpulimit = RLIM_INFINITY;
407 p->p_limit->p_cpulimit =
408 (rlim_t)1000000 * limp->rlim_cur;
411 if (limp->rlim_cur > MAXDSIZ)
412 limp->rlim_cur = MAXDSIZ;
413 if (limp->rlim_max > MAXDSIZ)
414 limp->rlim_max = MAXDSIZ;
418 if (limp->rlim_cur > MAXSSIZ)
419 limp->rlim_cur = MAXSSIZ;
420 if (limp->rlim_max > MAXSSIZ)
421 limp->rlim_max = MAXSSIZ;
423 * Stack is allocated to the max at exec time with only
424 * "rlim_cur" bytes accessible. If stack limit is going
425 * up make more accessible, if going down make inaccessible.
427 if (limp->rlim_cur != alimp->rlim_cur) {
432 if (limp->rlim_cur > alimp->rlim_cur) {
434 size = limp->rlim_cur - alimp->rlim_cur;
435 addr = USRSTACK - limp->rlim_cur;
438 size = alimp->rlim_cur - limp->rlim_cur;
439 addr = USRSTACK - alimp->rlim_cur;
441 addr = trunc_page(addr);
442 size = round_page(size);
443 (void) vm_map_protect(&p->p_vmspace->vm_map,
444 addr, addr+size, prot, FALSE);
449 if (limp->rlim_cur > maxfilesperproc)
450 limp->rlim_cur = maxfilesperproc;
451 if (limp->rlim_max > maxfilesperproc)
452 limp->rlim_max = maxfilesperproc;
456 if (limp->rlim_cur > maxprocperuid)
457 limp->rlim_cur = maxprocperuid;
458 if (limp->rlim_max > maxprocperuid)
459 limp->rlim_max = maxprocperuid;
466 #ifndef _SYS_SYSPROTO_H_
467 struct __getrlimit_args {
476 register struct __getrlimit_args *uap;
479 if (uap->which >= RLIM_NLIMITS)
481 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
482 sizeof (struct rlimit)));
486 * Transform the running time and tick information in proc p into user,
487 * system, and interrupt time usage.
490 calcru(p, up, sp, ip)
497 u_int64_t u, st, ut, it, tot;
501 /* XXX: why spl-protect ? worst case is an off-by-one report */
514 totusec = p->p_runtime;
516 if (p->p_oncpu != (char)0xff) {
521 * Adjust for the current time slice. This is actually fairly
522 * important since the error here is on the order of a time
523 * quantum, which is much greater than the sampling error.
526 totusec += (tv.tv_usec - switchtime.tv_usec) +
527 (tv.tv_sec - switchtime.tv_sec) * (int64_t)1000000;
530 * Copy the time that was just read to `switchtime' in case
531 * we are being called from exit1(). Exits don't go through
532 * mi_switch(), so `switchtime' doesn't get set in the normal
533 * way. We set it here instead of more cleanly in exit1()
534 * to avoid losing track of the time between the calls to
535 * microuptime(). Similarly for `switchticks'.
541 /* XXX no %qd in kernel. Truncate. */
542 printf("calcru: negative time of %ld usec for pid %d (%s)\n",
543 (long)totusec, p->p_pid, p->p_comm);
548 sp->tv_sec = st / 1000000;
549 sp->tv_usec = st % 1000000;
551 up->tv_sec = ut / 1000000;
552 up->tv_usec = ut % 1000000;
555 ip->tv_sec = it / 1000000;
556 ip->tv_usec = it % 1000000;
560 #ifndef _SYS_SYSPROTO_H_
561 struct getrusage_args {
563 struct rusage *rusage;
569 register struct proc *p;
570 register struct getrusage_args *uap;
572 register struct rusage *rup;
577 rup = &p->p_stats->p_ru;
578 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
581 case RUSAGE_CHILDREN:
582 rup = &p->p_stats->p_cru;
588 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
589 sizeof (struct rusage)));
594 register struct rusage *ru, *ru2;
596 register long *ip, *ip2;
599 timevaladd(&ru->ru_utime, &ru2->ru_utime);
600 timevaladd(&ru->ru_stime, &ru2->ru_stime);
601 if (ru->ru_maxrss < ru2->ru_maxrss)
602 ru->ru_maxrss = ru2->ru_maxrss;
603 ip = &ru->ru_first; ip2 = &ru2->ru_first;
604 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
609 * Make a copy of the plimit structure.
610 * We share these structures copy-on-write after fork,
611 * and copy when a limit is changed.
617 register struct plimit *copy;
619 MALLOC(copy, struct plimit *, sizeof(struct plimit),
620 M_SUBPROC, M_WAITOK);
621 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));