kern_proc.c revision 1.175 1 /* $NetBSD: kern_proc.c,v 1.175 2011/04/18 00:26:11 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1989, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.175 2011/04/18 00:26:11 rmind Exp $");
66
67 #ifdef _KERNEL_OPT
68 #include "opt_kstack.h"
69 #include "opt_maxuprc.h"
70 #include "opt_dtrace.h"
71 #include "opt_compat_netbsd32.h"
72 #endif
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/proc.h>
78 #include <sys/resourcevar.h>
79 #include <sys/buf.h>
80 #include <sys/acct.h>
81 #include <sys/wait.h>
82 #include <sys/file.h>
83 #include <ufs/ufs/quota.h>
84 #include <sys/uio.h>
85 #include <sys/pool.h>
86 #include <sys/pset.h>
87 #include <sys/mbuf.h>
88 #include <sys/ioctl.h>
89 #include <sys/tty.h>
90 #include <sys/signalvar.h>
91 #include <sys/ras.h>
92 #include <sys/sa.h>
93 #include <sys/savar.h>
94 #include <sys/filedesc.h>
95 #include "sys/syscall_stats.h"
96 #include <sys/kauth.h>
97 #include <sys/sleepq.h>
98 #include <sys/atomic.h>
99 #include <sys/kmem.h>
100 #include <sys/dtrace_bsd.h>
101 #include <sys/sysctl.h>
102 #include <sys/exec.h>
103 #include <sys/cpu.h>
104
105 #include <uvm/uvm_extern.h>
106 #include <uvm/uvm_extern.h>
107
108 #ifdef COMPAT_NETBSD32
109 #include <compat/netbsd32/netbsd32.h>
110 #endif
111
112 /*
113 * Other process lists
114 */
115
116 struct proclist allproc;
117 struct proclist zombproc; /* resources have been freed */
118
119 kmutex_t *proc_lock;
120
121 /*
122 * pid to proc lookup is done by indexing the pid_table array.
123 * Since pid numbers are only allocated when an empty slot
124 * has been found, there is no need to search any lists ever.
125 * (an orphaned pgrp will lock the slot, a session will lock
126 * the pgrp with the same number.)
127 * If the table is too small it is reallocated with twice the
128 * previous size and the entries 'unzipped' into the two halves.
129 * A linked list of free entries is passed through the pt_proc
130 * field of 'free' items - set odd to be an invalid ptr.
131 */
132
133 struct pid_table {
134 struct proc *pt_proc;
135 struct pgrp *pt_pgrp;
136 pid_t pt_pid;
137 };
138 #if 1 /* strongly typed cast - should be a noop */
139 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
140 #else
141 #define p2u(p) ((uint)p)
142 #endif
143 #define P_VALID(p) (!(p2u(p) & 1))
144 #define P_NEXT(p) (p2u(p) >> 1)
145 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
146
147 #define INITIAL_PID_TABLE_SIZE (1 << 5)
148 static struct pid_table *pid_table;
149 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
150 static uint pid_alloc_lim; /* max we allocate before growing table */
151 static uint pid_alloc_cnt; /* number of allocated pids */
152
153 /* links through free slots - never empty! */
154 static uint next_free_pt, last_free_pt;
155 static pid_t pid_max = PID_MAX; /* largest value we allocate */
156
157 /* Components of the first process -- never freed. */
158
159 extern struct emul emul_netbsd; /* defined in kern_exec.c */
160
161 struct session session0 = {
162 .s_count = 1,
163 .s_sid = 0,
164 };
165 struct pgrp pgrp0 = {
166 .pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members),
167 .pg_session = &session0,
168 };
169 filedesc_t filedesc0;
170 struct cwdinfo cwdi0 = {
171 .cwdi_cmask = CMASK, /* see cmask below */
172 .cwdi_refcnt = 1,
173 };
174 struct plimit limit0;
175 struct pstats pstat0;
176 struct vmspace vmspace0;
177 struct sigacts sigacts0;
178 struct proc proc0 = {
179 .p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps),
180 .p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters),
181 .p_nlwps = 1,
182 .p_nrlwps = 1,
183 .p_nlwpid = 1, /* must match lwp0.l_lid */
184 .p_pgrp = &pgrp0,
185 .p_comm = "system",
186 /*
187 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8)
188 * when they exit. init(8) can easily wait them out for us.
189 */
190 .p_flag = PK_SYSTEM | PK_NOCLDWAIT,
191 .p_stat = SACTIVE,
192 .p_nice = NZERO,
193 .p_emul = &emul_netbsd,
194 .p_cwdi = &cwdi0,
195 .p_limit = &limit0,
196 .p_fd = &filedesc0,
197 .p_vmspace = &vmspace0,
198 .p_stats = &pstat0,
199 .p_sigacts = &sigacts0,
200 };
201 kauth_cred_t cred0;
202
203 int nofile = NOFILE;
204 int maxuprc = MAXUPRC;
205 int cmask = CMASK;
206
207 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
208
209 static int sysctl_doeproc(SYSCTLFN_PROTO);
210 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
211 static void fill_kproc2(struct proc *, struct kinfo_proc2 *, bool);
212
213 /*
214 * The process list descriptors, used during pid allocation and
215 * by sysctl. No locking on this data structure is needed since
216 * it is completely static.
217 */
218 const struct proclist_desc proclists[] = {
219 { &allproc },
220 { &zombproc },
221 { NULL },
222 };
223
224 static struct pgrp * pg_remove(pid_t);
225 static void pg_delete(pid_t);
226 static void orphanpg(struct pgrp *);
227
228 static specificdata_domain_t proc_specificdata_domain;
229
230 static pool_cache_t proc_cache;
231
232 static kauth_listener_t proc_listener;
233
234 static int
235 proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
236 void *arg0, void *arg1, void *arg2, void *arg3)
237 {
238 struct proc *p;
239 int result;
240
241 result = KAUTH_RESULT_DEFER;
242 p = arg0;
243
244 switch (action) {
245 case KAUTH_PROCESS_CANSEE: {
246 enum kauth_process_req req;
247
248 req = (enum kauth_process_req)arg1;
249
250 switch (req) {
251 case KAUTH_REQ_PROCESS_CANSEE_ARGS:
252 case KAUTH_REQ_PROCESS_CANSEE_ENTRY:
253 case KAUTH_REQ_PROCESS_CANSEE_OPENFILES:
254 result = KAUTH_RESULT_ALLOW;
255
256 break;
257
258 case KAUTH_REQ_PROCESS_CANSEE_ENV:
259 if (kauth_cred_getuid(cred) !=
260 kauth_cred_getuid(p->p_cred) ||
261 kauth_cred_getuid(cred) !=
262 kauth_cred_getsvuid(p->p_cred))
263 break;
264
265 result = KAUTH_RESULT_ALLOW;
266
267 break;
268
269 default:
270 break;
271 }
272
273 break;
274 }
275
276 case KAUTH_PROCESS_FORK: {
277 int lnprocs = (int)(unsigned long)arg2;
278
279 /*
280 * Don't allow a nonprivileged user to use the last few
281 * processes. The variable lnprocs is the current number of
282 * processes, maxproc is the limit.
283 */
284 if (__predict_false((lnprocs >= maxproc - 5)))
285 break;
286
287 result = KAUTH_RESULT_ALLOW;
288
289 break;
290 }
291
292 case KAUTH_PROCESS_CORENAME:
293 case KAUTH_PROCESS_STOPFLAG:
294 if (proc_uidmatch(cred, p->p_cred) == 0)
295 result = KAUTH_RESULT_ALLOW;
296
297 break;
298
299 default:
300 break;
301 }
302
303 return result;
304 }
305
306 /*
307 * Initialize global process hashing structures.
308 */
309 void
310 procinit(void)
311 {
312 const struct proclist_desc *pd;
313 u_int i;
314 #define LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
315
316 for (pd = proclists; pd->pd_list != NULL; pd++)
317 LIST_INIT(pd->pd_list);
318
319 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
320 pid_table = kmem_alloc(INITIAL_PID_TABLE_SIZE
321 * sizeof(struct pid_table), KM_SLEEP);
322
323 /* Set free list running through table...
324 Preset 'use count' above PID_MAX so we allocate pid 1 next. */
325 for (i = 0; i <= pid_tbl_mask; i++) {
326 pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
327 pid_table[i].pt_pgrp = 0;
328 pid_table[i].pt_pid = 0;
329 }
330 /* slot 0 is just grabbed */
331 next_free_pt = 1;
332 /* Need to fix last entry. */
333 last_free_pt = pid_tbl_mask;
334 pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
335 /* point at which we grow table - to avoid reusing pids too often */
336 pid_alloc_lim = pid_tbl_mask - 1;
337 #undef LINK_EMPTY
338
339 proc_specificdata_domain = specificdata_domain_create();
340 KASSERT(proc_specificdata_domain != NULL);
341
342 proc_cache = pool_cache_init(sizeof(struct proc), 0, 0, 0,
343 "procpl", NULL, IPL_NONE, NULL, NULL, NULL);
344
345 proc_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
346 proc_listener_cb, NULL);
347 }
348
349 void
350 procinit_sysctl(void)
351 {
352 static struct sysctllog *clog;
353
354 sysctl_createv(&clog, 0, NULL, NULL,
355 CTLFLAG_PERMANENT,
356 CTLTYPE_NODE, "kern", NULL,
357 NULL, 0, NULL, 0,
358 CTL_KERN, CTL_EOL);
359
360 sysctl_createv(&clog, 0, NULL, NULL,
361 CTLFLAG_PERMANENT,
362 CTLTYPE_NODE, "proc",
363 SYSCTL_DESCR("System-wide process information"),
364 sysctl_doeproc, 0, NULL, 0,
365 CTL_KERN, KERN_PROC, CTL_EOL);
366 sysctl_createv(&clog, 0, NULL, NULL,
367 CTLFLAG_PERMANENT,
368 CTLTYPE_NODE, "proc2",
369 SYSCTL_DESCR("Machine-independent process information"),
370 sysctl_doeproc, 0, NULL, 0,
371 CTL_KERN, KERN_PROC2, CTL_EOL);
372 sysctl_createv(&clog, 0, NULL, NULL,
373 CTLFLAG_PERMANENT,
374 CTLTYPE_NODE, "proc_args",
375 SYSCTL_DESCR("Process argument information"),
376 sysctl_kern_proc_args, 0, NULL, 0,
377 CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
378
379 /*
380 "nodes" under these:
381
382 KERN_PROC_ALL
383 KERN_PROC_PID pid
384 KERN_PROC_PGRP pgrp
385 KERN_PROC_SESSION sess
386 KERN_PROC_TTY tty
387 KERN_PROC_UID uid
388 KERN_PROC_RUID uid
389 KERN_PROC_GID gid
390 KERN_PROC_RGID gid
391
392 all in all, probably not worth the effort...
393 */
394 }
395
396 /*
397 * Initialize process 0.
398 */
399 void
400 proc0_init(void)
401 {
402 struct proc *p;
403 struct pgrp *pg;
404 rlim_t lim;
405 int i;
406
407 p = &proc0;
408 pg = &pgrp0;
409
410 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
411 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
412 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
413
414 rw_init(&p->p_reflock);
415 cv_init(&p->p_waitcv, "wait");
416 cv_init(&p->p_lwpcv, "lwpwait");
417
418 LIST_INSERT_HEAD(&p->p_lwps, &lwp0, l_sibling);
419
420 pid_table[0].pt_proc = p;
421 LIST_INSERT_HEAD(&allproc, p, p_list);
422
423 pid_table[0].pt_pgrp = pg;
424 LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
425
426 #ifdef __HAVE_SYSCALL_INTERN
427 (*p->p_emul->e_syscall_intern)(p);
428 #endif
429
430 /* Create credentials. */
431 cred0 = kauth_cred_alloc();
432 p->p_cred = cred0;
433
434 /* Create the CWD info. */
435 rw_init(&cwdi0.cwdi_lock);
436
437 /* Create the limits structures. */
438 mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE);
439 for (i = 0; i < __arraycount(limit0.pl_rlimit); i++)
440 limit0.pl_rlimit[i].rlim_cur =
441 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
442
443 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
444 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
445 maxfiles < nofile ? maxfiles : nofile;
446
447 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
448 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
449 maxproc < maxuprc ? maxproc : maxuprc;
450
451 lim = MIN(VM_MAXUSER_ADDRESS, ctob((rlim_t)uvmexp.free));
452 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
453 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
454 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
455 limit0.pl_corename = defcorename;
456 limit0.pl_refcnt = 1;
457 limit0.pl_sv_limit = NULL;
458
459 /* Configure virtual memory system, set vm rlimits. */
460 uvm_init_limits(p);
461
462 /* Initialize file descriptor table for proc0. */
463 fd_init(&filedesc0);
464
465 /*
466 * Initialize proc0's vmspace, which uses the kernel pmap.
467 * All kernel processes (which never have user space mappings)
468 * share proc0's vmspace, and thus, the kernel pmap.
469 */
470 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
471 trunc_page(VM_MAX_ADDRESS));
472
473 /* Initialize signal state for proc0. XXX IPL_SCHED */
474 mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
475 siginit(p);
476
477 proc_initspecific(p);
478 kdtrace_proc_ctor(NULL, p);
479 }
480
481 /*
482 * Session reference counting.
483 */
484
485 void
486 proc_sesshold(struct session *ss)
487 {
488
489 KASSERT(mutex_owned(proc_lock));
490 ss->s_count++;
491 }
492
493 void
494 proc_sessrele(struct session *ss)
495 {
496
497 KASSERT(mutex_owned(proc_lock));
498 /*
499 * We keep the pgrp with the same id as the session in order to
500 * stop a process being given the same pid. Since the pgrp holds
501 * a reference to the session, it must be a 'zombie' pgrp by now.
502 */
503 if (--ss->s_count == 0) {
504 struct pgrp *pg;
505
506 pg = pg_remove(ss->s_sid);
507 mutex_exit(proc_lock);
508
509 kmem_free(pg, sizeof(struct pgrp));
510 kmem_free(ss, sizeof(struct session));
511 } else {
512 mutex_exit(proc_lock);
513 }
514 }
515
516 /*
517 * Check that the specified process group is in the session of the
518 * specified process.
519 * Treats -ve ids as process ids.
520 * Used to validate TIOCSPGRP requests.
521 */
522 int
523 pgid_in_session(struct proc *p, pid_t pg_id)
524 {
525 struct pgrp *pgrp;
526 struct session *session;
527 int error;
528
529 mutex_enter(proc_lock);
530 if (pg_id < 0) {
531 struct proc *p1 = proc_find(-pg_id);
532 if (p1 == NULL) {
533 error = EINVAL;
534 goto fail;
535 }
536 pgrp = p1->p_pgrp;
537 } else {
538 pgrp = pgrp_find(pg_id);
539 if (pgrp == NULL) {
540 error = EINVAL;
541 goto fail;
542 }
543 }
544 session = pgrp->pg_session;
545 error = (session != p->p_pgrp->pg_session) ? EPERM : 0;
546 fail:
547 mutex_exit(proc_lock);
548 return error;
549 }
550
551 /*
552 * p_inferior: is p an inferior of q?
553 */
554 static inline bool
555 p_inferior(struct proc *p, struct proc *q)
556 {
557
558 KASSERT(mutex_owned(proc_lock));
559
560 for (; p != q; p = p->p_pptr)
561 if (p->p_pid == 0)
562 return false;
563 return true;
564 }
565
566 /*
567 * proc_find: locate a process by the ID.
568 *
569 * => Must be called with proc_lock held.
570 */
571 proc_t *
572 proc_find_raw(pid_t pid)
573 {
574 struct pid_table *pt;
575 proc_t *p;
576
577 KASSERT(mutex_owned(proc_lock));
578 pt = &pid_table[pid & pid_tbl_mask];
579 p = pt->pt_proc;
580 if (__predict_false(!P_VALID(p) || pt->pt_pid != pid)) {
581 return NULL;
582 }
583 return p;
584 }
585
586 proc_t *
587 proc_find(pid_t pid)
588 {
589 proc_t *p;
590
591 p = proc_find_raw(pid);
592 if (__predict_false(p == NULL)) {
593 return NULL;
594 }
595
596 /*
597 * Only allow live processes to be found by PID.
598 * XXX: p_stat might change, since unlocked.
599 */
600 if (__predict_true(p->p_stat == SACTIVE || p->p_stat == SSTOP)) {
601 return p;
602 }
603 return NULL;
604 }
605
606 /*
607 * pgrp_find: locate a process group by the ID.
608 *
609 * => Must be called with proc_lock held.
610 */
611 struct pgrp *
612 pgrp_find(pid_t pgid)
613 {
614 struct pgrp *pg;
615
616 KASSERT(mutex_owned(proc_lock));
617
618 pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
619
620 /*
621 * Cannot look up a process group that only exists because the
622 * session has not died yet (traditional).
623 */
624 if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
625 return NULL;
626 }
627 return pg;
628 }
629
630 static void
631 expand_pid_table(void)
632 {
633 size_t pt_size, tsz;
634 struct pid_table *n_pt, *new_pt;
635 struct proc *proc;
636 struct pgrp *pgrp;
637 pid_t pid, rpid;
638 u_int i;
639 uint new_pt_mask;
640
641 pt_size = pid_tbl_mask + 1;
642 tsz = pt_size * 2 * sizeof(struct pid_table);
643 new_pt = kmem_alloc(tsz, KM_SLEEP);
644 new_pt_mask = pt_size * 2 - 1;
645
646 mutex_enter(proc_lock);
647 if (pt_size != pid_tbl_mask + 1) {
648 /* Another process beat us to it... */
649 mutex_exit(proc_lock);
650 kmem_free(new_pt, tsz);
651 return;
652 }
653
654 /*
655 * Copy entries from old table into new one.
656 * If 'pid' is 'odd' we need to place in the upper half,
657 * even pid's to the lower half.
658 * Free items stay in the low half so we don't have to
659 * fixup the reference to them.
660 * We stuff free items on the front of the freelist
661 * because we can't write to unmodified entries.
662 * Processing the table backwards maintains a semblance
663 * of issuing pid numbers that increase with time.
664 */
665 i = pt_size - 1;
666 n_pt = new_pt + i;
667 for (; ; i--, n_pt--) {
668 proc = pid_table[i].pt_proc;
669 pgrp = pid_table[i].pt_pgrp;
670 if (!P_VALID(proc)) {
671 /* Up 'use count' so that link is valid */
672 pid = (P_NEXT(proc) + pt_size) & ~pt_size;
673 rpid = 0;
674 proc = P_FREE(pid);
675 if (pgrp)
676 pid = pgrp->pg_id;
677 } else {
678 pid = pid_table[i].pt_pid;
679 rpid = pid;
680 }
681
682 /* Save entry in appropriate half of table */
683 n_pt[pid & pt_size].pt_proc = proc;
684 n_pt[pid & pt_size].pt_pgrp = pgrp;
685 n_pt[pid & pt_size].pt_pid = rpid;
686
687 /* Put other piece on start of free list */
688 pid = (pid ^ pt_size) & ~pid_tbl_mask;
689 n_pt[pid & pt_size].pt_proc =
690 P_FREE((pid & ~pt_size) | next_free_pt);
691 n_pt[pid & pt_size].pt_pgrp = 0;
692 n_pt[pid & pt_size].pt_pid = 0;
693
694 next_free_pt = i | (pid & pt_size);
695 if (i == 0)
696 break;
697 }
698
699 /* Save old table size and switch tables */
700 tsz = pt_size * sizeof(struct pid_table);
701 n_pt = pid_table;
702 pid_table = new_pt;
703 pid_tbl_mask = new_pt_mask;
704
705 /*
706 * pid_max starts as PID_MAX (= 30000), once we have 16384
707 * allocated pids we need it to be larger!
708 */
709 if (pid_tbl_mask > PID_MAX) {
710 pid_max = pid_tbl_mask * 2 + 1;
711 pid_alloc_lim |= pid_alloc_lim << 1;
712 } else
713 pid_alloc_lim <<= 1; /* doubles number of free slots... */
714
715 mutex_exit(proc_lock);
716 kmem_free(n_pt, tsz);
717 }
718
719 struct proc *
720 proc_alloc(void)
721 {
722 struct proc *p;
723
724 p = pool_cache_get(proc_cache, PR_WAITOK);
725 p->p_stat = SIDL; /* protect against others */
726 proc_initspecific(p);
727 kdtrace_proc_ctor(NULL, p);
728 p->p_pid = -1;
729 proc_alloc_pid(p);
730 return p;
731 }
732
733 pid_t
734 proc_alloc_pid(struct proc *p)
735 {
736 struct pid_table *pt;
737 pid_t pid;
738 int nxt;
739
740 for (;;expand_pid_table()) {
741 if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
742 /* ensure pids cycle through 2000+ values */
743 continue;
744 mutex_enter(proc_lock);
745 pt = &pid_table[next_free_pt];
746 #ifdef DIAGNOSTIC
747 if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
748 panic("proc_alloc: slot busy");
749 #endif
750 nxt = P_NEXT(pt->pt_proc);
751 if (nxt & pid_tbl_mask)
752 break;
753 /* Table full - expand (NB last entry not used....) */
754 mutex_exit(proc_lock);
755 }
756
757 /* pid is 'saved use count' + 'size' + entry */
758 pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
759 if ((uint)pid > (uint)pid_max)
760 pid &= pid_tbl_mask;
761 next_free_pt = nxt & pid_tbl_mask;
762
763 /* Grab table slot */
764 pt->pt_proc = p;
765
766 KASSERT(pt->pt_pid == 0);
767 pt->pt_pid = pid;
768 if (p->p_pid == -1) {
769 p->p_pid = pid;
770 }
771 pid_alloc_cnt++;
772 mutex_exit(proc_lock);
773
774 return pid;
775 }
776
777 /*
778 * Free a process id - called from proc_free (in kern_exit.c)
779 *
780 * Called with the proc_lock held.
781 */
782 void
783 proc_free_pid(pid_t pid)
784 {
785 struct pid_table *pt;
786
787 KASSERT(mutex_owned(proc_lock));
788
789 pt = &pid_table[pid & pid_tbl_mask];
790
791 /* save pid use count in slot */
792 pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
793 KASSERT(pt->pt_pid == pid);
794 pt->pt_pid = 0;
795
796 if (pt->pt_pgrp == NULL) {
797 /* link last freed entry onto ours */
798 pid &= pid_tbl_mask;
799 pt = &pid_table[last_free_pt];
800 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
801 pt->pt_pid = 0;
802 last_free_pt = pid;
803 pid_alloc_cnt--;
804 }
805
806 atomic_dec_uint(&nprocs);
807 }
808
809 void
810 proc_free_mem(struct proc *p)
811 {
812
813 kdtrace_proc_dtor(NULL, p);
814 pool_cache_put(proc_cache, p);
815 }
816
817 /*
818 * proc_enterpgrp: move p to a new or existing process group (and session).
819 *
820 * If we are creating a new pgrp, the pgid should equal
821 * the calling process' pid.
822 * If is only valid to enter a process group that is in the session
823 * of the process.
824 * Also mksess should only be set if we are creating a process group
825 *
826 * Only called from sys_setsid and sys_setpgid.
827 */
828 int
829 proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess)
830 {
831 struct pgrp *new_pgrp, *pgrp;
832 struct session *sess;
833 struct proc *p;
834 int rval;
835 pid_t pg_id = NO_PGID;
836
837 sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL;
838
839 /* Allocate data areas we might need before doing any validity checks */
840 mutex_enter(proc_lock); /* Because pid_table might change */
841 if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
842 mutex_exit(proc_lock);
843 new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP);
844 mutex_enter(proc_lock);
845 } else
846 new_pgrp = NULL;
847 rval = EPERM; /* most common error (to save typing) */
848
849 /* Check pgrp exists or can be created */
850 pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
851 if (pgrp != NULL && pgrp->pg_id != pgid)
852 goto done;
853
854 /* Can only set another process under restricted circumstances. */
855 if (pid != curp->p_pid) {
856 /* Must exist and be one of our children... */
857 p = proc_find(pid);
858 if (p == NULL || !p_inferior(p, curp)) {
859 rval = ESRCH;
860 goto done;
861 }
862 /* ... in the same session... */
863 if (sess != NULL || p->p_session != curp->p_session)
864 goto done;
865 /* ... existing pgid must be in same session ... */
866 if (pgrp != NULL && pgrp->pg_session != p->p_session)
867 goto done;
868 /* ... and not done an exec. */
869 if (p->p_flag & PK_EXEC) {
870 rval = EACCES;
871 goto done;
872 }
873 } else {
874 /* ... setsid() cannot re-enter a pgrp */
875 if (mksess && (curp->p_pgid == curp->p_pid ||
876 pgrp_find(curp->p_pid)))
877 goto done;
878 p = curp;
879 }
880
881 /* Changing the process group/session of a session
882 leader is definitely off limits. */
883 if (SESS_LEADER(p)) {
884 if (sess == NULL && p->p_pgrp == pgrp)
885 /* unless it's a definite noop */
886 rval = 0;
887 goto done;
888 }
889
890 /* Can only create a process group with id of process */
891 if (pgrp == NULL && pgid != pid)
892 goto done;
893
894 /* Can only create a session if creating pgrp */
895 if (sess != NULL && pgrp != NULL)
896 goto done;
897
898 /* Check we allocated memory for a pgrp... */
899 if (pgrp == NULL && new_pgrp == NULL)
900 goto done;
901
902 /* Don't attach to 'zombie' pgrp */
903 if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
904 goto done;
905
906 /* Expect to succeed now */
907 rval = 0;
908
909 if (pgrp == p->p_pgrp)
910 /* nothing to do */
911 goto done;
912
913 /* Ok all setup, link up required structures */
914
915 if (pgrp == NULL) {
916 pgrp = new_pgrp;
917 new_pgrp = NULL;
918 if (sess != NULL) {
919 sess->s_sid = p->p_pid;
920 sess->s_leader = p;
921 sess->s_count = 1;
922 sess->s_ttyvp = NULL;
923 sess->s_ttyp = NULL;
924 sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
925 memcpy(sess->s_login, p->p_session->s_login,
926 sizeof(sess->s_login));
927 p->p_lflag &= ~PL_CONTROLT;
928 } else {
929 sess = p->p_pgrp->pg_session;
930 proc_sesshold(sess);
931 }
932 pgrp->pg_session = sess;
933 sess = NULL;
934
935 pgrp->pg_id = pgid;
936 LIST_INIT(&pgrp->pg_members);
937 #ifdef DIAGNOSTIC
938 if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
939 panic("enterpgrp: pgrp table slot in use");
940 if (__predict_false(mksess && p != curp))
941 panic("enterpgrp: mksession and p != curproc");
942 #endif
943 pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
944 pgrp->pg_jobc = 0;
945 }
946
947 /*
948 * Adjust eligibility of affected pgrps to participate in job control.
949 * Increment eligibility counts before decrementing, otherwise we
950 * could reach 0 spuriously during the first call.
951 */
952 fixjobc(p, pgrp, 1);
953 fixjobc(p, p->p_pgrp, 0);
954
955 /* Interlock with ttread(). */
956 mutex_spin_enter(&tty_lock);
957
958 /* Move process to requested group. */
959 LIST_REMOVE(p, p_pglist);
960 if (LIST_EMPTY(&p->p_pgrp->pg_members))
961 /* defer delete until we've dumped the lock */
962 pg_id = p->p_pgrp->pg_id;
963 p->p_pgrp = pgrp;
964 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
965
966 /* Done with the swap; we can release the tty mutex. */
967 mutex_spin_exit(&tty_lock);
968
969 done:
970 if (pg_id != NO_PGID) {
971 /* Releases proc_lock. */
972 pg_delete(pg_id);
973 } else {
974 mutex_exit(proc_lock);
975 }
976 if (sess != NULL)
977 kmem_free(sess, sizeof(*sess));
978 if (new_pgrp != NULL)
979 kmem_free(new_pgrp, sizeof(*new_pgrp));
980 #ifdef DEBUG_PGRP
981 if (__predict_false(rval))
982 printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
983 pid, pgid, mksess, curp->p_pid, rval);
984 #endif
985 return rval;
986 }
987
988 /*
989 * proc_leavepgrp: remove a process from its process group.
990 * => must be called with the proc_lock held, which will be released;
991 */
992 void
993 proc_leavepgrp(struct proc *p)
994 {
995 struct pgrp *pgrp;
996
997 KASSERT(mutex_owned(proc_lock));
998
999 /* Interlock with ttread() */
1000 mutex_spin_enter(&tty_lock);
1001 pgrp = p->p_pgrp;
1002 LIST_REMOVE(p, p_pglist);
1003 p->p_pgrp = NULL;
1004 mutex_spin_exit(&tty_lock);
1005
1006 if (LIST_EMPTY(&pgrp->pg_members)) {
1007 /* Releases proc_lock. */
1008 pg_delete(pgrp->pg_id);
1009 } else {
1010 mutex_exit(proc_lock);
1011 }
1012 }
1013
1014 /*
1015 * pg_remove: remove a process group from the table.
1016 * => must be called with the proc_lock held;
1017 * => returns process group to free;
1018 */
1019 static struct pgrp *
1020 pg_remove(pid_t pg_id)
1021 {
1022 struct pgrp *pgrp;
1023 struct pid_table *pt;
1024
1025 KASSERT(mutex_owned(proc_lock));
1026
1027 pt = &pid_table[pg_id & pid_tbl_mask];
1028 pgrp = pt->pt_pgrp;
1029
1030 KASSERT(pgrp != NULL);
1031 KASSERT(pgrp->pg_id == pg_id);
1032 KASSERT(LIST_EMPTY(&pgrp->pg_members));
1033
1034 pt->pt_pgrp = NULL;
1035
1036 if (!P_VALID(pt->pt_proc)) {
1037 /* Orphaned pgrp, put slot onto free list. */
1038 KASSERT((P_NEXT(pt->pt_proc) & pid_tbl_mask) == 0);
1039 pg_id &= pid_tbl_mask;
1040 pt = &pid_table[last_free_pt];
1041 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
1042 KASSERT(pt->pt_pid == 0);
1043 last_free_pt = pg_id;
1044 pid_alloc_cnt--;
1045 }
1046 return pgrp;
1047 }
1048
1049 /*
1050 * pg_delete: delete and free a process group.
1051 * => must be called with the proc_lock held, which will be released.
1052 */
1053 static void
1054 pg_delete(pid_t pg_id)
1055 {
1056 struct pgrp *pg;
1057 struct tty *ttyp;
1058 struct session *ss;
1059
1060 KASSERT(mutex_owned(proc_lock));
1061
1062 pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
1063 if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) {
1064 mutex_exit(proc_lock);
1065 return;
1066 }
1067
1068 ss = pg->pg_session;
1069
1070 /* Remove reference (if any) from tty to this process group */
1071 mutex_spin_enter(&tty_lock);
1072 ttyp = ss->s_ttyp;
1073 if (ttyp != NULL && ttyp->t_pgrp == pg) {
1074 ttyp->t_pgrp = NULL;
1075 KASSERT(ttyp->t_session == ss);
1076 }
1077 mutex_spin_exit(&tty_lock);
1078
1079 /*
1080 * The leading process group in a session is freed by proc_sessrele(),
1081 * if last reference. Note: proc_sessrele() releases proc_lock.
1082 */
1083 pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL;
1084 proc_sessrele(ss);
1085
1086 if (pg != NULL) {
1087 /* Free it, if was not done by proc_sessrele(). */
1088 kmem_free(pg, sizeof(struct pgrp));
1089 }
1090 }
1091
1092 /*
1093 * Adjust pgrp jobc counters when specified process changes process group.
1094 * We count the number of processes in each process group that "qualify"
1095 * the group for terminal job control (those with a parent in a different
1096 * process group of the same session). If that count reaches zero, the
1097 * process group becomes orphaned. Check both the specified process'
1098 * process group and that of its children.
1099 * entering == 0 => p is leaving specified group.
1100 * entering == 1 => p is entering specified group.
1101 *
1102 * Call with proc_lock held.
1103 */
1104 void
1105 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
1106 {
1107 struct pgrp *hispgrp;
1108 struct session *mysession = pgrp->pg_session;
1109 struct proc *child;
1110
1111 KASSERT(mutex_owned(proc_lock));
1112
1113 /*
1114 * Check p's parent to see whether p qualifies its own process
1115 * group; if so, adjust count for p's process group.
1116 */
1117 hispgrp = p->p_pptr->p_pgrp;
1118 if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
1119 if (entering) {
1120 pgrp->pg_jobc++;
1121 p->p_lflag &= ~PL_ORPHANPG;
1122 } else if (--pgrp->pg_jobc == 0)
1123 orphanpg(pgrp);
1124 }
1125
1126 /*
1127 * Check this process' children to see whether they qualify
1128 * their process groups; if so, adjust counts for children's
1129 * process groups.
1130 */
1131 LIST_FOREACH(child, &p->p_children, p_sibling) {
1132 hispgrp = child->p_pgrp;
1133 if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
1134 !P_ZOMBIE(child)) {
1135 if (entering) {
1136 child->p_lflag &= ~PL_ORPHANPG;
1137 hispgrp->pg_jobc++;
1138 } else if (--hispgrp->pg_jobc == 0)
1139 orphanpg(hispgrp);
1140 }
1141 }
1142 }
1143
1144 /*
1145 * A process group has become orphaned;
1146 * if there are any stopped processes in the group,
1147 * hang-up all process in that group.
1148 *
1149 * Call with proc_lock held.
1150 */
1151 static void
1152 orphanpg(struct pgrp *pg)
1153 {
1154 struct proc *p;
1155
1156 KASSERT(mutex_owned(proc_lock));
1157
1158 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1159 if (p->p_stat == SSTOP) {
1160 p->p_lflag |= PL_ORPHANPG;
1161 psignal(p, SIGHUP);
1162 psignal(p, SIGCONT);
1163 }
1164 }
1165 }
1166
1167 #ifdef DDB
1168 #include <ddb/db_output.h>
1169 void pidtbl_dump(void);
1170 void
1171 pidtbl_dump(void)
1172 {
1173 struct pid_table *pt;
1174 struct proc *p;
1175 struct pgrp *pgrp;
1176 int id;
1177
1178 db_printf("pid table %p size %x, next %x, last %x\n",
1179 pid_table, pid_tbl_mask+1,
1180 next_free_pt, last_free_pt);
1181 for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
1182 p = pt->pt_proc;
1183 if (!P_VALID(p) && !pt->pt_pgrp)
1184 continue;
1185 db_printf(" id %x: ", id);
1186 if (P_VALID(p))
1187 db_printf("slotpid %d proc %p id %d (0x%x) %s\n",
1188 pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm);
1189 else
1190 db_printf("next %x use %x\n",
1191 P_NEXT(p) & pid_tbl_mask,
1192 P_NEXT(p) & ~pid_tbl_mask);
1193 if ((pgrp = pt->pt_pgrp)) {
1194 db_printf("\tsession %p, sid %d, count %d, login %s\n",
1195 pgrp->pg_session, pgrp->pg_session->s_sid,
1196 pgrp->pg_session->s_count,
1197 pgrp->pg_session->s_login);
1198 db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
1199 pgrp, pgrp->pg_id, pgrp->pg_jobc,
1200 LIST_FIRST(&pgrp->pg_members));
1201 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1202 db_printf("\t\tpid %d addr %p pgrp %p %s\n",
1203 p->p_pid, p, p->p_pgrp, p->p_comm);
1204 }
1205 }
1206 }
1207 }
1208 #endif /* DDB */
1209
1210 #ifdef KSTACK_CHECK_MAGIC
1211
1212 #define KSTACK_MAGIC 0xdeadbeaf
1213
1214 /* XXX should be per process basis? */
1215 static int kstackleftmin = KSTACK_SIZE;
1216 static int kstackleftthres = KSTACK_SIZE / 8;
1217
1218 void
1219 kstack_setup_magic(const struct lwp *l)
1220 {
1221 uint32_t *ip;
1222 uint32_t const *end;
1223
1224 KASSERT(l != NULL);
1225 KASSERT(l != &lwp0);
1226
1227 /*
1228 * fill all the stack with magic number
1229 * so that later modification on it can be detected.
1230 */
1231 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1232 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1233 for (; ip < end; ip++) {
1234 *ip = KSTACK_MAGIC;
1235 }
1236 }
1237
1238 void
1239 kstack_check_magic(const struct lwp *l)
1240 {
1241 uint32_t const *ip, *end;
1242 int stackleft;
1243
1244 KASSERT(l != NULL);
1245
1246 /* don't check proc0 */ /*XXX*/
1247 if (l == &lwp0)
1248 return;
1249
1250 #ifdef __MACHINE_STACK_GROWS_UP
1251 /* stack grows upwards (eg. hppa) */
1252 ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1253 end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1254 for (ip--; ip >= end; ip--)
1255 if (*ip != KSTACK_MAGIC)
1256 break;
1257
1258 stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
1259 #else /* __MACHINE_STACK_GROWS_UP */
1260 /* stack grows downwards (eg. i386) */
1261 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1262 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1263 for (; ip < end; ip++)
1264 if (*ip != KSTACK_MAGIC)
1265 break;
1266
1267 stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
1268 #endif /* __MACHINE_STACK_GROWS_UP */
1269
1270 if (kstackleftmin > stackleft) {
1271 kstackleftmin = stackleft;
1272 if (stackleft < kstackleftthres)
1273 printf("warning: kernel stack left %d bytes"
1274 "(pid %u:lid %u)\n", stackleft,
1275 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1276 }
1277
1278 if (stackleft <= 0) {
1279 panic("magic on the top of kernel stack changed for "
1280 "pid %u, lid %u: maybe kernel stack overflow",
1281 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1282 }
1283 }
1284 #endif /* KSTACK_CHECK_MAGIC */
1285
1286 int
1287 proclist_foreach_call(struct proclist *list,
1288 int (*callback)(struct proc *, void *arg), void *arg)
1289 {
1290 struct proc marker;
1291 struct proc *p;
1292 int ret = 0;
1293
1294 marker.p_flag = PK_MARKER;
1295 mutex_enter(proc_lock);
1296 for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
1297 if (p->p_flag & PK_MARKER) {
1298 p = LIST_NEXT(p, p_list);
1299 continue;
1300 }
1301 LIST_INSERT_AFTER(p, &marker, p_list);
1302 ret = (*callback)(p, arg);
1303 KASSERT(mutex_owned(proc_lock));
1304 p = LIST_NEXT(&marker, p_list);
1305 LIST_REMOVE(&marker, p_list);
1306 }
1307 mutex_exit(proc_lock);
1308
1309 return ret;
1310 }
1311
1312 int
1313 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
1314 {
1315
1316 /* XXXCDC: how should locking work here? */
1317
1318 /* curproc exception is for coredump. */
1319
1320 if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
1321 (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
1322 return EFAULT;
1323 }
1324
1325 uvmspace_addref(p->p_vmspace);
1326 *vm = p->p_vmspace;
1327
1328 return 0;
1329 }
1330
1331 /*
1332 * Acquire a write lock on the process credential.
1333 */
1334 void
1335 proc_crmod_enter(void)
1336 {
1337 struct lwp *l = curlwp;
1338 struct proc *p = l->l_proc;
1339 struct plimit *lim;
1340 kauth_cred_t oc;
1341 char *cn;
1342
1343 /* Reset what needs to be reset in plimit. */
1344 if (p->p_limit->pl_corename != defcorename) {
1345 lim_privatise(p, false);
1346 lim = p->p_limit;
1347 mutex_enter(&lim->pl_lock);
1348 cn = lim->pl_corename;
1349 lim->pl_corename = defcorename;
1350 mutex_exit(&lim->pl_lock);
1351 if (cn != defcorename)
1352 free(cn, M_TEMP);
1353 }
1354
1355 mutex_enter(p->p_lock);
1356
1357 /* Ensure the LWP cached credentials are up to date. */
1358 if ((oc = l->l_cred) != p->p_cred) {
1359 kauth_cred_hold(p->p_cred);
1360 l->l_cred = p->p_cred;
1361 kauth_cred_free(oc);
1362 }
1363
1364 }
1365
1366 /*
1367 * Set in a new process credential, and drop the write lock. The credential
1368 * must have a reference already. Optionally, free a no-longer required
1369 * credential. The scheduler also needs to inspect p_cred, so we also
1370 * briefly acquire the sched state mutex.
1371 */
1372 void
1373 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
1374 {
1375 struct lwp *l = curlwp, *l2;
1376 struct proc *p = l->l_proc;
1377 kauth_cred_t oc;
1378
1379 KASSERT(mutex_owned(p->p_lock));
1380
1381 /* Is there a new credential to set in? */
1382 if (scred != NULL) {
1383 p->p_cred = scred;
1384 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1385 if (l2 != l)
1386 l2->l_prflag |= LPR_CRMOD;
1387 }
1388
1389 /* Ensure the LWP cached credentials are up to date. */
1390 if ((oc = l->l_cred) != scred) {
1391 kauth_cred_hold(scred);
1392 l->l_cred = scred;
1393 }
1394 } else
1395 oc = NULL; /* XXXgcc */
1396
1397 if (sugid) {
1398 /*
1399 * Mark process as having changed credentials, stops
1400 * tracing etc.
1401 */
1402 p->p_flag |= PK_SUGID;
1403 }
1404
1405 mutex_exit(p->p_lock);
1406
1407 /* If there is a credential to be released, free it now. */
1408 if (fcred != NULL) {
1409 KASSERT(scred != NULL);
1410 kauth_cred_free(fcred);
1411 if (oc != scred)
1412 kauth_cred_free(oc);
1413 }
1414 }
1415
1416 /*
1417 * proc_specific_key_create --
1418 * Create a key for subsystem proc-specific data.
1419 */
1420 int
1421 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1422 {
1423
1424 return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
1425 }
1426
1427 /*
1428 * proc_specific_key_delete --
1429 * Delete a key for subsystem proc-specific data.
1430 */
1431 void
1432 proc_specific_key_delete(specificdata_key_t key)
1433 {
1434
1435 specificdata_key_delete(proc_specificdata_domain, key);
1436 }
1437
1438 /*
1439 * proc_initspecific --
1440 * Initialize a proc's specificdata container.
1441 */
1442 void
1443 proc_initspecific(struct proc *p)
1444 {
1445 int error;
1446
1447 error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
1448 KASSERT(error == 0);
1449 }
1450
1451 /*
1452 * proc_finispecific --
1453 * Finalize a proc's specificdata container.
1454 */
1455 void
1456 proc_finispecific(struct proc *p)
1457 {
1458
1459 specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
1460 }
1461
1462 /*
1463 * proc_getspecific --
1464 * Return proc-specific data corresponding to the specified key.
1465 */
1466 void *
1467 proc_getspecific(struct proc *p, specificdata_key_t key)
1468 {
1469
1470 return (specificdata_getspecific(proc_specificdata_domain,
1471 &p->p_specdataref, key));
1472 }
1473
1474 /*
1475 * proc_setspecific --
1476 * Set proc-specific data corresponding to the specified key.
1477 */
1478 void
1479 proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
1480 {
1481
1482 specificdata_setspecific(proc_specificdata_domain,
1483 &p->p_specdataref, key, data);
1484 }
1485
1486 int
1487 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
1488 {
1489 int r = 0;
1490
1491 if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) ||
1492 kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) {
1493 /*
1494 * suid proc of ours or proc not ours
1495 */
1496 r = EPERM;
1497 } else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) {
1498 /*
1499 * sgid proc has sgid back to us temporarily
1500 */
1501 r = EPERM;
1502 } else {
1503 /*
1504 * our rgid must be in target's group list (ie,
1505 * sub-processes started by a sgid process)
1506 */
1507 int ismember = 0;
1508
1509 if (kauth_cred_ismember_gid(cred,
1510 kauth_cred_getgid(target), &ismember) != 0 ||
1511 !ismember)
1512 r = EPERM;
1513 }
1514
1515 return (r);
1516 }
1517
1518 /*
1519 * sysctl stuff
1520 */
1521
1522 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
1523
1524 static const u_int sysctl_flagmap[] = {
1525 PK_ADVLOCK, P_ADVLOCK,
1526 PK_EXEC, P_EXEC,
1527 PK_NOCLDWAIT, P_NOCLDWAIT,
1528 PK_32, P_32,
1529 PK_CLDSIGIGN, P_CLDSIGIGN,
1530 PK_SUGID, P_SUGID,
1531 0
1532 };
1533
1534 static const u_int sysctl_sflagmap[] = {
1535 PS_NOCLDSTOP, P_NOCLDSTOP,
1536 PS_WEXIT, P_WEXIT,
1537 PS_STOPFORK, P_STOPFORK,
1538 PS_STOPEXEC, P_STOPEXEC,
1539 PS_STOPEXIT, P_STOPEXIT,
1540 0
1541 };
1542
1543 static const u_int sysctl_slflagmap[] = {
1544 PSL_TRACED, P_TRACED,
1545 PSL_FSTRACE, P_FSTRACE,
1546 PSL_CHTRACED, P_CHTRACED,
1547 PSL_SYSCALL, P_SYSCALL,
1548 0
1549 };
1550
1551 static const u_int sysctl_lflagmap[] = {
1552 PL_CONTROLT, P_CONTROLT,
1553 PL_PPWAIT, P_PPWAIT,
1554 0
1555 };
1556
1557 static const u_int sysctl_stflagmap[] = {
1558 PST_PROFIL, P_PROFIL,
1559 0
1560
1561 };
1562
1563 /* used by kern_lwp also */
1564 const u_int sysctl_lwpflagmap[] = {
1565 LW_SINTR, L_SINTR,
1566 LW_SYSTEM, L_SYSTEM,
1567 LW_SA, L_SA, /* WRS ??? */
1568 0
1569 };
1570
1571 /*
1572 * Find the most ``active'' lwp of a process and return it for ps display
1573 * purposes
1574 */
1575 static struct lwp *
1576 proc_active_lwp(struct proc *p)
1577 {
1578 static const int ostat[] = {
1579 0,
1580 2, /* LSIDL */
1581 6, /* LSRUN */
1582 5, /* LSSLEEP */
1583 4, /* LSSTOP */
1584 0, /* LSZOMB */
1585 1, /* LSDEAD */
1586 7, /* LSONPROC */
1587 3 /* LSSUSPENDED */
1588 };
1589
1590 struct lwp *l, *lp = NULL;
1591 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1592 KASSERT(l->l_stat >= 0 && l->l_stat < __arraycount(ostat));
1593 if (lp == NULL ||
1594 ostat[l->l_stat] > ostat[lp->l_stat] ||
1595 (ostat[l->l_stat] == ostat[lp->l_stat] &&
1596 l->l_cpticks > lp->l_cpticks)) {
1597 lp = l;
1598 continue;
1599 }
1600 }
1601 return lp;
1602 }
1603
1604 static int
1605 sysctl_doeproc(SYSCTLFN_ARGS)
1606 {
1607 union {
1608 struct kinfo_proc kproc;
1609 struct kinfo_proc2 kproc2;
1610 } *kbuf;
1611 struct proc *p, *next, *marker;
1612 char *where, *dp;
1613 int type, op, arg, error;
1614 u_int elem_size, kelem_size, elem_count;
1615 size_t buflen, needed;
1616 bool match, zombie, mmmbrains;
1617
1618 if (namelen == 1 && name[0] == CTL_QUERY)
1619 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1620
1621 dp = where = oldp;
1622 buflen = where != NULL ? *oldlenp : 0;
1623 error = 0;
1624 needed = 0;
1625 type = rnode->sysctl_num;
1626
1627 if (type == KERN_PROC) {
1628 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
1629 return (EINVAL);
1630 op = name[0];
1631 if (op != KERN_PROC_ALL)
1632 arg = name[1];
1633 else
1634 arg = 0; /* Quell compiler warning */
1635 elem_count = 0; /* Ditto */
1636 kelem_size = elem_size = sizeof(kbuf->kproc);
1637 } else {
1638 if (namelen != 4)
1639 return (EINVAL);
1640 op = name[0];
1641 arg = name[1];
1642 elem_size = name[2];
1643 elem_count = name[3];
1644 kelem_size = sizeof(kbuf->kproc2);
1645 }
1646
1647 sysctl_unlock();
1648
1649 kbuf = kmem_alloc(sizeof(*kbuf), KM_SLEEP);
1650 marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
1651 marker->p_flag = PK_MARKER;
1652
1653 mutex_enter(proc_lock);
1654 mmmbrains = false;
1655 for (p = LIST_FIRST(&allproc);; p = next) {
1656 if (p == NULL) {
1657 if (!mmmbrains) {
1658 p = LIST_FIRST(&zombproc);
1659 mmmbrains = true;
1660 }
1661 if (p == NULL)
1662 break;
1663 }
1664 next = LIST_NEXT(p, p_list);
1665 if ((p->p_flag & PK_MARKER) != 0)
1666 continue;
1667
1668 /*
1669 * Skip embryonic processes.
1670 */
1671 if (p->p_stat == SIDL)
1672 continue;
1673
1674 mutex_enter(p->p_lock);
1675 error = kauth_authorize_process(l->l_cred,
1676 KAUTH_PROCESS_CANSEE, p,
1677 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
1678 if (error != 0) {
1679 mutex_exit(p->p_lock);
1680 continue;
1681 }
1682
1683 /*
1684 * TODO - make more efficient (see notes below).
1685 * do by session.
1686 */
1687 switch (op) {
1688 case KERN_PROC_PID:
1689 /* could do this with just a lookup */
1690 match = (p->p_pid == (pid_t)arg);
1691 break;
1692
1693 case KERN_PROC_PGRP:
1694 /* could do this by traversing pgrp */
1695 match = (p->p_pgrp->pg_id == (pid_t)arg);
1696 break;
1697
1698 case KERN_PROC_SESSION:
1699 match = (p->p_session->s_sid == (pid_t)arg);
1700 break;
1701
1702 case KERN_PROC_TTY:
1703 match = true;
1704 if (arg == (int) KERN_PROC_TTY_REVOKE) {
1705 if ((p->p_lflag & PL_CONTROLT) == 0 ||
1706 p->p_session->s_ttyp == NULL ||
1707 p->p_session->s_ttyvp != NULL) {
1708 match = false;
1709 }
1710 } else if ((p->p_lflag & PL_CONTROLT) == 0 ||
1711 p->p_session->s_ttyp == NULL) {
1712 if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
1713 match = false;
1714 }
1715 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
1716 match = false;
1717 }
1718 break;
1719
1720 case KERN_PROC_UID:
1721 match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
1722 break;
1723
1724 case KERN_PROC_RUID:
1725 match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
1726 break;
1727
1728 case KERN_PROC_GID:
1729 match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
1730 break;
1731
1732 case KERN_PROC_RGID:
1733 match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
1734 break;
1735
1736 case KERN_PROC_ALL:
1737 match = true;
1738 /* allow everything */
1739 break;
1740
1741 default:
1742 error = EINVAL;
1743 mutex_exit(p->p_lock);
1744 goto cleanup;
1745 }
1746 if (!match) {
1747 mutex_exit(p->p_lock);
1748 continue;
1749 }
1750
1751 /*
1752 * Grab a hold on the process.
1753 */
1754 if (mmmbrains) {
1755 zombie = true;
1756 } else {
1757 zombie = !rw_tryenter(&p->p_reflock, RW_READER);
1758 }
1759 if (zombie) {
1760 LIST_INSERT_AFTER(p, marker, p_list);
1761 }
1762
1763 if (buflen >= elem_size &&
1764 (type == KERN_PROC || elem_count > 0)) {
1765 if (type == KERN_PROC) {
1766 kbuf->kproc.kp_proc = *p;
1767 fill_eproc(p, &kbuf->kproc.kp_eproc, zombie);
1768 } else {
1769 fill_kproc2(p, &kbuf->kproc2, zombie);
1770 elem_count--;
1771 }
1772 mutex_exit(p->p_lock);
1773 mutex_exit(proc_lock);
1774 /*
1775 * Copy out elem_size, but not larger than kelem_size
1776 */
1777 error = sysctl_copyout(l, kbuf, dp,
1778 min(kelem_size, elem_size));
1779 mutex_enter(proc_lock);
1780 if (error) {
1781 goto bah;
1782 }
1783 dp += elem_size;
1784 buflen -= elem_size;
1785 } else {
1786 mutex_exit(p->p_lock);
1787 }
1788 needed += elem_size;
1789
1790 /*
1791 * Release reference to process.
1792 */
1793 if (zombie) {
1794 next = LIST_NEXT(marker, p_list);
1795 LIST_REMOVE(marker, p_list);
1796 } else {
1797 rw_exit(&p->p_reflock);
1798 next = LIST_NEXT(p, p_list);
1799 }
1800 }
1801 mutex_exit(proc_lock);
1802
1803 if (where != NULL) {
1804 *oldlenp = dp - where;
1805 if (needed > *oldlenp) {
1806 error = ENOMEM;
1807 goto out;
1808 }
1809 } else {
1810 needed += KERN_PROCSLOP;
1811 *oldlenp = needed;
1812 }
1813 if (kbuf)
1814 kmem_free(kbuf, sizeof(*kbuf));
1815 if (marker)
1816 kmem_free(marker, sizeof(*marker));
1817 sysctl_relock();
1818 return 0;
1819 bah:
1820 if (zombie)
1821 LIST_REMOVE(marker, p_list);
1822 else
1823 rw_exit(&p->p_reflock);
1824 cleanup:
1825 mutex_exit(proc_lock);
1826 out:
1827 if (kbuf)
1828 kmem_free(kbuf, sizeof(*kbuf));
1829 if (marker)
1830 kmem_free(marker, sizeof(*marker));
1831 sysctl_relock();
1832 return error;
1833 }
1834
1835 int
1836 copyin_psstrings(struct proc *p, struct ps_strings *arginfo)
1837 {
1838
1839 #ifdef COMPAT_NETBSD32
1840 if (p->p_flag & PK_32) {
1841 struct ps_strings32 arginfo32;
1842
1843 int error = copyin_proc(p, (void *)p->p_psstrp, &arginfo32,
1844 sizeof(arginfo32));
1845 if (error)
1846 return error;
1847 arginfo->ps_argvstr = (void *)(uintptr_t)arginfo32.ps_argvstr;
1848 arginfo->ps_nargvstr = arginfo32.ps_nargvstr;
1849 arginfo->ps_envstr = (void *)(uintptr_t)arginfo32.ps_envstr;
1850 arginfo->ps_nenvstr = arginfo32.ps_nenvstr;
1851 return 0;
1852 }
1853 #endif
1854 return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo));
1855 }
1856
1857 static int
1858 copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len)
1859 {
1860 void **cookie = cookie_;
1861 struct lwp *l = cookie[0];
1862 char *dst = cookie[1];
1863
1864 return sysctl_copyout(l, src, dst + off, len);
1865 }
1866
1867 /*
1868 * sysctl helper routine for kern.proc_args pseudo-subtree.
1869 */
1870 static int
1871 sysctl_kern_proc_args(SYSCTLFN_ARGS)
1872 {
1873 struct ps_strings pss;
1874 struct proc *p;
1875 pid_t pid;
1876 int type, error;
1877 void *cookie[2];
1878
1879 if (namelen == 1 && name[0] == CTL_QUERY)
1880 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1881
1882 if (newp != NULL || namelen != 2)
1883 return (EINVAL);
1884 pid = name[0];
1885 type = name[1];
1886
1887 switch (type) {
1888 case KERN_PROC_ARGV:
1889 case KERN_PROC_NARGV:
1890 case KERN_PROC_ENV:
1891 case KERN_PROC_NENV:
1892 /* ok */
1893 break;
1894 default:
1895 return (EINVAL);
1896 }
1897
1898 sysctl_unlock();
1899
1900 /* check pid */
1901 mutex_enter(proc_lock);
1902 if ((p = proc_find(pid)) == NULL) {
1903 error = EINVAL;
1904 goto out_locked;
1905 }
1906 mutex_enter(p->p_lock);
1907
1908 /* Check permission. */
1909 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
1910 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
1911 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
1912 else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
1913 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
1914 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
1915 else
1916 error = EINVAL; /* XXXGCC */
1917 if (error) {
1918 mutex_exit(p->p_lock);
1919 goto out_locked;
1920 }
1921
1922 if (oldp == NULL) {
1923 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
1924 *oldlenp = sizeof (int);
1925 else
1926 *oldlenp = ARG_MAX; /* XXX XXX XXX */
1927 error = 0;
1928 mutex_exit(p->p_lock);
1929 goto out_locked;
1930 }
1931
1932 /*
1933 * Zombies don't have a stack, so we can't read their psstrings.
1934 * System processes also don't have a user stack.
1935 */
1936 if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
1937 error = EINVAL;
1938 mutex_exit(p->p_lock);
1939 goto out_locked;
1940 }
1941
1942 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
1943 mutex_exit(p->p_lock);
1944 if (error) {
1945 goto out_locked;
1946 }
1947 mutex_exit(proc_lock);
1948
1949 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
1950 int value;
1951 if ((error = copyin_psstrings(p, &pss)) == 0) {
1952 if (type == KERN_PROC_NARGV)
1953 value = pss.ps_nargvstr;
1954 else
1955 value = pss.ps_nenvstr;
1956 error = sysctl_copyout(l, &value, oldp, sizeof(value));
1957 *oldlenp = sizeof(value);
1958 }
1959 } else {
1960 cookie[0] = l;
1961 cookie[1] = oldp;
1962 error = copy_procargs(p, type, oldlenp,
1963 copy_procargs_sysctl_cb, cookie);
1964 }
1965 rw_exit(&p->p_reflock);
1966 sysctl_relock();
1967 return error;
1968
1969 out_locked:
1970 mutex_exit(proc_lock);
1971 sysctl_relock();
1972 return error;
1973 }
1974
1975 int
1976 copy_procargs(struct proc *p, int oid, size_t *limit,
1977 int (*cb)(void *, const void *, size_t, size_t), void *cookie)
1978 {
1979 struct ps_strings pss;
1980 size_t len, i, loaded, entry_len;
1981 struct uio auio;
1982 struct iovec aiov;
1983 int error, argvlen;
1984 char *arg;
1985 char **argv;
1986 vaddr_t user_argv;
1987 struct vmspace *vmspace;
1988
1989 /*
1990 * Allocate a temporary buffer to hold the argument vector and
1991 * the arguments themselve.
1992 */
1993 arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
1994 argv = kmem_alloc(PAGE_SIZE, KM_SLEEP);
1995
1996 /*
1997 * Lock the process down in memory.
1998 */
1999 vmspace = p->p_vmspace;
2000 uvmspace_addref(vmspace);
2001
2002 /*
2003 * Read in the ps_strings structure.
2004 */
2005 if ((error = copyin_psstrings(p, &pss)) != 0)
2006 goto done;
2007
2008 /*
2009 * Now read the address of the argument vector.
2010 */
2011 switch (oid) {
2012 case KERN_PROC_ARGV:
2013 user_argv = (uintptr_t)pss.ps_argvstr;
2014 argvlen = pss.ps_nargvstr;
2015 break;
2016 case KERN_PROC_ENV:
2017 user_argv = (uintptr_t)pss.ps_envstr;
2018 argvlen = pss.ps_nenvstr;
2019 break;
2020 default:
2021 error = EINVAL;
2022 goto done;
2023 }
2024
2025 if (argvlen < 0) {
2026 error = EIO;
2027 goto done;
2028 }
2029
2030 #ifdef COMPAT_NETBSD32
2031 if (p->p_flag & PK_32)
2032 entry_len = sizeof(netbsd32_charp);
2033 else
2034 #endif
2035 entry_len = sizeof(char *);
2036
2037 /*
2038 * Now copy each string.
2039 */
2040 len = 0; /* bytes written to user buffer */
2041 loaded = 0; /* bytes from argv already processed */
2042 i = 0; /* To make compiler happy */
2043
2044 for (; argvlen; --argvlen) {
2045 int finished = 0;
2046 vaddr_t base;
2047 size_t xlen;
2048 int j;
2049
2050 if (loaded == 0) {
2051 size_t rem = entry_len * argvlen;
2052 loaded = MIN(rem, PAGE_SIZE);
2053 error = copyin_vmspace(vmspace,
2054 (const void *)user_argv, argv, loaded);
2055 if (error)
2056 break;
2057 user_argv += loaded;
2058 i = 0;
2059 }
2060
2061 #ifdef COMPAT_NETBSD32
2062 if (p->p_flag & PK_32) {
2063 netbsd32_charp *argv32;
2064
2065 argv32 = (netbsd32_charp *)argv;
2066 base = (vaddr_t)NETBSD32PTR64(argv32[i++]);
2067 } else
2068 #endif
2069 base = (vaddr_t)argv[i++];
2070 loaded -= entry_len;
2071
2072 /*
2073 * The program has messed around with its arguments,
2074 * possibly deleting some, and replacing them with
2075 * NULL's. Treat this as the last argument and not
2076 * a failure.
2077 */
2078 if (base == 0)
2079 break;
2080
2081 while (!finished) {
2082 xlen = PAGE_SIZE - (base & PAGE_MASK);
2083
2084 aiov.iov_base = arg;
2085 aiov.iov_len = PAGE_SIZE;
2086 auio.uio_iov = &aiov;
2087 auio.uio_iovcnt = 1;
2088 auio.uio_offset = base;
2089 auio.uio_resid = xlen;
2090 auio.uio_rw = UIO_READ;
2091 UIO_SETUP_SYSSPACE(&auio);
2092 error = uvm_io(&vmspace->vm_map, &auio);
2093 if (error)
2094 goto done;
2095
2096 /* Look for the end of the string */
2097 for (j = 0; j < xlen; j++) {
2098 if (arg[j] == '\0') {
2099 xlen = j + 1;
2100 finished = 1;
2101 break;
2102 }
2103 }
2104
2105 /* Check for user buffer overflow */
2106 if (len + xlen > *limit) {
2107 finished = 1;
2108 if (len > *limit)
2109 xlen = 0;
2110 else
2111 xlen = *limit - len;
2112 }
2113
2114 /* Copyout the page */
2115 error = (*cb)(cookie, arg, len, xlen);
2116 if (error)
2117 goto done;
2118
2119 len += xlen;
2120 base += xlen;
2121 }
2122 }
2123 *limit = len;
2124
2125 done:
2126 kmem_free(argv, PAGE_SIZE);
2127 kmem_free(arg, PAGE_SIZE);
2128 uvmspace_free(vmspace);
2129 return error;
2130 }
2131
2132 /*
2133 * Fill in an eproc structure for the specified process.
2134 */
2135 void
2136 fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
2137 {
2138 struct tty *tp;
2139 struct lwp *l;
2140
2141 KASSERT(mutex_owned(proc_lock));
2142 KASSERT(mutex_owned(p->p_lock));
2143
2144 memset(ep, 0, sizeof(*ep));
2145
2146 ep->e_paddr = p;
2147 ep->e_sess = p->p_session;
2148 if (p->p_cred) {
2149 kauth_cred_topcred(p->p_cred, &ep->e_pcred);
2150 kauth_cred_toucred(p->p_cred, &ep->e_ucred);
2151 }
2152 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2153 struct vmspace *vm = p->p_vmspace;
2154
2155 ep->e_vm.vm_rssize = vm_resident_count(vm);
2156 ep->e_vm.vm_tsize = vm->vm_tsize;
2157 ep->e_vm.vm_dsize = vm->vm_dsize;
2158 ep->e_vm.vm_ssize = vm->vm_ssize;
2159 ep->e_vm.vm_map.size = vm->vm_map.size;
2160
2161 /* Pick the primary (first) LWP */
2162 l = proc_active_lwp(p);
2163 KASSERT(l != NULL);
2164 lwp_lock(l);
2165 if (l->l_wchan)
2166 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
2167 lwp_unlock(l);
2168 }
2169 if (p->p_pptr)
2170 ep->e_ppid = p->p_pptr->p_pid;
2171 if (p->p_pgrp && p->p_session) {
2172 ep->e_pgid = p->p_pgrp->pg_id;
2173 ep->e_jobc = p->p_pgrp->pg_jobc;
2174 ep->e_sid = p->p_session->s_sid;
2175 if ((p->p_lflag & PL_CONTROLT) &&
2176 (tp = ep->e_sess->s_ttyp)) {
2177 ep->e_tdev = tp->t_dev;
2178 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2179 ep->e_tsess = tp->t_session;
2180 } else
2181 ep->e_tdev = (uint32_t)NODEV;
2182 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
2183 if (SESS_LEADER(p))
2184 ep->e_flag |= EPROC_SLEADER;
2185 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
2186 }
2187 ep->e_xsize = ep->e_xrssize = 0;
2188 ep->e_xccount = ep->e_xswrss = 0;
2189 }
2190
2191 /*
2192 * Fill in a kinfo_proc2 structure for the specified process.
2193 */
2194 static void
2195 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie)
2196 {
2197 struct tty *tp;
2198 struct lwp *l, *l2;
2199 struct timeval ut, st, rt;
2200 sigset_t ss1, ss2;
2201 struct rusage ru;
2202 struct vmspace *vm;
2203
2204 KASSERT(mutex_owned(proc_lock));
2205 KASSERT(mutex_owned(p->p_lock));
2206
2207 sigemptyset(&ss1);
2208 sigemptyset(&ss2);
2209 memset(ki, 0, sizeof(*ki));
2210
2211 ki->p_paddr = PTRTOUINT64(p);
2212 ki->p_fd = PTRTOUINT64(p->p_fd);
2213 ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
2214 ki->p_stats = PTRTOUINT64(p->p_stats);
2215 ki->p_limit = PTRTOUINT64(p->p_limit);
2216 ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
2217 ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
2218 ki->p_sess = PTRTOUINT64(p->p_session);
2219 ki->p_tsess = 0; /* may be changed if controlling tty below */
2220 ki->p_ru = PTRTOUINT64(&p->p_stats->p_ru);
2221 ki->p_eflag = 0;
2222 ki->p_exitsig = p->p_exitsig;
2223 ki->p_flag = L_INMEM; /* Process never swapped out */
2224 ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
2225 ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
2226 ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
2227 ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
2228 ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
2229 ki->p_pid = p->p_pid;
2230 if (p->p_pptr)
2231 ki->p_ppid = p->p_pptr->p_pid;
2232 else
2233 ki->p_ppid = 0;
2234 ki->p_uid = kauth_cred_geteuid(p->p_cred);
2235 ki->p_ruid = kauth_cred_getuid(p->p_cred);
2236 ki->p_gid = kauth_cred_getegid(p->p_cred);
2237 ki->p_rgid = kauth_cred_getgid(p->p_cred);
2238 ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
2239 ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
2240 ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
2241 kauth_cred_getgroups(p->p_cred, ki->p_groups,
2242 min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
2243 UIO_SYSSPACE);
2244
2245 ki->p_uticks = p->p_uticks;
2246 ki->p_sticks = p->p_sticks;
2247 ki->p_iticks = p->p_iticks;
2248 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */
2249 ki->p_tracep = PTRTOUINT64(p->p_tracep);
2250 ki->p_traceflag = p->p_traceflag;
2251
2252 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
2253 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
2254
2255 ki->p_cpticks = 0;
2256 ki->p_pctcpu = p->p_pctcpu;
2257 ki->p_estcpu = 0;
2258 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
2259 ki->p_realstat = p->p_stat;
2260 ki->p_nice = p->p_nice;
2261 ki->p_xstat = p->p_xstat;
2262 ki->p_acflag = p->p_acflag;
2263
2264 strncpy(ki->p_comm, p->p_comm,
2265 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
2266 strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
2267
2268 ki->p_nlwps = p->p_nlwps;
2269 ki->p_realflag = ki->p_flag;
2270
2271 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2272 vm = p->p_vmspace;
2273 ki->p_vm_rssize = vm_resident_count(vm);
2274 ki->p_vm_tsize = vm->vm_tsize;
2275 ki->p_vm_dsize = vm->vm_dsize;
2276 ki->p_vm_ssize = vm->vm_ssize;
2277 ki->p_vm_vsize = vm->vm_map.size;
2278 /*
2279 * Since the stack is initially mapped mostly with
2280 * PROT_NONE and grown as needed, adjust the "mapped size"
2281 * to skip the unused stack portion.
2282 */
2283 ki->p_vm_msize =
2284 atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
2285
2286 /* Pick the primary (first) LWP */
2287 l = proc_active_lwp(p);
2288 KASSERT(l != NULL);
2289 lwp_lock(l);
2290 ki->p_nrlwps = p->p_nrlwps;
2291 ki->p_forw = 0;
2292 ki->p_back = 0;
2293 ki->p_addr = PTRTOUINT64(l->l_addr);
2294 ki->p_stat = l->l_stat;
2295 ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
2296 ki->p_swtime = l->l_swtime;
2297 ki->p_slptime = l->l_slptime;
2298 if (l->l_stat == LSONPROC)
2299 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2300 else
2301 ki->p_schedflags = 0;
2302 ki->p_priority = lwp_eprio(l);
2303 ki->p_usrpri = l->l_priority;
2304 if (l->l_wchan)
2305 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
2306 ki->p_wchan = PTRTOUINT64(l->l_wchan);
2307 ki->p_cpuid = cpu_index(l->l_cpu);
2308 lwp_unlock(l);
2309 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2310 /* This is hardly correct, but... */
2311 sigplusset(&l->l_sigpend.sp_set, &ss1);
2312 sigplusset(&l->l_sigmask, &ss2);
2313 ki->p_cpticks += l->l_cpticks;
2314 ki->p_pctcpu += l->l_pctcpu;
2315 ki->p_estcpu += l->l_estcpu;
2316 }
2317 }
2318 sigplusset(&p->p_sigpend.sp_set, &ss2);
2319 memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
2320 memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
2321
2322 if (p->p_session != NULL) {
2323 ki->p_sid = p->p_session->s_sid;
2324 ki->p__pgid = p->p_pgrp->pg_id;
2325 if (p->p_session->s_ttyvp)
2326 ki->p_eflag |= EPROC_CTTY;
2327 if (SESS_LEADER(p))
2328 ki->p_eflag |= EPROC_SLEADER;
2329 strncpy(ki->p_login, p->p_session->s_login,
2330 min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
2331 ki->p_jobc = p->p_pgrp->pg_jobc;
2332 if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2333 ki->p_tdev = tp->t_dev;
2334 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2335 ki->p_tsess = PTRTOUINT64(tp->t_session);
2336 } else {
2337 ki->p_tdev = (int32_t)NODEV;
2338 }
2339 }
2340
2341 if (!P_ZOMBIE(p) && !zombie) {
2342 ki->p_uvalid = 1;
2343 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
2344 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
2345
2346 calcru(p, &ut, &st, NULL, &rt);
2347 ki->p_rtime_sec = rt.tv_sec;
2348 ki->p_rtime_usec = rt.tv_usec;
2349 ki->p_uutime_sec = ut.tv_sec;
2350 ki->p_uutime_usec = ut.tv_usec;
2351 ki->p_ustime_sec = st.tv_sec;
2352 ki->p_ustime_usec = st.tv_usec;
2353
2354 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
2355 ki->p_uru_nvcsw = 0;
2356 ki->p_uru_nivcsw = 0;
2357 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
2358 ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
2359 ki->p_uru_nivcsw += l2->l_nivcsw;
2360 ruadd(&ru, &l2->l_ru);
2361 }
2362 ki->p_uru_maxrss = ru.ru_maxrss;
2363 ki->p_uru_ixrss = ru.ru_ixrss;
2364 ki->p_uru_idrss = ru.ru_idrss;
2365 ki->p_uru_isrss = ru.ru_isrss;
2366 ki->p_uru_minflt = ru.ru_minflt;
2367 ki->p_uru_majflt = ru.ru_majflt;
2368 ki->p_uru_nswap = ru.ru_nswap;
2369 ki->p_uru_inblock = ru.ru_inblock;
2370 ki->p_uru_oublock = ru.ru_oublock;
2371 ki->p_uru_msgsnd = ru.ru_msgsnd;
2372 ki->p_uru_msgrcv = ru.ru_msgrcv;
2373 ki->p_uru_nsignals = ru.ru_nsignals;
2374
2375 timeradd(&p->p_stats->p_cru.ru_utime,
2376 &p->p_stats->p_cru.ru_stime, &ut);
2377 ki->p_uctime_sec = ut.tv_sec;
2378 ki->p_uctime_usec = ut.tv_usec;
2379 }
2380 }
2381