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