1 /* $NetBSD: kern_resource.c,v 1.31 1995/10/07 06:28:23 mycroft Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/file.h> 47 #include <sys/resourcevar.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 51 #include <sys/mount.h> 52 #include <sys/syscallargs.h> 53 54 #include <vm/vm.h> 55 56 int donice __P((struct proc *curp, struct proc *chgp, int n)); 57 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp)); 58 59 /* 60 * Resource controls and accounting. 61 */ 62 63 int 64 sys_getpriority(curp, v, retval) 65 struct proc *curp; 66 void *v; 67 register_t *retval; 68 { 69 register struct sys_getpriority_args /* { 70 syscallarg(int) which; 71 syscallarg(int) who; 72 } */ *uap = v; 73 register struct proc *p; 74 register int low = PRIO_MAX + 1; 75 76 switch (SCARG(uap, which)) { 77 78 case PRIO_PROCESS: 79 if (SCARG(uap, who) == 0) 80 p = curp; 81 else 82 p = pfind(SCARG(uap, who)); 83 if (p == 0) 84 break; 85 low = p->p_nice; 86 break; 87 88 case PRIO_PGRP: { 89 register struct pgrp *pg; 90 91 if (SCARG(uap, who) == 0) 92 pg = curp->p_pgrp; 93 else if ((pg = pgfind(SCARG(uap, who))) == NULL) 94 break; 95 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 96 if (p->p_nice < low) 97 low = p->p_nice; 98 } 99 break; 100 } 101 102 case PRIO_USER: 103 if (SCARG(uap, who) == 0) 104 SCARG(uap, who) = curp->p_ucred->cr_uid; 105 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) 106 if (p->p_ucred->cr_uid == SCARG(uap, who) && 107 p->p_nice < low) 108 low = p->p_nice; 109 break; 110 111 default: 112 return (EINVAL); 113 } 114 if (low == PRIO_MAX + 1) 115 return (ESRCH); 116 *retval = low; 117 return (0); 118 } 119 120 /* ARGSUSED */ 121 int 122 sys_setpriority(curp, v, retval) 123 struct proc *curp; 124 void *v; 125 register_t *retval; 126 { 127 register struct sys_setpriority_args /* { 128 syscallarg(int) which; 129 syscallarg(int) who; 130 syscallarg(int) prio; 131 } */ *uap = v; 132 register struct proc *p; 133 int found = 0, error = 0; 134 135 switch (SCARG(uap, which)) { 136 137 case PRIO_PROCESS: 138 if (SCARG(uap, who) == 0) 139 p = curp; 140 else 141 p = pfind(SCARG(uap, who)); 142 if (p == 0) 143 break; 144 error = donice(curp, p, SCARG(uap, prio)); 145 found++; 146 break; 147 148 case PRIO_PGRP: { 149 register struct pgrp *pg; 150 151 if (SCARG(uap, who) == 0) 152 pg = curp->p_pgrp; 153 else if ((pg = pgfind(SCARG(uap, who))) == NULL) 154 break; 155 for (p = pg->pg_members.lh_first; p != 0; 156 p = p->p_pglist.le_next) { 157 error = donice(curp, p, SCARG(uap, prio)); 158 found++; 159 } 160 break; 161 } 162 163 case PRIO_USER: 164 if (SCARG(uap, who) == 0) 165 SCARG(uap, who) = curp->p_ucred->cr_uid; 166 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) 167 if (p->p_ucred->cr_uid == SCARG(uap, who)) { 168 error = donice(curp, p, SCARG(uap, prio)); 169 found++; 170 } 171 break; 172 173 default: 174 return (EINVAL); 175 } 176 if (found == 0) 177 return (ESRCH); 178 return (error); 179 } 180 181 int 182 donice(curp, chgp, n) 183 register struct proc *curp, *chgp; 184 register int n; 185 { 186 register struct pcred *pcred = curp->p_cred; 187 188 if (pcred->pc_ucred->cr_uid && pcred->p_ruid && 189 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid && 190 pcred->p_ruid != chgp->p_ucred->cr_uid) 191 return (EPERM); 192 if (n > PRIO_MAX) 193 n = PRIO_MAX; 194 if (n < PRIO_MIN) 195 n = PRIO_MIN; 196 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag)) 197 return (EACCES); 198 chgp->p_nice = n; 199 (void)resetpriority(chgp); 200 return (0); 201 } 202 203 /* ARGSUSED */ 204 int 205 sys_setrlimit(p, v, retval) 206 struct proc *p; 207 void *v; 208 register_t *retval; 209 { 210 register struct sys_setrlimit_args /* { 211 syscallarg(u_int) which; 212 syscallarg(struct rlimit *) rlp; 213 } */ *uap = v; 214 struct rlimit alim; 215 int error; 216 217 if (error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim, 218 sizeof (struct rlimit))) 219 return (error); 220 return (dosetrlimit(p, SCARG(uap, which), &alim)); 221 } 222 223 int 224 dosetrlimit(p, which, limp) 225 struct proc *p; 226 u_int which; 227 struct rlimit *limp; 228 { 229 register struct rlimit *alimp; 230 extern unsigned maxdmap, maxsmap; 231 int error; 232 233 if (which >= RLIM_NLIMITS) 234 return (EINVAL); 235 alimp = &p->p_rlimit[which]; 236 if (limp->rlim_cur > alimp->rlim_max || 237 limp->rlim_max > alimp->rlim_max) 238 if (error = suser(p->p_ucred, &p->p_acflag)) 239 return (error); 240 if (limp->rlim_cur > limp->rlim_max) 241 limp->rlim_cur = limp->rlim_max; 242 if (p->p_limit->p_refcnt > 1 && 243 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 244 p->p_limit->p_refcnt--; 245 p->p_limit = limcopy(p->p_limit); 246 alimp = &p->p_rlimit[which]; 247 } 248 249 switch (which) { 250 251 case RLIMIT_DATA: 252 if (limp->rlim_cur > maxdmap) 253 limp->rlim_cur = maxdmap; 254 if (limp->rlim_max > maxdmap) 255 limp->rlim_max = maxdmap; 256 break; 257 258 case RLIMIT_STACK: 259 if (limp->rlim_cur > maxsmap) 260 limp->rlim_cur = maxsmap; 261 if (limp->rlim_max > maxsmap) 262 limp->rlim_max = maxsmap; 263 /* 264 * Stack is allocated to the max at exec time with only 265 * "rlim_cur" bytes accessible. If stack limit is going 266 * up make more accessible, if going down make inaccessible. 267 */ 268 if (limp->rlim_cur != alimp->rlim_cur) { 269 vm_offset_t addr; 270 vm_size_t size; 271 vm_prot_t prot; 272 273 if (limp->rlim_cur > alimp->rlim_cur) { 274 prot = VM_PROT_ALL; 275 size = limp->rlim_cur - alimp->rlim_cur; 276 addr = USRSTACK - limp->rlim_cur; 277 } else { 278 prot = VM_PROT_NONE; 279 size = alimp->rlim_cur - limp->rlim_cur; 280 addr = USRSTACK - alimp->rlim_cur; 281 } 282 addr = trunc_page(addr); 283 size = round_page(size); 284 (void) vm_map_protect(&p->p_vmspace->vm_map, 285 addr, addr+size, prot, FALSE); 286 } 287 break; 288 289 case RLIMIT_NOFILE: 290 if (limp->rlim_cur > maxfiles) 291 limp->rlim_cur = maxfiles; 292 if (limp->rlim_max > maxfiles) 293 limp->rlim_max = maxfiles; 294 break; 295 296 case RLIMIT_NPROC: 297 if (limp->rlim_cur > maxproc) 298 limp->rlim_cur = maxproc; 299 if (limp->rlim_max > maxproc) 300 limp->rlim_max = maxproc; 301 break; 302 } 303 *alimp = *limp; 304 return (0); 305 } 306 307 /* ARGSUSED */ 308 int 309 sys_getrlimit(p, v, retval) 310 struct proc *p; 311 void *v; 312 register_t *retval; 313 { 314 register struct sys_getrlimit_args /* { 315 syscallarg(u_int) which; 316 syscallarg(struct rlimit *) rlp; 317 } */ *uap = v; 318 319 if (SCARG(uap, which) >= RLIM_NLIMITS) 320 return (EINVAL); 321 return (copyout((caddr_t)&p->p_rlimit[SCARG(uap, which)], 322 (caddr_t)SCARG(uap, rlp), sizeof (struct rlimit))); 323 } 324 325 /* 326 * Transform the running time and tick information in proc p into user, 327 * system, and interrupt time usage. 328 */ 329 void 330 calcru(p, up, sp, ip) 331 register struct proc *p; 332 register struct timeval *up; 333 register struct timeval *sp; 334 register struct timeval *ip; 335 { 336 register u_quad_t u, st, ut, it, tot; 337 register u_long sec, usec; 338 register int s; 339 struct timeval tv; 340 341 s = splstatclock(); 342 st = p->p_sticks; 343 ut = p->p_uticks; 344 it = p->p_iticks; 345 splx(s); 346 347 tot = st + ut + it; 348 if (tot == 0) { 349 up->tv_sec = up->tv_usec = 0; 350 sp->tv_sec = sp->tv_usec = 0; 351 if (ip != NULL) 352 ip->tv_sec = ip->tv_usec = 0; 353 return; 354 } 355 356 sec = p->p_rtime.tv_sec; 357 usec = p->p_rtime.tv_usec; 358 if (p == curproc) { 359 /* 360 * Adjust for the current time slice. This is actually fairly 361 * important since the error here is on the order of a time 362 * quantum, which is much greater than the sampling error. 363 */ 364 microtime(&tv); 365 sec += tv.tv_sec - runtime.tv_sec; 366 usec += tv.tv_usec - runtime.tv_usec; 367 } 368 u = sec * 1000000 + usec; 369 st = (u * st) / tot; 370 sp->tv_sec = st / 1000000; 371 sp->tv_usec = st % 1000000; 372 ut = (u * ut) / tot; 373 up->tv_sec = ut / 1000000; 374 up->tv_usec = ut % 1000000; 375 if (ip != NULL) { 376 it = (u * it) / tot; 377 ip->tv_sec = it / 1000000; 378 ip->tv_usec = it % 1000000; 379 } 380 } 381 382 /* ARGSUSED */ 383 int 384 sys_getrusage(p, v, retval) 385 register struct proc *p; 386 void *v; 387 register_t *retval; 388 { 389 register struct sys_getrusage_args /* { 390 syscallarg(int) who; 391 syscallarg(struct rusage *) rusage; 392 } */ *uap = v; 393 register struct rusage *rup; 394 395 switch (SCARG(uap, who)) { 396 397 case RUSAGE_SELF: 398 rup = &p->p_stats->p_ru; 399 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 400 break; 401 402 case RUSAGE_CHILDREN: 403 rup = &p->p_stats->p_cru; 404 break; 405 406 default: 407 return (EINVAL); 408 } 409 return (copyout((caddr_t)rup, (caddr_t)SCARG(uap, rusage), 410 sizeof (struct rusage))); 411 } 412 413 void 414 ruadd(ru, ru2) 415 register struct rusage *ru, *ru2; 416 { 417 register long *ip, *ip2; 418 register int i; 419 420 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime); 421 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime); 422 if (ru->ru_maxrss < ru2->ru_maxrss) 423 ru->ru_maxrss = ru2->ru_maxrss; 424 ip = &ru->ru_first; ip2 = &ru2->ru_first; 425 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 426 *ip++ += *ip2++; 427 } 428 429 /* 430 * Make a copy of the plimit structure. 431 * We share these structures copy-on-write after fork, 432 * and copy when a limit is changed. 433 */ 434 struct plimit * 435 limcopy(lim) 436 struct plimit *lim; 437 { 438 register struct plimit *copy; 439 440 MALLOC(copy, struct plimit *, sizeof(struct plimit), 441 M_SUBPROC, M_WAITOK); 442 bcopy(lim->pl_rlimit, copy->pl_rlimit, 443 sizeof(struct rlimit) * RLIM_NLIMITS); 444 copy->p_lflags = 0; 445 copy->p_refcnt = 1; 446 return (copy); 447 } 448