1: /*
   2:  * Copyright (c) 1986 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  *
   6:  *	@(#)kern_resource.c	1.4 (2.11BSD GTE) 1997/2/14
   7:  */
   8: 
   9: #include "param.h"
  10: #include "user.h"
  11: #include "proc.h"
  12: #include "systm.h"
  13: #include "vm.h"
  14: #include "kernel.h"
  15: 
  16: /*
  17:  * Resource controls and accounting.
  18:  */
  19: 
  20: getpriority()
  21: {
  22:     register struct a {
  23:         int which;
  24:         int who;
  25:     } *uap = (struct a *)u.u_ap;
  26:     register struct proc *p;
  27:     register int low = PRIO_MAX + 1;
  28: 
  29:     switch (uap->which) {
  30: 
  31:     case PRIO_PROCESS:
  32:         if (uap->who == 0)
  33:             p = u.u_procp;
  34:         else
  35:             p = pfind(uap->who);
  36:         if (p == 0)
  37:             break;
  38:         low = p->p_nice;
  39:         break;
  40: 
  41:     case PRIO_PGRP:
  42:         if (uap->who == 0)
  43:             uap->who = u.u_procp->p_pgrp;
  44:         for (p = allproc; p != NULL; p = p->p_nxt) {
  45:             if (p->p_pgrp == uap->who &&
  46:                 p->p_nice < low)
  47:                 low = p->p_nice;
  48:         }
  49:         break;
  50: 
  51:     case PRIO_USER:
  52:         if (uap->who == 0)
  53:             uap->who = u.u_uid;
  54:         for (p = allproc; p != NULL; p = p->p_nxt) {
  55:             if (p->p_uid == uap->who &&
  56:                 p->p_nice < low)
  57:                 low = p->p_nice;
  58:         }
  59:         break;
  60: 
  61:     default:
  62:         u.u_error = EINVAL;
  63:         return;
  64:     }
  65:     if (low == PRIO_MAX + 1) {
  66:         u.u_error = ESRCH;
  67:         return;
  68:     }
  69:     u.u_r.r_val1 = low;
  70: }
  71: 
  72: setpriority()
  73: {
  74:     register struct a {
  75:         int which;
  76:         int who;
  77:         int prio;
  78:     } *uap = (struct a *)u.u_ap;
  79:     register struct proc *p;
  80:     register int found = 0;
  81: 
  82:     switch (uap->which) {
  83: 
  84:     case PRIO_PROCESS:
  85:         if (uap->who == 0)
  86:             p = u.u_procp;
  87:         else
  88:             p = pfind(uap->who);
  89:         if (p == 0)
  90:             break;
  91:         donice(p, uap->prio);
  92:         found++;
  93:         break;
  94: 
  95:     case PRIO_PGRP:
  96:         if (uap->who == 0)
  97:             uap->who = u.u_procp->p_pgrp;
  98:         for (p = allproc; p != NULL; p = p->p_nxt)
  99:             if (p->p_pgrp == uap->who) {
 100:                 donice(p, uap->prio);
 101:                 found++;
 102:             }
 103:         break;
 104: 
 105:     case PRIO_USER:
 106:         if (uap->who == 0)
 107:             uap->who = u.u_uid;
 108:         for (p = allproc; p != NULL; p = p->p_nxt)
 109:             if (p->p_uid == uap->who) {
 110:                 donice(p, uap->prio);
 111:                 found++;
 112:             }
 113:         break;
 114: 
 115:     default:
 116:         u.u_error = EINVAL;
 117:         return;
 118:     }
 119:     if (found == 0)
 120:         u.u_error = ESRCH;
 121: }
 122: 
 123: donice(p, n)
 124:     register struct proc *p;
 125:     register int n;
 126: {
 127: 
 128:     if (u.u_uid && u.u_ruid &&
 129:         u.u_uid != p->p_uid && u.u_ruid != p->p_uid) {
 130:         u.u_error = EPERM;
 131:         return;
 132:     }
 133:     if (n > PRIO_MAX)
 134:         n = PRIO_MAX;
 135:     if (n < PRIO_MIN)
 136:         n = PRIO_MIN;
 137:     if (n < p->p_nice && !suser()) {
 138:         u.u_error = EACCES;
 139:         return;
 140:     }
 141:     p->p_nice = n;
 142: }
 143: 
 144: setrlimit()
 145: {
 146:     register struct a {
 147:         u_int   which;
 148:         struct  rlimit *lim;
 149:     } *uap = (struct a *)u.u_ap;
 150:     struct rlimit alim;
 151:     register struct rlimit *alimp;
 152:     extern unsigned maxdmap;
 153: 
 154:     if (uap->which >= RLIM_NLIMITS) {
 155:         u.u_error = EINVAL;
 156:         return;
 157:     }
 158:     alimp = &u.u_rlimit[uap->which];
 159:     u.u_error = copyin((caddr_t)uap->lim, (caddr_t)&alim,
 160:         sizeof (struct rlimit));
 161:     if (u.u_error)
 162:         return;
 163:     if (uap->which == RLIMIT_CPU) {
 164:         /*
 165: 		 * 2.11 stores RLIMIT_CPU as ticks to keep from making
 166: 		 * hardclock() do long multiplication/division.
 167: 		 */
 168:         if (alim.rlim_cur >= RLIM_INFINITY / hz)
 169:             alim.rlim_cur = RLIM_INFINITY;
 170:         else
 171:             alim.rlim_cur = alim.rlim_cur * hz;
 172:         if (alim.rlim_max >= RLIM_INFINITY / hz)
 173:             alim.rlim_max = RLIM_INFINITY;
 174:         else
 175:             alim.rlim_max = alim.rlim_max * hz;
 176:     }
 177:     if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max)
 178:         if (!suser())
 179:             return;
 180:     *alimp = alim;
 181: }
 182: 
 183: getrlimit()
 184: {
 185:     register struct a {
 186:         u_int   which;
 187:         struct  rlimit *rlp;
 188:     } *uap = (struct a *)u.u_ap;
 189: 
 190:     if (uap->which >= RLIM_NLIMITS) {
 191:         u.u_error = EINVAL;
 192:         return;
 193:     }
 194:     if (uap->which == RLIMIT_CPU) {
 195:         struct rlimit alim;
 196: 
 197:         alim = u.u_rlimit[uap->which];
 198:         if (alim.rlim_cur != RLIM_INFINITY)
 199:             alim.rlim_cur = alim.rlim_cur / hz;
 200:         if (alim.rlim_max != RLIM_INFINITY)
 201:             alim.rlim_max = alim.rlim_max / hz;
 202:         u.u_error = copyout((caddr_t)&alim,
 203:             (caddr_t)uap->rlp,sizeof (struct rlimit));
 204:     }
 205:     else u.u_error = copyout((caddr_t)&u.u_rlimit[uap->which],
 206:         (caddr_t)uap->rlp,sizeof (struct rlimit));
 207: }
 208: 
 209: getrusage()
 210: {
 211:     register struct a {
 212:         int who;
 213:         struct  rusage *rusage;
 214:     } *uap = (struct a *)u.u_ap;
 215:     register struct k_rusage *rup;
 216:     struct rusage ru;
 217: 
 218:     switch (uap->who) {
 219: 
 220:     case RUSAGE_SELF:
 221:         rup = &u.u_ru;
 222:         break;
 223: 
 224:     case RUSAGE_CHILDREN:
 225:         rup = &u.u_cru;
 226:         break;
 227: 
 228:     default:
 229:         u.u_error = EINVAL;
 230:         return;
 231:     }
 232:     rucvt(&ru,rup);
 233:     u.u_error = copyout((caddr_t)&ru, (caddr_t)uap->rusage,
 234:         sizeof (struct rusage));
 235: }
 236: 
 237: ruadd(ru, ru2)
 238:     struct k_rusage *ru, *ru2;
 239: {
 240:     register long *ip, *ip2;
 241:     register int i;
 242: 
 243:     /*
 244: 	 * since the kernel timeval structures are single longs,
 245: 	 * fold them into the loop.
 246: 	 */
 247:     ip = &ru->k_ru_first; ip2 = &ru2->k_ru_first;
 248:     for (i = &ru->k_ru_last - &ru->k_ru_first; i >= 0; i--)
 249:         *ip++ += *ip2++;
 250: }
 251: 
 252: /*
 253:  * Convert an internal kernel rusage structure into a `real' rusage structure.
 254:  */
 255: rucvt(rup, krup)
 256:     register struct rusage      *rup;
 257:     register struct k_rusage    *krup;
 258: {
 259:     bzero((caddr_t)rup, sizeof(*rup));
 260:     rup->ru_utime.tv_sec   = krup->ru_utime / hz;
 261:     rup->ru_utime.tv_usec  = (krup->ru_utime % hz) * mshz;
 262:     rup->ru_stime.tv_sec   = krup->ru_stime / hz;
 263:     rup->ru_stime.tv_usec  = (krup->ru_stime % hz) * mshz;
 264:     rup->ru_ovly = krup->ru_ovly;
 265:     rup->ru_nswap = krup->ru_nswap;
 266:     rup->ru_inblock = krup->ru_inblock;
 267:     rup->ru_oublock = krup->ru_oublock;
 268:     rup->ru_msgsnd = krup->ru_msgsnd;
 269:     rup->ru_msgrcv = krup->ru_msgrcv;
 270:     rup->ru_nsignals = krup->ru_nsignals;
 271:     rup->ru_nvcsw = krup->ru_nvcsw;
 272:     rup->ru_nivcsw = krup->ru_nivcsw;
 273: }

Defined functions

donice defined in line 123; used 3 times
getpriority defined in line 20; used 2 times
getrlimit defined in line 183; used 2 times
getrusage defined in line 209; used 2 times
ruadd defined in line 237; used 2 times
rucvt defined in line 255; used 2 times
setpriority defined in line 72; used 2 times
setrlimit defined in line 144; used 2 times

Defined struct's

a defined in line 211; used 10 times
Last modified: 1997-02-15
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3630
Valid CSS Valid XHTML 1.0 Strict