1*8100Sroot /* kern_resource.c 4.13 82/09/06 */ 27Sbill 37Sbill #include "../h/param.h" 47Sbill #include "../h/systm.h" 57Sbill #include "../h/dir.h" 67Sbill #include "../h/user.h" 77Sbill #include "../h/inode.h" 87Sbill #include "../h/proc.h" 97Sbill #include "../h/seg.h" 107499Sroot #include "../h/fs.h" 117817Sroot #include "../h/uio.h" 128031Sroot #include "../h/vm.h" 137Sbill 148031Sroot getpriority() 158031Sroot { 168031Sroot register struct a { 178031Sroot int which; 188031Sroot int who; 198031Sroot } *uap = (struct a *)u.u_ap; 208031Sroot register struct proc *p; 218031Sroot 228031Sroot u.u_r.r_val1 = NZERO+20; 238031Sroot u.u_error = ESRCH; 248031Sroot switch (uap->which) { 258031Sroot 268031Sroot case PRIO_PROCESS: 278031Sroot if (uap->who == 0) 288031Sroot p = u.u_procp; 298031Sroot else 308031Sroot p = pfind(uap->who); 318031Sroot if (p == 0) 328031Sroot return; 338031Sroot u.u_r.r_val1 = u.u_procp->p_nice; 348031Sroot break; 358031Sroot 368031Sroot case PRIO_PGRP: 378031Sroot if (uap->who == 0) 388031Sroot uap->who = u.u_procp->p_pgrp; 398031Sroot for (p = proc; p < procNPROC; p++) 408031Sroot if (p->p_pgrp == uap->who && 418031Sroot p->p_nice < u.u_r.r_val1) { 428031Sroot u.u_r.r_val1 = p->p_nice; 438031Sroot u.u_error = 0; 448031Sroot } 458031Sroot break; 468031Sroot 478031Sroot default: 488031Sroot u.u_error = EINVAL; 498031Sroot break; 508031Sroot } 518031Sroot u.u_r.r_val1 -= NZERO; 528031Sroot } 538031Sroot 548031Sroot setpriority() 558031Sroot { 568031Sroot register struct a { 578031Sroot int which; 588031Sroot int who; 598031Sroot int prio; 608031Sroot } *uap = (struct a *)u.u_ap; 618031Sroot register struct proc *p; 628031Sroot 638031Sroot u.u_error = ESRCH; 648031Sroot switch (uap->which) { 658031Sroot 668031Sroot case PRIO_PROCESS: 678031Sroot p = pfind(uap->who); 688031Sroot if (p == 0) 698031Sroot return; 708031Sroot donice(p, uap->prio); 718031Sroot break; 728031Sroot 738031Sroot case PRIO_PGRP: 748031Sroot for (p = proc; p < procNPROC; p++) 758031Sroot if (p->p_pgrp == uap->who) 768031Sroot donice(p, uap->prio); 778031Sroot break; 788031Sroot 798031Sroot default: 808031Sroot u.u_error = EINVAL; 818031Sroot break; 828031Sroot } 838031Sroot } 848031Sroot 858031Sroot donice(p, n) 868031Sroot register struct proc *p; 878031Sroot register int n; 888031Sroot { 898031Sroot 908031Sroot if (u.u_uid && u.u_ruid && 918031Sroot u.u_uid != p->p_uid && u.u_ruid != p->p_uid) { 928031Sroot u.u_error = EPERM; 938031Sroot return; 948031Sroot } 958031Sroot n += p->p_nice; 968031Sroot if (n >= 2*NZERO) 978031Sroot n = 2*NZERO - 1; 988031Sroot if (n < 0) 998031Sroot n = 0; 1008031Sroot if (n < p->p_nice && !suser()) 1018031Sroot return; 1028031Sroot p->p_nice = n; 1038031Sroot (void) setpri(p); 1048031Sroot if (u.u_error == ESRCH) 1058031Sroot u.u_error = 0; 1068031Sroot } 1078031Sroot 108*8100Sroot setrlimit() 1098031Sroot { 1108031Sroot register struct a { 1118031Sroot u_int which; 1128031Sroot struct rlimit *lim; 1138031Sroot } *uap = (struct a *)u.u_ap; 1148031Sroot struct rlimit alim; 1158031Sroot register struct rlimit *alimp; 1168031Sroot 1178031Sroot if (uap->which >= RLIM_NLIMITS) { 1188031Sroot u.u_error = EINVAL; 1198031Sroot return; 1208031Sroot } 1218031Sroot alimp = &u.u_rlimit[uap->which]; 1228031Sroot if (copyin((caddr_t)uap->lim, (caddr_t)&alim, sizeof (struct rlimit))) { 1238031Sroot u.u_error = EFAULT; 1248031Sroot return; 1258031Sroot } 1268031Sroot if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max) 1278031Sroot if (!suser()) 1288031Sroot return; 1298031Sroot switch (uap->which) { 1308031Sroot 1318031Sroot case RLIMIT_DATA: 1328031Sroot if (alim.rlim_cur > ctob(MAXDSIZ)) 1338031Sroot alim.rlim_cur = ctob(MAXDSIZ); 1348031Sroot break; 1358031Sroot 1368031Sroot case RLIMIT_STACK: 1378031Sroot if (alim.rlim_cur > ctob(MAXSSIZ)) 1388031Sroot alim.rlim_cur = ctob(MAXSSIZ); 1398031Sroot break; 1408031Sroot } 1418031Sroot *alimp = alim; 1428031Sroot if (uap->which == RLIMIT_RSS) 1438031Sroot u.u_procp->p_maxrss = alim.rlim_cur/NBPG; 1448031Sroot } 1458031Sroot 146*8100Sroot getrlimit() 1478031Sroot { 1488031Sroot register struct a { 1498031Sroot u_int which; 1508031Sroot struct rlimit *rlp; 1518031Sroot } *uap = (struct a *)u.u_ap; 1528031Sroot 1538031Sroot if (uap->which >= RLIM_NLIMITS) { 1548031Sroot u.u_error = EINVAL; 1558031Sroot return; 1568031Sroot } 1578031Sroot if (copyout((caddr_t)&u.u_rlimit[uap->which], uap->rlp, 1588031Sroot sizeof (struct rlimit))) { 1598031Sroot u.u_error = EFAULT; 1608031Sroot return; 1618031Sroot } 1628031Sroot } 1638031Sroot 1648031Sroot getrusage() 1658031Sroot { 1668031Sroot register struct a { 1678031Sroot int who; 1688031Sroot struct rusage *rusage; 1698031Sroot } *uap = (struct a *)u.u_ap; 1708031Sroot register struct rusage *rup; 1718031Sroot 1728031Sroot switch (uap->who) { 1738031Sroot 1748031Sroot case RUSAGE_SELF: 1758031Sroot rup = &u.u_ru; 1768031Sroot break; 1778031Sroot 1788031Sroot case RUSAGE_CHILDREN: 1798031Sroot rup = &u.u_cru; 1808031Sroot break; 1818031Sroot 1828031Sroot default: 1838031Sroot u.u_error = EINVAL; 1848031Sroot return; 1858031Sroot } 1868031Sroot if (copyout((caddr_t)rup, uap->rusage, sizeof (struct rusage))) { 1878031Sroot u.u_error = EFAULT; 1888031Sroot return; 1898031Sroot } 1908031Sroot } 1918031Sroot 1928031Sroot ruadd(ru, ru2) 1938031Sroot register struct rusage *ru, *ru2; 1948031Sroot { 1958031Sroot register int *ip, *ip2; 1968031Sroot register int i; 1978031Sroot 1988031Sroot timevaladd(&ru->ru_utime, &ru2->ru_utime); 1998031Sroot timevaladd(&ru->ru_stime, &ru2->ru_stime); 2008031Sroot if (ru->ru_maxrss < ru2->ru_maxrss) 2018031Sroot ru->ru_maxrss = ru2->ru_maxrss; 2028031Sroot ip = &ru->ru_first; ip2 = &ru2->ru_first; 2038031Sroot for (i = &ru->ru_last - &ru->ru_first; i > 0; i--) 2048031Sroot *ip++ += *ip2++; 2058031Sroot } 2068031Sroot 207*8100Sroot #ifndef NOCOMPAT 208*8100Sroot onice() 2097Sbill { 2107Sbill register struct a { 211*8100Sroot int niceness; 2127Sbill } *uap; 2137Sbill 2147Sbill uap = (struct a *)u.u_ap; 215*8100Sroot donice(u.u_procp, uap->niceness); 2167Sbill } 217*8100Sroot #endif 2187Sbill 219*8100Sroot otimes() 2207Sbill { 2217Sbill 222*8100Sroot /* XXX */ 2237Sbill } 2247Sbill 225*8100Sroot ovtimes() 2267Sbill { 2277Sbill 228*8100Sroot /* XXX */ 2297Sbill } 2307499Sroot 231*8100Sroot ovlimit() 2327499Sroot { 2337499Sroot 2347499Sroot } 235