xref: /freebsd/sys/kern/kern_resource.c (revision 6adf353a56a161443406b44a45d00c688ca7b857)
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 
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	*uilookup __P((uid_t uid));
72 
73 /*
74  * Resource controls and accounting.
75  */
76 
77 #ifndef _SYS_SYSPROTO_H_
78 struct getpriority_args {
79 	int	which;
80 	int	who;
81 };
82 #endif
83 int
84 getpriority(curp, uap)
85 	struct proc *curp;
86 	register struct getpriority_args *uap;
87 {
88 	register struct proc *p;
89 	register int low = PRIO_MAX + 1;
90 
91 	switch (uap->which) {
92 
93 	case PRIO_PROCESS:
94 		if (uap->who == 0)
95 			low = curp->p_nice;
96 		else {
97 			p = pfind(uap->who);
98 			if (p == NULL)
99 				break;
100 			if (p_cansee(curp, p) == 0)
101 				low = p->p_nice;
102 			PROC_UNLOCK(p);
103 		}
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_cansee(curp, p) && 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 		sx_slock(&allproc_lock);
124 		LIST_FOREACH(p, &allproc, p_list)
125 			if (!p_cansee(curp, p) &&
126 			    p->p_ucred->cr_uid == uap->who &&
127 			    p->p_nice < low)
128 				low = p->p_nice;
129 		sx_sunlock(&allproc_lock);
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 			error = donice(curp, curp, uap->prio);
162 		else {
163 			p = pfind(uap->who);
164 			if (p == 0)
165 				break;
166 			if (p_cansee(curp, p) == 0)
167 				error = donice(curp, p, uap->prio);
168 			PROC_UNLOCK(p);
169 		}
170 		found++;
171 		break;
172 
173 	case PRIO_PGRP: {
174 		register struct pgrp *pg;
175 
176 		if (uap->who == 0)
177 			pg = curp->p_pgrp;
178 		else if ((pg = pgfind(uap->who)) == NULL)
179 			break;
180 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
181 			if (!p_cansee(curp, p)) {
182 				error = donice(curp, p, uap->prio);
183 				found++;
184 			}
185 		}
186 		break;
187 	}
188 
189 	case PRIO_USER:
190 		if (uap->who == 0)
191 			uap->who = curp->p_ucred->cr_uid;
192 		sx_slock(&allproc_lock);
193 		LIST_FOREACH(p, &allproc, p_list)
194 			if (p->p_ucred->cr_uid == uap->who &&
195 			    !p_cansee(curp, p)) {
196 				error = donice(curp, p, uap->prio);
197 				found++;
198 			}
199 		sx_sunlock(&allproc_lock);
200 		break;
201 
202 	default:
203 		return (EINVAL);
204 	}
205 	if (found == 0)
206 		return (ESRCH);
207 	return (error);
208 }
209 
210 static int
211 donice(curp, chgp, n)
212 	register struct proc *curp, *chgp;
213 	register int n;
214 {
215 	int	error;
216 
217 	if ((error = p_cansched(curp, chgp)))
218 		return (error);
219 	if (n > PRIO_MAX)
220 		n = PRIO_MAX;
221 	if (n < PRIO_MIN)
222 		n = PRIO_MIN;
223 	if (n < chgp->p_nice && suser(curp))
224 		return (EACCES);
225 	chgp->p_nice = n;
226 	(void)resetpriority(chgp);
227 	return (0);
228 }
229 
230 /* rtprio system call */
231 #ifndef _SYS_SYSPROTO_H_
232 struct rtprio_args {
233 	int		function;
234 	pid_t		pid;
235 	struct rtprio	*rtp;
236 };
237 #endif
238 
239 /*
240  * Set realtime priority
241  */
242 
243 /* ARGSUSED */
244 int
245 rtprio(curp, uap)
246 	struct proc *curp;
247 	register struct rtprio_args *uap;
248 {
249 	register struct proc *p;
250 	struct rtprio rtp;
251 	int error;
252 
253 	if (uap->pid == 0) {
254 		p = curp;
255 		PROC_LOCK(p);
256 	} else
257 		p = pfind(uap->pid);
258 
259 	if (p == NULL)
260 		return (ESRCH);
261 
262 	switch (uap->function) {
263 	case RTP_LOOKUP:
264 		if ((error = p_cansee(curp, p)))
265 			break;
266 		pri_to_rtp(&p->p_pri, &rtp);
267 		error = copyout(&rtp, uap->rtp, sizeof(struct rtprio));
268 		break;
269 	case RTP_SET:
270 		if ((error = p_cansched(curp, p)) ||
271 		    (error = copyin(uap->rtp, &rtp, sizeof(struct rtprio))))
272 			break;
273 		/* disallow setting rtprio in most cases if not superuser */
274 		if (suser(curp) != 0) {
275 			/* can't set someone else's */
276 			if (uap->pid) {
277 				error = EPERM;
278 				break;
279 			}
280 			/* can't set realtime priority */
281 /*
282  * Realtime priority has to be restricted for reasons which should be
283  * obvious. However, for idle priority, there is a potential for
284  * system deadlock if an idleprio process gains a lock on a resource
285  * that other processes need (and the idleprio process can't run
286  * due to a CPU-bound normal process). Fix me! XXX
287  */
288 #if 0
289  			if (RTP_PRIO_IS_REALTIME(rtp.type))
290 #endif
291 			if (rtp.type != RTP_PRIO_NORMAL) {
292 				error = EPERM;
293 				break;
294 			}
295 		}
296 		error = rtp_to_pri(&rtp, &p->p_pri);
297 		break;
298 	default:
299 		error = EINVAL;
300 		break;
301 	}
302 	PROC_UNLOCK(p);
303 	return (error);
304 }
305 
306 int
307 rtp_to_pri(struct rtprio *rtp, struct priority *pri)
308 {
309 
310 	if (rtp->prio > RTP_PRIO_MAX)
311 		return (EINVAL);
312 	switch (RTP_PRIO_BASE(rtp->type)) {
313 	case RTP_PRIO_REALTIME:
314 		pri->pri_level = PRI_MIN_REALTIME + rtp->prio;
315 		break;
316 	case RTP_PRIO_NORMAL:
317 		pri->pri_level = PRI_MIN_TIMESHARE + rtp->prio;
318 		break;
319 	case RTP_PRIO_IDLE:
320 		pri->pri_level = PRI_MIN_IDLE + rtp->prio;
321 		break;
322 	default:
323 		return (EINVAL);
324 	}
325 	pri->pri_class = rtp->type;
326 	pri->pri_native = pri->pri_level;
327 	pri->pri_user = pri->pri_level;
328 	return (0);
329 }
330 
331 void
332 pri_to_rtp(struct priority *pri, struct rtprio *rtp)
333 {
334 
335 	switch (PRI_BASE(pri->pri_class)) {
336 	case PRI_REALTIME:
337 		rtp->prio = pri->pri_level - PRI_MIN_REALTIME;
338 		break;
339 	case PRI_TIMESHARE:
340 		rtp->prio = pri->pri_level - PRI_MIN_TIMESHARE;
341 		break;
342 	case PRI_IDLE:
343 		rtp->prio = pri->pri_level - PRI_MIN_IDLE;
344 		break;
345 	default:
346 		break;
347 	}
348 	rtp->type = pri->pri_class;
349 }
350 
351 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
352 #ifndef _SYS_SYSPROTO_H_
353 struct osetrlimit_args {
354 	u_int	which;
355 	struct	orlimit *rlp;
356 };
357 #endif
358 /* ARGSUSED */
359 int
360 osetrlimit(p, uap)
361 	struct proc *p;
362 	register struct osetrlimit_args *uap;
363 {
364 	struct orlimit olim;
365 	struct rlimit lim;
366 	int error;
367 
368 	if ((error =
369 	    copyin((caddr_t)uap->rlp, (caddr_t)&olim, sizeof(struct orlimit))))
370 		return (error);
371 	lim.rlim_cur = olim.rlim_cur;
372 	lim.rlim_max = olim.rlim_max;
373 	return (dosetrlimit(p, uap->which, &lim));
374 }
375 
376 #ifndef _SYS_SYSPROTO_H_
377 struct ogetrlimit_args {
378 	u_int	which;
379 	struct	orlimit *rlp;
380 };
381 #endif
382 /* ARGSUSED */
383 int
384 ogetrlimit(p, uap)
385 	struct proc *p;
386 	register struct ogetrlimit_args *uap;
387 {
388 	struct orlimit olim;
389 
390 	if (uap->which >= RLIM_NLIMITS)
391 		return (EINVAL);
392 	olim.rlim_cur = p->p_rlimit[uap->which].rlim_cur;
393 	if (olim.rlim_cur == -1)
394 		olim.rlim_cur = 0x7fffffff;
395 	olim.rlim_max = p->p_rlimit[uap->which].rlim_max;
396 	if (olim.rlim_max == -1)
397 		olim.rlim_max = 0x7fffffff;
398 	return (copyout((caddr_t)&olim, (caddr_t)uap->rlp, sizeof(olim)));
399 }
400 #endif /* COMPAT_43 || COMPAT_SUNOS */
401 
402 #ifndef _SYS_SYSPROTO_H_
403 struct __setrlimit_args {
404 	u_int	which;
405 	struct	rlimit *rlp;
406 };
407 #endif
408 /* ARGSUSED */
409 int
410 setrlimit(p, uap)
411 	struct proc *p;
412 	register struct __setrlimit_args *uap;
413 {
414 	struct rlimit alim;
415 	int error;
416 
417 	if ((error =
418 	    copyin((caddr_t)uap->rlp, (caddr_t)&alim, sizeof (struct rlimit))))
419 		return (error);
420 	return (dosetrlimit(p, uap->which, &alim));
421 }
422 
423 int
424 dosetrlimit(p, which, limp)
425 	struct proc *p;
426 	u_int which;
427 	struct rlimit *limp;
428 {
429 	register struct rlimit *alimp;
430 	int error;
431 
432 	GIANT_REQUIRED;
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