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