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