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