xref: /freebsd/sys/kern/kern_proc.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 #include "opt_ddb.h"
37 #include "opt_ktrace.h"
38 #include "opt_kstack_pages.h"
39 #include "opt_stack.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/elf.h>
44 #include <sys/exec.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/lock.h>
48 #include <sys/loginclass.h>
49 #include <sys/malloc.h>
50 #include <sys/mman.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/ptrace.h>
55 #include <sys/refcount.h>
56 #include <sys/resourcevar.h>
57 #include <sys/rwlock.h>
58 #include <sys/sbuf.h>
59 #include <sys/sysent.h>
60 #include <sys/sched.h>
61 #include <sys/smp.h>
62 #include <sys/stack.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/filedesc.h>
66 #include <sys/tty.h>
67 #include <sys/signalvar.h>
68 #include <sys/sdt.h>
69 #include <sys/sx.h>
70 #include <sys/user.h>
71 #include <sys/jail.h>
72 #include <sys/vnode.h>
73 #include <sys/eventhandler.h>
74 
75 #ifdef DDB
76 #include <ddb/ddb.h>
77 #endif
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_extern.h>
82 #include <vm/pmap.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/uma.h>
87 
88 #ifdef COMPAT_FREEBSD32
89 #include <compat/freebsd32/freebsd32.h>
90 #include <compat/freebsd32/freebsd32_util.h>
91 #endif
92 
93 SDT_PROVIDER_DEFINE(proc);
94 SDT_PROBE_DEFINE4(proc, kernel, ctor, entry, "struct proc *", "int",
95     "void *", "int");
96 SDT_PROBE_DEFINE4(proc, kernel, ctor, return, "struct proc *", "int",
97     "void *", "int");
98 SDT_PROBE_DEFINE4(proc, kernel, dtor, entry, "struct proc *", "int",
99     "void *", "struct thread *");
100 SDT_PROBE_DEFINE3(proc, kernel, dtor, return, "struct proc *", "int",
101     "void *");
102 SDT_PROBE_DEFINE3(proc, kernel, init, entry, "struct proc *", "int",
103     "int");
104 SDT_PROBE_DEFINE3(proc, kernel, init, return, "struct proc *", "int",
105     "int");
106 
107 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
108 MALLOC_DEFINE(M_SESSION, "session", "session header");
109 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
110 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
111 
112 static void doenterpgrp(struct proc *, struct pgrp *);
113 static void orphanpg(struct pgrp *pg);
114 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
115 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
116 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
117     int preferthread);
118 static void pgadjustjobc(struct pgrp *pgrp, int entering);
119 static void pgdelete(struct pgrp *);
120 static int proc_ctor(void *mem, int size, void *arg, int flags);
121 static void proc_dtor(void *mem, int size, void *arg);
122 static int proc_init(void *mem, int size, int flags);
123 static void proc_fini(void *mem, int size);
124 static void pargs_free(struct pargs *pa);
125 static struct proc *zpfind_locked(pid_t pid);
126 
127 /*
128  * Other process lists
129  */
130 struct pidhashhead *pidhashtbl;
131 u_long pidhash;
132 struct pgrphashhead *pgrphashtbl;
133 u_long pgrphash;
134 struct proclist allproc;
135 struct proclist zombproc;
136 struct sx allproc_lock;
137 struct sx proctree_lock;
138 struct mtx ppeers_lock;
139 uma_zone_t proc_zone;
140 
141 int kstack_pages = KSTACK_PAGES;
142 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
143     "Kernel stack size in pages");
144 static int vmmap_skip_res_cnt = 0;
145 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
146     &vmmap_skip_res_cnt, 0,
147     "Skip calculation of the pages resident count in kern.proc.vmmap");
148 
149 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
150 #ifdef COMPAT_FREEBSD32
151 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
152 #endif
153 
154 /*
155  * Initialize global process hashing structures.
156  */
157 void
158 procinit()
159 {
160 
161 	sx_init(&allproc_lock, "allproc");
162 	sx_init(&proctree_lock, "proctree");
163 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
164 	LIST_INIT(&allproc);
165 	LIST_INIT(&zombproc);
166 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
167 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
168 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
169 	    proc_ctor, proc_dtor, proc_init, proc_fini,
170 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
171 	uihashinit();
172 }
173 
174 /*
175  * Prepare a proc for use.
176  */
177 static int
178 proc_ctor(void *mem, int size, void *arg, int flags)
179 {
180 	struct proc *p;
181 
182 	p = (struct proc *)mem;
183 	SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
184 	EVENTHANDLER_INVOKE(process_ctor, p);
185 	SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
186 	return (0);
187 }
188 
189 /*
190  * Reclaim a proc after use.
191  */
192 static void
193 proc_dtor(void *mem, int size, void *arg)
194 {
195 	struct proc *p;
196 	struct thread *td;
197 
198 	/* INVARIANTS checks go here */
199 	p = (struct proc *)mem;
200 	td = FIRST_THREAD_IN_PROC(p);
201 	SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
202 	if (td != NULL) {
203 #ifdef INVARIANTS
204 		KASSERT((p->p_numthreads == 1),
205 		    ("bad number of threads in exiting process"));
206 		KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
207 #endif
208 		/* Free all OSD associated to this thread. */
209 		osd_thread_exit(td);
210 	}
211 	EVENTHANDLER_INVOKE(process_dtor, p);
212 	if (p->p_ksi != NULL)
213 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
214 	SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
215 }
216 
217 /*
218  * Initialize type-stable parts of a proc (when newly created).
219  */
220 static int
221 proc_init(void *mem, int size, int flags)
222 {
223 	struct proc *p;
224 
225 	p = (struct proc *)mem;
226 	SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
227 	p->p_sched = (struct p_sched *)&p[1];
228 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
229 	mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
230 	mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
231 	mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
232 	mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
233 	cv_init(&p->p_pwait, "ppwait");
234 	cv_init(&p->p_dbgwait, "dbgwait");
235 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
236 	EVENTHANDLER_INVOKE(process_init, p);
237 	p->p_stats = pstats_alloc();
238 	SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
239 	return (0);
240 }
241 
242 /*
243  * UMA should ensure that this function is never called.
244  * Freeing a proc structure would violate type stability.
245  */
246 static void
247 proc_fini(void *mem, int size)
248 {
249 #ifdef notnow
250 	struct proc *p;
251 
252 	p = (struct proc *)mem;
253 	EVENTHANDLER_INVOKE(process_fini, p);
254 	pstats_free(p->p_stats);
255 	thread_free(FIRST_THREAD_IN_PROC(p));
256 	mtx_destroy(&p->p_mtx);
257 	if (p->p_ksi != NULL)
258 		ksiginfo_free(p->p_ksi);
259 #else
260 	panic("proc reclaimed");
261 #endif
262 }
263 
264 /*
265  * Is p an inferior of the current process?
266  */
267 int
268 inferior(struct proc *p)
269 {
270 
271 	sx_assert(&proctree_lock, SX_LOCKED);
272 	PROC_LOCK_ASSERT(p, MA_OWNED);
273 	for (; p != curproc; p = proc_realparent(p)) {
274 		if (p->p_pid == 0)
275 			return (0);
276 	}
277 	return (1);
278 }
279 
280 struct proc *
281 pfind_locked(pid_t pid)
282 {
283 	struct proc *p;
284 
285 	sx_assert(&allproc_lock, SX_LOCKED);
286 	LIST_FOREACH(p, PIDHASH(pid), p_hash) {
287 		if (p->p_pid == pid) {
288 			PROC_LOCK(p);
289 			if (p->p_state == PRS_NEW) {
290 				PROC_UNLOCK(p);
291 				p = NULL;
292 			}
293 			break;
294 		}
295 	}
296 	return (p);
297 }
298 
299 /*
300  * Locate a process by number; return only "live" processes -- i.e., neither
301  * zombies nor newly born but incompletely initialized processes.  By not
302  * returning processes in the PRS_NEW state, we allow callers to avoid
303  * testing for that condition to avoid dereferencing p_ucred, et al.
304  */
305 struct proc *
306 pfind(pid_t pid)
307 {
308 	struct proc *p;
309 
310 	sx_slock(&allproc_lock);
311 	p = pfind_locked(pid);
312 	sx_sunlock(&allproc_lock);
313 	return (p);
314 }
315 
316 static struct proc *
317 pfind_tid_locked(pid_t tid)
318 {
319 	struct proc *p;
320 	struct thread *td;
321 
322 	sx_assert(&allproc_lock, SX_LOCKED);
323 	FOREACH_PROC_IN_SYSTEM(p) {
324 		PROC_LOCK(p);
325 		if (p->p_state == PRS_NEW) {
326 			PROC_UNLOCK(p);
327 			continue;
328 		}
329 		FOREACH_THREAD_IN_PROC(p, td) {
330 			if (td->td_tid == tid)
331 				goto found;
332 		}
333 		PROC_UNLOCK(p);
334 	}
335 found:
336 	return (p);
337 }
338 
339 /*
340  * Locate a process group by number.
341  * The caller must hold proctree_lock.
342  */
343 struct pgrp *
344 pgfind(pgid)
345 	register pid_t pgid;
346 {
347 	register struct pgrp *pgrp;
348 
349 	sx_assert(&proctree_lock, SX_LOCKED);
350 
351 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
352 		if (pgrp->pg_id == pgid) {
353 			PGRP_LOCK(pgrp);
354 			return (pgrp);
355 		}
356 	}
357 	return (NULL);
358 }
359 
360 /*
361  * Locate process and do additional manipulations, depending on flags.
362  */
363 int
364 pget(pid_t pid, int flags, struct proc **pp)
365 {
366 	struct proc *p;
367 	int error;
368 
369 	sx_slock(&allproc_lock);
370 	if (pid <= PID_MAX) {
371 		p = pfind_locked(pid);
372 		if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
373 			p = zpfind_locked(pid);
374 	} else if ((flags & PGET_NOTID) == 0) {
375 		p = pfind_tid_locked(pid);
376 	} else {
377 		p = NULL;
378 	}
379 	sx_sunlock(&allproc_lock);
380 	if (p == NULL)
381 		return (ESRCH);
382 	if ((flags & PGET_CANSEE) != 0) {
383 		error = p_cansee(curthread, p);
384 		if (error != 0)
385 			goto errout;
386 	}
387 	if ((flags & PGET_CANDEBUG) != 0) {
388 		error = p_candebug(curthread, p);
389 		if (error != 0)
390 			goto errout;
391 	}
392 	if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
393 		error = EPERM;
394 		goto errout;
395 	}
396 	if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
397 		error = ESRCH;
398 		goto errout;
399 	}
400 	if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
401 		/*
402 		 * XXXRW: Not clear ESRCH is the right error during proc
403 		 * execve().
404 		 */
405 		error = ESRCH;
406 		goto errout;
407 	}
408 	if ((flags & PGET_HOLD) != 0) {
409 		_PHOLD(p);
410 		PROC_UNLOCK(p);
411 	}
412 	*pp = p;
413 	return (0);
414 errout:
415 	PROC_UNLOCK(p);
416 	return (error);
417 }
418 
419 /*
420  * Create a new process group.
421  * pgid must be equal to the pid of p.
422  * Begin a new session if required.
423  */
424 int
425 enterpgrp(p, pgid, pgrp, sess)
426 	register struct proc *p;
427 	pid_t pgid;
428 	struct pgrp *pgrp;
429 	struct session *sess;
430 {
431 
432 	sx_assert(&proctree_lock, SX_XLOCKED);
433 
434 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
435 	KASSERT(p->p_pid == pgid,
436 	    ("enterpgrp: new pgrp and pid != pgid"));
437 	KASSERT(pgfind(pgid) == NULL,
438 	    ("enterpgrp: pgrp with pgid exists"));
439 	KASSERT(!SESS_LEADER(p),
440 	    ("enterpgrp: session leader attempted setpgrp"));
441 
442 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
443 
444 	if (sess != NULL) {
445 		/*
446 		 * new session
447 		 */
448 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
449 		PROC_LOCK(p);
450 		p->p_flag &= ~P_CONTROLT;
451 		PROC_UNLOCK(p);
452 		PGRP_LOCK(pgrp);
453 		sess->s_leader = p;
454 		sess->s_sid = p->p_pid;
455 		refcount_init(&sess->s_count, 1);
456 		sess->s_ttyvp = NULL;
457 		sess->s_ttydp = NULL;
458 		sess->s_ttyp = NULL;
459 		bcopy(p->p_session->s_login, sess->s_login,
460 			    sizeof(sess->s_login));
461 		pgrp->pg_session = sess;
462 		KASSERT(p == curproc,
463 		    ("enterpgrp: mksession and p != curproc"));
464 	} else {
465 		pgrp->pg_session = p->p_session;
466 		sess_hold(pgrp->pg_session);
467 		PGRP_LOCK(pgrp);
468 	}
469 	pgrp->pg_id = pgid;
470 	LIST_INIT(&pgrp->pg_members);
471 
472 	/*
473 	 * As we have an exclusive lock of proctree_lock,
474 	 * this should not deadlock.
475 	 */
476 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
477 	pgrp->pg_jobc = 0;
478 	SLIST_INIT(&pgrp->pg_sigiolst);
479 	PGRP_UNLOCK(pgrp);
480 
481 	doenterpgrp(p, pgrp);
482 
483 	return (0);
484 }
485 
486 /*
487  * Move p to an existing process group
488  */
489 int
490 enterthispgrp(p, pgrp)
491 	register struct proc *p;
492 	struct pgrp *pgrp;
493 {
494 
495 	sx_assert(&proctree_lock, SX_XLOCKED);
496 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
497 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
498 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
499 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
500 	KASSERT(pgrp->pg_session == p->p_session,
501 		("%s: pgrp's session %p, p->p_session %p.\n",
502 		__func__,
503 		pgrp->pg_session,
504 		p->p_session));
505 	KASSERT(pgrp != p->p_pgrp,
506 		("%s: p belongs to pgrp.", __func__));
507 
508 	doenterpgrp(p, pgrp);
509 
510 	return (0);
511 }
512 
513 /*
514  * Move p to a process group
515  */
516 static void
517 doenterpgrp(p, pgrp)
518 	struct proc *p;
519 	struct pgrp *pgrp;
520 {
521 	struct pgrp *savepgrp;
522 
523 	sx_assert(&proctree_lock, SX_XLOCKED);
524 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
525 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
526 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
527 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
528 
529 	savepgrp = p->p_pgrp;
530 
531 	/*
532 	 * Adjust eligibility of affected pgrps to participate in job control.
533 	 * Increment eligibility counts before decrementing, otherwise we
534 	 * could reach 0 spuriously during the first call.
535 	 */
536 	fixjobc(p, pgrp, 1);
537 	fixjobc(p, p->p_pgrp, 0);
538 
539 	PGRP_LOCK(pgrp);
540 	PGRP_LOCK(savepgrp);
541 	PROC_LOCK(p);
542 	LIST_REMOVE(p, p_pglist);
543 	p->p_pgrp = pgrp;
544 	PROC_UNLOCK(p);
545 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
546 	PGRP_UNLOCK(savepgrp);
547 	PGRP_UNLOCK(pgrp);
548 	if (LIST_EMPTY(&savepgrp->pg_members))
549 		pgdelete(savepgrp);
550 }
551 
552 /*
553  * remove process from process group
554  */
555 int
556 leavepgrp(p)
557 	register struct proc *p;
558 {
559 	struct pgrp *savepgrp;
560 
561 	sx_assert(&proctree_lock, SX_XLOCKED);
562 	savepgrp = p->p_pgrp;
563 	PGRP_LOCK(savepgrp);
564 	PROC_LOCK(p);
565 	LIST_REMOVE(p, p_pglist);
566 	p->p_pgrp = NULL;
567 	PROC_UNLOCK(p);
568 	PGRP_UNLOCK(savepgrp);
569 	if (LIST_EMPTY(&savepgrp->pg_members))
570 		pgdelete(savepgrp);
571 	return (0);
572 }
573 
574 /*
575  * delete a process group
576  */
577 static void
578 pgdelete(pgrp)
579 	register struct pgrp *pgrp;
580 {
581 	struct session *savesess;
582 	struct tty *tp;
583 
584 	sx_assert(&proctree_lock, SX_XLOCKED);
585 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
586 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
587 
588 	/*
589 	 * Reset any sigio structures pointing to us as a result of
590 	 * F_SETOWN with our pgid.
591 	 */
592 	funsetownlst(&pgrp->pg_sigiolst);
593 
594 	PGRP_LOCK(pgrp);
595 	tp = pgrp->pg_session->s_ttyp;
596 	LIST_REMOVE(pgrp, pg_hash);
597 	savesess = pgrp->pg_session;
598 	PGRP_UNLOCK(pgrp);
599 
600 	/* Remove the reference to the pgrp before deallocating it. */
601 	if (tp != NULL) {
602 		tty_lock(tp);
603 		tty_rel_pgrp(tp, pgrp);
604 	}
605 
606 	mtx_destroy(&pgrp->pg_mtx);
607 	free(pgrp, M_PGRP);
608 	sess_release(savesess);
609 }
610 
611 static void
612 pgadjustjobc(pgrp, entering)
613 	struct pgrp *pgrp;
614 	int entering;
615 {
616 
617 	PGRP_LOCK(pgrp);
618 	if (entering)
619 		pgrp->pg_jobc++;
620 	else {
621 		--pgrp->pg_jobc;
622 		if (pgrp->pg_jobc == 0)
623 			orphanpg(pgrp);
624 	}
625 	PGRP_UNLOCK(pgrp);
626 }
627 
628 /*
629  * Adjust pgrp jobc counters when specified process changes process group.
630  * We count the number of processes in each process group that "qualify"
631  * the group for terminal job control (those with a parent in a different
632  * process group of the same session).  If that count reaches zero, the
633  * process group becomes orphaned.  Check both the specified process'
634  * process group and that of its children.
635  * entering == 0 => p is leaving specified group.
636  * entering == 1 => p is entering specified group.
637  */
638 void
639 fixjobc(p, pgrp, entering)
640 	register struct proc *p;
641 	register struct pgrp *pgrp;
642 	int entering;
643 {
644 	register struct pgrp *hispgrp;
645 	register struct session *mysession;
646 
647 	sx_assert(&proctree_lock, SX_LOCKED);
648 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
649 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
650 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
651 
652 	/*
653 	 * Check p's parent to see whether p qualifies its own process
654 	 * group; if so, adjust count for p's process group.
655 	 */
656 	mysession = pgrp->pg_session;
657 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
658 	    hispgrp->pg_session == mysession)
659 		pgadjustjobc(pgrp, entering);
660 
661 	/*
662 	 * Check this process' children to see whether they qualify
663 	 * their process groups; if so, adjust counts for children's
664 	 * process groups.
665 	 */
666 	LIST_FOREACH(p, &p->p_children, p_sibling) {
667 		hispgrp = p->p_pgrp;
668 		if (hispgrp == pgrp ||
669 		    hispgrp->pg_session != mysession)
670 			continue;
671 		PROC_LOCK(p);
672 		if (p->p_state == PRS_ZOMBIE) {
673 			PROC_UNLOCK(p);
674 			continue;
675 		}
676 		PROC_UNLOCK(p);
677 		pgadjustjobc(hispgrp, entering);
678 	}
679 }
680 
681 /*
682  * A process group has become orphaned;
683  * if there are any stopped processes in the group,
684  * hang-up all process in that group.
685  */
686 static void
687 orphanpg(pg)
688 	struct pgrp *pg;
689 {
690 	register struct proc *p;
691 
692 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
693 
694 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
695 		PROC_LOCK(p);
696 		if (P_SHOULDSTOP(p)) {
697 			PROC_UNLOCK(p);
698 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
699 				PROC_LOCK(p);
700 				kern_psignal(p, SIGHUP);
701 				kern_psignal(p, SIGCONT);
702 				PROC_UNLOCK(p);
703 			}
704 			return;
705 		}
706 		PROC_UNLOCK(p);
707 	}
708 }
709 
710 void
711 sess_hold(struct session *s)
712 {
713 
714 	refcount_acquire(&s->s_count);
715 }
716 
717 void
718 sess_release(struct session *s)
719 {
720 
721 	if (refcount_release(&s->s_count)) {
722 		if (s->s_ttyp != NULL) {
723 			tty_lock(s->s_ttyp);
724 			tty_rel_sess(s->s_ttyp, s);
725 		}
726 		mtx_destroy(&s->s_mtx);
727 		free(s, M_SESSION);
728 	}
729 }
730 
731 #ifdef DDB
732 
733 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
734 {
735 	register struct pgrp *pgrp;
736 	register struct proc *p;
737 	register int i;
738 
739 	for (i = 0; i <= pgrphash; i++) {
740 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
741 			printf("\tindx %d\n", i);
742 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
743 				printf(
744 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
745 				    (void *)pgrp, (long)pgrp->pg_id,
746 				    (void *)pgrp->pg_session,
747 				    pgrp->pg_session->s_count,
748 				    (void *)LIST_FIRST(&pgrp->pg_members));
749 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
750 					printf("\t\tpid %ld addr %p pgrp %p\n",
751 					    (long)p->p_pid, (void *)p,
752 					    (void *)p->p_pgrp);
753 				}
754 			}
755 		}
756 	}
757 }
758 #endif /* DDB */
759 
760 /*
761  * Calculate the kinfo_proc members which contain process-wide
762  * informations.
763  * Must be called with the target process locked.
764  */
765 static void
766 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
767 {
768 	struct thread *td;
769 
770 	PROC_LOCK_ASSERT(p, MA_OWNED);
771 
772 	kp->ki_estcpu = 0;
773 	kp->ki_pctcpu = 0;
774 	FOREACH_THREAD_IN_PROC(p, td) {
775 		thread_lock(td);
776 		kp->ki_pctcpu += sched_pctcpu(td);
777 		kp->ki_estcpu += td->td_estcpu;
778 		thread_unlock(td);
779 	}
780 }
781 
782 /*
783  * Clear kinfo_proc and fill in any information that is common
784  * to all threads in the process.
785  * Must be called with the target process locked.
786  */
787 static void
788 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
789 {
790 	struct thread *td0;
791 	struct tty *tp;
792 	struct session *sp;
793 	struct ucred *cred;
794 	struct sigacts *ps;
795 
796 	/* For proc_realparent. */
797 	sx_assert(&proctree_lock, SX_LOCKED);
798 	PROC_LOCK_ASSERT(p, MA_OWNED);
799 	bzero(kp, sizeof(*kp));
800 
801 	kp->ki_structsize = sizeof(*kp);
802 	kp->ki_paddr = p;
803 	kp->ki_addr =/* p->p_addr; */0; /* XXX */
804 	kp->ki_args = p->p_args;
805 	kp->ki_textvp = p->p_textvp;
806 #ifdef KTRACE
807 	kp->ki_tracep = p->p_tracevp;
808 	kp->ki_traceflag = p->p_traceflag;
809 #endif
810 	kp->ki_fd = p->p_fd;
811 	kp->ki_vmspace = p->p_vmspace;
812 	kp->ki_flag = p->p_flag;
813 	kp->ki_flag2 = p->p_flag2;
814 	cred = p->p_ucred;
815 	if (cred) {
816 		kp->ki_uid = cred->cr_uid;
817 		kp->ki_ruid = cred->cr_ruid;
818 		kp->ki_svuid = cred->cr_svuid;
819 		kp->ki_cr_flags = 0;
820 		if (cred->cr_flags & CRED_FLAG_CAPMODE)
821 			kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
822 		/* XXX bde doesn't like KI_NGROUPS */
823 		if (cred->cr_ngroups > KI_NGROUPS) {
824 			kp->ki_ngroups = KI_NGROUPS;
825 			kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
826 		} else
827 			kp->ki_ngroups = cred->cr_ngroups;
828 		bcopy(cred->cr_groups, kp->ki_groups,
829 		    kp->ki_ngroups * sizeof(gid_t));
830 		kp->ki_rgid = cred->cr_rgid;
831 		kp->ki_svgid = cred->cr_svgid;
832 		/* If jailed(cred), emulate the old P_JAILED flag. */
833 		if (jailed(cred)) {
834 			kp->ki_flag |= P_JAILED;
835 			/* If inside the jail, use 0 as a jail ID. */
836 			if (cred->cr_prison != curthread->td_ucred->cr_prison)
837 				kp->ki_jid = cred->cr_prison->pr_id;
838 		}
839 		strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
840 		    sizeof(kp->ki_loginclass));
841 	}
842 	ps = p->p_sigacts;
843 	if (ps) {
844 		mtx_lock(&ps->ps_mtx);
845 		kp->ki_sigignore = ps->ps_sigignore;
846 		kp->ki_sigcatch = ps->ps_sigcatch;
847 		mtx_unlock(&ps->ps_mtx);
848 	}
849 	if (p->p_state != PRS_NEW &&
850 	    p->p_state != PRS_ZOMBIE &&
851 	    p->p_vmspace != NULL) {
852 		struct vmspace *vm = p->p_vmspace;
853 
854 		kp->ki_size = vm->vm_map.size;
855 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
856 		FOREACH_THREAD_IN_PROC(p, td0) {
857 			if (!TD_IS_SWAPPED(td0))
858 				kp->ki_rssize += td0->td_kstack_pages;
859 		}
860 		kp->ki_swrss = vm->vm_swrss;
861 		kp->ki_tsize = vm->vm_tsize;
862 		kp->ki_dsize = vm->vm_dsize;
863 		kp->ki_ssize = vm->vm_ssize;
864 	} else if (p->p_state == PRS_ZOMBIE)
865 		kp->ki_stat = SZOMB;
866 	if (kp->ki_flag & P_INMEM)
867 		kp->ki_sflag = PS_INMEM;
868 	else
869 		kp->ki_sflag = 0;
870 	/* Calculate legacy swtime as seconds since 'swtick'. */
871 	kp->ki_swtime = (ticks - p->p_swtick) / hz;
872 	kp->ki_pid = p->p_pid;
873 	kp->ki_nice = p->p_nice;
874 	kp->ki_fibnum = p->p_fibnum;
875 	kp->ki_start = p->p_stats->p_start;
876 	timevaladd(&kp->ki_start, &boottime);
877 	PROC_STATLOCK(p);
878 	rufetch(p, &kp->ki_rusage);
879 	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
880 	calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
881 	PROC_STATUNLOCK(p);
882 	calccru(p, &kp->ki_childutime, &kp->ki_childstime);
883 	/* Some callers want child times in a single value. */
884 	kp->ki_childtime = kp->ki_childstime;
885 	timevaladd(&kp->ki_childtime, &kp->ki_childutime);
886 
887 	FOREACH_THREAD_IN_PROC(p, td0)
888 		kp->ki_cow += td0->td_cow;
889 
890 	tp = NULL;
891 	if (p->p_pgrp) {
892 		kp->ki_pgid = p->p_pgrp->pg_id;
893 		kp->ki_jobc = p->p_pgrp->pg_jobc;
894 		sp = p->p_pgrp->pg_session;
895 
896 		if (sp != NULL) {
897 			kp->ki_sid = sp->s_sid;
898 			SESS_LOCK(sp);
899 			strlcpy(kp->ki_login, sp->s_login,
900 			    sizeof(kp->ki_login));
901 			if (sp->s_ttyvp)
902 				kp->ki_kiflag |= KI_CTTY;
903 			if (SESS_LEADER(p))
904 				kp->ki_kiflag |= KI_SLEADER;
905 			/* XXX proctree_lock */
906 			tp = sp->s_ttyp;
907 			SESS_UNLOCK(sp);
908 		}
909 	}
910 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
911 		kp->ki_tdev = tty_udev(tp);
912 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
913 		if (tp->t_session)
914 			kp->ki_tsid = tp->t_session->s_sid;
915 	} else
916 		kp->ki_tdev = NODEV;
917 	if (p->p_comm[0] != '\0')
918 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
919 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
920 	    p->p_sysent->sv_name[0] != '\0')
921 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
922 	kp->ki_siglist = p->p_siglist;
923 	kp->ki_xstat = p->p_xstat;
924 	kp->ki_acflag = p->p_acflag;
925 	kp->ki_lock = p->p_lock;
926 	if (p->p_pptr) {
927 		kp->ki_ppid = proc_realparent(p)->p_pid;
928 		if (p->p_flag & P_TRACED)
929 			kp->ki_tracer = p->p_pptr->p_pid;
930 	}
931 }
932 
933 /*
934  * Fill in information that is thread specific.  Must be called with
935  * target process locked.  If 'preferthread' is set, overwrite certain
936  * process-related fields that are maintained for both threads and
937  * processes.
938  */
939 static void
940 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
941 {
942 	struct proc *p;
943 
944 	p = td->td_proc;
945 	kp->ki_tdaddr = td;
946 	PROC_LOCK_ASSERT(p, MA_OWNED);
947 
948 	if (preferthread)
949 		PROC_STATLOCK(p);
950 	thread_lock(td);
951 	if (td->td_wmesg != NULL)
952 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
953 	else
954 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
955 	strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
956 	if (TD_ON_LOCK(td)) {
957 		kp->ki_kiflag |= KI_LOCKBLOCK;
958 		strlcpy(kp->ki_lockname, td->td_lockname,
959 		    sizeof(kp->ki_lockname));
960 	} else {
961 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
962 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
963 	}
964 
965 	if (p->p_state == PRS_NORMAL) { /* approximate. */
966 		if (TD_ON_RUNQ(td) ||
967 		    TD_CAN_RUN(td) ||
968 		    TD_IS_RUNNING(td)) {
969 			kp->ki_stat = SRUN;
970 		} else if (P_SHOULDSTOP(p)) {
971 			kp->ki_stat = SSTOP;
972 		} else if (TD_IS_SLEEPING(td)) {
973 			kp->ki_stat = SSLEEP;
974 		} else if (TD_ON_LOCK(td)) {
975 			kp->ki_stat = SLOCK;
976 		} else {
977 			kp->ki_stat = SWAIT;
978 		}
979 	} else if (p->p_state == PRS_ZOMBIE) {
980 		kp->ki_stat = SZOMB;
981 	} else {
982 		kp->ki_stat = SIDL;
983 	}
984 
985 	/* Things in the thread */
986 	kp->ki_wchan = td->td_wchan;
987 	kp->ki_pri.pri_level = td->td_priority;
988 	kp->ki_pri.pri_native = td->td_base_pri;
989 
990 	/*
991 	 * Note: legacy fields; clamp at the old NOCPU value and/or
992 	 * the maximum u_char CPU value.
993 	 */
994 	if (td->td_lastcpu == NOCPU)
995 		kp->ki_lastcpu_old = NOCPU_OLD;
996 	else if (td->td_lastcpu > MAXCPU_OLD)
997 		kp->ki_lastcpu_old = MAXCPU_OLD;
998 	else
999 		kp->ki_lastcpu_old = td->td_lastcpu;
1000 
1001 	if (td->td_oncpu == NOCPU)
1002 		kp->ki_oncpu_old = NOCPU_OLD;
1003 	else if (td->td_oncpu > MAXCPU_OLD)
1004 		kp->ki_oncpu_old = MAXCPU_OLD;
1005 	else
1006 		kp->ki_oncpu_old = td->td_oncpu;
1007 
1008 	kp->ki_lastcpu = td->td_lastcpu;
1009 	kp->ki_oncpu = td->td_oncpu;
1010 	kp->ki_tdflags = td->td_flags;
1011 	kp->ki_tid = td->td_tid;
1012 	kp->ki_numthreads = p->p_numthreads;
1013 	kp->ki_pcb = td->td_pcb;
1014 	kp->ki_kstack = (void *)td->td_kstack;
1015 	kp->ki_slptime = (ticks - td->td_slptick) / hz;
1016 	kp->ki_pri.pri_class = td->td_pri_class;
1017 	kp->ki_pri.pri_user = td->td_user_pri;
1018 
1019 	if (preferthread) {
1020 		rufetchtd(td, &kp->ki_rusage);
1021 		kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1022 		kp->ki_pctcpu = sched_pctcpu(td);
1023 		kp->ki_estcpu = td->td_estcpu;
1024 		kp->ki_cow = td->td_cow;
1025 	}
1026 
1027 	/* We can't get this anymore but ps etc never used it anyway. */
1028 	kp->ki_rqindex = 0;
1029 
1030 	if (preferthread)
1031 		kp->ki_siglist = td->td_siglist;
1032 	kp->ki_sigmask = td->td_sigmask;
1033 	thread_unlock(td);
1034 	if (preferthread)
1035 		PROC_STATUNLOCK(p);
1036 }
1037 
1038 /*
1039  * Fill in a kinfo_proc structure for the specified process.
1040  * Must be called with the target process locked.
1041  */
1042 void
1043 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1044 {
1045 
1046 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1047 
1048 	fill_kinfo_proc_only(p, kp);
1049 	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1050 	fill_kinfo_aggregate(p, kp);
1051 }
1052 
1053 struct pstats *
1054 pstats_alloc(void)
1055 {
1056 
1057 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1058 }
1059 
1060 /*
1061  * Copy parts of p_stats; zero the rest of p_stats (statistics).
1062  */
1063 void
1064 pstats_fork(struct pstats *src, struct pstats *dst)
1065 {
1066 
1067 	bzero(&dst->pstat_startzero,
1068 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1069 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1070 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1071 }
1072 
1073 void
1074 pstats_free(struct pstats *ps)
1075 {
1076 
1077 	free(ps, M_SUBPROC);
1078 }
1079 
1080 static struct proc *
1081 zpfind_locked(pid_t pid)
1082 {
1083 	struct proc *p;
1084 
1085 	sx_assert(&allproc_lock, SX_LOCKED);
1086 	LIST_FOREACH(p, &zombproc, p_list) {
1087 		if (p->p_pid == pid) {
1088 			PROC_LOCK(p);
1089 			break;
1090 		}
1091 	}
1092 	return (p);
1093 }
1094 
1095 /*
1096  * Locate a zombie process by number
1097  */
1098 struct proc *
1099 zpfind(pid_t pid)
1100 {
1101 	struct proc *p;
1102 
1103 	sx_slock(&allproc_lock);
1104 	p = zpfind_locked(pid);
1105 	sx_sunlock(&allproc_lock);
1106 	return (p);
1107 }
1108 
1109 #ifdef COMPAT_FREEBSD32
1110 
1111 /*
1112  * This function is typically used to copy out the kernel address, so
1113  * it can be replaced by assignment of zero.
1114  */
1115 static inline uint32_t
1116 ptr32_trim(void *ptr)
1117 {
1118 	uintptr_t uptr;
1119 
1120 	uptr = (uintptr_t)ptr;
1121 	return ((uptr > UINT_MAX) ? 0 : uptr);
1122 }
1123 
1124 #define PTRTRIM_CP(src,dst,fld) \
1125 	do { (dst).fld = ptr32_trim((src).fld); } while (0)
1126 
1127 static void
1128 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1129 {
1130 	int i;
1131 
1132 	bzero(ki32, sizeof(struct kinfo_proc32));
1133 	ki32->ki_structsize = sizeof(struct kinfo_proc32);
1134 	CP(*ki, *ki32, ki_layout);
1135 	PTRTRIM_CP(*ki, *ki32, ki_args);
1136 	PTRTRIM_CP(*ki, *ki32, ki_paddr);
1137 	PTRTRIM_CP(*ki, *ki32, ki_addr);
1138 	PTRTRIM_CP(*ki, *ki32, ki_tracep);
1139 	PTRTRIM_CP(*ki, *ki32, ki_textvp);
1140 	PTRTRIM_CP(*ki, *ki32, ki_fd);
1141 	PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1142 	PTRTRIM_CP(*ki, *ki32, ki_wchan);
1143 	CP(*ki, *ki32, ki_pid);
1144 	CP(*ki, *ki32, ki_ppid);
1145 	CP(*ki, *ki32, ki_pgid);
1146 	CP(*ki, *ki32, ki_tpgid);
1147 	CP(*ki, *ki32, ki_sid);
1148 	CP(*ki, *ki32, ki_tsid);
1149 	CP(*ki, *ki32, ki_jobc);
1150 	CP(*ki, *ki32, ki_tdev);
1151 	CP(*ki, *ki32, ki_siglist);
1152 	CP(*ki, *ki32, ki_sigmask);
1153 	CP(*ki, *ki32, ki_sigignore);
1154 	CP(*ki, *ki32, ki_sigcatch);
1155 	CP(*ki, *ki32, ki_uid);
1156 	CP(*ki, *ki32, ki_ruid);
1157 	CP(*ki, *ki32, ki_svuid);
1158 	CP(*ki, *ki32, ki_rgid);
1159 	CP(*ki, *ki32, ki_svgid);
1160 	CP(*ki, *ki32, ki_ngroups);
1161 	for (i = 0; i < KI_NGROUPS; i++)
1162 		CP(*ki, *ki32, ki_groups[i]);
1163 	CP(*ki, *ki32, ki_size);
1164 	CP(*ki, *ki32, ki_rssize);
1165 	CP(*ki, *ki32, ki_swrss);
1166 	CP(*ki, *ki32, ki_tsize);
1167 	CP(*ki, *ki32, ki_dsize);
1168 	CP(*ki, *ki32, ki_ssize);
1169 	CP(*ki, *ki32, ki_xstat);
1170 	CP(*ki, *ki32, ki_acflag);
1171 	CP(*ki, *ki32, ki_pctcpu);
1172 	CP(*ki, *ki32, ki_estcpu);
1173 	CP(*ki, *ki32, ki_slptime);
1174 	CP(*ki, *ki32, ki_swtime);
1175 	CP(*ki, *ki32, ki_cow);
1176 	CP(*ki, *ki32, ki_runtime);
1177 	TV_CP(*ki, *ki32, ki_start);
1178 	TV_CP(*ki, *ki32, ki_childtime);
1179 	CP(*ki, *ki32, ki_flag);
1180 	CP(*ki, *ki32, ki_kiflag);
1181 	CP(*ki, *ki32, ki_traceflag);
1182 	CP(*ki, *ki32, ki_stat);
1183 	CP(*ki, *ki32, ki_nice);
1184 	CP(*ki, *ki32, ki_lock);
1185 	CP(*ki, *ki32, ki_rqindex);
1186 	CP(*ki, *ki32, ki_oncpu);
1187 	CP(*ki, *ki32, ki_lastcpu);
1188 
1189 	/* XXX TODO: wrap cpu value as appropriate */
1190 	CP(*ki, *ki32, ki_oncpu_old);
1191 	CP(*ki, *ki32, ki_lastcpu_old);
1192 
1193 	bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1194 	bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1195 	bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1196 	bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1197 	bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1198 	bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1199 	bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1200 	CP(*ki, *ki32, ki_tracer);
1201 	CP(*ki, *ki32, ki_flag2);
1202 	CP(*ki, *ki32, ki_fibnum);
1203 	CP(*ki, *ki32, ki_cr_flags);
1204 	CP(*ki, *ki32, ki_jid);
1205 	CP(*ki, *ki32, ki_numthreads);
1206 	CP(*ki, *ki32, ki_tid);
1207 	CP(*ki, *ki32, ki_pri);
1208 	freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1209 	freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1210 	PTRTRIM_CP(*ki, *ki32, ki_pcb);
1211 	PTRTRIM_CP(*ki, *ki32, ki_kstack);
1212 	PTRTRIM_CP(*ki, *ki32, ki_udata);
1213 	CP(*ki, *ki32, ki_sflag);
1214 	CP(*ki, *ki32, ki_tdflags);
1215 }
1216 #endif
1217 
1218 int
1219 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1220 {
1221 	struct thread *td;
1222 	struct kinfo_proc ki;
1223 #ifdef COMPAT_FREEBSD32
1224 	struct kinfo_proc32 ki32;
1225 #endif
1226 	int error;
1227 
1228 	PROC_LOCK_ASSERT(p, MA_OWNED);
1229 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1230 
1231 	error = 0;
1232 	fill_kinfo_proc(p, &ki);
1233 	if ((flags & KERN_PROC_NOTHREADS) != 0) {
1234 #ifdef COMPAT_FREEBSD32
1235 		if ((flags & KERN_PROC_MASK32) != 0) {
1236 			freebsd32_kinfo_proc_out(&ki, &ki32);
1237 			if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1238 				error = ENOMEM;
1239 		} else
1240 #endif
1241 			if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1242 				error = ENOMEM;
1243 	} else {
1244 		FOREACH_THREAD_IN_PROC(p, td) {
1245 			fill_kinfo_thread(td, &ki, 1);
1246 #ifdef COMPAT_FREEBSD32
1247 			if ((flags & KERN_PROC_MASK32) != 0) {
1248 				freebsd32_kinfo_proc_out(&ki, &ki32);
1249 				if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1250 					error = ENOMEM;
1251 			} else
1252 #endif
1253 				if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1254 					error = ENOMEM;
1255 			if (error != 0)
1256 				break;
1257 		}
1258 	}
1259 	PROC_UNLOCK(p);
1260 	return (error);
1261 }
1262 
1263 static int
1264 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1265     int doingzomb)
1266 {
1267 	struct sbuf sb;
1268 	struct kinfo_proc ki;
1269 	struct proc *np;
1270 	int error, error2;
1271 	pid_t pid;
1272 
1273 	pid = p->p_pid;
1274 	sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1275 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1276 	error = kern_proc_out(p, &sb, flags);
1277 	error2 = sbuf_finish(&sb);
1278 	sbuf_delete(&sb);
1279 	if (error != 0)
1280 		return (error);
1281 	else if (error2 != 0)
1282 		return (error2);
1283 	if (doingzomb)
1284 		np = zpfind(pid);
1285 	else {
1286 		if (pid == 0)
1287 			return (0);
1288 		np = pfind(pid);
1289 	}
1290 	if (np == NULL)
1291 		return (ESRCH);
1292 	if (np != p) {
1293 		PROC_UNLOCK(np);
1294 		return (ESRCH);
1295 	}
1296 	PROC_UNLOCK(np);
1297 	return (0);
1298 }
1299 
1300 static int
1301 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1302 {
1303 	int *name = (int *)arg1;
1304 	u_int namelen = arg2;
1305 	struct proc *p;
1306 	int flags, doingzomb, oid_number;
1307 	int error = 0;
1308 
1309 	oid_number = oidp->oid_number;
1310 	if (oid_number != KERN_PROC_ALL &&
1311 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
1312 		flags = KERN_PROC_NOTHREADS;
1313 	else {
1314 		flags = 0;
1315 		oid_number &= ~KERN_PROC_INC_THREAD;
1316 	}
1317 #ifdef COMPAT_FREEBSD32
1318 	if (req->flags & SCTL_MASK32)
1319 		flags |= KERN_PROC_MASK32;
1320 #endif
1321 	if (oid_number == KERN_PROC_PID) {
1322 		if (namelen != 1)
1323 			return (EINVAL);
1324 		error = sysctl_wire_old_buffer(req, 0);
1325 		if (error)
1326 			return (error);
1327 		sx_slock(&proctree_lock);
1328 		error = pget((pid_t)name[0], PGET_CANSEE, &p);
1329 		if (error == 0)
1330 			error = sysctl_out_proc(p, req, flags, 0);
1331 		sx_sunlock(&proctree_lock);
1332 		return (error);
1333 	}
1334 
1335 	switch (oid_number) {
1336 	case KERN_PROC_ALL:
1337 		if (namelen != 0)
1338 			return (EINVAL);
1339 		break;
1340 	case KERN_PROC_PROC:
1341 		if (namelen != 0 && namelen != 1)
1342 			return (EINVAL);
1343 		break;
1344 	default:
1345 		if (namelen != 1)
1346 			return (EINVAL);
1347 		break;
1348 	}
1349 
1350 	if (!req->oldptr) {
1351 		/* overestimate by 5 procs */
1352 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1353 		if (error)
1354 			return (error);
1355 	}
1356 	error = sysctl_wire_old_buffer(req, 0);
1357 	if (error != 0)
1358 		return (error);
1359 	sx_slock(&proctree_lock);
1360 	sx_slock(&allproc_lock);
1361 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1362 		if (!doingzomb)
1363 			p = LIST_FIRST(&allproc);
1364 		else
1365 			p = LIST_FIRST(&zombproc);
1366 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1367 			/*
1368 			 * Skip embryonic processes.
1369 			 */
1370 			PROC_LOCK(p);
1371 			if (p->p_state == PRS_NEW) {
1372 				PROC_UNLOCK(p);
1373 				continue;
1374 			}
1375 			KASSERT(p->p_ucred != NULL,
1376 			    ("process credential is NULL for non-NEW proc"));
1377 			/*
1378 			 * Show a user only appropriate processes.
1379 			 */
1380 			if (p_cansee(curthread, p)) {
1381 				PROC_UNLOCK(p);
1382 				continue;
1383 			}
1384 			/*
1385 			 * TODO - make more efficient (see notes below).
1386 			 * do by session.
1387 			 */
1388 			switch (oid_number) {
1389 
1390 			case KERN_PROC_GID:
1391 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1392 					PROC_UNLOCK(p);
1393 					continue;
1394 				}
1395 				break;
1396 
1397 			case KERN_PROC_PGRP:
1398 				/* could do this by traversing pgrp */
1399 				if (p->p_pgrp == NULL ||
1400 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1401 					PROC_UNLOCK(p);
1402 					continue;
1403 				}
1404 				break;
1405 
1406 			case KERN_PROC_RGID:
1407 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1408 					PROC_UNLOCK(p);
1409 					continue;
1410 				}
1411 				break;
1412 
1413 			case KERN_PROC_SESSION:
1414 				if (p->p_session == NULL ||
1415 				    p->p_session->s_sid != (pid_t)name[0]) {
1416 					PROC_UNLOCK(p);
1417 					continue;
1418 				}
1419 				break;
1420 
1421 			case KERN_PROC_TTY:
1422 				if ((p->p_flag & P_CONTROLT) == 0 ||
1423 				    p->p_session == NULL) {
1424 					PROC_UNLOCK(p);
1425 					continue;
1426 				}
1427 				/* XXX proctree_lock */
1428 				SESS_LOCK(p->p_session);
1429 				if (p->p_session->s_ttyp == NULL ||
1430 				    tty_udev(p->p_session->s_ttyp) !=
1431 				    (dev_t)name[0]) {
1432 					SESS_UNLOCK(p->p_session);
1433 					PROC_UNLOCK(p);
1434 					continue;
1435 				}
1436 				SESS_UNLOCK(p->p_session);
1437 				break;
1438 
1439 			case KERN_PROC_UID:
1440 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1441 					PROC_UNLOCK(p);
1442 					continue;
1443 				}
1444 				break;
1445 
1446 			case KERN_PROC_RUID:
1447 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1448 					PROC_UNLOCK(p);
1449 					continue;
1450 				}
1451 				break;
1452 
1453 			case KERN_PROC_PROC:
1454 				break;
1455 
1456 			default:
1457 				break;
1458 
1459 			}
1460 
1461 			error = sysctl_out_proc(p, req, flags, doingzomb);
1462 			if (error) {
1463 				sx_sunlock(&allproc_lock);
1464 				sx_sunlock(&proctree_lock);
1465 				return (error);
1466 			}
1467 		}
1468 	}
1469 	sx_sunlock(&allproc_lock);
1470 	sx_sunlock(&proctree_lock);
1471 	return (0);
1472 }
1473 
1474 struct pargs *
1475 pargs_alloc(int len)
1476 {
1477 	struct pargs *pa;
1478 
1479 	pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1480 		M_WAITOK);
1481 	refcount_init(&pa->ar_ref, 1);
1482 	pa->ar_length = len;
1483 	return (pa);
1484 }
1485 
1486 static void
1487 pargs_free(struct pargs *pa)
1488 {
1489 
1490 	free(pa, M_PARGS);
1491 }
1492 
1493 void
1494 pargs_hold(struct pargs *pa)
1495 {
1496 
1497 	if (pa == NULL)
1498 		return;
1499 	refcount_acquire(&pa->ar_ref);
1500 }
1501 
1502 void
1503 pargs_drop(struct pargs *pa)
1504 {
1505 
1506 	if (pa == NULL)
1507 		return;
1508 	if (refcount_release(&pa->ar_ref))
1509 		pargs_free(pa);
1510 }
1511 
1512 static int
1513 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1514     size_t len)
1515 {
1516 	struct iovec iov;
1517 	struct uio uio;
1518 
1519 	iov.iov_base = (caddr_t)buf;
1520 	iov.iov_len = len;
1521 	uio.uio_iov = &iov;
1522 	uio.uio_iovcnt = 1;
1523 	uio.uio_offset = offset;
1524 	uio.uio_resid = (ssize_t)len;
1525 	uio.uio_segflg = UIO_SYSSPACE;
1526 	uio.uio_rw = UIO_READ;
1527 	uio.uio_td = td;
1528 
1529 	return (proc_rwmem(p, &uio));
1530 }
1531 
1532 static int
1533 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1534     size_t len)
1535 {
1536 	size_t i;
1537 	int error;
1538 
1539 	error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1540 	/*
1541 	 * Reading the chunk may validly return EFAULT if the string is shorter
1542 	 * than the chunk and is aligned at the end of the page, assuming the
1543 	 * next page is not mapped.  So if EFAULT is returned do a fallback to
1544 	 * one byte read loop.
1545 	 */
1546 	if (error == EFAULT) {
1547 		for (i = 0; i < len; i++, buf++, sptr++) {
1548 			error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1549 			if (error != 0)
1550 				return (error);
1551 			if (*buf == '\0')
1552 				break;
1553 		}
1554 		error = 0;
1555 	}
1556 	return (error);
1557 }
1558 
1559 #define PROC_AUXV_MAX	256	/* Safety limit on auxv size. */
1560 
1561 enum proc_vector_type {
1562 	PROC_ARG,
1563 	PROC_ENV,
1564 	PROC_AUX,
1565 };
1566 
1567 #ifdef COMPAT_FREEBSD32
1568 static int
1569 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1570     size_t *vsizep, enum proc_vector_type type)
1571 {
1572 	struct freebsd32_ps_strings pss;
1573 	Elf32_Auxinfo aux;
1574 	vm_offset_t vptr, ptr;
1575 	uint32_t *proc_vector32;
1576 	char **proc_vector;
1577 	size_t vsize, size;
1578 	int i, error;
1579 
1580 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1581 	    &pss, sizeof(pss));
1582 	if (error != 0)
1583 		return (error);
1584 	switch (type) {
1585 	case PROC_ARG:
1586 		vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1587 		vsize = pss.ps_nargvstr;
1588 		if (vsize > ARG_MAX)
1589 			return (ENOEXEC);
1590 		size = vsize * sizeof(int32_t);
1591 		break;
1592 	case PROC_ENV:
1593 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1594 		vsize = pss.ps_nenvstr;
1595 		if (vsize > ARG_MAX)
1596 			return (ENOEXEC);
1597 		size = vsize * sizeof(int32_t);
1598 		break;
1599 	case PROC_AUX:
1600 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1601 		    (pss.ps_nenvstr + 1) * sizeof(int32_t);
1602 		if (vptr % 4 != 0)
1603 			return (ENOEXEC);
1604 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1605 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1606 			if (error != 0)
1607 				return (error);
1608 			if (aux.a_type == AT_NULL)
1609 				break;
1610 			ptr += sizeof(aux);
1611 		}
1612 		if (aux.a_type != AT_NULL)
1613 			return (ENOEXEC);
1614 		vsize = i + 1;
1615 		size = vsize * sizeof(aux);
1616 		break;
1617 	default:
1618 		KASSERT(0, ("Wrong proc vector type: %d", type));
1619 		return (EINVAL);
1620 	}
1621 	proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1622 	error = proc_read_mem(td, p, vptr, proc_vector32, size);
1623 	if (error != 0)
1624 		goto done;
1625 	if (type == PROC_AUX) {
1626 		*proc_vectorp = (char **)proc_vector32;
1627 		*vsizep = vsize;
1628 		return (0);
1629 	}
1630 	proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1631 	for (i = 0; i < (int)vsize; i++)
1632 		proc_vector[i] = PTRIN(proc_vector32[i]);
1633 	*proc_vectorp = proc_vector;
1634 	*vsizep = vsize;
1635 done:
1636 	free(proc_vector32, M_TEMP);
1637 	return (error);
1638 }
1639 #endif
1640 
1641 static int
1642 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1643     size_t *vsizep, enum proc_vector_type type)
1644 {
1645 	struct ps_strings pss;
1646 	Elf_Auxinfo aux;
1647 	vm_offset_t vptr, ptr;
1648 	char **proc_vector;
1649 	size_t vsize, size;
1650 	int error, i;
1651 
1652 #ifdef COMPAT_FREEBSD32
1653 	if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1654 		return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1655 #endif
1656 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1657 	    &pss, sizeof(pss));
1658 	if (error != 0)
1659 		return (error);
1660 	switch (type) {
1661 	case PROC_ARG:
1662 		vptr = (vm_offset_t)pss.ps_argvstr;
1663 		vsize = pss.ps_nargvstr;
1664 		if (vsize > ARG_MAX)
1665 			return (ENOEXEC);
1666 		size = vsize * sizeof(char *);
1667 		break;
1668 	case PROC_ENV:
1669 		vptr = (vm_offset_t)pss.ps_envstr;
1670 		vsize = pss.ps_nenvstr;
1671 		if (vsize > ARG_MAX)
1672 			return (ENOEXEC);
1673 		size = vsize * sizeof(char *);
1674 		break;
1675 	case PROC_AUX:
1676 		/*
1677 		 * The aux array is just above env array on the stack. Check
1678 		 * that the address is naturally aligned.
1679 		 */
1680 		vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1681 		    * sizeof(char *);
1682 #if __ELF_WORD_SIZE == 64
1683 		if (vptr % sizeof(uint64_t) != 0)
1684 #else
1685 		if (vptr % sizeof(uint32_t) != 0)
1686 #endif
1687 			return (ENOEXEC);
1688 		/*
1689 		 * We count the array size reading the aux vectors from the
1690 		 * stack until AT_NULL vector is returned.  So (to keep the code
1691 		 * simple) we read the process stack twice: the first time here
1692 		 * to find the size and the second time when copying the vectors
1693 		 * to the allocated proc_vector.
1694 		 */
1695 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1696 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1697 			if (error != 0)
1698 				return (error);
1699 			if (aux.a_type == AT_NULL)
1700 				break;
1701 			ptr += sizeof(aux);
1702 		}
1703 		/*
1704 		 * If the PROC_AUXV_MAX entries are iterated over, and we have
1705 		 * not reached AT_NULL, it is most likely we are reading wrong
1706 		 * data: either the process doesn't have auxv array or data has
1707 		 * been modified. Return the error in this case.
1708 		 */
1709 		if (aux.a_type != AT_NULL)
1710 			return (ENOEXEC);
1711 		vsize = i + 1;
1712 		size = vsize * sizeof(aux);
1713 		break;
1714 	default:
1715 		KASSERT(0, ("Wrong proc vector type: %d", type));
1716 		return (EINVAL); /* In case we are built without INVARIANTS. */
1717 	}
1718 	proc_vector = malloc(size, M_TEMP, M_WAITOK);
1719 	if (proc_vector == NULL)
1720 		return (ENOMEM);
1721 	error = proc_read_mem(td, p, vptr, proc_vector, size);
1722 	if (error != 0) {
1723 		free(proc_vector, M_TEMP);
1724 		return (error);
1725 	}
1726 	*proc_vectorp = proc_vector;
1727 	*vsizep = vsize;
1728 
1729 	return (0);
1730 }
1731 
1732 #define GET_PS_STRINGS_CHUNK_SZ	256	/* Chunk size (bytes) for ps_strings operations. */
1733 
1734 static int
1735 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1736     enum proc_vector_type type)
1737 {
1738 	size_t done, len, nchr, vsize;
1739 	int error, i;
1740 	char **proc_vector, *sptr;
1741 	char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1742 
1743 	PROC_ASSERT_HELD(p);
1744 
1745 	/*
1746 	 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1747 	 */
1748 	nchr = 2 * (PATH_MAX + ARG_MAX);
1749 
1750 	error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1751 	if (error != 0)
1752 		return (error);
1753 	for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1754 		/*
1755 		 * The program may have scribbled into its argv array, e.g. to
1756 		 * remove some arguments.  If that has happened, break out
1757 		 * before trying to read from NULL.
1758 		 */
1759 		if (proc_vector[i] == NULL)
1760 			break;
1761 		for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1762 			error = proc_read_string(td, p, sptr, pss_string,
1763 			    sizeof(pss_string));
1764 			if (error != 0)
1765 				goto done;
1766 			len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1767 			if (done + len >= nchr)
1768 				len = nchr - done - 1;
1769 			sbuf_bcat(sb, pss_string, len);
1770 			if (len != GET_PS_STRINGS_CHUNK_SZ)
1771 				break;
1772 			done += GET_PS_STRINGS_CHUNK_SZ;
1773 		}
1774 		sbuf_bcat(sb, "", 1);
1775 		done += len + 1;
1776 	}
1777 done:
1778 	free(proc_vector, M_TEMP);
1779 	return (error);
1780 }
1781 
1782 int
1783 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1784 {
1785 
1786 	return (get_ps_strings(curthread, p, sb, PROC_ARG));
1787 }
1788 
1789 int
1790 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1791 {
1792 
1793 	return (get_ps_strings(curthread, p, sb, PROC_ENV));
1794 }
1795 
1796 int
1797 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1798 {
1799 	size_t vsize, size;
1800 	char **auxv;
1801 	int error;
1802 
1803 	error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1804 	if (error == 0) {
1805 #ifdef COMPAT_FREEBSD32
1806 		if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1807 			size = vsize * sizeof(Elf32_Auxinfo);
1808 		else
1809 #endif
1810 			size = vsize * sizeof(Elf_Auxinfo);
1811 		if (sbuf_bcat(sb, auxv, size) != 0)
1812 			error = ENOMEM;
1813 		free(auxv, M_TEMP);
1814 	}
1815 	return (error);
1816 }
1817 
1818 /*
1819  * This sysctl allows a process to retrieve the argument list or process
1820  * title for another process without groping around in the address space
1821  * of the other process.  It also allow a process to set its own "process
1822  * title to a string of its own choice.
1823  */
1824 static int
1825 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1826 {
1827 	int *name = (int *)arg1;
1828 	u_int namelen = arg2;
1829 	struct pargs *newpa, *pa;
1830 	struct proc *p;
1831 	struct sbuf sb;
1832 	int flags, error = 0, error2;
1833 
1834 	if (namelen != 1)
1835 		return (EINVAL);
1836 
1837 	flags = PGET_CANSEE;
1838 	if (req->newptr != NULL)
1839 		flags |= PGET_ISCURRENT;
1840 	error = pget((pid_t)name[0], flags, &p);
1841 	if (error)
1842 		return (error);
1843 
1844 	pa = p->p_args;
1845 	if (pa != NULL) {
1846 		pargs_hold(pa);
1847 		PROC_UNLOCK(p);
1848 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1849 		pargs_drop(pa);
1850 	} else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1851 		_PHOLD(p);
1852 		PROC_UNLOCK(p);
1853 		sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1854 		sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1855 		error = proc_getargv(curthread, p, &sb);
1856 		error2 = sbuf_finish(&sb);
1857 		PRELE(p);
1858 		sbuf_delete(&sb);
1859 		if (error == 0 && error2 != 0)
1860 			error = error2;
1861 	} else {
1862 		PROC_UNLOCK(p);
1863 	}
1864 	if (error != 0 || req->newptr == NULL)
1865 		return (error);
1866 
1867 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1868 		return (ENOMEM);
1869 	newpa = pargs_alloc(req->newlen);
1870 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1871 	if (error != 0) {
1872 		pargs_free(newpa);
1873 		return (error);
1874 	}
1875 	PROC_LOCK(p);
1876 	pa = p->p_args;
1877 	p->p_args = newpa;
1878 	PROC_UNLOCK(p);
1879 	pargs_drop(pa);
1880 	return (0);
1881 }
1882 
1883 /*
1884  * This sysctl allows a process to retrieve environment of another process.
1885  */
1886 static int
1887 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1888 {
1889 	int *name = (int *)arg1;
1890 	u_int namelen = arg2;
1891 	struct proc *p;
1892 	struct sbuf sb;
1893 	int error, error2;
1894 
1895 	if (namelen != 1)
1896 		return (EINVAL);
1897 
1898 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1899 	if (error != 0)
1900 		return (error);
1901 	if ((p->p_flag & P_SYSTEM) != 0) {
1902 		PRELE(p);
1903 		return (0);
1904 	}
1905 
1906 	sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1907 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1908 	error = proc_getenvv(curthread, p, &sb);
1909 	error2 = sbuf_finish(&sb);
1910 	PRELE(p);
1911 	sbuf_delete(&sb);
1912 	return (error != 0 ? error : error2);
1913 }
1914 
1915 /*
1916  * This sysctl allows a process to retrieve ELF auxiliary vector of
1917  * another process.
1918  */
1919 static int
1920 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1921 {
1922 	int *name = (int *)arg1;
1923 	u_int namelen = arg2;
1924 	struct proc *p;
1925 	struct sbuf sb;
1926 	int error, error2;
1927 
1928 	if (namelen != 1)
1929 		return (EINVAL);
1930 
1931 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1932 	if (error != 0)
1933 		return (error);
1934 	if ((p->p_flag & P_SYSTEM) != 0) {
1935 		PRELE(p);
1936 		return (0);
1937 	}
1938 	sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1939 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1940 	error = proc_getauxv(curthread, p, &sb);
1941 	error2 = sbuf_finish(&sb);
1942 	PRELE(p);
1943 	sbuf_delete(&sb);
1944 	return (error != 0 ? error : error2);
1945 }
1946 
1947 /*
1948  * This sysctl allows a process to retrieve the path of the executable for
1949  * itself or another process.
1950  */
1951 static int
1952 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1953 {
1954 	pid_t *pidp = (pid_t *)arg1;
1955 	unsigned int arglen = arg2;
1956 	struct proc *p;
1957 	struct vnode *vp;
1958 	char *retbuf, *freebuf;
1959 	int error;
1960 
1961 	if (arglen != 1)
1962 		return (EINVAL);
1963 	if (*pidp == -1) {	/* -1 means this process */
1964 		p = req->td->td_proc;
1965 	} else {
1966 		error = pget(*pidp, PGET_CANSEE, &p);
1967 		if (error != 0)
1968 			return (error);
1969 	}
1970 
1971 	vp = p->p_textvp;
1972 	if (vp == NULL) {
1973 		if (*pidp != -1)
1974 			PROC_UNLOCK(p);
1975 		return (0);
1976 	}
1977 	vref(vp);
1978 	if (*pidp != -1)
1979 		PROC_UNLOCK(p);
1980 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1981 	vrele(vp);
1982 	if (error)
1983 		return (error);
1984 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1985 	free(freebuf, M_TEMP);
1986 	return (error);
1987 }
1988 
1989 static int
1990 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1991 {
1992 	struct proc *p;
1993 	char *sv_name;
1994 	int *name;
1995 	int namelen;
1996 	int error;
1997 
1998 	namelen = arg2;
1999 	if (namelen != 1)
2000 		return (EINVAL);
2001 
2002 	name = (int *)arg1;
2003 	error = pget((pid_t)name[0], PGET_CANSEE, &p);
2004 	if (error != 0)
2005 		return (error);
2006 	sv_name = p->p_sysent->sv_name;
2007 	PROC_UNLOCK(p);
2008 	return (sysctl_handle_string(oidp, sv_name, 0, req));
2009 }
2010 
2011 #ifdef KINFO_OVMENTRY_SIZE
2012 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2013 #endif
2014 
2015 #ifdef COMPAT_FREEBSD7
2016 static int
2017 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2018 {
2019 	vm_map_entry_t entry, tmp_entry;
2020 	unsigned int last_timestamp;
2021 	char *fullpath, *freepath;
2022 	struct kinfo_ovmentry *kve;
2023 	struct vattr va;
2024 	struct ucred *cred;
2025 	int error, *name;
2026 	struct vnode *vp;
2027 	struct proc *p;
2028 	vm_map_t map;
2029 	struct vmspace *vm;
2030 
2031 	name = (int *)arg1;
2032 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2033 	if (error != 0)
2034 		return (error);
2035 	vm = vmspace_acquire_ref(p);
2036 	if (vm == NULL) {
2037 		PRELE(p);
2038 		return (ESRCH);
2039 	}
2040 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2041 
2042 	map = &vm->vm_map;
2043 	vm_map_lock_read(map);
2044 	for (entry = map->header.next; entry != &map->header;
2045 	    entry = entry->next) {
2046 		vm_object_t obj, tobj, lobj;
2047 		vm_offset_t addr;
2048 
2049 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2050 			continue;
2051 
2052 		bzero(kve, sizeof(*kve));
2053 		kve->kve_structsize = sizeof(*kve);
2054 
2055 		kve->kve_private_resident = 0;
2056 		obj = entry->object.vm_object;
2057 		if (obj != NULL) {
2058 			VM_OBJECT_RLOCK(obj);
2059 			if (obj->shadow_count == 1)
2060 				kve->kve_private_resident =
2061 				    obj->resident_page_count;
2062 		}
2063 		kve->kve_resident = 0;
2064 		addr = entry->start;
2065 		while (addr < entry->end) {
2066 			if (pmap_extract(map->pmap, addr))
2067 				kve->kve_resident++;
2068 			addr += PAGE_SIZE;
2069 		}
2070 
2071 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2072 			if (tobj != obj)
2073 				VM_OBJECT_RLOCK(tobj);
2074 			if (lobj != obj)
2075 				VM_OBJECT_RUNLOCK(lobj);
2076 			lobj = tobj;
2077 		}
2078 
2079 		kve->kve_start = (void*)entry->start;
2080 		kve->kve_end = (void*)entry->end;
2081 		kve->kve_offset = (off_t)entry->offset;
2082 
2083 		if (entry->protection & VM_PROT_READ)
2084 			kve->kve_protection |= KVME_PROT_READ;
2085 		if (entry->protection & VM_PROT_WRITE)
2086 			kve->kve_protection |= KVME_PROT_WRITE;
2087 		if (entry->protection & VM_PROT_EXECUTE)
2088 			kve->kve_protection |= KVME_PROT_EXEC;
2089 
2090 		if (entry->eflags & MAP_ENTRY_COW)
2091 			kve->kve_flags |= KVME_FLAG_COW;
2092 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2093 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2094 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2095 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2096 
2097 		last_timestamp = map->timestamp;
2098 		vm_map_unlock_read(map);
2099 
2100 		kve->kve_fileid = 0;
2101 		kve->kve_fsid = 0;
2102 		freepath = NULL;
2103 		fullpath = "";
2104 		if (lobj) {
2105 			vp = NULL;
2106 			switch (lobj->type) {
2107 			case OBJT_DEFAULT:
2108 				kve->kve_type = KVME_TYPE_DEFAULT;
2109 				break;
2110 			case OBJT_VNODE:
2111 				kve->kve_type = KVME_TYPE_VNODE;
2112 				vp = lobj->handle;
2113 				vref(vp);
2114 				break;
2115 			case OBJT_SWAP:
2116 				kve->kve_type = KVME_TYPE_SWAP;
2117 				break;
2118 			case OBJT_DEVICE:
2119 				kve->kve_type = KVME_TYPE_DEVICE;
2120 				break;
2121 			case OBJT_PHYS:
2122 				kve->kve_type = KVME_TYPE_PHYS;
2123 				break;
2124 			case OBJT_DEAD:
2125 				kve->kve_type = KVME_TYPE_DEAD;
2126 				break;
2127 			case OBJT_SG:
2128 				kve->kve_type = KVME_TYPE_SG;
2129 				break;
2130 			default:
2131 				kve->kve_type = KVME_TYPE_UNKNOWN;
2132 				break;
2133 			}
2134 			if (lobj != obj)
2135 				VM_OBJECT_RUNLOCK(lobj);
2136 
2137 			kve->kve_ref_count = obj->ref_count;
2138 			kve->kve_shadow_count = obj->shadow_count;
2139 			VM_OBJECT_RUNLOCK(obj);
2140 			if (vp != NULL) {
2141 				vn_fullpath(curthread, vp, &fullpath,
2142 				    &freepath);
2143 				cred = curthread->td_ucred;
2144 				vn_lock(vp, LK_SHARED | LK_RETRY);
2145 				if (VOP_GETATTR(vp, &va, cred) == 0) {
2146 					kve->kve_fileid = va.va_fileid;
2147 					kve->kve_fsid = va.va_fsid;
2148 				}
2149 				vput(vp);
2150 			}
2151 		} else {
2152 			kve->kve_type = KVME_TYPE_NONE;
2153 			kve->kve_ref_count = 0;
2154 			kve->kve_shadow_count = 0;
2155 		}
2156 
2157 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2158 		if (freepath != NULL)
2159 			free(freepath, M_TEMP);
2160 
2161 		error = SYSCTL_OUT(req, kve, sizeof(*kve));
2162 		vm_map_lock_read(map);
2163 		if (error)
2164 			break;
2165 		if (last_timestamp != map->timestamp) {
2166 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2167 			entry = tmp_entry;
2168 		}
2169 	}
2170 	vm_map_unlock_read(map);
2171 	vmspace_free(vm);
2172 	PRELE(p);
2173 	free(kve, M_TEMP);
2174 	return (error);
2175 }
2176 #endif	/* COMPAT_FREEBSD7 */
2177 
2178 #ifdef KINFO_VMENTRY_SIZE
2179 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2180 #endif
2181 
2182 static void
2183 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2184     struct kinfo_vmentry *kve)
2185 {
2186 	vm_object_t obj, tobj;
2187 	vm_page_t m, m_adv;
2188 	vm_offset_t addr;
2189 	vm_paddr_t locked_pa;
2190 	vm_pindex_t pi, pi_adv, pindex;
2191 
2192 	locked_pa = 0;
2193 	obj = entry->object.vm_object;
2194 	addr = entry->start;
2195 	m_adv = NULL;
2196 	pi = OFF_TO_IDX(entry->offset);
2197 	for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2198 		if (m_adv != NULL) {
2199 			m = m_adv;
2200 		} else {
2201 			pi_adv = OFF_TO_IDX(entry->end - addr);
2202 			pindex = pi;
2203 			for (tobj = obj;; tobj = tobj->backing_object) {
2204 				m = vm_page_find_least(tobj, pindex);
2205 				if (m != NULL) {
2206 					if (m->pindex == pindex)
2207 						break;
2208 					if (pi_adv > m->pindex - pindex) {
2209 						pi_adv = m->pindex - pindex;
2210 						m_adv = m;
2211 					}
2212 				}
2213 				if (tobj->backing_object == NULL)
2214 					goto next;
2215 				pindex += OFF_TO_IDX(tobj->
2216 				    backing_object_offset);
2217 			}
2218 		}
2219 		m_adv = NULL;
2220 		if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2221 		    (addr & (pagesizes[1] - 1)) == 0 &&
2222 		    (pmap_mincore(map->pmap, addr, &locked_pa) &
2223 		    MINCORE_SUPER) != 0) {
2224 			kve->kve_flags |= KVME_FLAG_SUPER;
2225 			pi_adv = OFF_TO_IDX(pagesizes[1]);
2226 		} else {
2227 			/*
2228 			 * We do not test the found page on validity.
2229 			 * Either the page is busy and being paged in,
2230 			 * or it was invalidated.  The first case
2231 			 * should be counted as resident, the second
2232 			 * is not so clear; we do account both.
2233 			 */
2234 			pi_adv = 1;
2235 		}
2236 		kve->kve_resident += pi_adv;
2237 next:;
2238 	}
2239 	PA_UNLOCK_COND(locked_pa);
2240 }
2241 
2242 /*
2243  * Must be called with the process locked and will return unlocked.
2244  */
2245 int
2246 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb)
2247 {
2248 	vm_map_entry_t entry, tmp_entry;
2249 	struct vattr va;
2250 	vm_map_t map;
2251 	vm_object_t obj, tobj, lobj;
2252 	char *fullpath, *freepath;
2253 	struct kinfo_vmentry *kve;
2254 	struct ucred *cred;
2255 	struct vnode *vp;
2256 	struct vmspace *vm;
2257 	vm_offset_t addr;
2258 	unsigned int last_timestamp;
2259 	int error;
2260 
2261 	PROC_LOCK_ASSERT(p, MA_OWNED);
2262 
2263 	_PHOLD(p);
2264 	PROC_UNLOCK(p);
2265 	vm = vmspace_acquire_ref(p);
2266 	if (vm == NULL) {
2267 		PRELE(p);
2268 		return (ESRCH);
2269 	}
2270 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2271 
2272 	error = 0;
2273 	map = &vm->vm_map;
2274 	vm_map_lock_read(map);
2275 	for (entry = map->header.next; entry != &map->header;
2276 	    entry = entry->next) {
2277 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2278 			continue;
2279 
2280 		addr = entry->end;
2281 		bzero(kve, sizeof(*kve));
2282 		obj = entry->object.vm_object;
2283 		if (obj != NULL) {
2284 			for (tobj = obj; tobj != NULL;
2285 			    tobj = tobj->backing_object) {
2286 				VM_OBJECT_RLOCK(tobj);
2287 				lobj = tobj;
2288 			}
2289 			if (obj->backing_object == NULL)
2290 				kve->kve_private_resident =
2291 				    obj->resident_page_count;
2292 			if (!vmmap_skip_res_cnt)
2293 				kern_proc_vmmap_resident(map, entry, kve);
2294 			for (tobj = obj; tobj != NULL;
2295 			    tobj = tobj->backing_object) {
2296 				if (tobj != obj && tobj != lobj)
2297 					VM_OBJECT_RUNLOCK(tobj);
2298 			}
2299 		} else {
2300 			lobj = NULL;
2301 		}
2302 
2303 		kve->kve_start = entry->start;
2304 		kve->kve_end = entry->end;
2305 		kve->kve_offset = entry->offset;
2306 
2307 		if (entry->protection & VM_PROT_READ)
2308 			kve->kve_protection |= KVME_PROT_READ;
2309 		if (entry->protection & VM_PROT_WRITE)
2310 			kve->kve_protection |= KVME_PROT_WRITE;
2311 		if (entry->protection & VM_PROT_EXECUTE)
2312 			kve->kve_protection |= KVME_PROT_EXEC;
2313 
2314 		if (entry->eflags & MAP_ENTRY_COW)
2315 			kve->kve_flags |= KVME_FLAG_COW;
2316 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2317 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2318 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2319 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2320 		if (entry->eflags & MAP_ENTRY_GROWS_UP)
2321 			kve->kve_flags |= KVME_FLAG_GROWS_UP;
2322 		if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2323 			kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2324 
2325 		last_timestamp = map->timestamp;
2326 		vm_map_unlock_read(map);
2327 
2328 		freepath = NULL;
2329 		fullpath = "";
2330 		if (lobj != NULL) {
2331 			vp = NULL;
2332 			switch (lobj->type) {
2333 			case OBJT_DEFAULT:
2334 				kve->kve_type = KVME_TYPE_DEFAULT;
2335 				break;
2336 			case OBJT_VNODE:
2337 				kve->kve_type = KVME_TYPE_VNODE;
2338 				vp = lobj->handle;
2339 				vref(vp);
2340 				break;
2341 			case OBJT_SWAP:
2342 				kve->kve_type = KVME_TYPE_SWAP;
2343 				break;
2344 			case OBJT_DEVICE:
2345 				kve->kve_type = KVME_TYPE_DEVICE;
2346 				break;
2347 			case OBJT_PHYS:
2348 				kve->kve_type = KVME_TYPE_PHYS;
2349 				break;
2350 			case OBJT_DEAD:
2351 				kve->kve_type = KVME_TYPE_DEAD;
2352 				break;
2353 			case OBJT_SG:
2354 				kve->kve_type = KVME_TYPE_SG;
2355 				break;
2356 			case OBJT_MGTDEVICE:
2357 				kve->kve_type = KVME_TYPE_MGTDEVICE;
2358 				break;
2359 			default:
2360 				kve->kve_type = KVME_TYPE_UNKNOWN;
2361 				break;
2362 			}
2363 			if (lobj != obj)
2364 				VM_OBJECT_RUNLOCK(lobj);
2365 
2366 			kve->kve_ref_count = obj->ref_count;
2367 			kve->kve_shadow_count = obj->shadow_count;
2368 			VM_OBJECT_RUNLOCK(obj);
2369 			if (vp != NULL) {
2370 				vn_fullpath(curthread, vp, &fullpath,
2371 				    &freepath);
2372 				kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2373 				cred = curthread->td_ucred;
2374 				vn_lock(vp, LK_SHARED | LK_RETRY);
2375 				if (VOP_GETATTR(vp, &va, cred) == 0) {
2376 					kve->kve_vn_fileid = va.va_fileid;
2377 					kve->kve_vn_fsid = va.va_fsid;
2378 					kve->kve_vn_mode =
2379 					    MAKEIMODE(va.va_type, va.va_mode);
2380 					kve->kve_vn_size = va.va_size;
2381 					kve->kve_vn_rdev = va.va_rdev;
2382 					kve->kve_status = KF_ATTR_VALID;
2383 				}
2384 				vput(vp);
2385 			}
2386 		} else {
2387 			kve->kve_type = KVME_TYPE_NONE;
2388 			kve->kve_ref_count = 0;
2389 			kve->kve_shadow_count = 0;
2390 		}
2391 
2392 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2393 		if (freepath != NULL)
2394 			free(freepath, M_TEMP);
2395 
2396 		/* Pack record size down */
2397 		kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
2398 		    strlen(kve->kve_path) + 1;
2399 		kve->kve_structsize = roundup(kve->kve_structsize,
2400 		    sizeof(uint64_t));
2401 		if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2402 			error = ENOMEM;
2403 		vm_map_lock_read(map);
2404 		if (error != 0)
2405 			break;
2406 		if (last_timestamp != map->timestamp) {
2407 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2408 			entry = tmp_entry;
2409 		}
2410 	}
2411 	vm_map_unlock_read(map);
2412 	vmspace_free(vm);
2413 	PRELE(p);
2414 	free(kve, M_TEMP);
2415 	return (error);
2416 }
2417 
2418 static int
2419 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2420 {
2421 	struct proc *p;
2422 	struct sbuf sb;
2423 	int error, error2, *name;
2424 
2425 	name = (int *)arg1;
2426 	sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2427 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2428 	error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2429 	if (error != 0) {
2430 		sbuf_delete(&sb);
2431 		return (error);
2432 	}
2433 	error = kern_proc_vmmap_out(p, &sb);
2434 	error2 = sbuf_finish(&sb);
2435 	sbuf_delete(&sb);
2436 	return (error != 0 ? error : error2);
2437 }
2438 
2439 #if defined(STACK) || defined(DDB)
2440 static int
2441 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2442 {
2443 	struct kinfo_kstack *kkstp;
2444 	int error, i, *name, numthreads;
2445 	lwpid_t *lwpidarray;
2446 	struct thread *td;
2447 	struct stack *st;
2448 	struct sbuf sb;
2449 	struct proc *p;
2450 
2451 	name = (int *)arg1;
2452 	error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2453 	if (error != 0)
2454 		return (error);
2455 
2456 	kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2457 	st = stack_create();
2458 
2459 	lwpidarray = NULL;
2460 	numthreads = 0;
2461 	PROC_LOCK(p);
2462 repeat:
2463 	if (numthreads < p->p_numthreads) {
2464 		if (lwpidarray != NULL) {
2465 			free(lwpidarray, M_TEMP);
2466 			lwpidarray = NULL;
2467 		}
2468 		numthreads = p->p_numthreads;
2469 		PROC_UNLOCK(p);
2470 		lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2471 		    M_WAITOK | M_ZERO);
2472 		PROC_LOCK(p);
2473 		goto repeat;
2474 	}
2475 	i = 0;
2476 
2477 	/*
2478 	 * XXXRW: During the below loop, execve(2) and countless other sorts
2479 	 * of changes could have taken place.  Should we check to see if the
2480 	 * vmspace has been replaced, or the like, in order to prevent
2481 	 * giving a snapshot that spans, say, execve(2), with some threads
2482 	 * before and some after?  Among other things, the credentials could
2483 	 * have changed, in which case the right to extract debug info might
2484 	 * no longer be assured.
2485 	 */
2486 	FOREACH_THREAD_IN_PROC(p, td) {
2487 		KASSERT(i < numthreads,
2488 		    ("sysctl_kern_proc_kstack: numthreads"));
2489 		lwpidarray[i] = td->td_tid;
2490 		i++;
2491 	}
2492 	numthreads = i;
2493 	for (i = 0; i < numthreads; i++) {
2494 		td = thread_find(p, lwpidarray[i]);
2495 		if (td == NULL) {
2496 			continue;
2497 		}
2498 		bzero(kkstp, sizeof(*kkstp));
2499 		(void)sbuf_new(&sb, kkstp->kkst_trace,
2500 		    sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2501 		thread_lock(td);
2502 		kkstp->kkst_tid = td->td_tid;
2503 		if (TD_IS_SWAPPED(td))
2504 			kkstp->kkst_state = KKST_STATE_SWAPPED;
2505 		else if (TD_IS_RUNNING(td))
2506 			kkstp->kkst_state = KKST_STATE_RUNNING;
2507 		else {
2508 			kkstp->kkst_state = KKST_STATE_STACKOK;
2509 			stack_save_td(st, td);
2510 		}
2511 		thread_unlock(td);
2512 		PROC_UNLOCK(p);
2513 		stack_sbuf_print(&sb, st);
2514 		sbuf_finish(&sb);
2515 		sbuf_delete(&sb);
2516 		error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2517 		PROC_LOCK(p);
2518 		if (error)
2519 			break;
2520 	}
2521 	_PRELE(p);
2522 	PROC_UNLOCK(p);
2523 	if (lwpidarray != NULL)
2524 		free(lwpidarray, M_TEMP);
2525 	stack_destroy(st);
2526 	free(kkstp, M_TEMP);
2527 	return (error);
2528 }
2529 #endif
2530 
2531 /*
2532  * This sysctl allows a process to retrieve the full list of groups from
2533  * itself or another process.
2534  */
2535 static int
2536 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2537 {
2538 	pid_t *pidp = (pid_t *)arg1;
2539 	unsigned int arglen = arg2;
2540 	struct proc *p;
2541 	struct ucred *cred;
2542 	int error;
2543 
2544 	if (arglen != 1)
2545 		return (EINVAL);
2546 	if (*pidp == -1) {	/* -1 means this process */
2547 		p = req->td->td_proc;
2548 		PROC_LOCK(p);
2549 	} else {
2550 		error = pget(*pidp, PGET_CANSEE, &p);
2551 		if (error != 0)
2552 			return (error);
2553 	}
2554 
2555 	cred = crhold(p->p_ucred);
2556 	PROC_UNLOCK(p);
2557 
2558 	error = SYSCTL_OUT(req, cred->cr_groups,
2559 	    cred->cr_ngroups * sizeof(gid_t));
2560 	crfree(cred);
2561 	return (error);
2562 }
2563 
2564 /*
2565  * This sysctl allows a process to retrieve or/and set the resource limit for
2566  * another process.
2567  */
2568 static int
2569 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2570 {
2571 	int *name = (int *)arg1;
2572 	u_int namelen = arg2;
2573 	struct rlimit rlim;
2574 	struct proc *p;
2575 	u_int which;
2576 	int flags, error;
2577 
2578 	if (namelen != 2)
2579 		return (EINVAL);
2580 
2581 	which = (u_int)name[1];
2582 	if (which >= RLIM_NLIMITS)
2583 		return (EINVAL);
2584 
2585 	if (req->newptr != NULL && req->newlen != sizeof(rlim))
2586 		return (EINVAL);
2587 
2588 	flags = PGET_HOLD | PGET_NOTWEXIT;
2589 	if (req->newptr != NULL)
2590 		flags |= PGET_CANDEBUG;
2591 	else
2592 		flags |= PGET_CANSEE;
2593 	error = pget((pid_t)name[0], flags, &p);
2594 	if (error != 0)
2595 		return (error);
2596 
2597 	/*
2598 	 * Retrieve limit.
2599 	 */
2600 	if (req->oldptr != NULL) {
2601 		PROC_LOCK(p);
2602 		lim_rlimit(p, which, &rlim);
2603 		PROC_UNLOCK(p);
2604 	}
2605 	error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2606 	if (error != 0)
2607 		goto errout;
2608 
2609 	/*
2610 	 * Set limit.
2611 	 */
2612 	if (req->newptr != NULL) {
2613 		error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2614 		if (error == 0)
2615 			error = kern_proc_setrlimit(curthread, p, which, &rlim);
2616 	}
2617 
2618 errout:
2619 	PRELE(p);
2620 	return (error);
2621 }
2622 
2623 /*
2624  * This sysctl allows a process to retrieve ps_strings structure location of
2625  * another process.
2626  */
2627 static int
2628 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2629 {
2630 	int *name = (int *)arg1;
2631 	u_int namelen = arg2;
2632 	struct proc *p;
2633 	vm_offset_t ps_strings;
2634 	int error;
2635 #ifdef COMPAT_FREEBSD32
2636 	uint32_t ps_strings32;
2637 #endif
2638 
2639 	if (namelen != 1)
2640 		return (EINVAL);
2641 
2642 	error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2643 	if (error != 0)
2644 		return (error);
2645 #ifdef COMPAT_FREEBSD32
2646 	if ((req->flags & SCTL_MASK32) != 0) {
2647 		/*
2648 		 * We return 0 if the 32 bit emulation request is for a 64 bit
2649 		 * process.
2650 		 */
2651 		ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2652 		    PTROUT(p->p_sysent->sv_psstrings) : 0;
2653 		PROC_UNLOCK(p);
2654 		error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2655 		return (error);
2656 	}
2657 #endif
2658 	ps_strings = p->p_sysent->sv_psstrings;
2659 	PROC_UNLOCK(p);
2660 	error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2661 	return (error);
2662 }
2663 
2664 /*
2665  * This sysctl allows a process to retrieve umask of another process.
2666  */
2667 static int
2668 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2669 {
2670 	int *name = (int *)arg1;
2671 	u_int namelen = arg2;
2672 	struct proc *p;
2673 	int error;
2674 	u_short fd_cmask;
2675 
2676 	if (namelen != 1)
2677 		return (EINVAL);
2678 
2679 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2680 	if (error != 0)
2681 		return (error);
2682 
2683 	FILEDESC_SLOCK(p->p_fd);
2684 	fd_cmask = p->p_fd->fd_cmask;
2685 	FILEDESC_SUNLOCK(p->p_fd);
2686 	PRELE(p);
2687 	error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2688 	return (error);
2689 }
2690 
2691 /*
2692  * This sysctl allows a process to set and retrieve binary osreldate of
2693  * another process.
2694  */
2695 static int
2696 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2697 {
2698 	int *name = (int *)arg1;
2699 	u_int namelen = arg2;
2700 	struct proc *p;
2701 	int flags, error, osrel;
2702 
2703 	if (namelen != 1)
2704 		return (EINVAL);
2705 
2706 	if (req->newptr != NULL && req->newlen != sizeof(osrel))
2707 		return (EINVAL);
2708 
2709 	flags = PGET_HOLD | PGET_NOTWEXIT;
2710 	if (req->newptr != NULL)
2711 		flags |= PGET_CANDEBUG;
2712 	else
2713 		flags |= PGET_CANSEE;
2714 	error = pget((pid_t)name[0], flags, &p);
2715 	if (error != 0)
2716 		return (error);
2717 
2718 	error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2719 	if (error != 0)
2720 		goto errout;
2721 
2722 	if (req->newptr != NULL) {
2723 		error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2724 		if (error != 0)
2725 			goto errout;
2726 		if (osrel < 0) {
2727 			error = EINVAL;
2728 			goto errout;
2729 		}
2730 		p->p_osrel = osrel;
2731 	}
2732 errout:
2733 	PRELE(p);
2734 	return (error);
2735 }
2736 
2737 static int
2738 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2739 {
2740 	int *name = (int *)arg1;
2741 	u_int namelen = arg2;
2742 	struct proc *p;
2743 	struct kinfo_sigtramp kst;
2744 	const struct sysentvec *sv;
2745 	int error;
2746 #ifdef COMPAT_FREEBSD32
2747 	struct kinfo_sigtramp32 kst32;
2748 #endif
2749 
2750 	if (namelen != 1)
2751 		return (EINVAL);
2752 
2753 	error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2754 	if (error != 0)
2755 		return (error);
2756 	sv = p->p_sysent;
2757 #ifdef COMPAT_FREEBSD32
2758 	if ((req->flags & SCTL_MASK32) != 0) {
2759 		bzero(&kst32, sizeof(kst32));
2760 		if (SV_PROC_FLAG(p, SV_ILP32)) {
2761 			if (sv->sv_sigcode_base != 0) {
2762 				kst32.ksigtramp_start = sv->sv_sigcode_base;
2763 				kst32.ksigtramp_end = sv->sv_sigcode_base +
2764 				    *sv->sv_szsigcode;
2765 			} else {
2766 				kst32.ksigtramp_start = sv->sv_psstrings -
2767 				    *sv->sv_szsigcode;
2768 				kst32.ksigtramp_end = sv->sv_psstrings;
2769 			}
2770 		}
2771 		PROC_UNLOCK(p);
2772 		error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2773 		return (error);
2774 	}
2775 #endif
2776 	bzero(&kst, sizeof(kst));
2777 	if (sv->sv_sigcode_base != 0) {
2778 		kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2779 		kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2780 		    *sv->sv_szsigcode;
2781 	} else {
2782 		kst.ksigtramp_start = (char *)sv->sv_psstrings -
2783 		    *sv->sv_szsigcode;
2784 		kst.ksigtramp_end = (char *)sv->sv_psstrings;
2785 	}
2786 	PROC_UNLOCK(p);
2787 	error = SYSCTL_OUT(req, &kst, sizeof(kst));
2788 	return (error);
2789 }
2790 
2791 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
2792 
2793 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2794 	CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2795 	"Return entire process table");
2796 
2797 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2798 	sysctl_kern_proc, "Process table");
2799 
2800 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2801 	sysctl_kern_proc, "Process table");
2802 
2803 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2804 	sysctl_kern_proc, "Process table");
2805 
2806 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2807 	CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2808 
2809 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2810 	sysctl_kern_proc, "Process table");
2811 
2812 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2813 	sysctl_kern_proc, "Process table");
2814 
2815 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2816 	sysctl_kern_proc, "Process table");
2817 
2818 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2819 	sysctl_kern_proc, "Process table");
2820 
2821 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2822 	sysctl_kern_proc, "Return process table, no threads");
2823 
2824 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2825 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2826 	sysctl_kern_proc_args, "Process argument list");
2827 
2828 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2829 	sysctl_kern_proc_env, "Process environment");
2830 
2831 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2832 	CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2833 
2834 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2835 	CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2836 
2837 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2838 	CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2839 	"Process syscall vector name (ABI type)");
2840 
2841 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2842 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2843 
2844 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2845 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2846 
2847 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2848 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2849 
2850 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2851 	sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2852 
2853 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2854 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2855 
2856 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2857 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2858 
2859 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2860 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2861 
2862 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2863 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2864 
2865 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2866 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2867 	"Return process table, no threads");
2868 
2869 #ifdef COMPAT_FREEBSD7
2870 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2871 	CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2872 #endif
2873 
2874 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2875 	CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2876 
2877 #if defined(STACK) || defined(DDB)
2878 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2879 	CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2880 #endif
2881 
2882 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2883 	CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2884 
2885 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2886 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2887 	"Process resource limits");
2888 
2889 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2890 	CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2891 	"Process ps_strings location");
2892 
2893 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2894 	CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2895 
2896 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2897 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2898 	"Process binary osreldate");
2899 
2900 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2901 	CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2902 	"Process signal trampoline location");
2903 
2904 int allproc_gen;
2905 
2906 void
2907 stop_all_proc(void)
2908 {
2909 	struct proc *cp, *p;
2910 	int r, gen;
2911 	bool restart, seen_stopped, seen_exiting, stopped_some;
2912 
2913 	cp = curproc;
2914 	/*
2915 	 * stop_all_proc() assumes that all process which have
2916 	 * usermode must be stopped, except current process, for
2917 	 * obvious reasons.  Since other threads in the process
2918 	 * establishing global stop could unstop something, disable
2919 	 * calls from multithreaded processes as precaution.  The
2920 	 * service must not be user-callable anyway.
2921 	 */
2922 	KASSERT((cp->p_flag & P_HADTHREADS) == 0 ||
2923 	    (cp->p_flag & P_KTHREAD) != 0, ("mt stop_all_proc"));
2924 
2925 allproc_loop:
2926 	sx_xlock(&allproc_lock);
2927 	gen = allproc_gen;
2928 	seen_exiting = seen_stopped = stopped_some = restart = false;
2929 	LIST_REMOVE(cp, p_list);
2930 	LIST_INSERT_HEAD(&allproc, cp, p_list);
2931 	for (;;) {
2932 		p = LIST_NEXT(cp, p_list);
2933 		if (p == NULL)
2934 			break;
2935 		LIST_REMOVE(cp, p_list);
2936 		LIST_INSERT_AFTER(p, cp, p_list);
2937 		PROC_LOCK(p);
2938 		if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
2939 		    P_TOTAL_STOP)) != 0) {
2940 			PROC_UNLOCK(p);
2941 			continue;
2942 		}
2943 		if ((p->p_flag & P_WEXIT) != 0) {
2944 			seen_exiting = true;
2945 			PROC_UNLOCK(p);
2946 			continue;
2947 		}
2948 		if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
2949 			/*
2950 			 * Stopped processes are tolerated when there
2951 			 * are no other processes which might continue
2952 			 * them.  P_STOPPED_SINGLE but not
2953 			 * P_TOTAL_STOP process still has at least one
2954 			 * thread running.
2955 			 */
2956 			seen_stopped = true;
2957 			PROC_UNLOCK(p);
2958 			continue;
2959 		}
2960 		_PHOLD(p);
2961 		sx_xunlock(&allproc_lock);
2962 		r = thread_single(p, SINGLE_ALLPROC);
2963 		if (r != 0)
2964 			restart = true;
2965 		else
2966 			stopped_some = true;
2967 		_PRELE(p);
2968 		PROC_UNLOCK(p);
2969 		sx_xlock(&allproc_lock);
2970 	}
2971 	/* Catch forked children we did not see in iteration. */
2972 	if (gen != allproc_gen)
2973 		restart = true;
2974 	sx_xunlock(&allproc_lock);
2975 	if (restart || stopped_some || seen_exiting || seen_stopped) {
2976 		kern_yield(PRI_USER);
2977 		goto allproc_loop;
2978 	}
2979 }
2980 
2981 void
2982 resume_all_proc(void)
2983 {
2984 	struct proc *cp, *p;
2985 
2986 	cp = curproc;
2987 	sx_xlock(&allproc_lock);
2988 	LIST_REMOVE(cp, p_list);
2989 	LIST_INSERT_HEAD(&allproc, cp, p_list);
2990 	for (;;) {
2991 		p = LIST_NEXT(cp, p_list);
2992 		if (p == NULL)
2993 			break;
2994 		LIST_REMOVE(cp, p_list);
2995 		LIST_INSERT_AFTER(p, cp, p_list);
2996 		PROC_LOCK(p);
2997 		if ((p->p_flag & P_TOTAL_STOP) != 0) {
2998 			sx_xunlock(&allproc_lock);
2999 			_PHOLD(p);
3000 			thread_single_end(p, SINGLE_ALLPROC);
3001 			_PRELE(p);
3002 			PROC_UNLOCK(p);
3003 			sx_xlock(&allproc_lock);
3004 		} else {
3005 			PROC_UNLOCK(p);
3006 		}
3007 	}
3008 	sx_xunlock(&allproc_lock);
3009 }
3010 
3011 #define	TOTAL_STOP_DEBUG	1
3012 #ifdef TOTAL_STOP_DEBUG
3013 volatile static int ap_resume;
3014 #include <sys/mount.h>
3015 
3016 static int
3017 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3018 {
3019 	int error, val;
3020 
3021 	val = 0;
3022 	ap_resume = 0;
3023 	error = sysctl_handle_int(oidp, &val, 0, req);
3024 	if (error != 0 || req->newptr == NULL)
3025 		return (error);
3026 	if (val != 0) {
3027 		stop_all_proc();
3028 		syncer_suspend();
3029 		while (ap_resume == 0)
3030 			;
3031 		syncer_resume();
3032 		resume_all_proc();
3033 	}
3034 	return (0);
3035 }
3036 
3037 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3038     CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3039     sysctl_debug_stop_all_proc, "I",
3040     "");
3041 #endif
3042