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