lint
[unix-history] / usr / src / sys / kern / kern_resource.c
index b488947..eb5af6c 100644 (file)
@@ -1,8 +1,7 @@
-/*     kern_resource.c 4.12    82/09/04        */
+/*     kern_resource.c 4.16    82/10/17        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #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/dir.h"
 #include "../h/user.h"
 #include "../h/inode.h"
 #include "../h/uio.h"
 #include "../h/vm.h"
 
 #include "../h/uio.h"
 #include "../h/vm.h"
 
+/*
+ * Resource controls and accounting.
+ */
+
 getpriority()
 {
        register struct a {
 getpriority()
 {
        register struct a {
@@ -32,17 +35,35 @@ getpriority()
                if (p == 0)
                        return;
                u.u_r.r_val1 = u.u_procp->p_nice;
                if (p == 0)
                        return;
                u.u_r.r_val1 = u.u_procp->p_nice;
+               u.u_error = 0;
                break;
 
        case PRIO_PGRP:
                if (uap->who == 0)
                        uap->who = u.u_procp->p_pgrp;
                break;
 
        case PRIO_PGRP:
                if (uap->who == 0)
                        uap->who = u.u_procp->p_pgrp;
-               for (p = proc; p < procNPROC; p++)
+               for (p = proc; p < procNPROC; p++) {
+                       if (p->p_stat == NULL)
+                               continue;
                        if (p->p_pgrp == uap->who &&
                            p->p_nice < u.u_r.r_val1) {
                                u.u_r.r_val1 = p->p_nice;
                                u.u_error = 0;
                        }
                        if (p->p_pgrp == uap->who &&
                            p->p_nice < u.u_r.r_val1) {
                                u.u_r.r_val1 = p->p_nice;
                                u.u_error = 0;
                        }
+               }
+               break;
+
+       case PRIO_USER:
+               if (uap->who == 0)
+                       uap->who = u.u_uid;
+               for (p = proc; p < procNPROC; p++) {
+                       if (p->p_stat == NULL)
+                               continue;
+                       if (p->p_uid == uap->who &&
+                           p->p_nice < u.u_r.r_val1) {
+                               u.u_r.r_val1 = p->p_nice;
+                               u.u_error = 0;
+                       }
+               }
                break;
 
        default:
                break;
 
        default:
@@ -65,18 +86,31 @@ setpriority()
        switch (uap->which) {
 
        case PRIO_PROCESS:
        switch (uap->which) {
 
        case PRIO_PROCESS:
-               p = pfind(uap->who);
+               if (uap->who == 0)
+                       p = u.u_procp;
+               else
+                       p = pfind(uap->who);
                if (p == 0)
                        return;
                donice(p, uap->prio);
                break;
 
        case PRIO_PGRP:
                if (p == 0)
                        return;
                donice(p, uap->prio);
                break;
 
        case PRIO_PGRP:
+               if (uap->who == 0)
+                       uap->who = u.u_procp->p_pgrp;
                for (p = proc; p < procNPROC; p++)
                        if (p->p_pgrp == uap->who)
                                donice(p, uap->prio);
                break;
 
                for (p = proc; p < procNPROC; p++)
                        if (p->p_pgrp == uap->who)
                                donice(p, uap->prio);
                break;
 
+       case PRIO_USER:
+               if (uap->who == 0)
+                       uap->who = u.u_uid;
+               for (p = proc; p < procNPROC; p++)
+                       if (p->p_uid == uap->who)
+                               donice(p, uap->prio);
+               break;
+
        default:
                u.u_error = EINVAL;
                break;
        default:
                u.u_error = EINVAL;
                break;
@@ -90,23 +124,25 @@ donice(p, n)
 
        if (u.u_uid && u.u_ruid &&
            u.u_uid != p->p_uid && u.u_ruid != p->p_uid) {
 
        if (u.u_uid && u.u_ruid &&
            u.u_uid != p->p_uid && u.u_ruid != p->p_uid) {
-               u.u_error = EPERM;
+               u.u_error = EACCES;
                return;
        }
                return;
        }
-       n += p->p_nice;
+       n += NZERO;
        if (n >= 2*NZERO)
                n = 2*NZERO - 1;
        if (n < 0)
                n = 0;
        if (n >= 2*NZERO)
                n = 2*NZERO - 1;
        if (n < 0)
                n = 0;
-       if (n < p->p_nice && !suser())
+       if (n < p->p_nice && !suser()) {
+               u.u_error = EACCES;
                return;
                return;
+       }
        p->p_nice = n;
        (void) setpri(p);
        if (u.u_error == ESRCH)
                u.u_error = 0;
 }
 
        p->p_nice = n;
        (void) setpri(p);
        if (u.u_error == ESRCH)
                u.u_error = 0;
 }
 
-setlimit()
+setrlimit()
 {
        register struct a {
                u_int   which;
 {
        register struct a {
                u_int   which;
@@ -144,7 +180,7 @@ setlimit()
                u.u_procp->p_maxrss = alim.rlim_cur/NBPG;
 }
 
                u.u_procp->p_maxrss = alim.rlim_cur/NBPG;
 }
 
-getlimit()
+getrlimit()
 {
        register struct a {
                u_int   which;
 {
        register struct a {
                u_int   which;
@@ -155,7 +191,7 @@ getlimit()
                u.u_error = EINVAL;
                return;
        }
                u.u_error = EINVAL;
                return;
        }
-       if (copyout((caddr_t)&u.u_rlimit[uap->which], uap->rlp,
+       if (copyout((caddr_t)&u.u_rlimit[uap->which], (caddr_t)uap->rlp,
            sizeof (struct rlimit))) {
                u.u_error = EFAULT;
                return;
            sizeof (struct rlimit))) {
                u.u_error = EFAULT;
                return;
@@ -184,7 +220,8 @@ getrusage()
                u.u_error = EINVAL;
                return;
        }
                u.u_error = EINVAL;
                return;
        }
-       if (copyout((caddr_t)rup, uap->rusage, sizeof (struct rusage))) {
+       if (copyout((caddr_t)rup, (caddr_t)uap->rusage,
+           sizeof (struct rusage))) {
                u.u_error = EFAULT;
                return;
        }
                u.u_error = EFAULT;
                return;
        }
@@ -193,7 +230,7 @@ getrusage()
 ruadd(ru, ru2)
        register struct rusage *ru, *ru2;
 {
 ruadd(ru, ru2)
        register struct rusage *ru, *ru2;
 {
-       register int *ip, *ip2;
+       register u_int *ip, *ip2;
        register int i;
 
        timevaladd(&ru->ru_utime, &ru2->ru_utime);
        register int i;
 
        timevaladd(&ru->ru_utime, &ru2->ru_utime);
@@ -205,137 +242,88 @@ ruadd(ru, ru2)
                *ip++ += *ip2++;
 }
 
                *ip++ += *ip2++;
 }
 
-struct inode *acctp;
-struct inode *savacctp;
+#ifndef NOCOMPAT
+onice()
+{
+       register struct a {
+               int     niceness;
+       } *uap = (struct a *)u.u_ap;
+       register struct proc *p = u.u_procp;
 
 
-long   acctlow = 2;            /* stop accounting when < 2% data space left */
-long   accthigh = 4;           /* resume when space risen to > 4% */
+       donice(p, (p->p_nice-NZERO)+uap->niceness);
+}
 
 
-/*
- * Perform process accounting functions.
- */
-sysacct()
+#include "../h/times.h"
+
+otimes()
 {
 {
-       register struct inode *ip;
        register struct a {
        register struct a {
-               char    *fname;
-       } *uap;
-
-       uap = (struct a *)u.u_ap;
-       if (suser()) {
-               if (savacctp) {
-                       acctp = savacctp;
-                       savacctp = NULL;
-               }
-               if (uap->fname==NULL) {
-                       if (ip = acctp) {
-                               irele(ip);
-                               acctp = NULL;
-                       }
-                       return;
-               }
-               ip = namei(uchar, 0, 1);
-               if(ip == NULL)
-                       return;
-               if((ip->i_mode & IFMT) != IFREG) {
-                       u.u_error = EACCES;
-                       iput(ip);
-                       return;
-               }
-               if (acctp && (acctp->i_number != ip->i_number ||
-                   acctp->i_dev != ip->i_dev))
-                       irele(acctp);
-               acctp = ip;
-               iunlock(ip);
+               struct  tms *tmsb;
+       } *uap = (struct a *)u.u_ap;
+       struct tms atms;
+
+       atms.tms_utime = scale60(&u.u_ru.ru_utime);
+       atms.tms_stime = scale60(&u.u_ru.ru_stime);
+       atms.tms_cutime = scale60(&u.u_cru.ru_utime);
+       atms.tms_cstime = scale60(&u.u_cru.ru_stime);
+       if (copyout((caddr_t)&atms, (caddr_t)uap->tmsb, sizeof (atms))) {
+               u.u_error = EFAULT;
+               return;
        }
 }
 
        }
 }
 
-struct acct acctbuf;
-/*
- * On exit, write a record on the accounting file.
- */
-acct()
+scale60(tvp)
+       register struct timeval *tvp;
 {
 {
-       register i;
-       register struct inode *ip;
-       off_t siz;
-       register struct acct *ap = &acctbuf;
-
-       if (savacctp && savacctp->i_fs->fs_cstotal.cs_nbfree >
-           accthigh * savacctp->i_fs->fs_dsize / 100) {
-               acctp = savacctp;
-               savacctp = NULL;
-               printf("Accounting resumed\n");
-       }
-       if ((ip=acctp)==NULL)
-               return;
-       if (acctp->i_fs->fs_cstotal.cs_nbfree <
-           acctlow * acctp->i_fs->fs_dsize / 100) {
-               savacctp = acctp;
-               acctp = NULL;
-               printf("Accounting suspended\n");
-               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_ru.ru_utime.tv_sec);
-       ap->ac_stime = compress((long)u.u_ru.ru_stime.tv_sec);
-       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_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec)
-               ap->ac_mem =
-                   (u.u_ru.ru_ixrss + u.u_ru.ru_idrss + u.u_ru.ru_isrss) / i;
-       ap->ac_io = compress((long)(u.u_ru.ru_inblock + u.u_ru.ru_oublock));
-       if (u.u_ttyp)
-               ap->ac_tty = u.u_ttyd;
-       else
-               ap->ac_tty = NODEV;
-       ap->ac_flag = u.u_acflag;
-       siz = ip->i_size;
-       u.u_error =
-           rdwri(UIO_WRITE, ip, (caddr_t)ap, sizeof (acctbuf), siz,
-               1, (int *)0);
-       if (u.u_error)
-               ip->i_size = siz;
-       iunlock(ip);
+
+       return (tvp->tv_sec * 60 + tvp->tv_usec / 16667);
 }
 
 }
 
-/*
- * Produce a pseudo-floating point representation
- * with 3 bits base-8 exponent, 13 bits fraction.
- */
-compress(t)
-register long t;
+#include <vtimes.h>
+
+ovtimes()
 {
 {
-       register exp = 0, round = 0;
+       register struct a {
+               struct  vtimes *par;
+               struct  vtimes *chi;
+       } *uap = (struct a *)u.u_ap;
+       struct vtimes avt;
 
 
-       while (t >= 8192) {
-               exp++;
-               round = t&04;
-               t >>= 3;
+       if (uap->par) {
+               getvtimes(&u.u_ru, &avt);
+               if (copyout((caddr_t)&avt, (caddr_t)uap->par, sizeof (avt))) {
+                       u.u_error = EFAULT;
+                       return;
+               }
        }
        }
-       if (round) {
-               t++;
-               if (t >= 8192) {
-                       t >>= 3;
-                       exp++;
+       if (uap->chi) {
+               getvtimes(&u.u_cru, &avt);
+               if (copyout((caddr_t)&avt, (caddr_t)uap->chi, sizeof (avt))) {
+                       u.u_error = EFAULT;
+                       return;
                }
        }
                }
        }
-       return((exp<<13) + t);
 }
 
 }
 
-#ifndef NOCOMPAT
-onice()
+getvtimes(aru, avt)
+       register struct rusage *aru;
+       register struct vtimes *avt;
+{
+
+       avt->vm_utime = scale60(&aru->ru_utime);
+       avt->vm_stime = scale60(&aru->ru_stime);
+       avt->vm_idsrss = ((aru->ru_idrss+aru->ru_isrss) / hz) * 60;
+       avt->vm_ixrss = aru->ru_ixrss / hz * 60;
+       avt->vm_maxrss = aru->ru_maxrss;
+       avt->vm_majflt = aru->ru_majflt;
+       avt->vm_minflt = aru->ru_minflt;
+       avt->vm_nswap = aru->ru_nswap;
+       avt->vm_inblk = aru->ru_inblock;
+       avt->vm_oublk = aru->ru_oublock;
+}
+
+ovlimit()
 {
 {
-       register struct a {
-               int     niceness;
-       } *uap;
 
 
-       uap = (struct a *)u.u_ap;
-       donice(u.u_procp, uap->niceness);
+       u.u_error = EACCES;
 }
 }
-#endif