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