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