do not panic after modified data found on the freelist
[unix-history] / usr / src / sys / kern / kern_resource.c
index 44d4c58..47cb6f4 100644 (file)
-/*     kern_resource.c 4.9     82/07/12        */
+/*-
+ * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)kern_resource.c     7.13 (Berkeley) %G%
+ */
 
 
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/acct.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/inode.h"
-#include "../h/proc.h"
-#include "../h/seg.h"
+#include "param.h"
+#include "resourcevar.h"
+#include "malloc.h"
+#include "proc.h"
 
 
-struct inode *acctp;
+#include "vm/vm.h"
 
 /*
 
 /*
- * Perform process accounting functions.
+ * Resource controls and accounting.
  */
  */
-sysacct()
-{
-       register struct inode *ip;
-       register struct a {
-               char    *fname;
+
+getpriority(curp, uap, retval)
+       struct proc *curp;
+       register struct args {
+               int     which;
+               int     who;
        } *uap;
        } *uap;
+       int *retval;
+{
+       register struct proc *p;
+       register int low = PRIO_MAX + 1;
 
 
-       uap = (struct a *)u.u_ap;
-       if (suser()) {
-               if (uap->fname==NULL) {
-                       if (ip = acctp) {
-                               irele(ip);
-                               acctp = NULL;
-                       }
-                       return;
+       switch (uap->which) {
+
+       case PRIO_PROCESS:
+               if (uap->who == 0)
+                       p = curp;
+               else
+                       p = pfind(uap->who);
+               if (p == 0)
+                       break;
+               low = p->p_nice;
+               break;
+
+       case PRIO_PGRP: {
+               register struct pgrp *pg;
+
+               if (uap->who == 0)
+                       pg = curp->p_pgrp;
+               else if ((pg = pgfind(uap->who)) == NULL)
+                       break;
+               for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
+                       if (p->p_nice < low)
+                               low = p->p_nice;
                }
                }
-               if (acctp) {
-                       u.u_error = EBUSY;
-                       return;
+               break;
+       }
+
+       case PRIO_USER:
+               if (uap->who == 0)
+                       uap->who = curp->p_ucred->cr_uid;
+               for (p = allproc; p != NULL; p = p->p_nxt) {
+                       if (p->p_ucred->cr_uid == uap->who &&
+                           p->p_nice < low)
+                               low = p->p_nice;
                }
                }
-               ip = namei(uchar, 0, 1);
-               if(ip == NULL)
-                       return;
-               if((ip->i_mode & IFMT) != IFREG) {
-                       u.u_error = EACCES;
-                       iput(ip);
-                       return;
+               break;
+
+       default:
+               return (EINVAL);
+       }
+       if (low == PRIO_MAX + 1)
+               return (ESRCH);
+       *retval = low;
+       return (0);
+}
+
+/* ARGSUSED */
+setpriority(curp, uap, retval)
+       struct proc *curp;
+       register struct args {
+               int     which;
+               int     who;
+               int     prio;
+       } *uap;
+       int *retval;
+{
+       register struct proc *p;
+       int found = 0, error = 0;
+
+       switch (uap->which) {
+
+       case PRIO_PROCESS:
+               if (uap->who == 0)
+                       p = curp;
+               else
+                       p = pfind(uap->who);
+               if (p == 0)
+                       break;
+               error = donice(curp, p, uap->prio);
+               found++;
+               break;
+
+       case PRIO_PGRP: {
+               register struct pgrp *pg;
+                
+               if (uap->who == 0)
+                       pg = curp->p_pgrp;
+               else if ((pg = pgfind(uap->who)) == NULL)
+                       break;
+               for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) {
+                       error = donice(curp, p, uap->prio);
+                       found++;
                }
                }
-               acctp = ip;
-               iunlock(ip);
+               break;
+       }
+
+       case PRIO_USER:
+               if (uap->who == 0)
+                       uap->who = curp->p_ucred->cr_uid;
+               for (p = allproc; p != NULL; p = p->p_nxt)
+                       if (p->p_ucred->cr_uid == uap->who) {
+                               error = donice(curp, p, uap->prio);
+                               found++;
+                       }
+               break;
+
+       default:
+               return (EINVAL);
        }
        }
+       if (found == 0)
+               return (ESRCH);
+       return (0);
 }
 
 }
 
-struct acct acctbuf;
-/*
- * On exit, write a record on the accounting file.
- */
-acct()
+donice(curp, chgp, n)
+       register struct proc *curp, *chgp;
+       register int n;
 {
 {
-       register i;
-       register struct inode *ip;
-       off_t siz;
-       register struct acct *ap = &acctbuf;
-
-       if ((ip=acctp)==NULL)
-               return;
-       ilock(ip);
-       for (i=0; i<sizeof(ap->ac_comm); i++)
-               ap->ac_comm[i] = u.u_comm[i];
-       ap->ac_utime = compress((long)u.u_vm.vm_utime);
-       ap->ac_stime = compress((long)u.u_vm.vm_stime);
-       ap->ac_etime = compress((long)(time - u.u_start));
-       ap->ac_btime = u.u_start;
-       ap->ac_uid = u.u_ruid;
-       ap->ac_gid = u.u_rgid;
-       ap->ac_mem = 0;
-       if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
-               ap->ac_mem = (u.u_vm.vm_ixrss + u.u_vm.vm_idsrss) / i;
-       ap->ac_io = compress((long)(u.u_vm.vm_inblk + u.u_vm.vm_oublk));
-       ap->ac_tty = u.u_ttyd;
-       ap->ac_flag = u.u_acflag;
-       siz = ip->i_size;
-       u.u_offset = siz;
-       u.u_base = (caddr_t)ap;
-       u.u_count = sizeof(acctbuf);
-       u.u_segflg = 1;
-       u.u_error = 0;
-       writei(ip);
-       if(u.u_error)
-               ip->i_size = siz;
-       iunlock(ip);
+       register struct pcred *pcred = curp->p_cred;
+
+       if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
+           pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
+           pcred->p_ruid != chgp->p_ucred->cr_uid)
+               return (EPERM);
+       if (n > PRIO_MAX)
+               n = PRIO_MAX;
+       if (n < PRIO_MIN)
+               n = PRIO_MIN;
+       if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
+               return (EACCES);
+       chgp->p_nice = n;
+       (void) setpri(chgp);
+       return (0);
 }
 
 }
 
-/*
- * Produce a pseudo-floating point representation
- * with 3 bits base-8 exponent, 13 bits fraction.
- */
-compress(t)
-register long t;
+/* ARGSUSED */
+setrlimit(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               u_int   which;
+               struct  rlimit *lim;
+       } *uap;
+       int *retval;
 {
 {
-       register exp = 0, round = 0;
+       struct rlimit alim;
+       register struct rlimit *alimp;
+       extern unsigned maxdmap;
+       int error;
 
 
-       while (t >= 8192) {
-               exp++;
-               round = t&04;
-               t >>= 3;
+       if (uap->which >= RLIM_NLIMITS)
+               return (EINVAL);
+       alimp = &p->p_rlimit[uap->which];
+       if (error =
+           copyin((caddr_t)uap->lim, (caddr_t)&alim, sizeof (struct rlimit)))
+               return (error);
+       if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max)
+               if (error = suser(p->p_ucred, &p->p_acflag))
+                       return (error);
+       if (p->p_limit->p_refcnt > 1 &&
+           (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
+               p->p_limit->p_refcnt--;
+               p->p_limit = limcopy(p->p_limit);
        }
        }
-       if (round) {
-               t++;
-               if (t >= 8192) {
-                       t >>= 3;
-                       exp++;
+
+       switch (uap->which) {
+
+       case RLIMIT_DATA:
+               if (alim.rlim_cur > maxdmap)
+                       alim.rlim_cur = maxdmap;
+               if (alim.rlim_max > maxdmap)
+                       alim.rlim_max = maxdmap;
+               break;
+
+       case RLIMIT_STACK:
+               if (alim.rlim_cur > maxdmap)
+                       alim.rlim_cur = maxdmap;
+               if (alim.rlim_max > maxdmap)
+                       alim.rlim_max = maxdmap;
+               /*
+                * Stack is allocated to the max at exec time with only
+                * "rlim_cur" bytes accessible.  If stack limit is going
+                * up make more accessible, if going down make inaccessible.
+                */
+               if (alim.rlim_cur != alimp->rlim_cur) {
+                       vm_offset_t addr;
+                       vm_size_t size;
+                       vm_prot_t prot;
+
+                       if (alim.rlim_cur > alimp->rlim_cur) {
+                               prot = VM_PROT_ALL;
+                               size = alim.rlim_cur - alimp->rlim_cur;
+                               addr = USRSTACK - alim.rlim_cur;
+                       } else {
+                               prot = VM_PROT_NONE;
+                               size = alimp->rlim_cur - alim.rlim_cur;
+                               addr = USRSTACK - alimp->rlim_cur;
+                       }
+                       addr = trunc_page(addr);
+                       size = round_page(size);
+                       (void) vm_map_protect(&p->p_vmspace->vm_map,
+                                             addr, addr+size, prot, FALSE);
                }
                }
+               break;
+       }
+       p->p_rlimit[uap->which] = alim;
+       return (0);
+}
+
+/* ARGSUSED */
+getrlimit(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               u_int   which;
+               struct  rlimit *rlp;
+       } *uap;
+       int *retval;
+{
+
+       if (uap->which >= RLIM_NLIMITS)
+               return (EINVAL);
+       return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp,
+           sizeof (struct rlimit)));
+}
+
+/* ARGSUSED */
+getrusage(p, uap, retval)
+       register struct proc *p;
+       register struct args {
+               int     who;
+               struct  rusage *rusage;
+       } *uap;
+       int *retval;
+{
+       register struct rusage *rup;
+
+       switch (uap->who) {
+
+       case RUSAGE_SELF: {
+               int s;
+
+               rup = &p->p_stats->p_ru;
+               s = splclock();
+               rup->ru_stime = p->p_stime;
+               rup->ru_utime = p->p_utime;
+               splx(s);
+               break;
+       }
+
+       case RUSAGE_CHILDREN:
+               rup = &p->p_stats->p_cru;
+               break;
+
+       default:
+               return (EINVAL);
        }
        }
-       return((exp<<13) + t);
+       return (copyout((caddr_t)rup, (caddr_t)uap->rusage,
+           sizeof (struct rusage)));
+}
+
+ruadd(ru, ru2)
+       register struct rusage *ru, *ru2;
+{
+       register long *ip, *ip2;
+       register int i;
+
+       timevaladd(&ru->ru_utime, &ru2->ru_utime);
+       timevaladd(&ru->ru_stime, &ru2->ru_stime);
+       if (ru->ru_maxrss < ru2->ru_maxrss)
+               ru->ru_maxrss = ru2->ru_maxrss;
+       ip = &ru->ru_first; ip2 = &ru2->ru_first;
+       for (i = &ru->ru_last - &ru->ru_first; i > 0; i--)
+               *ip++ += *ip2++;
+}
+
+/*
+ * Make a copy of the plimit structure.
+ * We share these structures copy-on-write after fork,
+ * and copy when a limit is changed.
+ */
+struct plimit *
+limcopy(lim)
+       struct plimit *lim;
+{
+       register struct plimit *copy;
+
+       MALLOC(copy, struct plimit *, sizeof(struct plimit),
+           M_SUBPROC, M_WAITOK);
+       bcopy(lim->pl_rlimit, copy->pl_rlimit,
+           sizeof(struct rlimit) * RLIM_NLIMITS);
+       copy->p_lflags = 0;
+       copy->p_refcnt = 1;
+       return (copy);
 }
 }