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.37 1998/05/28 09:30:18 phk 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 static 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 for (p = pg->pg_members.lh_first; p != 0;
102 p = p->p_pglist.le_next) {
111 uap->who = curp->p_ucred->cr_uid;
112 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
113 if (p->p_ucred->cr_uid == uap->who &&
121 if (low == PRIO_MAX + 1)
123 curp->p_retval[0] = low;
127 #ifndef _SYS_SYSPROTO_H_
128 struct setpriority_args {
136 setpriority(curp, uap)
138 register struct setpriority_args *uap;
140 register struct proc *p;
141 int found = 0, error = 0;
143 switch (uap->which) {
152 error = donice(curp, p, uap->prio);
157 register struct pgrp *pg;
161 else if ((pg = pgfind(uap->who)) == NULL)
163 for (p = pg->pg_members.lh_first; p != 0;
164 p = p->p_pglist.le_next) {
165 error = donice(curp, p, uap->prio);
173 uap->who = curp->p_ucred->cr_uid;
174 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next)
175 if (p->p_ucred->cr_uid == uap->who) {
176 error = donice(curp, p, uap->prio);
190 donice(curp, chgp, n)
191 register struct proc *curp, *chgp;
194 register struct pcred *pcred = curp->p_cred;
196 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
197 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
198 pcred->p_ruid != chgp->p_ucred->cr_uid)
204 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
207 (void)resetpriority(chgp);
211 /* rtprio system call */
212 #ifndef _SYS_SYSPROTO_H_
221 * Set realtime priority
228 register struct rtprio_args *uap;
230 register struct proc *p;
231 register struct pcred *pcred = curp->p_cred;
235 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
247 switch (uap->function) {
249 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio)));
251 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
252 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid &&
253 pcred->p_ruid != p->p_ucred->cr_uid)
255 /* disallow setting rtprio in most cases if not superuser */
256 if (suser(pcred->pc_ucred, &curp->p_acflag)) {
257 /* can't set someone else's */
260 /* can't set realtime priority */
262 * Realtime priority has to be restricted for reasons which should be
263 * obvious. However, for idle priority, there is a potential for
264 * system deadlock if an idleprio process gains a lock on a resource
265 * that other processes need (and the idleprio process can't run
266 * due to a CPU-bound normal process). Fix me! XXX
269 if (RTP_PRIO_IS_REALTIME(rtp.type))
271 if (rtp.type != RTP_PRIO_NORMAL)
278 case RTP_PRIO_REALTIME:
279 case RTP_PRIO_NORMAL:
281 if (rtp.prio > RTP_PRIO_MAX)
294 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
295 #ifndef _SYS_SYSPROTO_H_
296 struct osetrlimit_args {
305 register struct osetrlimit_args *uap;
312 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
314 lim.rlim_cur = olim.rlim_cur;
315 lim.rlim_max = olim.rlim_max;
316 return (dosetrlimit(p, uap->which, &lim));
319 #ifndef _SYS_SYSPROTO_H_
320 struct ogetrlimit_args {
329 register struct ogetrlimit_args *uap;
333 if (uap->which >= RLIM_NLIMITS)
335 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
336 if (olim.rlim_cur == -1)
337 olim.rlim_cur = 0x7fffffff;
338 olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
339 if (olim.rlim_max == -1)
340 olim.rlim_max = 0x7fffffff;
341 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
343 #endif /* COMPAT_43 || COMPAT_SUNOS */
345 #ifndef _SYS_SYSPROTO_H_
346 struct __setrlimit_args {
355 register struct __setrlimit_args *uap;
361 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
363 return (dosetrlimit(p, uap->which, &alim));
367 dosetrlimit(p, which, limp)
372 register struct rlimit *alimp;
375 if (which >= RLIM_NLIMITS)
377 alimp = &p->p_rlimit[which];
380 * Preserve historical bugs by treating negative limits as unsigned.
382 if (limp->rlim_cur < 0)
383 limp->rlim_cur = RLIM_INFINITY;
384 if (limp->rlim_max < 0)
385 limp->rlim_max = RLIM_INFINITY;
387 if (limp->rlim_cur > alimp->rlim_max ||
388 limp->rlim_max > alimp->rlim_max)
389 if ((error = suser(p->p_ucred, &p->p_acflag)))
391 if (limp->rlim_cur > limp->rlim_max)
392 limp->rlim_cur = limp->rlim_max;
393 if (p->p_limit->p_refcnt > 1 &&
394 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
395 p->p_limit->p_refcnt--;
396 p->p_limit = limcopy(p->p_limit);
397 alimp = &p->p_rlimit[which];
403 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000)
404 p->p_limit->p_cpulimit = RLIM_INFINITY;
406 p->p_limit->p_cpulimit =
407 (rlim_t)1000000 * limp->rlim_cur;
410 if (limp->rlim_cur > MAXDSIZ)
411 limp->rlim_cur = MAXDSIZ;
412 if (limp->rlim_max > MAXDSIZ)
413 limp->rlim_max = MAXDSIZ;
417 if (limp->rlim_cur > MAXSSIZ)
418 limp->rlim_cur = MAXSSIZ;
419 if (limp->rlim_max > MAXSSIZ)
420 limp->rlim_max = MAXSSIZ;
422 * Stack is allocated to the max at exec time with only
423 * "rlim_cur" bytes accessible. If stack limit is going
424 * up make more accessible, if going down make inaccessible.
426 if (limp->rlim_cur != alimp->rlim_cur) {
431 if (limp->rlim_cur > alimp->rlim_cur) {
433 size = limp->rlim_cur - alimp->rlim_cur;
434 addr = USRSTACK - limp->rlim_cur;
437 size = alimp->rlim_cur - limp->rlim_cur;
438 addr = USRSTACK - alimp->rlim_cur;
440 addr = trunc_page(addr);
441 size = round_page(size);
442 (void) vm_map_protect(&p->p_vmspace->vm_map,
443 addr, addr+size, prot, FALSE);
448 if (limp->rlim_cur > maxfilesperproc)
449 limp->rlim_cur = maxfilesperproc;
450 if (limp->rlim_max > maxfilesperproc)
451 limp->rlim_max = maxfilesperproc;
455 if (limp->rlim_cur > maxprocperuid)
456 limp->rlim_cur = maxprocperuid;
457 if (limp->rlim_max > maxprocperuid)
458 limp->rlim_max = maxprocperuid;
465 #ifndef _SYS_SYSPROTO_H_
466 struct __getrlimit_args {
475 register struct __getrlimit_args *uap;
478 if (uap->which >= RLIM_NLIMITS)
480 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
481 sizeof (struct rlimit)));
485 * Transform the running time and tick information in proc p into user,
486 * system, and interrupt time usage.
489 calcru(p, up, sp, ip)
496 u_int64_t u, st, ut, it, tot;
500 /* XXX: why spl-protect ? worst case is an off-by-one report */
513 totusec = p->p_runtime;
515 if (p->p_oncpu != (char)0xff) {
520 * Adjust for the current time slice. This is actually fairly
521 * important since the error here is on the order of a time
522 * quantum, which is much greater than the sampling error.
525 totusec += (tv.tv_usec - p->p_switchtime.tv_usec) +
526 (tv.tv_sec - p->p_switchtime.tv_sec) * (int64_t)1000000;
529 * Copy the time that was just read to `switchtime' in case
530 * we are being called from exit1(). Exits don't go through
531 * mi_switch(), so `switchtime' doesn't get set in the normal
532 * way. We set it here instead of more cleanly in exit1()
533 * to avoid losing track of the time between the calls to
539 /* XXX no %qd in kernel. Truncate. */
540 printf("calcru: negative time of %ld usec for pid %d (%s)\n",
541 (long)totusec, p->p_pid, p->p_comm);
546 sp->tv_sec = st / 1000000;
547 sp->tv_usec = st % 1000000;
549 up->tv_sec = ut / 1000000;
550 up->tv_usec = ut % 1000000;
553 ip->tv_sec = it / 1000000;
554 ip->tv_usec = it % 1000000;
558 #ifndef _SYS_SYSPROTO_H_
559 struct getrusage_args {
561 struct rusage *rusage;
567 register struct proc *p;
568 register struct getrusage_args *uap;
570 register struct rusage *rup;
575 rup = &p->p_stats->p_ru;
576 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
579 case RUSAGE_CHILDREN:
580 rup = &p->p_stats->p_cru;
586 return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
587 sizeof (struct rusage)));
592 register struct rusage *ru, *ru2;
594 register long *ip, *ip2;
597 timevaladd(&ru->ru_utime, &ru2->ru_utime);
598 timevaladd(&ru->ru_stime, &ru2->ru_stime);
599 if (ru->ru_maxrss < ru2->ru_maxrss)
600 ru->ru_maxrss = ru2->ru_maxrss;
601 ip = &ru->ru_first; ip2 = &ru2->ru_first;
602 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
607 * Make a copy of the plimit structure.
608 * We share these structures copy-on-write after fork,
609 * and copy when a limit is changed.
615 register struct plimit *copy;
617 MALLOC(copy, struct plimit *, sizeof(struct plimit),
618 M_SUBPROC, M_WAITOK);
619 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit));