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