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