1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 39 * $Id: kern_resource.c,v 1.9 1994/12/02 23:00:40 ats Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/file.h> 46 #include <sys/resourcevar.h> 47 #include <sys/malloc.h> 48 #include <sys/proc.h> 49 50 #include <vm/vm.h> 51 52 int donice __P((struct proc *, struct proc *, int)); 53 int dosetrlimit __P((struct proc *, u_int, struct rlimit *)); 54 55 /* 56 * Resource controls and accounting. 57 */ 58 59 struct getpriority_args { 60 int which; 61 int who; 62 }; 63 int 64 getpriority(curp, uap, retval) 65 struct proc *curp; 66 register struct getpriority_args *uap; 67 int *retval; 68 { 69 register struct proc *p; 70 register int low = PRIO_MAX + 1; 71 72 switch (uap->which) { 73 74 case PRIO_PROCESS: 75 if (uap->who == 0) 76 p = curp; 77 else 78 p = pfind(uap->who); 79 if (p == 0) 80 break; 81 low = p->p_nice; 82 break; 83 84 case PRIO_PGRP: { 85 register struct pgrp *pg; 86 87 if (uap->who == 0) 88 pg = curp->p_pgrp; 89 else if ((pg = pgfind(uap->who)) == NULL) 90 break; 91 for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) { 92 if (p->p_nice < low) 93 low = p->p_nice; 94 } 95 break; 96 } 97 98 case PRIO_USER: 99 if (uap->who == 0) 100 uap->who = curp->p_ucred->cr_uid; 101 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 102 if (p->p_ucred->cr_uid == uap->who && 103 p->p_nice < low) 104 low = p->p_nice; 105 } 106 break; 107 108 default: 109 return (EINVAL); 110 } 111 if (low == PRIO_MAX + 1) 112 return (ESRCH); 113 *retval = low; 114 return (0); 115 } 116 117 struct setpriority_args { 118 int which; 119 int who; 120 int prio; 121 }; 122 /* ARGSUSED */ 123 int 124 setpriority(curp, uap, retval) 125 struct proc *curp; 126 register struct setpriority_args *uap; 127 int *retval; 128 { 129 register struct proc *p; 130 int found = 0, error = 0; 131 132 switch (uap->which) { 133 134 case PRIO_PROCESS: 135 if (uap->who == 0) 136 p = curp; 137 else 138 p = pfind(uap->who); 139 if (p == 0) 140 break; 141 error = donice(curp, p, uap->prio); 142 found++; 143 break; 144 145 case PRIO_PGRP: { 146 register struct pgrp *pg; 147 148 if (uap->who == 0) 149 pg = curp->p_pgrp; 150 else if ((pg = pgfind(uap->who)) == NULL) 151 break; 152 for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) { 153 error = donice(curp, p, uap->prio); 154 found++; 155 } 156 break; 157 } 158 159 case PRIO_USER: 160 if (uap->who == 0) 161 uap->who = curp->p_ucred->cr_uid; 162 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) 163 if (p->p_ucred->cr_uid == uap->who) { 164 error = donice(curp, p, uap->prio); 165 found++; 166 } 167 break; 168 169 default: 170 return (EINVAL); 171 } 172 if (found == 0) 173 return (ESRCH); 174 return (error); 175 } 176 177 int 178 donice(curp, chgp, n) 179 register struct proc *curp, *chgp; 180 register int n; 181 { 182 register struct pcred *pcred = curp->p_cred; 183 184 if (pcred->pc_ucred->cr_uid && pcred->p_ruid && 185 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid && 186 pcred->p_ruid != chgp->p_ucred->cr_uid) 187 return (EPERM); 188 if (n > PRIO_MAX) 189 n = PRIO_MAX; 190 if (n < PRIO_MIN) 191 n = PRIO_MIN; 192 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag)) 193 return (EACCES); 194 chgp->p_nice = n; 195 (void)resetpriority(chgp); 196 return (0); 197 } 198 199 /* rtprio system call */ 200 struct rtprio_args { 201 int function; 202 pid_t pid; 203 struct rtprio *rtprio; 204 }; 205 206 /* 207 * Set realtime priority 208 */ 209 210 /* ARGSUSED */ 211 int 212 rtprio(curp, uap, retval) 213 struct proc *curp; 214 register struct rtprio_args *uap; 215 int *retval; 216 { 217 register struct proc *p; 218 register struct pcred *pcred = curp->p_cred; 219 struct rtprio rtp; 220 int error; 221 222 error = copyin(uap->rtprio, &rtp, sizeof(struct rtprio)); 223 if (error) 224 return (error); 225 226 if (uap->pid == 0) 227 p = curp; 228 else 229 p = pfind(uap->pid); 230 231 if (p == 0) 232 return (ESRCH); 233 234 switch (uap->function) { 235 case RTP_LOOKUP: 236 return (copyout(&p->p_rtprio, uap->rtprio, sizeof(struct rtprio))); 237 case RTP_SET: 238 if (pcred->pc_ucred->cr_uid && pcred->p_ruid && 239 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid && 240 pcred->p_ruid != p->p_ucred->cr_uid) 241 return (EPERM); 242 /* disallow setting rtprio in most cases if not superuser */ 243 if (suser(pcred->pc_ucred, &curp->p_acflag)) { 244 /* can't set someone else's */ 245 if (uap->pid) 246 return (EPERM); 247 /* can't set realtime priority */ 248 if (rtp.type == RTP_PRIO_REALTIME) 249 return (EPERM); 250 } 251 switch (rtp.type) { 252 case RTP_PRIO_REALTIME: 253 case RTP_PRIO_NORMAL: 254 case RTP_PRIO_IDLE: 255 if (rtp.prio > RTP_PRIO_MAX) 256 return (EINVAL); 257 p->p_rtprio = rtp; 258 return (0); 259 default: 260 return (EINVAL); 261 } 262 263 default: 264 return (EINVAL); 265 } 266 } 267 268 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 269 struct setrlimit_args { 270 u_int which; 271 struct orlimit *lim; 272 }; 273 /* ARGSUSED */ 274 int 275 osetrlimit(p, uap, retval) 276 struct proc *p; 277 register struct setrlimit_args *uap; 278 int *retval; 279 { 280 struct orlimit olim; 281 struct rlimit lim; 282 int error; 283 284 if ((error = 285 copyin((caddr_t)uap->lim, (caddr_t)&olim, sizeof(struct orlimit)))) 286 return (error); 287 lim.rlim_cur = olim.rlim_cur; 288 lim.rlim_max = olim.rlim_max; 289 return (dosetrlimit(p, uap->which, &lim)); 290 } 291 292 struct getrlimit_args { 293 u_int which; 294 struct orlimit *rlp; 295 }; 296 /* ARGSUSED */ 297 int 298 ogetrlimit(p, uap, retval) 299 struct proc *p; 300 register struct getrlimit_args *uap; 301 int *retval; 302 { 303 struct orlimit olim; 304 305 if (uap->which >= RLIM_NLIMITS) 306 return (EINVAL); 307 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur; 308 if (olim.rlim_cur == -1) 309 olim.rlim_cur = 0x7fffffff; 310 olim.rlim_max = p->p_rlimit[uap->which].rlim_max; 311 if (olim.rlim_max == -1) 312 olim.rlim_max = 0x7fffffff; 313 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim))); 314 } 315 #endif /* COMPAT_43 || COMPAT_SUNOS */ 316 317 struct __setrlimit_args { 318 u_int which; 319 struct rlimit *lim; 320 }; 321 /* ARGSUSED */ 322 int 323 setrlimit(p, uap, retval) 324 struct proc *p; 325 register struct __setrlimit_args *uap; 326 int *retval; 327 { 328 struct rlimit alim; 329 int error; 330 331 if ((error = 332 copyin((caddr_t)uap->lim, (caddr_t)&alim, sizeof (struct rlimit)))) 333 return (error); 334 return (dosetrlimit(p, uap->which, &alim)); 335 } 336 337 int 338 dosetrlimit(p, which, limp) 339 struct proc *p; 340 u_int which; 341 struct rlimit *limp; 342 { 343 register struct rlimit *alimp; 344 int error; 345 346 if (which >= RLIM_NLIMITS) 347 return (EINVAL); 348 alimp = &p->p_rlimit[which]; 349 350 /* 351 * Preserve historical bugs by treating negative limits as unsigned. 352 */ 353 if (limp->rlim_cur < 0) 354 limp->rlim_cur = RLIM_INFINITY; 355 if (limp->rlim_max < 0) 356 limp->rlim_max = RLIM_INFINITY; 357 358 if (limp->rlim_cur > alimp->rlim_max || 359 limp->rlim_max > alimp->rlim_max) 360 if ((error = suser(p->p_ucred, &p->p_acflag))) 361 return (error); 362 if (limp->rlim_cur > limp->rlim_max) 363 limp->rlim_cur = limp->rlim_max; 364 if (p->p_limit->p_refcnt > 1 && 365 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 366 p->p_limit->p_refcnt--; 367 p->p_limit = limcopy(p->p_limit); 368 alimp = &p->p_rlimit[which]; 369 } 370 371 switch (which) { 372 373 case RLIMIT_DATA: 374 if (limp->rlim_cur > MAXDSIZ) 375 limp->rlim_cur = MAXDSIZ; 376 if (limp->rlim_max > MAXDSIZ) 377 limp->rlim_max = MAXDSIZ; 378 break; 379 380 case RLIMIT_STACK: 381 if (limp->rlim_cur > MAXSSIZ) 382 limp->rlim_cur = MAXSSIZ; 383 if (limp->rlim_max > MAXSSIZ) 384 limp->rlim_max = MAXSSIZ; 385 /* 386 * Stack is allocated to the max at exec time with only 387 * "rlim_cur" bytes accessible. If stack limit is going 388 * up make more accessible, if going down make inaccessible. 389 */ 390 if (limp->rlim_cur != alimp->rlim_cur) { 391 vm_offset_t addr; 392 vm_size_t size; 393 vm_prot_t prot; 394 395 if (limp->rlim_cur > alimp->rlim_cur) { 396 prot = VM_PROT_ALL; 397 size = limp->rlim_cur - alimp->rlim_cur; 398 addr = USRSTACK - limp->rlim_cur; 399 } else { 400 prot = VM_PROT_NONE; 401 size = alimp->rlim_cur - limp->rlim_cur; 402 addr = USRSTACK - alimp->rlim_cur; 403 } 404 addr = trunc_page(addr); 405 size = round_page(size); 406 (void) vm_map_protect(&p->p_vmspace->vm_map, 407 addr, addr+size, prot, FALSE); 408 } 409 break; 410 411 case RLIMIT_NOFILE: 412 if (limp->rlim_cur > maxfiles) 413 limp->rlim_cur = maxfiles; 414 if (limp->rlim_max > maxfiles) 415 limp->rlim_max = maxfiles; 416 break; 417 418 case RLIMIT_NPROC: 419 if (limp->rlim_cur > maxproc) 420 limp->rlim_cur = maxproc; 421 if (limp->rlim_max > maxproc) 422 limp->rlim_max = maxproc; 423 break; 424 } 425 *alimp = *limp; 426 return (0); 427 } 428 429 struct __getrlimit_args { 430 u_int which; 431 struct rlimit *rlp; 432 }; 433 /* ARGSUSED */ 434 int 435 getrlimit(p, uap, retval) 436 struct proc *p; 437 register struct __getrlimit_args *uap; 438 int *retval; 439 { 440 441 if (uap->which >= RLIM_NLIMITS) 442 return (EINVAL); 443 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp, 444 sizeof (struct rlimit))); 445 } 446 447 /* 448 * Transform the running time and tick information in proc p into user, 449 * system, and interrupt time usage. 450 */ 451 void 452 calcru(p, up, sp, ip) 453 register struct proc *p; 454 register struct timeval *up; 455 register struct timeval *sp; 456 register struct timeval *ip; 457 { 458 register u_quad_t u, st, ut, it, tot; 459 register u_long sec, usec; 460 register int s; 461 struct timeval tv; 462 463 s = splstatclock(); 464 st = p->p_sticks; 465 ut = p->p_uticks; 466 it = p->p_iticks; 467 splx(s); 468 469 tot = st + ut + it; 470 if (tot == 0) { 471 up->tv_sec = up->tv_usec = 0; 472 sp->tv_sec = sp->tv_usec = 0; 473 if (ip != NULL) 474 ip->tv_sec = ip->tv_usec = 0; 475 return; 476 } 477 478 sec = p->p_rtime.tv_sec; 479 usec = p->p_rtime.tv_usec; 480 if (p == curproc) { 481 /* 482 * Adjust for the current time slice. This is actually fairly 483 * important since the error here is on the order of a time 484 * quantum, which is much greater than the sampling error. 485 */ 486 microtime(&tv); 487 sec += tv.tv_sec - runtime.tv_sec; 488 usec += tv.tv_usec - runtime.tv_usec; 489 } 490 u = sec * 1000000 + usec; 491 st = (u * st) / tot; 492 sp->tv_sec = st / 1000000; 493 sp->tv_usec = st % 1000000; 494 ut = (u * ut) / tot; 495 up->tv_sec = ut / 1000000; 496 up->tv_usec = ut % 1000000; 497 if (ip != NULL) { 498 it = (u * it) / tot; 499 ip->tv_sec = it / 1000000; 500 ip->tv_usec = it % 1000000; 501 } 502 } 503 504 struct getrusage_args { 505 int who; 506 struct rusage *rusage; 507 }; 508 /* ARGSUSED */ 509 int 510 getrusage(p, uap, retval) 511 register struct proc *p; 512 register struct getrusage_args *uap; 513 int *retval; 514 { 515 register struct rusage *rup; 516 517 switch (uap->who) { 518 519 case RUSAGE_SELF: 520 rup = &p->p_stats->p_ru; 521 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 522 break; 523 524 case RUSAGE_CHILDREN: 525 rup = &p->p_stats->p_cru; 526 break; 527 528 default: 529 return (EINVAL); 530 } 531 return (copyout((caddr_t)rup, (caddr_t)uap->rusage, 532 sizeof (struct rusage))); 533 } 534 535 void 536 ruadd(ru, ru2) 537 register struct rusage *ru, *ru2; 538 { 539 register long *ip, *ip2; 540 register int i; 541 542 timevaladd(&ru->ru_utime, &ru2->ru_utime); 543 timevaladd(&ru->ru_stime, &ru2->ru_stime); 544 if (ru->ru_maxrss < ru2->ru_maxrss) 545 ru->ru_maxrss = ru2->ru_maxrss; 546 ip = &ru->ru_first; ip2 = &ru2->ru_first; 547 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 548 *ip++ += *ip2++; 549 } 550 551 /* 552 * Make a copy of the plimit structure. 553 * We share these structures copy-on-write after fork, 554 * and copy when a limit is changed. 555 */ 556 struct plimit * 557 limcopy(lim) 558 struct plimit *lim; 559 { 560 register struct plimit *copy; 561 562 MALLOC(copy, struct plimit *, sizeof(struct plimit), 563 M_SUBPROC, M_WAITOK); 564 bcopy(lim->pl_rlimit, copy->pl_rlimit, 565 sizeof(struct rlimit) * RLIM_NLIMITS); 566 copy->p_lflags = 0; 567 copy->p_refcnt = 1; 568 return (copy); 569 } 570