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