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