xref: /freebsd/sys/kern/kern_proc.c (revision 3e65b9c6e6b7b2081d54e1dc40983c3c00eaf738)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 #include "opt_ddb.h"
37 #include "opt_kdtrace.h"
38 #include "opt_ktrace.h"
39 #include "opt_kstack_pages.h"
40 #include "opt_stack.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/elf.h>
45 #include <sys/exec.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/lock.h>
49 #include <sys/loginclass.h>
50 #include <sys/malloc.h>
51 #include <sys/mman.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/ptrace.h>
56 #include <sys/refcount.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sbuf.h>
59 #include <sys/sysent.h>
60 #include <sys/sched.h>
61 #include <sys/smp.h>
62 #include <sys/stack.h>
63 #include <sys/sysctl.h>
64 #include <sys/filedesc.h>
65 #include <sys/tty.h>
66 #include <sys/signalvar.h>
67 #include <sys/sdt.h>
68 #include <sys/sx.h>
69 #include <sys/user.h>
70 #include <sys/jail.h>
71 #include <sys/vnode.h>
72 #include <sys/eventhandler.h>
73 
74 #ifdef DDB
75 #include <ddb/ddb.h>
76 #endif
77 
78 #include <vm/vm.h>
79 #include <vm/vm_extern.h>
80 #include <vm/pmap.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/uma.h>
85 
86 #ifdef COMPAT_FREEBSD32
87 #include <compat/freebsd32/freebsd32.h>
88 #include <compat/freebsd32/freebsd32_util.h>
89 #endif
90 
91 SDT_PROVIDER_DEFINE(proc);
92 SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry);
93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
94 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
95 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
96 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
97 SDT_PROBE_DEFINE(proc, kernel, ctor, return, return);
98 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
99 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
100 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
101 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
102 SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry);
103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
104 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
105 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
106 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
107 SDT_PROBE_DEFINE(proc, kernel, dtor, return, return);
108 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
109 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
110 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
111 SDT_PROBE_DEFINE(proc, kernel, init, entry, entry);
112 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
113 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
114 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
115 SDT_PROBE_DEFINE(proc, kernel, init, return, return);
116 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
117 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
118 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
119 
120 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
121 MALLOC_DEFINE(M_SESSION, "session", "session header");
122 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
123 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
124 
125 static void doenterpgrp(struct proc *, struct pgrp *);
126 static void orphanpg(struct pgrp *pg);
127 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
128 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
129 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
130     int preferthread);
131 static void pgadjustjobc(struct pgrp *pgrp, int entering);
132 static void pgdelete(struct pgrp *);
133 static int proc_ctor(void *mem, int size, void *arg, int flags);
134 static void proc_dtor(void *mem, int size, void *arg);
135 static int proc_init(void *mem, int size, int flags);
136 static void proc_fini(void *mem, int size);
137 static void pargs_free(struct pargs *pa);
138 
139 /*
140  * Other process lists
141  */
142 struct pidhashhead *pidhashtbl;
143 u_long pidhash;
144 struct pgrphashhead *pgrphashtbl;
145 u_long pgrphash;
146 struct proclist allproc;
147 struct proclist zombproc;
148 struct sx allproc_lock;
149 struct sx proctree_lock;
150 struct mtx ppeers_lock;
151 uma_zone_t proc_zone;
152 
153 int kstack_pages = KSTACK_PAGES;
154 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
155     "Kernel stack size in pages");
156 
157 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
158 #ifdef COMPAT_FREEBSD32
159 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
160 #endif
161 
162 /*
163  * Initialize global process hashing structures.
164  */
165 void
166 procinit()
167 {
168 
169 	sx_init(&allproc_lock, "allproc");
170 	sx_init(&proctree_lock, "proctree");
171 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
172 	LIST_INIT(&allproc);
173 	LIST_INIT(&zombproc);
174 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
175 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
176 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
177 	    proc_ctor, proc_dtor, proc_init, proc_fini,
178 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
179 	uihashinit();
180 }
181 
182 /*
183  * Prepare a proc for use.
184  */
185 static int
186 proc_ctor(void *mem, int size, void *arg, int flags)
187 {
188 	struct proc *p;
189 
190 	p = (struct proc *)mem;
191 	SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
192 	EVENTHANDLER_INVOKE(process_ctor, p);
193 	SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
194 	return (0);
195 }
196 
197 /*
198  * Reclaim a proc after use.
199  */
200 static void
201 proc_dtor(void *mem, int size, void *arg)
202 {
203 	struct proc *p;
204 	struct thread *td;
205 
206 	/* INVARIANTS checks go here */
207 	p = (struct proc *)mem;
208 	td = FIRST_THREAD_IN_PROC(p);
209 	SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
210 	if (td != NULL) {
211 #ifdef INVARIANTS
212 		KASSERT((p->p_numthreads == 1),
213 		    ("bad number of threads in exiting process"));
214 		KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
215 #endif
216 		/* Free all OSD associated to this thread. */
217 		osd_thread_exit(td);
218 	}
219 	EVENTHANDLER_INVOKE(process_dtor, p);
220 	if (p->p_ksi != NULL)
221 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
222 	SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
223 }
224 
225 /*
226  * Initialize type-stable parts of a proc (when newly created).
227  */
228 static int
229 proc_init(void *mem, int size, int flags)
230 {
231 	struct proc *p;
232 
233 	p = (struct proc *)mem;
234 	SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
235 	p->p_sched = (struct p_sched *)&p[1];
236 	bzero(&p->p_mtx, sizeof(struct mtx));
237 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
238 	mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
239 	cv_init(&p->p_pwait, "ppwait");
240 	cv_init(&p->p_dbgwait, "dbgwait");
241 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
242 	EVENTHANDLER_INVOKE(process_init, p);
243 	p->p_stats = pstats_alloc();
244 	SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
245 	return (0);
246 }
247 
248 /*
249  * UMA should ensure that this function is never called.
250  * Freeing a proc structure would violate type stability.
251  */
252 static void
253 proc_fini(void *mem, int size)
254 {
255 #ifdef notnow
256 	struct proc *p;
257 
258 	p = (struct proc *)mem;
259 	EVENTHANDLER_INVOKE(process_fini, p);
260 	pstats_free(p->p_stats);
261 	thread_free(FIRST_THREAD_IN_PROC(p));
262 	mtx_destroy(&p->p_mtx);
263 	if (p->p_ksi != NULL)
264 		ksiginfo_free(p->p_ksi);
265 #else
266 	panic("proc reclaimed");
267 #endif
268 }
269 
270 /*
271  * Is p an inferior of the current process?
272  */
273 int
274 inferior(p)
275 	register struct proc *p;
276 {
277 
278 	sx_assert(&proctree_lock, SX_LOCKED);
279 	for (; p != curproc; p = p->p_pptr)
280 		if (p->p_pid == 0)
281 			return (0);
282 	return (1);
283 }
284 
285 /*
286  * Locate a process by number; return only "live" processes -- i.e., neither
287  * zombies nor newly born but incompletely initialized processes.  By not
288  * returning processes in the PRS_NEW state, we allow callers to avoid
289  * testing for that condition to avoid dereferencing p_ucred, et al.
290  */
291 struct proc *
292 pfind(pid)
293 	register pid_t pid;
294 {
295 	register struct proc *p;
296 
297 	sx_slock(&allproc_lock);
298 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
299 		if (p->p_pid == pid) {
300 			PROC_LOCK(p);
301 			if (p->p_state == PRS_NEW) {
302 				PROC_UNLOCK(p);
303 				p = NULL;
304 			}
305 			break;
306 		}
307 	sx_sunlock(&allproc_lock);
308 	return (p);
309 }
310 
311 /*
312  * Locate a process group by number.
313  * The caller must hold proctree_lock.
314  */
315 struct pgrp *
316 pgfind(pgid)
317 	register pid_t pgid;
318 {
319 	register struct pgrp *pgrp;
320 
321 	sx_assert(&proctree_lock, SX_LOCKED);
322 
323 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
324 		if (pgrp->pg_id == pgid) {
325 			PGRP_LOCK(pgrp);
326 			return (pgrp);
327 		}
328 	}
329 	return (NULL);
330 }
331 
332 /*
333  * Locate process and do additional manipulations, depending on flags.
334  */
335 int
336 pget(pid_t pid, int flags, struct proc **pp)
337 {
338 	struct proc *p;
339 	int error;
340 
341 	p = pfind(pid);
342 	if (p == NULL)
343 		return (ESRCH);
344 	if ((flags & PGET_CANSEE) != 0) {
345 		error = p_cansee(curthread, p);
346 		if (error != 0)
347 			goto errout;
348 	}
349 	if ((flags & PGET_CANDEBUG) != 0) {
350 		error = p_candebug(curthread, p);
351 		if (error != 0)
352 			goto errout;
353 	}
354 	if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
355 		error = EPERM;
356 		goto errout;
357 	}
358 	if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
359 		error = ESRCH;
360 		goto errout;
361 	}
362 	if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
363 		/*
364 		 * XXXRW: Not clear ESRCH is the right error during proc
365 		 * execve().
366 		 */
367 		error = ESRCH;
368 		goto errout;
369 	}
370 	if ((flags & PGET_HOLD) != 0) {
371 		_PHOLD(p);
372 		PROC_UNLOCK(p);
373 	}
374 	*pp = p;
375 	return (0);
376 errout:
377 	PROC_UNLOCK(p);
378 	return (error);
379 }
380 
381 /*
382  * Create a new process group.
383  * pgid must be equal to the pid of p.
384  * Begin a new session if required.
385  */
386 int
387 enterpgrp(p, pgid, pgrp, sess)
388 	register struct proc *p;
389 	pid_t pgid;
390 	struct pgrp *pgrp;
391 	struct session *sess;
392 {
393 	struct pgrp *pgrp2;
394 
395 	sx_assert(&proctree_lock, SX_XLOCKED);
396 
397 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
398 	KASSERT(p->p_pid == pgid,
399 	    ("enterpgrp: new pgrp and pid != pgid"));
400 
401 	pgrp2 = pgfind(pgid);
402 
403 	KASSERT(pgrp2 == NULL,
404 	    ("enterpgrp: pgrp with pgid exists"));
405 	KASSERT(!SESS_LEADER(p),
406 	    ("enterpgrp: session leader attempted setpgrp"));
407 
408 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
409 
410 	if (sess != NULL) {
411 		/*
412 		 * new session
413 		 */
414 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
415 		PROC_LOCK(p);
416 		p->p_flag &= ~P_CONTROLT;
417 		PROC_UNLOCK(p);
418 		PGRP_LOCK(pgrp);
419 		sess->s_leader = p;
420 		sess->s_sid = p->p_pid;
421 		refcount_init(&sess->s_count, 1);
422 		sess->s_ttyvp = NULL;
423 		sess->s_ttydp = NULL;
424 		sess->s_ttyp = NULL;
425 		bcopy(p->p_session->s_login, sess->s_login,
426 			    sizeof(sess->s_login));
427 		pgrp->pg_session = sess;
428 		KASSERT(p == curproc,
429 		    ("enterpgrp: mksession and p != curproc"));
430 	} else {
431 		pgrp->pg_session = p->p_session;
432 		sess_hold(pgrp->pg_session);
433 		PGRP_LOCK(pgrp);
434 	}
435 	pgrp->pg_id = pgid;
436 	LIST_INIT(&pgrp->pg_members);
437 
438 	/*
439 	 * As we have an exclusive lock of proctree_lock,
440 	 * this should not deadlock.
441 	 */
442 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
443 	pgrp->pg_jobc = 0;
444 	SLIST_INIT(&pgrp->pg_sigiolst);
445 	PGRP_UNLOCK(pgrp);
446 
447 	doenterpgrp(p, pgrp);
448 
449 	return (0);
450 }
451 
452 /*
453  * Move p to an existing process group
454  */
455 int
456 enterthispgrp(p, pgrp)
457 	register struct proc *p;
458 	struct pgrp *pgrp;
459 {
460 
461 	sx_assert(&proctree_lock, SX_XLOCKED);
462 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
463 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
464 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
465 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
466 	KASSERT(pgrp->pg_session == p->p_session,
467 		("%s: pgrp's session %p, p->p_session %p.\n",
468 		__func__,
469 		pgrp->pg_session,
470 		p->p_session));
471 	KASSERT(pgrp != p->p_pgrp,
472 		("%s: p belongs to pgrp.", __func__));
473 
474 	doenterpgrp(p, pgrp);
475 
476 	return (0);
477 }
478 
479 /*
480  * Move p to a process group
481  */
482 static void
483 doenterpgrp(p, pgrp)
484 	struct proc *p;
485 	struct pgrp *pgrp;
486 {
487 	struct pgrp *savepgrp;
488 
489 	sx_assert(&proctree_lock, SX_XLOCKED);
490 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
491 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
492 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
493 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
494 
495 	savepgrp = p->p_pgrp;
496 
497 	/*
498 	 * Adjust eligibility of affected pgrps to participate in job control.
499 	 * Increment eligibility counts before decrementing, otherwise we
500 	 * could reach 0 spuriously during the first call.
501 	 */
502 	fixjobc(p, pgrp, 1);
503 	fixjobc(p, p->p_pgrp, 0);
504 
505 	PGRP_LOCK(pgrp);
506 	PGRP_LOCK(savepgrp);
507 	PROC_LOCK(p);
508 	LIST_REMOVE(p, p_pglist);
509 	p->p_pgrp = pgrp;
510 	PROC_UNLOCK(p);
511 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
512 	PGRP_UNLOCK(savepgrp);
513 	PGRP_UNLOCK(pgrp);
514 	if (LIST_EMPTY(&savepgrp->pg_members))
515 		pgdelete(savepgrp);
516 }
517 
518 /*
519  * remove process from process group
520  */
521 int
522 leavepgrp(p)
523 	register struct proc *p;
524 {
525 	struct pgrp *savepgrp;
526 
527 	sx_assert(&proctree_lock, SX_XLOCKED);
528 	savepgrp = p->p_pgrp;
529 	PGRP_LOCK(savepgrp);
530 	PROC_LOCK(p);
531 	LIST_REMOVE(p, p_pglist);
532 	p->p_pgrp = NULL;
533 	PROC_UNLOCK(p);
534 	PGRP_UNLOCK(savepgrp);
535 	if (LIST_EMPTY(&savepgrp->pg_members))
536 		pgdelete(savepgrp);
537 	return (0);
538 }
539 
540 /*
541  * delete a process group
542  */
543 static void
544 pgdelete(pgrp)
545 	register struct pgrp *pgrp;
546 {
547 	struct session *savesess;
548 	struct tty *tp;
549 
550 	sx_assert(&proctree_lock, SX_XLOCKED);
551 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
552 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
553 
554 	/*
555 	 * Reset any sigio structures pointing to us as a result of
556 	 * F_SETOWN with our pgid.
557 	 */
558 	funsetownlst(&pgrp->pg_sigiolst);
559 
560 	PGRP_LOCK(pgrp);
561 	tp = pgrp->pg_session->s_ttyp;
562 	LIST_REMOVE(pgrp, pg_hash);
563 	savesess = pgrp->pg_session;
564 	PGRP_UNLOCK(pgrp);
565 
566 	/* Remove the reference to the pgrp before deallocating it. */
567 	if (tp != NULL) {
568 		tty_lock(tp);
569 		tty_rel_pgrp(tp, pgrp);
570 	}
571 
572 	mtx_destroy(&pgrp->pg_mtx);
573 	free(pgrp, M_PGRP);
574 	sess_release(savesess);
575 }
576 
577 static void
578 pgadjustjobc(pgrp, entering)
579 	struct pgrp *pgrp;
580 	int entering;
581 {
582 
583 	PGRP_LOCK(pgrp);
584 	if (entering)
585 		pgrp->pg_jobc++;
586 	else {
587 		--pgrp->pg_jobc;
588 		if (pgrp->pg_jobc == 0)
589 			orphanpg(pgrp);
590 	}
591 	PGRP_UNLOCK(pgrp);
592 }
593 
594 /*
595  * Adjust pgrp jobc counters when specified process changes process group.
596  * We count the number of processes in each process group that "qualify"
597  * the group for terminal job control (those with a parent in a different
598  * process group of the same session).  If that count reaches zero, the
599  * process group becomes orphaned.  Check both the specified process'
600  * process group and that of its children.
601  * entering == 0 => p is leaving specified group.
602  * entering == 1 => p is entering specified group.
603  */
604 void
605 fixjobc(p, pgrp, entering)
606 	register struct proc *p;
607 	register struct pgrp *pgrp;
608 	int entering;
609 {
610 	register struct pgrp *hispgrp;
611 	register struct session *mysession;
612 
613 	sx_assert(&proctree_lock, SX_LOCKED);
614 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
615 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
616 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
617 
618 	/*
619 	 * Check p's parent to see whether p qualifies its own process
620 	 * group; if so, adjust count for p's process group.
621 	 */
622 	mysession = pgrp->pg_session;
623 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
624 	    hispgrp->pg_session == mysession)
625 		pgadjustjobc(pgrp, entering);
626 
627 	/*
628 	 * Check this process' children to see whether they qualify
629 	 * their process groups; if so, adjust counts for children's
630 	 * process groups.
631 	 */
632 	LIST_FOREACH(p, &p->p_children, p_sibling) {
633 		hispgrp = p->p_pgrp;
634 		if (hispgrp == pgrp ||
635 		    hispgrp->pg_session != mysession)
636 			continue;
637 		PROC_LOCK(p);
638 		if (p->p_state == PRS_ZOMBIE) {
639 			PROC_UNLOCK(p);
640 			continue;
641 		}
642 		PROC_UNLOCK(p);
643 		pgadjustjobc(hispgrp, entering);
644 	}
645 }
646 
647 /*
648  * A process group has become orphaned;
649  * if there are any stopped processes in the group,
650  * hang-up all process in that group.
651  */
652 static void
653 orphanpg(pg)
654 	struct pgrp *pg;
655 {
656 	register struct proc *p;
657 
658 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
659 
660 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
661 		PROC_LOCK(p);
662 		if (P_SHOULDSTOP(p)) {
663 			PROC_UNLOCK(p);
664 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
665 				PROC_LOCK(p);
666 				kern_psignal(p, SIGHUP);
667 				kern_psignal(p, SIGCONT);
668 				PROC_UNLOCK(p);
669 			}
670 			return;
671 		}
672 		PROC_UNLOCK(p);
673 	}
674 }
675 
676 void
677 sess_hold(struct session *s)
678 {
679 
680 	refcount_acquire(&s->s_count);
681 }
682 
683 void
684 sess_release(struct session *s)
685 {
686 
687 	if (refcount_release(&s->s_count)) {
688 		if (s->s_ttyp != NULL) {
689 			tty_lock(s->s_ttyp);
690 			tty_rel_sess(s->s_ttyp, s);
691 		}
692 		mtx_destroy(&s->s_mtx);
693 		free(s, M_SESSION);
694 	}
695 }
696 
697 #include "opt_ddb.h"
698 #ifdef DDB
699 #include <ddb/ddb.h>
700 
701 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
702 {
703 	register struct pgrp *pgrp;
704 	register struct proc *p;
705 	register int i;
706 
707 	for (i = 0; i <= pgrphash; i++) {
708 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
709 			printf("\tindx %d\n", i);
710 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
711 				printf(
712 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
713 				    (void *)pgrp, (long)pgrp->pg_id,
714 				    (void *)pgrp->pg_session,
715 				    pgrp->pg_session->s_count,
716 				    (void *)LIST_FIRST(&pgrp->pg_members));
717 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
718 					printf("\t\tpid %ld addr %p pgrp %p\n",
719 					    (long)p->p_pid, (void *)p,
720 					    (void *)p->p_pgrp);
721 				}
722 			}
723 		}
724 	}
725 }
726 #endif /* DDB */
727 
728 /*
729  * Calculate the kinfo_proc members which contain process-wide
730  * informations.
731  * Must be called with the target process locked.
732  */
733 static void
734 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
735 {
736 	struct thread *td;
737 
738 	PROC_LOCK_ASSERT(p, MA_OWNED);
739 
740 	kp->ki_estcpu = 0;
741 	kp->ki_pctcpu = 0;
742 	FOREACH_THREAD_IN_PROC(p, td) {
743 		thread_lock(td);
744 		kp->ki_pctcpu += sched_pctcpu(td);
745 		kp->ki_estcpu += td->td_estcpu;
746 		thread_unlock(td);
747 	}
748 }
749 
750 /*
751  * Clear kinfo_proc and fill in any information that is common
752  * to all threads in the process.
753  * Must be called with the target process locked.
754  */
755 static void
756 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
757 {
758 	struct thread *td0;
759 	struct tty *tp;
760 	struct session *sp;
761 	struct ucred *cred;
762 	struct sigacts *ps;
763 
764 	PROC_LOCK_ASSERT(p, MA_OWNED);
765 	bzero(kp, sizeof(*kp));
766 
767 	kp->ki_structsize = sizeof(*kp);
768 	kp->ki_paddr = p;
769 	kp->ki_addr =/* p->p_addr; */0; /* XXX */
770 	kp->ki_args = p->p_args;
771 	kp->ki_textvp = p->p_textvp;
772 #ifdef KTRACE
773 	kp->ki_tracep = p->p_tracevp;
774 	kp->ki_traceflag = p->p_traceflag;
775 #endif
776 	kp->ki_fd = p->p_fd;
777 	kp->ki_vmspace = p->p_vmspace;
778 	kp->ki_flag = p->p_flag;
779 	cred = p->p_ucred;
780 	if (cred) {
781 		kp->ki_uid = cred->cr_uid;
782 		kp->ki_ruid = cred->cr_ruid;
783 		kp->ki_svuid = cred->cr_svuid;
784 		kp->ki_cr_flags = 0;
785 		if (cred->cr_flags & CRED_FLAG_CAPMODE)
786 			kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
787 		/* XXX bde doesn't like KI_NGROUPS */
788 		if (cred->cr_ngroups > KI_NGROUPS) {
789 			kp->ki_ngroups = KI_NGROUPS;
790 			kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
791 		} else
792 			kp->ki_ngroups = cred->cr_ngroups;
793 		bcopy(cred->cr_groups, kp->ki_groups,
794 		    kp->ki_ngroups * sizeof(gid_t));
795 		kp->ki_rgid = cred->cr_rgid;
796 		kp->ki_svgid = cred->cr_svgid;
797 		/* If jailed(cred), emulate the old P_JAILED flag. */
798 		if (jailed(cred)) {
799 			kp->ki_flag |= P_JAILED;
800 			/* If inside the jail, use 0 as a jail ID. */
801 			if (cred->cr_prison != curthread->td_ucred->cr_prison)
802 				kp->ki_jid = cred->cr_prison->pr_id;
803 		}
804 		strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
805 		    sizeof(kp->ki_loginclass));
806 	}
807 	ps = p->p_sigacts;
808 	if (ps) {
809 		mtx_lock(&ps->ps_mtx);
810 		kp->ki_sigignore = ps->ps_sigignore;
811 		kp->ki_sigcatch = ps->ps_sigcatch;
812 		mtx_unlock(&ps->ps_mtx);
813 	}
814 	if (p->p_state != PRS_NEW &&
815 	    p->p_state != PRS_ZOMBIE &&
816 	    p->p_vmspace != NULL) {
817 		struct vmspace *vm = p->p_vmspace;
818 
819 		kp->ki_size = vm->vm_map.size;
820 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
821 		FOREACH_THREAD_IN_PROC(p, td0) {
822 			if (!TD_IS_SWAPPED(td0))
823 				kp->ki_rssize += td0->td_kstack_pages;
824 		}
825 		kp->ki_swrss = vm->vm_swrss;
826 		kp->ki_tsize = vm->vm_tsize;
827 		kp->ki_dsize = vm->vm_dsize;
828 		kp->ki_ssize = vm->vm_ssize;
829 	} else if (p->p_state == PRS_ZOMBIE)
830 		kp->ki_stat = SZOMB;
831 	if (kp->ki_flag & P_INMEM)
832 		kp->ki_sflag = PS_INMEM;
833 	else
834 		kp->ki_sflag = 0;
835 	/* Calculate legacy swtime as seconds since 'swtick'. */
836 	kp->ki_swtime = (ticks - p->p_swtick) / hz;
837 	kp->ki_pid = p->p_pid;
838 	kp->ki_nice = p->p_nice;
839 	kp->ki_start = p->p_stats->p_start;
840 	timevaladd(&kp->ki_start, &boottime);
841 	PROC_SLOCK(p);
842 	rufetch(p, &kp->ki_rusage);
843 	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
844 	calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
845 	PROC_SUNLOCK(p);
846 	calccru(p, &kp->ki_childutime, &kp->ki_childstime);
847 	/* Some callers want child times in a single value. */
848 	kp->ki_childtime = kp->ki_childstime;
849 	timevaladd(&kp->ki_childtime, &kp->ki_childutime);
850 
851 	tp = NULL;
852 	if (p->p_pgrp) {
853 		kp->ki_pgid = p->p_pgrp->pg_id;
854 		kp->ki_jobc = p->p_pgrp->pg_jobc;
855 		sp = p->p_pgrp->pg_session;
856 
857 		if (sp != NULL) {
858 			kp->ki_sid = sp->s_sid;
859 			SESS_LOCK(sp);
860 			strlcpy(kp->ki_login, sp->s_login,
861 			    sizeof(kp->ki_login));
862 			if (sp->s_ttyvp)
863 				kp->ki_kiflag |= KI_CTTY;
864 			if (SESS_LEADER(p))
865 				kp->ki_kiflag |= KI_SLEADER;
866 			/* XXX proctree_lock */
867 			tp = sp->s_ttyp;
868 			SESS_UNLOCK(sp);
869 		}
870 	}
871 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
872 		kp->ki_tdev = tty_udev(tp);
873 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
874 		if (tp->t_session)
875 			kp->ki_tsid = tp->t_session->s_sid;
876 	} else
877 		kp->ki_tdev = NODEV;
878 	if (p->p_comm[0] != '\0')
879 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
880 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
881 	    p->p_sysent->sv_name[0] != '\0')
882 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
883 	kp->ki_siglist = p->p_siglist;
884 	kp->ki_xstat = p->p_xstat;
885 	kp->ki_acflag = p->p_acflag;
886 	kp->ki_lock = p->p_lock;
887 	if (p->p_pptr)
888 		kp->ki_ppid = p->p_pptr->p_pid;
889 }
890 
891 /*
892  * Fill in information that is thread specific.  Must be called with
893  * target process locked.  If 'preferthread' is set, overwrite certain
894  * process-related fields that are maintained for both threads and
895  * processes.
896  */
897 static void
898 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
899 {
900 	struct proc *p;
901 
902 	p = td->td_proc;
903 	kp->ki_tdaddr = td;
904 	PROC_LOCK_ASSERT(p, MA_OWNED);
905 
906 	if (preferthread)
907 		PROC_SLOCK(p);
908 	thread_lock(td);
909 	if (td->td_wmesg != NULL)
910 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
911 	else
912 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
913 	strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
914 	if (TD_ON_LOCK(td)) {
915 		kp->ki_kiflag |= KI_LOCKBLOCK;
916 		strlcpy(kp->ki_lockname, td->td_lockname,
917 		    sizeof(kp->ki_lockname));
918 	} else {
919 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
920 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
921 	}
922 
923 	if (p->p_state == PRS_NORMAL) { /* approximate. */
924 		if (TD_ON_RUNQ(td) ||
925 		    TD_CAN_RUN(td) ||
926 		    TD_IS_RUNNING(td)) {
927 			kp->ki_stat = SRUN;
928 		} else if (P_SHOULDSTOP(p)) {
929 			kp->ki_stat = SSTOP;
930 		} else if (TD_IS_SLEEPING(td)) {
931 			kp->ki_stat = SSLEEP;
932 		} else if (TD_ON_LOCK(td)) {
933 			kp->ki_stat = SLOCK;
934 		} else {
935 			kp->ki_stat = SWAIT;
936 		}
937 	} else if (p->p_state == PRS_ZOMBIE) {
938 		kp->ki_stat = SZOMB;
939 	} else {
940 		kp->ki_stat = SIDL;
941 	}
942 
943 	/* Things in the thread */
944 	kp->ki_wchan = td->td_wchan;
945 	kp->ki_pri.pri_level = td->td_priority;
946 	kp->ki_pri.pri_native = td->td_base_pri;
947 	kp->ki_lastcpu = td->td_lastcpu;
948 	kp->ki_oncpu = td->td_oncpu;
949 	kp->ki_tdflags = td->td_flags;
950 	kp->ki_tid = td->td_tid;
951 	kp->ki_numthreads = p->p_numthreads;
952 	kp->ki_pcb = td->td_pcb;
953 	kp->ki_kstack = (void *)td->td_kstack;
954 	kp->ki_slptime = (ticks - td->td_slptick) / hz;
955 	kp->ki_pri.pri_class = td->td_pri_class;
956 	kp->ki_pri.pri_user = td->td_user_pri;
957 
958 	if (preferthread) {
959 		rufetchtd(td, &kp->ki_rusage);
960 		kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
961 		kp->ki_pctcpu = sched_pctcpu(td);
962 		kp->ki_estcpu = td->td_estcpu;
963 	}
964 
965 	/* We can't get this anymore but ps etc never used it anyway. */
966 	kp->ki_rqindex = 0;
967 
968 	if (preferthread)
969 		kp->ki_siglist = td->td_siglist;
970 	kp->ki_sigmask = td->td_sigmask;
971 	thread_unlock(td);
972 	if (preferthread)
973 		PROC_SUNLOCK(p);
974 }
975 
976 /*
977  * Fill in a kinfo_proc structure for the specified process.
978  * Must be called with the target process locked.
979  */
980 void
981 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
982 {
983 
984 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
985 
986 	fill_kinfo_proc_only(p, kp);
987 	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
988 	fill_kinfo_aggregate(p, kp);
989 }
990 
991 struct pstats *
992 pstats_alloc(void)
993 {
994 
995 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
996 }
997 
998 /*
999  * Copy parts of p_stats; zero the rest of p_stats (statistics).
1000  */
1001 void
1002 pstats_fork(struct pstats *src, struct pstats *dst)
1003 {
1004 
1005 	bzero(&dst->pstat_startzero,
1006 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1007 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1008 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1009 }
1010 
1011 void
1012 pstats_free(struct pstats *ps)
1013 {
1014 
1015 	free(ps, M_SUBPROC);
1016 }
1017 
1018 /*
1019  * Locate a zombie process by number
1020  */
1021 struct proc *
1022 zpfind(pid_t pid)
1023 {
1024 	struct proc *p;
1025 
1026 	sx_slock(&allproc_lock);
1027 	LIST_FOREACH(p, &zombproc, p_list)
1028 		if (p->p_pid == pid) {
1029 			PROC_LOCK(p);
1030 			break;
1031 		}
1032 	sx_sunlock(&allproc_lock);
1033 	return (p);
1034 }
1035 
1036 #define KERN_PROC_ZOMBMASK	0x3
1037 #define KERN_PROC_NOTHREADS	0x4
1038 
1039 #ifdef COMPAT_FREEBSD32
1040 
1041 /*
1042  * This function is typically used to copy out the kernel address, so
1043  * it can be replaced by assignment of zero.
1044  */
1045 static inline uint32_t
1046 ptr32_trim(void *ptr)
1047 {
1048 	uintptr_t uptr;
1049 
1050 	uptr = (uintptr_t)ptr;
1051 	return ((uptr > UINT_MAX) ? 0 : uptr);
1052 }
1053 
1054 #define PTRTRIM_CP(src,dst,fld) \
1055 	do { (dst).fld = ptr32_trim((src).fld); } while (0)
1056 
1057 static void
1058 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1059 {
1060 	int i;
1061 
1062 	bzero(ki32, sizeof(struct kinfo_proc32));
1063 	ki32->ki_structsize = sizeof(struct kinfo_proc32);
1064 	CP(*ki, *ki32, ki_layout);
1065 	PTRTRIM_CP(*ki, *ki32, ki_args);
1066 	PTRTRIM_CP(*ki, *ki32, ki_paddr);
1067 	PTRTRIM_CP(*ki, *ki32, ki_addr);
1068 	PTRTRIM_CP(*ki, *ki32, ki_tracep);
1069 	PTRTRIM_CP(*ki, *ki32, ki_textvp);
1070 	PTRTRIM_CP(*ki, *ki32, ki_fd);
1071 	PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1072 	PTRTRIM_CP(*ki, *ki32, ki_wchan);
1073 	CP(*ki, *ki32, ki_pid);
1074 	CP(*ki, *ki32, ki_ppid);
1075 	CP(*ki, *ki32, ki_pgid);
1076 	CP(*ki, *ki32, ki_tpgid);
1077 	CP(*ki, *ki32, ki_sid);
1078 	CP(*ki, *ki32, ki_tsid);
1079 	CP(*ki, *ki32, ki_jobc);
1080 	CP(*ki, *ki32, ki_tdev);
1081 	CP(*ki, *ki32, ki_siglist);
1082 	CP(*ki, *ki32, ki_sigmask);
1083 	CP(*ki, *ki32, ki_sigignore);
1084 	CP(*ki, *ki32, ki_sigcatch);
1085 	CP(*ki, *ki32, ki_uid);
1086 	CP(*ki, *ki32, ki_ruid);
1087 	CP(*ki, *ki32, ki_svuid);
1088 	CP(*ki, *ki32, ki_rgid);
1089 	CP(*ki, *ki32, ki_svgid);
1090 	CP(*ki, *ki32, ki_ngroups);
1091 	for (i = 0; i < KI_NGROUPS; i++)
1092 		CP(*ki, *ki32, ki_groups[i]);
1093 	CP(*ki, *ki32, ki_size);
1094 	CP(*ki, *ki32, ki_rssize);
1095 	CP(*ki, *ki32, ki_swrss);
1096 	CP(*ki, *ki32, ki_tsize);
1097 	CP(*ki, *ki32, ki_dsize);
1098 	CP(*ki, *ki32, ki_ssize);
1099 	CP(*ki, *ki32, ki_xstat);
1100 	CP(*ki, *ki32, ki_acflag);
1101 	CP(*ki, *ki32, ki_pctcpu);
1102 	CP(*ki, *ki32, ki_estcpu);
1103 	CP(*ki, *ki32, ki_slptime);
1104 	CP(*ki, *ki32, ki_swtime);
1105 	CP(*ki, *ki32, ki_runtime);
1106 	TV_CP(*ki, *ki32, ki_start);
1107 	TV_CP(*ki, *ki32, ki_childtime);
1108 	CP(*ki, *ki32, ki_flag);
1109 	CP(*ki, *ki32, ki_kiflag);
1110 	CP(*ki, *ki32, ki_traceflag);
1111 	CP(*ki, *ki32, ki_stat);
1112 	CP(*ki, *ki32, ki_nice);
1113 	CP(*ki, *ki32, ki_lock);
1114 	CP(*ki, *ki32, ki_rqindex);
1115 	CP(*ki, *ki32, ki_oncpu);
1116 	CP(*ki, *ki32, ki_lastcpu);
1117 	bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1118 	bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1119 	bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1120 	bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1121 	bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1122 	bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1123 	bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1124 	CP(*ki, *ki32, ki_cr_flags);
1125 	CP(*ki, *ki32, ki_jid);
1126 	CP(*ki, *ki32, ki_numthreads);
1127 	CP(*ki, *ki32, ki_tid);
1128 	CP(*ki, *ki32, ki_pri);
1129 	freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1130 	freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1131 	PTRTRIM_CP(*ki, *ki32, ki_pcb);
1132 	PTRTRIM_CP(*ki, *ki32, ki_kstack);
1133 	PTRTRIM_CP(*ki, *ki32, ki_udata);
1134 	CP(*ki, *ki32, ki_sflag);
1135 	CP(*ki, *ki32, ki_tdflags);
1136 }
1137 
1138 static int
1139 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1140 {
1141 	struct kinfo_proc32 ki32;
1142 	int error;
1143 
1144 	if (req->flags & SCTL_MASK32) {
1145 		freebsd32_kinfo_proc_out(ki, &ki32);
1146 		error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
1147 	} else
1148 		error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
1149 	return (error);
1150 }
1151 #else
1152 static int
1153 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1154 {
1155 
1156 	return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
1157 }
1158 #endif
1159 
1160 /*
1161  * Must be called with the process locked and will return with it unlocked.
1162  */
1163 static int
1164 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1165 {
1166 	struct thread *td;
1167 	struct kinfo_proc kinfo_proc;
1168 	int error = 0;
1169 	struct proc *np;
1170 	pid_t pid = p->p_pid;
1171 
1172 	PROC_LOCK_ASSERT(p, MA_OWNED);
1173 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1174 
1175 	fill_kinfo_proc(p, &kinfo_proc);
1176 	if (flags & KERN_PROC_NOTHREADS)
1177 		error = sysctl_out_proc_copyout(&kinfo_proc, req);
1178 	else {
1179 		FOREACH_THREAD_IN_PROC(p, td) {
1180 			fill_kinfo_thread(td, &kinfo_proc, 1);
1181 			error = sysctl_out_proc_copyout(&kinfo_proc, req);
1182 			if (error)
1183 				break;
1184 		}
1185 	}
1186 	PROC_UNLOCK(p);
1187 	if (error)
1188 		return (error);
1189 	if (flags & KERN_PROC_ZOMBMASK)
1190 		np = zpfind(pid);
1191 	else {
1192 		if (pid == 0)
1193 			return (0);
1194 		np = pfind(pid);
1195 	}
1196 	if (np == NULL)
1197 		return (ESRCH);
1198 	if (np != p) {
1199 		PROC_UNLOCK(np);
1200 		return (ESRCH);
1201 	}
1202 	PROC_UNLOCK(np);
1203 	return (0);
1204 }
1205 
1206 static int
1207 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1208 {
1209 	int *name = (int *)arg1;
1210 	u_int namelen = arg2;
1211 	struct proc *p;
1212 	int flags, doingzomb, oid_number;
1213 	int error = 0;
1214 
1215 	oid_number = oidp->oid_number;
1216 	if (oid_number != KERN_PROC_ALL &&
1217 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
1218 		flags = KERN_PROC_NOTHREADS;
1219 	else {
1220 		flags = 0;
1221 		oid_number &= ~KERN_PROC_INC_THREAD;
1222 	}
1223 	if (oid_number == KERN_PROC_PID) {
1224 		if (namelen != 1)
1225 			return (EINVAL);
1226 		error = sysctl_wire_old_buffer(req, 0);
1227 		if (error)
1228 			return (error);
1229 		error = pget((pid_t)name[0], PGET_CANSEE, &p);
1230 		if (error != 0)
1231 			return (error);
1232 		error = sysctl_out_proc(p, req, flags);
1233 		return (error);
1234 	}
1235 
1236 	switch (oid_number) {
1237 	case KERN_PROC_ALL:
1238 		if (namelen != 0)
1239 			return (EINVAL);
1240 		break;
1241 	case KERN_PROC_PROC:
1242 		if (namelen != 0 && namelen != 1)
1243 			return (EINVAL);
1244 		break;
1245 	default:
1246 		if (namelen != 1)
1247 			return (EINVAL);
1248 		break;
1249 	}
1250 
1251 	if (!req->oldptr) {
1252 		/* overestimate by 5 procs */
1253 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1254 		if (error)
1255 			return (error);
1256 	}
1257 	error = sysctl_wire_old_buffer(req, 0);
1258 	if (error != 0)
1259 		return (error);
1260 	sx_slock(&allproc_lock);
1261 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1262 		if (!doingzomb)
1263 			p = LIST_FIRST(&allproc);
1264 		else
1265 			p = LIST_FIRST(&zombproc);
1266 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1267 			/*
1268 			 * Skip embryonic processes.
1269 			 */
1270 			PROC_LOCK(p);
1271 			if (p->p_state == PRS_NEW) {
1272 				PROC_UNLOCK(p);
1273 				continue;
1274 			}
1275 			KASSERT(p->p_ucred != NULL,
1276 			    ("process credential is NULL for non-NEW proc"));
1277 			/*
1278 			 * Show a user only appropriate processes.
1279 			 */
1280 			if (p_cansee(curthread, p)) {
1281 				PROC_UNLOCK(p);
1282 				continue;
1283 			}
1284 			/*
1285 			 * TODO - make more efficient (see notes below).
1286 			 * do by session.
1287 			 */
1288 			switch (oid_number) {
1289 
1290 			case KERN_PROC_GID:
1291 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1292 					PROC_UNLOCK(p);
1293 					continue;
1294 				}
1295 				break;
1296 
1297 			case KERN_PROC_PGRP:
1298 				/* could do this by traversing pgrp */
1299 				if (p->p_pgrp == NULL ||
1300 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1301 					PROC_UNLOCK(p);
1302 					continue;
1303 				}
1304 				break;
1305 
1306 			case KERN_PROC_RGID:
1307 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1308 					PROC_UNLOCK(p);
1309 					continue;
1310 				}
1311 				break;
1312 
1313 			case KERN_PROC_SESSION:
1314 				if (p->p_session == NULL ||
1315 				    p->p_session->s_sid != (pid_t)name[0]) {
1316 					PROC_UNLOCK(p);
1317 					continue;
1318 				}
1319 				break;
1320 
1321 			case KERN_PROC_TTY:
1322 				if ((p->p_flag & P_CONTROLT) == 0 ||
1323 				    p->p_session == NULL) {
1324 					PROC_UNLOCK(p);
1325 					continue;
1326 				}
1327 				/* XXX proctree_lock */
1328 				SESS_LOCK(p->p_session);
1329 				if (p->p_session->s_ttyp == NULL ||
1330 				    tty_udev(p->p_session->s_ttyp) !=
1331 				    (dev_t)name[0]) {
1332 					SESS_UNLOCK(p->p_session);
1333 					PROC_UNLOCK(p);
1334 					continue;
1335 				}
1336 				SESS_UNLOCK(p->p_session);
1337 				break;
1338 
1339 			case KERN_PROC_UID:
1340 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1341 					PROC_UNLOCK(p);
1342 					continue;
1343 				}
1344 				break;
1345 
1346 			case KERN_PROC_RUID:
1347 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1348 					PROC_UNLOCK(p);
1349 					continue;
1350 				}
1351 				break;
1352 
1353 			case KERN_PROC_PROC:
1354 				break;
1355 
1356 			default:
1357 				break;
1358 
1359 			}
1360 
1361 			error = sysctl_out_proc(p, req, flags | doingzomb);
1362 			if (error) {
1363 				sx_sunlock(&allproc_lock);
1364 				return (error);
1365 			}
1366 		}
1367 	}
1368 	sx_sunlock(&allproc_lock);
1369 	return (0);
1370 }
1371 
1372 struct pargs *
1373 pargs_alloc(int len)
1374 {
1375 	struct pargs *pa;
1376 
1377 	pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1378 		M_WAITOK);
1379 	refcount_init(&pa->ar_ref, 1);
1380 	pa->ar_length = len;
1381 	return (pa);
1382 }
1383 
1384 static void
1385 pargs_free(struct pargs *pa)
1386 {
1387 
1388 	free(pa, M_PARGS);
1389 }
1390 
1391 void
1392 pargs_hold(struct pargs *pa)
1393 {
1394 
1395 	if (pa == NULL)
1396 		return;
1397 	refcount_acquire(&pa->ar_ref);
1398 }
1399 
1400 void
1401 pargs_drop(struct pargs *pa)
1402 {
1403 
1404 	if (pa == NULL)
1405 		return;
1406 	if (refcount_release(&pa->ar_ref))
1407 		pargs_free(pa);
1408 }
1409 
1410 static int
1411 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1412     size_t len)
1413 {
1414 	struct iovec iov;
1415 	struct uio uio;
1416 
1417 	iov.iov_base = (caddr_t)buf;
1418 	iov.iov_len = len;
1419 	uio.uio_iov = &iov;
1420 	uio.uio_iovcnt = 1;
1421 	uio.uio_offset = offset;
1422 	uio.uio_resid = (ssize_t)len;
1423 	uio.uio_segflg = UIO_SYSSPACE;
1424 	uio.uio_rw = UIO_READ;
1425 	uio.uio_td = td;
1426 
1427 	return (proc_rwmem(p, &uio));
1428 }
1429 
1430 static int
1431 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1432     size_t len)
1433 {
1434 	size_t i;
1435 	int error;
1436 
1437 	error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1438 	/*
1439 	 * Reading the chunk may validly return EFAULT if the string is shorter
1440 	 * than the chunk and is aligned at the end of the page, assuming the
1441 	 * next page is not mapped.  So if EFAULT is returned do a fallback to
1442 	 * one byte read loop.
1443 	 */
1444 	if (error == EFAULT) {
1445 		for (i = 0; i < len; i++, buf++, sptr++) {
1446 			error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1447 			if (error != 0)
1448 				return (error);
1449 			if (*buf == '\0')
1450 				break;
1451 		}
1452 		error = 0;
1453 	}
1454 	return (error);
1455 }
1456 
1457 #define PROC_AUXV_MAX	256	/* Safety limit on auxv size. */
1458 
1459 enum proc_vector_type {
1460 	PROC_ARG,
1461 	PROC_ENV,
1462 	PROC_AUX,
1463 };
1464 
1465 #ifdef COMPAT_FREEBSD32
1466 static int
1467 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1468     size_t *vsizep, enum proc_vector_type type)
1469 {
1470 	struct freebsd32_ps_strings pss;
1471 	Elf32_Auxinfo aux;
1472 	vm_offset_t vptr, ptr;
1473 	uint32_t *proc_vector32;
1474 	char **proc_vector;
1475 	size_t vsize, size;
1476 	int i, error;
1477 
1478 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1479 	    &pss, sizeof(pss));
1480 	if (error != 0)
1481 		return (error);
1482 	switch (type) {
1483 	case PROC_ARG:
1484 		vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1485 		vsize = pss.ps_nargvstr;
1486 		if (vsize > ARG_MAX)
1487 			return (ENOEXEC);
1488 		size = vsize * sizeof(int32_t);
1489 		break;
1490 	case PROC_ENV:
1491 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1492 		vsize = pss.ps_nenvstr;
1493 		if (vsize > ARG_MAX)
1494 			return (ENOEXEC);
1495 		size = vsize * sizeof(int32_t);
1496 		break;
1497 	case PROC_AUX:
1498 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1499 		    (pss.ps_nenvstr + 1) * sizeof(int32_t);
1500 		if (vptr % 4 != 0)
1501 			return (ENOEXEC);
1502 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1503 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1504 			if (error != 0)
1505 				return (error);
1506 			if (aux.a_type == AT_NULL)
1507 				break;
1508 			ptr += sizeof(aux);
1509 		}
1510 		if (aux.a_type != AT_NULL)
1511 			return (ENOEXEC);
1512 		vsize = i + 1;
1513 		size = vsize * sizeof(aux);
1514 		break;
1515 	default:
1516 		KASSERT(0, ("Wrong proc vector type: %d", type));
1517 		return (EINVAL);
1518 	}
1519 	proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1520 	error = proc_read_mem(td, p, vptr, proc_vector32, size);
1521 	if (error != 0)
1522 		goto done;
1523 	if (type == PROC_AUX) {
1524 		*proc_vectorp = (char **)proc_vector32;
1525 		*vsizep = vsize;
1526 		return (0);
1527 	}
1528 	proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1529 	for (i = 0; i < (int)vsize; i++)
1530 		proc_vector[i] = PTRIN(proc_vector32[i]);
1531 	*proc_vectorp = proc_vector;
1532 	*vsizep = vsize;
1533 done:
1534 	free(proc_vector32, M_TEMP);
1535 	return (error);
1536 }
1537 #endif
1538 
1539 static int
1540 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1541     size_t *vsizep, enum proc_vector_type type)
1542 {
1543 	struct ps_strings pss;
1544 	Elf_Auxinfo aux;
1545 	vm_offset_t vptr, ptr;
1546 	char **proc_vector;
1547 	size_t vsize, size;
1548 	int error, i;
1549 
1550 #ifdef COMPAT_FREEBSD32
1551 	if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1552 		return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1553 #endif
1554 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1555 	    &pss, sizeof(pss));
1556 	if (error != 0)
1557 		return (error);
1558 	switch (type) {
1559 	case PROC_ARG:
1560 		vptr = (vm_offset_t)pss.ps_argvstr;
1561 		vsize = pss.ps_nargvstr;
1562 		if (vsize > ARG_MAX)
1563 			return (ENOEXEC);
1564 		size = vsize * sizeof(char *);
1565 		break;
1566 	case PROC_ENV:
1567 		vptr = (vm_offset_t)pss.ps_envstr;
1568 		vsize = pss.ps_nenvstr;
1569 		if (vsize > ARG_MAX)
1570 			return (ENOEXEC);
1571 		size = vsize * sizeof(char *);
1572 		break;
1573 	case PROC_AUX:
1574 		/*
1575 		 * The aux array is just above env array on the stack. Check
1576 		 * that the address is naturally aligned.
1577 		 */
1578 		vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1579 		    * sizeof(char *);
1580 #if __ELF_WORD_SIZE == 64
1581 		if (vptr % sizeof(uint64_t) != 0)
1582 #else
1583 		if (vptr % sizeof(uint32_t) != 0)
1584 #endif
1585 			return (ENOEXEC);
1586 		/*
1587 		 * We count the array size reading the aux vectors from the
1588 		 * stack until AT_NULL vector is returned.  So (to keep the code
1589 		 * simple) we read the process stack twice: the first time here
1590 		 * to find the size and the second time when copying the vectors
1591 		 * to the allocated proc_vector.
1592 		 */
1593 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1594 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1595 			if (error != 0)
1596 				return (error);
1597 			if (aux.a_type == AT_NULL)
1598 				break;
1599 			ptr += sizeof(aux);
1600 		}
1601 		/*
1602 		 * If the PROC_AUXV_MAX entries are iterated over, and we have
1603 		 * not reached AT_NULL, it is most likely we are reading wrong
1604 		 * data: either the process doesn't have auxv array or data has
1605 		 * been modified. Return the error in this case.
1606 		 */
1607 		if (aux.a_type != AT_NULL)
1608 			return (ENOEXEC);
1609 		vsize = i + 1;
1610 		size = vsize * sizeof(aux);
1611 		break;
1612 	default:
1613 		KASSERT(0, ("Wrong proc vector type: %d", type));
1614 		return (EINVAL); /* In case we are built without INVARIANTS. */
1615 	}
1616 	proc_vector = malloc(size, M_TEMP, M_WAITOK);
1617 	if (proc_vector == NULL)
1618 		return (ENOMEM);
1619 	error = proc_read_mem(td, p, vptr, proc_vector, size);
1620 	if (error != 0) {
1621 		free(proc_vector, M_TEMP);
1622 		return (error);
1623 	}
1624 	*proc_vectorp = proc_vector;
1625 	*vsizep = vsize;
1626 
1627 	return (0);
1628 }
1629 
1630 #define GET_PS_STRINGS_CHUNK_SZ	256	/* Chunk size (bytes) for ps_strings operations. */
1631 
1632 static int
1633 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1634     enum proc_vector_type type, size_t nchr)
1635 {
1636 	size_t done, len, vsize;
1637 	int error, i;
1638 	char **proc_vector, *sptr;
1639 	char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1640 
1641 	PROC_ASSERT_HELD(p);
1642 
1643 	 /*
1644 	  * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1645 	  */
1646 	if (nchr > 2 * (PATH_MAX + ARG_MAX))
1647 		nchr = 2 * (PATH_MAX + ARG_MAX);
1648 
1649 	error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1650 	if (error != 0)
1651 		return (error);
1652 	for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1653 		/*
1654 		 * The program may have scribbled into its argv array, e.g. to
1655 		 * remove some arguments.  If that has happened, break out
1656 		 * before trying to read from NULL.
1657 		 */
1658 		if (proc_vector[i] == NULL)
1659 			break;
1660 		for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1661 			error = proc_read_string(td, p, sptr, pss_string,
1662 			    sizeof(pss_string));
1663 			if (error != 0)
1664 				goto done;
1665 			len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1666 			if (done + len >= nchr)
1667 				len = nchr - done - 1;
1668 			sbuf_bcat(sb, pss_string, len);
1669 			if (len != GET_PS_STRINGS_CHUNK_SZ)
1670 				break;
1671 			done += GET_PS_STRINGS_CHUNK_SZ;
1672 		}
1673 		sbuf_bcat(sb, "", 1);
1674 		done += len + 1;
1675 	}
1676 done:
1677 	free(proc_vector, M_TEMP);
1678 	return (error);
1679 }
1680 
1681 int
1682 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb, size_t nchr)
1683 {
1684 
1685 	return (get_ps_strings(curthread, p, sb, PROC_ARG, nchr));
1686 }
1687 
1688 int
1689 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb, size_t nchr)
1690 {
1691 
1692 	return (get_ps_strings(curthread, p, sb, PROC_ENV, nchr));
1693 }
1694 
1695 /*
1696  * This sysctl allows a process to retrieve the argument list or process
1697  * title for another process without groping around in the address space
1698  * of the other process.  It also allow a process to set its own "process
1699  * title to a string of its own choice.
1700  */
1701 static int
1702 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1703 {
1704 	int *name = (int *)arg1;
1705 	u_int namelen = arg2;
1706 	struct pargs *newpa, *pa;
1707 	struct proc *p;
1708 	struct sbuf sb;
1709 	int flags, error = 0, error2;
1710 
1711 	if (namelen != 1)
1712 		return (EINVAL);
1713 
1714 	flags = PGET_CANSEE;
1715 	if (req->newptr != NULL)
1716 		flags |= PGET_ISCURRENT;
1717 	error = pget((pid_t)name[0], flags, &p);
1718 	if (error)
1719 		return (error);
1720 
1721 	pa = p->p_args;
1722 	if (pa != NULL) {
1723 		pargs_hold(pa);
1724 		PROC_UNLOCK(p);
1725 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1726 		pargs_drop(pa);
1727 	} else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1728 		_PHOLD(p);
1729 		PROC_UNLOCK(p);
1730 		sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1731 		error = proc_getargv(curthread, p, &sb, req->oldlen);
1732 		error2 = sbuf_finish(&sb);
1733 		PRELE(p);
1734 		sbuf_delete(&sb);
1735 		if (error == 0 && error2 != 0)
1736 			error = error2;
1737 	} else {
1738 		PROC_UNLOCK(p);
1739 	}
1740 	if (error != 0 || req->newptr == NULL)
1741 		return (error);
1742 
1743 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1744 		return (ENOMEM);
1745 	newpa = pargs_alloc(req->newlen);
1746 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1747 	if (error != 0) {
1748 		pargs_free(newpa);
1749 		return (error);
1750 	}
1751 	PROC_LOCK(p);
1752 	pa = p->p_args;
1753 	p->p_args = newpa;
1754 	PROC_UNLOCK(p);
1755 	pargs_drop(pa);
1756 	return (0);
1757 }
1758 
1759 /*
1760  * This sysctl allows a process to retrieve environment of another process.
1761  */
1762 static int
1763 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1764 {
1765 	int *name = (int *)arg1;
1766 	u_int namelen = arg2;
1767 	struct proc *p;
1768 	struct sbuf sb;
1769 	int error, error2;
1770 
1771 	if (namelen != 1)
1772 		return (EINVAL);
1773 
1774 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1775 	if (error != 0)
1776 		return (error);
1777 	if ((p->p_flag & P_SYSTEM) != 0) {
1778 		PRELE(p);
1779 		return (0);
1780 	}
1781 
1782 	sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1783 	error = proc_getenvv(curthread, p, &sb, req->oldlen);
1784 	error2 = sbuf_finish(&sb);
1785 	PRELE(p);
1786 	sbuf_delete(&sb);
1787 	return (error != 0 ? error : error2);
1788 }
1789 
1790 /*
1791  * This sysctl allows a process to retrieve ELF auxiliary vector of
1792  * another process.
1793  */
1794 static int
1795 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1796 {
1797 	int *name = (int *)arg1;
1798 	u_int namelen = arg2;
1799 	struct proc *p;
1800 	size_t vsize, size;
1801 	char **auxv;
1802 	int error;
1803 
1804 	if (namelen != 1)
1805 		return (EINVAL);
1806 
1807 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1808 	if (error != 0)
1809 		return (error);
1810 	if ((p->p_flag & P_SYSTEM) != 0) {
1811 		PRELE(p);
1812 		return (0);
1813 	}
1814 	error = get_proc_vector(curthread, p, &auxv, &vsize, PROC_AUX);
1815 	if (error == 0) {
1816 #ifdef COMPAT_FREEBSD32
1817 		if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1818 			size = vsize * sizeof(Elf32_Auxinfo);
1819 		else
1820 #endif
1821 		size = vsize * sizeof(Elf_Auxinfo);
1822 		PRELE(p);
1823 		error = SYSCTL_OUT(req, auxv, size);
1824 		free(auxv, M_TEMP);
1825 	} else {
1826 		PRELE(p);
1827 	}
1828 	return (error);
1829 }
1830 
1831 /*
1832  * This sysctl allows a process to retrieve the path of the executable for
1833  * itself or another process.
1834  */
1835 static int
1836 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1837 {
1838 	pid_t *pidp = (pid_t *)arg1;
1839 	unsigned int arglen = arg2;
1840 	struct proc *p;
1841 	struct vnode *vp;
1842 	char *retbuf, *freebuf;
1843 	int error, vfslocked;
1844 
1845 	if (arglen != 1)
1846 		return (EINVAL);
1847 	if (*pidp == -1) {	/* -1 means this process */
1848 		p = req->td->td_proc;
1849 	} else {
1850 		error = pget(*pidp, PGET_CANSEE, &p);
1851 		if (error != 0)
1852 			return (error);
1853 	}
1854 
1855 	vp = p->p_textvp;
1856 	if (vp == NULL) {
1857 		if (*pidp != -1)
1858 			PROC_UNLOCK(p);
1859 		return (0);
1860 	}
1861 	vref(vp);
1862 	if (*pidp != -1)
1863 		PROC_UNLOCK(p);
1864 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1865 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1866 	vrele(vp);
1867 	VFS_UNLOCK_GIANT(vfslocked);
1868 	if (error)
1869 		return (error);
1870 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1871 	free(freebuf, M_TEMP);
1872 	return (error);
1873 }
1874 
1875 static int
1876 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1877 {
1878 	struct proc *p;
1879 	char *sv_name;
1880 	int *name;
1881 	int namelen;
1882 	int error;
1883 
1884 	namelen = arg2;
1885 	if (namelen != 1)
1886 		return (EINVAL);
1887 
1888 	name = (int *)arg1;
1889 	error = pget((pid_t)name[0], PGET_CANSEE, &p);
1890 	if (error != 0)
1891 		return (error);
1892 	sv_name = p->p_sysent->sv_name;
1893 	PROC_UNLOCK(p);
1894 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1895 }
1896 
1897 #ifdef KINFO_OVMENTRY_SIZE
1898 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1899 #endif
1900 
1901 #ifdef COMPAT_FREEBSD7
1902 static int
1903 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1904 {
1905 	vm_map_entry_t entry, tmp_entry;
1906 	unsigned int last_timestamp;
1907 	char *fullpath, *freepath;
1908 	struct kinfo_ovmentry *kve;
1909 	struct vattr va;
1910 	struct ucred *cred;
1911 	int error, *name;
1912 	struct vnode *vp;
1913 	struct proc *p;
1914 	vm_map_t map;
1915 	struct vmspace *vm;
1916 
1917 	name = (int *)arg1;
1918 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1919 	if (error != 0)
1920 		return (error);
1921 	vm = vmspace_acquire_ref(p);
1922 	if (vm == NULL) {
1923 		PRELE(p);
1924 		return (ESRCH);
1925 	}
1926 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1927 
1928 	map = &vm->vm_map;
1929 	vm_map_lock_read(map);
1930 	for (entry = map->header.next; entry != &map->header;
1931 	    entry = entry->next) {
1932 		vm_object_t obj, tobj, lobj;
1933 		vm_offset_t addr;
1934 		int vfslocked;
1935 
1936 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1937 			continue;
1938 
1939 		bzero(kve, sizeof(*kve));
1940 		kve->kve_structsize = sizeof(*kve);
1941 
1942 		kve->kve_private_resident = 0;
1943 		obj = entry->object.vm_object;
1944 		if (obj != NULL) {
1945 			VM_OBJECT_LOCK(obj);
1946 			if (obj->shadow_count == 1)
1947 				kve->kve_private_resident =
1948 				    obj->resident_page_count;
1949 		}
1950 		kve->kve_resident = 0;
1951 		addr = entry->start;
1952 		while (addr < entry->end) {
1953 			if (pmap_extract(map->pmap, addr))
1954 				kve->kve_resident++;
1955 			addr += PAGE_SIZE;
1956 		}
1957 
1958 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1959 			if (tobj != obj)
1960 				VM_OBJECT_LOCK(tobj);
1961 			if (lobj != obj)
1962 				VM_OBJECT_UNLOCK(lobj);
1963 			lobj = tobj;
1964 		}
1965 
1966 		kve->kve_start = (void*)entry->start;
1967 		kve->kve_end = (void*)entry->end;
1968 		kve->kve_offset = (off_t)entry->offset;
1969 
1970 		if (entry->protection & VM_PROT_READ)
1971 			kve->kve_protection |= KVME_PROT_READ;
1972 		if (entry->protection & VM_PROT_WRITE)
1973 			kve->kve_protection |= KVME_PROT_WRITE;
1974 		if (entry->protection & VM_PROT_EXECUTE)
1975 			kve->kve_protection |= KVME_PROT_EXEC;
1976 
1977 		if (entry->eflags & MAP_ENTRY_COW)
1978 			kve->kve_flags |= KVME_FLAG_COW;
1979 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1980 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1981 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1982 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1983 
1984 		last_timestamp = map->timestamp;
1985 		vm_map_unlock_read(map);
1986 
1987 		kve->kve_fileid = 0;
1988 		kve->kve_fsid = 0;
1989 		freepath = NULL;
1990 		fullpath = "";
1991 		if (lobj) {
1992 			vp = NULL;
1993 			switch (lobj->type) {
1994 			case OBJT_DEFAULT:
1995 				kve->kve_type = KVME_TYPE_DEFAULT;
1996 				break;
1997 			case OBJT_VNODE:
1998 				kve->kve_type = KVME_TYPE_VNODE;
1999 				vp = lobj->handle;
2000 				vref(vp);
2001 				break;
2002 			case OBJT_SWAP:
2003 				kve->kve_type = KVME_TYPE_SWAP;
2004 				break;
2005 			case OBJT_DEVICE:
2006 				kve->kve_type = KVME_TYPE_DEVICE;
2007 				break;
2008 			case OBJT_PHYS:
2009 				kve->kve_type = KVME_TYPE_PHYS;
2010 				break;
2011 			case OBJT_DEAD:
2012 				kve->kve_type = KVME_TYPE_DEAD;
2013 				break;
2014 			case OBJT_SG:
2015 				kve->kve_type = KVME_TYPE_SG;
2016 				break;
2017 			default:
2018 				kve->kve_type = KVME_TYPE_UNKNOWN;
2019 				break;
2020 			}
2021 			if (lobj != obj)
2022 				VM_OBJECT_UNLOCK(lobj);
2023 
2024 			kve->kve_ref_count = obj->ref_count;
2025 			kve->kve_shadow_count = obj->shadow_count;
2026 			VM_OBJECT_UNLOCK(obj);
2027 			if (vp != NULL) {
2028 				vn_fullpath(curthread, vp, &fullpath,
2029 				    &freepath);
2030 				cred = curthread->td_ucred;
2031 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2032 				vn_lock(vp, LK_SHARED | LK_RETRY);
2033 				if (VOP_GETATTR(vp, &va, cred) == 0) {
2034 					kve->kve_fileid = va.va_fileid;
2035 					kve->kve_fsid = va.va_fsid;
2036 				}
2037 				vput(vp);
2038 				VFS_UNLOCK_GIANT(vfslocked);
2039 			}
2040 		} else {
2041 			kve->kve_type = KVME_TYPE_NONE;
2042 			kve->kve_ref_count = 0;
2043 			kve->kve_shadow_count = 0;
2044 		}
2045 
2046 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2047 		if (freepath != NULL)
2048 			free(freepath, M_TEMP);
2049 
2050 		error = SYSCTL_OUT(req, kve, sizeof(*kve));
2051 		vm_map_lock_read(map);
2052 		if (error)
2053 			break;
2054 		if (last_timestamp != map->timestamp) {
2055 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2056 			entry = tmp_entry;
2057 		}
2058 	}
2059 	vm_map_unlock_read(map);
2060 	vmspace_free(vm);
2061 	PRELE(p);
2062 	free(kve, M_TEMP);
2063 	return (error);
2064 }
2065 #endif	/* COMPAT_FREEBSD7 */
2066 
2067 #ifdef KINFO_VMENTRY_SIZE
2068 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2069 #endif
2070 
2071 static int
2072 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2073 {
2074 	vm_map_entry_t entry, tmp_entry;
2075 	unsigned int last_timestamp;
2076 	char *fullpath, *freepath;
2077 	struct kinfo_vmentry *kve;
2078 	struct vattr va;
2079 	struct ucred *cred;
2080 	int error, *name;
2081 	struct vnode *vp;
2082 	struct proc *p;
2083 	struct vmspace *vm;
2084 	vm_map_t map;
2085 
2086 	name = (int *)arg1;
2087 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2088 	if (error != 0)
2089 		return (error);
2090 	vm = vmspace_acquire_ref(p);
2091 	if (vm == NULL) {
2092 		PRELE(p);
2093 		return (ESRCH);
2094 	}
2095 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2096 
2097 	map = &vm->vm_map;
2098 	vm_map_lock_read(map);
2099 	for (entry = map->header.next; entry != &map->header;
2100 	    entry = entry->next) {
2101 		vm_object_t obj, tobj, lobj;
2102 		vm_offset_t addr;
2103 		vm_paddr_t locked_pa;
2104 		int vfslocked, mincoreinfo;
2105 
2106 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2107 			continue;
2108 
2109 		bzero(kve, sizeof(*kve));
2110 
2111 		kve->kve_private_resident = 0;
2112 		obj = entry->object.vm_object;
2113 		if (obj != NULL) {
2114 			VM_OBJECT_LOCK(obj);
2115 			if (obj->shadow_count == 1)
2116 				kve->kve_private_resident =
2117 				    obj->resident_page_count;
2118 		}
2119 		kve->kve_resident = 0;
2120 		addr = entry->start;
2121 		while (addr < entry->end) {
2122 			locked_pa = 0;
2123 			mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa);
2124 			if (locked_pa != 0)
2125 				vm_page_unlock(PHYS_TO_VM_PAGE(locked_pa));
2126 			if (mincoreinfo & MINCORE_INCORE)
2127 				kve->kve_resident++;
2128 			if (mincoreinfo & MINCORE_SUPER)
2129 				kve->kve_flags |= KVME_FLAG_SUPER;
2130 			addr += PAGE_SIZE;
2131 		}
2132 
2133 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2134 			if (tobj != obj)
2135 				VM_OBJECT_LOCK(tobj);
2136 			if (lobj != obj)
2137 				VM_OBJECT_UNLOCK(lobj);
2138 			lobj = tobj;
2139 		}
2140 
2141 		kve->kve_start = entry->start;
2142 		kve->kve_end = entry->end;
2143 		kve->kve_offset = entry->offset;
2144 
2145 		if (entry->protection & VM_PROT_READ)
2146 			kve->kve_protection |= KVME_PROT_READ;
2147 		if (entry->protection & VM_PROT_WRITE)
2148 			kve->kve_protection |= KVME_PROT_WRITE;
2149 		if (entry->protection & VM_PROT_EXECUTE)
2150 			kve->kve_protection |= KVME_PROT_EXEC;
2151 
2152 		if (entry->eflags & MAP_ENTRY_COW)
2153 			kve->kve_flags |= KVME_FLAG_COW;
2154 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2155 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2156 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2157 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2158 
2159 		last_timestamp = map->timestamp;
2160 		vm_map_unlock_read(map);
2161 
2162 		freepath = NULL;
2163 		fullpath = "";
2164 		if (lobj) {
2165 			vp = NULL;
2166 			switch (lobj->type) {
2167 			case OBJT_DEFAULT:
2168 				kve->kve_type = KVME_TYPE_DEFAULT;
2169 				break;
2170 			case OBJT_VNODE:
2171 				kve->kve_type = KVME_TYPE_VNODE;
2172 				vp = lobj->handle;
2173 				vref(vp);
2174 				break;
2175 			case OBJT_SWAP:
2176 				kve->kve_type = KVME_TYPE_SWAP;
2177 				break;
2178 			case OBJT_DEVICE:
2179 				kve->kve_type = KVME_TYPE_DEVICE;
2180 				break;
2181 			case OBJT_PHYS:
2182 				kve->kve_type = KVME_TYPE_PHYS;
2183 				break;
2184 			case OBJT_DEAD:
2185 				kve->kve_type = KVME_TYPE_DEAD;
2186 				break;
2187 			case OBJT_SG:
2188 				kve->kve_type = KVME_TYPE_SG;
2189 				break;
2190 			default:
2191 				kve->kve_type = KVME_TYPE_UNKNOWN;
2192 				break;
2193 			}
2194 			if (lobj != obj)
2195 				VM_OBJECT_UNLOCK(lobj);
2196 
2197 			kve->kve_ref_count = obj->ref_count;
2198 			kve->kve_shadow_count = obj->shadow_count;
2199 			VM_OBJECT_UNLOCK(obj);
2200 			if (vp != NULL) {
2201 				vn_fullpath(curthread, vp, &fullpath,
2202 				    &freepath);
2203 				kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2204 				cred = curthread->td_ucred;
2205 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2206 				vn_lock(vp, LK_SHARED | LK_RETRY);
2207 				if (VOP_GETATTR(vp, &va, cred) == 0) {
2208 					kve->kve_vn_fileid = va.va_fileid;
2209 					kve->kve_vn_fsid = va.va_fsid;
2210 					kve->kve_vn_mode =
2211 					    MAKEIMODE(va.va_type, va.va_mode);
2212 					kve->kve_vn_size = va.va_size;
2213 					kve->kve_vn_rdev = va.va_rdev;
2214 					kve->kve_status = KF_ATTR_VALID;
2215 				}
2216 				vput(vp);
2217 				VFS_UNLOCK_GIANT(vfslocked);
2218 			}
2219 		} else {
2220 			kve->kve_type = KVME_TYPE_NONE;
2221 			kve->kve_ref_count = 0;
2222 			kve->kve_shadow_count = 0;
2223 		}
2224 
2225 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2226 		if (freepath != NULL)
2227 			free(freepath, M_TEMP);
2228 
2229 		/* Pack record size down */
2230 		kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
2231 		    strlen(kve->kve_path) + 1;
2232 		kve->kve_structsize = roundup(kve->kve_structsize,
2233 		    sizeof(uint64_t));
2234 		error = SYSCTL_OUT(req, kve, kve->kve_structsize);
2235 		vm_map_lock_read(map);
2236 		if (error)
2237 			break;
2238 		if (last_timestamp != map->timestamp) {
2239 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2240 			entry = tmp_entry;
2241 		}
2242 	}
2243 	vm_map_unlock_read(map);
2244 	vmspace_free(vm);
2245 	PRELE(p);
2246 	free(kve, M_TEMP);
2247 	return (error);
2248 }
2249 
2250 #if defined(STACK) || defined(DDB)
2251 static int
2252 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2253 {
2254 	struct kinfo_kstack *kkstp;
2255 	int error, i, *name, numthreads;
2256 	lwpid_t *lwpidarray;
2257 	struct thread *td;
2258 	struct stack *st;
2259 	struct sbuf sb;
2260 	struct proc *p;
2261 
2262 	name = (int *)arg1;
2263 	error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2264 	if (error != 0)
2265 		return (error);
2266 
2267 	kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2268 	st = stack_create();
2269 
2270 	lwpidarray = NULL;
2271 	numthreads = 0;
2272 	PROC_LOCK(p);
2273 repeat:
2274 	if (numthreads < p->p_numthreads) {
2275 		if (lwpidarray != NULL) {
2276 			free(lwpidarray, M_TEMP);
2277 			lwpidarray = NULL;
2278 		}
2279 		numthreads = p->p_numthreads;
2280 		PROC_UNLOCK(p);
2281 		lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2282 		    M_WAITOK | M_ZERO);
2283 		PROC_LOCK(p);
2284 		goto repeat;
2285 	}
2286 	i = 0;
2287 
2288 	/*
2289 	 * XXXRW: During the below loop, execve(2) and countless other sorts
2290 	 * of changes could have taken place.  Should we check to see if the
2291 	 * vmspace has been replaced, or the like, in order to prevent
2292 	 * giving a snapshot that spans, say, execve(2), with some threads
2293 	 * before and some after?  Among other things, the credentials could
2294 	 * have changed, in which case the right to extract debug info might
2295 	 * no longer be assured.
2296 	 */
2297 	FOREACH_THREAD_IN_PROC(p, td) {
2298 		KASSERT(i < numthreads,
2299 		    ("sysctl_kern_proc_kstack: numthreads"));
2300 		lwpidarray[i] = td->td_tid;
2301 		i++;
2302 	}
2303 	numthreads = i;
2304 	for (i = 0; i < numthreads; i++) {
2305 		td = thread_find(p, lwpidarray[i]);
2306 		if (td == NULL) {
2307 			continue;
2308 		}
2309 		bzero(kkstp, sizeof(*kkstp));
2310 		(void)sbuf_new(&sb, kkstp->kkst_trace,
2311 		    sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2312 		thread_lock(td);
2313 		kkstp->kkst_tid = td->td_tid;
2314 		if (TD_IS_SWAPPED(td))
2315 			kkstp->kkst_state = KKST_STATE_SWAPPED;
2316 		else if (TD_IS_RUNNING(td))
2317 			kkstp->kkst_state = KKST_STATE_RUNNING;
2318 		else {
2319 			kkstp->kkst_state = KKST_STATE_STACKOK;
2320 			stack_save_td(st, td);
2321 		}
2322 		thread_unlock(td);
2323 		PROC_UNLOCK(p);
2324 		stack_sbuf_print(&sb, st);
2325 		sbuf_finish(&sb);
2326 		sbuf_delete(&sb);
2327 		error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2328 		PROC_LOCK(p);
2329 		if (error)
2330 			break;
2331 	}
2332 	_PRELE(p);
2333 	PROC_UNLOCK(p);
2334 	if (lwpidarray != NULL)
2335 		free(lwpidarray, M_TEMP);
2336 	stack_destroy(st);
2337 	free(kkstp, M_TEMP);
2338 	return (error);
2339 }
2340 #endif
2341 
2342 /*
2343  * This sysctl allows a process to retrieve the full list of groups from
2344  * itself or another process.
2345  */
2346 static int
2347 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2348 {
2349 	pid_t *pidp = (pid_t *)arg1;
2350 	unsigned int arglen = arg2;
2351 	struct proc *p;
2352 	struct ucred *cred;
2353 	int error;
2354 
2355 	if (arglen != 1)
2356 		return (EINVAL);
2357 	if (*pidp == -1) {	/* -1 means this process */
2358 		p = req->td->td_proc;
2359 	} else {
2360 		error = pget(*pidp, PGET_CANSEE, &p);
2361 		if (error != 0)
2362 			return (error);
2363 	}
2364 
2365 	cred = crhold(p->p_ucred);
2366 	if (*pidp != -1)
2367 		PROC_UNLOCK(p);
2368 
2369 	error = SYSCTL_OUT(req, cred->cr_groups,
2370 	    cred->cr_ngroups * sizeof(gid_t));
2371 	crfree(cred);
2372 	return (error);
2373 }
2374 
2375 /*
2376  * This sysctl allows a process to retrieve the resource limits for
2377  * another process.
2378  */
2379 static int
2380 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2381 {
2382 	int *name = (int *)arg1;
2383 	u_int namelen = arg2;
2384 	struct plimit *limp;
2385 	struct proc *p;
2386 	int error = 0;
2387 
2388 	if (namelen != 1)
2389 		return (EINVAL);
2390 
2391 	error = pget((pid_t)name[0], PGET_CANSEE, &p);
2392 	if (error != 0)
2393 		return (error);
2394 	/*
2395 	 * Check the request size.  We alow sizes smaller rlimit array for
2396 	 * backward binary compatibility: the number of resource limits may
2397 	 * grow.
2398 	 */
2399 	if (sizeof(limp->pl_rlimit) < req->oldlen) {
2400 		PROC_UNLOCK(p);
2401 		return (EINVAL);
2402 	}
2403 
2404 	limp = lim_hold(p->p_limit);
2405 	PROC_UNLOCK(p);
2406 	error = SYSCTL_OUT(req, limp->pl_rlimit, req->oldlen);
2407 	lim_free(limp);
2408 	return (error);
2409 }
2410 
2411 /*
2412  * This sysctl allows a process to retrieve ps_strings structure location of
2413  * another process.
2414  */
2415 static int
2416 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2417 {
2418 	int *name = (int *)arg1;
2419 	u_int namelen = arg2;
2420 	struct proc *p;
2421 	vm_offset_t ps_strings;
2422 	int error;
2423 #ifdef COMPAT_FREEBSD32
2424 	uint32_t ps_strings32;
2425 #endif
2426 
2427 	if (namelen != 1)
2428 		return (EINVAL);
2429 
2430 	error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2431 	if (error != 0)
2432 		return (error);
2433 #ifdef COMPAT_FREEBSD32
2434 	if ((req->flags & SCTL_MASK32) != 0) {
2435 		/*
2436 		 * We return 0 if the 32 bit emulation request is for a 64 bit
2437 		 * process.
2438 		 */
2439 		ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2440 		    PTROUT(p->p_sysent->sv_psstrings) : 0;
2441 		PROC_UNLOCK(p);
2442 		error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2443 		return (error);
2444 	}
2445 #endif
2446 	ps_strings = p->p_sysent->sv_psstrings;
2447 	PROC_UNLOCK(p);
2448 	error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2449 	return (error);
2450 }
2451 
2452 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
2453 
2454 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2455 	CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2456 	"Return entire process table");
2457 
2458 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2459 	sysctl_kern_proc, "Process table");
2460 
2461 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2462 	sysctl_kern_proc, "Process table");
2463 
2464 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2465 	sysctl_kern_proc, "Process table");
2466 
2467 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2468 	CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2469 
2470 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2471 	sysctl_kern_proc, "Process table");
2472 
2473 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2474 	sysctl_kern_proc, "Process table");
2475 
2476 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2477 	sysctl_kern_proc, "Process table");
2478 
2479 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2480 	sysctl_kern_proc, "Process table");
2481 
2482 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2483 	sysctl_kern_proc, "Return process table, no threads");
2484 
2485 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2486 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2487 	sysctl_kern_proc_args, "Process argument list");
2488 
2489 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env,
2490 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2491 	sysctl_kern_proc_env, "Process environment");
2492 
2493 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv,
2494 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2495 	sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2496 
2497 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2498 	CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2499 
2500 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2501 	CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2502 	"Process syscall vector name (ABI type)");
2503 
2504 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2505 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2506 
2507 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2508 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2509 
2510 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2511 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2512 
2513 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2514 	sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2515 
2516 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2517 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2518 
2519 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2520 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2521 
2522 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2523 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2524 
2525 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2526 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2527 
2528 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2529 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2530 	"Return process table, no threads");
2531 
2532 #ifdef COMPAT_FREEBSD7
2533 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2534 	CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2535 #endif
2536 
2537 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2538 	CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2539 
2540 #if defined(STACK) || defined(DDB)
2541 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2542 	CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2543 #endif
2544 
2545 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2546 	CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2547 
2548 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RD |
2549 	CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, "Process resource limits");
2550 
2551 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings,
2552 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2553 	sysctl_kern_proc_ps_strings, "Process ps_strings location");
2554