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