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