1 /* $NetBSD: kern_resource.c,v 1.61 2001/11/12 15:25:14 lukem 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.8 (Berkeley) 2/14/95 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.61 2001/11/12 15:25:14 lukem Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/file.h> 50 #include <sys/resourcevar.h> 51 #include <sys/malloc.h> 52 #include <sys/pool.h> 53 #include <sys/proc.h> 54 55 #include <sys/mount.h> 56 #include <sys/syscallargs.h> 57 58 #include <uvm/uvm_extern.h> 59 60 /* 61 * Maximum process data and stack limits. 62 * They are variables so they are patchable. 63 * 64 * XXXX Do we really need them to be patchable? 65 */ 66 rlim_t maxdmap = MAXDSIZ; 67 rlim_t maxsmap = MAXSSIZ; 68 69 /* 70 * Resource controls and accounting. 71 */ 72 73 int 74 sys_getpriority(curp, v, retval) 75 struct proc *curp; 76 void *v; 77 register_t *retval; 78 { 79 struct sys_getpriority_args /* { 80 syscallarg(int) which; 81 syscallarg(int) who; 82 } */ *uap = v; 83 struct proc *p; 84 int low = NZERO + PRIO_MAX + 1; 85 86 switch (SCARG(uap, which)) { 87 88 case PRIO_PROCESS: 89 if (SCARG(uap, who) == 0) 90 p = curp; 91 else 92 p = pfind(SCARG(uap, who)); 93 if (p == 0) 94 break; 95 low = p->p_nice; 96 break; 97 98 case PRIO_PGRP: { 99 struct pgrp *pg; 100 101 if (SCARG(uap, who) == 0) 102 pg = curp->p_pgrp; 103 else if ((pg = pgfind(SCARG(uap, who))) == NULL) 104 break; 105 for (p = pg->pg_members.lh_first; p != 0; 106 p = p->p_pglist.le_next) { 107 if (p->p_nice < low) 108 low = p->p_nice; 109 } 110 break; 111 } 112 113 case PRIO_USER: 114 if (SCARG(uap, who) == 0) 115 SCARG(uap, who) = curp->p_ucred->cr_uid; 116 proclist_lock_read(); 117 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) 118 if (p->p_ucred->cr_uid == SCARG(uap, who) && 119 p->p_nice < low) 120 low = p->p_nice; 121 proclist_unlock_read(); 122 break; 123 124 default: 125 return (EINVAL); 126 } 127 if (low == NZERO + PRIO_MAX + 1) 128 return (ESRCH); 129 *retval = low - NZERO; 130 return (0); 131 } 132 133 /* ARGSUSED */ 134 int 135 sys_setpriority(curp, v, retval) 136 struct proc *curp; 137 void *v; 138 register_t *retval; 139 { 140 struct sys_setpriority_args /* { 141 syscallarg(int) which; 142 syscallarg(int) who; 143 syscallarg(int) prio; 144 } */ *uap = v; 145 struct proc *p; 146 int found = 0, error = 0; 147 148 switch (SCARG(uap, which)) { 149 150 case PRIO_PROCESS: 151 if (SCARG(uap, who) == 0) 152 p = curp; 153 else 154 p = pfind(SCARG(uap, who)); 155 if (p == 0) 156 break; 157 error = donice(curp, p, SCARG(uap, prio)); 158 found++; 159 break; 160 161 case PRIO_PGRP: { 162 struct pgrp *pg; 163 164 if (SCARG(uap, who) == 0) 165 pg = curp->p_pgrp; 166 else if ((pg = pgfind(SCARG(uap, who))) == NULL) 167 break; 168 for (p = pg->pg_members.lh_first; p != 0; 169 p = p->p_pglist.le_next) { 170 error = donice(curp, p, SCARG(uap, prio)); 171 found++; 172 } 173 break; 174 } 175 176 case PRIO_USER: 177 if (SCARG(uap, who) == 0) 178 SCARG(uap, who) = curp->p_ucred->cr_uid; 179 proclist_lock_read(); 180 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) 181 if (p->p_ucred->cr_uid == SCARG(uap, who)) { 182 error = donice(curp, p, SCARG(uap, prio)); 183 found++; 184 } 185 proclist_unlock_read(); 186 break; 187 188 default: 189 return (EINVAL); 190 } 191 if (found == 0) 192 return (ESRCH); 193 return (error); 194 } 195 196 int 197 donice(curp, chgp, n) 198 struct proc *curp, *chgp; 199 int n; 200 { 201 struct pcred *pcred = curp->p_cred; 202 int s; 203 204 if (pcred->pc_ucred->cr_uid && pcred->p_ruid && 205 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid && 206 pcred->p_ruid != chgp->p_ucred->cr_uid) 207 return (EPERM); 208 if (n > PRIO_MAX) 209 n = PRIO_MAX; 210 if (n < PRIO_MIN) 211 n = PRIO_MIN; 212 n += NZERO; 213 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag)) 214 return (EACCES); 215 chgp->p_nice = n; 216 SCHED_LOCK(s); 217 (void)resetpriority(chgp); 218 SCHED_UNLOCK(s); 219 return (0); 220 } 221 222 /* ARGSUSED */ 223 int 224 sys_setrlimit(p, v, retval) 225 struct proc *p; 226 void *v; 227 register_t *retval; 228 { 229 struct sys_setrlimit_args /* { 230 syscallarg(int) which; 231 syscallarg(const struct rlimit *) rlp; 232 } */ *uap = v; 233 int which = SCARG(uap, which); 234 struct rlimit alim; 235 int error; 236 237 error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit)); 238 if (error) 239 return (error); 240 return (dosetrlimit(p, p->p_cred, which, &alim)); 241 } 242 243 int 244 dosetrlimit(p, cred, which, limp) 245 struct proc *p; 246 struct pcred *cred; 247 int which; 248 struct rlimit *limp; 249 { 250 struct rlimit *alimp; 251 struct plimit *newplim; 252 int error; 253 254 if ((u_int)which >= RLIM_NLIMITS) 255 return (EINVAL); 256 257 if (limp->rlim_cur < 0 || limp->rlim_max < 0) 258 return (EINVAL); 259 260 alimp = &p->p_rlimit[which]; 261 /* if we don't change the value, no need to limcopy() */ 262 if (limp->rlim_cur == alimp->rlim_cur && 263 limp->rlim_max == alimp->rlim_max) 264 return 0; 265 266 if (limp->rlim_cur > alimp->rlim_max || 267 limp->rlim_max > alimp->rlim_max) 268 if ((error = suser(cred->pc_ucred, &p->p_acflag)) != 0) 269 return (error); 270 if (limp->rlim_cur > limp->rlim_max) 271 limp->rlim_cur = limp->rlim_max; 272 if (p->p_limit->p_refcnt > 1 && 273 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 274 newplim = limcopy(p->p_limit); 275 limfree(p->p_limit); 276 p->p_limit = newplim; 277 alimp = &p->p_rlimit[which]; 278 } 279 280 switch (which) { 281 282 case RLIMIT_DATA: 283 if (limp->rlim_cur > maxdmap) 284 limp->rlim_cur = maxdmap; 285 if (limp->rlim_max > maxdmap) 286 limp->rlim_max = maxdmap; 287 break; 288 289 case RLIMIT_STACK: 290 if (limp->rlim_cur > maxsmap) 291 limp->rlim_cur = maxsmap; 292 if (limp->rlim_max > maxsmap) 293 limp->rlim_max = maxsmap; 294 295 /* 296 * Stack is allocated to the max at exec time with 297 * only "rlim_cur" bytes accessible (In other words, 298 * allocates stack dividing two contiguous regions at 299 * "rlim_cur" bytes boundary). 300 * 301 * Since allocation is done in terms of page, roundup 302 * "rlim_cur" (otherwise, contiguous regions 303 * overlap). If stack limit is going up make more 304 * accessible, if going down make inaccessible. 305 */ 306 limp->rlim_cur = round_page(limp->rlim_cur); 307 if (limp->rlim_cur != alimp->rlim_cur) { 308 vaddr_t addr; 309 vsize_t size; 310 vm_prot_t prot; 311 312 if (limp->rlim_cur > alimp->rlim_cur) { 313 prot = VM_PROT_ALL; 314 size = limp->rlim_cur - alimp->rlim_cur; 315 addr = USRSTACK - limp->rlim_cur; 316 } else { 317 prot = VM_PROT_NONE; 318 size = alimp->rlim_cur - limp->rlim_cur; 319 addr = USRSTACK - alimp->rlim_cur; 320 } 321 (void) uvm_map_protect(&p->p_vmspace->vm_map, 322 addr, addr+size, prot, FALSE); 323 } 324 break; 325 326 case RLIMIT_NOFILE: 327 if (limp->rlim_cur > maxfiles) 328 limp->rlim_cur = maxfiles; 329 if (limp->rlim_max > maxfiles) 330 limp->rlim_max = maxfiles; 331 break; 332 333 case RLIMIT_NPROC: 334 if (limp->rlim_cur > maxproc) 335 limp->rlim_cur = maxproc; 336 if (limp->rlim_max > maxproc) 337 limp->rlim_max = maxproc; 338 break; 339 } 340 *alimp = *limp; 341 return (0); 342 } 343 344 /* ARGSUSED */ 345 int 346 sys_getrlimit(p, v, retval) 347 struct proc *p; 348 void *v; 349 register_t *retval; 350 { 351 struct sys_getrlimit_args /* { 352 syscallarg(int) which; 353 syscallarg(struct rlimit *) rlp; 354 } */ *uap = v; 355 int which = SCARG(uap, which); 356 357 if ((u_int)which >= RLIM_NLIMITS) 358 return (EINVAL); 359 return (copyout(&p->p_rlimit[which], SCARG(uap, rlp), 360 sizeof(struct rlimit))); 361 } 362 363 /* 364 * Transform the running time and tick information in proc p into user, 365 * system, and interrupt time usage. 366 */ 367 void 368 calcru(p, up, sp, ip) 369 struct proc *p; 370 struct timeval *up; 371 struct timeval *sp; 372 struct timeval *ip; 373 { 374 u_quad_t u, st, ut, it, tot; 375 long sec, usec; 376 int s; 377 struct timeval tv; 378 379 s = splstatclock(); 380 st = p->p_sticks; 381 ut = p->p_uticks; 382 it = p->p_iticks; 383 splx(s); 384 385 tot = st + ut + it; 386 if (tot == 0) { 387 up->tv_sec = up->tv_usec = 0; 388 sp->tv_sec = sp->tv_usec = 0; 389 if (ip != NULL) 390 ip->tv_sec = ip->tv_usec = 0; 391 return; 392 } 393 394 sec = p->p_rtime.tv_sec; 395 usec = p->p_rtime.tv_usec; 396 if (p->p_stat == SONPROC) { 397 struct schedstate_percpu *spc; 398 399 KDASSERT(p->p_cpu != NULL); 400 spc = &p->p_cpu->ci_schedstate; 401 402 /* 403 * Adjust for the current time slice. This is actually fairly 404 * important since the error here is on the order of a time 405 * quantum, which is much greater than the sampling error. 406 */ 407 microtime(&tv); 408 sec += tv.tv_sec - spc->spc_runtime.tv_sec; 409 usec += tv.tv_usec - spc->spc_runtime.tv_usec; 410 } 411 u = (u_quad_t) sec * 1000000 + usec; 412 st = (u * st) / tot; 413 sp->tv_sec = st / 1000000; 414 sp->tv_usec = st % 1000000; 415 ut = (u * ut) / tot; 416 up->tv_sec = ut / 1000000; 417 up->tv_usec = ut % 1000000; 418 if (ip != NULL) { 419 it = (u * it) / tot; 420 ip->tv_sec = it / 1000000; 421 ip->tv_usec = it % 1000000; 422 } 423 } 424 425 /* ARGSUSED */ 426 int 427 sys_getrusage(p, v, retval) 428 struct proc *p; 429 void *v; 430 register_t *retval; 431 { 432 struct sys_getrusage_args /* { 433 syscallarg(int) who; 434 syscallarg(struct rusage *) rusage; 435 } */ *uap = v; 436 struct rusage *rup; 437 438 switch (SCARG(uap, who)) { 439 440 case RUSAGE_SELF: 441 rup = &p->p_stats->p_ru; 442 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 443 break; 444 445 case RUSAGE_CHILDREN: 446 rup = &p->p_stats->p_cru; 447 break; 448 449 default: 450 return (EINVAL); 451 } 452 return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage))); 453 } 454 455 void 456 ruadd(ru, ru2) 457 struct rusage *ru, *ru2; 458 { 459 long *ip, *ip2; 460 int i; 461 462 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime); 463 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime); 464 if (ru->ru_maxrss < ru2->ru_maxrss) 465 ru->ru_maxrss = ru2->ru_maxrss; 466 ip = &ru->ru_first; ip2 = &ru2->ru_first; 467 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 468 *ip++ += *ip2++; 469 } 470 471 /* 472 * Make a copy of the plimit structure. 473 * We share these structures copy-on-write after fork, 474 * and copy when a limit is changed. 475 */ 476 struct plimit * 477 limcopy(lim) 478 struct plimit *lim; 479 { 480 struct plimit *newlim; 481 482 newlim = pool_get(&plimit_pool, PR_WAITOK); 483 memcpy(newlim->pl_rlimit, lim->pl_rlimit, 484 sizeof(struct rlimit) * RLIM_NLIMITS); 485 if (lim->pl_corename == defcorename) { 486 newlim->pl_corename = defcorename; 487 } else { 488 newlim->pl_corename = malloc(strlen(lim->pl_corename)+1, 489 M_TEMP, M_WAITOK); 490 strcpy(newlim->pl_corename, lim->pl_corename); 491 } 492 newlim->p_lflags = 0; 493 newlim->p_refcnt = 1; 494 return (newlim); 495 } 496 497 void 498 limfree(lim) 499 struct plimit *lim; 500 { 501 502 if (--lim->p_refcnt > 0) 503 return; 504 #ifdef DIAGNOSTIC 505 if (lim->p_refcnt < 0) 506 panic("limfree"); 507 #endif 508 if (lim->pl_corename != defcorename) 509 free(lim->pl_corename, M_TEMP); 510 pool_put(&plimit_pool, lim); 511 } 512