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