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