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