kern_resource.c revision 1.89 1 /* $NetBSD: kern_resource.c,v 1.89 2005/03/23 01:16:44 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.89 2005/03/23 01:16:44 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 struct uihashhead *uihashtbl;
66 u_long uihash; /* size of hash table - 1 */
67 struct simplelock uihashtbl_slock = SIMPLELOCK_INITIALIZER;
68
69
70 /*
71 * Resource controls and accounting.
72 */
73
74 int
75 sys_getpriority(l, v, retval)
76 struct lwp *l;
77 void *v;
78 register_t *retval;
79 {
80 struct sys_getpriority_args /* {
81 syscallarg(int) which;
82 syscallarg(id_t) who;
83 } */ *uap = v;
84 struct proc *curp = l->l_proc, *p;
85 int low = NZERO + PRIO_MAX + 1;
86
87 switch (SCARG(uap, which)) {
88
89 case PRIO_PROCESS:
90 if (SCARG(uap, who) == 0)
91 p = curp;
92 else
93 p = pfind(SCARG(uap, who));
94 if (p == 0)
95 break;
96 low = p->p_nice;
97 break;
98
99 case PRIO_PGRP: {
100 struct pgrp *pg;
101
102 if (SCARG(uap, who) == 0)
103 pg = curp->p_pgrp;
104 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
105 break;
106 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
107 if (p->p_nice < low)
108 low = p->p_nice;
109 }
110 break;
111 }
112
113 case PRIO_USER:
114 if (SCARG(uap, who) == 0)
115 SCARG(uap, who) = curp->p_ucred->cr_uid;
116 proclist_lock_read();
117 PROCLIST_FOREACH(p, &allproc) {
118 if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who) &&
119 p->p_nice < low)
120 low = p->p_nice;
121 }
122 proclist_unlock_read();
123 break;
124
125 default:
126 return (EINVAL);
127 }
128 if (low == NZERO + PRIO_MAX + 1)
129 return (ESRCH);
130 *retval = low - NZERO;
131 return (0);
132 }
133
134 /* ARGSUSED */
135 int
136 sys_setpriority(l, v, retval)
137 struct lwp *l;
138 void *v;
139 register_t *retval;
140 {
141 struct sys_setpriority_args /* {
142 syscallarg(int) which;
143 syscallarg(id_t) who;
144 syscallarg(int) prio;
145 } */ *uap = v;
146 struct proc *curp = l->l_proc, *p;
147 int found = 0, error = 0;
148
149 switch (SCARG(uap, which)) {
150
151 case PRIO_PROCESS:
152 if (SCARG(uap, who) == 0)
153 p = curp;
154 else
155 p = pfind(SCARG(uap, who));
156 if (p == 0)
157 break;
158 error = donice(curp, p, SCARG(uap, prio));
159 found++;
160 break;
161
162 case PRIO_PGRP: {
163 struct pgrp *pg;
164
165 if (SCARG(uap, who) == 0)
166 pg = curp->p_pgrp;
167 else if ((pg = pgfind(SCARG(uap, who))) == NULL)
168 break;
169 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
170 error = donice(curp, p, SCARG(uap, prio));
171 found++;
172 }
173 break;
174 }
175
176 case PRIO_USER:
177 if (SCARG(uap, who) == 0)
178 SCARG(uap, who) = curp->p_ucred->cr_uid;
179 proclist_lock_read();
180 PROCLIST_FOREACH(p, &allproc) {
181 if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who)) {
182 error = donice(curp, p, SCARG(uap, prio));
183 found++;
184 }
185 }
186 proclist_unlock_read();
187 break;
188
189 default:
190 return (EINVAL);
191 }
192 if (found == 0)
193 return (ESRCH);
194 return (error);
195 }
196
197 int
198 donice(curp, chgp, n)
199 struct proc *curp, *chgp;
200 int n;
201 {
202 struct pcred *pcred = curp->p_cred;
203 int s;
204
205 if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
206 pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
207 pcred->p_ruid != chgp->p_ucred->cr_uid)
208 return (EPERM);
209 if (n > PRIO_MAX)
210 n = PRIO_MAX;
211 if (n < PRIO_MIN)
212 n = PRIO_MIN;
213 n += NZERO;
214 if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
215 return (EACCES);
216 chgp->p_nice = n;
217 SCHED_LOCK(s);
218 (void)resetprocpriority(chgp);
219 SCHED_UNLOCK(s);
220 return (0);
221 }
222
223 /* ARGSUSED */
224 int
225 sys_setrlimit(l, v, retval)
226 struct lwp *l;
227 void *v;
228 register_t *retval;
229 {
230 struct sys_setrlimit_args /* {
231 syscallarg(int) which;
232 syscallarg(const struct rlimit *) rlp;
233 } */ *uap = v;
234 struct proc *p = l->l_proc;
235 int which = SCARG(uap, which);
236 struct rlimit alim;
237 int error;
238
239 error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
240 if (error)
241 return (error);
242 return (dosetrlimit(p, p->p_cred, which, &alim));
243 }
244
245 int
246 dosetrlimit(p, cred, which, limp)
247 struct proc *p;
248 struct pcred *cred;
249 int which;
250 struct rlimit *limp;
251 {
252 struct rlimit *alimp;
253 struct plimit *oldplim;
254 int error;
255
256 if ((u_int)which >= RLIM_NLIMITS)
257 return (EINVAL);
258
259 if (limp->rlim_cur < 0 || limp->rlim_max < 0)
260 return (EINVAL);
261
262 alimp = &p->p_rlimit[which];
263 /* if we don't change the value, no need to limcopy() */
264 if (limp->rlim_cur == alimp->rlim_cur &&
265 limp->rlim_max == alimp->rlim_max)
266 return 0;
267
268 if (limp->rlim_cur > limp->rlim_max) {
269 /*
270 * This is programming error. According to SUSv2, we should
271 * return error in this case.
272 */
273 return (EINVAL);
274 }
275 if (limp->rlim_max > alimp->rlim_max
276 && (error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
277 return (error);
278
279 if (p->p_limit->p_refcnt > 1 &&
280 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
281 p->p_limit = limcopy(oldplim = p->p_limit);
282 limfree(oldplim);
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 = 0;
506
507 simple_lock(&lim->p_slock);
508 if (lim->pl_corename != defcorename)
509 l = strlen(lim->pl_corename) + 1;
510 simple_unlock(&lim->p_slock);
511
512 newlim = pool_get(&plimit_pool, PR_WAITOK);
513 simple_lock_init(&newlim->p_slock);
514 newlim->p_lflags = 0;
515 newlim->p_refcnt = 1;
516 newlim->pl_corename = (l != 0)
517 ? malloc(l, M_TEMP, M_WAITOK)
518 : defcorename;
519
520 simple_lock(&lim->p_slock);
521 memcpy(newlim->pl_rlimit, lim->pl_rlimit,
522 sizeof(struct rlimit) * RLIM_NLIMITS);
523
524 if (l != 0)
525 strlcpy(newlim->pl_corename, lim->pl_corename, l);
526 simple_unlock(&lim->p_slock);
527
528 return (newlim);
529 }
530
531 void
532 limfree(lim)
533 struct plimit *lim;
534 {
535 int n;
536
537 simple_lock(&lim->p_slock);
538 n = --lim->p_refcnt;
539 simple_unlock(&lim->p_slock);
540 if (n > 0)
541 return;
542 #ifdef DIAGNOSTIC
543 if (n < 0)
544 panic("limfree");
545 #endif
546 if (lim->pl_corename != defcorename)
547 free(lim->pl_corename, M_TEMP);
548 pool_put(&plimit_pool, lim);
549 }
550
551 struct pstats *
552 pstatscopy(ps)
553 struct pstats *ps;
554 {
555
556 struct pstats *newps;
557
558 newps = pool_get(&pstats_pool, PR_WAITOK);
559
560 memset(&newps->pstat_startzero, 0,
561 (unsigned) ((caddr_t)&newps->pstat_endzero -
562 (caddr_t)&newps->pstat_startzero));
563 memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
564 ((caddr_t)&newps->pstat_endcopy -
565 (caddr_t)&newps->pstat_startcopy));
566
567 return (newps);
568
569 }
570
571 void
572 pstatsfree(ps)
573 struct pstats *ps;
574 {
575
576 pool_put(&pstats_pool, ps);
577 }
578
579 /*
580 * sysctl interface in five parts
581 */
582
583 /*
584 * a routine for sysctl proc subtree helpers that need to pick a valid
585 * process by pid.
586 */
587 static int
588 sysctl_proc_findproc(struct proc *p, struct proc **p2, pid_t pid)
589 {
590 struct proc *ptmp;
591 int i, error = 0;
592
593 if (pid == PROC_CURPROC)
594 ptmp = p;
595 else if ((ptmp = pfind(pid)) == NULL)
596 error = ESRCH;
597 else {
598 /*
599 * suid proc of ours or proc not ours
600 */
601 if (p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
602 p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
603 error = suser(p->p_ucred, &p->p_acflag);
604
605 /*
606 * sgid proc has sgid back to us temporarily
607 */
608 else if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
609 error = suser(p->p_ucred, &p->p_acflag);
610
611 /*
612 * our rgid must be in target's group list (ie,
613 * sub-processes started by a sgid process)
614 */
615 else {
616 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
617 if (p->p_ucred->cr_groups[i] ==
618 ptmp->p_cred->p_rgid)
619 break;
620 }
621 if (i == p->p_ucred->cr_ngroups)
622 error = suser(p->p_ucred, &p->p_acflag);
623 }
624 }
625
626 *p2 = ptmp;
627 return (error);
628 }
629
630 /*
631 * sysctl helper routine for setting a process's specific corefile
632 * name. picks the process based on the given pid and checks the
633 * correctness of the new value.
634 */
635 static int
636 sysctl_proc_corename(SYSCTLFN_ARGS)
637 {
638 struct proc *ptmp, *p;
639 struct plimit *lim;
640 int error = 0, len;
641 char cname[MAXPATHLEN], *tmp;
642 struct sysctlnode node;
643
644 /*
645 * is this all correct?
646 */
647 if (namelen != 0)
648 return (EINVAL);
649 if (name[-1] != PROC_PID_CORENAME)
650 return (EINVAL);
651
652 /*
653 * whom are we tweaking?
654 */
655 p = l->l_proc;
656 error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
657 if (error)
658 return (error);
659
660 /*
661 * let them modify a temporary copy of the core name
662 */
663 node = *rnode;
664 strlcpy(cname, ptmp->p_limit->pl_corename, sizeof(cname));
665 node.sysctl_data = cname;
666 error = sysctl_lookup(SYSCTLFN_CALL(&node));
667
668 /*
669 * if that failed, or they have nothing new to say, or we've
670 * heard it before...
671 */
672 if (error || newp == NULL ||
673 strcmp(cname, ptmp->p_limit->pl_corename) == 0)
674 return (error);
675
676 /*
677 * no error yet and cname now has the new core name in it.
678 * let's see if it looks acceptable. it must be either "core"
679 * or end in ".core" or "/core".
680 */
681 len = strlen(cname);
682 if (len < 4)
683 return (EINVAL);
684 if (strcmp(cname + len - 4, "core") != 0)
685 return (EINVAL);
686 if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.')
687 return (EINVAL);
688
689 /*
690 * hmm...looks good. now...where do we put it?
691 */
692 tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
693 if (tmp == NULL)
694 return (ENOMEM);
695 strlcpy(tmp, cname, len + 1);
696
697 lim = ptmp->p_limit;
698 if (lim->p_refcnt > 1 && (lim->p_lflags & PL_SHAREMOD) == 0) {
699 ptmp->p_limit = limcopy(lim);
700 limfree(lim);
701 lim = ptmp->p_limit;
702 }
703 if (lim->pl_corename != defcorename)
704 free(lim->pl_corename, M_TEMP);
705 lim->pl_corename = tmp;
706
707 return (error);
708 }
709
710 /*
711 * sysctl helper routine for checking/setting a process's stop flags,
712 * one for fork and one for exec.
713 */
714 static int
715 sysctl_proc_stop(SYSCTLFN_ARGS)
716 {
717 struct proc *p, *ptmp;
718 int i, f, error = 0;
719 struct sysctlnode node;
720
721 if (namelen != 0)
722 return (EINVAL);
723
724 p = l->l_proc;
725 error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
726 if (error)
727 return (error);
728
729 switch (rnode->sysctl_num) {
730 case PROC_PID_STOPFORK:
731 f = P_STOPFORK;
732 break;
733 case PROC_PID_STOPEXEC:
734 f = P_STOPEXEC;
735 break;
736 case PROC_PID_STOPEXIT:
737 f = P_STOPEXIT;
738 break;
739 default:
740 return (EINVAL);
741 }
742
743 i = (ptmp->p_flag & f) ? 1 : 0;
744 node = *rnode;
745 node.sysctl_data = &i;
746 error = sysctl_lookup(SYSCTLFN_CALL(&node));
747 if (error || newp == NULL)
748 return (error);
749
750 if (i)
751 ptmp->p_flag |= f;
752 else
753 ptmp->p_flag &= ~f;
754
755 return (0);
756 }
757
758 /*
759 * sysctl helper routine for a process's rlimits as exposed by sysctl.
760 */
761 static int
762 sysctl_proc_plimit(SYSCTLFN_ARGS)
763 {
764 struct proc *ptmp, *p;
765 u_int limitno;
766 int which, error = 0;
767 struct rlimit alim;
768 struct sysctlnode node;
769
770 if (namelen != 0)
771 return (EINVAL);
772
773 which = name[-1];
774 if (which != PROC_PID_LIMIT_TYPE_SOFT &&
775 which != PROC_PID_LIMIT_TYPE_HARD)
776 return (EINVAL);
777
778 limitno = name[-2] - 1;
779 if (limitno >= RLIM_NLIMITS)
780 return (EINVAL);
781
782 if (name[-3] != PROC_PID_LIMIT)
783 return (EINVAL);
784
785 p = l->l_proc;
786 error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-4]);
787 if (error)
788 return (error);
789
790 node = *rnode;
791 memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
792 if (which == PROC_PID_LIMIT_TYPE_HARD)
793 node.sysctl_data = &alim.rlim_max;
794 else
795 node.sysctl_data = &alim.rlim_cur;
796
797 error = sysctl_lookup(SYSCTLFN_CALL(&node));
798 if (error || newp == NULL)
799 return (error);
800
801 return (dosetrlimit(ptmp, p->p_cred, limitno, &alim));
802 }
803
804 /*
805 * and finally, the actually glue that sticks it to the tree
806 */
807 SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
808 {
809
810 sysctl_createv(clog, 0, NULL, NULL,
811 CTLFLAG_PERMANENT,
812 CTLTYPE_NODE, "proc", NULL,
813 NULL, 0, NULL, 0,
814 CTL_PROC, CTL_EOL);
815 sysctl_createv(clog, 0, NULL, NULL,
816 CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
817 CTLTYPE_NODE, "curproc",
818 SYSCTL_DESCR("Per-process settings"),
819 NULL, 0, NULL, 0,
820 CTL_PROC, PROC_CURPROC, CTL_EOL);
821
822 sysctl_createv(clog, 0, NULL, NULL,
823 CTLFLAG_PERMANENT|CTLFLAG_READONLY2|CTLFLAG_ANYWRITE,
824 CTLTYPE_STRING, "corename",
825 SYSCTL_DESCR("Core file name"),
826 sysctl_proc_corename, 0, NULL, MAXPATHLEN,
827 CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
828 sysctl_createv(clog, 0, NULL, NULL,
829 CTLFLAG_PERMANENT,
830 CTLTYPE_NODE, "rlimit",
831 SYSCTL_DESCR("Process limits"),
832 NULL, 0, NULL, 0,
833 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
834
835 #define create_proc_plimit(s, n) do { \
836 sysctl_createv(clog, 0, NULL, NULL, \
837 CTLFLAG_PERMANENT, \
838 CTLTYPE_NODE, s, \
839 SYSCTL_DESCR("Process " s " limits"), \
840 NULL, 0, NULL, 0, \
841 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
842 CTL_EOL); \
843 sysctl_createv(clog, 0, NULL, NULL, \
844 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
845 CTLTYPE_QUAD, "soft", \
846 SYSCTL_DESCR("Process soft " s " limit"), \
847 sysctl_proc_plimit, 0, NULL, 0, \
848 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
849 PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
850 sysctl_createv(clog, 0, NULL, NULL, \
851 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
852 CTLTYPE_QUAD, "hard", \
853 SYSCTL_DESCR("Process hard " s " limit"), \
854 sysctl_proc_plimit, 0, NULL, 0, \
855 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
856 PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
857 } while (0/*CONSTCOND*/)
858
859 create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
860 create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
861 create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
862 create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
863 create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
864 create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
865 create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
866 create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
867 create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
868 create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
869
870 #undef create_proc_plimit
871
872 sysctl_createv(clog, 0, NULL, NULL,
873 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
874 CTLTYPE_INT, "stopfork",
875 SYSCTL_DESCR("Stop process at fork(2)"),
876 sysctl_proc_stop, 0, NULL, 0,
877 CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
878 sysctl_createv(clog, 0, NULL, NULL,
879 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
880 CTLTYPE_INT, "stopexec",
881 SYSCTL_DESCR("Stop process at execve(2)"),
882 sysctl_proc_stop, 0, NULL, 0,
883 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
884 sysctl_createv(clog, 0, NULL, NULL,
885 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
886 CTLTYPE_INT, "stopexit",
887 SYSCTL_DESCR("Stop process before completing exit"),
888 sysctl_proc_stop, 0, NULL, 0,
889 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
890 }
891
892 struct uidinfo *
893 uid_find(uid_t uid)
894 {
895 struct uidinfo *uip;
896 struct uihashhead *uipp;
897
898 uipp = UIHASH(uid);
899
900 simple_lock(&uihashtbl_slock);
901 LIST_FOREACH(uip, uipp, ui_hash)
902 if (uip->ui_uid == uid) {
903 simple_unlock(&uihashtbl_slock);
904 return uip;
905 }
906 simple_unlock(&uihashtbl_slock);
907
908 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK|M_ZERO);
909
910 simple_lock(&uihashtbl_slock);
911 LIST_INSERT_HEAD(uipp, uip, ui_hash);
912 uip->ui_uid = uid;
913 simple_unlock(&uihashtbl_slock);
914
915 return uip;
916 }
917
918 /*
919 * Change the count associated with number of processes
920 * a given user is using.
921 */
922 int
923 chgproccnt(uid_t uid, int diff)
924 {
925 struct uidinfo *uip;
926
927 if (diff == 0)
928 return 0;
929
930 uip = uid_find(uid);
931 uip->ui_proccnt += diff;
932 KASSERT(uip->ui_proccnt >= 0);
933 return uip->ui_proccnt;
934 }
935
936 int
937 chgsbsize(uid_t uid, u_long *hiwat, u_long to, rlim_t max)
938 {
939 *hiwat = to;
940 return 1;
941 struct uidinfo *uip;
942 rlim_t nsb;
943
944 uip = uid_find(uid);
945 nsb = uip->ui_sbsize + to - *hiwat;
946 if (to > *hiwat && nsb > max)
947 return 0;
948 *hiwat = to;
949 uip->ui_sbsize = nsb;
950 KASSERT(uip->ui_sbsize >= 0);
951 return 1;
952 }
953