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