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$ 40 */ 41 42 #include "opt_compat.h" 43 #include "opt_rlimit.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/sysproto.h> 48 #include <sys/file.h> 49 #include <sys/kernel.h> 50 #include <sys/resourcevar.h> 51 #include <sys/malloc.h> 52 #include <sys/proc.h> 53 #include <sys/time.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_param.h> 57 #include <sys/lock.h> 58 #include <vm/pmap.h> 59 #include <vm/vm_map.h> 60 61 static int donice __P((struct proc *curp, struct proc *chgp, int n)); 62 /* dosetrlimit non-static: Needed by SysVR4 emulator */ 63 int dosetrlimit __P((struct proc *p, u_int which, struct rlimit *limp)); 64 65 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); 66 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 67 static struct mtx uihashtbl_mtx; 68 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 69 static u_long uihash; /* size of hash table - 1 */ 70 71 static struct uidinfo *uicreate __P((uid_t uid)); 72 static struct uidinfo *uilookup __P((uid_t uid)); 73 74 /* 75 * Resource controls and accounting. 76 */ 77 78 #ifndef _SYS_SYSPROTO_H_ 79 struct getpriority_args { 80 int which; 81 int who; 82 }; 83 #endif 84 int 85 getpriority(curp, uap) 86 struct proc *curp; 87 register struct getpriority_args *uap; 88 { 89 register struct proc *p; 90 register int low = PRIO_MAX + 1; 91 92 switch (uap->which) { 93 94 case PRIO_PROCESS: 95 if (uap->who == 0) 96 p = curp; 97 else 98 p = pfind(uap->who); 99 if (p == 0) 100 break; 101 if (p_can(curp, p, P_CAN_SEE, NULL)) 102 break; 103 low = p->p_nice; 104 break; 105 106 case PRIO_PGRP: { 107 register struct pgrp *pg; 108 109 if (uap->who == 0) 110 pg = curp->p_pgrp; 111 else if ((pg = pgfind(uap->who)) == NULL) 112 break; 113 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 114 if (!p_can(curp, p, P_CAN_SEE, NULL) && p->p_nice < low) 115 low = p->p_nice; 116 } 117 break; 118 } 119 120 case PRIO_USER: 121 if (uap->who == 0) 122 uap->who = curp->p_ucred->cr_uid; 123 lockmgr(&allproc_lock, LK_SHARED, NULL, CURPROC); 124 LIST_FOREACH(p, &allproc, p_list) 125 if (!p_can(curp, p, P_CAN_SEE, NULL) && 126 p->p_ucred->cr_uid == uap->who && 127 p->p_nice < low) 128 low = p->p_nice; 129 lockmgr(&allproc_lock, LK_RELEASE, NULL, CURPROC); 130 break; 131 132 default: 133 return (EINVAL); 134 } 135 if (low == PRIO_MAX + 1) 136 return (ESRCH); 137 curp->p_retval[0] = low; 138 return (0); 139 } 140 141 #ifndef _SYS_SYSPROTO_H_ 142 struct setpriority_args { 143 int which; 144 int who; 145 int prio; 146 }; 147 #endif 148 /* ARGSUSED */ 149 int 150 setpriority(curp, uap) 151 struct proc *curp; 152 register struct setpriority_args *uap; 153 { 154 register struct proc *p; 155 int found = 0, error = 0; 156 157 switch (uap->which) { 158 159 case PRIO_PROCESS: 160 if (uap->who == 0) 161 p = curp; 162 else 163 p = pfind(uap->who); 164 if (p == 0) 165 break; 166 if (p_can(curp, p, P_CAN_SEE, NULL)) 167 break; 168 error = donice(curp, p, uap->prio); 169 found++; 170 break; 171 172 case PRIO_PGRP: { 173 register struct pgrp *pg; 174 175 if (uap->who == 0) 176 pg = curp->p_pgrp; 177 else if ((pg = pgfind(uap->who)) == NULL) 178 break; 179 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 180 if (!p_can(curp, p, P_CAN_SEE, NULL)) { 181 error = donice(curp, p, uap->prio); 182 found++; 183 } 184 } 185 break; 186 } 187 188 case PRIO_USER: 189 if (uap->who == 0) 190 uap->who = curp->p_ucred->cr_uid; 191 lockmgr(&allproc_lock, LK_SHARED, NULL, CURPROC); 192 LIST_FOREACH(p, &allproc, p_list) 193 if (p->p_ucred->cr_uid == uap->who && 194 !p_can(curp, p, P_CAN_SEE, NULL)) { 195 error = donice(curp, p, uap->prio); 196 found++; 197 } 198 lockmgr(&allproc_lock, LK_RELEASE, NULL, CURPROC); 199 break; 200 201 default: 202 return (EINVAL); 203 } 204 if (found == 0) 205 return (ESRCH); 206 return (error); 207 } 208 209 static int 210 donice(curp, chgp, n) 211 register struct proc *curp, *chgp; 212 register int n; 213 { 214 int error; 215 216 if ((error = p_can(curp, chgp, P_CAN_SCHED, NULL))) 217 return (error); 218 if (n > PRIO_MAX) 219 n = PRIO_MAX; 220 if (n < PRIO_MIN) 221 n = PRIO_MIN; 222 if (n < chgp->p_nice && suser(curp)) 223 return (EACCES); 224 chgp->p_nice = n; 225 (void)resetpriority(chgp); 226 return (0); 227 } 228 229 /* rtprio system call */ 230 #ifndef _SYS_SYSPROTO_H_ 231 struct rtprio_args { 232 int function; 233 pid_t pid; 234 struct rtprio *rtp; 235 }; 236 #endif 237 238 /* 239 * Set realtime priority 240 */ 241 242 /* ARGSUSED */ 243 int 244 rtprio(curp, uap) 245 struct proc *curp; 246 register struct rtprio_args *uap; 247 { 248 register struct proc *p; 249 struct rtprio rtp; 250 int error; 251 252 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); 253 if (error) 254 return (error); 255 256 if (uap->pid == 0) 257 p = curp; 258 else 259 p = pfind(uap->pid); 260 261 if (p == 0) 262 return (ESRCH); 263 264 switch (uap->function) { 265 case RTP_LOOKUP: 266 return (copyout(&p->p_rtprio, uap->rtp, sizeof(struct rtprio))); 267 case RTP_SET: 268 if ((error = p_can(curp, p, P_CAN_SCHED, NULL))) 269 return (error); 270 /* disallow setting rtprio in most cases if not superuser */ 271 if (suser(curp) != 0) { 272 /* can't set someone else's */ 273 if (uap->pid) 274 return (EPERM); 275 /* can't set realtime priority */ 276 /* 277 * Realtime priority has to be restricted for reasons which should be 278 * obvious. However, for idle priority, there is a potential for 279 * system deadlock if an idleprio process gains a lock on a resource 280 * that other processes need (and the idleprio process can't run 281 * due to a CPU-bound normal process). Fix me! XXX 282 */ 283 #if 0 284 if (RTP_PRIO_IS_REALTIME(rtp.type)) 285 #endif 286 if (rtp.type != RTP_PRIO_NORMAL) 287 return (EPERM); 288 } 289 switch (rtp.type) { 290 #ifdef RTP_PRIO_FIFO 291 case RTP_PRIO_FIFO: 292 #endif 293 case RTP_PRIO_REALTIME: 294 case RTP_PRIO_NORMAL: 295 case RTP_PRIO_IDLE: 296 if (rtp.prio > RTP_PRIO_MAX) 297 return (EINVAL); 298 p->p_rtprio = rtp; 299 return (0); 300 default: 301 return (EINVAL); 302 } 303 304 default: 305 return (EINVAL); 306 } 307 } 308 309 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 310 #ifndef _SYS_SYSPROTO_H_ 311 struct osetrlimit_args { 312 u_int which; 313 struct orlimit *rlp; 314 }; 315 #endif 316 /* ARGSUSED */ 317 int 318 osetrlimit(p, uap) 319 struct proc *p; 320 register struct osetrlimit_args *uap; 321 { 322 struct orlimit olim; 323 struct rlimit lim; 324 int error; 325 326 if ((error = 327 copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit)))) 328 return (error); 329 lim.rlim_cur = olim.rlim_cur; 330 lim.rlim_max = olim.rlim_max; 331 return (dosetrlimit(p, uap->which, &lim)); 332 } 333 334 #ifndef _SYS_SYSPROTO_H_ 335 struct ogetrlimit_args { 336 u_int which; 337 struct orlimit *rlp; 338 }; 339 #endif 340 /* ARGSUSED */ 341 int 342 ogetrlimit(p, uap) 343 struct proc *p; 344 register struct ogetrlimit_args *uap; 345 { 346 struct orlimit olim; 347 348 if (uap->which >= RLIM_NLIMITS) 349 return (EINVAL); 350 olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur; 351 if (olim.rlim_cur == -1) 352 olim.rlim_cur = 0x7fffffff; 353 olim.rlim_max = p->p_rlimit[uap->which].rlim_max; 354 if (olim.rlim_max == -1) 355 olim.rlim_max = 0x7fffffff; 356 return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim))); 357 } 358 #endif /* COMPAT_43 || COMPAT_SUNOS */ 359 360 #ifndef _SYS_SYSPROTO_H_ 361 struct __setrlimit_args { 362 u_int which; 363 struct rlimit *rlp; 364 }; 365 #endif 366 /* ARGSUSED */ 367 int 368 setrlimit(p, uap) 369 struct proc *p; 370 register struct __setrlimit_args *uap; 371 { 372 struct rlimit alim; 373 int error; 374 375 if ((error = 376 copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit)))) 377 return (error); 378 return (dosetrlimit(p, uap->which, &alim)); 379 } 380 381 int 382 dosetrlimit(p, which, limp) 383 struct proc *p; 384 u_int which; 385 struct rlimit *limp; 386 { 387 register struct rlimit *alimp; 388 int error; 389 390 if (which >= RLIM_NLIMITS) 391 return (EINVAL); 392 alimp = &p->p_rlimit[which]; 393 394 /* 395 * Preserve historical bugs by treating negative limits as unsigned. 396 */ 397 if (limp->rlim_cur < 0) 398 limp->rlim_cur = RLIM_INFINITY; 399 if (limp->rlim_max < 0) 400 limp->rlim_max = RLIM_INFINITY; 401 402 if (limp->rlim_cur > alimp->rlim_max || 403 limp->rlim_max > alimp->rlim_max) 404 if ((error = suser_xxx(0, p, PRISON_ROOT))) 405 return (error); 406 if (limp->rlim_cur > limp->rlim_max) 407 limp->rlim_cur = limp->rlim_max; 408 if (p->p_limit->p_refcnt > 1 && 409 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 410 p->p_limit->p_refcnt--; 411 p->p_limit = limcopy(p->p_limit); 412 alimp = &p->p_rlimit[which]; 413 } 414 415 switch (which) { 416 417 case RLIMIT_CPU: 418 if (limp->rlim_cur > RLIM_INFINITY / (rlim_t)1000000) 419 p->p_limit->p_cpulimit = RLIM_INFINITY; 420 else 421 p->p_limit->p_cpulimit = 422 (rlim_t)1000000 * limp->rlim_cur; 423 break; 424 case RLIMIT_DATA: 425 if (limp->rlim_cur > MAXDSIZ) 426 limp->rlim_cur = MAXDSIZ; 427 if (limp->rlim_max > MAXDSIZ) 428 limp->rlim_max = MAXDSIZ; 429 break; 430 431 case RLIMIT_STACK: 432 if (limp->rlim_cur > MAXSSIZ) 433 limp->rlim_cur = MAXSSIZ; 434 if (limp->rlim_max > MAXSSIZ) 435 limp->rlim_max = MAXSSIZ; 436 /* 437 * Stack is allocated to the max at exec time with only 438 * "rlim_cur" bytes accessible. If stack limit is going 439 * up make more accessible, if going down make inaccessible. 440 */ 441 if (limp->rlim_cur != alimp->rlim_cur) { 442 vm_offset_t addr; 443 vm_size_t size; 444 vm_prot_t prot; 445 446 if (limp->rlim_cur > alimp->rlim_cur) { 447 prot = VM_PROT_ALL; 448 size = limp->rlim_cur - alimp->rlim_cur; 449 addr = USRSTACK - limp->rlim_cur; 450 } else { 451 prot = VM_PROT_NONE; 452 size = alimp->rlim_cur - limp->rlim_cur; 453 addr = USRSTACK - alimp->rlim_cur; 454 } 455 addr = trunc_page(addr); 456 size = round_page(size); 457 (void) vm_map_protect(&p->p_vmspace->vm_map, 458 addr, addr+size, prot, FALSE); 459 } 460 break; 461 462 case RLIMIT_NOFILE: 463 if (limp->rlim_cur > maxfilesperproc) 464 limp->rlim_cur = maxfilesperproc; 465 if (limp->rlim_max > maxfilesperproc) 466 limp->rlim_max = maxfilesperproc; 467 break; 468 469 case RLIMIT_NPROC: 470 if (limp->rlim_cur > maxprocperuid) 471 limp->rlim_cur = maxprocperuid; 472 if (limp->rlim_max > maxprocperuid) 473 limp->rlim_max = maxprocperuid; 474 break; 475 } 476 *alimp = *limp; 477 return (0); 478 } 479 480 #ifndef _SYS_SYSPROTO_H_ 481 struct __getrlimit_args { 482 u_int which; 483 struct rlimit *rlp; 484 }; 485 #endif 486 /* ARGSUSED */ 487 int 488 getrlimit(p, uap) 489 struct proc *p; 490 register struct __getrlimit_args *uap; 491 { 492 493 if (uap->which >= RLIM_NLIMITS) 494 return (EINVAL); 495 return (copyout((caddr_t)&p->p_rlimit[uap->which], (caddr_t)uap->rlp, 496 sizeof (struct rlimit))); 497 } 498 499 /* 500 * Transform the running time and tick information in proc p into user, 501 * system, and interrupt time usage. 502 */ 503 void 504 calcru(p, up, sp, ip) 505 struct proc *p; 506 struct timeval *up; 507 struct timeval *sp; 508 struct timeval *ip; 509 { 510 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ 511 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; 512 int s; 513 struct timeval tv; 514 515 /* XXX: why spl-protect ? worst case is an off-by-one report */ 516 s = splstatclock(); 517 ut = p->p_uticks; 518 st = p->p_sticks; 519 it = p->p_iticks; 520 splx(s); 521 522 tt = ut + st + it; 523 if (tt == 0) { 524 st = 1; 525 tt = 1; 526 } 527 528 tu = p->p_runtime; 529 if (p == curproc) { 530 /* 531 * Adjust for the current time slice. This is actually fairly 532 * important since the error here is on the order of a time 533 * quantum, which is much greater than the sampling error. 534 */ 535 microuptime(&tv); 536 if (timevalcmp(&tv, &switchtime, <)) 537 printf("microuptime() went backwards (%ld.%06ld -> %ld.%06ld)\n", 538 switchtime.tv_sec, switchtime.tv_usec, 539 tv.tv_sec, tv.tv_usec); 540 else 541 tu += (tv.tv_usec - switchtime.tv_usec) + 542 (tv.tv_sec - switchtime.tv_sec) * (int64_t)1000000; 543 } 544 ptu = p->p_uu + p->p_su + p->p_iu; 545 if (tu < ptu || (int64_t)tu < 0) { 546 /* XXX no %qd in kernel. Truncate. */ 547 printf("calcru: negative time of %ld usec for pid %d (%s)\n", 548 (long)tu, p->p_pid, p->p_comm); 549 tu = ptu; 550 } 551 552 /* Subdivide tu. */ 553 uu = (tu * ut) / tt; 554 su = (tu * st) / tt; 555 iu = tu - uu - su; 556 557 /* Enforce monotonicity. */ 558 if (uu < p->p_uu || su < p->p_su || iu < p->p_iu) { 559 if (uu < p->p_uu) 560 uu = p->p_uu; 561 else if (uu + p->p_su + p->p_iu > tu) 562 uu = tu - p->p_su - p->p_iu; 563 if (st == 0) 564 su = p->p_su; 565 else { 566 su = ((tu - uu) * st) / (st + it); 567 if (su < p->p_su) 568 su = p->p_su; 569 else if (uu + su + p->p_iu > tu) 570 su = tu - uu - p->p_iu; 571 } 572 KASSERT(uu + su + p->p_iu <= tu, 573 ("calcru: monotonisation botch 1")); 574 iu = tu - uu - su; 575 KASSERT(iu >= p->p_iu, 576 ("calcru: monotonisation botch 2")); 577 } 578 p->p_uu = uu; 579 p->p_su = su; 580 p->p_iu = iu; 581 582 up->tv_sec = uu / 1000000; 583 up->tv_usec = uu % 1000000; 584 sp->tv_sec = su / 1000000; 585 sp->tv_usec = su % 1000000; 586 if (ip != NULL) { 587 ip->tv_sec = iu / 1000000; 588 ip->tv_usec = iu % 1000000; 589 } 590 } 591 592 #ifndef _SYS_SYSPROTO_H_ 593 struct getrusage_args { 594 int who; 595 struct rusage *rusage; 596 }; 597 #endif 598 /* ARGSUSED */ 599 int 600 getrusage(p, uap) 601 register struct proc *p; 602 register struct getrusage_args *uap; 603 { 604 register struct rusage *rup; 605 606 switch (uap->who) { 607 608 case RUSAGE_SELF: 609 rup = &p->p_stats->p_ru; 610 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 611 break; 612 613 case RUSAGE_CHILDREN: 614 rup = &p->p_stats->p_cru; 615 break; 616 617 default: 618 return (EINVAL); 619 } 620 return (copyout((caddr_t)rup, (caddr_t)uap->rusage, 621 sizeof (struct rusage))); 622 } 623 624 void 625 ruadd(ru, ru2) 626 register struct rusage *ru, *ru2; 627 { 628 register long *ip, *ip2; 629 register int i; 630 631 timevaladd(&ru->ru_utime, &ru2->ru_utime); 632 timevaladd(&ru->ru_stime, &ru2->ru_stime); 633 if (ru->ru_maxrss < ru2->ru_maxrss) 634 ru->ru_maxrss = ru2->ru_maxrss; 635 ip = &ru->ru_first; ip2 = &ru2->ru_first; 636 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 637 *ip++ += *ip2++; 638 } 639 640 /* 641 * Make a copy of the plimit structure. 642 * We share these structures copy-on-write after fork, 643 * and copy when a limit is changed. 644 */ 645 struct plimit * 646 limcopy(lim) 647 struct plimit *lim; 648 { 649 register struct plimit *copy; 650 651 MALLOC(copy, struct plimit *, sizeof(struct plimit), 652 M_SUBPROC, M_WAITOK); 653 bcopy(lim->pl_rlimit, copy->pl_rlimit, sizeof(struct plimit)); 654 copy->p_lflags = 0; 655 copy->p_refcnt = 1; 656 return (copy); 657 } 658 659 /* 660 * Find the uidinfo structure for a uid. This structure is used to 661 * track the total resource consumption (process count, socket buffer 662 * size, etc.) for the uid and impose limits. 663 */ 664 void 665 uihashinit() 666 { 667 668 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash); 669 mtx_init(&uihashtbl_mtx, "uidinfo hash", MTX_DEF); 670 } 671 672 /* 673 * lookup a uidinfo struct for the parameter uid. 674 * uihashtbl_mtx must be locked. 675 */ 676 static struct uidinfo * 677 uilookup(uid) 678 uid_t uid; 679 { 680 struct uihashhead *uipp; 681 struct uidinfo *uip; 682 683 mtx_assert(&uihashtbl_mtx, MA_OWNED); 684 uipp = UIHASH(uid); 685 LIST_FOREACH(uip, uipp, ui_hash) 686 if (uip->ui_uid == uid) 687 break; 688 689 return (uip); 690 } 691 692 /* 693 * Create a uidinfo struct for the parameter uid. 694 * uihashtbl_mtx must be locked. 695 */ 696 static struct uidinfo * 697 uicreate(uid) 698 uid_t uid; 699 { 700 struct uidinfo *uip; 701 702 mtx_assert(&uihashtbl_mtx, MA_OWNED); 703 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK|M_ZERO); 704 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash); 705 uip->ui_uid = uid; 706 mtx_init(&uip->ui_mtx, "uidinfo struct", MTX_DEF); 707 return (uip); 708 } 709 710 /* 711 * find or allocate a struct uidinfo for a particular uid 712 * increases refcount on uidinfo struct returned. 713 * uifree() should be called on a struct uidinfo when released. 714 */ 715 struct uidinfo * 716 uifind(uid) 717 uid_t uid; 718 { 719 struct uidinfo *uip; 720 721 mtx_enter(&uihashtbl_mtx, MTX_DEF); 722 uip = uilookup(uid); 723 if (uip == NULL) 724 uip = uicreate(uid); 725 uihold(uip); 726 mtx_exit(&uihashtbl_mtx, MTX_DEF); 727 return (uip); 728 } 729 730 /* 731 * subtract one from the refcount in the struct uidinfo, if 0 free it 732 */ 733 void 734 uifree(uip) 735 struct uidinfo *uip; 736 { 737 738 mtx_enter(&uihashtbl_mtx, MTX_DEF); 739 mtx_enter(&uip->ui_mtx, MTX_DEF); 740 if (--uip->ui_ref == 0) { 741 if (uip->ui_sbsize != 0) 742 /* XXX no %qd in kernel. Truncate. */ 743 printf("freeing uidinfo: uid = %d, sbsize = %ld\n", 744 uip->ui_uid, (long)uip->ui_sbsize); 745 if (uip->ui_proccnt != 0) 746 printf("freeing uidinfo: uid = %d, proccnt = %ld\n", 747 uip->ui_uid, uip->ui_proccnt); 748 LIST_REMOVE(uip, ui_hash); 749 mtx_exit(&uihashtbl_mtx, MTX_DEF); 750 mtx_destroy(&uip->ui_mtx); 751 FREE(uip, M_UIDINFO); 752 return; 753 } 754 mtx_exit(&uip->ui_mtx, MTX_DEF); 755 mtx_exit(&uihashtbl_mtx, MTX_DEF); 756 return; 757 } 758 759 /* 760 * Change the count associated with number of processes 761 * a given user is using. When 'max' is 0, don't enforce a limit 762 */ 763 int 764 chgproccnt(uip, diff, max) 765 struct uidinfo *uip; 766 int diff; 767 int max; 768 { 769 770 mtx_enter(&uip->ui_mtx, MTX_DEF); 771 /* don't allow them to exceed max, but allow subtraction */ 772 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) { 773 mtx_exit(&uip->ui_mtx, MTX_DEF); 774 return (0); 775 } 776 uip->ui_proccnt += diff; 777 if (uip->ui_proccnt < 0) 778 printf("negative proccnt for uid = %d\n", uip->ui_uid); 779 mtx_exit(&uip->ui_mtx, MTX_DEF); 780 return (1); 781 } 782 783 /* 784 * Change the total socket buffer size a user has used. 785 */ 786 int 787 chgsbsize(uip, hiwat, to, max) 788 struct uidinfo *uip; 789 u_long *hiwat; 790 u_long to; 791 rlim_t max; 792 { 793 rlim_t new; 794 int s; 795 796 mtx_enter(&uip->ui_mtx, MTX_DEF); 797 s = splnet(); 798 new = uip->ui_sbsize + to - *hiwat; 799 /* don't allow them to exceed max, but allow subtraction */ 800 if (to > *hiwat && new > max) { 801 splx(s); 802 mtx_exit(&uip->ui_mtx, MTX_DEF); 803 return (0); 804 } 805 uip->ui_sbsize = new; 806 *hiwat = to; 807 if (uip->ui_sbsize < 0) 808 printf("negative sbsize for uid = %d\n", uip->ui_uid); 809 splx(s); 810 mtx_exit(&uip->ui_mtx, MTX_DEF); 811 return (1); 812 } 813