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