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