kern_resource.c revision 1.83 1 /* $NetBSD: kern_resource.c,v 1.83 2004/05/06 22:20:30 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_resource.c 8.8 (Berkeley) 2/14/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.83 2004/05/06 22:20:30 pk Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/file.h>
46 #include <sys/resourcevar.h>
47 #include <sys/malloc.h>
48 #include <sys/pool.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51
52 #include <sys/mount.h>
53 #include <sys/sa.h>
54 #include <sys/syscallargs.h>
55
56 #include <uvm/uvm_extern.h>
57
58 /*
59 * Maximum process data and stack limits.
60 * They are variables so they are patchable.
61 */
62 rlim_t maxdmap = MAXDSIZ;
63 rlim_t maxsmap = MAXSSIZ;
64
65 struct uihashhead *uihashtbl;
66 u_long uihash; /* size of hash table - 1 */
67
68 static struct uidinfo *getuidinfo(uid_t);
69 static void freeuidinfo(struct uidinfo *);
70 static struct uidinfo *allocuidinfo(uid_t);
71
72 /*
73 * Resource controls and accounting.
74 */
75
76 int
77 sys_getpriority(l, v, retval)
78 struct lwp *l;
79 void *v;
80 register_t *retval;
81 {
82 struct sys_getpriority_args /* {
83 syscallarg(int) which;
84 syscallarg(id_t) who;
85 } */ *uap = v;
86 struct proc *curp = l->l_proc, *p;
87 int low = NZERO + PRIO_MAX + 1;
88
89 switch (SCARG(uap, which)) {
90
91 case PRIO_PROCESS:
92 if (SCARG(uap, who) == 0)
93 p = curp;
94 else
95 p = pfind(SCARG(uap, who));
96 if (p == 0)
97 break;
98 low = p->p_nice;
99 break;
100
101 case PRIO_PGRP: {
102 struct pgrp *pg;
103
104 if (SCARG(uap, who) == 0)
105 pg = curp->p_pgrp;
106 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
107 break;
108 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
109 if (p->p_nice < low)
110 low = p->p_nice;
111 }
112 break;
113 }
114
115 case PRIO_USER:
116 if (SCARG(uap, who) == 0)
117 SCARG(uap, who) = curp->p_ucred->cr_uid;
118 proclist_lock_read();
119 LIST_FOREACH(p, &allproc, p_list) {
120 if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who) &&
121 p->p_nice < low)
122 low = p->p_nice;
123 }
124 proclist_unlock_read();
125 break;
126
127 default:
128 return (EINVAL);
129 }
130 if (low == NZERO + PRIO_MAX + 1)
131 return (ESRCH);
132 *retval = low - NZERO;
133 return (0);
134 }
135
136 /* ARGSUSED */
137 int
138 sys_setpriority(l, v, retval)
139 struct lwp *l;
140 void *v;
141 register_t *retval;
142 {
143 struct sys_setpriority_args /* {
144 syscallarg(int) which;
145 syscallarg(id_t) who;
146 syscallarg(int) prio;
147 } */ *uap = v;
148 struct proc *curp = l->l_proc, *p;
149 int found = 0, error = 0;
150
151 switch (SCARG(uap, which)) {
152
153 case PRIO_PROCESS:
154 if (SCARG(uap, who) == 0)
155 p = curp;
156 else
157 p = pfind(SCARG(uap, who));
158 if (p == 0)
159 break;
160 error = donice(curp, p, SCARG(uap, prio));
161 found++;
162 break;
163
164 case PRIO_PGRP: {
165 struct pgrp *pg;
166
167 if (SCARG(uap, who) == 0)
168 pg = curp->p_pgrp;
169 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
170 break;
171 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
172 error = donice(curp, p, SCARG(uap, prio));
173 found++;
174 }
175 break;
176 }
177
178 case PRIO_USER:
179 if (SCARG(uap, who) == 0)
180 SCARG(uap, who) = curp->p_ucred->cr_uid;
181 proclist_lock_read();
182 LIST_FOREACH(p, &allproc, p_list) {
183 if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who)) {
184 error = donice(curp, p, SCARG(uap, prio));
185 found++;
186 }
187 }
188 proclist_unlock_read();
189 break;
190
191 default:
192 return (EINVAL);
193 }
194 if (found == 0)
195 return (ESRCH);
196 return (error);
197 }
198
199 int
200 donice(curp, chgp, n)
201 struct proc *curp, *chgp;
202 int n;
203 {
204 struct pcred *pcred = curp->p_cred;
205 int s;
206
207 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
208 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
209 pcred->p_ruid != chgp->p_ucred->cr_uid)
210 return (EPERM);
211 if (n > PRIO_MAX)
212 n = PRIO_MAX;
213 if (n < PRIO_MIN)
214 n = PRIO_MIN;
215 n += NZERO;
216 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
217 return (EACCES);
218 chgp->p_nice = n;
219 SCHED_LOCK(s);
220 (void)resetprocpriority(chgp);
221 SCHED_UNLOCK(s);
222 return (0);
223 }
224
225 /* ARGSUSED */
226 int
227 sys_setrlimit(l, v, retval)
228 struct lwp *l;
229 void *v;
230 register_t *retval;
231 {
232 struct sys_setrlimit_args /* {
233 syscallarg(int) which;
234 syscallarg(const struct rlimit *) rlp;
235 } */ *uap = v;
236 struct proc *p = l->l_proc;
237 int which = SCARG(uap, which);
238 struct rlimit alim;
239 int error;
240
241 error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
242 if (error)
243 return (error);
244 return (dosetrlimit(p, p->p_cred, which, &alim));
245 }
246
247 int
248 dosetrlimit(p, cred, which, limp)
249 struct proc *p;
250 struct pcred *cred;
251 int which;
252 struct rlimit *limp;
253 {
254 struct rlimit *alimp;
255 struct plimit *oldplim;
256 int error;
257
258 if ((u_int)which >= RLIM_NLIMITS)
259 return (EINVAL);
260
261 if (limp->rlim_cur < 0 || limp->rlim_max < 0)
262 return (EINVAL);
263
264 alimp = &p->p_rlimit[which];
265 /* if we don't change the value, no need to limcopy() */
266 if (limp->rlim_cur == alimp->rlim_cur &&
267 limp->rlim_max == alimp->rlim_max)
268 return 0;
269
270 if (limp->rlim_cur > limp->rlim_max) {
271 /*
272 * This is programming error. According to SUSv2, we should
273 * return error in this case.
274 */
275 return (EINVAL);
276 }
277 if (limp->rlim_max > alimp->rlim_max
278 && (error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
279 return (error);
280
281 if (p->p_limit->p_refcnt > 1 &&
282 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
283 p->p_limit = limcopy(oldplim = p->p_limit);
284 limfree(oldplim);
285 alimp = &p->p_rlimit[which];
286 }
287
288 switch (which) {
289
290 case RLIMIT_DATA:
291 if (limp->rlim_cur > maxdmap)
292 limp->rlim_cur = maxdmap;
293 if (limp->rlim_max > maxdmap)
294 limp->rlim_max = maxdmap;
295 break;
296
297 case RLIMIT_STACK:
298 if (limp->rlim_cur > maxsmap)
299 limp->rlim_cur = maxsmap;
300 if (limp->rlim_max > maxsmap)
301 limp->rlim_max = maxsmap;
302
303 /*
304 * Return EINVAL if the new stack size limit is lower than
305 * current usage. Otherwise, the process would get SIGSEGV the
306 * moment it would try to access anything on it's current stack.
307 * This conforms to SUSv2.
308 */
309 if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
310 || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE)
311 return (EINVAL);
312
313 /*
314 * Stack is allocated to the max at exec time with
315 * only "rlim_cur" bytes accessible (In other words,
316 * allocates stack dividing two contiguous regions at
317 * "rlim_cur" bytes boundary).
318 *
319 * Since allocation is done in terms of page, roundup
320 * "rlim_cur" (otherwise, contiguous regions
321 * overlap). If stack limit is going up make more
322 * accessible, if going down make inaccessible.
323 */
324 limp->rlim_cur = round_page(limp->rlim_cur);
325 if (limp->rlim_cur != alimp->rlim_cur) {
326 vaddr_t addr;
327 vsize_t size;
328 vm_prot_t prot;
329
330 if (limp->rlim_cur > alimp->rlim_cur) {
331 prot = VM_PROT_READ | VM_PROT_WRITE;
332 size = limp->rlim_cur - alimp->rlim_cur;
333 addr = USRSTACK - limp->rlim_cur;
334 } else {
335 prot = VM_PROT_NONE;
336 size = alimp->rlim_cur - limp->rlim_cur;
337 addr = USRSTACK - alimp->rlim_cur;
338 }
339 (void) uvm_map_protect(&p->p_vmspace->vm_map,
340 addr, addr+size, prot, FALSE);
341 }
342 break;
343
344 case RLIMIT_NOFILE:
345 if (limp->rlim_cur > maxfiles)
346 limp->rlim_cur = maxfiles;
347 if (limp->rlim_max > maxfiles)
348 limp->rlim_max = maxfiles;
349 break;
350
351 case RLIMIT_NPROC:
352 if (limp->rlim_cur > maxproc)
353 limp->rlim_cur = maxproc;
354 if (limp->rlim_max > maxproc)
355 limp->rlim_max = maxproc;
356 break;
357 }
358 *alimp = *limp;
359 return (0);
360 }
361
362 /* ARGSUSED */
363 int
364 sys_getrlimit(l, v, retval)
365 struct lwp *l;
366 void *v;
367 register_t *retval;
368 {
369 struct sys_getrlimit_args /* {
370 syscallarg(int) which;
371 syscallarg(struct rlimit *) rlp;
372 } */ *uap = v;
373 struct proc *p = l->l_proc;
374 int which = SCARG(uap, which);
375
376 if ((u_int)which >= RLIM_NLIMITS)
377 return (EINVAL);
378 return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
379 sizeof(struct rlimit)));
380 }
381
382 /*
383 * Transform the running time and tick information in proc p into user,
384 * system, and interrupt time usage.
385 */
386 void
387 calcru(p, up, sp, ip)
388 struct proc *p;
389 struct timeval *up;
390 struct timeval *sp;
391 struct timeval *ip;
392 {
393 u_quad_t u, st, ut, it, tot;
394 unsigned long sec;
395 long usec;
396 int s;
397 struct timeval tv;
398 struct lwp *l;
399
400 s = splstatclock();
401 st = p->p_sticks;
402 ut = p->p_uticks;
403 it = p->p_iticks;
404 splx(s);
405
406 sec = p->p_rtime.tv_sec;
407 usec = p->p_rtime.tv_usec;
408 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
409 if (l->l_stat == LSONPROC) {
410 struct schedstate_percpu *spc;
411
412 KDASSERT(l->l_cpu != NULL);
413 spc = &l->l_cpu->ci_schedstate;
414
415 /*
416 * Adjust for the current time slice. This is
417 * actually fairly important since the error
418 * here is on the order of a time quantum,
419 * which is much greater than the sampling
420 * error.
421 */
422 microtime(&tv);
423 sec += tv.tv_sec - spc->spc_runtime.tv_sec;
424 usec += tv.tv_usec - spc->spc_runtime.tv_usec;
425 }
426 }
427
428 tot = st + ut + it;
429 u = sec * 1000000ull + usec;
430
431 if (tot == 0) {
432 /* No ticks, so can't use to share time out, split 50-50 */
433 st = ut = u / 2;
434 } else {
435 st = (u * st) / tot;
436 ut = (u * ut) / tot;
437 }
438 sp->tv_sec = st / 1000000;
439 sp->tv_usec = st % 1000000;
440 up->tv_sec = ut / 1000000;
441 up->tv_usec = ut % 1000000;
442 if (ip != NULL) {
443 if (it != 0)
444 it = (u * it) / tot;
445 ip->tv_sec = it / 1000000;
446 ip->tv_usec = it % 1000000;
447 }
448 }
449
450 /* ARGSUSED */
451 int
452 sys_getrusage(l, v, retval)
453 struct lwp *l;
454 void *v;
455 register_t *retval;
456 {
457 struct sys_getrusage_args /* {
458 syscallarg(int) who;
459 syscallarg(struct rusage *) rusage;
460 } */ *uap = v;
461 struct rusage *rup;
462 struct proc *p = l->l_proc;
463
464 switch (SCARG(uap, who)) {
465
466 case RUSAGE_SELF:
467 rup = &p->p_stats->p_ru;
468 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
469 break;
470
471 case RUSAGE_CHILDREN:
472 rup = &p->p_stats->p_cru;
473 break;
474
475 default:
476 return (EINVAL);
477 }
478 return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
479 }
480
481 void
482 ruadd(ru, ru2)
483 struct rusage *ru, *ru2;
484 {
485 long *ip, *ip2;
486 int i;
487
488 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
489 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
490 if (ru->ru_maxrss < ru2->ru_maxrss)
491 ru->ru_maxrss = ru2->ru_maxrss;
492 ip = &ru->ru_first; ip2 = &ru2->ru_first;
493 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
494 *ip++ += *ip2++;
495 }
496
497 /*
498 * Make a copy of the plimit structure.
499 * We share these structures copy-on-write after fork,
500 * and copy when a limit is changed.
501 */
502 struct plimit *
503 limcopy(lim)
504 struct plimit *lim;
505 {
506 struct plimit *newlim;
507 size_t l = 0;
508
509 simple_lock(&lim->p_slock);
510 if (lim->pl_corename != defcorename)
511 l = strlen(lim->pl_corename) + 1;
512 simple_unlock(&lim->p_slock);
513
514 newlim = pool_get(&plimit_pool, PR_WAITOK);
515 simple_lock_init(&newlim->p_slock);
516 newlim->p_lflags = 0;
517 newlim->p_refcnt = 1;
518 newlim->pl_corename = (l != 0)
519 ? malloc(l, M_TEMP, M_WAITOK)
520 : defcorename;
521
522 simple_lock(&lim->p_slock);
523 memcpy(newlim->pl_rlimit, lim->pl_rlimit,
524 sizeof(struct rlimit) * RLIM_NLIMITS);
525
526 if (l != 0)
527 strlcpy(newlim->pl_corename, lim->pl_corename, l);
528 simple_unlock(&lim->p_slock);
529
530 return (newlim);
531 }
532
533 void
534 limfree(lim)
535 struct plimit *lim;
536 {
537
538 simple_lock(&lim->p_slock);
539 int n = --lim->p_refcnt;
540 simple_unlock(&lim->p_slock);
541 if (n > 0)
542 return;
543 #ifdef DIAGNOSTIC
544 if (n < 0)
545 panic("limfree");
546 #endif
547 if (lim->pl_corename != defcorename)
548 free(lim->pl_corename, M_TEMP);
549 pool_put(&plimit_pool, lim);
550 }
551
552 struct pstats *
553 pstatscopy(ps)
554 struct pstats *ps;
555 {
556
557 struct pstats *newps;
558
559 newps = pool_get(&pstats_pool, PR_WAITOK);
560
561 memset(&newps->pstat_startzero, 0,
562 (unsigned) ((caddr_t)&newps->pstat_endzero -
563 (caddr_t)&newps->pstat_startzero));
564 memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
565 ((caddr_t)&newps->pstat_endcopy -
566 (caddr_t)&newps->pstat_startcopy));
567
568 return (newps);
569
570 }
571
572 void
573 pstatsfree(ps)
574 struct pstats *ps;
575 {
576
577 pool_put(&pstats_pool, ps);
578 }
579
580 /*
581 * sysctl interface in five parts
582 */
583
584 /*
585 * a routine for sysctl proc subtree helpers that need to pick a valid
586 * process by pid.
587 */
588 static int
589 sysctl_proc_findproc(struct proc *p, struct proc **p2, pid_t pid)
590 {
591 struct proc *ptmp;
592 int i, error = 0;
593
594 if (pid == PROC_CURPROC)
595 ptmp = p;
596 else if ((ptmp = pfind(pid)) == NULL)
597 error = ESRCH;
598 else {
599 /*
600 * suid proc of ours or proc not ours
601 */
602 if (p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
603 p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
604 error = suser(p->p_ucred, &p->p_acflag);
605
606 /*
607 * sgid proc has sgid back to us temporarily
608 */
609 else if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
610 error = suser(p->p_ucred, &p->p_acflag);
611
612 /*
613 * our rgid must be in target's group list (ie,
614 * sub-processes started by a sgid process)
615 */
616 else {
617 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
618 if (p->p_ucred->cr_groups[i] ==
619 ptmp->p_cred->p_rgid)
620 break;
621 }
622 if (i == p->p_ucred->cr_ngroups)
623 error = suser(p->p_ucred, &p->p_acflag);
624 }
625 }
626
627 *p2 = ptmp;
628 return (error);
629 }
630
631 /*
632 * sysctl helper routine for setting a process's specific corefile
633 * name. picks the process based on the given pid and checks the
634 * correctness of the new value.
635 */
636 static int
637 sysctl_proc_corename(SYSCTLFN_ARGS)
638 {
639 struct proc *ptmp, *p;
640 struct plimit *lim;
641 int error = 0, len;
642 char cname[MAXPATHLEN], *tmp;
643 struct sysctlnode node;
644
645 /*
646 * is this all correct?
647 */
648 if (namelen != 0)
649 return (EINVAL);
650 if (name[-1] != PROC_PID_CORENAME)
651 return (EINVAL);
652
653 /*
654 * whom are we tweaking?
655 */
656 p = l->l_proc;
657 error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
658 if (error)
659 return (error);
660
661 /*
662 * let them modify a temporary copy of the core name
663 */
664 node = *rnode;
665 strlcpy(cname, ptmp->p_limit->pl_corename, sizeof(cname));
666 node.sysctl_data = cname;
667 error = sysctl_lookup(SYSCTLFN_CALL(&node));
668
669 /*
670 * if that failed, or they have nothing new to say, or we've
671 * heard it before...
672 */
673 if (error || newp == NULL ||
674 strcmp(cname, ptmp->p_limit->pl_corename) == 0)
675 return (error);
676
677 /*
678 * no error yet and cname now has the new core name in it.
679 * let's see if it looks acceptable. it must be either "core"
680 * or end in ".core" or "/core".
681 */
682 len = strlen(cname);
683 if (len < 4)
684 return (EINVAL);
685 if (strcmp(cname + len - 4, "core") != 0)
686 return (EINVAL);
687 if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.')
688 return (EINVAL);
689
690 /*
691 * hmm...looks good. now...where do we put it?
692 */
693 tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
694 if (tmp == NULL)
695 return (ENOMEM);
696 strlcpy(tmp, cname, len + 1);
697
698 lim = ptmp->p_limit;
699 if (lim->p_refcnt > 1 && (lim->p_lflags & PL_SHAREMOD) == 0) {
700 ptmp->p_limit = limcopy(lim);
701 limfree(lim);
702 lim = ptmp->p_limit;
703 }
704 if (lim->pl_corename != defcorename)
705 free(lim->pl_corename, M_TEMP);
706 lim->pl_corename = tmp;
707
708 return (error);
709 }
710
711 /*
712 * sysctl helper routine for checking/setting a process's stop flags,
713 * one for fork and one for exec.
714 */
715 static int
716 sysctl_proc_stop(SYSCTLFN_ARGS)
717 {
718 struct proc *p, *ptmp;
719 int i, f, error = 0;
720 struct sysctlnode node;
721
722 if (namelen != 0)
723 return (EINVAL);
724
725 p = l->l_proc;
726 error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
727 if (error)
728 return (error);
729
730 switch (rnode->sysctl_num) {
731 case PROC_PID_STOPFORK:
732 f = P_STOPFORK;
733 break;
734 case PROC_PID_STOPEXEC:
735 f = P_STOPEXEC;
736 break;
737 case PROC_PID_STOPEXIT:
738 f = P_STOPEXIT;
739 break;
740 default:
741 return (EINVAL);
742 }
743
744 i = (ptmp->p_flag & f) ? 1 : 0;
745 node = *rnode;
746 node.sysctl_data = &i;
747 error = sysctl_lookup(SYSCTLFN_CALL(&node));
748 if (error || newp == NULL)
749 return (error);
750
751 if (i)
752 ptmp->p_flag |= f;
753 else
754 ptmp->p_flag &= ~f;
755
756 return (0);
757 }
758
759 /*
760 * sysctl helper routine for a process's rlimits as exposed by sysctl.
761 */
762 static int
763 sysctl_proc_plimit(SYSCTLFN_ARGS)
764 {
765 struct proc *ptmp, *p;
766 u_int limitno;
767 int which, error = 0;
768 struct rlimit alim;
769 struct sysctlnode node;
770
771 if (namelen != 0)
772 return (EINVAL);
773
774 which = name[-1];
775 if (which != PROC_PID_LIMIT_TYPE_SOFT &&
776 which != PROC_PID_LIMIT_TYPE_HARD)
777 return (EINVAL);
778
779 limitno = name[-2] - 1;
780 if (limitno >= RLIM_NLIMITS)
781 return (EINVAL);
782
783 if (name[-3] != PROC_PID_LIMIT)
784 return (EINVAL);
785
786 p = l->l_proc;
787 error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-4]);
788 if (error)
789 return (error);
790
791 node = *rnode;
792 memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
793 if (which == PROC_PID_LIMIT_TYPE_HARD)
794 node.sysctl_data = &alim.rlim_max;
795 else
796 node.sysctl_data = &alim.rlim_cur;
797
798 error = sysctl_lookup(SYSCTLFN_CALL(&node));
799 if (error || newp == NULL)
800 return (error);
801
802 return (dosetrlimit(ptmp, p->p_cred, limitno, &alim));
803 }
804
805 /*
806 * and finally, the actually glue that sticks it to the tree
807 */
808 SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
809 {
810
811 sysctl_createv(clog, 0, NULL, NULL,
812 CTLFLAG_PERMANENT,
813 CTLTYPE_NODE, "proc", NULL,
814 NULL, 0, NULL, 0,
815 CTL_PROC, CTL_EOL);
816 sysctl_createv(clog, 0, NULL, NULL,
817 CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
818 CTLTYPE_NODE, "curproc",
819 SYSCTL_DESCR("Per-process settings"),
820 NULL, 0, NULL, 0,
821 CTL_PROC, PROC_CURPROC, CTL_EOL);
822
823 sysctl_createv(clog, 0, NULL, NULL,
824 CTLFLAG_PERMANENT|CTLFLAG_READONLY2|CTLFLAG_ANYWRITE,
825 CTLTYPE_STRING, "corename",
826 SYSCTL_DESCR("Core file name"),
827 sysctl_proc_corename, 0, NULL, MAXPATHLEN,
828 CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
829 sysctl_createv(clog, 0, NULL, NULL,
830 CTLFLAG_PERMANENT,
831 CTLTYPE_NODE, "rlimit",
832 SYSCTL_DESCR("Process limits"),
833 NULL, 0, NULL, 0,
834 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
835
836 #define create_proc_plimit(s, n) do { \
837 sysctl_createv(clog, 0, NULL, NULL, \
838 CTLFLAG_PERMANENT, \
839 CTLTYPE_NODE, s, \
840 SYSCTL_DESCR("Process " s " limits"), \
841 NULL, 0, NULL, 0, \
842 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
843 CTL_EOL); \
844 sysctl_createv(clog, 0, NULL, NULL, \
845 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
846 CTLTYPE_QUAD, "soft", \
847 SYSCTL_DESCR("Process soft " s " limit"), \
848 sysctl_proc_plimit, 0, NULL, 0, \
849 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
850 PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
851 sysctl_createv(clog, 0, NULL, NULL, \
852 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
853 CTLTYPE_QUAD, "hard", \
854 SYSCTL_DESCR("Process hard " s " limit"), \
855 sysctl_proc_plimit, 0, NULL, 0, \
856 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
857 PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
858 } while (0/*CONSTCOND*/)
859
860 create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
861 create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
862 create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
863 create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
864 create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
865 create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
866 create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
867 create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
868 create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
869 create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
870
871 #undef create_proc_plimit
872
873 sysctl_createv(clog, 0, NULL, NULL,
874 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
875 CTLTYPE_INT, "stopfork",
876 SYSCTL_DESCR("Stop process at fork(2)"),
877 sysctl_proc_stop, 0, NULL, 0,
878 CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
879 sysctl_createv(clog, 0, NULL, NULL,
880 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
881 CTLTYPE_INT, "stopexec",
882 SYSCTL_DESCR("Stop process at execve(2)"),
883 sysctl_proc_stop, 0, NULL, 0,
884 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
885 sysctl_createv(clog, 0, NULL, NULL,
886 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
887 CTLTYPE_INT, "stopexit",
888 SYSCTL_DESCR("Stop process before completing exit"),
889 sysctl_proc_stop, 0, NULL, 0,
890 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
891 }
892
893 static struct uidinfo *
894 getuidinfo(uid_t uid)
895 {
896 struct uidinfo *uip;
897 struct uihashhead *uipp;
898
899 uipp = UIHASH(uid);
900
901 LIST_FOREACH(uip, uipp, ui_hash)
902 if (uip->ui_uid == uid)
903 return uip;
904 return NULL;
905 }
906
907 static void
908 freeuidinfo(struct uidinfo *uip)
909 {
910 LIST_REMOVE(uip, ui_hash);
911 FREE(uip, M_PROC);
912 }
913
914 static struct uidinfo *
915 allocuidinfo(uid_t uid)
916 {
917 struct uidinfo *uip;
918 struct uihashhead *uipp;
919
920 uipp = UIHASH(uid);
921 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
922 LIST_INSERT_HEAD(uipp, uip, ui_hash);
923 uip->ui_uid = uid;
924 uip->ui_proccnt = 0;
925 uip->ui_sbsize = 0;
926 return uip;
927 }
928
929 /*
930 * Change the count associated with number of processes
931 * a given user is using.
932 */
933 int
934 chgproccnt(uid_t uid, int diff)
935 {
936 struct uidinfo *uip;
937
938 if (diff == 0)
939 return 0;
940
941 if ((uip = getuidinfo(uid)) != NULL) {
942 uip->ui_proccnt += diff;
943 KASSERT(uip->ui_proccnt >= 0);
944 if (uip->ui_proccnt > 0)
945 return uip->ui_proccnt;
946 else {
947 if (uip->ui_sbsize == 0)
948 freeuidinfo(uip);
949 return 0;
950 }
951 } else {
952 if (diff < 0)
953 panic("chgproccnt: lost user %lu", (unsigned long)uid);
954 uip = allocuidinfo(uid);
955 uip->ui_proccnt = diff;
956 return uip->ui_proccnt;
957 }
958 }
959
960 int
961 chgsbsize(uid_t uid, u_long *hiwat, u_long to, rlim_t max)
962 {
963 struct uidinfo *uip;
964 rlim_t nsb;
965 int rv = 0;
966
967 if ((uip = getuidinfo(uid)) == NULL)
968 uip = allocuidinfo(uid);
969 nsb = uip->ui_sbsize + to - *hiwat;
970 if (to > *hiwat && nsb > max)
971 goto done;
972 *hiwat = to;
973 uip->ui_sbsize = nsb;
974 rv = 1;
975 KASSERT(uip->ui_sbsize >= 0);
976 done:
977 if (uip->ui_sbsize == 0 && uip->ui_proccnt == 0)
978 freeuidinfo(uip);
979 return rv;
980 }
981