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