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