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