kern_resource.c revision 1.161 1 /* $NetBSD: kern_resource.c,v 1.161 2011/05/01 01:15:18 rmind 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.161 2011/05/01 01:15:18 rmind 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/kmem.h>
49 #include <sys/namei.h>
50 #include <sys/pool.h>
51 #include <sys/proc.h>
52 #include <sys/sysctl.h>
53 #include <sys/timevar.h>
54 #include <sys/kauth.h>
55 #include <sys/atomic.h>
56 #include <sys/mount.h>
57 #include <sys/syscallargs.h>
58 #include <sys/atomic.h>
59
60 #include <uvm/uvm_extern.h>
61
62 /*
63 * Maximum process data and stack limits.
64 * They are variables so they are patchable.
65 */
66 rlim_t maxdmap = MAXDSIZ;
67 rlim_t maxsmap = MAXSSIZ;
68
69 static pool_cache_t plimit_cache;
70 static pool_cache_t pstats_cache;
71
72 static kauth_listener_t resource_listener;
73
74 static void sysctl_proc_setup(void);
75
76 static int
77 resource_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
78 void *arg0, void *arg1, void *arg2, void *arg3)
79 {
80 struct proc *p;
81 int result;
82
83 result = KAUTH_RESULT_DEFER;
84 p = arg0;
85
86 switch (action) {
87 case KAUTH_PROCESS_NICE:
88 if (kauth_cred_geteuid(cred) != kauth_cred_geteuid(p->p_cred) &&
89 kauth_cred_getuid(cred) != kauth_cred_geteuid(p->p_cred)) {
90 break;
91 }
92
93 if ((u_long)arg1 >= p->p_nice)
94 result = KAUTH_RESULT_ALLOW;
95
96 break;
97
98 case KAUTH_PROCESS_RLIMIT: {
99 enum kauth_process_req req;
100
101 req = (enum kauth_process_req)(unsigned long)arg1;
102
103 switch (req) {
104 case KAUTH_REQ_PROCESS_RLIMIT_GET:
105 result = KAUTH_RESULT_ALLOW;
106 break;
107
108 case KAUTH_REQ_PROCESS_RLIMIT_SET: {
109 struct rlimit *new_rlimit;
110 u_long which;
111
112 if ((p != curlwp->l_proc) &&
113 (proc_uidmatch(cred, p->p_cred) != 0))
114 break;
115
116 new_rlimit = arg2;
117 which = (u_long)arg3;
118
119 if (new_rlimit->rlim_max <= p->p_rlimit[which].rlim_max)
120 result = KAUTH_RESULT_ALLOW;
121
122 break;
123 }
124
125 default:
126 break;
127 }
128
129 break;
130 }
131
132 default:
133 break;
134 }
135
136 return result;
137 }
138
139 void
140 resource_init(void)
141 {
142
143 plimit_cache = pool_cache_init(sizeof(struct plimit), 0, 0, 0,
144 "plimitpl", NULL, IPL_NONE, NULL, NULL, NULL);
145 pstats_cache = pool_cache_init(sizeof(struct pstats), 0, 0, 0,
146 "pstatspl", NULL, IPL_NONE, NULL, NULL, NULL);
147
148 resource_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
149 resource_listener_cb, NULL);
150
151 sysctl_proc_setup();
152 }
153
154 /*
155 * Resource controls and accounting.
156 */
157
158 int
159 sys_getpriority(struct lwp *l, const struct sys_getpriority_args *uap,
160 register_t *retval)
161 {
162 /* {
163 syscallarg(int) which;
164 syscallarg(id_t) who;
165 } */
166 struct proc *curp = l->l_proc, *p;
167 int low = NZERO + PRIO_MAX + 1;
168 int who = SCARG(uap, who);
169
170 mutex_enter(proc_lock);
171 switch (SCARG(uap, which)) {
172 case PRIO_PROCESS:
173 p = who ? proc_find(who) : curp;;
174 if (p != NULL)
175 low = p->p_nice;
176 break;
177
178 case PRIO_PGRP: {
179 struct pgrp *pg;
180
181 if (who == 0)
182 pg = curp->p_pgrp;
183 else if ((pg = pgrp_find(who)) == NULL)
184 break;
185 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
186 if (p->p_nice < low)
187 low = p->p_nice;
188 }
189 break;
190 }
191
192 case PRIO_USER:
193 if (who == 0)
194 who = (int)kauth_cred_geteuid(l->l_cred);
195 PROCLIST_FOREACH(p, &allproc) {
196 mutex_enter(p->p_lock);
197 if (kauth_cred_geteuid(p->p_cred) ==
198 (uid_t)who && p->p_nice < low)
199 low = p->p_nice;
200 mutex_exit(p->p_lock);
201 }
202 break;
203
204 default:
205 mutex_exit(proc_lock);
206 return (EINVAL);
207 }
208 mutex_exit(proc_lock);
209
210 if (low == NZERO + PRIO_MAX + 1)
211 return (ESRCH);
212 *retval = low - NZERO;
213 return (0);
214 }
215
216 /* ARGSUSED */
217 int
218 sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap,
219 register_t *retval)
220 {
221 /* {
222 syscallarg(int) which;
223 syscallarg(id_t) who;
224 syscallarg(int) prio;
225 } */
226 struct proc *curp = l->l_proc, *p;
227 int found = 0, error = 0;
228 int who = SCARG(uap, who);
229
230 mutex_enter(proc_lock);
231 switch (SCARG(uap, which)) {
232 case PRIO_PROCESS:
233 p = who ? proc_find(who) : curp;
234 if (p != NULL) {
235 mutex_enter(p->p_lock);
236 error = donice(l, p, SCARG(uap, prio));
237 mutex_exit(p->p_lock);
238 found++;
239 }
240 break;
241
242 case PRIO_PGRP: {
243 struct pgrp *pg;
244
245 if (who == 0)
246 pg = curp->p_pgrp;
247 else if ((pg = pgrp_find(who)) == NULL)
248 break;
249 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
250 mutex_enter(p->p_lock);
251 error = donice(l, p, SCARG(uap, prio));
252 mutex_exit(p->p_lock);
253 found++;
254 }
255 break;
256 }
257
258 case PRIO_USER:
259 if (who == 0)
260 who = (int)kauth_cred_geteuid(l->l_cred);
261 PROCLIST_FOREACH(p, &allproc) {
262 mutex_enter(p->p_lock);
263 if (kauth_cred_geteuid(p->p_cred) ==
264 (uid_t)SCARG(uap, who)) {
265 error = donice(l, p, SCARG(uap, prio));
266 found++;
267 }
268 mutex_exit(p->p_lock);
269 }
270 break;
271
272 default:
273 mutex_exit(proc_lock);
274 return EINVAL;
275 }
276 mutex_exit(proc_lock);
277 if (found == 0)
278 return (ESRCH);
279 return (error);
280 }
281
282 /*
283 * Renice a process.
284 *
285 * Call with the target process' credentials locked.
286 */
287 int
288 donice(struct lwp *l, struct proc *chgp, int n)
289 {
290 kauth_cred_t cred = l->l_cred;
291
292 KASSERT(mutex_owned(chgp->p_lock));
293
294 if (kauth_cred_geteuid(cred) && kauth_cred_getuid(cred) &&
295 kauth_cred_geteuid(cred) != kauth_cred_geteuid(chgp->p_cred) &&
296 kauth_cred_getuid(cred) != kauth_cred_geteuid(chgp->p_cred))
297 return (EPERM);
298
299 if (n > PRIO_MAX)
300 n = PRIO_MAX;
301 if (n < PRIO_MIN)
302 n = PRIO_MIN;
303 n += NZERO;
304 if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp,
305 KAUTH_ARG(n), NULL, NULL))
306 return (EACCES);
307 sched_nice(chgp, n);
308 return (0);
309 }
310
311 /* ARGSUSED */
312 int
313 sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap,
314 register_t *retval)
315 {
316 /* {
317 syscallarg(int) which;
318 syscallarg(const struct rlimit *) rlp;
319 } */
320 int which = SCARG(uap, which);
321 struct rlimit alim;
322 int error;
323
324 error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
325 if (error)
326 return (error);
327 return (dosetrlimit(l, l->l_proc, which, &alim));
328 }
329
330 int
331 dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp)
332 {
333 struct rlimit *alimp;
334 int error;
335
336 if ((u_int)which >= RLIM_NLIMITS)
337 return (EINVAL);
338
339 if (limp->rlim_cur > limp->rlim_max) {
340 /*
341 * This is programming error. According to SUSv2, we should
342 * return error in this case.
343 */
344 return (EINVAL);
345 }
346
347 alimp = &p->p_rlimit[which];
348 /* if we don't change the value, no need to limcopy() */
349 if (limp->rlim_cur == alimp->rlim_cur &&
350 limp->rlim_max == alimp->rlim_max)
351 return 0;
352
353 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
354 p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which));
355 if (error)
356 return (error);
357
358 lim_privatise(p);
359 /* p->p_limit is now unchangeable */
360 alimp = &p->p_rlimit[which];
361
362 switch (which) {
363
364 case RLIMIT_DATA:
365 if (limp->rlim_cur > maxdmap)
366 limp->rlim_cur = maxdmap;
367 if (limp->rlim_max > maxdmap)
368 limp->rlim_max = maxdmap;
369 break;
370
371 case RLIMIT_STACK:
372 if (limp->rlim_cur > maxsmap)
373 limp->rlim_cur = maxsmap;
374 if (limp->rlim_max > maxsmap)
375 limp->rlim_max = maxsmap;
376
377 /*
378 * Return EINVAL if the new stack size limit is lower than
379 * current usage. Otherwise, the process would get SIGSEGV the
380 * moment it would try to access anything on it's current stack.
381 * This conforms to SUSv2.
382 */
383 if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
384 || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
385 return (EINVAL);
386 }
387
388 /*
389 * Stack is allocated to the max at exec time with
390 * only "rlim_cur" bytes accessible (In other words,
391 * allocates stack dividing two contiguous regions at
392 * "rlim_cur" bytes boundary).
393 *
394 * Since allocation is done in terms of page, roundup
395 * "rlim_cur" (otherwise, contiguous regions
396 * overlap). If stack limit is going up make more
397 * accessible, if going down make inaccessible.
398 */
399 limp->rlim_cur = round_page(limp->rlim_cur);
400 if (limp->rlim_cur != alimp->rlim_cur) {
401 vaddr_t addr;
402 vsize_t size;
403 vm_prot_t prot;
404
405 if (limp->rlim_cur > alimp->rlim_cur) {
406 prot = VM_PROT_READ | VM_PROT_WRITE;
407 size = limp->rlim_cur - alimp->rlim_cur;
408 addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
409 limp->rlim_cur;
410 } else {
411 prot = VM_PROT_NONE;
412 size = alimp->rlim_cur - limp->rlim_cur;
413 addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
414 alimp->rlim_cur;
415 }
416 (void) uvm_map_protect(&p->p_vmspace->vm_map,
417 addr, addr+size, prot, false);
418 }
419 break;
420
421 case RLIMIT_NOFILE:
422 if (limp->rlim_cur > maxfiles)
423 limp->rlim_cur = maxfiles;
424 if (limp->rlim_max > maxfiles)
425 limp->rlim_max = maxfiles;
426 break;
427
428 case RLIMIT_NPROC:
429 if (limp->rlim_cur > maxproc)
430 limp->rlim_cur = maxproc;
431 if (limp->rlim_max > maxproc)
432 limp->rlim_max = maxproc;
433 break;
434 }
435
436 mutex_enter(&p->p_limit->pl_lock);
437 *alimp = *limp;
438 mutex_exit(&p->p_limit->pl_lock);
439 return (0);
440 }
441
442 /* ARGSUSED */
443 int
444 sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap,
445 register_t *retval)
446 {
447 /* {
448 syscallarg(int) which;
449 syscallarg(struct rlimit *) rlp;
450 } */
451 struct proc *p = l->l_proc;
452 int which = SCARG(uap, which);
453 struct rlimit rl;
454
455 if ((u_int)which >= RLIM_NLIMITS)
456 return (EINVAL);
457
458 mutex_enter(p->p_lock);
459 memcpy(&rl, &p->p_rlimit[which], sizeof(rl));
460 mutex_exit(p->p_lock);
461
462 return copyout(&rl, SCARG(uap, rlp), sizeof(rl));
463 }
464
465 /*
466 * Transform the running time and tick information in proc p into user,
467 * system, and interrupt time usage.
468 *
469 * Should be called with p->p_lock held unless called from exit1().
470 */
471 void
472 calcru(struct proc *p, struct timeval *up, struct timeval *sp,
473 struct timeval *ip, struct timeval *rp)
474 {
475 uint64_t u, st, ut, it, tot;
476 struct lwp *l;
477 struct bintime tm;
478 struct timeval tv;
479
480 mutex_spin_enter(&p->p_stmutex);
481 st = p->p_sticks;
482 ut = p->p_uticks;
483 it = p->p_iticks;
484 mutex_spin_exit(&p->p_stmutex);
485
486 tm = p->p_rtime;
487
488 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
489 lwp_lock(l);
490 bintime_add(&tm, &l->l_rtime);
491 if ((l->l_pflag & LP_RUNNING) != 0) {
492 struct bintime diff;
493 /*
494 * Adjust for the current time slice. This is
495 * actually fairly important since the error
496 * here is on the order of a time quantum,
497 * which is much greater than the sampling
498 * error.
499 */
500 binuptime(&diff);
501 bintime_sub(&diff, &l->l_stime);
502 bintime_add(&tm, &diff);
503 }
504 lwp_unlock(l);
505 }
506
507 tot = st + ut + it;
508 bintime2timeval(&tm, &tv);
509 u = (uint64_t)tv.tv_sec * 1000000ul + tv.tv_usec;
510
511 if (tot == 0) {
512 /* No ticks, so can't use to share time out, split 50-50 */
513 st = ut = u / 2;
514 } else {
515 st = (u * st) / tot;
516 ut = (u * ut) / tot;
517 }
518 if (sp != NULL) {
519 sp->tv_sec = st / 1000000;
520 sp->tv_usec = st % 1000000;
521 }
522 if (up != NULL) {
523 up->tv_sec = ut / 1000000;
524 up->tv_usec = ut % 1000000;
525 }
526 if (ip != NULL) {
527 if (it != 0)
528 it = (u * it) / tot;
529 ip->tv_sec = it / 1000000;
530 ip->tv_usec = it % 1000000;
531 }
532 if (rp != NULL) {
533 *rp = tv;
534 }
535 }
536
537 /* ARGSUSED */
538 int
539 sys___getrusage50(struct lwp *l, const struct sys___getrusage50_args *uap,
540 register_t *retval)
541 {
542 /* {
543 syscallarg(int) who;
544 syscallarg(struct rusage *) rusage;
545 } */
546 struct rusage ru;
547 struct proc *p = l->l_proc;
548
549 switch (SCARG(uap, who)) {
550 case RUSAGE_SELF:
551 mutex_enter(p->p_lock);
552 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
553 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
554 rulwps(p, &ru);
555 mutex_exit(p->p_lock);
556 break;
557
558 case RUSAGE_CHILDREN:
559 mutex_enter(p->p_lock);
560 memcpy(&ru, &p->p_stats->p_cru, sizeof(ru));
561 mutex_exit(p->p_lock);
562 break;
563
564 default:
565 return EINVAL;
566 }
567
568 return copyout(&ru, SCARG(uap, rusage), sizeof(ru));
569 }
570
571 void
572 ruadd(struct rusage *ru, struct rusage *ru2)
573 {
574 long *ip, *ip2;
575 int i;
576
577 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
578 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
579 if (ru->ru_maxrss < ru2->ru_maxrss)
580 ru->ru_maxrss = ru2->ru_maxrss;
581 ip = &ru->ru_first; ip2 = &ru2->ru_first;
582 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
583 *ip++ += *ip2++;
584 }
585
586 void
587 rulwps(proc_t *p, struct rusage *ru)
588 {
589 lwp_t *l;
590
591 KASSERT(mutex_owned(p->p_lock));
592
593 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
594 ruadd(ru, &l->l_ru);
595 ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
596 ru->ru_nivcsw += l->l_nivcsw;
597 }
598 }
599
600 /*
601 * lim_copy: make a copy of the plimit structure.
602 *
603 * We use copy-on-write after fork, and copy when a limit is changed.
604 */
605 struct plimit *
606 lim_copy(struct plimit *lim)
607 {
608 struct plimit *newlim;
609 char *corename;
610 size_t alen, len;
611
612 newlim = pool_cache_get(plimit_cache, PR_WAITOK);
613 mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE);
614 newlim->pl_writeable = false;
615 newlim->pl_refcnt = 1;
616 newlim->pl_sv_limit = NULL;
617
618 mutex_enter(&lim->pl_lock);
619 memcpy(newlim->pl_rlimit, lim->pl_rlimit,
620 sizeof(struct rlimit) * RLIM_NLIMITS);
621
622 /*
623 * Note: the common case is a use of default core name.
624 */
625 alen = 0;
626 corename = NULL;
627 for (;;) {
628 if (lim->pl_corename == defcorename) {
629 newlim->pl_corename = defcorename;
630 newlim->pl_cnlen = 0;
631 break;
632 }
633 len = lim->pl_cnlen;
634 if (len == alen) {
635 newlim->pl_corename = corename;
636 newlim->pl_cnlen = len;
637 memcpy(corename, lim->pl_corename, len);
638 corename = NULL;
639 break;
640 }
641 mutex_exit(&lim->pl_lock);
642 if (corename) {
643 kmem_free(corename, alen);
644 }
645 alen = len;
646 corename = kmem_alloc(alen, KM_SLEEP);
647 mutex_enter(&lim->pl_lock);
648 }
649 mutex_exit(&lim->pl_lock);
650
651 if (corename) {
652 kmem_free(corename, alen);
653 }
654 return newlim;
655 }
656
657 void
658 lim_addref(struct plimit *lim)
659 {
660 atomic_inc_uint(&lim->pl_refcnt);
661 }
662
663 /*
664 * lim_privatise: give a process its own private plimit structure.
665 */
666 void
667 lim_privatise(proc_t *p)
668 {
669 struct plimit *lim = p->p_limit, *newlim;
670
671 if (lim->pl_writeable) {
672 return;
673 }
674
675 newlim = lim_copy(lim);
676
677 mutex_enter(p->p_lock);
678 if (p->p_limit->pl_writeable) {
679 /* Other thread won the race. */
680 mutex_exit(p->p_lock);
681 lim_free(newlim);
682 return;
683 }
684
685 /*
686 * Since p->p_limit can be accessed without locked held,
687 * old limit structure must not be deleted yet.
688 */
689 newlim->pl_sv_limit = p->p_limit;
690 newlim->pl_writeable = true;
691 p->p_limit = newlim;
692 mutex_exit(p->p_lock);
693 }
694
695 void
696 lim_setcorename(proc_t *p, char *name, size_t len)
697 {
698 struct plimit *lim;
699 char *oname;
700 size_t olen;
701
702 lim_privatise(p);
703 lim = p->p_limit;
704
705 mutex_enter(&lim->pl_lock);
706 oname = lim->pl_corename;
707 olen = lim->pl_cnlen;
708 lim->pl_corename = name;
709 lim->pl_cnlen = len;
710 mutex_exit(&lim->pl_lock);
711
712 if (oname != defcorename) {
713 kmem_free(oname, olen);
714 }
715 }
716
717 void
718 lim_free(struct plimit *lim)
719 {
720 struct plimit *sv_lim;
721
722 do {
723 if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0) {
724 return;
725 }
726 if (lim->pl_corename != defcorename) {
727 kmem_free(lim->pl_corename, lim->pl_cnlen);
728 }
729 sv_lim = lim->pl_sv_limit;
730 mutex_destroy(&lim->pl_lock);
731 pool_cache_put(plimit_cache, lim);
732 } while ((lim = sv_lim) != NULL);
733 }
734
735 struct pstats *
736 pstatscopy(struct pstats *ps)
737 {
738
739 struct pstats *newps;
740
741 newps = pool_cache_get(pstats_cache, PR_WAITOK);
742
743 memset(&newps->pstat_startzero, 0,
744 (unsigned) ((char *)&newps->pstat_endzero -
745 (char *)&newps->pstat_startzero));
746 memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
747 ((char *)&newps->pstat_endcopy -
748 (char *)&newps->pstat_startcopy));
749
750 return (newps);
751
752 }
753
754 void
755 pstatsfree(struct pstats *ps)
756 {
757
758 pool_cache_put(pstats_cache, ps);
759 }
760
761 /*
762 * sysctl interface in five parts
763 */
764
765 /*
766 * sysctl_proc_findproc: a routine for sysctl proc subtree helpers that
767 * need to pick a valid process by PID.
768 *
769 * => Hold a reference on the process, on success.
770 */
771 static int
772 sysctl_proc_findproc(lwp_t *l, pid_t pid, proc_t **p2)
773 {
774 proc_t *p;
775 int error;
776
777 if (pid == PROC_CURPROC) {
778 p = l->l_proc;
779 } else {
780 mutex_enter(proc_lock);
781 p = proc_find(pid);
782 if (p == NULL) {
783 mutex_exit(proc_lock);
784 return ESRCH;
785 }
786 }
787 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
788 if (pid != PROC_CURPROC) {
789 mutex_exit(proc_lock);
790 }
791 *p2 = p;
792 return error;
793 }
794
795 /*
796 * sysctl_proc_corename: helper routine to get or set the core file name
797 * for a process specified by PID.
798 */
799 static int
800 sysctl_proc_corename(SYSCTLFN_ARGS)
801 {
802 struct proc *p;
803 struct plimit *lim;
804 char *cnbuf, *cname;
805 struct sysctlnode node;
806 size_t len;
807 int error;
808
809 /* First, validate the request. */
810 if (namelen != 0 || name[-1] != PROC_PID_CORENAME)
811 return EINVAL;
812
813 /* Find the process. Hold a reference (p_reflock), if found. */
814 error = sysctl_proc_findproc(l, (pid_t)name[-2], &p);
815 if (error)
816 return error;
817
818 /* XXX-elad */
819 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, p,
820 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
821 if (error) {
822 rw_exit(&p->p_reflock);
823 return error;
824 }
825
826 cnbuf = PNBUF_GET();
827
828 if (newp == NULL) {
829 /* Get case: copy the core name into the buffer. */
830 error = kauth_authorize_process(l->l_cred,
831 KAUTH_PROCESS_CORENAME, p,
832 KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
833 if (error) {
834 goto done;
835 }
836 lim = p->p_limit;
837 mutex_enter(&lim->pl_lock);
838 strlcpy(cnbuf, lim->pl_corename, MAXPATHLEN);
839 mutex_exit(&lim->pl_lock);
840 } else {
841 /* Set case: just use the temporary buffer. */
842 error = kauth_authorize_process(l->l_cred,
843 KAUTH_PROCESS_CORENAME, p,
844 KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cnbuf, NULL);
845 if (error) {
846 goto done;
847 }
848 }
849
850 node = *rnode;
851 node.sysctl_data = cnbuf;
852 error = sysctl_lookup(SYSCTLFN_CALL(&node));
853
854 /* Return if error, or if we are only retrieving the core name. */
855 if (error || newp == NULL) {
856 goto done;
857 }
858
859 /*
860 * Validate new core name. It must be either "core", "/core",
861 * or end in ".core".
862 */
863 len = strlen(cnbuf);
864 if ((len < 4 || strcmp(cnbuf + len - 4, "core") != 0) ||
865 (len > 4 && cnbuf[len - 5] != '/' && cnbuf[len - 5] != '.')) {
866 error = EINVAL;
867 goto done;
868 }
869
870 /* Allocate, copy and set the new core name for plimit structure. */
871 cname = kmem_alloc(++len, KM_NOSLEEP);
872 if (cname == NULL) {
873 error = ENOMEM;
874 goto done;
875 }
876 memcpy(cname, cnbuf, len);
877 lim_setcorename(p, cname, len);
878 done:
879 rw_exit(&p->p_reflock);
880 PNBUF_PUT(cnbuf);
881 return error;
882 }
883
884 /*
885 * sysctl helper routine for checking/setting a process's stop flags,
886 * one for fork and one for exec.
887 */
888 static int
889 sysctl_proc_stop(SYSCTLFN_ARGS)
890 {
891 struct proc *ptmp;
892 int i, f, error = 0;
893 struct sysctlnode node;
894
895 if (namelen != 0)
896 return (EINVAL);
897
898 /* Find the process. Hold a reference (p_reflock), if found. */
899 error = sysctl_proc_findproc(l, (pid_t)name[-2], &ptmp);
900 if (error)
901 return error;
902
903 /* XXX-elad */
904 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
905 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
906 if (error)
907 goto out;
908
909 switch (rnode->sysctl_num) {
910 case PROC_PID_STOPFORK:
911 f = PS_STOPFORK;
912 break;
913 case PROC_PID_STOPEXEC:
914 f = PS_STOPEXEC;
915 break;
916 case PROC_PID_STOPEXIT:
917 f = PS_STOPEXIT;
918 break;
919 default:
920 error = EINVAL;
921 goto out;
922 }
923
924 i = (ptmp->p_flag & f) ? 1 : 0;
925 node = *rnode;
926 node.sysctl_data = &i;
927 error = sysctl_lookup(SYSCTLFN_CALL(&node));
928 if (error || newp == NULL)
929 goto out;
930
931 mutex_enter(ptmp->p_lock);
932 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
933 ptmp, KAUTH_ARG(f), NULL, NULL);
934 if (!error) {
935 if (i) {
936 ptmp->p_sflag |= f;
937 } else {
938 ptmp->p_sflag &= ~f;
939 }
940 }
941 mutex_exit(ptmp->p_lock);
942 out:
943 rw_exit(&ptmp->p_reflock);
944 return error;
945 }
946
947 /*
948 * sysctl helper routine for a process's rlimits as exposed by sysctl.
949 */
950 static int
951 sysctl_proc_plimit(SYSCTLFN_ARGS)
952 {
953 struct proc *ptmp;
954 u_int limitno;
955 int which, error = 0;
956 struct rlimit alim;
957 struct sysctlnode node;
958
959 if (namelen != 0)
960 return (EINVAL);
961
962 which = name[-1];
963 if (which != PROC_PID_LIMIT_TYPE_SOFT &&
964 which != PROC_PID_LIMIT_TYPE_HARD)
965 return (EINVAL);
966
967 limitno = name[-2] - 1;
968 if (limitno >= RLIM_NLIMITS)
969 return (EINVAL);
970
971 if (name[-3] != PROC_PID_LIMIT)
972 return (EINVAL);
973
974 /* Find the process. Hold a reference (p_reflock), if found. */
975 error = sysctl_proc_findproc(l, (pid_t)name[-4], &ptmp);
976 if (error)
977 return error;
978
979 /* XXX-elad */
980 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
981 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
982 if (error)
983 goto out;
984
985 /* Check if we can view limits. */
986 if (newp == NULL) {
987 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
988 ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
989 KAUTH_ARG(which));
990 if (error)
991 goto out;
992 }
993
994 node = *rnode;
995 memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
996 if (which == PROC_PID_LIMIT_TYPE_HARD)
997 node.sysctl_data = &alim.rlim_max;
998 else
999 node.sysctl_data = &alim.rlim_cur;
1000
1001 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1002 if (error || newp == NULL) {
1003 goto out;
1004 }
1005 error = dosetrlimit(l, ptmp, limitno, &alim);
1006 out:
1007 rw_exit(&ptmp->p_reflock);
1008 return error;
1009 }
1010
1011 static struct sysctllog *proc_sysctllog;
1012
1013 /*
1014 * and finally, the actually glue that sticks it to the tree
1015 */
1016 static void
1017 sysctl_proc_setup()
1018 {
1019
1020 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1021 CTLFLAG_PERMANENT,
1022 CTLTYPE_NODE, "proc", NULL,
1023 NULL, 0, NULL, 0,
1024 CTL_PROC, CTL_EOL);
1025 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1026 CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
1027 CTLTYPE_NODE, "curproc",
1028 SYSCTL_DESCR("Per-process settings"),
1029 NULL, 0, NULL, 0,
1030 CTL_PROC, PROC_CURPROC, CTL_EOL);
1031
1032 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1033 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1034 CTLTYPE_STRING, "corename",
1035 SYSCTL_DESCR("Core file name"),
1036 sysctl_proc_corename, 0, NULL, MAXPATHLEN,
1037 CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
1038 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1039 CTLFLAG_PERMANENT,
1040 CTLTYPE_NODE, "rlimit",
1041 SYSCTL_DESCR("Process limits"),
1042 NULL, 0, NULL, 0,
1043 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
1044
1045 #define create_proc_plimit(s, n) do { \
1046 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1047 CTLFLAG_PERMANENT, \
1048 CTLTYPE_NODE, s, \
1049 SYSCTL_DESCR("Process " s " limits"), \
1050 NULL, 0, NULL, 0, \
1051 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1052 CTL_EOL); \
1053 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1054 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1055 CTLTYPE_QUAD, "soft", \
1056 SYSCTL_DESCR("Process soft " s " limit"), \
1057 sysctl_proc_plimit, 0, NULL, 0, \
1058 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1059 PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
1060 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1061 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1062 CTLTYPE_QUAD, "hard", \
1063 SYSCTL_DESCR("Process hard " s " limit"), \
1064 sysctl_proc_plimit, 0, NULL, 0, \
1065 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1066 PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
1067 } while (0/*CONSTCOND*/)
1068
1069 create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
1070 create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
1071 create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
1072 create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
1073 create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
1074 create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
1075 create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
1076 create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
1077 create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
1078 create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
1079 create_proc_plimit("vmemoryuse", PROC_PID_LIMIT_AS);
1080
1081 #undef create_proc_plimit
1082
1083 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1084 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1085 CTLTYPE_INT, "stopfork",
1086 SYSCTL_DESCR("Stop process at fork(2)"),
1087 sysctl_proc_stop, 0, NULL, 0,
1088 CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
1089 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1090 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1091 CTLTYPE_INT, "stopexec",
1092 SYSCTL_DESCR("Stop process at execve(2)"),
1093 sysctl_proc_stop, 0, NULL, 0,
1094 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
1095 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1096 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1097 CTLTYPE_INT, "stopexit",
1098 SYSCTL_DESCR("Stop process before completing exit"),
1099 sysctl_proc_stop, 0, NULL, 0,
1100 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
1101 }
1102