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
| 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 * $FreeBSD: head/sys/kern/kern_resource.c 103367 2002-09-15 23:52:25Z julian $
| 39 * $FreeBSD: head/sys/kern/kern_resource.c 103767 2002-09-21 22:07:17Z jake $
|
40 */ 41 42#include "opt_compat.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/sysproto.h> 47#include <sys/file.h> 48#include <sys/kernel.h> 49#include <sys/lock.h> 50#include <sys/malloc.h> 51#include <sys/mutex.h> 52#include <sys/proc.h> 53#include <sys/resourcevar.h> 54#include <sys/sx.h>
| 40 */ 41 42#include "opt_compat.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/sysproto.h> 47#include <sys/file.h> 48#include <sys/kernel.h> 49#include <sys/lock.h> 50#include <sys/malloc.h> 51#include <sys/mutex.h> 52#include <sys/proc.h> 53#include <sys/resourcevar.h> 54#include <sys/sx.h>
|
| 55#include <sys/sysent.h>
|
55#include <sys/time.h> 56 57#include <vm/vm.h> 58#include <vm/vm_param.h> 59#include <vm/pmap.h> 60#include <vm/vm_map.h> 61 62static int donice(struct thread *td, struct proc *chgp, int n); 63 64static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); 65#define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 66static struct mtx uihashtbl_mtx; 67static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 68static u_long uihash; /* size of hash table - 1 */ 69 70static struct uidinfo *uilookup(uid_t uid); 71 72/* 73 * Resource controls and accounting. 74 */ 75 76#ifndef _SYS_SYSPROTO_H_ 77struct getpriority_args { 78 int which; 79 int who; 80}; 81#endif 82/* 83 * MPSAFE 84 */ 85int 86getpriority(td, uap) 87 struct thread *td; 88 register struct getpriority_args *uap; 89{ 90 struct proc *p; 91 int low = PRIO_MAX + 1; 92 int error = 0; 93 struct ksegrp *kg; 94 95 mtx_lock(&Giant); 96 97 switch (uap->which) { 98 case PRIO_PROCESS: 99 if (uap->who == 0) 100 low = td->td_ksegrp->kg_nice; 101 else { 102 p = pfind(uap->who); 103 if (p == NULL) 104 break; 105 if (p_cansee(td, p) == 0) { 106 FOREACH_KSEGRP_IN_PROC(p, kg) { 107 if (kg->kg_nice < low) 108 low = kg->kg_nice; 109 } 110 } 111 PROC_UNLOCK(p); 112 } 113 break; 114 115 case PRIO_PGRP: { 116 register struct pgrp *pg; 117 118 sx_slock(&proctree_lock); 119 if (uap->who == 0) { 120 pg = td->td_proc->p_pgrp; 121 PGRP_LOCK(pg); 122 } else { 123 pg = pgfind(uap->who); 124 if (pg == NULL) { 125 sx_sunlock(&proctree_lock); 126 break; 127 } 128 } 129 sx_sunlock(&proctree_lock); 130 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 131 PROC_LOCK(p); 132 if (!p_cansee(td, p)) { 133 FOREACH_KSEGRP_IN_PROC(p, kg) { 134 if (kg->kg_nice < low) 135 low = kg->kg_nice; 136 } 137 } 138 PROC_UNLOCK(p); 139 } 140 PGRP_UNLOCK(pg); 141 break; 142 } 143 144 case PRIO_USER: 145 if (uap->who == 0) 146 uap->who = td->td_ucred->cr_uid; 147 sx_slock(&allproc_lock); 148 LIST_FOREACH(p, &allproc, p_list) { 149 PROC_LOCK(p); 150 if (!p_cansee(td, p) && 151 p->p_ucred->cr_uid == uap->who) { 152 FOREACH_KSEGRP_IN_PROC(p, kg) { 153 if (kg->kg_nice < low) 154 low = kg->kg_nice; 155 } 156 } 157 PROC_UNLOCK(p); 158 } 159 sx_sunlock(&allproc_lock); 160 break; 161 162 default: 163 error = EINVAL; 164 break; 165 } 166 if (low == PRIO_MAX + 1 && error == 0) 167 error = ESRCH; 168 td->td_retval[0] = low; 169 mtx_unlock(&Giant); 170 return (error); 171} 172 173#ifndef _SYS_SYSPROTO_H_ 174struct setpriority_args { 175 int which; 176 int who; 177 int prio; 178}; 179#endif 180/* 181 * MPSAFE 182 */ 183/* ARGSUSED */ 184int 185setpriority(td, uap) 186 struct thread *td; 187 register struct setpriority_args *uap; 188{ 189 struct proc *curp = td->td_proc; 190 register struct proc *p; 191 int found = 0, error = 0; 192 193 mtx_lock(&Giant); 194 195 switch (uap->which) { 196 case PRIO_PROCESS: 197 if (uap->who == 0) { 198 PROC_LOCK(curp); 199 error = donice(td, curp, uap->prio); 200 PROC_UNLOCK(curp); 201 } else { 202 p = pfind(uap->who); 203 if (p == 0) 204 break; 205 if (p_cansee(td, p) == 0) 206 error = donice(td, p, uap->prio); 207 PROC_UNLOCK(p); 208 } 209 found++; 210 break; 211 212 case PRIO_PGRP: { 213 register struct pgrp *pg; 214 215 sx_slock(&proctree_lock); 216 if (uap->who == 0) { 217 pg = curp->p_pgrp; 218 PGRP_LOCK(pg); 219 } else { 220 pg = pgfind(uap->who); 221 if (pg == NULL) { 222 sx_sunlock(&proctree_lock); 223 break; 224 } 225 } 226 sx_sunlock(&proctree_lock); 227 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 228 PROC_LOCK(p); 229 if (!p_cansee(td, p)) { 230 error = donice(td, p, uap->prio); 231 found++; 232 } 233 PROC_UNLOCK(p); 234 } 235 PGRP_UNLOCK(pg); 236 break; 237 } 238 239 case PRIO_USER: 240 if (uap->who == 0) 241 uap->who = td->td_ucred->cr_uid; 242 sx_slock(&allproc_lock); 243 FOREACH_PROC_IN_SYSTEM(p) { 244 PROC_LOCK(p); 245 if (p->p_ucred->cr_uid == uap->who && 246 !p_cansee(td, p)) { 247 error = donice(td, p, uap->prio); 248 found++; 249 } 250 PROC_UNLOCK(p); 251 } 252 sx_sunlock(&allproc_lock); 253 break; 254 255 default: 256 error = EINVAL; 257 break; 258 } 259 if (found == 0 && error == 0) 260 error = ESRCH; 261 mtx_unlock(&Giant); 262 return (error); 263} 264 265/* 266 * Set "nice" for a process. Doesn't really understand threaded processes well 267 * but does try. Has the unfortunate side effect of making all the NICE 268 * values for a process's ksegrps the same.. This suggests that 269 * NICE valuse should be stored as a process nice and deltas for the ksegrps. 270 * (but not yet). 271 */ 272static int 273donice(struct thread *td, struct proc *p, int n) 274{ 275 int error; 276 int low = PRIO_MAX + 1; 277 struct ksegrp *kg; 278 279 PROC_LOCK_ASSERT(p, MA_OWNED); 280 if ((error = p_cansched(td, p))) 281 return (error); 282 if (n > PRIO_MAX) 283 n = PRIO_MAX; 284 if (n < PRIO_MIN) 285 n = PRIO_MIN; 286 /* 287 * Only allow nicing if to more than the lowest nice. 288 * e.g. nices of 4,3,2 allow nice to 3 but not 1 289 */ 290 FOREACH_KSEGRP_IN_PROC(p, kg) { 291 if (kg->kg_nice < low) 292 low = kg->kg_nice; 293 } 294 if (n < low && suser(td)) 295 return (EACCES); 296 FOREACH_KSEGRP_IN_PROC(p, kg) { 297 kg->kg_nice = n; 298 (void)resetpriority(kg); 299 } 300 return (0); 301} 302 303/* rtprio system call */ 304#ifndef _SYS_SYSPROTO_H_ 305struct rtprio_args { 306 int function; 307 pid_t pid; 308 struct rtprio *rtp; 309}; 310#endif 311 312/* 313 * Set realtime priority 314 */ 315 316/* 317 * MPSAFE 318 */ 319/* ARGSUSED */ 320int 321rtprio(td, uap) 322 struct thread *td; 323 register struct rtprio_args *uap; 324{ 325 struct proc *curp = td->td_proc; 326 register struct proc *p; 327 struct rtprio rtp; 328 int error, cierror = 0; 329 330 /* Perform copyin before acquiring locks if needed. */ 331 if (uap->function == RTP_SET) 332 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); 333 334 if (uap->pid == 0) { 335 p = curp; 336 PROC_LOCK(p); 337 } else { 338 p = pfind(uap->pid); 339 if (p == NULL) 340 return (ESRCH); 341 } 342 343 switch (uap->function) { 344 case RTP_LOOKUP: 345 if ((error = p_cansee(td, p))) 346 break; 347 mtx_lock_spin(&sched_lock); 348 pri_to_rtp(FIRST_KSEGRP_IN_PROC(p), &rtp); 349 mtx_unlock_spin(&sched_lock); 350 PROC_UNLOCK(p); 351 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio))); 352 case RTP_SET: 353 if ((error = p_cansched(td, p)) || (error = cierror)) 354 break; 355 /* disallow setting rtprio in most cases if not superuser */ 356 if (suser(td) != 0) { 357 /* can't set someone else's */ 358 if (uap->pid) { 359 error = EPERM; 360 break; 361 } 362 /* can't set realtime priority */ 363/* 364 * Realtime priority has to be restricted for reasons which should be 365 * obvious. However, for idle priority, there is a potential for 366 * system deadlock if an idleprio process gains a lock on a resource 367 * that other processes need (and the idleprio process can't run 368 * due to a CPU-bound normal process). Fix me! XXX 369 */ 370#if 0 371 if (RTP_PRIO_IS_REALTIME(rtp.type)) 372#endif 373 if (rtp.type != RTP_PRIO_NORMAL) { 374 error = EPERM; 375 break; 376 } 377 } 378 mtx_lock_spin(&sched_lock); 379 error = rtp_to_pri(&rtp, FIRST_KSEGRP_IN_PROC(p)); 380 mtx_unlock_spin(&sched_lock); 381 break; 382 default: 383 error = EINVAL; 384 break; 385 } 386 PROC_UNLOCK(p); 387 return (error); 388} 389 390int 391rtp_to_pri(struct rtprio *rtp, struct ksegrp *kg) 392{ 393 394 if (rtp->prio > RTP_PRIO_MAX) 395 return (EINVAL); 396 switch (RTP_PRIO_BASE(rtp->type)) { 397 case RTP_PRIO_REALTIME: 398 kg->kg_user_pri = PRI_MIN_REALTIME + rtp->prio; 399 break; 400 case RTP_PRIO_NORMAL: 401 kg->kg_user_pri = PRI_MIN_TIMESHARE + rtp->prio; 402 break; 403 case RTP_PRIO_IDLE: 404 kg->kg_user_pri = PRI_MIN_IDLE + rtp->prio; 405 break; 406 default: 407 return (EINVAL); 408 } 409 kg->kg_pri_class = rtp->type; 410 if (curthread->td_ksegrp == kg) { 411 curthread->td_base_pri = kg->kg_user_pri; 412 curthread->td_priority = kg->kg_user_pri; /* XXX dubious */ 413 } 414 return (0); 415} 416 417void 418pri_to_rtp(struct ksegrp *kg, struct rtprio *rtp) 419{ 420 421 switch (PRI_BASE(kg->kg_pri_class)) { 422 case PRI_REALTIME: 423 rtp->prio = kg->kg_user_pri - PRI_MIN_REALTIME; 424 break; 425 case PRI_TIMESHARE: 426 rtp->prio = kg->kg_user_pri - PRI_MIN_TIMESHARE; 427 break; 428 case PRI_IDLE: 429 rtp->prio = kg->kg_user_pri - PRI_MIN_IDLE; 430 break; 431 default: 432 break; 433 } 434 rtp->type = kg->kg_pri_class; 435} 436 437#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 438#ifndef _SYS_SYSPROTO_H_ 439struct osetrlimit_args { 440 u_int which; 441 struct orlimit *rlp; 442}; 443#endif 444/* 445 * MPSAFE 446 */ 447/* ARGSUSED */ 448int 449osetrlimit(td, uap) 450 struct thread *td; 451 register struct osetrlimit_args *uap; 452{ 453 struct orlimit olim; 454 struct rlimit lim; 455 int error; 456 457 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit)))) 458 return (error); 459 lim.rlim_cur = olim.rlim_cur; 460 lim.rlim_max = olim.rlim_max; 461 mtx_lock(&Giant); 462 error = dosetrlimit(td, uap->which, &lim); 463 mtx_unlock(&Giant); 464 return (error); 465} 466 467#ifndef _SYS_SYSPROTO_H_ 468struct ogetrlimit_args { 469 u_int which; 470 struct orlimit *rlp; 471}; 472#endif 473/* 474 * MPSAFE 475 */ 476/* ARGSUSED */ 477int 478ogetrlimit(td, uap) 479 struct thread *td; 480 register struct ogetrlimit_args *uap; 481{ 482 struct proc *p = td->td_proc; 483 struct orlimit olim; 484 int error; 485 486 if (uap->which >= RLIM_NLIMITS) 487 return (EINVAL); 488 mtx_lock(&Giant); 489 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur; 490 if (olim.rlim_cur == -1) 491 olim.rlim_cur = 0x7fffffff; 492 olim.rlim_max = p->p_rlimit[uap->which].rlim_max; 493 if (olim.rlim_max == -1) 494 olim.rlim_max = 0x7fffffff; 495 error = copyout(&olim, uap->rlp, sizeof(olim)); 496 mtx_unlock(&Giant); 497 return (error); 498} 499#endif /* COMPAT_43 || COMPAT_SUNOS */ 500 501#ifndef _SYS_SYSPROTO_H_ 502struct __setrlimit_args { 503 u_int which; 504 struct rlimit *rlp; 505}; 506#endif 507/* 508 * MPSAFE 509 */ 510/* ARGSUSED */ 511int 512setrlimit(td, uap) 513 struct thread *td; 514 register struct __setrlimit_args *uap; 515{ 516 struct rlimit alim; 517 int error; 518 519 if ((error = copyin(uap->rlp, &alim, sizeof (struct rlimit)))) 520 return (error); 521 mtx_lock(&Giant); 522 error = dosetrlimit(td, uap->which, &alim); 523 mtx_unlock(&Giant); 524 return (error); 525} 526 527int 528dosetrlimit(td, which, limp) 529 struct thread *td; 530 u_int which; 531 struct rlimit *limp; 532{ 533 struct proc *p = td->td_proc; 534 register struct rlimit *alimp; 535 int error; 536 537 GIANT_REQUIRED; 538 539 if (which >= RLIM_NLIMITS) 540 return (EINVAL); 541 alimp = &p->p_rlimit[which]; 542 543 /* 544 * Preserve historical bugs by treating negative limits as unsigned. 545 */ 546 if (limp->rlim_cur < 0) 547 limp->rlim_cur = RLIM_INFINITY; 548 if (limp->rlim_max < 0) 549 limp->rlim_max = RLIM_INFINITY; 550 551 if (limp->rlim_cur > alimp->rlim_max || 552 limp->rlim_max > alimp->rlim_max) 553 if ((error = suser_cred(td->td_ucred, PRISON_ROOT))) 554 return (error); 555 if (limp->rlim_cur > limp->rlim_max) 556 limp->rlim_cur = limp->rlim_max; 557 if (p->p_limit->p_refcnt > 1 && 558 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 559 p->p_limit->p_refcnt--; 560 p->p_limit = limcopy(p->p_limit); 561 alimp = &p->p_rlimit[which]; 562 } 563 564 switch (which) { 565 566 case RLIMIT_CPU: 567 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000) 568 p->p_limit->p_cpulimit = RLIM_INFINITY; 569 else 570 p->p_limit->p_cpulimit = 571 (rlim_t)1000000 * limp->rlim_cur; 572 break; 573 case RLIMIT_DATA: 574 if (limp->rlim_cur > maxdsiz) 575 limp->rlim_cur = maxdsiz; 576 if (limp->rlim_max > maxdsiz) 577 limp->rlim_max = maxdsiz; 578 break; 579 580 case RLIMIT_STACK: 581 if (limp->rlim_cur > maxssiz) 582 limp->rlim_cur = maxssiz; 583 if (limp->rlim_max > maxssiz) 584 limp->rlim_max = maxssiz; 585 /* 586 * Stack is allocated to the max at exec time with only 587 * "rlim_cur" bytes accessible. If stack limit is going 588 * up make more accessible, if going down make inaccessible. 589 */ 590 if (limp->rlim_cur != alimp->rlim_cur) { 591 vm_offset_t addr; 592 vm_size_t size; 593 vm_prot_t prot; 594 595 if (limp->rlim_cur > alimp->rlim_cur) {
| 56#include <sys/time.h> 57 58#include <vm/vm.h> 59#include <vm/vm_param.h> 60#include <vm/pmap.h> 61#include <vm/vm_map.h> 62 63static int donice(struct thread *td, struct proc *chgp, int n); 64 65static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); 66#define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 67static struct mtx uihashtbl_mtx; 68static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 69static u_long uihash; /* size of hash table - 1 */ 70 71static struct uidinfo *uilookup(uid_t uid); 72 73/* 74 * Resource controls and accounting. 75 */ 76 77#ifndef _SYS_SYSPROTO_H_ 78struct getpriority_args { 79 int which; 80 int who; 81}; 82#endif 83/* 84 * MPSAFE 85 */ 86int 87getpriority(td, uap) 88 struct thread *td; 89 register struct getpriority_args *uap; 90{ 91 struct proc *p; 92 int low = PRIO_MAX + 1; 93 int error = 0; 94 struct ksegrp *kg; 95 96 mtx_lock(&Giant); 97 98 switch (uap->which) { 99 case PRIO_PROCESS: 100 if (uap->who == 0) 101 low = td->td_ksegrp->kg_nice; 102 else { 103 p = pfind(uap->who); 104 if (p == NULL) 105 break; 106 if (p_cansee(td, p) == 0) { 107 FOREACH_KSEGRP_IN_PROC(p, kg) { 108 if (kg->kg_nice < low) 109 low = kg->kg_nice; 110 } 111 } 112 PROC_UNLOCK(p); 113 } 114 break; 115 116 case PRIO_PGRP: { 117 register struct pgrp *pg; 118 119 sx_slock(&proctree_lock); 120 if (uap->who == 0) { 121 pg = td->td_proc->p_pgrp; 122 PGRP_LOCK(pg); 123 } else { 124 pg = pgfind(uap->who); 125 if (pg == NULL) { 126 sx_sunlock(&proctree_lock); 127 break; 128 } 129 } 130 sx_sunlock(&proctree_lock); 131 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 132 PROC_LOCK(p); 133 if (!p_cansee(td, p)) { 134 FOREACH_KSEGRP_IN_PROC(p, kg) { 135 if (kg->kg_nice < low) 136 low = kg->kg_nice; 137 } 138 } 139 PROC_UNLOCK(p); 140 } 141 PGRP_UNLOCK(pg); 142 break; 143 } 144 145 case PRIO_USER: 146 if (uap->who == 0) 147 uap->who = td->td_ucred->cr_uid; 148 sx_slock(&allproc_lock); 149 LIST_FOREACH(p, &allproc, p_list) { 150 PROC_LOCK(p); 151 if (!p_cansee(td, p) && 152 p->p_ucred->cr_uid == uap->who) { 153 FOREACH_KSEGRP_IN_PROC(p, kg) { 154 if (kg->kg_nice < low) 155 low = kg->kg_nice; 156 } 157 } 158 PROC_UNLOCK(p); 159 } 160 sx_sunlock(&allproc_lock); 161 break; 162 163 default: 164 error = EINVAL; 165 break; 166 } 167 if (low == PRIO_MAX + 1 && error == 0) 168 error = ESRCH; 169 td->td_retval[0] = low; 170 mtx_unlock(&Giant); 171 return (error); 172} 173 174#ifndef _SYS_SYSPROTO_H_ 175struct setpriority_args { 176 int which; 177 int who; 178 int prio; 179}; 180#endif 181/* 182 * MPSAFE 183 */ 184/* ARGSUSED */ 185int 186setpriority(td, uap) 187 struct thread *td; 188 register struct setpriority_args *uap; 189{ 190 struct proc *curp = td->td_proc; 191 register struct proc *p; 192 int found = 0, error = 0; 193 194 mtx_lock(&Giant); 195 196 switch (uap->which) { 197 case PRIO_PROCESS: 198 if (uap->who == 0) { 199 PROC_LOCK(curp); 200 error = donice(td, curp, uap->prio); 201 PROC_UNLOCK(curp); 202 } else { 203 p = pfind(uap->who); 204 if (p == 0) 205 break; 206 if (p_cansee(td, p) == 0) 207 error = donice(td, p, uap->prio); 208 PROC_UNLOCK(p); 209 } 210 found++; 211 break; 212 213 case PRIO_PGRP: { 214 register struct pgrp *pg; 215 216 sx_slock(&proctree_lock); 217 if (uap->who == 0) { 218 pg = curp->p_pgrp; 219 PGRP_LOCK(pg); 220 } else { 221 pg = pgfind(uap->who); 222 if (pg == NULL) { 223 sx_sunlock(&proctree_lock); 224 break; 225 } 226 } 227 sx_sunlock(&proctree_lock); 228 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 229 PROC_LOCK(p); 230 if (!p_cansee(td, p)) { 231 error = donice(td, p, uap->prio); 232 found++; 233 } 234 PROC_UNLOCK(p); 235 } 236 PGRP_UNLOCK(pg); 237 break; 238 } 239 240 case PRIO_USER: 241 if (uap->who == 0) 242 uap->who = td->td_ucred->cr_uid; 243 sx_slock(&allproc_lock); 244 FOREACH_PROC_IN_SYSTEM(p) { 245 PROC_LOCK(p); 246 if (p->p_ucred->cr_uid == uap->who && 247 !p_cansee(td, p)) { 248 error = donice(td, p, uap->prio); 249 found++; 250 } 251 PROC_UNLOCK(p); 252 } 253 sx_sunlock(&allproc_lock); 254 break; 255 256 default: 257 error = EINVAL; 258 break; 259 } 260 if (found == 0 && error == 0) 261 error = ESRCH; 262 mtx_unlock(&Giant); 263 return (error); 264} 265 266/* 267 * Set "nice" for a process. Doesn't really understand threaded processes well 268 * but does try. Has the unfortunate side effect of making all the NICE 269 * values for a process's ksegrps the same.. This suggests that 270 * NICE valuse should be stored as a process nice and deltas for the ksegrps. 271 * (but not yet). 272 */ 273static int 274donice(struct thread *td, struct proc *p, int n) 275{ 276 int error; 277 int low = PRIO_MAX + 1; 278 struct ksegrp *kg; 279 280 PROC_LOCK_ASSERT(p, MA_OWNED); 281 if ((error = p_cansched(td, p))) 282 return (error); 283 if (n > PRIO_MAX) 284 n = PRIO_MAX; 285 if (n < PRIO_MIN) 286 n = PRIO_MIN; 287 /* 288 * Only allow nicing if to more than the lowest nice. 289 * e.g. nices of 4,3,2 allow nice to 3 but not 1 290 */ 291 FOREACH_KSEGRP_IN_PROC(p, kg) { 292 if (kg->kg_nice < low) 293 low = kg->kg_nice; 294 } 295 if (n < low && suser(td)) 296 return (EACCES); 297 FOREACH_KSEGRP_IN_PROC(p, kg) { 298 kg->kg_nice = n; 299 (void)resetpriority(kg); 300 } 301 return (0); 302} 303 304/* rtprio system call */ 305#ifndef _SYS_SYSPROTO_H_ 306struct rtprio_args { 307 int function; 308 pid_t pid; 309 struct rtprio *rtp; 310}; 311#endif 312 313/* 314 * Set realtime priority 315 */ 316 317/* 318 * MPSAFE 319 */ 320/* ARGSUSED */ 321int 322rtprio(td, uap) 323 struct thread *td; 324 register struct rtprio_args *uap; 325{ 326 struct proc *curp = td->td_proc; 327 register struct proc *p; 328 struct rtprio rtp; 329 int error, cierror = 0; 330 331 /* Perform copyin before acquiring locks if needed. */ 332 if (uap->function == RTP_SET) 333 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); 334 335 if (uap->pid == 0) { 336 p = curp; 337 PROC_LOCK(p); 338 } else { 339 p = pfind(uap->pid); 340 if (p == NULL) 341 return (ESRCH); 342 } 343 344 switch (uap->function) { 345 case RTP_LOOKUP: 346 if ((error = p_cansee(td, p))) 347 break; 348 mtx_lock_spin(&sched_lock); 349 pri_to_rtp(FIRST_KSEGRP_IN_PROC(p), &rtp); 350 mtx_unlock_spin(&sched_lock); 351 PROC_UNLOCK(p); 352 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio))); 353 case RTP_SET: 354 if ((error = p_cansched(td, p)) || (error = cierror)) 355 break; 356 /* disallow setting rtprio in most cases if not superuser */ 357 if (suser(td) != 0) { 358 /* can't set someone else's */ 359 if (uap->pid) { 360 error = EPERM; 361 break; 362 } 363 /* can't set realtime priority */ 364/* 365 * Realtime priority has to be restricted for reasons which should be 366 * obvious. However, for idle priority, there is a potential for 367 * system deadlock if an idleprio process gains a lock on a resource 368 * that other processes need (and the idleprio process can't run 369 * due to a CPU-bound normal process). Fix me! XXX 370 */ 371#if 0 372 if (RTP_PRIO_IS_REALTIME(rtp.type)) 373#endif 374 if (rtp.type != RTP_PRIO_NORMAL) { 375 error = EPERM; 376 break; 377 } 378 } 379 mtx_lock_spin(&sched_lock); 380 error = rtp_to_pri(&rtp, FIRST_KSEGRP_IN_PROC(p)); 381 mtx_unlock_spin(&sched_lock); 382 break; 383 default: 384 error = EINVAL; 385 break; 386 } 387 PROC_UNLOCK(p); 388 return (error); 389} 390 391int 392rtp_to_pri(struct rtprio *rtp, struct ksegrp *kg) 393{ 394 395 if (rtp->prio > RTP_PRIO_MAX) 396 return (EINVAL); 397 switch (RTP_PRIO_BASE(rtp->type)) { 398 case RTP_PRIO_REALTIME: 399 kg->kg_user_pri = PRI_MIN_REALTIME + rtp->prio; 400 break; 401 case RTP_PRIO_NORMAL: 402 kg->kg_user_pri = PRI_MIN_TIMESHARE + rtp->prio; 403 break; 404 case RTP_PRIO_IDLE: 405 kg->kg_user_pri = PRI_MIN_IDLE + rtp->prio; 406 break; 407 default: 408 return (EINVAL); 409 } 410 kg->kg_pri_class = rtp->type; 411 if (curthread->td_ksegrp == kg) { 412 curthread->td_base_pri = kg->kg_user_pri; 413 curthread->td_priority = kg->kg_user_pri; /* XXX dubious */ 414 } 415 return (0); 416} 417 418void 419pri_to_rtp(struct ksegrp *kg, struct rtprio *rtp) 420{ 421 422 switch (PRI_BASE(kg->kg_pri_class)) { 423 case PRI_REALTIME: 424 rtp->prio = kg->kg_user_pri - PRI_MIN_REALTIME; 425 break; 426 case PRI_TIMESHARE: 427 rtp->prio = kg->kg_user_pri - PRI_MIN_TIMESHARE; 428 break; 429 case PRI_IDLE: 430 rtp->prio = kg->kg_user_pri - PRI_MIN_IDLE; 431 break; 432 default: 433 break; 434 } 435 rtp->type = kg->kg_pri_class; 436} 437 438#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 439#ifndef _SYS_SYSPROTO_H_ 440struct osetrlimit_args { 441 u_int which; 442 struct orlimit *rlp; 443}; 444#endif 445/* 446 * MPSAFE 447 */ 448/* ARGSUSED */ 449int 450osetrlimit(td, uap) 451 struct thread *td; 452 register struct osetrlimit_args *uap; 453{ 454 struct orlimit olim; 455 struct rlimit lim; 456 int error; 457 458 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit)))) 459 return (error); 460 lim.rlim_cur = olim.rlim_cur; 461 lim.rlim_max = olim.rlim_max; 462 mtx_lock(&Giant); 463 error = dosetrlimit(td, uap->which, &lim); 464 mtx_unlock(&Giant); 465 return (error); 466} 467 468#ifndef _SYS_SYSPROTO_H_ 469struct ogetrlimit_args { 470 u_int which; 471 struct orlimit *rlp; 472}; 473#endif 474/* 475 * MPSAFE 476 */ 477/* ARGSUSED */ 478int 479ogetrlimit(td, uap) 480 struct thread *td; 481 register struct ogetrlimit_args *uap; 482{ 483 struct proc *p = td->td_proc; 484 struct orlimit olim; 485 int error; 486 487 if (uap->which >= RLIM_NLIMITS) 488 return (EINVAL); 489 mtx_lock(&Giant); 490 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur; 491 if (olim.rlim_cur == -1) 492 olim.rlim_cur = 0x7fffffff; 493 olim.rlim_max = p->p_rlimit[uap->which].rlim_max; 494 if (olim.rlim_max == -1) 495 olim.rlim_max = 0x7fffffff; 496 error = copyout(&olim, uap->rlp, sizeof(olim)); 497 mtx_unlock(&Giant); 498 return (error); 499} 500#endif /* COMPAT_43 || COMPAT_SUNOS */ 501 502#ifndef _SYS_SYSPROTO_H_ 503struct __setrlimit_args { 504 u_int which; 505 struct rlimit *rlp; 506}; 507#endif 508/* 509 * MPSAFE 510 */ 511/* ARGSUSED */ 512int 513setrlimit(td, uap) 514 struct thread *td; 515 register struct __setrlimit_args *uap; 516{ 517 struct rlimit alim; 518 int error; 519 520 if ((error = copyin(uap->rlp, &alim, sizeof (struct rlimit)))) 521 return (error); 522 mtx_lock(&Giant); 523 error = dosetrlimit(td, uap->which, &alim); 524 mtx_unlock(&Giant); 525 return (error); 526} 527 528int 529dosetrlimit(td, which, limp) 530 struct thread *td; 531 u_int which; 532 struct rlimit *limp; 533{ 534 struct proc *p = td->td_proc; 535 register struct rlimit *alimp; 536 int error; 537 538 GIANT_REQUIRED; 539 540 if (which >= RLIM_NLIMITS) 541 return (EINVAL); 542 alimp = &p->p_rlimit[which]; 543 544 /* 545 * Preserve historical bugs by treating negative limits as unsigned. 546 */ 547 if (limp->rlim_cur < 0) 548 limp->rlim_cur = RLIM_INFINITY; 549 if (limp->rlim_max < 0) 550 limp->rlim_max = RLIM_INFINITY; 551 552 if (limp->rlim_cur > alimp->rlim_max || 553 limp->rlim_max > alimp->rlim_max) 554 if ((error = suser_cred(td->td_ucred, PRISON_ROOT))) 555 return (error); 556 if (limp->rlim_cur > limp->rlim_max) 557 limp->rlim_cur = limp->rlim_max; 558 if (p->p_limit->p_refcnt > 1 && 559 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 560 p->p_limit->p_refcnt--; 561 p->p_limit = limcopy(p->p_limit); 562 alimp = &p->p_rlimit[which]; 563 } 564 565 switch (which) { 566 567 case RLIMIT_CPU: 568 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000) 569 p->p_limit->p_cpulimit = RLIM_INFINITY; 570 else 571 p->p_limit->p_cpulimit = 572 (rlim_t)1000000 * limp->rlim_cur; 573 break; 574 case RLIMIT_DATA: 575 if (limp->rlim_cur > maxdsiz) 576 limp->rlim_cur = maxdsiz; 577 if (limp->rlim_max > maxdsiz) 578 limp->rlim_max = maxdsiz; 579 break; 580 581 case RLIMIT_STACK: 582 if (limp->rlim_cur > maxssiz) 583 limp->rlim_cur = maxssiz; 584 if (limp->rlim_max > maxssiz) 585 limp->rlim_max = maxssiz; 586 /* 587 * Stack is allocated to the max at exec time with only 588 * "rlim_cur" bytes accessible. If stack limit is going 589 * up make more accessible, if going down make inaccessible. 590 */ 591 if (limp->rlim_cur != alimp->rlim_cur) { 592 vm_offset_t addr; 593 vm_size_t size; 594 vm_prot_t prot; 595 596 if (limp->rlim_cur > alimp->rlim_cur) {
|
596 prot = VM_PROT_ALL;
| 597 prot = p->p_sysent->sv_stackprot;
|
597 size = limp->rlim_cur - alimp->rlim_cur;
| 598 size = limp->rlim_cur - alimp->rlim_cur;
|
598 addr = USRSTACK - limp->rlim_cur;
| 599 addr = p->p_sysent->sv_usrstack - 600 limp->rlim_cur;
|
599 } else { 600 prot = VM_PROT_NONE; 601 size = alimp->rlim_cur - limp->rlim_cur;
| 601 } else { 602 prot = VM_PROT_NONE; 603 size = alimp->rlim_cur - limp->rlim_cur;
|
602 addr = USRSTACK - alimp->rlim_cur;
| 604 addr = p->p_sysent->sv_usrstack - 605 alimp->rlim_cur;
|
603 } 604 addr = trunc_page(addr); 605 size = round_page(size); 606 (void) vm_map_protect(&p->p_vmspace->vm_map, 607 addr, addr+size, prot, FALSE); 608 } 609 break; 610 611 case RLIMIT_NOFILE: 612 if (limp->rlim_cur > maxfilesperproc) 613 limp->rlim_cur = maxfilesperproc; 614 if (limp->rlim_max > maxfilesperproc) 615 limp->rlim_max = maxfilesperproc; 616 break; 617 618 case RLIMIT_NPROC: 619 if (limp->rlim_cur > maxprocperuid) 620 limp->rlim_cur = maxprocperuid; 621 if (limp->rlim_max > maxprocperuid) 622 limp->rlim_max = maxprocperuid; 623 if (limp->rlim_cur < 1) 624 limp->rlim_cur = 1; 625 if (limp->rlim_max < 1) 626 limp->rlim_max = 1; 627 break; 628 } 629 *alimp = *limp; 630 return (0); 631} 632 633#ifndef _SYS_SYSPROTO_H_ 634struct __getrlimit_args { 635 u_int which; 636 struct rlimit *rlp; 637}; 638#endif 639/* 640 * MPSAFE 641 */ 642/* ARGSUSED */ 643int 644getrlimit(td, uap) 645 struct thread *td; 646 register struct __getrlimit_args *uap; 647{ 648 int error; 649 struct proc *p = td->td_proc; 650 651 if (uap->which >= RLIM_NLIMITS) 652 return (EINVAL); 653 mtx_lock(&Giant); 654 error = copyout(&p->p_rlimit[uap->which], uap->rlp, 655 sizeof (struct rlimit)); 656 mtx_unlock(&Giant); 657 return(error); 658} 659 660/* 661 * Transform the running time and tick information in proc p into user, 662 * system, and interrupt time usage. 663 */ 664void 665calcru(p, up, sp, ip) 666 struct proc *p; 667 struct timeval *up; 668 struct timeval *sp; 669 struct timeval *ip; 670{ 671 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ 672 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; 673 u_int64_t uut = 0, sut = 0, iut = 0; 674 int s; 675 struct timeval tv; 676 struct bintime bt; 677 struct kse *ke; 678 struct ksegrp *kg; 679 680 mtx_assert(&sched_lock, MA_OWNED); 681 /* XXX: why spl-protect ? worst case is an off-by-one report */ 682 683 FOREACH_KSEGRP_IN_PROC(p, kg) { 684 /* we could accumulate per ksegrp and per process here*/ 685 FOREACH_KSE_IN_GROUP(kg, ke) { 686 s = splstatclock(); 687 ut = ke->ke_uticks; 688 st = ke->ke_sticks; 689 it = ke->ke_iticks; 690 splx(s); 691 692 tt = ut + st + it; 693 if (tt == 0) { 694 st = 1; 695 tt = 1; 696 } 697 698 if (ke == curthread->td_kse) { 699 /* 700 * Adjust for the current time slice. This is actually fairly 701 * important since the error here is on the order of a time 702 * quantum, which is much greater than the sampling error. 703 * XXXKSE use a different test due to threads on other 704 * processors also being 'current'. 705 */ 706 707 binuptime(&bt); 708 bintime_sub(&bt, PCPU_PTR(switchtime)); 709 bintime_add(&bt, &p->p_runtime); 710 } else { 711 bt = p->p_runtime; 712 } 713 bintime2timeval(&bt, &tv); 714 tu = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; 715 ptu = ke->ke_uu + ke->ke_su + ke->ke_iu; 716 if (tu < ptu || (int64_t)tu < 0) { 717 /* XXX no %qd in kernel. Truncate. */ 718 printf("calcru: negative time of %ld usec for pid %d (%s)\n", 719 (long)tu, p->p_pid, p->p_comm); 720 tu = ptu; 721 } 722 723 /* Subdivide tu. */ 724 uu = (tu * ut) / tt; 725 su = (tu * st) / tt; 726 iu = tu - uu - su; 727 728 /* Enforce monotonicity. */ 729 if (uu < ke->ke_uu || su < ke->ke_su || iu < ke->ke_iu) { 730 if (uu < ke->ke_uu) 731 uu = ke->ke_uu; 732 else if (uu + ke->ke_su + ke->ke_iu > tu) 733 uu = tu - ke->ke_su - ke->ke_iu; 734 if (st == 0) 735 su = ke->ke_su; 736 else { 737 su = ((tu - uu) * st) / (st + it); 738 if (su < ke->ke_su) 739 su = ke->ke_su; 740 else if (uu + su + ke->ke_iu > tu) 741 su = tu - uu - ke->ke_iu; 742 } 743 KASSERT(uu + su + ke->ke_iu <= tu, 744 ("calcru: monotonisation botch 1")); 745 iu = tu - uu - su; 746 KASSERT(iu >= ke->ke_iu, 747 ("calcru: monotonisation botch 2")); 748 } 749 ke->ke_uu = uu; 750 ke->ke_su = su; 751 ke->ke_iu = iu; 752 uut += uu; 753 sut += su; 754 iut += iu; 755 756 } /* end kse loop */ 757 } /* end kseg loop */ 758 up->tv_sec = uut / 1000000; 759 up->tv_usec = uut % 1000000; 760 sp->tv_sec = sut / 1000000; 761 sp->tv_usec = sut % 1000000; 762 if (ip != NULL) { 763 ip->tv_sec = iut / 1000000; 764 ip->tv_usec = iut % 1000000; 765 } 766} 767 768#ifndef _SYS_SYSPROTO_H_ 769struct getrusage_args { 770 int who; 771 struct rusage *rusage; 772}; 773#endif 774/* 775 * MPSAFE 776 */ 777/* ARGSUSED */ 778int 779getrusage(td, uap) 780 register struct thread *td; 781 register struct getrusage_args *uap; 782{ 783 struct proc *p = td->td_proc; 784 register struct rusage *rup; 785 int error = 0; 786 787 mtx_lock(&Giant); 788 789 switch (uap->who) { 790 case RUSAGE_SELF: 791 rup = &p->p_stats->p_ru; 792 mtx_lock_spin(&sched_lock); 793 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 794 mtx_unlock_spin(&sched_lock); 795 break; 796 797 case RUSAGE_CHILDREN: 798 rup = &p->p_stats->p_cru; 799 break; 800 801 default: 802 rup = NULL; 803 error = EINVAL; 804 break; 805 } 806 mtx_unlock(&Giant); 807 if (error == 0) { 808 error = copyout(rup, uap->rusage, sizeof (struct rusage)); 809 } 810 return(error); 811} 812 813void 814ruadd(ru, ru2) 815 register struct rusage *ru, *ru2; 816{ 817 register long *ip, *ip2; 818 register int i; 819 820 timevaladd(&ru->ru_utime, &ru2->ru_utime); 821 timevaladd(&ru->ru_stime, &ru2->ru_stime); 822 if (ru->ru_maxrss < ru2->ru_maxrss) 823 ru->ru_maxrss = ru2->ru_maxrss; 824 ip = &ru->ru_first; ip2 = &ru2->ru_first; 825 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 826 *ip++ += *ip2++; 827} 828 829/* 830 * Make a copy of the plimit structure. 831 * We share these structures copy-on-write after fork, 832 * and copy when a limit is changed. 833 */ 834struct plimit * 835limcopy(lim) 836 struct plimit *lim; 837{ 838 register struct plimit *copy; 839 840 MALLOC(copy, struct plimit *, sizeof(struct plimit), 841 M_SUBPROC, M_WAITOK); 842 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit)); 843 copy->p_lflags = 0; 844 copy->p_refcnt = 1; 845 return (copy); 846} 847 848/* 849 * Find the uidinfo structure for a uid. This structure is used to 850 * track the total resource consumption (process count, socket buffer 851 * size, etc.) for the uid and impose limits. 852 */ 853void 854uihashinit() 855{ 856 857 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash); 858 mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF); 859} 860 861/* 862 * lookup a uidinfo struct for the parameter uid. 863 * uihashtbl_mtx must be locked. 864 */ 865static struct uidinfo * 866uilookup(uid) 867 uid_t uid; 868{ 869 struct uihashhead *uipp; 870 struct uidinfo *uip; 871 872 mtx_assert(&uihashtbl_mtx, MA_OWNED); 873 uipp = UIHASH(uid); 874 LIST_FOREACH(uip, uipp, ui_hash) 875 if (uip->ui_uid == uid) 876 break; 877 878 return (uip); 879} 880 881/* 882 * Find or allocate a struct uidinfo for a particular uid. 883 * Increase refcount on uidinfo struct returned. 884 * uifree() should be called on a struct uidinfo when released. 885 */ 886struct uidinfo * 887uifind(uid) 888 uid_t uid; 889{ 890 struct uidinfo *uip; 891 892 mtx_lock(&uihashtbl_mtx); 893 uip = uilookup(uid); 894 if (uip == NULL) { 895 struct uidinfo *old_uip; 896 897 mtx_unlock(&uihashtbl_mtx); 898 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO); 899 mtx_lock(&uihashtbl_mtx); 900 /* 901 * There's a chance someone created our uidinfo while we 902 * were in malloc and not holding the lock, so we have to 903 * make sure we don't insert a duplicate uidinfo 904 */ 905 if ((old_uip = uilookup(uid)) != NULL) { 906 /* someone else beat us to it */ 907 free(uip, M_UIDINFO); 908 uip = old_uip; 909 } else { 910 uip->ui_mtxp = mtx_pool_alloc(); 911 uip->ui_uid = uid; 912 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash); 913 } 914 } 915 uihold(uip); 916 mtx_unlock(&uihashtbl_mtx); 917 return (uip); 918} 919 920/* 921 * Place another refcount on a uidinfo struct. 922 */ 923void 924uihold(uip) 925 struct uidinfo *uip; 926{ 927 928 UIDINFO_LOCK(uip); 929 uip->ui_ref++; 930 UIDINFO_UNLOCK(uip); 931} 932 933/*- 934 * Since uidinfo structs have a long lifetime, we use an 935 * opportunistic refcounting scheme to avoid locking the lookup hash 936 * for each release. 937 * 938 * If the refcount hits 0, we need to free the structure, 939 * which means we need to lock the hash. 940 * Optimal case: 941 * After locking the struct and lowering the refcount, if we find 942 * that we don't need to free, simply unlock and return. 943 * Suboptimal case: 944 * If refcount lowering results in need to free, bump the count 945 * back up, loose the lock and aquire the locks in the proper 946 * order to try again. 947 */ 948void 949uifree(uip) 950 struct uidinfo *uip; 951{ 952 953 /* Prepare for optimal case. */ 954 UIDINFO_LOCK(uip); 955 956 if (--uip->ui_ref != 0) { 957 UIDINFO_UNLOCK(uip); 958 return; 959 } 960 961 /* Prepare for suboptimal case. */ 962 uip->ui_ref++; 963 UIDINFO_UNLOCK(uip); 964 mtx_lock(&uihashtbl_mtx); 965 UIDINFO_LOCK(uip); 966 967 /* 968 * We must subtract one from the count again because we backed out 969 * our initial subtraction before dropping the lock. 970 * Since another thread may have added a reference after we dropped the 971 * initial lock we have to test for zero again. 972 */ 973 if (--uip->ui_ref == 0) { 974 LIST_REMOVE(uip, ui_hash); 975 mtx_unlock(&uihashtbl_mtx); 976 if (uip->ui_sbsize != 0) 977 /* XXX no %qd in kernel. Truncate. */ 978 printf("freeing uidinfo: uid = %d, sbsize = %ld\n", 979 uip->ui_uid, (long)uip->ui_sbsize); 980 if (uip->ui_proccnt != 0) 981 printf("freeing uidinfo: uid = %d, proccnt = %ld\n", 982 uip->ui_uid, uip->ui_proccnt); 983 UIDINFO_UNLOCK(uip); 984 FREE(uip, M_UIDINFO); 985 return; 986 } 987 988 mtx_unlock(&uihashtbl_mtx); 989 UIDINFO_UNLOCK(uip); 990} 991 992/* 993 * Change the count associated with number of processes 994 * a given user is using. When 'max' is 0, don't enforce a limit 995 */ 996int 997chgproccnt(uip, diff, max) 998 struct uidinfo *uip; 999 int diff; 1000 int max; 1001{ 1002 1003 UIDINFO_LOCK(uip); 1004 /* don't allow them to exceed max, but allow subtraction */ 1005 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) { 1006 UIDINFO_UNLOCK(uip); 1007 return (0); 1008 } 1009 uip->ui_proccnt += diff; 1010 if (uip->ui_proccnt < 0) 1011 printf("negative proccnt for uid = %d\n", uip->ui_uid); 1012 UIDINFO_UNLOCK(uip); 1013 return (1); 1014} 1015 1016/* 1017 * Change the total socket buffer size a user has used. 1018 */ 1019int 1020chgsbsize(uip, hiwat, to, max) 1021 struct uidinfo *uip; 1022 u_int *hiwat; 1023 u_int to; 1024 rlim_t max; 1025{ 1026 rlim_t new; 1027 int s; 1028 1029 s = splnet(); 1030 UIDINFO_LOCK(uip); 1031 new = uip->ui_sbsize + to - *hiwat; 1032 /* don't allow them to exceed max, but allow subtraction */ 1033 if (to > *hiwat && new > max) { 1034 splx(s); 1035 UIDINFO_UNLOCK(uip); 1036 return (0); 1037 } 1038 uip->ui_sbsize = new; 1039 *hiwat = to; 1040 if (uip->ui_sbsize < 0) 1041 printf("negative sbsize for uid = %d\n", uip->ui_uid); 1042 splx(s); 1043 UIDINFO_UNLOCK(uip); 1044 return (1); 1045}
| 606 } 607 addr = trunc_page(addr); 608 size = round_page(size); 609 (void) vm_map_protect(&p->p_vmspace->vm_map, 610 addr, addr+size, prot, FALSE); 611 } 612 break; 613 614 case RLIMIT_NOFILE: 615 if (limp->rlim_cur > maxfilesperproc) 616 limp->rlim_cur = maxfilesperproc; 617 if (limp->rlim_max > maxfilesperproc) 618 limp->rlim_max = maxfilesperproc; 619 break; 620 621 case RLIMIT_NPROC: 622 if (limp->rlim_cur > maxprocperuid) 623 limp->rlim_cur = maxprocperuid; 624 if (limp->rlim_max > maxprocperuid) 625 limp->rlim_max = maxprocperuid; 626 if (limp->rlim_cur < 1) 627 limp->rlim_cur = 1; 628 if (limp->rlim_max < 1) 629 limp->rlim_max = 1; 630 break; 631 } 632 *alimp = *limp; 633 return (0); 634} 635 636#ifndef _SYS_SYSPROTO_H_ 637struct __getrlimit_args { 638 u_int which; 639 struct rlimit *rlp; 640}; 641#endif 642/* 643 * MPSAFE 644 */ 645/* ARGSUSED */ 646int 647getrlimit(td, uap) 648 struct thread *td; 649 register struct __getrlimit_args *uap; 650{ 651 int error; 652 struct proc *p = td->td_proc; 653 654 if (uap->which >= RLIM_NLIMITS) 655 return (EINVAL); 656 mtx_lock(&Giant); 657 error = copyout(&p->p_rlimit[uap->which], uap->rlp, 658 sizeof (struct rlimit)); 659 mtx_unlock(&Giant); 660 return(error); 661} 662 663/* 664 * Transform the running time and tick information in proc p into user, 665 * system, and interrupt time usage. 666 */ 667void 668calcru(p, up, sp, ip) 669 struct proc *p; 670 struct timeval *up; 671 struct timeval *sp; 672 struct timeval *ip; 673{ 674 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ 675 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; 676 u_int64_t uut = 0, sut = 0, iut = 0; 677 int s; 678 struct timeval tv; 679 struct bintime bt; 680 struct kse *ke; 681 struct ksegrp *kg; 682 683 mtx_assert(&sched_lock, MA_OWNED); 684 /* XXX: why spl-protect ? worst case is an off-by-one report */ 685 686 FOREACH_KSEGRP_IN_PROC(p, kg) { 687 /* we could accumulate per ksegrp and per process here*/ 688 FOREACH_KSE_IN_GROUP(kg, ke) { 689 s = splstatclock(); 690 ut = ke->ke_uticks; 691 st = ke->ke_sticks; 692 it = ke->ke_iticks; 693 splx(s); 694 695 tt = ut + st + it; 696 if (tt == 0) { 697 st = 1; 698 tt = 1; 699 } 700 701 if (ke == curthread->td_kse) { 702 /* 703 * Adjust for the current time slice. This is actually fairly 704 * important since the error here is on the order of a time 705 * quantum, which is much greater than the sampling error. 706 * XXXKSE use a different test due to threads on other 707 * processors also being 'current'. 708 */ 709 710 binuptime(&bt); 711 bintime_sub(&bt, PCPU_PTR(switchtime)); 712 bintime_add(&bt, &p->p_runtime); 713 } else { 714 bt = p->p_runtime; 715 } 716 bintime2timeval(&bt, &tv); 717 tu = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; 718 ptu = ke->ke_uu + ke->ke_su + ke->ke_iu; 719 if (tu < ptu || (int64_t)tu < 0) { 720 /* XXX no %qd in kernel. Truncate. */ 721 printf("calcru: negative time of %ld usec for pid %d (%s)\n", 722 (long)tu, p->p_pid, p->p_comm); 723 tu = ptu; 724 } 725 726 /* Subdivide tu. */ 727 uu = (tu * ut) / tt; 728 su = (tu * st) / tt; 729 iu = tu - uu - su; 730 731 /* Enforce monotonicity. */ 732 if (uu < ke->ke_uu || su < ke->ke_su || iu < ke->ke_iu) { 733 if (uu < ke->ke_uu) 734 uu = ke->ke_uu; 735 else if (uu + ke->ke_su + ke->ke_iu > tu) 736 uu = tu - ke->ke_su - ke->ke_iu; 737 if (st == 0) 738 su = ke->ke_su; 739 else { 740 su = ((tu - uu) * st) / (st + it); 741 if (su < ke->ke_su) 742 su = ke->ke_su; 743 else if (uu + su + ke->ke_iu > tu) 744 su = tu - uu - ke->ke_iu; 745 } 746 KASSERT(uu + su + ke->ke_iu <= tu, 747 ("calcru: monotonisation botch 1")); 748 iu = tu - uu - su; 749 KASSERT(iu >= ke->ke_iu, 750 ("calcru: monotonisation botch 2")); 751 } 752 ke->ke_uu = uu; 753 ke->ke_su = su; 754 ke->ke_iu = iu; 755 uut += uu; 756 sut += su; 757 iut += iu; 758 759 } /* end kse loop */ 760 } /* end kseg loop */ 761 up->tv_sec = uut / 1000000; 762 up->tv_usec = uut % 1000000; 763 sp->tv_sec = sut / 1000000; 764 sp->tv_usec = sut % 1000000; 765 if (ip != NULL) { 766 ip->tv_sec = iut / 1000000; 767 ip->tv_usec = iut % 1000000; 768 } 769} 770 771#ifndef _SYS_SYSPROTO_H_ 772struct getrusage_args { 773 int who; 774 struct rusage *rusage; 775}; 776#endif 777/* 778 * MPSAFE 779 */ 780/* ARGSUSED */ 781int 782getrusage(td, uap) 783 register struct thread *td; 784 register struct getrusage_args *uap; 785{ 786 struct proc *p = td->td_proc; 787 register struct rusage *rup; 788 int error = 0; 789 790 mtx_lock(&Giant); 791 792 switch (uap->who) { 793 case RUSAGE_SELF: 794 rup = &p->p_stats->p_ru; 795 mtx_lock_spin(&sched_lock); 796 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 797 mtx_unlock_spin(&sched_lock); 798 break; 799 800 case RUSAGE_CHILDREN: 801 rup = &p->p_stats->p_cru; 802 break; 803 804 default: 805 rup = NULL; 806 error = EINVAL; 807 break; 808 } 809 mtx_unlock(&Giant); 810 if (error == 0) { 811 error = copyout(rup, uap->rusage, sizeof (struct rusage)); 812 } 813 return(error); 814} 815 816void 817ruadd(ru, ru2) 818 register struct rusage *ru, *ru2; 819{ 820 register long *ip, *ip2; 821 register int i; 822 823 timevaladd(&ru->ru_utime, &ru2->ru_utime); 824 timevaladd(&ru->ru_stime, &ru2->ru_stime); 825 if (ru->ru_maxrss < ru2->ru_maxrss) 826 ru->ru_maxrss = ru2->ru_maxrss; 827 ip = &ru->ru_first; ip2 = &ru2->ru_first; 828 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 829 *ip++ += *ip2++; 830} 831 832/* 833 * Make a copy of the plimit structure. 834 * We share these structures copy-on-write after fork, 835 * and copy when a limit is changed. 836 */ 837struct plimit * 838limcopy(lim) 839 struct plimit *lim; 840{ 841 register struct plimit *copy; 842 843 MALLOC(copy, struct plimit *, sizeof(struct plimit), 844 M_SUBPROC, M_WAITOK); 845 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit)); 846 copy->p_lflags = 0; 847 copy->p_refcnt = 1; 848 return (copy); 849} 850 851/* 852 * Find the uidinfo structure for a uid. This structure is used to 853 * track the total resource consumption (process count, socket buffer 854 * size, etc.) for the uid and impose limits. 855 */ 856void 857uihashinit() 858{ 859 860 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash); 861 mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF); 862} 863 864/* 865 * lookup a uidinfo struct for the parameter uid. 866 * uihashtbl_mtx must be locked. 867 */ 868static struct uidinfo * 869uilookup(uid) 870 uid_t uid; 871{ 872 struct uihashhead *uipp; 873 struct uidinfo *uip; 874 875 mtx_assert(&uihashtbl_mtx, MA_OWNED); 876 uipp = UIHASH(uid); 877 LIST_FOREACH(uip, uipp, ui_hash) 878 if (uip->ui_uid == uid) 879 break; 880 881 return (uip); 882} 883 884/* 885 * Find or allocate a struct uidinfo for a particular uid. 886 * Increase refcount on uidinfo struct returned. 887 * uifree() should be called on a struct uidinfo when released. 888 */ 889struct uidinfo * 890uifind(uid) 891 uid_t uid; 892{ 893 struct uidinfo *uip; 894 895 mtx_lock(&uihashtbl_mtx); 896 uip = uilookup(uid); 897 if (uip == NULL) { 898 struct uidinfo *old_uip; 899 900 mtx_unlock(&uihashtbl_mtx); 901 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO); 902 mtx_lock(&uihashtbl_mtx); 903 /* 904 * There's a chance someone created our uidinfo while we 905 * were in malloc and not holding the lock, so we have to 906 * make sure we don't insert a duplicate uidinfo 907 */ 908 if ((old_uip = uilookup(uid)) != NULL) { 909 /* someone else beat us to it */ 910 free(uip, M_UIDINFO); 911 uip = old_uip; 912 } else { 913 uip->ui_mtxp = mtx_pool_alloc(); 914 uip->ui_uid = uid; 915 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash); 916 } 917 } 918 uihold(uip); 919 mtx_unlock(&uihashtbl_mtx); 920 return (uip); 921} 922 923/* 924 * Place another refcount on a uidinfo struct. 925 */ 926void 927uihold(uip) 928 struct uidinfo *uip; 929{ 930 931 UIDINFO_LOCK(uip); 932 uip->ui_ref++; 933 UIDINFO_UNLOCK(uip); 934} 935 936/*- 937 * Since uidinfo structs have a long lifetime, we use an 938 * opportunistic refcounting scheme to avoid locking the lookup hash 939 * for each release. 940 * 941 * If the refcount hits 0, we need to free the structure, 942 * which means we need to lock the hash. 943 * Optimal case: 944 * After locking the struct and lowering the refcount, if we find 945 * that we don't need to free, simply unlock and return. 946 * Suboptimal case: 947 * If refcount lowering results in need to free, bump the count 948 * back up, loose the lock and aquire the locks in the proper 949 * order to try again. 950 */ 951void 952uifree(uip) 953 struct uidinfo *uip; 954{ 955 956 /* Prepare for optimal case. */ 957 UIDINFO_LOCK(uip); 958 959 if (--uip->ui_ref != 0) { 960 UIDINFO_UNLOCK(uip); 961 return; 962 } 963 964 /* Prepare for suboptimal case. */ 965 uip->ui_ref++; 966 UIDINFO_UNLOCK(uip); 967 mtx_lock(&uihashtbl_mtx); 968 UIDINFO_LOCK(uip); 969 970 /* 971 * We must subtract one from the count again because we backed out 972 * our initial subtraction before dropping the lock. 973 * Since another thread may have added a reference after we dropped the 974 * initial lock we have to test for zero again. 975 */ 976 if (--uip->ui_ref == 0) { 977 LIST_REMOVE(uip, ui_hash); 978 mtx_unlock(&uihashtbl_mtx); 979 if (uip->ui_sbsize != 0) 980 /* XXX no %qd in kernel. Truncate. */ 981 printf("freeing uidinfo: uid = %d, sbsize = %ld\n", 982 uip->ui_uid, (long)uip->ui_sbsize); 983 if (uip->ui_proccnt != 0) 984 printf("freeing uidinfo: uid = %d, proccnt = %ld\n", 985 uip->ui_uid, uip->ui_proccnt); 986 UIDINFO_UNLOCK(uip); 987 FREE(uip, M_UIDINFO); 988 return; 989 } 990 991 mtx_unlock(&uihashtbl_mtx); 992 UIDINFO_UNLOCK(uip); 993} 994 995/* 996 * Change the count associated with number of processes 997 * a given user is using. When 'max' is 0, don't enforce a limit 998 */ 999int 1000chgproccnt(uip, diff, max) 1001 struct uidinfo *uip; 1002 int diff; 1003 int max; 1004{ 1005 1006 UIDINFO_LOCK(uip); 1007 /* don't allow them to exceed max, but allow subtraction */ 1008 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) { 1009 UIDINFO_UNLOCK(uip); 1010 return (0); 1011 } 1012 uip->ui_proccnt += diff; 1013 if (uip->ui_proccnt < 0) 1014 printf("negative proccnt for uid = %d\n", uip->ui_uid); 1015 UIDINFO_UNLOCK(uip); 1016 return (1); 1017} 1018 1019/* 1020 * Change the total socket buffer size a user has used. 1021 */ 1022int 1023chgsbsize(uip, hiwat, to, max) 1024 struct uidinfo *uip; 1025 u_int *hiwat; 1026 u_int to; 1027 rlim_t max; 1028{ 1029 rlim_t new; 1030 int s; 1031 1032 s = splnet(); 1033 UIDINFO_LOCK(uip); 1034 new = uip->ui_sbsize + to - *hiwat; 1035 /* don't allow them to exceed max, but allow subtraction */ 1036 if (to > *hiwat && new > max) { 1037 splx(s); 1038 UIDINFO_UNLOCK(uip); 1039 return (0); 1040 } 1041 uip->ui_sbsize = new; 1042 *hiwat = to; 1043 if (uip->ui_sbsize < 0) 1044 printf("negative sbsize for uid = %d\n", uip->ui_uid); 1045 splx(s); 1046 UIDINFO_UNLOCK(uip); 1047 return (1); 1048}
|