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