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