correct no-such-process error
[unix-history] / usr / src / sys / kern / kern_resource.c
index 70bc347..1f71393 100644 (file)
-/*     kern_resource.c 4.7     82/06/07        */
-
-#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"
+/*
+ * Copyright (c) 1982 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)kern_resource.c     6.9 (Berkeley) %G%
+ */
 
 
-struct inode *acctp;
+#include "param.h"
+#include "systm.h"
+#include "dir.h"
+#include "user.h"
+#include "inode.h"
+#include "proc.h"
+#include "seg.h"
+#include "fs.h"
+#include "uio.h"
+#include "vm.h"
+#include "kernel.h"
 
 /*
 
 /*
- * Perform process accounting functions.
+ * Resource controls and accounting.
  */
  */
-sysacct()
+
+getpriority()
 {
 {
-       register struct inode *ip;
        register struct a {
        register struct a {
-               char    *fname;
-       } *uap;
-
-       uap = (struct a *)u.u_ap;
-       if (suser()) {
-               if (uap->fname==NULL) {
-                       if (ip = acctp) {
-                               ilock(ip);
-                               iput(ip);
-                               acctp = NULL;
-                       }
-                       return;
-               }
-               if (acctp) {
-                       u.u_error = EBUSY;
-                       return;
+               int     which;
+               int     who;
+       } *uap = (struct a *)u.u_ap;
+       register struct proc *p;
+       int low = PRIO_MAX + 1;
+
+       switch (uap->which) {
+
+       case PRIO_PROCESS:
+               if (uap->who == 0)
+                       p = u.u_procp;
+               else
+                       p = pfind(uap->who);
+               if (p == 0)
+                       break;
+               low = p->p_nice;
+               break;
+
+       case PRIO_PGRP:
+               if (uap->who == 0)
+                       uap->who = u.u_procp->p_pgrp;
+               for (p = allproc; p != NULL; p = p->p_nxt) {
+                       if (p->p_pgrp == 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;
+
+       case PRIO_USER:
+               if (uap->who == 0)
+                       uap->who = u.u_uid;
+               for (p = allproc; p != NULL; p = p->p_nxt) {
+                       if (p->p_uid == uap->who &&
+                           p->p_nice < low)
+                               low = p->p_nice;
                }
                }
-               acctp = ip;
-               iunlock(ip);
+               break;
+
+       default:
+               u.u_error = EINVAL;
+               return;
        }
        }
+       if (low == PRIO_MAX + 1) {
+               u.u_error = ESRCH;
+               return;
+       }
+       u.u_r.r_val1 = low;
 }
 
 }
 
-struct acct acctbuf;
-/*
- * On exit, write a record on the accounting file.
- */
-acct()
+setpriority()
 {
 {
-       register i;
-       register struct inode *ip;
-       off_t siz;
-       register struct acct *ap = &acctbuf;
+       register struct a {
+               int     which;
+               int     who;
+               int     prio;
+       } *uap = (struct a *)u.u_ap;
+       register struct proc *p;
+       int found = 0;
+
+       switch (uap->which) {
+
+       case PRIO_PROCESS:
+               if (uap->who == 0)
+                       p = u.u_procp;
+               else
+                       p = pfind(uap->who);
+               if (p == 0)
+                       break;
+               donice(p, uap->prio);
+               found++;
+               break;
+
+       case PRIO_PGRP:
+               if (uap->who == 0)
+                       uap->who = u.u_procp->p_pgrp;
+               for (p = allproc; p != NULL; p = p->p_nxt)
+                       if (p->p_pgrp == uap->who) {
+                               donice(p, uap->prio);
+                               found++;
+                       }
+               break;
 
 
-       if ((ip=acctp)==NULL)
+       case PRIO_USER:
+               if (uap->who == 0)
+                       uap->who = u.u_uid;
+               for (p = allproc; p != NULL; p = p->p_nxt)
+                       if (p->p_uid == uap->who) {
+                               donice(p, uap->prio);
+                               found++;
+                       }
+               break;
+
+       default:
+               u.u_error = EINVAL;
                return;
                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);
+       }
+       if (found == 0)
+               u.u_error = ESRCH;
 }
 
 }
 
-/*
- * Produce a pseudo-floating point representation
- * with 3 bits base-8 exponent, 13 bits fraction.
- */
-compress(t)
-register long t;
+donice(p, n)
+       register struct proc *p;
+       register int n;
 {
 {
-       register exp = 0, round = 0;
 
 
-       while (t >= 8192) {
-               exp++;
-               round = t&04;
-               t >>= 3;
+       if (u.u_uid && u.u_ruid &&
+           u.u_uid != p->p_uid && u.u_ruid != p->p_uid) {
+               u.u_error = EPERM;
+               return;
        }
        }
-       if (round) {
-               t++;
-               if (t >= 8192) {
-                       t >>= 3;
-                       exp++;
-               }
+       if (n > PRIO_MAX)
+               n = PRIO_MAX;
+       if (n < PRIO_MIN)
+               n = PRIO_MIN;
+       if (n < p->p_nice && !suser()) {
+               u.u_error = EACCES;
+               return;
        }
        }
-       return((exp<<13) + t);
+       p->p_nice = n;
+       (void) setpri(p);
+}
+
+setrlimit()
+{
+       register struct a {
+               u_int   which;
+               struct  rlimit *lim;
+       } *uap = (struct a *)u.u_ap;
+       struct rlimit alim;
+       register struct rlimit *alimp;
+       extern unsigned maxdmap;
+
+       if (uap->which >= RLIM_NLIMITS) {
+               u.u_error = EINVAL;
+               return;
+       }
+       alimp = &u.u_rlimit[uap->which];
+       u.u_error = copyin((caddr_t)uap->lim, (caddr_t)&alim,
+               sizeof (struct rlimit));
+       if (u.u_error)
+               return;
+       if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max)
+               if (!suser())
+                       return;
+       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;
+               break;
+       }
+       *alimp = alim;
+       if (uap->which == RLIMIT_RSS)
+               u.u_procp->p_maxrss = alim.rlim_cur/NBPG;
+}
+
+getrlimit()
+{
+       register struct a {
+               u_int   which;
+               struct  rlimit *rlp;
+       } *uap = (struct a *)u.u_ap;
+
+       if (uap->which >= RLIM_NLIMITS) {
+               u.u_error = EINVAL;
+               return;
+       }
+       u.u_error = copyout((caddr_t)&u.u_rlimit[uap->which], (caddr_t)uap->rlp,
+           sizeof (struct rlimit));
+}
+
+getrusage()
+{
+       register struct a {
+               int     who;
+               struct  rusage *rusage;
+       } *uap = (struct a *)u.u_ap;
+       register struct rusage *rup;
+
+       switch (uap->who) {
+
+       case RUSAGE_SELF:
+               rup = &u.u_ru;
+               break;
+
+       case RUSAGE_CHILDREN:
+               rup = &u.u_cru;
+               break;
+
+       default:
+               u.u_error = EINVAL;
+               return;
+       }
+       u.u_error = 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++;
 }
 }