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