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