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