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