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.17 1995/12/07 12:46:48 davidg Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/sysproto.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 <vm/vm.h> 52 #include <vm/vm_param.h> 53 #include <vm/vm_prot.h> 54 #include <vm/lock.h> 55 #include <vm/pmap.h> 56 #include <vm/vm_map.h> 57 58 int donice __P((struct proc *, struct proc *, int)); 59 int dosetrlimit __P((struct proc *, u_int, struct rlimit *)); 60 61 /* 62 * Resource controls and accounting. 63 */ 64 65 #ifndef _SYS_SYSPROTO_H_ 66 struct getpriority_args { 67 int which; 68 int who; 69 }; 70 #endif 71 int 72 getpriority(curp, uap, retval) 73 struct proc *curp; 74 register struct getpriority_args *uap; 75 int *retval; 76 { 77 register struct proc *p; 78 register int low = PRIO_MAX + 1; 79 80 switch (uap->which) { 81 82 case PRIO_PROCESS: 83 if (uap->who == 0) 84 p = curp; 85 else 86 p = pfind(uap->who); 87 if (p == 0) 88 break; 89 low = p->p_nice; 90 break; 91 92 case PRIO_PGRP: { 93 register struct pgrp *pg; 94 95 if (uap->who == 0) 96 pg = curp->p_pgrp; 97 else if ((pg = pgfind(uap->who)) == NULL) 98 break; 99 for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) { 100 if (p->p_nice < low) 101 low = p->p_nice; 102 } 103 break; 104 } 105 106 case PRIO_USER: 107 if (uap->who == 0) 108 uap->who = curp->p_ucred->cr_uid; 109 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 110 if (p->p_ucred->cr_uid == uap->who && 111 p->p_nice < low) 112 low = p->p_nice; 113 } 114 break; 115 116 default: 117 return (EINVAL); 118 } 119 if (low == PRIO_MAX + 1) 120 return (ESRCH); 121 *retval = low; 122 return (0); 123 } 124 125 #ifndef _SYS_SYSPROTO_H_ 126 struct setpriority_args { 127 int which; 128 int who; 129 int prio; 130 }; 131 #endif 132 /* ARGSUSED */ 133 int 134 setpriority(curp, uap, retval) 135 struct proc *curp; 136 register struct setpriority_args *uap; 137 int *retval; 138 { 139 register struct proc *p; 140 int found = 0, error = 0; 141 142 switch (uap->which) { 143 144 case PRIO_PROCESS: 145 if (uap->who == 0) 146 p = curp; 147 else 148 p = pfind(uap->who); 149 if (p == 0) 150 break; 151 error = donice(curp, p, uap->prio); 152 found++; 153 break; 154 155 case PRIO_PGRP: { 156 register struct pgrp *pg; 157 158 if (uap->who == 0) 159 pg = curp->p_pgrp; 160 else if ((pg = pgfind(uap->who)) == NULL) 161 break; 162 for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) { 163 error = donice(curp, p, uap->prio); 164 found++; 165 } 166 break; 167 } 168 169 case PRIO_USER: 170 if (uap->who == 0) 171 uap->who = curp->p_ucred->cr_uid; 172 for (p = (struct proc *)allproc; p != NULL; p = p->p_next) 173 if (p->p_ucred->cr_uid == uap->who) { 174 error = donice(curp, p, uap->prio); 175 found++; 176 } 177 break; 178 179 default: 180 return (EINVAL); 181 } 182 if (found == 0) 183 return (ESRCH); 184 return (error); 185 } 186 187 int 188 donice(curp, chgp, n) 189 register struct proc *curp, *chgp; 190 register int n; 191 { 192 register struct pcred *pcred = curp->p_cred; 193 194 if (pcred->pc_ucred->cr_uid && pcred->p_ruid && 195 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid && 196 pcred->p_ruid != chgp->p_ucred->cr_uid) 197 return (EPERM); 198 if (n > PRIO_MAX) 199 n = PRIO_MAX; 200 if (n < PRIO_MIN) 201 n = PRIO_MIN; 202 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag)) 203 return (EACCES); 204 chgp->p_nice = n; 205 (void)resetpriority(chgp); 206 return (0); 207 } 208 209 /* rtprio system call */ 210 #ifndef _SYS_SYSPROTO_H_ 211 struct rtprio_args { 212 int function; 213 pid_t pid; 214 struct rtprio *rtp; 215 }; 216 #endif 217 218 /* 219 * Set realtime priority 220 */ 221 222 /* ARGSUSED */ 223 int 224 rtprio(curp, uap, retval) 225 struct proc *curp; 226 register struct rtprio_args *uap; 227 int *retval; 228 { 229 register struct proc *p; 230 register struct pcred *pcred = curp->p_cred; 231 struct rtprio rtp; 232 int error; 233 234 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); 235 if (error) 236 return (error); 237 238 if (uap->pid == 0) 239 p = curp; 240 else 241 p = pfind(uap->pid); 242 243 if (p == 0) 244 return (ESRCH); 245 246 switch (uap->function) { 247 case RTP_LOOKUP: 248 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio))); 249 case RTP_SET: 250 if (pcred->pc_ucred->cr_uid && pcred->p_ruid && 251 pcred->pc_ucred->cr_uid != p->p_ucred->cr_uid && 252 pcred->p_ruid != p->p_ucred->cr_uid) 253 return (EPERM); 254 /* disallow setting rtprio in most cases if not superuser */ 255 if (suser(pcred->pc_ucred, &curp->p_acflag)) { 256 /* can't set someone else's */ 257 if (uap->pid) 258 return (EPERM); 259 /* can't set realtime priority */ 260 if (rtp.type == RTP_PRIO_REALTIME) 261 return (EPERM); 262 } 263 switch (rtp.type) { 264 case RTP_PRIO_REALTIME: 265 case RTP_PRIO_NORMAL: 266 case RTP_PRIO_IDLE: 267 if (rtp.prio > RTP_PRIO_MAX) 268 return (EINVAL); 269 p->p_rtprio = rtp; 270 return (0); 271 default: 272 return (EINVAL); 273 } 274 275 default: 276 return (EINVAL); 277 } 278 } 279 280 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 281 #ifndef _SYS_SYSPROTO_H_ 282 struct osetrlimit_args { 283 u_int which; 284 struct orlimit *rlp; 285 }; 286 #endif 287 /* ARGSUSED */ 288 int 289 osetrlimit(p, uap, retval) 290 struct proc *p; 291 register struct osetrlimit_args *uap; 292 int *retval; 293 { 294 struct orlimit olim; 295 struct rlimit lim; 296 int error; 297 298 if ((error = 299 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit)))) 300 return (error); 301 lim.rlim_cur = olim.rlim_cur; 302 lim.rlim_max = olim.rlim_max; 303 return (dosetrlimit(p, uap->which, &lim)); 304 } 305 306 #ifndef _SYS_SYSPROTO_H_ 307 struct ogetrlimit_args { 308 u_int which; 309 struct orlimit *rlp; 310 }; 311 #endif 312 /* ARGSUSED */ 313 int 314 ogetrlimit(p, uap, retval) 315 struct proc *p; 316 register struct ogetrlimit_args *uap; 317 int *retval; 318 { 319 struct orlimit olim; 320 321 if (uap->which >= RLIM_NLIMITS) 322 return (EINVAL); 323 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur; 324 if (olim.rlim_cur == -1) 325 olim.rlim_cur = 0x7fffffff; 326 olim.rlim_max = p->p_rlimit[uap->which].rlim_max; 327 if (olim.rlim_max == -1) 328 olim.rlim_max = 0x7fffffff; 329 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim))); 330 } 331 #endif /* COMPAT_43 || COMPAT_SUNOS */ 332 333 #ifndef _SYS_SYSPROTO_H_ 334 struct __setrlimit_args { 335 u_int which; 336 struct rlimit *rlp; 337 }; 338 #endif 339 /* ARGSUSED */ 340 int 341 setrlimit(p, uap, retval) 342 struct proc *p; 343 register struct __setrlimit_args *uap; 344 int *retval; 345 { 346 struct rlimit alim; 347 int error; 348 349 if ((error = 350 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit)))) 351 return (error); 352 return (dosetrlimit(p, uap->which, &alim)); 353 } 354 355 int 356 dosetrlimit(p, which, limp) 357 struct proc *p; 358 u_int which; 359 struct rlimit *limp; 360 { 361 register struct rlimit *alimp; 362 int error; 363 364 if (which >= RLIM_NLIMITS) 365 return (EINVAL); 366 alimp = &p->p_rlimit[which]; 367 368 /* 369 * Preserve historical bugs by treating negative limits as unsigned. 370 */ 371 if (limp->rlim_cur < 0) 372 limp->rlim_cur = RLIM_INFINITY; 373 if (limp->rlim_max < 0) 374 limp->rlim_max = RLIM_INFINITY; 375 376 if (limp->rlim_cur > alimp->rlim_max || 377 limp->rlim_max > alimp->rlim_max) 378 if ((error = suser(p->p_ucred, &p->p_acflag))) 379 return (error); 380 if (limp->rlim_cur > limp->rlim_max) 381 limp->rlim_cur = limp->rlim_max; 382 if (p->p_limit->p_refcnt > 1 && 383 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 384 p->p_limit->p_refcnt--; 385 p->p_limit = limcopy(p->p_limit); 386 alimp = &p->p_rlimit[which]; 387 } 388 389 switch (which) { 390 391 case RLIMIT_DATA: 392 if (limp->rlim_cur > MAXDSIZ) 393 limp->rlim_cur = MAXDSIZ; 394 if (limp->rlim_max > MAXDSIZ) 395 limp->rlim_max = MAXDSIZ; 396 break; 397 398 case RLIMIT_STACK: 399 if (limp->rlim_cur > MAXSSIZ) 400 limp->rlim_cur = MAXSSIZ; 401 if (limp->rlim_max > MAXSSIZ) 402 limp->rlim_max = MAXSSIZ; 403 /* 404 * Stack is allocated to the max at exec time with only 405 * "rlim_cur" bytes accessible. If stack limit is going 406 * up make more accessible, if going down make inaccessible. 407 */ 408 if (limp->rlim_cur != alimp->rlim_cur) { 409 vm_offset_t addr; 410 vm_size_t size; 411 vm_prot_t prot; 412 413 if (limp->rlim_cur > alimp->rlim_cur) { 414 prot = VM_PROT_ALL; 415 size = limp->rlim_cur - alimp->rlim_cur; 416 addr = USRSTACK - limp->rlim_cur; 417 } else { 418 prot = VM_PROT_NONE; 419 size = alimp->rlim_cur - limp->rlim_cur; 420 addr = USRSTACK - alimp->rlim_cur; 421 } 422 addr = trunc_page(addr); 423 size = round_page(size); 424 (void) vm_map_protect(&p->p_vmspace->vm_map, 425 addr, addr+size, prot, FALSE); 426 } 427 break; 428 429 case RLIMIT_NOFILE: 430 if (limp->rlim_cur > maxfilesperproc) 431 limp->rlim_cur = maxfilesperproc; 432 if (limp->rlim_max > maxfilesperproc) 433 limp->rlim_max = maxfilesperproc; 434 break; 435 436 case RLIMIT_NPROC: 437 if (limp->rlim_cur > maxprocperuid) 438 limp->rlim_cur = maxprocperuid; 439 if (limp->rlim_max > maxprocperuid) 440 limp->rlim_max = maxprocperuid; 441 break; 442 } 443 *alimp = *limp; 444 return (0); 445 } 446 447 #ifndef _SYS_SYSPROTO_H_ 448 struct __getrlimit_args { 449 u_int which; 450 struct rlimit *rlp; 451 }; 452 #endif 453 /* ARGSUSED */ 454 int 455 getrlimit(p, uap, retval) 456 struct proc *p; 457 register struct __getrlimit_args *uap; 458 int *retval; 459 { 460 461 if (uap->which >= RLIM_NLIMITS) 462 return (EINVAL); 463 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp, 464 sizeof (struct rlimit))); 465 } 466 467 /* 468 * Transform the running time and tick information in proc p into user, 469 * system, and interrupt time usage. 470 */ 471 void 472 calcru(p, up, sp, ip) 473 struct proc *p; 474 struct timeval *up; 475 struct timeval *sp; 476 struct timeval *ip; 477 { 478 quad_t totusec; 479 u_quad_t u, st, ut, it, tot; 480 long sec, usec; 481 int s; 482 struct timeval tv; 483 484 s = splstatclock(); 485 st = p->p_sticks; 486 ut = p->p_uticks; 487 it = p->p_iticks; 488 splx(s); 489 490 tot = st + ut + it; 491 if (tot == 0) { 492 up->tv_sec = up->tv_usec = 0; 493 sp->tv_sec = sp->tv_usec = 0; 494 if (ip != NULL) 495 ip->tv_sec = ip->tv_usec = 0; 496 return; 497 } 498 499 sec = p->p_rtime.tv_sec; 500 usec = p->p_rtime.tv_usec; 501 if (p == curproc) { 502 /* 503 * Adjust for the current time slice. This is actually fairly 504 * important since the error here is on the order of a time 505 * quantum, which is much greater than the sampling error. 506 */ 507 microtime(&tv); 508 sec += tv.tv_sec - runtime.tv_sec; 509 usec += tv.tv_usec - runtime.tv_usec; 510 } 511 totusec = (quad_t)sec * 1000000 + usec; 512 if (totusec < 0) { 513 printf("calcru: negative time: %d usec\n", totusec); 514 totusec = 0; 515 } 516 u = totusec; 517 st = (u * st) / tot; 518 sp->tv_sec = st / 1000000; 519 sp->tv_usec = st % 1000000; 520 ut = (u * ut) / tot; 521 up->tv_sec = ut / 1000000; 522 up->tv_usec = ut % 1000000; 523 if (ip != NULL) { 524 it = (u * it) / tot; 525 ip->tv_sec = it / 1000000; 526 ip->tv_usec = it % 1000000; 527 } 528 } 529 530 #ifndef _SYS_SYSPROTO_H_ 531 struct getrusage_args { 532 int who; 533 struct rusage *rusage; 534 }; 535 #endif 536 /* ARGSUSED */ 537 int 538 getrusage(p, uap, retval) 539 register struct proc *p; 540 register struct getrusage_args *uap; 541 int *retval; 542 { 543 register struct rusage *rup; 544 545 switch (uap->who) { 546 547 case RUSAGE_SELF: 548 rup = &p->p_stats->p_ru; 549 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 550 break; 551 552 case RUSAGE_CHILDREN: 553 rup = &p->p_stats->p_cru; 554 break; 555 556 default: 557 return (EINVAL); 558 } 559 return (copyout((caddr_t)rup, (caddr_t)uap->rusage, 560 sizeof (struct rusage))); 561 } 562 563 void 564 ruadd(ru, ru2) 565 register struct rusage *ru, *ru2; 566 { 567 register long *ip, *ip2; 568 register int i; 569 570 timevaladd(&ru->ru_utime, &ru2->ru_utime); 571 timevaladd(&ru->ru_stime, &ru2->ru_stime); 572 if (ru->ru_maxrss < ru2->ru_maxrss) 573 ru->ru_maxrss = ru2->ru_maxrss; 574 ip = &ru->ru_first; ip2 = &ru2->ru_first; 575 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 576 *ip++ += *ip2++; 577 } 578 579 /* 580 * Make a copy of the plimit structure. 581 * We share these structures copy-on-write after fork, 582 * and copy when a limit is changed. 583 */ 584 struct plimit * 585 limcopy(lim) 586 struct plimit *lim; 587 { 588 register struct plimit *copy; 589 590 MALLOC(copy, struct plimit *, sizeof(struct plimit), 591 M_SUBPROC, M_WAITOK); 592 bcopy(lim->pl_rlimit, copy->pl_rlimit, 593 sizeof(struct rlimit) * RLIM_NLIMITS); 594 copy->p_lflags = 0; 595 copy->p_refcnt = 1; 596 return (copy); 597 } 598