Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.278
      1 /*	$NetBSD: kern_proc.c,v 1.278 2025/03/16 15:52:03 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2006, 2007, 2008, 2020, 2023
      5  *     The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  * NASA Ames Research Center, and by Andrew Doran.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     36  *	The Regents of the University of California.  All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  *
     62  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.278 2025/03/16 15:52:03 riastradh Exp $");
     67 
     68 #ifdef _KERNEL_OPT
     69 #include "opt_kstack.h"
     70 #include "opt_maxuprc.h"
     71 #include "opt_dtrace.h"
     72 #include "opt_compat_netbsd32.h"
     73 #include "opt_kaslr.h"
     74 #endif
     75 
     76 #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
     77     && !defined(_RUMPKERNEL)
     78 #define COMPAT_NETBSD32
     79 #endif
     80 
     81 #include <sys/param.h>
     82 #include <sys/types.h>
     83 
     84 #include <sys/acct.h>
     85 #include <sys/atomic.h>
     86 #include <sys/buf.h>
     87 #include <sys/compat_stub.h>
     88 #include <sys/cpu.h>
     89 #include <sys/dtrace_bsd.h>
     90 #include <sys/exec.h>
     91 #include <sys/file.h>
     92 #include <sys/filedesc.h>
     93 #include <sys/futex.h>
     94 #include <sys/ioctl.h>
     95 #include <sys/kauth.h>
     96 #include <sys/kernel.h>
     97 #include <sys/kmem.h>
     98 #include <sys/namei.h>
     99 #include <sys/pool.h>
    100 #include <sys/proc.h>
    101 #include <sys/pserialize.h>
    102 #include <sys/pset.h>
    103 #include <sys/ras.h>
    104 #include <sys/resourcevar.h>
    105 #include <sys/sdt.h>
    106 #include <sys/signalvar.h>
    107 #include <sys/sleepq.h>
    108 #include <sys/syscall_stats.h>
    109 #include <sys/sysctl.h>
    110 #include <sys/systm.h>
    111 #include <sys/tty.h>
    112 #include <sys/uio.h>
    113 #include <sys/wait.h>
    114 #include <ufs/ufs/quota.h>
    115 
    116 #include <uvm/uvm_extern.h>
    117 
    118 /*
    119  * Process lists.
    120  */
    121 
    122 struct proclist		allproc		__cacheline_aligned;
    123 struct proclist		zombproc	__cacheline_aligned;
    124 
    125 kmutex_t		proc_lock	__cacheline_aligned;
    126 static pserialize_t	proc_psz;
    127 
    128 /*
    129  * pid to lwp/proc lookup is done by indexing the pid_table array.
    130  * Since pid numbers are only allocated when an empty slot
    131  * has been found, there is no need to search any lists ever.
    132  * (an orphaned pgrp will lock the slot, a session will lock
    133  * the pgrp with the same number.)
    134  * If the table is too small it is reallocated with twice the
    135  * previous size and the entries 'unzipped' into the two halves.
    136  * A linked list of free entries is passed through the pt_lwp
    137  * field of 'free' items - set odd to be an invalid ptr.  Two
    138  * additional bits are also used to indicate if the slot is
    139  * currently occupied by a proc or lwp, and if the PID is
    140  * hidden from certain kinds of lookups.  We thus require a
    141  * minimum alignment for proc and lwp structures (LWPs are
    142  * at least 32-byte aligned).
    143  */
    144 
    145 struct pid_table {
    146 	uintptr_t	pt_slot;
    147 	struct pgrp	*pt_pgrp;
    148 	pid_t		pt_pid;
    149 };
    150 
    151 #define	PT_F_FREE		((uintptr_t)__BIT(0))
    152 #define	PT_F_LWP		0	/* pseudo-flag */
    153 #define	PT_F_PROC		((uintptr_t)__BIT(1))
    154 
    155 #define	PT_F_TYPEBITS		(PT_F_FREE|PT_F_PROC)
    156 #define	PT_F_ALLBITS		(PT_F_FREE|PT_F_PROC)
    157 
    158 #define	PT_VALID(s)		(((s) & PT_F_FREE) == 0)
    159 #define	PT_RESERVED(s)		((s) == 0)
    160 #define	PT_NEXT(s)		((u_int)(s) >> 1)
    161 #define	PT_SET_FREE(pid)	(((pid) << 1) | PT_F_FREE)
    162 #define	PT_SET_LWP(l)		((uintptr_t)(l))
    163 #define	PT_SET_PROC(p)		(((uintptr_t)(p)) | PT_F_PROC)
    164 #define	PT_SET_RESERVED		0
    165 #define	PT_GET_LWP(s)		((struct lwp *)((s) & ~PT_F_ALLBITS))
    166 #define	PT_GET_PROC(s)		((struct proc *)((s) & ~PT_F_ALLBITS))
    167 #define	PT_GET_TYPE(s)		((s) & PT_F_TYPEBITS)
    168 #define	PT_IS_LWP(s)		(PT_GET_TYPE(s) == PT_F_LWP && (s) != 0)
    169 #define	PT_IS_PROC(s)		(PT_GET_TYPE(s) == PT_F_PROC)
    170 
    171 #define	MIN_PROC_ALIGNMENT	(PT_F_ALLBITS + 1)
    172 
    173 /*
    174  * Table of process IDs (PIDs).
    175  */
    176 static struct pid_table *pid_table	__read_mostly;
    177 
    178 #define	INITIAL_PID_TABLE_SIZE		(1 << 5)
    179 
    180 /* Table mask, threshold for growing and number of allocated PIDs. */
    181 static u_int		pid_tbl_mask	__read_mostly;
    182 static u_int		pid_alloc_lim	__read_mostly;
    183 static u_int		pid_alloc_cnt	__cacheline_aligned;
    184 
    185 /* Next free, last free and maximum PIDs. */
    186 static u_int		next_free_pt	__cacheline_aligned;
    187 static u_int		last_free_pt	__cacheline_aligned;
    188 static pid_t		pid_max		__read_mostly;
    189 
    190 /* Components of the first process -- never freed. */
    191 
    192 struct session session0 = {
    193 	.s_count = 1,
    194 	.s_sid = 0,
    195 };
    196 struct pgrp pgrp0 = {
    197 	.pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members),
    198 	.pg_session = &session0,
    199 };
    200 filedesc_t filedesc0;
    201 struct cwdinfo cwdi0 = {
    202 	.cwdi_cmask = CMASK,
    203 	.cwdi_refcnt = 1,
    204 };
    205 struct plimit limit0;
    206 struct pstats pstat0;
    207 struct vmspace vmspace0;
    208 struct sigacts sigacts0;
    209 struct proc proc0 = {
    210 	.p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps),
    211 	.p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters),
    212 	.p_nlwps = 1,
    213 	.p_nrlwps = 1,
    214 	.p_pgrp = &pgrp0,
    215 	.p_comm = "system",
    216 	/*
    217 	 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8)
    218 	 * when they exit.  init(8) can easily wait them out for us.
    219 	 */
    220 	.p_flag = PK_SYSTEM | PK_NOCLDWAIT,
    221 	.p_stat = SACTIVE,
    222 	.p_nice = NZERO,
    223 	.p_emul = &emul_netbsd,
    224 	.p_cwdi = &cwdi0,
    225 	.p_limit = &limit0,
    226 	.p_fd = &filedesc0,
    227 	.p_vmspace = &vmspace0,
    228 	.p_stats = &pstat0,
    229 	.p_sigacts = &sigacts0,
    230 #ifdef PROC0_MD_INITIALIZERS
    231 	PROC0_MD_INITIALIZERS
    232 #endif
    233 };
    234 kauth_cred_t cred0;
    235 
    236 static const int	nofile	= NOFILE;
    237 static const int	maxuprc	= MAXUPRC;
    238 
    239 static int sysctl_doeproc(SYSCTLFN_PROTO);
    240 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    241 static int sysctl_security_expose_address(SYSCTLFN_PROTO);
    242 
    243 #ifdef KASLR
    244 static int kern_expose_address = 0;
    245 #else
    246 static int kern_expose_address = 1;
    247 #endif
    248 /*
    249  * The process list descriptors, used during pid allocation and
    250  * by sysctl.  No locking on this data structure is needed since
    251  * it is completely static.
    252  */
    253 const struct proclist_desc proclists[] = {
    254 	{ &allproc	},
    255 	{ &zombproc	},
    256 	{ NULL		},
    257 };
    258 
    259 static struct pgrp *	pg_remove(pid_t);
    260 static void		pg_delete(pid_t);
    261 static void		orphanpg(struct pgrp *);
    262 
    263 static specificdata_domain_t proc_specificdata_domain;
    264 
    265 static pool_cache_t proc_cache;
    266 
    267 static kauth_listener_t proc_listener;
    268 
    269 static void fill_proc(const struct proc *, struct proc *, bool);
    270 static int fill_pathname(struct lwp *, pid_t, void *, size_t *);
    271 static int fill_cwd(struct lwp *, pid_t, void *, size_t *);
    272 
    273 static int
    274 proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    275     void *arg0, void *arg1, void *arg2, void *arg3)
    276 {
    277 	struct proc *p;
    278 	int result;
    279 
    280 	result = KAUTH_RESULT_DEFER;
    281 	p = arg0;
    282 
    283 	switch (action) {
    284 	case KAUTH_PROCESS_CANSEE: {
    285 		enum kauth_process_req req;
    286 
    287 		req = (enum kauth_process_req)(uintptr_t)arg1;
    288 
    289 		switch (req) {
    290 		case KAUTH_REQ_PROCESS_CANSEE_ARGS:
    291 		case KAUTH_REQ_PROCESS_CANSEE_ENTRY:
    292 		case KAUTH_REQ_PROCESS_CANSEE_OPENFILES:
    293 		case KAUTH_REQ_PROCESS_CANSEE_EPROC:
    294 			result = KAUTH_RESULT_ALLOW;
    295 			break;
    296 
    297 		case KAUTH_REQ_PROCESS_CANSEE_ENV:
    298 			if (kauth_cred_getuid(cred) !=
    299 			    kauth_cred_getuid(p->p_cred) ||
    300 			    kauth_cred_getuid(cred) !=
    301 			    kauth_cred_getsvuid(p->p_cred))
    302 				break;
    303 
    304 			result = KAUTH_RESULT_ALLOW;
    305 
    306 			break;
    307 
    308 		case KAUTH_REQ_PROCESS_CANSEE_KPTR:
    309 			if (!kern_expose_address)
    310 				break;
    311 
    312 			if (kern_expose_address == 1 && !(p->p_flag & PK_KMEM))
    313 				break;
    314 
    315 			result = KAUTH_RESULT_ALLOW;
    316 
    317 			break;
    318 
    319 		default:
    320 			break;
    321 		}
    322 
    323 		break;
    324 		}
    325 
    326 	case KAUTH_PROCESS_FORK: {
    327 		int lnprocs = (int)(unsigned long)arg2;
    328 
    329 		/*
    330 		 * Don't allow a nonprivileged user to use the last few
    331 		 * processes. The variable lnprocs is the current number of
    332 		 * processes, maxproc is the limit.
    333 		 */
    334 		if (__predict_false((lnprocs >= maxproc - 5)))
    335 			break;
    336 
    337 		result = KAUTH_RESULT_ALLOW;
    338 
    339 		break;
    340 		}
    341 
    342 	case KAUTH_PROCESS_CORENAME:
    343 	case KAUTH_PROCESS_STOPFLAG:
    344 		if (proc_uidmatch(cred, p->p_cred) == 0)
    345 			result = KAUTH_RESULT_ALLOW;
    346 
    347 		break;
    348 
    349 	default:
    350 		break;
    351 	}
    352 
    353 	return result;
    354 }
    355 
    356 static int
    357 proc_ctor(void *arg __unused, void *obj, int flags __unused)
    358 {
    359 	struct proc *p = obj;
    360 
    361 	memset(p, 0, sizeof(*p));
    362 	klist_init(&p->p_klist);
    363 
    364 	/*
    365 	 * There is no need for a proc_dtor() to do a klist_fini(),
    366 	 * since knote_proc_exit() ensures that p->p_klist is empty
    367 	 * when a process exits.
    368 	 */
    369 
    370 	return 0;
    371 }
    372 
    373 static pid_t proc_alloc_pid_slot(struct proc *, uintptr_t);
    374 
    375 /*
    376  * Initialize global process hashing structures.
    377  */
    378 void
    379 procinit(void)
    380 {
    381 	const struct proclist_desc *pd;
    382 	u_int i;
    383 #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    384 
    385 	for (pd = proclists; pd->pd_list != NULL; pd++)
    386 		LIST_INIT(pd->pd_list);
    387 
    388 	mutex_init(&proc_lock, MUTEX_DEFAULT, IPL_NONE);
    389 
    390 	proc_psz = pserialize_create();
    391 
    392 	pid_table = kmem_alloc(INITIAL_PID_TABLE_SIZE
    393 	    * sizeof(struct pid_table), KM_SLEEP);
    394 	pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    395 	pid_max = PID_MAX;
    396 
    397 	/* Set free list running through table...
    398 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    399 	for (i = 0; i <= pid_tbl_mask; i++) {
    400 		pid_table[i].pt_slot = PT_SET_FREE(LINK_EMPTY + i + 1);
    401 		pid_table[i].pt_pgrp = 0;
    402 		pid_table[i].pt_pid = 0;
    403 	}
    404 	/* slot 0 is just grabbed */
    405 	next_free_pt = 1;
    406 	/* Need to fix last entry. */
    407 	last_free_pt = pid_tbl_mask;
    408 	pid_table[last_free_pt].pt_slot = PT_SET_FREE(LINK_EMPTY);
    409 	/* point at which we grow table - to avoid reusing pids too often */
    410 	pid_alloc_lim = pid_tbl_mask - 1;
    411 #undef LINK_EMPTY
    412 
    413 	/* Reserve PID 1 for init(8). */	/* XXX slightly gross */
    414 	mutex_enter(&proc_lock);
    415 	if (proc_alloc_pid_slot(&proc0, PT_SET_RESERVED) != 1)
    416 		panic("failed to reserve PID 1 for init(8)");
    417 	mutex_exit(&proc_lock);
    418 
    419 	proc_specificdata_domain = specificdata_domain_create();
    420 	KASSERT(proc_specificdata_domain != NULL);
    421 
    422 	size_t proc_alignment = coherency_unit;
    423 	if (proc_alignment < MIN_PROC_ALIGNMENT)
    424 		proc_alignment = MIN_PROC_ALIGNMENT;
    425 
    426 	proc_cache = pool_cache_init(sizeof(struct proc), proc_alignment, 0, 0,
    427 	    "procpl", NULL, IPL_NONE, proc_ctor, NULL, NULL);
    428 
    429 	proc_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    430 	    proc_listener_cb, NULL);
    431 }
    432 
    433 void
    434 procinit_sysctl(void)
    435 {
    436 	static struct sysctllog *clog;
    437 
    438 	sysctl_createv(&clog, 0, NULL, NULL,
    439 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    440 		       CTLTYPE_INT, "expose_address",
    441 		       SYSCTL_DESCR("Enable exposing kernel addresses"),
    442 		       sysctl_security_expose_address, 0,
    443 		       &kern_expose_address, 0, CTL_KERN, CTL_CREATE, CTL_EOL);
    444 	sysctl_createv(&clog, 0, NULL, NULL,
    445 		       CTLFLAG_PERMANENT,
    446 		       CTLTYPE_NODE, "proc",
    447 		       SYSCTL_DESCR("System-wide process information"),
    448 		       sysctl_doeproc, 0, NULL, 0,
    449 		       CTL_KERN, KERN_PROC, CTL_EOL);
    450 	sysctl_createv(&clog, 0, NULL, NULL,
    451 		       CTLFLAG_PERMANENT,
    452 		       CTLTYPE_NODE, "proc2",
    453 		       SYSCTL_DESCR("Machine-independent process information"),
    454 		       sysctl_doeproc, 0, NULL, 0,
    455 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    456 	sysctl_createv(&clog, 0, NULL, NULL,
    457 		       CTLFLAG_PERMANENT,
    458 		       CTLTYPE_NODE, "proc_args",
    459 		       SYSCTL_DESCR("Process argument information"),
    460 		       sysctl_kern_proc_args, 0, NULL, 0,
    461 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    462 
    463 	/*
    464 	  "nodes" under these:
    465 
    466 	  KERN_PROC_ALL
    467 	  KERN_PROC_PID pid
    468 	  KERN_PROC_PGRP pgrp
    469 	  KERN_PROC_SESSION sess
    470 	  KERN_PROC_TTY tty
    471 	  KERN_PROC_UID uid
    472 	  KERN_PROC_RUID uid
    473 	  KERN_PROC_GID gid
    474 	  KERN_PROC_RGID gid
    475 
    476 	  all in all, probably not worth the effort...
    477 	*/
    478 }
    479 
    480 /*
    481  * Initialize process 0.
    482  */
    483 void
    484 proc0_init(void)
    485 {
    486 	struct proc *p;
    487 	struct pgrp *pg;
    488 	struct rlimit *rlim;
    489 	rlim_t lim;
    490 	int i;
    491 
    492 	p = &proc0;
    493 	pg = &pgrp0;
    494 
    495 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    496 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    497 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    498 
    499 	rw_init(&p->p_reflock);
    500 	cv_init(&p->p_waitcv, "wait");
    501 	cv_init(&p->p_lwpcv, "lwpwait");
    502 
    503 	LIST_INSERT_HEAD(&p->p_lwps, &lwp0, l_sibling);
    504 
    505 	KASSERT(lwp0.l_lid == 0);
    506 	pid_table[lwp0.l_lid].pt_slot = PT_SET_LWP(&lwp0);
    507 	LIST_INSERT_HEAD(&allproc, p, p_list);
    508 
    509 	pid_table[lwp0.l_lid].pt_pgrp = pg;
    510 	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
    511 
    512 #ifdef __HAVE_SYSCALL_INTERN
    513 	(*p->p_emul->e_syscall_intern)(p);
    514 #endif
    515 
    516 	/* Create credentials. */
    517 	cred0 = kauth_cred_alloc();
    518 	p->p_cred = cred0;
    519 
    520 	/* Create the CWD info. */
    521 	rw_init(&cwdi0.cwdi_lock);
    522 
    523 	/* Create the limits structures. */
    524 	mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    525 
    526 	rlim = limit0.pl_rlimit;
    527 	for (i = 0; i < __arraycount(limit0.pl_rlimit); i++) {
    528 		rlim[i].rlim_cur = RLIM_INFINITY;
    529 		rlim[i].rlim_max = RLIM_INFINITY;
    530 	}
    531 
    532 	rlim[RLIMIT_NOFILE].rlim_max = maxfiles;
    533 	rlim[RLIMIT_NOFILE].rlim_cur = maxfiles < nofile ? maxfiles : nofile;
    534 
    535 	rlim[RLIMIT_NPROC].rlim_max = maxproc;
    536 	rlim[RLIMIT_NPROC].rlim_cur = maxproc < maxuprc ? maxproc : maxuprc;
    537 
    538 	lim = MIN(VM_MAXUSER_ADDRESS, ctob((rlim_t)uvm_availmem(false)));
    539 	rlim[RLIMIT_RSS].rlim_max = lim;
    540 	rlim[RLIMIT_MEMLOCK].rlim_max = lim;
    541 	rlim[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    542 
    543 	rlim[RLIMIT_NTHR].rlim_max = maxlwp;
    544 	rlim[RLIMIT_NTHR].rlim_cur = maxlwp / 2;
    545 
    546 	/* Note that default core name has zero length. */
    547 	limit0.pl_corename = defcorename;
    548 	limit0.pl_cnlen = 0;
    549 	limit0.pl_refcnt = 1;
    550 	limit0.pl_writeable = false;
    551 	limit0.pl_sv_limit = NULL;
    552 
    553 	/* Configure virtual memory system, set vm rlimits. */
    554 	uvm_init_limits(p);
    555 
    556 	/* Initialize file descriptor table for proc0. */
    557 	fd_init(&filedesc0);
    558 
    559 	/*
    560 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    561 	 * All kernel processes (which never have user space mappings)
    562 	 * share proc0's vmspace, and thus, the kernel pmap.
    563 	 */
    564 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    565 	    trunc_page(VM_MAXUSER_ADDRESS),
    566 #ifdef __USE_TOPDOWN_VM
    567 	    true
    568 #else
    569 	    false
    570 #endif
    571 	    );
    572 
    573 	/* Initialize signal state for proc0. XXX IPL_SCHED */
    574 	mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
    575 	siginit(p);
    576 
    577 	proc_initspecific(p);
    578 	kdtrace_proc_ctor(NULL, p);
    579 }
    580 
    581 /*
    582  * Session reference counting.
    583  */
    584 
    585 void
    586 proc_sesshold(struct session *ss)
    587 {
    588 
    589 	KASSERT(mutex_owned(&proc_lock));
    590 	ss->s_count++;
    591 }
    592 
    593 void
    594 proc_sessrele(struct session *ss)
    595 {
    596 	struct pgrp *pg;
    597 
    598 	KASSERT(mutex_owned(&proc_lock));
    599 	KASSERT(ss->s_count > 0);
    600 
    601 	/*
    602 	 * We keep the pgrp with the same id as the session in order to
    603 	 * stop a process being given the same pid.  Since the pgrp holds
    604 	 * a reference to the session, it must be a 'zombie' pgrp by now.
    605 	 */
    606 	if (--ss->s_count == 0) {
    607 		pg = pg_remove(ss->s_sid);
    608 	} else {
    609 		pg = NULL;
    610 		ss = NULL;
    611 	}
    612 
    613 	mutex_exit(&proc_lock);
    614 
    615 	if (pg)
    616 		kmem_free(pg, sizeof(struct pgrp));
    617 	if (ss)
    618 		kmem_free(ss, sizeof(struct session));
    619 }
    620 
    621 /*
    622  * Check that the specified process group is in the session of the
    623  * specified process.
    624  * Treats -ve ids as process ids.
    625  * Used to validate TIOCSPGRP requests.
    626  */
    627 int
    628 pgid_in_session(struct proc *p, pid_t pg_id)
    629 {
    630 	struct pgrp *pgrp;
    631 	struct session *session;
    632 	int error;
    633 
    634 	if (pg_id <= INT_MIN)
    635 		return SET_ERROR(EINVAL);
    636 
    637 	mutex_enter(&proc_lock);
    638 	if (pg_id < 0) {
    639 		struct proc *p1 = proc_find(-pg_id);
    640 		if (p1 == NULL) {
    641 			error = SET_ERROR(EINVAL);
    642 			goto fail;
    643 		}
    644 		pgrp = p1->p_pgrp;
    645 	} else {
    646 		pgrp = pgrp_find(pg_id);
    647 		if (pgrp == NULL) {
    648 			error = SET_ERROR(EINVAL);
    649 			goto fail;
    650 		}
    651 	}
    652 	session = pgrp->pg_session;
    653 	error = (session != p->p_pgrp->pg_session) ? SET_ERROR(EPERM) : 0;
    654 fail:
    655 	mutex_exit(&proc_lock);
    656 	return error;
    657 }
    658 
    659 /*
    660  * p_inferior: is p an inferior of q?
    661  */
    662 static inline bool
    663 p_inferior(struct proc *p, struct proc *q)
    664 {
    665 
    666 	KASSERT(mutex_owned(&proc_lock));
    667 
    668 	for (; p != q; p = p->p_pptr)
    669 		if (p->p_pid == 0)
    670 			return false;
    671 	return true;
    672 }
    673 
    674 /*
    675  * proc_find_lwp: locate an lwp in said proc by the ID.
    676  *
    677  * => Must be called with p::p_lock held.
    678  * => LSIDL lwps are not returned because they are only partially
    679  *    constructed while occupying the slot.
    680  * => Callers need to be careful about lwp::l_stat of the returned
    681  *    lwp.
    682  */
    683 struct lwp *
    684 proc_find_lwp(proc_t *p, pid_t pid)
    685 {
    686 	struct pid_table *pt;
    687 	unsigned pt_mask;
    688 	struct lwp *l = NULL;
    689 	uintptr_t slot;
    690 	int s;
    691 
    692 	KASSERT(mutex_owned(p->p_lock));
    693 
    694 	/*
    695 	 * Look in the pid_table.  This is done unlocked inside a
    696 	 * pserialize read section covering pid_table's memory
    697 	 * allocation only, so take care to read things in the correct
    698 	 * order:
    699 	 *
    700 	 * 1. First read the table mask -- this only ever increases, in
    701 	 *    expand_pid_table, so a stale value is safely
    702 	 *    conservative.
    703 	 *
    704 	 * 2. Next read the pid table -- this is always set _before_
    705 	 *    the mask increases, so if we see a new table and stale
    706 	 *    mask, the mask is still valid for the table.
    707 	 */
    708 	s = pserialize_read_enter();
    709 	pt_mask = atomic_load_acquire(&pid_tbl_mask);
    710 	pt = &atomic_load_consume(&pid_table)[pid & pt_mask];
    711 	slot = atomic_load_consume(&pt->pt_slot);
    712 	if (__predict_false(!PT_IS_LWP(slot))) {
    713 		pserialize_read_exit(s);
    714 		return NULL;
    715 	}
    716 
    717 	/*
    718 	 * Check to see if the LWP is from the correct process.  We won't
    719 	 * see entries in pid_table from a prior process that also used "p",
    720 	 * by virtue of the fact that allocating "p" means all prior updates
    721 	 * to dependant data structures are visible to this thread.
    722 	 */
    723 	l = PT_GET_LWP(slot);
    724 	if (__predict_false(atomic_load_relaxed(&l->l_proc) != p)) {
    725 		pserialize_read_exit(s);
    726 		return NULL;
    727 	}
    728 
    729 	/*
    730 	 * We now know that p->p_lock holds this LWP stable.
    731 	 *
    732 	 * If the status is not LSIDL, it means the LWP is intended to be
    733 	 * findable by LID and l_lid cannot change behind us.
    734 	 *
    735 	 * No need to acquire the LWP's lock to check for LSIDL, as
    736 	 * p->p_lock must be held to transition in and out of LSIDL.
    737 	 * Any other observed state of is no particular interest.
    738 	 */
    739 	pserialize_read_exit(s);
    740 	return l->l_stat != LSIDL && l->l_lid == pid ? l : NULL;
    741 }
    742 
    743 /*
    744  * proc_find_lwp_unlocked: locate an lwp in said proc by the ID.
    745  *
    746  * => Called in a pserialize read section with no locks held.
    747  * => LSIDL lwps are not returned because they are only partially
    748  *    constructed while occupying the slot.
    749  * => Callers need to be careful about lwp::l_stat of the returned
    750  *    lwp.
    751  * => If an LWP is found, it's returned locked.
    752  */
    753 struct lwp *
    754 proc_find_lwp_unlocked(proc_t *p, pid_t pid)
    755 {
    756 	struct pid_table *pt;
    757 	unsigned pt_mask;
    758 	struct lwp *l = NULL;
    759 	uintptr_t slot;
    760 
    761 	KASSERT(pserialize_in_read_section());
    762 
    763 	/*
    764 	 * Look in the pid_table.  This is done unlocked inside a
    765 	 * pserialize read section covering pid_table's memory
    766 	 * allocation only, so take care to read things in the correct
    767 	 * order:
    768 	 *
    769 	 * 1. First read the table mask -- this only ever increases, in
    770 	 *    expand_pid_table, so a stale value is safely
    771 	 *    conservative.
    772 	 *
    773 	 * 2. Next read the pid table -- this is always set _before_
    774 	 *    the mask increases, so if we see a new table and stale
    775 	 *    mask, the mask is still valid for the table.
    776 	 */
    777 	pt_mask = atomic_load_acquire(&pid_tbl_mask);
    778 	pt = &atomic_load_consume(&pid_table)[pid & pt_mask];
    779 	slot = atomic_load_consume(&pt->pt_slot);
    780 	if (__predict_false(!PT_IS_LWP(slot))) {
    781 		return NULL;
    782 	}
    783 
    784 	/*
    785 	 * Lock the LWP we found to get it stable.  If it's embryonic or
    786 	 * reaped (LSIDL) then none of the other fields can safely be
    787 	 * checked.
    788 	 */
    789 	l = PT_GET_LWP(slot);
    790 	lwp_lock(l);
    791 	if (__predict_false(l->l_stat == LSIDL)) {
    792 		lwp_unlock(l);
    793 		return NULL;
    794 	}
    795 
    796 	/*
    797 	 * l_proc and l_lid are now known stable because the LWP is not
    798 	 * LSIDL, so check those fields too to make sure we found the
    799 	 * right thing.
    800 	 */
    801 	if (__predict_false(l->l_proc != p || l->l_lid != pid)) {
    802 		lwp_unlock(l);
    803 		return NULL;
    804 	}
    805 
    806 	/* Everything checks out, return it locked. */
    807 	return l;
    808 }
    809 
    810 /*
    811  * proc_find_lwp_acquire_proc: locate an lwp and acquire a lock
    812  * on its containing proc.
    813  *
    814  * => Similar to proc_find_lwp(), but does not require you to have
    815  *    the proc a priori.
    816  * => Also returns proc * to caller, with p::p_lock held.
    817  * => Same caveats apply.
    818  */
    819 struct lwp *
    820 proc_find_lwp_acquire_proc(pid_t pid, struct proc **pp)
    821 {
    822 	struct pid_table *pt;
    823 	struct proc *p = NULL;
    824 	struct lwp *l = NULL;
    825 	uintptr_t slot;
    826 
    827 	KASSERT(pp != NULL);
    828 	mutex_enter(&proc_lock);
    829 	pt = &pid_table[pid & pid_tbl_mask];
    830 
    831 	slot = pt->pt_slot;
    832 	if (__predict_true(PT_IS_LWP(slot) && pt->pt_pid == pid)) {
    833 		l = PT_GET_LWP(slot);
    834 		p = l->l_proc;
    835 		mutex_enter(p->p_lock);
    836 		if (__predict_false(l->l_stat == LSIDL)) {
    837 			mutex_exit(p->p_lock);
    838 			l = NULL;
    839 			p = NULL;
    840 		}
    841 	}
    842 	mutex_exit(&proc_lock);
    843 
    844 	KASSERT(p == NULL || mutex_owned(p->p_lock));
    845 	*pp = p;
    846 	return l;
    847 }
    848 
    849 /*
    850  * proc_find_raw_pid_table_locked: locate a process by the ID.
    851  *
    852  * => Must be called with proc_lock held.
    853  */
    854 static proc_t *
    855 proc_find_raw_pid_table_locked(pid_t pid, bool any_lwpid)
    856 {
    857 	struct pid_table *pt;
    858 	proc_t *p = NULL;
    859 	uintptr_t slot;
    860 
    861 	/* No - used by DDB.  KASSERT(mutex_owned(&proc_lock)); */
    862 	pt = &pid_table[pid & pid_tbl_mask];
    863 
    864 	slot = pt->pt_slot;
    865 	if (__predict_true(PT_IS_LWP(slot) && pt->pt_pid == pid)) {
    866 		/*
    867 		 * When looking up processes, require a direct match
    868 		 * on the PID assigned to the proc, not just one of
    869 		 * its LWPs.
    870 		 *
    871 		 * N.B. We require lwp::l_proc of LSIDL LWPs to be
    872 		 * valid here.
    873 		 */
    874 		p = PT_GET_LWP(slot)->l_proc;
    875 		if (__predict_false(p->p_pid != pid && !any_lwpid))
    876 			p = NULL;
    877 	} else if (PT_IS_PROC(slot) && pt->pt_pid == pid) {
    878 		p = PT_GET_PROC(slot);
    879 	}
    880 	return p;
    881 }
    882 
    883 proc_t *
    884 proc_find_raw(pid_t pid)
    885 {
    886 
    887 	return proc_find_raw_pid_table_locked(pid, false);
    888 }
    889 
    890 static proc_t *
    891 proc_find_internal(pid_t pid, bool any_lwpid)
    892 {
    893 	proc_t *p;
    894 
    895 	KASSERT(mutex_owned(&proc_lock));
    896 
    897 	p = proc_find_raw_pid_table_locked(pid, any_lwpid);
    898 	if (__predict_false(p == NULL)) {
    899 		return NULL;
    900 	}
    901 
    902 	/*
    903 	 * Only allow live processes to be found by PID.
    904 	 * XXX: p_stat might change, since proc unlocked.
    905 	 */
    906 	if (__predict_true(p->p_stat == SACTIVE || p->p_stat == SSTOP)) {
    907 		return p;
    908 	}
    909 	return NULL;
    910 }
    911 
    912 proc_t *
    913 proc_find(pid_t pid)
    914 {
    915 	return proc_find_internal(pid, false);
    916 }
    917 
    918 proc_t *
    919 proc_find_lwpid(pid_t pid)
    920 {
    921 	return proc_find_internal(pid, true);
    922 }
    923 
    924 /*
    925  * pgrp_find: locate a process group by the ID.
    926  *
    927  * => Must be called with proc_lock held.
    928  */
    929 struct pgrp *
    930 pgrp_find(pid_t pgid)
    931 {
    932 	struct pgrp *pg;
    933 
    934 	KASSERT(mutex_owned(&proc_lock));
    935 
    936 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    937 
    938 	/*
    939 	 * Cannot look up a process group that only exists because the
    940 	 * session has not died yet (traditional).
    941 	 */
    942 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    943 		return NULL;
    944 	}
    945 	return pg;
    946 }
    947 
    948 static void
    949 expand_pid_table(void)
    950 {
    951 	size_t pt_size, tsz;
    952 	struct pid_table *n_pt, *new_pt;
    953 	uintptr_t slot;
    954 	struct pgrp *pgrp;
    955 	pid_t pid, rpid;
    956 	u_int i;
    957 	uint new_pt_mask;
    958 
    959 	KASSERT(mutex_owned(&proc_lock));
    960 
    961 	/* Unlock the pid_table briefly to allocate memory. */
    962 	pt_size = pid_tbl_mask + 1;
    963 	mutex_exit(&proc_lock);
    964 
    965 	tsz = pt_size * 2 * sizeof(struct pid_table);
    966 	new_pt = kmem_alloc(tsz, KM_SLEEP);
    967 	new_pt_mask = pt_size * 2 - 1;
    968 
    969 	/* XXX For now.  The pratical limit is much lower anyway. */
    970 	KASSERT(new_pt_mask <= FUTEX_TID_MASK);
    971 
    972 	mutex_enter(&proc_lock);
    973 	if (pt_size != pid_tbl_mask + 1) {
    974 		/* Another process beat us to it... */
    975 		mutex_exit(&proc_lock);
    976 		kmem_free(new_pt, tsz);
    977 		goto out;
    978 	}
    979 
    980 	/*
    981 	 * Copy entries from old table into new one.
    982 	 * If 'pid' is 'odd' we need to place in the upper half,
    983 	 * even pid's to the lower half.
    984 	 * Free items stay in the low half so we don't have to
    985 	 * fixup the reference to them.
    986 	 * We stuff free items on the front of the freelist
    987 	 * because we can't write to unmodified entries.
    988 	 * Processing the table backwards maintains a semblance
    989 	 * of issuing pid numbers that increase with time.
    990 	 */
    991 	i = pt_size - 1;
    992 	n_pt = new_pt + i;
    993 	for (; ; i--, n_pt--) {
    994 		slot = pid_table[i].pt_slot;
    995 		pgrp = pid_table[i].pt_pgrp;
    996 		if (!PT_VALID(slot)) {
    997 			/* Up 'use count' so that link is valid */
    998 			pid = (PT_NEXT(slot) + pt_size) & ~pt_size;
    999 			rpid = 0;
   1000 			slot = PT_SET_FREE(pid);
   1001 			if (pgrp)
   1002 				pid = pgrp->pg_id;
   1003 		} else {
   1004 			pid = pid_table[i].pt_pid;
   1005 			rpid = pid;
   1006 		}
   1007 
   1008 		/* Save entry in appropriate half of table */
   1009 		n_pt[pid & pt_size].pt_slot = slot;
   1010 		n_pt[pid & pt_size].pt_pgrp = pgrp;
   1011 		n_pt[pid & pt_size].pt_pid = rpid;
   1012 
   1013 		/* Put other piece on start of free list */
   1014 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
   1015 		n_pt[pid & pt_size].pt_slot =
   1016 			PT_SET_FREE((pid & ~pt_size) | next_free_pt);
   1017 		n_pt[pid & pt_size].pt_pgrp = 0;
   1018 		n_pt[pid & pt_size].pt_pid = 0;
   1019 
   1020 		next_free_pt = i | (pid & pt_size);
   1021 		if (i == 0)
   1022 			break;
   1023 	}
   1024 
   1025 	/* Save old table size and switch tables */
   1026 	tsz = pt_size * sizeof(struct pid_table);
   1027 	n_pt = pid_table;
   1028 	atomic_store_release(&pid_table, new_pt);
   1029 	KASSERT(new_pt_mask >= pid_tbl_mask);
   1030 	atomic_store_release(&pid_tbl_mask, new_pt_mask);
   1031 
   1032 	/*
   1033 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
   1034 	 * allocated pids we need it to be larger!
   1035 	 */
   1036 	if (pid_tbl_mask > PID_MAX) {
   1037 		pid_max = pid_tbl_mask * 2 + 1;
   1038 		pid_alloc_lim |= pid_alloc_lim << 1;
   1039 	} else
   1040 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
   1041 
   1042 	mutex_exit(&proc_lock);
   1043 
   1044 	/*
   1045 	 * Make sure that unlocked access to the old pid_table is complete
   1046 	 * and then free it.
   1047 	 */
   1048 	pserialize_perform(proc_psz);
   1049 	kmem_free(n_pt, tsz);
   1050 
   1051  out:	/* Return with proc_lock held again. */
   1052 	mutex_enter(&proc_lock);
   1053 }
   1054 
   1055 struct proc *
   1056 proc_alloc(void)
   1057 {
   1058 	struct proc *p;
   1059 
   1060 	p = pool_cache_get(proc_cache, PR_WAITOK);
   1061 	p->p_stat = SIDL;			/* protect against others */
   1062 	proc_initspecific(p);
   1063 	kdtrace_proc_ctor(NULL, p);
   1064 
   1065 	/*
   1066 	 * Allocate a placeholder in the pid_table.  When we create the
   1067 	 * first LWP for this process, it will take ownership of the
   1068 	 * slot.
   1069 	 */
   1070 	if (__predict_false(proc_alloc_pid(p) == -1)) {
   1071 		/* Allocating the PID failed; unwind. */
   1072 		proc_finispecific(p);
   1073 		proc_free_mem(p);
   1074 		p = NULL;
   1075 	}
   1076 	return p;
   1077 }
   1078 
   1079 /*
   1080  * proc_alloc_pid_slot: allocate PID and record the occupant so that
   1081  * proc_find_raw() can find it by the PID.
   1082  */
   1083 static pid_t __noinline
   1084 proc_alloc_pid_slot(struct proc *p, uintptr_t slot)
   1085 {
   1086 	struct pid_table *pt;
   1087 	pid_t pid;
   1088 	int nxt;
   1089 
   1090 	KASSERT(mutex_owned(&proc_lock));
   1091 
   1092 	for (;;expand_pid_table()) {
   1093 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim)) {
   1094 			/* ensure pids cycle through 2000+ values */
   1095 			continue;
   1096 		}
   1097 		/*
   1098 		 * The first user process *must* be given PID 1.
   1099 		 * it has already been reserved for us.  This
   1100 		 * will be coming in from the proc_alloc() call
   1101 		 * above, and the entry will be usurped later when
   1102 		 * the first user LWP is created.
   1103 		 * XXX this is slightly gross.
   1104 		 */
   1105 		if (__predict_false(PT_RESERVED(pid_table[1].pt_slot) &&
   1106 				    p != &proc0)) {
   1107 			KASSERT(PT_IS_PROC(slot));
   1108 			pt = &pid_table[1];
   1109 			pt->pt_slot = slot;
   1110 			return 1;
   1111 		}
   1112 		pt = &pid_table[next_free_pt];
   1113 #ifdef DIAGNOSTIC
   1114 		if (__predict_false(PT_VALID(pt->pt_slot) || pt->pt_pgrp))
   1115 			panic("proc_alloc: slot busy");
   1116 #endif
   1117 		nxt = PT_NEXT(pt->pt_slot);
   1118 		if (nxt & pid_tbl_mask)
   1119 			break;
   1120 		/* Table full - expand (NB last entry not used....) */
   1121 	}
   1122 
   1123 	/* pid is 'saved use count' + 'size' + entry */
   1124 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
   1125 	if ((uint)pid > (uint)pid_max)
   1126 		pid &= pid_tbl_mask;
   1127 	next_free_pt = nxt & pid_tbl_mask;
   1128 
   1129 	/* XXX For now.  The pratical limit is much lower anyway. */
   1130 	KASSERT(pid <= FUTEX_TID_MASK);
   1131 
   1132 	/* Grab table slot */
   1133 	pt->pt_slot = slot;
   1134 
   1135 	KASSERT(pt->pt_pid == 0);
   1136 	pt->pt_pid = pid;
   1137 	pid_alloc_cnt++;
   1138 
   1139 	return pid;
   1140 }
   1141 
   1142 pid_t
   1143 proc_alloc_pid(struct proc *p)
   1144 {
   1145 	pid_t pid;
   1146 
   1147 	KASSERT((((uintptr_t)p) & PT_F_ALLBITS) == 0);
   1148 	KASSERT(p->p_stat == SIDL);
   1149 
   1150 	mutex_enter(&proc_lock);
   1151 	pid = proc_alloc_pid_slot(p, PT_SET_PROC(p));
   1152 	if (pid != -1)
   1153 		p->p_pid = pid;
   1154 	mutex_exit(&proc_lock);
   1155 
   1156 	return pid;
   1157 }
   1158 
   1159 pid_t
   1160 proc_alloc_lwpid(struct proc *p, struct lwp *l)
   1161 {
   1162 	struct pid_table *pt;
   1163 	pid_t pid;
   1164 
   1165 	KASSERT((((uintptr_t)l) & PT_F_ALLBITS) == 0);
   1166 	KASSERT(l->l_proc == p);
   1167 	KASSERT(l->l_stat == LSIDL);
   1168 
   1169 	/*
   1170 	 * For unlocked lookup in proc_find_lwp(), make sure l->l_proc
   1171 	 * is globally visible before the LWP becomes visible via the
   1172 	 * pid_table.
   1173 	 */
   1174 #ifndef __HAVE_ATOMIC_AS_MEMBAR
   1175 	membar_producer();
   1176 #endif
   1177 
   1178 	/*
   1179 	 * If the slot for p->p_pid currently points to the proc,
   1180 	 * then we should usurp this ID for the LWP.  This happens
   1181 	 * at least once per process (for the first LWP), and can
   1182 	 * happen again if the first LWP for a process exits and
   1183 	 * before the process creates another.
   1184 	 */
   1185 	mutex_enter(&proc_lock);
   1186 	pid = p->p_pid;
   1187 	pt = &pid_table[pid & pid_tbl_mask];
   1188 	KASSERT(pt->pt_pid == pid);
   1189 	if (PT_IS_PROC(pt->pt_slot)) {
   1190 		KASSERT(PT_GET_PROC(pt->pt_slot) == p);
   1191 		l->l_lid = pid;
   1192 		pt->pt_slot = PT_SET_LWP(l);
   1193 	} else {
   1194 		/* Need to allocate a new slot. */
   1195 		pid = proc_alloc_pid_slot(p, PT_SET_LWP(l));
   1196 		if (pid != -1)
   1197 			l->l_lid = pid;
   1198 	}
   1199 	mutex_exit(&proc_lock);
   1200 
   1201 	return pid;
   1202 }
   1203 
   1204 static void __noinline
   1205 proc_free_pid_internal(pid_t pid, uintptr_t type __diagused)
   1206 {
   1207 	struct pid_table *pt;
   1208 
   1209 	KASSERT(mutex_owned(&proc_lock));
   1210 
   1211 	pt = &pid_table[pid & pid_tbl_mask];
   1212 
   1213 	KASSERT(PT_GET_TYPE(pt->pt_slot) == type);
   1214 	KASSERT(pt->pt_pid == pid);
   1215 
   1216 	/* save pid use count in slot */
   1217 	pt->pt_slot = PT_SET_FREE(pid & ~pid_tbl_mask);
   1218 	pt->pt_pid = 0;
   1219 
   1220 	if (pt->pt_pgrp == NULL) {
   1221 		/* link last freed entry onto ours */
   1222 		pid &= pid_tbl_mask;
   1223 		pt = &pid_table[last_free_pt];
   1224 		pt->pt_slot = PT_SET_FREE(PT_NEXT(pt->pt_slot) | pid);
   1225 		pt->pt_pid = 0;
   1226 		last_free_pt = pid;
   1227 		pid_alloc_cnt--;
   1228 	}
   1229 }
   1230 
   1231 /*
   1232  * Free a process id - called from proc_free (in kern_exit.c)
   1233  *
   1234  * Called with the proc_lock held.
   1235  */
   1236 void
   1237 proc_free_pid(pid_t pid)
   1238 {
   1239 
   1240 	KASSERT(mutex_owned(&proc_lock));
   1241 	proc_free_pid_internal(pid, PT_F_PROC);
   1242 }
   1243 
   1244 /*
   1245  * Free a process id used by an LWP.  If this was the process's
   1246  * first LWP, we convert the slot to point to the process; the
   1247  * entry will get cleaned up later when the process finishes exiting.
   1248  *
   1249  * If not, then it's the same as proc_free_pid().
   1250  */
   1251 void
   1252 proc_free_lwpid(struct proc *p, pid_t pid)
   1253 {
   1254 
   1255 	KASSERT(mutex_owned(&proc_lock));
   1256 
   1257 	if (__predict_true(p->p_pid == pid)) {
   1258 		struct pid_table *pt;
   1259 
   1260 		pt = &pid_table[pid & pid_tbl_mask];
   1261 
   1262 		KASSERT(pt->pt_pid == pid);
   1263 		KASSERT(PT_IS_LWP(pt->pt_slot));
   1264 		KASSERT(PT_GET_LWP(pt->pt_slot)->l_proc == p);
   1265 
   1266 		pt->pt_slot = PT_SET_PROC(p);
   1267 		return;
   1268 	}
   1269 	proc_free_pid_internal(pid, PT_F_LWP);
   1270 }
   1271 
   1272 void
   1273 proc_free_mem(struct proc *p)
   1274 {
   1275 
   1276 	kdtrace_proc_dtor(NULL, p);
   1277 	pool_cache_put(proc_cache, p);
   1278 }
   1279 
   1280 /*
   1281  * proc_enterpgrp: move p to a new or existing process group (and session).
   1282  *
   1283  * If we are creating a new pgrp, the pgid should equal
   1284  * the calling process' pid.
   1285  * If is only valid to enter a process group that is in the session
   1286  * of the process.
   1287  * Also mksess should only be set if we are creating a process group
   1288  *
   1289  * Only called from sys_setsid, sys_setpgid and posix_spawn/spawn_return.
   1290  */
   1291 int
   1292 proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess)
   1293 {
   1294 	struct pgrp *new_pgrp, *pgrp;
   1295 	struct session *sess;
   1296 	struct proc *p;
   1297 	int rval;
   1298 	pid_t pg_id = NO_PGID;
   1299 
   1300 	/* Allocate data areas we might need before doing any validity checks */
   1301 	sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL;
   1302 	new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP);
   1303 
   1304 	mutex_enter(&proc_lock);
   1305 
   1306 	/* Check pgrp exists or can be created */
   1307 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
   1308 	if (pgrp != NULL && pgrp->pg_id != pgid)
   1309 		goto eperm;
   1310 
   1311 	/* Can only set another process under restricted circumstances. */
   1312 	if (pid != curp->p_pid) {
   1313 		/* Must exist and be one of our children... */
   1314 		p = proc_find_internal(pid, false);
   1315 		if (p == NULL || !p_inferior(p, curp)) {
   1316 			rval = SET_ERROR(ESRCH);
   1317 			goto done;
   1318 		}
   1319 		/* ... in the same session... */
   1320 		if (sess != NULL || p->p_session != curp->p_session)
   1321 			goto eperm;
   1322 		/* ... existing pgid must be in same session ... */
   1323 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
   1324 			goto eperm;
   1325 		/* ... and not done an exec. */
   1326 		if (p->p_flag & PK_EXEC) {
   1327 			rval = SET_ERROR(EACCES);
   1328 			goto done;
   1329 		}
   1330 	} else {
   1331 		/* ... setsid() cannot re-enter a pgrp */
   1332 		if (mksess && (curp->p_pgid == curp->p_pid ||
   1333 		    pgrp_find(curp->p_pid)))
   1334 			goto eperm;
   1335 		p = curp;
   1336 	}
   1337 
   1338 	/* Changing the process group/session of a session
   1339 	   leader is definitely off limits. */
   1340 	if (SESS_LEADER(p)) {
   1341 		if (sess == NULL && p->p_pgrp == pgrp)
   1342 			/* unless it's a definite noop */
   1343 			rval = 0;
   1344 		goto eperm;
   1345 	}
   1346 
   1347 	/* Can only create a process group with id of process */
   1348 	if (pgrp == NULL && pgid != pid)
   1349 		goto eperm;
   1350 
   1351 	/* Can only create a session if creating pgrp */
   1352 	if (sess != NULL && pgrp != NULL)
   1353 		goto eperm;
   1354 
   1355 	/* Check we allocated memory for a pgrp... */
   1356 	if (pgrp == NULL && new_pgrp == NULL)
   1357 		goto eperm;
   1358 
   1359 	/* Don't attach to 'zombie' pgrp */
   1360 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
   1361 		goto eperm;
   1362 
   1363 	/* Expect to succeed now */
   1364 	rval = 0;
   1365 
   1366 	if (pgrp == p->p_pgrp)
   1367 		/* nothing to do */
   1368 		goto eperm;
   1369 
   1370 	/* Ok all setup, link up required structures */
   1371 
   1372 	if (pgrp == NULL) {
   1373 		pgrp = new_pgrp;
   1374 		new_pgrp = NULL;
   1375 		if (sess != NULL) {
   1376 			sess->s_sid = p->p_pid;
   1377 			sess->s_leader = p;
   1378 			sess->s_count = 1;
   1379 			sess->s_ttyvp = NULL;
   1380 			sess->s_ttyp = NULL;
   1381 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
   1382 			memcpy(sess->s_login, p->p_session->s_login,
   1383 			    sizeof(sess->s_login));
   1384 			p->p_lflag &= ~PL_CONTROLT;
   1385 		} else {
   1386 			sess = p->p_pgrp->pg_session;
   1387 			proc_sesshold(sess);
   1388 		}
   1389 		pgrp->pg_session = sess;
   1390 		sess = NULL;
   1391 
   1392 		pgrp->pg_id = pgid;
   1393 		LIST_INIT(&pgrp->pg_members);
   1394 #ifdef DIAGNOSTIC
   1395 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
   1396 			panic("enterpgrp: pgrp table slot in use");
   1397 		if (__predict_false(mksess && p != curp))
   1398 			panic("enterpgrp: mksession and p != curproc");
   1399 #endif
   1400 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
   1401 		pgrp->pg_jobc = 0;
   1402 	}
   1403 
   1404 	/*
   1405 	 * Adjust eligibility of affected pgrps to participate in job control.
   1406 	 * Increment eligibility counts before decrementing, otherwise we
   1407 	 * could reach 0 spuriously during the first call.
   1408 	 */
   1409 	fixjobc(p, pgrp, 1);
   1410 	fixjobc(p, p->p_pgrp, 0);
   1411 
   1412 	/* Interlock with ttread(). */
   1413 	mutex_spin_enter(&tty_lock);
   1414 
   1415 	/* Move process to requested group. */
   1416 	LIST_REMOVE(p, p_pglist);
   1417 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
   1418 		/* defer delete until we've dumped the lock */
   1419 		pg_id = p->p_pgrp->pg_id;
   1420 	p->p_pgrp = pgrp;
   1421 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
   1422 
   1423 	/* Done with the swap; we can release the tty mutex. */
   1424 	mutex_spin_exit(&tty_lock);
   1425 	goto done;
   1426 
   1427 eperm:
   1428 	rval = SET_ERROR(EPERM);
   1429 done:
   1430 	if (pg_id != NO_PGID) {
   1431 		/* Releases proc_lock. */
   1432 		pg_delete(pg_id);
   1433 	} else {
   1434 		mutex_exit(&proc_lock);
   1435 	}
   1436 	if (sess != NULL)
   1437 		kmem_free(sess, sizeof(*sess));
   1438 	if (new_pgrp != NULL)
   1439 		kmem_free(new_pgrp, sizeof(*new_pgrp));
   1440 #ifdef DEBUG_PGRP
   1441 	if (__predict_false(rval))
   1442 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
   1443 			pid, pgid, mksess, curp->p_pid, rval);
   1444 #endif
   1445 	return rval;
   1446 }
   1447 
   1448 /*
   1449  * proc_leavepgrp: remove a process from its process group.
   1450  *  => must be called with the proc_lock held, which will be released;
   1451  */
   1452 void
   1453 proc_leavepgrp(struct proc *p)
   1454 {
   1455 	struct pgrp *pgrp;
   1456 
   1457 	KASSERT(mutex_owned(&proc_lock));
   1458 
   1459 	/* Interlock with ttread() */
   1460 	mutex_spin_enter(&tty_lock);
   1461 	pgrp = p->p_pgrp;
   1462 	LIST_REMOVE(p, p_pglist);
   1463 	p->p_pgrp = NULL;
   1464 	mutex_spin_exit(&tty_lock);
   1465 
   1466 	if (LIST_EMPTY(&pgrp->pg_members)) {
   1467 		/* Releases proc_lock. */
   1468 		pg_delete(pgrp->pg_id);
   1469 	} else {
   1470 		mutex_exit(&proc_lock);
   1471 	}
   1472 }
   1473 
   1474 /*
   1475  * pg_remove: remove a process group from the table.
   1476  *  => must be called with the proc_lock held;
   1477  *  => returns process group to free;
   1478  */
   1479 static struct pgrp *
   1480 pg_remove(pid_t pg_id)
   1481 {
   1482 	struct pgrp *pgrp;
   1483 	struct pid_table *pt;
   1484 
   1485 	KASSERT(mutex_owned(&proc_lock));
   1486 
   1487 	pt = &pid_table[pg_id & pid_tbl_mask];
   1488 	pgrp = pt->pt_pgrp;
   1489 
   1490 	KASSERT(pgrp != NULL);
   1491 	KASSERT(pgrp->pg_id == pg_id);
   1492 	KASSERT(LIST_EMPTY(&pgrp->pg_members));
   1493 
   1494 	pt->pt_pgrp = NULL;
   1495 
   1496 	if (!PT_VALID(pt->pt_slot)) {
   1497 		/* Orphaned pgrp, put slot onto free list. */
   1498 		KASSERT((PT_NEXT(pt->pt_slot) & pid_tbl_mask) == 0);
   1499 		pg_id &= pid_tbl_mask;
   1500 		pt = &pid_table[last_free_pt];
   1501 		pt->pt_slot = PT_SET_FREE(PT_NEXT(pt->pt_slot) | pg_id);
   1502 		KASSERT(pt->pt_pid == 0);
   1503 		last_free_pt = pg_id;
   1504 		pid_alloc_cnt--;
   1505 	}
   1506 	return pgrp;
   1507 }
   1508 
   1509 /*
   1510  * pg_delete: delete and free a process group.
   1511  *  => must be called with the proc_lock held, which will be released.
   1512  */
   1513 static void
   1514 pg_delete(pid_t pg_id)
   1515 {
   1516 	struct pgrp *pg;
   1517 	struct tty *ttyp;
   1518 	struct session *ss;
   1519 
   1520 	KASSERT(mutex_owned(&proc_lock));
   1521 
   1522 	pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
   1523 	if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) {
   1524 		mutex_exit(&proc_lock);
   1525 		return;
   1526 	}
   1527 
   1528 	ss = pg->pg_session;
   1529 
   1530 	/* Remove reference (if any) from tty to this process group */
   1531 	mutex_spin_enter(&tty_lock);
   1532 	ttyp = ss->s_ttyp;
   1533 	if (ttyp != NULL && ttyp->t_pgrp == pg) {
   1534 		ttyp->t_pgrp = NULL;
   1535 		KASSERT(ttyp->t_session == ss);
   1536 	}
   1537 	mutex_spin_exit(&tty_lock);
   1538 
   1539 	/*
   1540 	 * The leading process group in a session is freed by proc_sessrele(),
   1541 	 * if last reference.  It will also release the locks.
   1542 	 */
   1543 	pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL;
   1544 	proc_sessrele(ss);
   1545 
   1546 	if (pg != NULL) {
   1547 		/* Free it, if was not done above. */
   1548 		kmem_free(pg, sizeof(struct pgrp));
   1549 	}
   1550 }
   1551 
   1552 /*
   1553  * Adjust pgrp jobc counters when specified process changes process group.
   1554  * We count the number of processes in each process group that "qualify"
   1555  * the group for terminal job control (those with a parent in a different
   1556  * process group of the same session).  If that count reaches zero, the
   1557  * process group becomes orphaned.  Check both the specified process'
   1558  * process group and that of its children.
   1559  * entering == 0 => p is leaving specified group.
   1560  * entering == 1 => p is entering specified group.
   1561  *
   1562  * Call with proc_lock held.
   1563  */
   1564 void
   1565 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
   1566 {
   1567 	struct pgrp *hispgrp;
   1568 	struct session *mysession = pgrp->pg_session;
   1569 	struct proc *child;
   1570 
   1571 	KASSERT(mutex_owned(&proc_lock));
   1572 
   1573 	/*
   1574 	 * Check p's parent to see whether p qualifies its own process
   1575 	 * group; if so, adjust count for p's process group.
   1576 	 */
   1577 	hispgrp = p->p_pptr->p_pgrp;
   1578 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
   1579 		if (entering) {
   1580 			pgrp->pg_jobc++;
   1581 			p->p_lflag &= ~PL_ORPHANPG;
   1582 		} else {
   1583 			/* KASSERT(pgrp->pg_jobc > 0); */
   1584 			if (--pgrp->pg_jobc == 0)
   1585 				orphanpg(pgrp);
   1586 		}
   1587 	}
   1588 
   1589 	/*
   1590 	 * Check this process' children to see whether they qualify
   1591 	 * their process groups; if so, adjust counts for children's
   1592 	 * process groups.
   1593 	 */
   1594 	LIST_FOREACH(child, &p->p_children, p_sibling) {
   1595 		hispgrp = child->p_pgrp;
   1596 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
   1597 		    !P_ZOMBIE(child)) {
   1598 			if (entering) {
   1599 				child->p_lflag &= ~PL_ORPHANPG;
   1600 				hispgrp->pg_jobc++;
   1601 			} else {
   1602 				KASSERT(hispgrp->pg_jobc > 0);
   1603 				if (--hispgrp->pg_jobc == 0)
   1604 					orphanpg(hispgrp);
   1605 			}
   1606 		}
   1607 	}
   1608 }
   1609 
   1610 /*
   1611  * A process group has become orphaned;
   1612  * if there are any stopped processes in the group,
   1613  * hang-up all process in that group.
   1614  *
   1615  * Call with proc_lock held.
   1616  */
   1617 static void
   1618 orphanpg(struct pgrp *pg)
   1619 {
   1620 	struct proc *p;
   1621 
   1622 	KASSERT(mutex_owned(&proc_lock));
   1623 
   1624 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1625 		if (p->p_stat == SSTOP) {
   1626 			p->p_lflag |= PL_ORPHANPG;
   1627 			psignal(p, SIGHUP);
   1628 			psignal(p, SIGCONT);
   1629 		}
   1630 	}
   1631 }
   1632 
   1633 #ifdef DDB
   1634 #include <ddb/db_output.h>
   1635 void pidtbl_dump(void);
   1636 void
   1637 pidtbl_dump(void)
   1638 {
   1639 	struct pid_table *pt;
   1640 	struct proc *p;
   1641 	struct pgrp *pgrp;
   1642 	uintptr_t slot;
   1643 	int id;
   1644 
   1645 	db_printf("pid table %p size %x, next %x, last %x\n",
   1646 		pid_table, pid_tbl_mask+1,
   1647 		next_free_pt, last_free_pt);
   1648 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1649 		slot = pt->pt_slot;
   1650 		if (!PT_VALID(slot) && !pt->pt_pgrp)
   1651 			continue;
   1652 		if (PT_IS_LWP(slot)) {
   1653 			p = PT_GET_LWP(slot)->l_proc;
   1654 		} else if (PT_IS_PROC(slot)) {
   1655 			p = PT_GET_PROC(slot);
   1656 		} else {
   1657 			p = NULL;
   1658 		}
   1659 		db_printf("  id %x: ", id);
   1660 		if (p != NULL)
   1661 			db_printf("slotpid %d proc %p id %d (0x%x) %s\n",
   1662 				pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm);
   1663 		else
   1664 			db_printf("next %x use %x\n",
   1665 				PT_NEXT(slot) & pid_tbl_mask,
   1666 				PT_NEXT(slot) & ~pid_tbl_mask);
   1667 		if ((pgrp = pt->pt_pgrp)) {
   1668 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1669 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1670 			    pgrp->pg_session->s_count,
   1671 			    pgrp->pg_session->s_login);
   1672 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1673 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1674 			    LIST_FIRST(&pgrp->pg_members));
   1675 			LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
   1676 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1677 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1678 			}
   1679 		}
   1680 	}
   1681 }
   1682 #endif /* DDB */
   1683 
   1684 #ifdef KSTACK_CHECK_MAGIC
   1685 
   1686 #define	KSTACK_MAGIC	0xdeadbeaf
   1687 
   1688 /* XXX should be per process basis? */
   1689 static int	kstackleftmin = KSTACK_SIZE;
   1690 static int	kstackleftthres = KSTACK_SIZE / 8;
   1691 
   1692 void
   1693 kstack_setup_magic(const struct lwp *l)
   1694 {
   1695 	uint32_t *ip;
   1696 	uint32_t const *end;
   1697 
   1698 	KASSERT(l != NULL);
   1699 	KASSERT(l != &lwp0);
   1700 
   1701 	/*
   1702 	 * fill all the stack with magic number
   1703 	 * so that later modification on it can be detected.
   1704 	 */
   1705 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1706 	end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1707 	for (; ip < end; ip++) {
   1708 		*ip = KSTACK_MAGIC;
   1709 	}
   1710 }
   1711 
   1712 void
   1713 kstack_check_magic(const struct lwp *l)
   1714 {
   1715 	uint32_t const *ip, *end;
   1716 	int stackleft;
   1717 
   1718 	KASSERT(l != NULL);
   1719 
   1720 	/* don't check proc0 */ /*XXX*/
   1721 	if (l == &lwp0)
   1722 		return;
   1723 
   1724 #ifdef __MACHINE_STACK_GROWS_UP
   1725 	/* stack grows upwards (eg. hppa) */
   1726 	ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1727 	end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1728 	for (ip--; ip >= end; ip--)
   1729 		if (*ip != KSTACK_MAGIC)
   1730 			break;
   1731 
   1732 	stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
   1733 #else /* __MACHINE_STACK_GROWS_UP */
   1734 	/* stack grows downwards (eg. i386) */
   1735 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1736 	end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1737 	for (; ip < end; ip++)
   1738 		if (*ip != KSTACK_MAGIC)
   1739 			break;
   1740 
   1741 	stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
   1742 #endif /* __MACHINE_STACK_GROWS_UP */
   1743 
   1744 	if (kstackleftmin > stackleft) {
   1745 		kstackleftmin = stackleft;
   1746 		if (stackleft < kstackleftthres)
   1747 			printf("warning: kernel stack left %d bytes"
   1748 			    "(pid %u:lid %u)\n", stackleft,
   1749 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1750 	}
   1751 
   1752 	if (stackleft <= 0) {
   1753 		panic("magic on the top of kernel stack changed for "
   1754 		    "pid %u, lid %u: maybe kernel stack overflow",
   1755 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1756 	}
   1757 }
   1758 #endif /* KSTACK_CHECK_MAGIC */
   1759 
   1760 int
   1761 proclist_foreach_call(struct proclist *list,
   1762     int (*callback)(struct proc *, void *arg), void *arg)
   1763 {
   1764 	struct proc marker;
   1765 	struct proc *p;
   1766 	int ret = 0;
   1767 
   1768 	marker.p_flag = PK_MARKER;
   1769 	mutex_enter(&proc_lock);
   1770 	for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
   1771 		if (p->p_flag & PK_MARKER) {
   1772 			p = LIST_NEXT(p, p_list);
   1773 			continue;
   1774 		}
   1775 		LIST_INSERT_AFTER(p, &marker, p_list);
   1776 		ret = (*callback)(p, arg);
   1777 		KASSERT(mutex_owned(&proc_lock));
   1778 		p = LIST_NEXT(&marker, p_list);
   1779 		LIST_REMOVE(&marker, p_list);
   1780 	}
   1781 	mutex_exit(&proc_lock);
   1782 
   1783 	return ret;
   1784 }
   1785 
   1786 int
   1787 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
   1788 {
   1789 
   1790 	/* XXXCDC: how should locking work here? */
   1791 
   1792 	/* curproc exception is for coredump. */
   1793 
   1794 	if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
   1795 	    (p->p_vmspace->vm_refcnt < 1)) {
   1796 		return SET_ERROR(EFAULT);
   1797 	}
   1798 
   1799 	uvmspace_addref(p->p_vmspace);
   1800 	*vm = p->p_vmspace;
   1801 
   1802 	return 0;
   1803 }
   1804 
   1805 /*
   1806  * Acquire a write lock on the process credential.
   1807  */
   1808 void
   1809 proc_crmod_enter(void)
   1810 {
   1811 	struct lwp *l = curlwp;
   1812 	struct proc *p = l->l_proc;
   1813 	kauth_cred_t oc;
   1814 
   1815 	/* Reset what needs to be reset in plimit. */
   1816 	if (p->p_limit->pl_corename != defcorename) {
   1817 		lim_setcorename(p, defcorename, 0);
   1818 	}
   1819 
   1820 	mutex_enter(p->p_lock);
   1821 
   1822 	/* Ensure the LWP cached credentials are up to date. */
   1823 	if ((oc = l->l_cred) != p->p_cred) {
   1824 		l->l_cred = kauth_cred_hold(p->p_cred);
   1825 		kauth_cred_free(oc);
   1826 	}
   1827 }
   1828 
   1829 /*
   1830  * Set in a new process credential, and drop the write lock.  The credential
   1831  * must have a reference already.  Optionally, free a no-longer required
   1832  * credential.
   1833  */
   1834 void
   1835 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
   1836 {
   1837 	struct lwp *l = curlwp, *l2;
   1838 	struct proc *p = l->l_proc;
   1839 	kauth_cred_t oc;
   1840 
   1841 	KASSERT(mutex_owned(p->p_lock));
   1842 
   1843 	/* Is there a new credential to set in? */
   1844 	if (scred != NULL) {
   1845 		p->p_cred = scred;
   1846 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1847 			if (l2 != l) {
   1848 				lwp_lock(l2);
   1849 				l2->l_flag |= LW_CACHECRED;
   1850 				lwp_need_userret(l2);
   1851 				lwp_unlock(l2);
   1852 			}
   1853 		}
   1854 
   1855 		/* Ensure the LWP cached credentials are up to date. */
   1856 		if ((oc = l->l_cred) != scred) {
   1857 			l->l_cred = kauth_cred_hold(scred);
   1858 		}
   1859 	} else
   1860 		oc = NULL;	/* XXXgcc */
   1861 
   1862 	if (sugid) {
   1863 		/*
   1864 		 * Mark process as having changed credentials, stops
   1865 		 * tracing etc.
   1866 		 */
   1867 		p->p_flag |= PK_SUGID;
   1868 	}
   1869 
   1870 	mutex_exit(p->p_lock);
   1871 
   1872 	/* If there is a credential to be released, free it now. */
   1873 	if (fcred != NULL) {
   1874 		KASSERT(scred != NULL);
   1875 		kauth_cred_free(fcred);
   1876 		if (oc != scred)
   1877 			kauth_cred_free(oc);
   1878 	}
   1879 }
   1880 
   1881 /*
   1882  * proc_specific_key_create --
   1883  *	Create a key for subsystem proc-specific data.
   1884  */
   1885 int
   1886 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1887 {
   1888 
   1889 	return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
   1890 }
   1891 
   1892 /*
   1893  * proc_specific_key_delete --
   1894  *	Delete a key for subsystem proc-specific data.
   1895  */
   1896 void
   1897 proc_specific_key_delete(specificdata_key_t key)
   1898 {
   1899 
   1900 	specificdata_key_delete(proc_specificdata_domain, key);
   1901 }
   1902 
   1903 /*
   1904  * proc_initspecific --
   1905  *	Initialize a proc's specificdata container.
   1906  */
   1907 void
   1908 proc_initspecific(struct proc *p)
   1909 {
   1910 	int error __diagused;
   1911 
   1912 	error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
   1913 	KASSERT(error == 0);
   1914 }
   1915 
   1916 /*
   1917  * proc_finispecific --
   1918  *	Finalize a proc's specificdata container.
   1919  */
   1920 void
   1921 proc_finispecific(struct proc *p)
   1922 {
   1923 
   1924 	specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
   1925 }
   1926 
   1927 /*
   1928  * proc_getspecific --
   1929  *	Return proc-specific data corresponding to the specified key.
   1930  */
   1931 void *
   1932 proc_getspecific(struct proc *p, specificdata_key_t key)
   1933 {
   1934 
   1935 	return (specificdata_getspecific(proc_specificdata_domain,
   1936 					 &p->p_specdataref, key));
   1937 }
   1938 
   1939 /*
   1940  * proc_setspecific --
   1941  *	Set proc-specific data corresponding to the specified key.
   1942  */
   1943 void
   1944 proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
   1945 {
   1946 
   1947 	specificdata_setspecific(proc_specificdata_domain,
   1948 				 &p->p_specdataref, key, data);
   1949 }
   1950 
   1951 int
   1952 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
   1953 {
   1954 
   1955 	if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) ||
   1956 	    kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) {
   1957 		/*
   1958 		 * suid proc of ours or proc not ours
   1959 		 */
   1960 		return SET_ERROR(EPERM);
   1961 	} else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) {
   1962 		/*
   1963 		 * sgid proc has sgid back to us temporarily
   1964 		 */
   1965 		return SET_ERROR(EPERM);
   1966 	} else {
   1967 		/*
   1968 		 * our rgid must be in target's group list (ie,
   1969 		 * sub-processes started by a sgid process)
   1970 		 */
   1971 		int ismember = 0;
   1972 
   1973 		if (kauth_cred_ismember_gid(cred,
   1974 		    kauth_cred_getgid(target), &ismember) != 0 ||
   1975 		    !ismember)
   1976 			return SET_ERROR(EPERM);
   1977 	}
   1978 
   1979 	return 0;
   1980 }
   1981 
   1982 /*
   1983  * sysctl stuff
   1984  */
   1985 
   1986 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1987 
   1988 static const u_int sysctl_flagmap[] = {
   1989 	PK_ADVLOCK, P_ADVLOCK,
   1990 	PK_EXEC, P_EXEC,
   1991 	PK_NOCLDWAIT, P_NOCLDWAIT,
   1992 	PK_32, P_32,
   1993 	PK_CLDSIGIGN, P_CLDSIGIGN,
   1994 	PK_SUGID, P_SUGID,
   1995 	0
   1996 };
   1997 
   1998 static const u_int sysctl_sflagmap[] = {
   1999 	PS_NOCLDSTOP, P_NOCLDSTOP,
   2000 	PS_WEXIT, P_WEXIT,
   2001 	PS_STOPFORK, P_STOPFORK,
   2002 	PS_STOPEXEC, P_STOPEXEC,
   2003 	PS_STOPEXIT, P_STOPEXIT,
   2004 	0
   2005 };
   2006 
   2007 static const u_int sysctl_slflagmap[] = {
   2008 	PSL_TRACED, P_TRACED,
   2009 	PSL_CHTRACED, P_CHTRACED,
   2010 	PSL_SYSCALL, P_SYSCALL,
   2011 	0
   2012 };
   2013 
   2014 static const u_int sysctl_lflagmap[] = {
   2015 	PL_CONTROLT, P_CONTROLT,
   2016 	PL_PPWAIT, P_PPWAIT,
   2017 	0
   2018 };
   2019 
   2020 static const u_int sysctl_stflagmap[] = {
   2021 	PST_PROFIL, P_PROFIL,
   2022 	0
   2023 
   2024 };
   2025 
   2026 /* used by kern_lwp also */
   2027 const u_int sysctl_lwpflagmap[] = {
   2028 	LW_SINTR, L_SINTR,
   2029 	LW_SYSTEM, L_SYSTEM,
   2030 	0
   2031 };
   2032 
   2033 /*
   2034  * Find the most ``active'' lwp of a process and return it for ps display
   2035  * purposes
   2036  */
   2037 static struct lwp *
   2038 proc_active_lwp(struct proc *p)
   2039 {
   2040 	static const int ostat[] = {
   2041 		0,
   2042 		2,	/* LSIDL */
   2043 		6,	/* LSRUN */
   2044 		5,	/* LSSLEEP */
   2045 		4,	/* LSSTOP */
   2046 		0,	/* LSZOMB */
   2047 		1,	/* LSDEAD */
   2048 		7,	/* LSONPROC */
   2049 		3	/* LSSUSPENDED */
   2050 	};
   2051 
   2052 	struct lwp *l, *lp = NULL;
   2053 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2054 		KASSERT(l->l_stat >= 0);
   2055 		KASSERT(l->l_stat < __arraycount(ostat));
   2056 		if (lp == NULL ||
   2057 		    ostat[l->l_stat] > ostat[lp->l_stat] ||
   2058 		    (ostat[l->l_stat] == ostat[lp->l_stat] &&
   2059 		    l->l_cpticks > lp->l_cpticks)) {
   2060 			lp = l;
   2061 			continue;
   2062 		}
   2063 	}
   2064 	return lp;
   2065 }
   2066 
   2067 static int
   2068 sysctl_doeproc(SYSCTLFN_ARGS)
   2069 {
   2070 	union {
   2071 		struct kinfo_proc kproc;
   2072 		struct kinfo_proc2 kproc2;
   2073 	} *kbuf;
   2074 	struct proc *p, *next, *marker;
   2075 	char *where, *dp;
   2076 	int type, op, arg, error;
   2077 	u_int elem_size, kelem_size, elem_count;
   2078 	size_t buflen, needed;
   2079 	bool match, zombie, mmmbrains;
   2080 	const bool allowaddr = get_expose_address(curproc);
   2081 
   2082 	if (namelen == 1 && name[0] == CTL_QUERY)
   2083 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2084 
   2085 	dp = where = oldp;
   2086 	buflen = where != NULL ? *oldlenp : 0;
   2087 	error = 0;
   2088 	needed = 0;
   2089 	type = rnode->sysctl_num;
   2090 
   2091 	if (type == KERN_PROC) {
   2092 		if (namelen == 0)
   2093 			return SET_ERROR(EINVAL);
   2094 		switch (op = name[0]) {
   2095 		case KERN_PROC_ALL:
   2096 			if (namelen != 1)
   2097 				return SET_ERROR(EINVAL);
   2098 			arg = 0;
   2099 			break;
   2100 		default:
   2101 			if (namelen != 2)
   2102 				return SET_ERROR(EINVAL);
   2103 			arg = name[1];
   2104 			break;
   2105 		}
   2106 		elem_count = 0;	/* Hush little compiler, don't you cry */
   2107 		kelem_size = elem_size = sizeof(kbuf->kproc);
   2108 	} else {
   2109 		if (namelen != 4)
   2110 			return SET_ERROR(EINVAL);
   2111 		op = name[0];
   2112 		arg = name[1];
   2113 		elem_size = name[2];
   2114 		elem_count = name[3];
   2115 		kelem_size = sizeof(kbuf->kproc2);
   2116 	}
   2117 
   2118 	sysctl_unlock();
   2119 
   2120 	kbuf = kmem_zalloc(sizeof(*kbuf), KM_SLEEP);
   2121 	marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
   2122 	marker->p_flag = PK_MARKER;
   2123 
   2124 	mutex_enter(&proc_lock);
   2125 	/*
   2126 	 * Start with zombies to prevent reporting processes twice, in case they
   2127 	 * are dying and being moved from the list of alive processes to zombies.
   2128 	 */
   2129 	mmmbrains = true;
   2130 	for (p = LIST_FIRST(&zombproc);; p = next) {
   2131 		if (p == NULL) {
   2132 			if (mmmbrains) {
   2133 				p = LIST_FIRST(&allproc);
   2134 				mmmbrains = false;
   2135 			}
   2136 			if (p == NULL)
   2137 				break;
   2138 		}
   2139 		next = LIST_NEXT(p, p_list);
   2140 		if ((p->p_flag & PK_MARKER) != 0)
   2141 			continue;
   2142 
   2143 		/*
   2144 		 * Skip embryonic processes.
   2145 		 */
   2146 		if (p->p_stat == SIDL)
   2147 			continue;
   2148 
   2149 		mutex_enter(p->p_lock);
   2150 		error = kauth_authorize_process(l->l_cred,
   2151 		    KAUTH_PROCESS_CANSEE, p,
   2152 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_EPROC), NULL, NULL);
   2153 		if (error != 0) {
   2154 			mutex_exit(p->p_lock);
   2155 			continue;
   2156 		}
   2157 
   2158 		/*
   2159 		 * Hande all the operations in one switch on the cost of
   2160 		 * algorithm complexity is on purpose. The win splitting this
   2161 		 * function into several similar copies makes maintenance
   2162 		 * burden, code grow and boost is negligible in practical
   2163 		 * systems.
   2164 		 */
   2165 		switch (op) {
   2166 		case KERN_PROC_PID:
   2167 			match = (p->p_pid == (pid_t)arg);
   2168 			break;
   2169 
   2170 		case KERN_PROC_PGRP:
   2171 			match = (p->p_pgrp->pg_id == (pid_t)arg);
   2172 			break;
   2173 
   2174 		case KERN_PROC_SESSION:
   2175 			match = (p->p_session->s_sid == (pid_t)arg);
   2176 			break;
   2177 
   2178 		case KERN_PROC_TTY:
   2179 			match = true;
   2180 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2181 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2182 				    p->p_session->s_ttyp == NULL ||
   2183 				    p->p_session->s_ttyvp != NULL) {
   2184 				    	match = false;
   2185 				}
   2186 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2187 			    p->p_session->s_ttyp == NULL) {
   2188 				if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
   2189 					match = false;
   2190 				}
   2191 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
   2192 				match = false;
   2193 			}
   2194 			break;
   2195 
   2196 		case KERN_PROC_UID:
   2197 			match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
   2198 			break;
   2199 
   2200 		case KERN_PROC_RUID:
   2201 			match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
   2202 			break;
   2203 
   2204 		case KERN_PROC_GID:
   2205 			match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
   2206 			break;
   2207 
   2208 		case KERN_PROC_RGID:
   2209 			match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
   2210 			break;
   2211 
   2212 		case KERN_PROC_ALL:
   2213 			match = true;
   2214 			/* allow everything */
   2215 			break;
   2216 
   2217 		default:
   2218 			error = SET_ERROR(EINVAL);
   2219 			mutex_exit(p->p_lock);
   2220 			goto cleanup;
   2221 		}
   2222 		if (!match) {
   2223 			mutex_exit(p->p_lock);
   2224 			continue;
   2225 		}
   2226 
   2227 		/*
   2228 		 * Grab a hold on the process.
   2229 		 */
   2230 		if (mmmbrains) {
   2231 			zombie = true;
   2232 		} else {
   2233 			zombie = !rw_tryenter(&p->p_reflock, RW_READER);
   2234 		}
   2235 		if (zombie) {
   2236 			LIST_INSERT_AFTER(p, marker, p_list);
   2237 		}
   2238 
   2239 		if (buflen >= elem_size &&
   2240 		    (type == KERN_PROC || elem_count > 0)) {
   2241 			ruspace(p);	/* Update process vm resource use */
   2242 
   2243 			if (type == KERN_PROC) {
   2244 				fill_proc(p, &kbuf->kproc.kp_proc, allowaddr);
   2245 				fill_eproc(p, &kbuf->kproc.kp_eproc, zombie,
   2246 				    allowaddr);
   2247 			} else {
   2248 				fill_kproc2(p, &kbuf->kproc2, zombie,
   2249 				    allowaddr);
   2250 				elem_count--;
   2251 			}
   2252 			mutex_exit(p->p_lock);
   2253 			mutex_exit(&proc_lock);
   2254 			/*
   2255 			 * Copy out elem_size, but not larger than kelem_size
   2256 			 */
   2257 			error = sysctl_copyout(l, kbuf, dp,
   2258 			    uimin(kelem_size, elem_size));
   2259 			mutex_enter(&proc_lock);
   2260 			if (error) {
   2261 				goto bah;
   2262 			}
   2263 			dp += elem_size;
   2264 			buflen -= elem_size;
   2265 		} else {
   2266 			mutex_exit(p->p_lock);
   2267 		}
   2268 		needed += elem_size;
   2269 
   2270 		/*
   2271 		 * Release reference to process.
   2272 		 */
   2273 	 	if (zombie) {
   2274 			next = LIST_NEXT(marker, p_list);
   2275  			LIST_REMOVE(marker, p_list);
   2276 		} else {
   2277 			rw_exit(&p->p_reflock);
   2278 			next = LIST_NEXT(p, p_list);
   2279 		}
   2280 
   2281 		/*
   2282 		 * Short-circuit break quickly!
   2283 		 */
   2284 		if (op == KERN_PROC_PID)
   2285                 	break;
   2286 	}
   2287 	mutex_exit(&proc_lock);
   2288 
   2289 	if (where != NULL) {
   2290 		*oldlenp = dp - where;
   2291 		if (needed > *oldlenp) {
   2292 			error = SET_ERROR(ENOMEM);
   2293 			goto out;
   2294 		}
   2295 	} else {
   2296 		needed += KERN_PROCSLOP;
   2297 		*oldlenp = needed;
   2298 	}
   2299 	kmem_free(kbuf, sizeof(*kbuf));
   2300 	kmem_free(marker, sizeof(*marker));
   2301 	sysctl_relock();
   2302 	return 0;
   2303  bah:
   2304  	if (zombie)
   2305  		LIST_REMOVE(marker, p_list);
   2306 	else
   2307 		rw_exit(&p->p_reflock);
   2308  cleanup:
   2309 	mutex_exit(&proc_lock);
   2310  out:
   2311 	kmem_free(kbuf, sizeof(*kbuf));
   2312 	kmem_free(marker, sizeof(*marker));
   2313 	sysctl_relock();
   2314 	return error;
   2315 }
   2316 
   2317 int
   2318 copyin_psstrings(struct proc *p, struct ps_strings *arginfo)
   2319 {
   2320 #if !defined(_RUMPKERNEL)
   2321 	int retval;
   2322 
   2323 	if (p->p_flag & PK_32) {
   2324 		MODULE_HOOK_CALL(kern_proc32_copyin_hook, (p, arginfo),
   2325 		    enosys(), retval);
   2326 		return retval;
   2327 	}
   2328 #endif /* !defined(_RUMPKERNEL) */
   2329 
   2330 	return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo));
   2331 }
   2332 
   2333 static int
   2334 copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len)
   2335 {
   2336 	void **cookie = cookie_;
   2337 	struct lwp *l = cookie[0];
   2338 	char *dst = cookie[1];
   2339 
   2340 	return sysctl_copyout(l, src, dst + off, len);
   2341 }
   2342 
   2343 /*
   2344  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2345  */
   2346 static int
   2347 sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2348 {
   2349 	struct ps_strings pss;
   2350 	struct proc *p;
   2351 	pid_t pid;
   2352 	int type, error;
   2353 	void *cookie[2];
   2354 
   2355 	if (namelen == 1 && name[0] == CTL_QUERY)
   2356 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2357 
   2358 	if (newp != NULL || namelen != 2)
   2359 		return SET_ERROR(EINVAL);
   2360 	pid = name[0];
   2361 	type = name[1];
   2362 
   2363 	switch (type) {
   2364 	case KERN_PROC_PATHNAME:
   2365 		sysctl_unlock();
   2366 		error = fill_pathname(l, pid, oldp, oldlenp);
   2367 		sysctl_relock();
   2368 		return error;
   2369 
   2370 	case KERN_PROC_CWD:
   2371 		sysctl_unlock();
   2372 		error = fill_cwd(l, pid, oldp, oldlenp);
   2373 		sysctl_relock();
   2374 		return error;
   2375 
   2376 	case KERN_PROC_ARGV:
   2377 	case KERN_PROC_NARGV:
   2378 	case KERN_PROC_ENV:
   2379 	case KERN_PROC_NENV:
   2380 		/* ok */
   2381 		break;
   2382 	default:
   2383 		return SET_ERROR(EINVAL);
   2384 	}
   2385 
   2386 	sysctl_unlock();
   2387 
   2388 	/* check pid */
   2389 	mutex_enter(&proc_lock);
   2390 	if ((p = proc_find(pid)) == NULL) {
   2391 		error = SET_ERROR(EINVAL);
   2392 		goto out_locked;
   2393 	}
   2394 	mutex_enter(p->p_lock);
   2395 
   2396 	/* Check permission. */
   2397 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2398 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2399 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
   2400 	else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
   2401 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2402 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
   2403 	else
   2404 		error = SET_ERROR(EINVAL); /* XXXGCC */
   2405 	if (error) {
   2406 		mutex_exit(p->p_lock);
   2407 		goto out_locked;
   2408 	}
   2409 
   2410 	if (oldp == NULL) {
   2411 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2412 			*oldlenp = sizeof (int);
   2413 		else
   2414 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2415 		error = 0;
   2416 		mutex_exit(p->p_lock);
   2417 		goto out_locked;
   2418 	}
   2419 
   2420 	/*
   2421 	 * Zombies don't have a stack, so we can't read their psstrings.
   2422 	 * System processes also don't have a user stack.
   2423 	 */
   2424 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
   2425 		error = SET_ERROR(EINVAL);
   2426 		mutex_exit(p->p_lock);
   2427 		goto out_locked;
   2428 	}
   2429 
   2430 	error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : SET_ERROR(EBUSY);
   2431 	mutex_exit(p->p_lock);
   2432 	if (error) {
   2433 		goto out_locked;
   2434 	}
   2435 	mutex_exit(&proc_lock);
   2436 
   2437 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2438 		int value;
   2439 		if ((error = copyin_psstrings(p, &pss)) == 0) {
   2440 			if (type == KERN_PROC_NARGV)
   2441 				value = pss.ps_nargvstr;
   2442 			else
   2443 				value = pss.ps_nenvstr;
   2444 			error = sysctl_copyout(l, &value, oldp, sizeof(value));
   2445 			*oldlenp = sizeof(value);
   2446 		}
   2447 	} else {
   2448 		cookie[0] = l;
   2449 		cookie[1] = oldp;
   2450 		error = copy_procargs(p, type, oldlenp,
   2451 		    copy_procargs_sysctl_cb, cookie);
   2452 	}
   2453 	rw_exit(&p->p_reflock);
   2454 	sysctl_relock();
   2455 	return error;
   2456 
   2457 out_locked:
   2458 	mutex_exit(&proc_lock);
   2459 	sysctl_relock();
   2460 	return error;
   2461 }
   2462 
   2463 int
   2464 copy_procargs(struct proc *p, int oid, size_t *limit,
   2465     int (*cb)(void *, const void *, size_t, size_t), void *cookie)
   2466 {
   2467 	struct ps_strings pss;
   2468 	size_t len, i, loaded, entry_len;
   2469 	struct uio auio;
   2470 	struct iovec aiov;
   2471 	int error, argvlen;
   2472 	char *arg;
   2473 	char **argv;
   2474 	vaddr_t user_argv;
   2475 	struct vmspace *vmspace;
   2476 
   2477 	/*
   2478 	 * Allocate a temporary buffer to hold the argument vector and
   2479 	 * the arguments themselve.
   2480 	 */
   2481 	arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   2482 	argv = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   2483 
   2484 	/*
   2485 	 * Lock the process down in memory.
   2486 	 */
   2487 	vmspace = p->p_vmspace;
   2488 	uvmspace_addref(vmspace);
   2489 
   2490 	/*
   2491 	 * Read in the ps_strings structure.
   2492 	 */
   2493 	if ((error = copyin_psstrings(p, &pss)) != 0)
   2494 		goto done;
   2495 
   2496 	/*
   2497 	 * Now read the address of the argument vector.
   2498 	 */
   2499 	switch (oid) {
   2500 	case KERN_PROC_ARGV:
   2501 		user_argv = (uintptr_t)pss.ps_argvstr;
   2502 		argvlen = pss.ps_nargvstr;
   2503 		break;
   2504 	case KERN_PROC_ENV:
   2505 		user_argv = (uintptr_t)pss.ps_envstr;
   2506 		argvlen = pss.ps_nenvstr;
   2507 		break;
   2508 	default:
   2509 		error = SET_ERROR(EINVAL);
   2510 		goto done;
   2511 	}
   2512 
   2513 	if (argvlen < 0) {
   2514 		error = SET_ERROR(EIO);
   2515 		goto done;
   2516 	}
   2517 
   2518 
   2519 	/*
   2520 	 * Now copy each string.
   2521 	 */
   2522 	len = 0; /* bytes written to user buffer */
   2523 	loaded = 0; /* bytes from argv already processed */
   2524 	i = 0; /* To make compiler happy */
   2525 	entry_len = PROC_PTRSZ(p);
   2526 
   2527 	for (; argvlen; --argvlen) {
   2528 		int finished = 0;
   2529 		vaddr_t base;
   2530 		size_t xlen;
   2531 		int j;
   2532 
   2533 		if (loaded == 0) {
   2534 			size_t rem = entry_len * argvlen;
   2535 			loaded = MIN(rem, PAGE_SIZE);
   2536 			error = copyin_vmspace(vmspace,
   2537 			    (const void *)user_argv, argv, loaded);
   2538 			if (error)
   2539 				break;
   2540 			user_argv += loaded;
   2541 			i = 0;
   2542 		}
   2543 
   2544 #if !defined(_RUMPKERNEL)
   2545 		if (p->p_flag & PK_32)
   2546 			MODULE_HOOK_CALL(kern_proc32_base_hook,
   2547 			    (argv, i++), 0, base);
   2548 		else
   2549 #endif /* !defined(_RUMPKERNEL) */
   2550 			base = (vaddr_t)argv[i++];
   2551 		loaded -= entry_len;
   2552 
   2553 		/*
   2554 		 * The program has messed around with its arguments,
   2555 		 * possibly deleting some, and replacing them with
   2556 		 * NULL's. Treat this as the last argument and not
   2557 		 * a failure.
   2558 		 */
   2559 		if (base == 0)
   2560 			break;
   2561 
   2562 		while (!finished) {
   2563 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2564 
   2565 			aiov.iov_base = arg;
   2566 			aiov.iov_len = PAGE_SIZE;
   2567 			auio.uio_iov = &aiov;
   2568 			auio.uio_iovcnt = 1;
   2569 			auio.uio_offset = base;
   2570 			auio.uio_resid = xlen;
   2571 			auio.uio_rw = UIO_READ;
   2572 			UIO_SETUP_SYSSPACE(&auio);
   2573 			error = uvm_io(&vmspace->vm_map, &auio, 0);
   2574 			if (error)
   2575 				goto done;
   2576 
   2577 			/* Look for the end of the string */
   2578 			for (j = 0; j < xlen; j++) {
   2579 				if (arg[j] == '\0') {
   2580 					xlen = j + 1;
   2581 					finished = 1;
   2582 					break;
   2583 				}
   2584 			}
   2585 
   2586 			/* Check for user buffer overflow */
   2587 			if (len + xlen > *limit) {
   2588 				finished = 1;
   2589 				if (len > *limit)
   2590 					xlen = 0;
   2591 				else
   2592 					xlen = *limit - len;
   2593 			}
   2594 
   2595 			/* Copyout the page */
   2596 			error = (*cb)(cookie, arg, len, xlen);
   2597 			if (error)
   2598 				goto done;
   2599 
   2600 			len += xlen;
   2601 			base += xlen;
   2602 		}
   2603 	}
   2604 	*limit = len;
   2605 
   2606 done:
   2607 	kmem_free(argv, PAGE_SIZE);
   2608 	kmem_free(arg, PAGE_SIZE);
   2609 	uvmspace_free(vmspace);
   2610 	return error;
   2611 }
   2612 
   2613 /*
   2614  * Fill in a proc structure for the specified process.
   2615  */
   2616 static void
   2617 fill_proc(const struct proc *psrc, struct proc *p, bool allowaddr)
   2618 {
   2619 	COND_SET_STRUCT(p->p_list, psrc->p_list, allowaddr);
   2620 	memset(&p->p_auxlock, 0, sizeof(p->p_auxlock));
   2621 	COND_SET_STRUCT(p->p_lock, psrc->p_lock, allowaddr);
   2622 	memset(&p->p_stmutex, 0, sizeof(p->p_stmutex));
   2623 	memset(&p->p_reflock, 0, sizeof(p->p_reflock));
   2624 	COND_SET_STRUCT(p->p_waitcv, psrc->p_waitcv, allowaddr);
   2625 	COND_SET_STRUCT(p->p_lwpcv, psrc->p_lwpcv, allowaddr);
   2626 	COND_SET_PTR(p->p_cred, psrc->p_cred, allowaddr);
   2627 	COND_SET_PTR(p->p_fd, psrc->p_fd, allowaddr);
   2628 	COND_SET_PTR(p->p_cwdi, psrc->p_cwdi, allowaddr);
   2629 	COND_SET_PTR(p->p_stats, psrc->p_stats, allowaddr);
   2630 	COND_SET_PTR(p->p_limit, psrc->p_limit, allowaddr);
   2631 	COND_SET_PTR(p->p_vmspace, psrc->p_vmspace, allowaddr);
   2632 	COND_SET_PTR(p->p_sigacts, psrc->p_sigacts, allowaddr);
   2633 	COND_SET_PTR(p->p_aio, psrc->p_aio, allowaddr);
   2634 	p->p_mqueue_cnt = psrc->p_mqueue_cnt;
   2635 	memset(&p->p_specdataref, 0, sizeof(p->p_specdataref));
   2636 	p->p_exitsig = psrc->p_exitsig;
   2637 	p->p_flag = psrc->p_flag;
   2638 	p->p_sflag = psrc->p_sflag;
   2639 	p->p_slflag = psrc->p_slflag;
   2640 	p->p_lflag = psrc->p_lflag;
   2641 	p->p_stflag = psrc->p_stflag;
   2642 	p->p_stat = psrc->p_stat;
   2643 	p->p_trace_enabled = psrc->p_trace_enabled;
   2644 	p->p_pid = psrc->p_pid;
   2645 	COND_SET_STRUCT(p->p_pglist, psrc->p_pglist, allowaddr);
   2646 	COND_SET_PTR(p->p_pptr, psrc->p_pptr, allowaddr);
   2647 	COND_SET_STRUCT(p->p_sibling, psrc->p_sibling, allowaddr);
   2648 	COND_SET_STRUCT(p->p_children, psrc->p_children, allowaddr);
   2649 	COND_SET_STRUCT(p->p_lwps, psrc->p_lwps, allowaddr);
   2650 	COND_SET_PTR(p->p_raslist, psrc->p_raslist, allowaddr);
   2651 	p->p_nlwps = psrc->p_nlwps;
   2652 	p->p_nzlwps = psrc->p_nzlwps;
   2653 	p->p_nrlwps = psrc->p_nrlwps;
   2654 	p->p_nlwpwait = psrc->p_nlwpwait;
   2655 	p->p_ndlwps = psrc->p_ndlwps;
   2656 	p->p_nstopchild = psrc->p_nstopchild;
   2657 	p->p_waited = psrc->p_waited;
   2658 	COND_SET_PTR(p->p_zomblwp, psrc->p_zomblwp, allowaddr);
   2659 	COND_SET_PTR(p->p_vforklwp, psrc->p_vforklwp, allowaddr);
   2660 	COND_SET_PTR(p->p_sched_info, psrc->p_sched_info, allowaddr);
   2661 	p->p_estcpu = psrc->p_estcpu;
   2662 	p->p_estcpu_inherited = psrc->p_estcpu_inherited;
   2663 	p->p_forktime = psrc->p_forktime;
   2664 	p->p_pctcpu = psrc->p_pctcpu;
   2665 	COND_SET_PTR(p->p_opptr, psrc->p_opptr, allowaddr);
   2666 	COND_SET_PTR(p->p_timers, psrc->p_timers, allowaddr);
   2667 	p->p_rtime = psrc->p_rtime;
   2668 	p->p_uticks = psrc->p_uticks;
   2669 	p->p_sticks = psrc->p_sticks;
   2670 	p->p_iticks = psrc->p_iticks;
   2671 	p->p_xutime = psrc->p_xutime;
   2672 	p->p_xstime = psrc->p_xstime;
   2673 	p->p_traceflag = psrc->p_traceflag;
   2674 	COND_SET_PTR(p->p_tracep, psrc->p_tracep, allowaddr);
   2675 	COND_SET_PTR(p->p_textvp, psrc->p_textvp, allowaddr);
   2676 	COND_SET_PTR(p->p_emul, psrc->p_emul, allowaddr);
   2677 	COND_SET_PTR(p->p_emuldata, psrc->p_emuldata, allowaddr);
   2678 	COND_SET_CPTR(p->p_execsw, psrc->p_execsw, allowaddr);
   2679 	COND_SET_STRUCT(p->p_klist, psrc->p_klist, allowaddr);
   2680 	COND_SET_STRUCT(p->p_sigwaiters, psrc->p_sigwaiters, allowaddr);
   2681 	COND_SET_STRUCT(p->p_sigpend.sp_info, psrc->p_sigpend.sp_info,
   2682 	    allowaddr);
   2683 	p->p_sigpend.sp_set = psrc->p_sigpend.sp_set;
   2684 	COND_SET_PTR(p->p_lwpctl, psrc->p_lwpctl, allowaddr);
   2685 	p->p_ppid = psrc->p_ppid;
   2686 	p->p_oppid = psrc->p_oppid;
   2687 	COND_SET_PTR(p->p_path, psrc->p_path, allowaddr);
   2688 	p->p_sigctx = psrc->p_sigctx;
   2689 	p->p_nice = psrc->p_nice;
   2690 	memcpy(p->p_comm, psrc->p_comm, sizeof(p->p_comm));
   2691 	COND_SET_PTR(p->p_pgrp, psrc->p_pgrp, allowaddr);
   2692 	COND_SET_VALUE(p->p_psstrp, psrc->p_psstrp, allowaddr);
   2693 	p->p_pax = psrc->p_pax;
   2694 	p->p_xexit = psrc->p_xexit;
   2695 	p->p_xsig = psrc->p_xsig;
   2696 	p->p_acflag = psrc->p_acflag;
   2697 	COND_SET_STRUCT(p->p_md, psrc->p_md, allowaddr);
   2698 	p->p_stackbase = psrc->p_stackbase;
   2699 	COND_SET_PTR(p->p_dtrace, psrc->p_dtrace, allowaddr);
   2700 }
   2701 
   2702 /*
   2703  * Fill in an eproc structure for the specified process.
   2704  */
   2705 void
   2706 fill_eproc(struct proc *p, struct eproc *ep, bool zombie, bool allowaddr)
   2707 {
   2708 	struct tty *tp;
   2709 	struct lwp *l;
   2710 
   2711 	KASSERT(mutex_owned(&proc_lock));
   2712 	KASSERT(mutex_owned(p->p_lock));
   2713 
   2714 	COND_SET_PTR(ep->e_paddr, p, allowaddr);
   2715 	COND_SET_PTR(ep->e_sess, p->p_session, allowaddr);
   2716 	if (p->p_cred) {
   2717 		kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   2718 		kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   2719 	}
   2720 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2721 		struct vmspace *vm = p->p_vmspace;
   2722 
   2723 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2724 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2725 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2726 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2727 		ep->e_vm.vm_map.size = vm->vm_map.size;
   2728 
   2729 		/* Pick the primary (first) LWP */
   2730 		l = proc_active_lwp(p);
   2731 		KASSERT(l != NULL);
   2732 		lwp_lock(l);
   2733 		if (l->l_wchan)
   2734 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2735 		lwp_unlock(l);
   2736 	}
   2737 	ep->e_ppid = p->p_ppid;
   2738 	if (p->p_pgrp && p->p_session) {
   2739 		ep->e_pgid = p->p_pgrp->pg_id;
   2740 		ep->e_jobc = p->p_pgrp->pg_jobc;
   2741 		ep->e_sid = p->p_session->s_sid;
   2742 		if ((p->p_lflag & PL_CONTROLT) &&
   2743 		    (tp = p->p_session->s_ttyp)) {
   2744 			ep->e_tdev = tp->t_dev;
   2745 			ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2746 			COND_SET_PTR(ep->e_tsess, tp->t_session, allowaddr);
   2747 		} else
   2748 			ep->e_tdev = (uint32_t)NODEV;
   2749 		ep->e_flag = p->p_session->s_ttyvp ? EPROC_CTTY : 0;
   2750 		if (SESS_LEADER(p))
   2751 			ep->e_flag |= EPROC_SLEADER;
   2752 		strncpy(ep->e_login, p->p_session->s_login, MAXLOGNAME);
   2753 	}
   2754 	ep->e_xsize = ep->e_xrssize = 0;
   2755 	ep->e_xccount = ep->e_xswrss = 0;
   2756 }
   2757 
   2758 /*
   2759  * Fill in a kinfo_proc2 structure for the specified process.
   2760  */
   2761 void
   2762 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie, bool allowaddr)
   2763 {
   2764 	struct tty *tp;
   2765 	struct lwp *l;
   2766 	struct timeval ut, st, rt;
   2767 	sigset_t ss1, ss2;
   2768 	struct rusage ru;
   2769 	struct vmspace *vm;
   2770 
   2771 	KASSERT(mutex_owned(&proc_lock));
   2772 	KASSERT(mutex_owned(p->p_lock));
   2773 
   2774 	sigemptyset(&ss1);
   2775 	sigemptyset(&ss2);
   2776 
   2777 	COND_SET_VALUE(ki->p_paddr, PTRTOUINT64(p), allowaddr);
   2778 	COND_SET_VALUE(ki->p_fd, PTRTOUINT64(p->p_fd), allowaddr);
   2779 	COND_SET_VALUE(ki->p_cwdi, PTRTOUINT64(p->p_cwdi), allowaddr);
   2780 	COND_SET_VALUE(ki->p_stats, PTRTOUINT64(p->p_stats), allowaddr);
   2781 	COND_SET_VALUE(ki->p_limit, PTRTOUINT64(p->p_limit), allowaddr);
   2782 	COND_SET_VALUE(ki->p_vmspace, PTRTOUINT64(p->p_vmspace), allowaddr);
   2783 	COND_SET_VALUE(ki->p_sigacts, PTRTOUINT64(p->p_sigacts), allowaddr);
   2784 	COND_SET_VALUE(ki->p_sess, PTRTOUINT64(p->p_session), allowaddr);
   2785 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2786 	COND_SET_VALUE(ki->p_ru, PTRTOUINT64(&p->p_stats->p_ru), allowaddr);
   2787 	ki->p_eflag = 0;
   2788 	ki->p_exitsig = p->p_exitsig;
   2789 	ki->p_flag = L_INMEM;   /* Process never swapped out */
   2790 	ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
   2791 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
   2792 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
   2793 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
   2794 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
   2795 	ki->p_pid = p->p_pid;
   2796 	ki->p_ppid = p->p_ppid;
   2797 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2798 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2799 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2800 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2801 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2802 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2803 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2804 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2805 	    uimin(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
   2806 	    UIO_SYSSPACE);
   2807 
   2808 	ki->p_uticks = p->p_uticks;
   2809 	ki->p_sticks = p->p_sticks;
   2810 	ki->p_iticks = p->p_iticks;
   2811 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2812 	COND_SET_VALUE(ki->p_tracep, PTRTOUINT64(p->p_tracep), allowaddr);
   2813 	ki->p_traceflag = p->p_traceflag;
   2814 
   2815 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2816 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2817 
   2818 	ki->p_cpticks = 0;
   2819 	ki->p_pctcpu = p->p_pctcpu;
   2820 	ki->p_estcpu = 0;
   2821 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2822 	ki->p_realstat = p->p_stat;
   2823 	ki->p_nice = p->p_nice;
   2824 	ki->p_xstat = P_WAITSTATUS(p);
   2825 	ki->p_acflag = p->p_acflag;
   2826 
   2827 	strncpy(ki->p_comm, p->p_comm,
   2828 	    uimin(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2829 	strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
   2830 
   2831 	ki->p_nlwps = p->p_nlwps;
   2832 	ki->p_realflag = ki->p_flag;
   2833 
   2834 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2835 		vm = p->p_vmspace;
   2836 		ki->p_vm_rssize = vm_resident_count(vm);
   2837 		ki->p_vm_tsize = vm->vm_tsize;
   2838 		ki->p_vm_dsize = vm->vm_dsize;
   2839 		ki->p_vm_ssize = vm->vm_ssize;
   2840 		ki->p_vm_vsize = atop(vm->vm_map.size);
   2841 		/*
   2842 		 * Since the stack is initially mapped mostly with
   2843 		 * PROT_NONE and grown as needed, adjust the "mapped size"
   2844 		 * to skip the unused stack portion.
   2845 		 */
   2846 		ki->p_vm_msize =
   2847 		    atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
   2848 
   2849 		/* Pick the primary (first) LWP */
   2850 		l = proc_active_lwp(p);
   2851 		KASSERT(l != NULL);
   2852 		lwp_lock(l);
   2853 		ki->p_nrlwps = p->p_nrlwps;
   2854 		ki->p_forw = 0;
   2855 		ki->p_back = 0;
   2856 		COND_SET_VALUE(ki->p_addr, PTRTOUINT64(l->l_addr), allowaddr);
   2857 		ki->p_stat = l->l_stat;
   2858 		ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   2859 		ki->p_swtime = l->l_swtime;
   2860 		ki->p_slptime = l->l_slptime;
   2861 		if (l->l_stat == LSONPROC)
   2862 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2863 		else
   2864 			ki->p_schedflags = 0;
   2865 		ki->p_priority = lwp_eprio(l);
   2866 		ki->p_usrpri = l->l_priority;
   2867 		if (l->l_wchan)
   2868 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2869 		COND_SET_VALUE(ki->p_wchan, PTRTOUINT64(l->l_wchan), allowaddr);
   2870 		ki->p_cpuid = cpu_index(l->l_cpu);
   2871 		lwp_unlock(l);
   2872 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2873 			/* This is hardly correct, but... */
   2874 			sigplusset(&l->l_sigpend.sp_set, &ss1);
   2875 			sigplusset(&l->l_sigmask, &ss2);
   2876 			ki->p_cpticks += l->l_cpticks;
   2877 			ki->p_pctcpu += l->l_pctcpu;
   2878 			ki->p_estcpu += l->l_estcpu;
   2879 		}
   2880 	}
   2881 	sigplusset(&p->p_sigpend.sp_set, &ss1);
   2882 	memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
   2883 	memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
   2884 
   2885 	if (p->p_session != NULL) {
   2886 		ki->p_sid = p->p_session->s_sid;
   2887 		ki->p__pgid = p->p_pgrp->pg_id;
   2888 		if (p->p_session->s_ttyvp)
   2889 			ki->p_eflag |= EPROC_CTTY;
   2890 		if (SESS_LEADER(p))
   2891 			ki->p_eflag |= EPROC_SLEADER;
   2892 		strncpy(ki->p_login, p->p_session->s_login,
   2893 		    uimin(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2894 		ki->p_jobc = p->p_pgrp->pg_jobc;
   2895 		if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2896 			ki->p_tdev = tp->t_dev;
   2897 			ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2898 			COND_SET_VALUE(ki->p_tsess, PTRTOUINT64(tp->t_session),
   2899 			    allowaddr);
   2900 		} else {
   2901 			ki->p_tdev = (int32_t)NODEV;
   2902 		}
   2903 	}
   2904 
   2905 	if (!P_ZOMBIE(p) && !zombie) {
   2906 		ki->p_uvalid = 1;
   2907 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2908 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2909 
   2910 		calcru(p, &ut, &st, NULL, &rt);
   2911 		ki->p_rtime_sec = rt.tv_sec;
   2912 		ki->p_rtime_usec = rt.tv_usec;
   2913 		ki->p_uutime_sec = ut.tv_sec;
   2914 		ki->p_uutime_usec = ut.tv_usec;
   2915 		ki->p_ustime_sec = st.tv_sec;
   2916 		ki->p_ustime_usec = st.tv_usec;
   2917 
   2918 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
   2919 		rulwps(p, &ru);
   2920 		ki->p_uru_nvcsw = ru.ru_nvcsw;
   2921 		ki->p_uru_nivcsw = ru.ru_nivcsw;
   2922 		ki->p_uru_maxrss = ru.ru_maxrss;
   2923 		ki->p_uru_ixrss = ru.ru_ixrss;
   2924 		ki->p_uru_idrss = ru.ru_idrss;
   2925 		ki->p_uru_isrss = ru.ru_isrss;
   2926 		ki->p_uru_minflt = ru.ru_minflt;
   2927 		ki->p_uru_majflt = ru.ru_majflt;
   2928 		ki->p_uru_nswap = ru.ru_nswap;
   2929 		ki->p_uru_inblock = ru.ru_inblock;
   2930 		ki->p_uru_oublock = ru.ru_oublock;
   2931 		ki->p_uru_msgsnd = ru.ru_msgsnd;
   2932 		ki->p_uru_msgrcv = ru.ru_msgrcv;
   2933 		ki->p_uru_nsignals = ru.ru_nsignals;
   2934 
   2935 		timeradd(&p->p_stats->p_cru.ru_utime,
   2936 			 &p->p_stats->p_cru.ru_stime, &ut);
   2937 		ki->p_uctime_sec = ut.tv_sec;
   2938 		ki->p_uctime_usec = ut.tv_usec;
   2939 	}
   2940 }
   2941 
   2942 
   2943 int
   2944 proc_find_locked(struct lwp *l, struct proc **p, pid_t pid)
   2945 {
   2946 	int error;
   2947 
   2948 	mutex_enter(&proc_lock);
   2949 	if (pid == -1)
   2950 		*p = l->l_proc;
   2951 	else
   2952 		*p = proc_find(pid);
   2953 
   2954 	if (*p == NULL) {
   2955 		if (pid != -1)
   2956 			mutex_exit(&proc_lock);
   2957 		return SET_ERROR(ESRCH);
   2958 	}
   2959 	if (pid != -1)
   2960 		mutex_enter((*p)->p_lock);
   2961 	mutex_exit(&proc_lock);
   2962 
   2963 	error = kauth_authorize_process(l->l_cred,
   2964 	    KAUTH_PROCESS_CANSEE, *p,
   2965 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
   2966 	if (error) {
   2967 		if (pid != -1)
   2968 			mutex_exit((*p)->p_lock);
   2969 	}
   2970 	return error;
   2971 }
   2972 
   2973 static int
   2974 fill_pathname(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
   2975 {
   2976 	int error;
   2977 	struct proc *p;
   2978 
   2979 	if ((error = proc_find_locked(l, &p, pid)) != 0)
   2980 		return error;
   2981 
   2982 	if (p->p_path == NULL) {
   2983 		if (pid != -1)
   2984 			mutex_exit(p->p_lock);
   2985 		return SET_ERROR(ENOENT);
   2986 	}
   2987 
   2988 	size_t len = strlen(p->p_path) + 1;
   2989 	if (oldp != NULL) {
   2990 		size_t copylen = uimin(len, *oldlenp);
   2991 		error = sysctl_copyout(l, p->p_path, oldp, copylen);
   2992 		if (error == 0 && *oldlenp < len)
   2993 			error = SET_ERROR(ENOSPC);
   2994 	}
   2995 	*oldlenp = len;
   2996 	if (pid != -1)
   2997 		mutex_exit(p->p_lock);
   2998 	return error;
   2999 }
   3000 
   3001 static int
   3002 fill_cwd(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
   3003 {
   3004 	int error;
   3005 	struct proc *p;
   3006 	char *path;
   3007 	char *bp, *bend;
   3008 	struct cwdinfo *cwdi;
   3009 	struct vnode *vp;
   3010 	size_t len, lenused;
   3011 
   3012 	if ((error = proc_find_locked(l, &p, pid)) != 0)
   3013 		return error;
   3014 
   3015 	len = MAXPATHLEN * 4;
   3016 
   3017 	path = kmem_alloc(len, KM_SLEEP);
   3018 
   3019 	bp = &path[len];
   3020 	bend = bp;
   3021 	*(--bp) = '\0';
   3022 
   3023 	cwdi = p->p_cwdi;
   3024 	rw_enter(&cwdi->cwdi_lock, RW_READER);
   3025 	vp = cwdi->cwdi_cdir;
   3026 	error = getcwd_common(vp, NULL, &bp, path, len/2, 0, l);
   3027 	rw_exit(&cwdi->cwdi_lock);
   3028 
   3029 	if (error)
   3030 		goto out;
   3031 
   3032 	lenused = bend - bp;
   3033 
   3034 	if (oldp != NULL) {
   3035 		size_t copylen = uimin(lenused, *oldlenp);
   3036 		error = sysctl_copyout(l, bp, oldp, copylen);
   3037 		if (error == 0 && *oldlenp < lenused)
   3038 			error = SET_ERROR(ENOSPC);
   3039 	}
   3040 	*oldlenp = lenused;
   3041 out:
   3042 	if (pid != -1)
   3043 		mutex_exit(p->p_lock);
   3044 	kmem_free(path, len);
   3045 	return error;
   3046 }
   3047 
   3048 int
   3049 proc_getauxv(struct proc *p, void **buf, size_t *len)
   3050 {
   3051 	struct ps_strings pss;
   3052 	int error;
   3053 	void *uauxv, *kauxv;
   3054 	size_t size;
   3055 
   3056 	if ((error = copyin_psstrings(p, &pss)) != 0)
   3057 		return error;
   3058 	if (pss.ps_envstr == NULL)
   3059 		return SET_ERROR(EIO);
   3060 
   3061 	size = p->p_execsw->es_arglen;
   3062 	if (size == 0)
   3063 		return SET_ERROR(EIO);
   3064 
   3065 	size_t ptrsz = PROC_PTRSZ(p);
   3066 	uauxv = (void *)((char *)pss.ps_envstr + (pss.ps_nenvstr + 1) * ptrsz);
   3067 
   3068 	kauxv = kmem_alloc(size, KM_SLEEP);
   3069 
   3070 	error = copyin_proc(p, uauxv, kauxv, size);
   3071 	if (error) {
   3072 		kmem_free(kauxv, size);
   3073 		return error;
   3074 	}
   3075 
   3076 	*buf = kauxv;
   3077 	*len = size;
   3078 
   3079 	return 0;
   3080 }
   3081 
   3082 
   3083 static int
   3084 sysctl_security_expose_address(SYSCTLFN_ARGS)
   3085 {
   3086 	int expose_address, error;
   3087 	struct sysctlnode node;
   3088 
   3089 	node = *rnode;
   3090 	node.sysctl_data = &expose_address;
   3091 	expose_address = *(int *)rnode->sysctl_data;
   3092 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   3093 	if (error || newp == NULL)
   3094 		return error;
   3095 
   3096 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_KERNADDR,
   3097 	    0, NULL, NULL, NULL))
   3098 		return SET_ERROR(EPERM);
   3099 
   3100 	switch (expose_address) {
   3101 	case 0:
   3102 	case 1:
   3103 	case 2:
   3104 		break;
   3105 	default:
   3106 		return SET_ERROR(EINVAL);
   3107 	}
   3108 
   3109 	*(int *)rnode->sysctl_data = expose_address;
   3110 
   3111 	return 0;
   3112 }
   3113 
   3114 bool
   3115 get_expose_address(struct proc *p)
   3116 {
   3117 	/* allow only if sysctl variable is set or privileged */
   3118 	return kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANSEE,
   3119 	    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_KPTR), NULL, NULL) == 0;
   3120 }
   3121