kern_resource.c revision 1.154.2.2 1 /* $NetBSD: kern_resource.c,v 1.154.2.2 2010/08/17 06:47:28 uebayasi 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.154.2.2 2010/08/17 06:47:28 uebayasi 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, false);
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 * Make a copy of the plimit structure.
602 * We share these structures copy-on-write after fork,
603 * and copy when a limit is changed.
604 *
605 * Unfortunately (due to PL_SHAREMOD) it is possibly for the structure
606 * we are copying to change beneath our feet!
607 */
608 struct plimit *
609 lim_copy(struct plimit *lim)
610 {
611 struct plimit *newlim;
612 char *corename;
613 size_t alen, len;
614
615 newlim = pool_cache_get(plimit_cache, PR_WAITOK);
616 mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE);
617 newlim->pl_flags = 0;
618 newlim->pl_refcnt = 1;
619 newlim->pl_sv_limit = NULL;
620
621 mutex_enter(&lim->pl_lock);
622 memcpy(newlim->pl_rlimit, lim->pl_rlimit,
623 sizeof(struct rlimit) * RLIM_NLIMITS);
624
625 alen = 0;
626 corename = NULL;
627 for (;;) {
628 if (lim->pl_corename == defcorename) {
629 newlim->pl_corename = defcorename;
630 break;
631 }
632 len = strlen(lim->pl_corename) + 1;
633 if (len <= alen) {
634 newlim->pl_corename = corename;
635 memcpy(corename, lim->pl_corename, len);
636 corename = NULL;
637 break;
638 }
639 mutex_exit(&lim->pl_lock);
640 if (corename != NULL)
641 free(corename, M_TEMP);
642 alen = len;
643 corename = malloc(alen, M_TEMP, M_WAITOK);
644 mutex_enter(&lim->pl_lock);
645 }
646 mutex_exit(&lim->pl_lock);
647 if (corename != NULL)
648 free(corename, M_TEMP);
649 return newlim;
650 }
651
652 void
653 lim_addref(struct plimit *lim)
654 {
655 atomic_inc_uint(&lim->pl_refcnt);
656 }
657
658 /*
659 * Give a process it's own private plimit structure.
660 * This will only be shared (in fork) if modifications are to be shared.
661 */
662 void
663 lim_privatise(struct proc *p, bool set_shared)
664 {
665 struct plimit *lim, *newlim;
666
667 lim = p->p_limit;
668 if (lim->pl_flags & PL_WRITEABLE) {
669 if (set_shared)
670 lim->pl_flags |= PL_SHAREMOD;
671 return;
672 }
673
674 if (set_shared && lim->pl_flags & PL_SHAREMOD)
675 return;
676
677 newlim = lim_copy(lim);
678
679 mutex_enter(p->p_lock);
680 if (p->p_limit->pl_flags & PL_WRITEABLE) {
681 /* Someone crept in while we were busy */
682 mutex_exit(p->p_lock);
683 limfree(newlim);
684 if (set_shared)
685 p->p_limit->pl_flags |= PL_SHAREMOD;
686 return;
687 }
688
689 /*
690 * Since most accesses to p->p_limit aren't locked, we must not
691 * delete the old limit structure yet.
692 */
693 newlim->pl_sv_limit = p->p_limit;
694 newlim->pl_flags |= PL_WRITEABLE;
695 if (set_shared)
696 newlim->pl_flags |= PL_SHAREMOD;
697 p->p_limit = newlim;
698 mutex_exit(p->p_lock);
699 }
700
701 void
702 limfree(struct plimit *lim)
703 {
704 struct plimit *sv_lim;
705
706 do {
707 if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0)
708 return;
709 if (lim->pl_corename != defcorename)
710 free(lim->pl_corename, M_TEMP);
711 sv_lim = lim->pl_sv_limit;
712 mutex_destroy(&lim->pl_lock);
713 pool_cache_put(plimit_cache, lim);
714 } while ((lim = sv_lim) != NULL);
715 }
716
717 struct pstats *
718 pstatscopy(struct pstats *ps)
719 {
720
721 struct pstats *newps;
722
723 newps = pool_cache_get(pstats_cache, PR_WAITOK);
724
725 memset(&newps->pstat_startzero, 0,
726 (unsigned) ((char *)&newps->pstat_endzero -
727 (char *)&newps->pstat_startzero));
728 memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
729 ((char *)&newps->pstat_endcopy -
730 (char *)&newps->pstat_startcopy));
731
732 return (newps);
733
734 }
735
736 void
737 pstatsfree(struct pstats *ps)
738 {
739
740 pool_cache_put(pstats_cache, ps);
741 }
742
743 /*
744 * sysctl interface in five parts
745 */
746
747 /*
748 * sysctl_proc_findproc: a routine for sysctl proc subtree helpers that
749 * need to pick a valid process by PID.
750 *
751 * => Hold a reference on the process, on success.
752 */
753 static int
754 sysctl_proc_findproc(lwp_t *l, pid_t pid, proc_t **p2)
755 {
756 proc_t *p;
757 int error;
758
759 if (pid == PROC_CURPROC) {
760 p = l->l_proc;
761 } else {
762 mutex_enter(proc_lock);
763 p = proc_find(pid);
764 if (p == NULL) {
765 mutex_exit(proc_lock);
766 return ESRCH;
767 }
768 }
769 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
770 if (pid != PROC_CURPROC) {
771 mutex_exit(proc_lock);
772 }
773 *p2 = p;
774 return error;
775 }
776
777 /*
778 * sysctl helper routine for setting a process's specific corefile
779 * name. picks the process based on the given pid and checks the
780 * correctness of the new value.
781 */
782 static int
783 sysctl_proc_corename(SYSCTLFN_ARGS)
784 {
785 struct proc *ptmp;
786 struct plimit *lim;
787 char *cname, *ocore, *tmp;
788 struct sysctlnode node;
789 int error = 0, len;
790
791 /*
792 * is this all correct?
793 */
794 if (namelen != 0)
795 return (EINVAL);
796 if (name[-1] != PROC_PID_CORENAME)
797 return (EINVAL);
798
799 /* Find the process. Hold a reference (p_reflock), if found. */
800 error = sysctl_proc_findproc(l, (pid_t)name[-2], &ptmp);
801 if (error)
802 return error;
803
804 /* XXX-elad */
805 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
806 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
807 if (error) {
808 rw_exit(&ptmp->p_reflock);
809 return error;
810 }
811
812 if (newp == NULL) {
813 error = kauth_authorize_process(l->l_cred,
814 KAUTH_PROCESS_CORENAME, ptmp,
815 KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
816 if (error) {
817 rw_exit(&ptmp->p_reflock);
818 return error;
819 }
820 }
821
822 /*
823 * let them modify a temporary copy of the core name
824 */
825 cname = PNBUF_GET();
826 lim = ptmp->p_limit;
827 mutex_enter(&lim->pl_lock);
828 strlcpy(cname, lim->pl_corename, MAXPATHLEN);
829 mutex_exit(&lim->pl_lock);
830
831 node = *rnode;
832 node.sysctl_data = cname;
833 error = sysctl_lookup(SYSCTLFN_CALL(&node));
834
835 /*
836 * if that failed, or they have nothing new to say, or we've
837 * heard it before...
838 */
839 if (error || newp == NULL)
840 goto done;
841 lim = ptmp->p_limit;
842 mutex_enter(&lim->pl_lock);
843 error = strcmp(cname, lim->pl_corename);
844 mutex_exit(&lim->pl_lock);
845 if (error == 0) {
846 /* Unchanged */
847 goto done;
848 }
849 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CORENAME,
850 ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cname, NULL);
851 if (error)
852 goto done;
853
854 /*
855 * no error yet and cname now has the new core name in it.
856 * let's see if it looks acceptable. it must be either "core"
857 * or end in ".core" or "/core".
858 */
859 len = strlen(cname);
860 if (len < 4) {
861 error = EINVAL;
862 } else if (strcmp(cname + len - 4, "core") != 0) {
863 error = EINVAL;
864 } else if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.') {
865 error = EINVAL;
866 }
867 if (error != 0) {
868 goto done;
869 }
870
871 /*
872 * hmm...looks good. now...where do we put it?
873 */
874 tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
875 if (tmp == NULL) {
876 error = ENOMEM;
877 goto done;
878 }
879 memcpy(tmp, cname, len + 1);
880
881 lim_privatise(ptmp, false);
882 lim = ptmp->p_limit;
883 mutex_enter(&lim->pl_lock);
884 ocore = lim->pl_corename;
885 lim->pl_corename = tmp;
886 mutex_exit(&lim->pl_lock);
887 if (ocore != defcorename)
888 free(ocore, M_TEMP);
889
890 done:
891 rw_exit(&ptmp->p_reflock);
892 PNBUF_PUT(cname);
893 return error;
894 }
895
896 /*
897 * sysctl helper routine for checking/setting a process's stop flags,
898 * one for fork and one for exec.
899 */
900 static int
901 sysctl_proc_stop(SYSCTLFN_ARGS)
902 {
903 struct proc *ptmp;
904 int i, f, error = 0;
905 struct sysctlnode node;
906
907 if (namelen != 0)
908 return (EINVAL);
909
910 /* Find the process. Hold a reference (p_reflock), if found. */
911 error = sysctl_proc_findproc(l, (pid_t)name[-2], &ptmp);
912 if (error)
913 return error;
914
915 /* XXX-elad */
916 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
917 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
918 if (error)
919 goto out;
920
921 switch (rnode->sysctl_num) {
922 case PROC_PID_STOPFORK:
923 f = PS_STOPFORK;
924 break;
925 case PROC_PID_STOPEXEC:
926 f = PS_STOPEXEC;
927 break;
928 case PROC_PID_STOPEXIT:
929 f = PS_STOPEXIT;
930 break;
931 default:
932 error = EINVAL;
933 goto out;
934 }
935
936 i = (ptmp->p_flag & f) ? 1 : 0;
937 node = *rnode;
938 node.sysctl_data = &i;
939 error = sysctl_lookup(SYSCTLFN_CALL(&node));
940 if (error || newp == NULL)
941 goto out;
942
943 mutex_enter(ptmp->p_lock);
944 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
945 ptmp, KAUTH_ARG(f), NULL, NULL);
946 if (!error) {
947 if (i) {
948 ptmp->p_sflag |= f;
949 } else {
950 ptmp->p_sflag &= ~f;
951 }
952 }
953 mutex_exit(ptmp->p_lock);
954 out:
955 rw_exit(&ptmp->p_reflock);
956 return error;
957 }
958
959 /*
960 * sysctl helper routine for a process's rlimits as exposed by sysctl.
961 */
962 static int
963 sysctl_proc_plimit(SYSCTLFN_ARGS)
964 {
965 struct proc *ptmp;
966 u_int limitno;
967 int which, error = 0;
968 struct rlimit alim;
969 struct sysctlnode node;
970
971 if (namelen != 0)
972 return (EINVAL);
973
974 which = name[-1];
975 if (which != PROC_PID_LIMIT_TYPE_SOFT &&
976 which != PROC_PID_LIMIT_TYPE_HARD)
977 return (EINVAL);
978
979 limitno = name[-2] - 1;
980 if (limitno >= RLIM_NLIMITS)
981 return (EINVAL);
982
983 if (name[-3] != PROC_PID_LIMIT)
984 return (EINVAL);
985
986 /* Find the process. Hold a reference (p_reflock), if found. */
987 error = sysctl_proc_findproc(l, (pid_t)name[-4], &ptmp);
988 if (error)
989 return error;
990
991 /* XXX-elad */
992 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, ptmp,
993 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
994 if (error)
995 goto out;
996
997 /* Check if we can view limits. */
998 if (newp == NULL) {
999 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
1000 ptmp, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
1001 KAUTH_ARG(which));
1002 if (error)
1003 goto out;
1004 }
1005
1006 node = *rnode;
1007 memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
1008 if (which == PROC_PID_LIMIT_TYPE_HARD)
1009 node.sysctl_data = &alim.rlim_max;
1010 else
1011 node.sysctl_data = &alim.rlim_cur;
1012
1013 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1014 if (error || newp == NULL) {
1015 goto out;
1016 }
1017 error = dosetrlimit(l, ptmp, limitno, &alim);
1018 out:
1019 rw_exit(&ptmp->p_reflock);
1020 return error;
1021 }
1022
1023 static struct sysctllog *proc_sysctllog;
1024
1025 /*
1026 * and finally, the actually glue that sticks it to the tree
1027 */
1028 static void
1029 sysctl_proc_setup()
1030 {
1031
1032 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1033 CTLFLAG_PERMANENT,
1034 CTLTYPE_NODE, "proc", NULL,
1035 NULL, 0, NULL, 0,
1036 CTL_PROC, CTL_EOL);
1037 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1038 CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
1039 CTLTYPE_NODE, "curproc",
1040 SYSCTL_DESCR("Per-process settings"),
1041 NULL, 0, NULL, 0,
1042 CTL_PROC, PROC_CURPROC, CTL_EOL);
1043
1044 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1045 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1046 CTLTYPE_STRING, "corename",
1047 SYSCTL_DESCR("Core file name"),
1048 sysctl_proc_corename, 0, NULL, MAXPATHLEN,
1049 CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
1050 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1051 CTLFLAG_PERMANENT,
1052 CTLTYPE_NODE, "rlimit",
1053 SYSCTL_DESCR("Process limits"),
1054 NULL, 0, NULL, 0,
1055 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
1056
1057 #define create_proc_plimit(s, n) do { \
1058 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1059 CTLFLAG_PERMANENT, \
1060 CTLTYPE_NODE, s, \
1061 SYSCTL_DESCR("Process " s " limits"), \
1062 NULL, 0, NULL, 0, \
1063 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1064 CTL_EOL); \
1065 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1066 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1067 CTLTYPE_QUAD, "soft", \
1068 SYSCTL_DESCR("Process soft " s " limit"), \
1069 sysctl_proc_plimit, 0, NULL, 0, \
1070 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1071 PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
1072 sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1073 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1074 CTLTYPE_QUAD, "hard", \
1075 SYSCTL_DESCR("Process hard " s " limit"), \
1076 sysctl_proc_plimit, 0, NULL, 0, \
1077 CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1078 PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
1079 } while (0/*CONSTCOND*/)
1080
1081 create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
1082 create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
1083 create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
1084 create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
1085 create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
1086 create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
1087 create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
1088 create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
1089 create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
1090 create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
1091 create_proc_plimit("vmemoryuse", PROC_PID_LIMIT_AS);
1092
1093 #undef create_proc_plimit
1094
1095 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1096 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1097 CTLTYPE_INT, "stopfork",
1098 SYSCTL_DESCR("Stop process at fork(2)"),
1099 sysctl_proc_stop, 0, NULL, 0,
1100 CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
1101 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1102 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1103 CTLTYPE_INT, "stopexec",
1104 SYSCTL_DESCR("Stop process at execve(2)"),
1105 sysctl_proc_stop, 0, NULL, 0,
1106 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
1107 sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1108 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1109 CTLTYPE_INT, "stopexit",
1110 SYSCTL_DESCR("Stop process before completing exit"),
1111 sysctl_proc_stop, 0, NULL, 0,
1112 CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
1113 }
1114