xref: /freebsd/sys/kern/kern_proc.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/refcount.h>
51 #include <sys/sbuf.h>
52 #include <sys/sysent.h>
53 #include <sys/sched.h>
54 #include <sys/smp.h>
55 #include <sys/stack.h>
56 #include <sys/sysctl.h>
57 #include <sys/filedesc.h>
58 #include <sys/tty.h>
59 #include <sys/signalvar.h>
60 #include <sys/sdt.h>
61 #include <sys/sx.h>
62 #include <sys/user.h>
63 #include <sys/jail.h>
64 #include <sys/vnode.h>
65 #include <sys/eventhandler.h>
66 #ifdef KTRACE
67 #include <sys/uio.h>
68 #include <sys/ktrace.h>
69 #endif
70 
71 #ifdef DDB
72 #include <ddb/ddb.h>
73 #endif
74 
75 #include <vm/vm.h>
76 #include <vm/vm_extern.h>
77 #include <vm/pmap.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/uma.h>
81 
82 SDT_PROVIDER_DEFINE(proc);
83 SDT_PROBE_DEFINE(proc, kernel, ctor, entry);
84 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
85 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
86 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
87 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
88 SDT_PROBE_DEFINE(proc, kernel, ctor, return);
89 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
93 SDT_PROBE_DEFINE(proc, kernel, dtor, entry);
94 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
95 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
96 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
97 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
98 SDT_PROBE_DEFINE(proc, kernel, dtor, return);
99 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
102 SDT_PROBE_DEFINE(proc, kernel, init, entry);
103 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
104 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
105 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
106 SDT_PROBE_DEFINE(proc, kernel, init, return);
107 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
108 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
109 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
110 
111 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
112 MALLOC_DEFINE(M_SESSION, "session", "session header");
113 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
114 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
115 
116 static void doenterpgrp(struct proc *, struct pgrp *);
117 static void orphanpg(struct pgrp *pg);
118 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
119 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
120 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
121     int preferthread);
122 static void pgadjustjobc(struct pgrp *pgrp, int entering);
123 static void pgdelete(struct pgrp *);
124 static int proc_ctor(void *mem, int size, void *arg, int flags);
125 static void proc_dtor(void *mem, int size, void *arg);
126 static int proc_init(void *mem, int size, int flags);
127 static void proc_fini(void *mem, int size);
128 static void pargs_free(struct pargs *pa);
129 
130 /*
131  * Other process lists
132  */
133 struct pidhashhead *pidhashtbl;
134 u_long pidhash;
135 struct pgrphashhead *pgrphashtbl;
136 u_long pgrphash;
137 struct proclist allproc;
138 struct proclist zombproc;
139 struct sx allproc_lock;
140 struct sx proctree_lock;
141 struct mtx ppeers_lock;
142 uma_zone_t proc_zone;
143 uma_zone_t ithread_zone;
144 
145 int kstack_pages = KSTACK_PAGES;
146 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
147 
148 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
149 
150 /*
151  * Initialize global process hashing structures.
152  */
153 void
154 procinit()
155 {
156 
157 	sx_init(&allproc_lock, "allproc");
158 	sx_init(&proctree_lock, "proctree");
159 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
160 	LIST_INIT(&allproc);
161 	LIST_INIT(&zombproc);
162 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
163 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
164 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
165 	    proc_ctor, proc_dtor, proc_init, proc_fini,
166 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
167 	uihashinit();
168 }
169 
170 /*
171  * Prepare a proc for use.
172  */
173 static int
174 proc_ctor(void *mem, int size, void *arg, int flags)
175 {
176 	struct proc *p;
177 
178 	p = (struct proc *)mem;
179 	SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
180 	EVENTHANDLER_INVOKE(process_ctor, p);
181 	SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
182 	return (0);
183 }
184 
185 /*
186  * Reclaim a proc after use.
187  */
188 static void
189 proc_dtor(void *mem, int size, void *arg)
190 {
191 	struct proc *p;
192 	struct thread *td;
193 
194 	/* INVARIANTS checks go here */
195 	p = (struct proc *)mem;
196 	td = FIRST_THREAD_IN_PROC(p);
197 	SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
198 	if (td != NULL) {
199 #ifdef INVARIANTS
200 		KASSERT((p->p_numthreads == 1),
201 		    ("bad number of threads in exiting process"));
202 		KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
203 #endif
204 		/* Free all OSD associated to this thread. */
205 		osd_thread_exit(td);
206 
207 		/* Dispose of an alternate kstack, if it exists.
208 		 * XXX What if there are more than one thread in the proc?
209 		 *     The first thread in the proc is special and not
210 		 *     freed, so you gotta do this here.
211 		 */
212 		if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
213 			vm_thread_dispose_altkstack(td);
214 	}
215 	EVENTHANDLER_INVOKE(process_dtor, p);
216 	if (p->p_ksi != NULL)
217 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
218 	SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
219 }
220 
221 /*
222  * Initialize type-stable parts of a proc (when newly created).
223  */
224 static int
225 proc_init(void *mem, int size, int flags)
226 {
227 	struct proc *p;
228 
229 	p = (struct proc *)mem;
230 	SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
231 	p->p_sched = (struct p_sched *)&p[1];
232 	bzero(&p->p_mtx, sizeof(struct mtx));
233 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
234 	mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
235 	cv_init(&p->p_pwait, "ppwait");
236 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
237 	EVENTHANDLER_INVOKE(process_init, p);
238 	p->p_stats = pstats_alloc();
239 	SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
240 	return (0);
241 }
242 
243 /*
244  * UMA should ensure that this function is never called.
245  * Freeing a proc structure would violate type stability.
246  */
247 static void
248 proc_fini(void *mem, int size)
249 {
250 #ifdef notnow
251 	struct proc *p;
252 
253 	p = (struct proc *)mem;
254 	EVENTHANDLER_INVOKE(process_fini, p);
255 	pstats_free(p->p_stats);
256 	thread_free(FIRST_THREAD_IN_PROC(p));
257 	mtx_destroy(&p->p_mtx);
258 	if (p->p_ksi != NULL)
259 		ksiginfo_free(p->p_ksi);
260 #else
261 	panic("proc reclaimed");
262 #endif
263 }
264 
265 /*
266  * Is p an inferior of the current process?
267  */
268 int
269 inferior(p)
270 	register struct proc *p;
271 {
272 
273 	sx_assert(&proctree_lock, SX_LOCKED);
274 	for (; p != curproc; p = p->p_pptr)
275 		if (p->p_pid == 0)
276 			return (0);
277 	return (1);
278 }
279 
280 /*
281  * Locate a process by number; return only "live" processes -- i.e., neither
282  * zombies nor newly born but incompletely initialized processes.  By not
283  * returning processes in the PRS_NEW state, we allow callers to avoid
284  * testing for that condition to avoid dereferencing p_ucred, et al.
285  */
286 struct proc *
287 pfind(pid)
288 	register pid_t pid;
289 {
290 	register struct proc *p;
291 
292 	sx_slock(&allproc_lock);
293 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
294 		if (p->p_pid == pid) {
295 			if (p->p_state == PRS_NEW) {
296 				p = NULL;
297 				break;
298 			}
299 			PROC_LOCK(p);
300 			break;
301 		}
302 	sx_sunlock(&allproc_lock);
303 	return (p);
304 }
305 
306 /*
307  * Locate a process group by number.
308  * The caller must hold proctree_lock.
309  */
310 struct pgrp *
311 pgfind(pgid)
312 	register pid_t pgid;
313 {
314 	register struct pgrp *pgrp;
315 
316 	sx_assert(&proctree_lock, SX_LOCKED);
317 
318 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
319 		if (pgrp->pg_id == pgid) {
320 			PGRP_LOCK(pgrp);
321 			return (pgrp);
322 		}
323 	}
324 	return (NULL);
325 }
326 
327 /*
328  * Create a new process group.
329  * pgid must be equal to the pid of p.
330  * Begin a new session if required.
331  */
332 int
333 enterpgrp(p, pgid, pgrp, sess)
334 	register struct proc *p;
335 	pid_t pgid;
336 	struct pgrp *pgrp;
337 	struct session *sess;
338 {
339 	struct pgrp *pgrp2;
340 
341 	sx_assert(&proctree_lock, SX_XLOCKED);
342 
343 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
344 	KASSERT(p->p_pid == pgid,
345 	    ("enterpgrp: new pgrp and pid != pgid"));
346 
347 	pgrp2 = pgfind(pgid);
348 
349 	KASSERT(pgrp2 == NULL,
350 	    ("enterpgrp: pgrp with pgid exists"));
351 	KASSERT(!SESS_LEADER(p),
352 	    ("enterpgrp: session leader attempted setpgrp"));
353 
354 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
355 
356 	if (sess != NULL) {
357 		/*
358 		 * new session
359 		 */
360 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
361 		PROC_LOCK(p);
362 		p->p_flag &= ~P_CONTROLT;
363 		PROC_UNLOCK(p);
364 		PGRP_LOCK(pgrp);
365 		sess->s_leader = p;
366 		sess->s_sid = p->p_pid;
367 		refcount_init(&sess->s_count, 1);
368 		sess->s_ttyvp = NULL;
369 		sess->s_ttyp = NULL;
370 		bcopy(p->p_session->s_login, sess->s_login,
371 			    sizeof(sess->s_login));
372 		pgrp->pg_session = sess;
373 		KASSERT(p == curproc,
374 		    ("enterpgrp: mksession and p != curproc"));
375 	} else {
376 		pgrp->pg_session = p->p_session;
377 		sess_hold(pgrp->pg_session);
378 		PGRP_LOCK(pgrp);
379 	}
380 	pgrp->pg_id = pgid;
381 	LIST_INIT(&pgrp->pg_members);
382 
383 	/*
384 	 * As we have an exclusive lock of proctree_lock,
385 	 * this should not deadlock.
386 	 */
387 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
388 	pgrp->pg_jobc = 0;
389 	SLIST_INIT(&pgrp->pg_sigiolst);
390 	PGRP_UNLOCK(pgrp);
391 
392 	doenterpgrp(p, pgrp);
393 
394 	return (0);
395 }
396 
397 /*
398  * Move p to an existing process group
399  */
400 int
401 enterthispgrp(p, pgrp)
402 	register struct proc *p;
403 	struct pgrp *pgrp;
404 {
405 
406 	sx_assert(&proctree_lock, SX_XLOCKED);
407 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
408 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
409 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
410 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
411 	KASSERT(pgrp->pg_session == p->p_session,
412 		("%s: pgrp's session %p, p->p_session %p.\n",
413 		__func__,
414 		pgrp->pg_session,
415 		p->p_session));
416 	KASSERT(pgrp != p->p_pgrp,
417 		("%s: p belongs to pgrp.", __func__));
418 
419 	doenterpgrp(p, pgrp);
420 
421 	return (0);
422 }
423 
424 /*
425  * Move p to a process group
426  */
427 static void
428 doenterpgrp(p, pgrp)
429 	struct proc *p;
430 	struct pgrp *pgrp;
431 {
432 	struct pgrp *savepgrp;
433 
434 	sx_assert(&proctree_lock, SX_XLOCKED);
435 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
436 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
437 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
438 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
439 
440 	savepgrp = p->p_pgrp;
441 
442 	/*
443 	 * Adjust eligibility of affected pgrps to participate in job control.
444 	 * Increment eligibility counts before decrementing, otherwise we
445 	 * could reach 0 spuriously during the first call.
446 	 */
447 	fixjobc(p, pgrp, 1);
448 	fixjobc(p, p->p_pgrp, 0);
449 
450 	PGRP_LOCK(pgrp);
451 	PGRP_LOCK(savepgrp);
452 	PROC_LOCK(p);
453 	LIST_REMOVE(p, p_pglist);
454 	p->p_pgrp = pgrp;
455 	PROC_UNLOCK(p);
456 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
457 	PGRP_UNLOCK(savepgrp);
458 	PGRP_UNLOCK(pgrp);
459 	if (LIST_EMPTY(&savepgrp->pg_members))
460 		pgdelete(savepgrp);
461 }
462 
463 /*
464  * remove process from process group
465  */
466 int
467 leavepgrp(p)
468 	register struct proc *p;
469 {
470 	struct pgrp *savepgrp;
471 
472 	sx_assert(&proctree_lock, SX_XLOCKED);
473 	savepgrp = p->p_pgrp;
474 	PGRP_LOCK(savepgrp);
475 	PROC_LOCK(p);
476 	LIST_REMOVE(p, p_pglist);
477 	p->p_pgrp = NULL;
478 	PROC_UNLOCK(p);
479 	PGRP_UNLOCK(savepgrp);
480 	if (LIST_EMPTY(&savepgrp->pg_members))
481 		pgdelete(savepgrp);
482 	return (0);
483 }
484 
485 /*
486  * delete a process group
487  */
488 static void
489 pgdelete(pgrp)
490 	register struct pgrp *pgrp;
491 {
492 	struct session *savesess;
493 	struct tty *tp;
494 
495 	sx_assert(&proctree_lock, SX_XLOCKED);
496 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
497 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
498 
499 	/*
500 	 * Reset any sigio structures pointing to us as a result of
501 	 * F_SETOWN with our pgid.
502 	 */
503 	funsetownlst(&pgrp->pg_sigiolst);
504 
505 	PGRP_LOCK(pgrp);
506 	tp = pgrp->pg_session->s_ttyp;
507 	LIST_REMOVE(pgrp, pg_hash);
508 	savesess = pgrp->pg_session;
509 	PGRP_UNLOCK(pgrp);
510 
511 	/* Remove the reference to the pgrp before deallocating it. */
512 	if (tp != NULL) {
513 		tty_lock(tp);
514 		tty_rel_pgrp(tp, pgrp);
515 	}
516 
517 	mtx_destroy(&pgrp->pg_mtx);
518 	free(pgrp, M_PGRP);
519 	sess_release(savesess);
520 }
521 
522 static void
523 pgadjustjobc(pgrp, entering)
524 	struct pgrp *pgrp;
525 	int entering;
526 {
527 
528 	PGRP_LOCK(pgrp);
529 	if (entering)
530 		pgrp->pg_jobc++;
531 	else {
532 		--pgrp->pg_jobc;
533 		if (pgrp->pg_jobc == 0)
534 			orphanpg(pgrp);
535 	}
536 	PGRP_UNLOCK(pgrp);
537 }
538 
539 /*
540  * Adjust pgrp jobc counters when specified process changes process group.
541  * We count the number of processes in each process group that "qualify"
542  * the group for terminal job control (those with a parent in a different
543  * process group of the same session).  If that count reaches zero, the
544  * process group becomes orphaned.  Check both the specified process'
545  * process group and that of its children.
546  * entering == 0 => p is leaving specified group.
547  * entering == 1 => p is entering specified group.
548  */
549 void
550 fixjobc(p, pgrp, entering)
551 	register struct proc *p;
552 	register struct pgrp *pgrp;
553 	int entering;
554 {
555 	register struct pgrp *hispgrp;
556 	register struct session *mysession;
557 
558 	sx_assert(&proctree_lock, SX_LOCKED);
559 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
560 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
561 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
562 
563 	/*
564 	 * Check p's parent to see whether p qualifies its own process
565 	 * group; if so, adjust count for p's process group.
566 	 */
567 	mysession = pgrp->pg_session;
568 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
569 	    hispgrp->pg_session == mysession)
570 		pgadjustjobc(pgrp, entering);
571 
572 	/*
573 	 * Check this process' children to see whether they qualify
574 	 * their process groups; if so, adjust counts for children's
575 	 * process groups.
576 	 */
577 	LIST_FOREACH(p, &p->p_children, p_sibling) {
578 		hispgrp = p->p_pgrp;
579 		if (hispgrp == pgrp ||
580 		    hispgrp->pg_session != mysession)
581 			continue;
582 		PROC_LOCK(p);
583 		if (p->p_state == PRS_ZOMBIE) {
584 			PROC_UNLOCK(p);
585 			continue;
586 		}
587 		PROC_UNLOCK(p);
588 		pgadjustjobc(hispgrp, entering);
589 	}
590 }
591 
592 /*
593  * A process group has become orphaned;
594  * if there are any stopped processes in the group,
595  * hang-up all process in that group.
596  */
597 static void
598 orphanpg(pg)
599 	struct pgrp *pg;
600 {
601 	register struct proc *p;
602 
603 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
604 
605 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
606 		PROC_LOCK(p);
607 		if (P_SHOULDSTOP(p)) {
608 			PROC_UNLOCK(p);
609 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
610 				PROC_LOCK(p);
611 				psignal(p, SIGHUP);
612 				psignal(p, SIGCONT);
613 				PROC_UNLOCK(p);
614 			}
615 			return;
616 		}
617 		PROC_UNLOCK(p);
618 	}
619 }
620 
621 void
622 sess_hold(struct session *s)
623 {
624 
625 	refcount_acquire(&s->s_count);
626 }
627 
628 void
629 sess_release(struct session *s)
630 {
631 
632 	if (refcount_release(&s->s_count)) {
633 		if (s->s_ttyp != NULL) {
634 			tty_lock(s->s_ttyp);
635 			tty_rel_sess(s->s_ttyp, s);
636 		}
637 		mtx_destroy(&s->s_mtx);
638 		free(s, M_SESSION);
639 	}
640 }
641 
642 #include "opt_ddb.h"
643 #ifdef DDB
644 #include <ddb/ddb.h>
645 
646 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
647 {
648 	register struct pgrp *pgrp;
649 	register struct proc *p;
650 	register int i;
651 
652 	for (i = 0; i <= pgrphash; i++) {
653 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
654 			printf("\tindx %d\n", i);
655 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
656 				printf(
657 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
658 				    (void *)pgrp, (long)pgrp->pg_id,
659 				    (void *)pgrp->pg_session,
660 				    pgrp->pg_session->s_count,
661 				    (void *)LIST_FIRST(&pgrp->pg_members));
662 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
663 					printf("\t\tpid %ld addr %p pgrp %p\n",
664 					    (long)p->p_pid, (void *)p,
665 					    (void *)p->p_pgrp);
666 				}
667 			}
668 		}
669 	}
670 }
671 #endif /* DDB */
672 
673 /*
674  * Calculate the kinfo_proc members which contain process-wide
675  * informations.
676  * Must be called with the target process locked.
677  */
678 static void
679 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
680 {
681 	struct thread *td;
682 
683 	PROC_LOCK_ASSERT(p, MA_OWNED);
684 
685 	kp->ki_estcpu = 0;
686 	kp->ki_pctcpu = 0;
687 	kp->ki_runtime = 0;
688 	FOREACH_THREAD_IN_PROC(p, td) {
689 		thread_lock(td);
690 		kp->ki_pctcpu += sched_pctcpu(td);
691 		kp->ki_runtime += cputick2usec(td->td_runtime);
692 		kp->ki_estcpu += td->td_estcpu;
693 		thread_unlock(td);
694 	}
695 }
696 
697 /*
698  * Clear kinfo_proc and fill in any information that is common
699  * to all threads in the process.
700  * Must be called with the target process locked.
701  */
702 static void
703 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
704 {
705 	struct thread *td0;
706 	struct tty *tp;
707 	struct session *sp;
708 	struct ucred *cred;
709 	struct sigacts *ps;
710 
711 	PROC_LOCK_ASSERT(p, MA_OWNED);
712 	bzero(kp, sizeof(*kp));
713 
714 	kp->ki_structsize = sizeof(*kp);
715 	kp->ki_paddr = p;
716 	kp->ki_addr =/* p->p_addr; */0; /* XXX */
717 	kp->ki_args = p->p_args;
718 	kp->ki_textvp = p->p_textvp;
719 #ifdef KTRACE
720 	kp->ki_tracep = p->p_tracevp;
721 	mtx_lock(&ktrace_mtx);
722 	kp->ki_traceflag = p->p_traceflag;
723 	mtx_unlock(&ktrace_mtx);
724 #endif
725 	kp->ki_fd = p->p_fd;
726 	kp->ki_vmspace = p->p_vmspace;
727 	kp->ki_flag = p->p_flag;
728 	cred = p->p_ucred;
729 	if (cred) {
730 		kp->ki_uid = cred->cr_uid;
731 		kp->ki_ruid = cred->cr_ruid;
732 		kp->ki_svuid = cred->cr_svuid;
733 		/* XXX bde doesn't like KI_NGROUPS */
734 		kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
735 		bcopy(cred->cr_groups, kp->ki_groups,
736 		    kp->ki_ngroups * sizeof(gid_t));
737 		kp->ki_rgid = cred->cr_rgid;
738 		kp->ki_svgid = cred->cr_svgid;
739 		kp->ki_cr_flags = cred->cr_flags;
740 		/* If jailed(cred), emulate the old P_JAILED flag. */
741 		if (jailed(cred)) {
742 			kp->ki_flag |= P_JAILED;
743 			/* If inside the jail, use 0 as a jail ID. */
744 			if (cred->cr_prison != curthread->td_ucred->cr_prison)
745 				kp->ki_jid = cred->cr_prison->pr_id;
746 		}
747 	}
748 	ps = p->p_sigacts;
749 	if (ps) {
750 		mtx_lock(&ps->ps_mtx);
751 		kp->ki_sigignore = ps->ps_sigignore;
752 		kp->ki_sigcatch = ps->ps_sigcatch;
753 		mtx_unlock(&ps->ps_mtx);
754 	}
755 	PROC_SLOCK(p);
756 	if (p->p_state != PRS_NEW &&
757 	    p->p_state != PRS_ZOMBIE &&
758 	    p->p_vmspace != NULL) {
759 		struct vmspace *vm = p->p_vmspace;
760 
761 		kp->ki_size = vm->vm_map.size;
762 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
763 		FOREACH_THREAD_IN_PROC(p, td0) {
764 			if (!TD_IS_SWAPPED(td0))
765 				kp->ki_rssize += td0->td_kstack_pages;
766 			if (td0->td_altkstack_obj != NULL)
767 				kp->ki_rssize += td0->td_altkstack_pages;
768 		}
769 		kp->ki_swrss = vm->vm_swrss;
770 		kp->ki_tsize = vm->vm_tsize;
771 		kp->ki_dsize = vm->vm_dsize;
772 		kp->ki_ssize = vm->vm_ssize;
773 	} else if (p->p_state == PRS_ZOMBIE)
774 		kp->ki_stat = SZOMB;
775 	if (kp->ki_flag & P_INMEM)
776 		kp->ki_sflag = PS_INMEM;
777 	else
778 		kp->ki_sflag = 0;
779 	/* Calculate legacy swtime as seconds since 'swtick'. */
780 	kp->ki_swtime = (ticks - p->p_swtick) / hz;
781 	kp->ki_pid = p->p_pid;
782 	kp->ki_nice = p->p_nice;
783 	rufetch(p, &kp->ki_rusage);
784 	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
785 	PROC_SUNLOCK(p);
786 	if ((p->p_flag & P_INMEM) && p->p_stats != NULL) {
787 		kp->ki_start = p->p_stats->p_start;
788 		timevaladd(&kp->ki_start, &boottime);
789 		PROC_SLOCK(p);
790 		calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
791 		PROC_SUNLOCK(p);
792 		calccru(p, &kp->ki_childutime, &kp->ki_childstime);
793 
794 		/* Some callers want child-times in a single value */
795 		kp->ki_childtime = kp->ki_childstime;
796 		timevaladd(&kp->ki_childtime, &kp->ki_childutime);
797 	}
798 	tp = NULL;
799 	if (p->p_pgrp) {
800 		kp->ki_pgid = p->p_pgrp->pg_id;
801 		kp->ki_jobc = p->p_pgrp->pg_jobc;
802 		sp = p->p_pgrp->pg_session;
803 
804 		if (sp != NULL) {
805 			kp->ki_sid = sp->s_sid;
806 			SESS_LOCK(sp);
807 			strlcpy(kp->ki_login, sp->s_login,
808 			    sizeof(kp->ki_login));
809 			if (sp->s_ttyvp)
810 				kp->ki_kiflag |= KI_CTTY;
811 			if (SESS_LEADER(p))
812 				kp->ki_kiflag |= KI_SLEADER;
813 			/* XXX proctree_lock */
814 			tp = sp->s_ttyp;
815 			SESS_UNLOCK(sp);
816 		}
817 	}
818 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
819 		kp->ki_tdev = tty_udev(tp);
820 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
821 		if (tp->t_session)
822 			kp->ki_tsid = tp->t_session->s_sid;
823 	} else
824 		kp->ki_tdev = NODEV;
825 	if (p->p_comm[0] != '\0')
826 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
827 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
828 	    p->p_sysent->sv_name[0] != '\0')
829 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
830 	kp->ki_siglist = p->p_siglist;
831 	kp->ki_xstat = p->p_xstat;
832 	kp->ki_acflag = p->p_acflag;
833 	kp->ki_lock = p->p_lock;
834 	if (p->p_pptr)
835 		kp->ki_ppid = p->p_pptr->p_pid;
836 }
837 
838 /*
839  * Fill in information that is thread specific.  Must be called with p_slock
840  * locked.  If 'preferthread' is set, overwrite certain process-related
841  * fields that are maintained for both threads and processes.
842  */
843 static void
844 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
845 {
846 	struct proc *p;
847 
848 	p = td->td_proc;
849 	PROC_LOCK_ASSERT(p, MA_OWNED);
850 
851 	thread_lock(td);
852 	if (td->td_wmesg != NULL)
853 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
854 	else
855 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
856 	if (td->td_name[0] != '\0')
857 		strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
858 	if (TD_ON_LOCK(td)) {
859 		kp->ki_kiflag |= KI_LOCKBLOCK;
860 		strlcpy(kp->ki_lockname, td->td_lockname,
861 		    sizeof(kp->ki_lockname));
862 	} else {
863 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
864 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
865 	}
866 
867 	if (p->p_state == PRS_NORMAL) { /* approximate. */
868 		if (TD_ON_RUNQ(td) ||
869 		    TD_CAN_RUN(td) ||
870 		    TD_IS_RUNNING(td)) {
871 			kp->ki_stat = SRUN;
872 		} else if (P_SHOULDSTOP(p)) {
873 			kp->ki_stat = SSTOP;
874 		} else if (TD_IS_SLEEPING(td)) {
875 			kp->ki_stat = SSLEEP;
876 		} else if (TD_ON_LOCK(td)) {
877 			kp->ki_stat = SLOCK;
878 		} else {
879 			kp->ki_stat = SWAIT;
880 		}
881 	} else if (p->p_state == PRS_ZOMBIE) {
882 		kp->ki_stat = SZOMB;
883 	} else {
884 		kp->ki_stat = SIDL;
885 	}
886 
887 	/* Things in the thread */
888 	kp->ki_wchan = td->td_wchan;
889 	kp->ki_pri.pri_level = td->td_priority;
890 	kp->ki_pri.pri_native = td->td_base_pri;
891 	kp->ki_lastcpu = td->td_lastcpu;
892 	kp->ki_oncpu = td->td_oncpu;
893 	kp->ki_tdflags = td->td_flags;
894 	kp->ki_tid = td->td_tid;
895 	kp->ki_numthreads = p->p_numthreads;
896 	kp->ki_pcb = td->td_pcb;
897 	kp->ki_kstack = (void *)td->td_kstack;
898 	kp->ki_slptime = (ticks - td->td_slptick) / hz;
899 	kp->ki_pri.pri_class = td->td_pri_class;
900 	kp->ki_pri.pri_user = td->td_user_pri;
901 
902 	if (preferthread) {
903 		kp->ki_runtime = cputick2usec(td->td_runtime);
904 		kp->ki_pctcpu = sched_pctcpu(td);
905 		kp->ki_estcpu = td->td_estcpu;
906 	}
907 
908 	/* We can't get this anymore but ps etc never used it anyway. */
909 	kp->ki_rqindex = 0;
910 
911 	SIGSETOR(kp->ki_siglist, td->td_siglist);
912 	kp->ki_sigmask = td->td_sigmask;
913 	thread_unlock(td);
914 }
915 
916 /*
917  * Fill in a kinfo_proc structure for the specified process.
918  * Must be called with the target process locked.
919  */
920 void
921 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
922 {
923 
924 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
925 
926 	fill_kinfo_proc_only(p, kp);
927 	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
928 	fill_kinfo_aggregate(p, kp);
929 }
930 
931 struct pstats *
932 pstats_alloc(void)
933 {
934 
935 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
936 }
937 
938 /*
939  * Copy parts of p_stats; zero the rest of p_stats (statistics).
940  */
941 void
942 pstats_fork(struct pstats *src, struct pstats *dst)
943 {
944 
945 	bzero(&dst->pstat_startzero,
946 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
947 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
948 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
949 }
950 
951 void
952 pstats_free(struct pstats *ps)
953 {
954 
955 	free(ps, M_SUBPROC);
956 }
957 
958 /*
959  * Locate a zombie process by number
960  */
961 struct proc *
962 zpfind(pid_t pid)
963 {
964 	struct proc *p;
965 
966 	sx_slock(&allproc_lock);
967 	LIST_FOREACH(p, &zombproc, p_list)
968 		if (p->p_pid == pid) {
969 			PROC_LOCK(p);
970 			break;
971 		}
972 	sx_sunlock(&allproc_lock);
973 	return (p);
974 }
975 
976 #define KERN_PROC_ZOMBMASK	0x3
977 #define KERN_PROC_NOTHREADS	0x4
978 
979 /*
980  * Must be called with the process locked and will return with it unlocked.
981  */
982 static int
983 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
984 {
985 	struct thread *td;
986 	struct kinfo_proc kinfo_proc;
987 	int error = 0;
988 	struct proc *np;
989 	pid_t pid = p->p_pid;
990 
991 	PROC_LOCK_ASSERT(p, MA_OWNED);
992 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
993 
994 	fill_kinfo_proc(p, &kinfo_proc);
995 	if (flags & KERN_PROC_NOTHREADS)
996 		error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
997 		    sizeof(kinfo_proc));
998 	else {
999 		FOREACH_THREAD_IN_PROC(p, td) {
1000 			fill_kinfo_thread(td, &kinfo_proc, 1);
1001 			error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
1002 			    sizeof(kinfo_proc));
1003 			if (error)
1004 				break;
1005 		}
1006 	}
1007 	PROC_UNLOCK(p);
1008 	if (error)
1009 		return (error);
1010 	if (flags & KERN_PROC_ZOMBMASK)
1011 		np = zpfind(pid);
1012 	else {
1013 		if (pid == 0)
1014 			return (0);
1015 		np = pfind(pid);
1016 	}
1017 	if (np == NULL)
1018 		return (ESRCH);
1019 	if (np != p) {
1020 		PROC_UNLOCK(np);
1021 		return (ESRCH);
1022 	}
1023 	PROC_UNLOCK(np);
1024 	return (0);
1025 }
1026 
1027 static int
1028 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1029 {
1030 	int *name = (int*) arg1;
1031 	u_int namelen = arg2;
1032 	struct proc *p;
1033 	int flags, doingzomb, oid_number;
1034 	int error = 0;
1035 
1036 	oid_number = oidp->oid_number;
1037 	if (oid_number != KERN_PROC_ALL &&
1038 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
1039 		flags = KERN_PROC_NOTHREADS;
1040 	else {
1041 		flags = 0;
1042 		oid_number &= ~KERN_PROC_INC_THREAD;
1043 	}
1044 	if (oid_number == KERN_PROC_PID) {
1045 		if (namelen != 1)
1046 			return (EINVAL);
1047 		error = sysctl_wire_old_buffer(req, 0);
1048 		if (error)
1049 			return (error);
1050 		p = pfind((pid_t)name[0]);
1051 		if (!p)
1052 			return (ESRCH);
1053 		if ((error = p_cansee(curthread, p))) {
1054 			PROC_UNLOCK(p);
1055 			return (error);
1056 		}
1057 		error = sysctl_out_proc(p, req, flags);
1058 		return (error);
1059 	}
1060 
1061 	switch (oid_number) {
1062 	case KERN_PROC_ALL:
1063 		if (namelen != 0)
1064 			return (EINVAL);
1065 		break;
1066 	case KERN_PROC_PROC:
1067 		if (namelen != 0 && namelen != 1)
1068 			return (EINVAL);
1069 		break;
1070 	default:
1071 		if (namelen != 1)
1072 			return (EINVAL);
1073 		break;
1074 	}
1075 
1076 	if (!req->oldptr) {
1077 		/* overestimate by 5 procs */
1078 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1079 		if (error)
1080 			return (error);
1081 	}
1082 	error = sysctl_wire_old_buffer(req, 0);
1083 	if (error != 0)
1084 		return (error);
1085 	sx_slock(&allproc_lock);
1086 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1087 		if (!doingzomb)
1088 			p = LIST_FIRST(&allproc);
1089 		else
1090 			p = LIST_FIRST(&zombproc);
1091 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1092 			/*
1093 			 * Skip embryonic processes.
1094 			 */
1095 			PROC_SLOCK(p);
1096 			if (p->p_state == PRS_NEW) {
1097 				PROC_SUNLOCK(p);
1098 				continue;
1099 			}
1100 			PROC_SUNLOCK(p);
1101 			PROC_LOCK(p);
1102 			KASSERT(p->p_ucred != NULL,
1103 			    ("process credential is NULL for non-NEW proc"));
1104 			/*
1105 			 * Show a user only appropriate processes.
1106 			 */
1107 			if (p_cansee(curthread, p)) {
1108 				PROC_UNLOCK(p);
1109 				continue;
1110 			}
1111 			/*
1112 			 * TODO - make more efficient (see notes below).
1113 			 * do by session.
1114 			 */
1115 			switch (oid_number) {
1116 
1117 			case KERN_PROC_GID:
1118 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1119 					PROC_UNLOCK(p);
1120 					continue;
1121 				}
1122 				break;
1123 
1124 			case KERN_PROC_PGRP:
1125 				/* could do this by traversing pgrp */
1126 				if (p->p_pgrp == NULL ||
1127 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1128 					PROC_UNLOCK(p);
1129 					continue;
1130 				}
1131 				break;
1132 
1133 			case KERN_PROC_RGID:
1134 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1135 					PROC_UNLOCK(p);
1136 					continue;
1137 				}
1138 				break;
1139 
1140 			case KERN_PROC_SESSION:
1141 				if (p->p_session == NULL ||
1142 				    p->p_session->s_sid != (pid_t)name[0]) {
1143 					PROC_UNLOCK(p);
1144 					continue;
1145 				}
1146 				break;
1147 
1148 			case KERN_PROC_TTY:
1149 				if ((p->p_flag & P_CONTROLT) == 0 ||
1150 				    p->p_session == NULL) {
1151 					PROC_UNLOCK(p);
1152 					continue;
1153 				}
1154 				/* XXX proctree_lock */
1155 				SESS_LOCK(p->p_session);
1156 				if (p->p_session->s_ttyp == NULL ||
1157 				    tty_udev(p->p_session->s_ttyp) !=
1158 				    (dev_t)name[0]) {
1159 					SESS_UNLOCK(p->p_session);
1160 					PROC_UNLOCK(p);
1161 					continue;
1162 				}
1163 				SESS_UNLOCK(p->p_session);
1164 				break;
1165 
1166 			case KERN_PROC_UID:
1167 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1168 					PROC_UNLOCK(p);
1169 					continue;
1170 				}
1171 				break;
1172 
1173 			case KERN_PROC_RUID:
1174 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1175 					PROC_UNLOCK(p);
1176 					continue;
1177 				}
1178 				break;
1179 
1180 			case KERN_PROC_PROC:
1181 				break;
1182 
1183 			default:
1184 				break;
1185 
1186 			}
1187 
1188 			error = sysctl_out_proc(p, req, flags | doingzomb);
1189 			if (error) {
1190 				sx_sunlock(&allproc_lock);
1191 				return (error);
1192 			}
1193 		}
1194 	}
1195 	sx_sunlock(&allproc_lock);
1196 	return (0);
1197 }
1198 
1199 struct pargs *
1200 pargs_alloc(int len)
1201 {
1202 	struct pargs *pa;
1203 
1204 	pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1205 		M_WAITOK);
1206 	refcount_init(&pa->ar_ref, 1);
1207 	pa->ar_length = len;
1208 	return (pa);
1209 }
1210 
1211 static void
1212 pargs_free(struct pargs *pa)
1213 {
1214 
1215 	free(pa, M_PARGS);
1216 }
1217 
1218 void
1219 pargs_hold(struct pargs *pa)
1220 {
1221 
1222 	if (pa == NULL)
1223 		return;
1224 	refcount_acquire(&pa->ar_ref);
1225 }
1226 
1227 void
1228 pargs_drop(struct pargs *pa)
1229 {
1230 
1231 	if (pa == NULL)
1232 		return;
1233 	if (refcount_release(&pa->ar_ref))
1234 		pargs_free(pa);
1235 }
1236 
1237 /*
1238  * This sysctl allows a process to retrieve the argument list or process
1239  * title for another process without groping around in the address space
1240  * of the other process.  It also allow a process to set its own "process
1241  * title to a string of its own choice.
1242  */
1243 static int
1244 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1245 {
1246 	int *name = (int*) arg1;
1247 	u_int namelen = arg2;
1248 	struct pargs *newpa, *pa;
1249 	struct proc *p;
1250 	int error = 0;
1251 
1252 	if (namelen != 1)
1253 		return (EINVAL);
1254 
1255 	p = pfind((pid_t)name[0]);
1256 	if (!p)
1257 		return (ESRCH);
1258 
1259 	if ((error = p_cansee(curthread, p)) != 0) {
1260 		PROC_UNLOCK(p);
1261 		return (error);
1262 	}
1263 
1264 	if (req->newptr && curproc != p) {
1265 		PROC_UNLOCK(p);
1266 		return (EPERM);
1267 	}
1268 
1269 	pa = p->p_args;
1270 	pargs_hold(pa);
1271 	PROC_UNLOCK(p);
1272 	if (req->oldptr != NULL && pa != NULL)
1273 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1274 	pargs_drop(pa);
1275 	if (error != 0 || req->newptr == NULL)
1276 		return (error);
1277 
1278 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1279 		return (ENOMEM);
1280 	newpa = pargs_alloc(req->newlen);
1281 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1282 	if (error != 0) {
1283 		pargs_free(newpa);
1284 		return (error);
1285 	}
1286 	PROC_LOCK(p);
1287 	pa = p->p_args;
1288 	p->p_args = newpa;
1289 	PROC_UNLOCK(p);
1290 	pargs_drop(pa);
1291 	return (0);
1292 }
1293 
1294 /*
1295  * This sysctl allows a process to retrieve the path of the executable for
1296  * itself or another process.
1297  */
1298 static int
1299 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1300 {
1301 	pid_t *pidp = (pid_t *)arg1;
1302 	unsigned int arglen = arg2;
1303 	struct proc *p;
1304 	struct vnode *vp;
1305 	char *retbuf, *freebuf;
1306 	int error, vfslocked;
1307 
1308 	if (arglen != 1)
1309 		return (EINVAL);
1310 	if (*pidp == -1) {	/* -1 means this process */
1311 		p = req->td->td_proc;
1312 	} else {
1313 		p = pfind(*pidp);
1314 		if (p == NULL)
1315 			return (ESRCH);
1316 		if ((error = p_cansee(curthread, p)) != 0) {
1317 			PROC_UNLOCK(p);
1318 			return (error);
1319 		}
1320 	}
1321 
1322 	vp = p->p_textvp;
1323 	if (vp == NULL) {
1324 		if (*pidp != -1)
1325 			PROC_UNLOCK(p);
1326 		return (0);
1327 	}
1328 	vref(vp);
1329 	if (*pidp != -1)
1330 		PROC_UNLOCK(p);
1331 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1332 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1333 	vrele(vp);
1334 	VFS_UNLOCK_GIANT(vfslocked);
1335 	if (error)
1336 		return (error);
1337 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1338 	free(freebuf, M_TEMP);
1339 	return (error);
1340 }
1341 
1342 static int
1343 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1344 {
1345 	struct proc *p;
1346 	char *sv_name;
1347 	int *name;
1348 	int namelen;
1349 	int error;
1350 
1351 	namelen = arg2;
1352 	if (namelen != 1)
1353 		return (EINVAL);
1354 
1355 	name = (int *)arg1;
1356 	if ((p = pfind((pid_t)name[0])) == NULL)
1357 		return (ESRCH);
1358 	if ((error = p_cansee(curthread, p))) {
1359 		PROC_UNLOCK(p);
1360 		return (error);
1361 	}
1362 	sv_name = p->p_sysent->sv_name;
1363 	PROC_UNLOCK(p);
1364 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1365 }
1366 
1367 #ifdef KINFO_OVMENTRY_SIZE
1368 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1369 #endif
1370 
1371 #ifdef COMPAT_FREEBSD7
1372 static int
1373 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1374 {
1375 	vm_map_entry_t entry, tmp_entry;
1376 	unsigned int last_timestamp;
1377 	char *fullpath, *freepath;
1378 	struct kinfo_ovmentry *kve;
1379 	struct vattr va;
1380 	struct ucred *cred;
1381 	int error, *name;
1382 	struct vnode *vp;
1383 	struct proc *p;
1384 	vm_map_t map;
1385 	struct vmspace *vm;
1386 
1387 	name = (int *)arg1;
1388 	if ((p = pfind((pid_t)name[0])) == NULL)
1389 		return (ESRCH);
1390 	if (p->p_flag & P_WEXIT) {
1391 		PROC_UNLOCK(p);
1392 		return (ESRCH);
1393 	}
1394 	if ((error = p_candebug(curthread, p))) {
1395 		PROC_UNLOCK(p);
1396 		return (error);
1397 	}
1398 	_PHOLD(p);
1399 	PROC_UNLOCK(p);
1400 	vm = vmspace_acquire_ref(p);
1401 	if (vm == NULL) {
1402 		PRELE(p);
1403 		return (ESRCH);
1404 	}
1405 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1406 
1407 	map = &p->p_vmspace->vm_map;	/* XXXRW: More locking required? */
1408 	vm_map_lock_read(map);
1409 	for (entry = map->header.next; entry != &map->header;
1410 	    entry = entry->next) {
1411 		vm_object_t obj, tobj, lobj;
1412 		vm_offset_t addr;
1413 		int vfslocked;
1414 
1415 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1416 			continue;
1417 
1418 		bzero(kve, sizeof(*kve));
1419 		kve->kve_structsize = sizeof(*kve);
1420 
1421 		kve->kve_private_resident = 0;
1422 		obj = entry->object.vm_object;
1423 		if (obj != NULL) {
1424 			VM_OBJECT_LOCK(obj);
1425 			if (obj->shadow_count == 1)
1426 				kve->kve_private_resident =
1427 				    obj->resident_page_count;
1428 		}
1429 		kve->kve_resident = 0;
1430 		addr = entry->start;
1431 		while (addr < entry->end) {
1432 			if (pmap_extract(map->pmap, addr))
1433 				kve->kve_resident++;
1434 			addr += PAGE_SIZE;
1435 		}
1436 
1437 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1438 			if (tobj != obj)
1439 				VM_OBJECT_LOCK(tobj);
1440 			if (lobj != obj)
1441 				VM_OBJECT_UNLOCK(lobj);
1442 			lobj = tobj;
1443 		}
1444 
1445 		kve->kve_start = (void*)entry->start;
1446 		kve->kve_end = (void*)entry->end;
1447 		kve->kve_offset = (off_t)entry->offset;
1448 
1449 		if (entry->protection & VM_PROT_READ)
1450 			kve->kve_protection |= KVME_PROT_READ;
1451 		if (entry->protection & VM_PROT_WRITE)
1452 			kve->kve_protection |= KVME_PROT_WRITE;
1453 		if (entry->protection & VM_PROT_EXECUTE)
1454 			kve->kve_protection |= KVME_PROT_EXEC;
1455 
1456 		if (entry->eflags & MAP_ENTRY_COW)
1457 			kve->kve_flags |= KVME_FLAG_COW;
1458 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1459 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1460 
1461 		last_timestamp = map->timestamp;
1462 		vm_map_unlock_read(map);
1463 
1464 		kve->kve_fileid = 0;
1465 		kve->kve_fsid = 0;
1466 		freepath = NULL;
1467 		fullpath = "";
1468 		if (lobj) {
1469 			vp = NULL;
1470 			switch (lobj->type) {
1471 			case OBJT_DEFAULT:
1472 				kve->kve_type = KVME_TYPE_DEFAULT;
1473 				break;
1474 			case OBJT_VNODE:
1475 				kve->kve_type = KVME_TYPE_VNODE;
1476 				vp = lobj->handle;
1477 				vref(vp);
1478 				break;
1479 			case OBJT_SWAP:
1480 				kve->kve_type = KVME_TYPE_SWAP;
1481 				break;
1482 			case OBJT_DEVICE:
1483 				kve->kve_type = KVME_TYPE_DEVICE;
1484 				break;
1485 			case OBJT_PHYS:
1486 				kve->kve_type = KVME_TYPE_PHYS;
1487 				break;
1488 			case OBJT_DEAD:
1489 				kve->kve_type = KVME_TYPE_DEAD;
1490 				break;
1491 			default:
1492 				kve->kve_type = KVME_TYPE_UNKNOWN;
1493 				break;
1494 			}
1495 			if (lobj != obj)
1496 				VM_OBJECT_UNLOCK(lobj);
1497 
1498 			kve->kve_ref_count = obj->ref_count;
1499 			kve->kve_shadow_count = obj->shadow_count;
1500 			VM_OBJECT_UNLOCK(obj);
1501 			if (vp != NULL) {
1502 				vn_fullpath(curthread, vp, &fullpath,
1503 				    &freepath);
1504 				cred = curthread->td_ucred;
1505 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1506 				vn_lock(vp, LK_SHARED | LK_RETRY);
1507 				if (VOP_GETATTR(vp, &va, cred) == 0) {
1508 					kve->kve_fileid = va.va_fileid;
1509 					kve->kve_fsid = va.va_fsid;
1510 				}
1511 				vput(vp);
1512 				VFS_UNLOCK_GIANT(vfslocked);
1513 			}
1514 		} else {
1515 			kve->kve_type = KVME_TYPE_NONE;
1516 			kve->kve_ref_count = 0;
1517 			kve->kve_shadow_count = 0;
1518 		}
1519 
1520 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1521 		if (freepath != NULL)
1522 			free(freepath, M_TEMP);
1523 
1524 		error = SYSCTL_OUT(req, kve, sizeof(*kve));
1525 		vm_map_lock_read(map);
1526 		if (error)
1527 			break;
1528 		if (last_timestamp != map->timestamp) {
1529 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1530 			entry = tmp_entry;
1531 		}
1532 	}
1533 	vm_map_unlock_read(map);
1534 	vmspace_free(vm);
1535 	PRELE(p);
1536 	free(kve, M_TEMP);
1537 	return (error);
1538 }
1539 #endif	/* COMPAT_FREEBSD7 */
1540 
1541 #ifdef KINFO_VMENTRY_SIZE
1542 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1543 #endif
1544 
1545 static int
1546 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
1547 {
1548 	vm_map_entry_t entry, tmp_entry;
1549 	unsigned int last_timestamp;
1550 	char *fullpath, *freepath;
1551 	struct kinfo_vmentry *kve;
1552 	struct vattr va;
1553 	struct ucred *cred;
1554 	int error, *name;
1555 	struct vnode *vp;
1556 	struct proc *p;
1557 	struct vmspace *vm;
1558 	vm_map_t map;
1559 
1560 	name = (int *)arg1;
1561 	if ((p = pfind((pid_t)name[0])) == NULL)
1562 		return (ESRCH);
1563 	if (p->p_flag & P_WEXIT) {
1564 		PROC_UNLOCK(p);
1565 		return (ESRCH);
1566 	}
1567 	if ((error = p_candebug(curthread, p))) {
1568 		PROC_UNLOCK(p);
1569 		return (error);
1570 	}
1571 	_PHOLD(p);
1572 	PROC_UNLOCK(p);
1573 	vm = vmspace_acquire_ref(p);
1574 	if (vm == NULL) {
1575 		PRELE(p);
1576 		return (ESRCH);
1577 	}
1578 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1579 
1580 	map = &vm->vm_map;	/* XXXRW: More locking required? */
1581 	vm_map_lock_read(map);
1582 	for (entry = map->header.next; entry != &map->header;
1583 	    entry = entry->next) {
1584 		vm_object_t obj, tobj, lobj;
1585 		vm_offset_t addr;
1586 		int vfslocked;
1587 
1588 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1589 			continue;
1590 
1591 		bzero(kve, sizeof(*kve));
1592 
1593 		kve->kve_private_resident = 0;
1594 		obj = entry->object.vm_object;
1595 		if (obj != NULL) {
1596 			VM_OBJECT_LOCK(obj);
1597 			if (obj->shadow_count == 1)
1598 				kve->kve_private_resident =
1599 				    obj->resident_page_count;
1600 		}
1601 		kve->kve_resident = 0;
1602 		addr = entry->start;
1603 		while (addr < entry->end) {
1604 			if (pmap_extract(map->pmap, addr))
1605 				kve->kve_resident++;
1606 			addr += PAGE_SIZE;
1607 		}
1608 
1609 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1610 			if (tobj != obj)
1611 				VM_OBJECT_LOCK(tobj);
1612 			if (lobj != obj)
1613 				VM_OBJECT_UNLOCK(lobj);
1614 			lobj = tobj;
1615 		}
1616 
1617 		kve->kve_start = entry->start;
1618 		kve->kve_end = entry->end;
1619 		kve->kve_offset = entry->offset;
1620 
1621 		if (entry->protection & VM_PROT_READ)
1622 			kve->kve_protection |= KVME_PROT_READ;
1623 		if (entry->protection & VM_PROT_WRITE)
1624 			kve->kve_protection |= KVME_PROT_WRITE;
1625 		if (entry->protection & VM_PROT_EXECUTE)
1626 			kve->kve_protection |= KVME_PROT_EXEC;
1627 
1628 		if (entry->eflags & MAP_ENTRY_COW)
1629 			kve->kve_flags |= KVME_FLAG_COW;
1630 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1631 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1632 
1633 		last_timestamp = map->timestamp;
1634 		vm_map_unlock_read(map);
1635 
1636 		kve->kve_fileid = 0;
1637 		kve->kve_fsid = 0;
1638 		freepath = NULL;
1639 		fullpath = "";
1640 		if (lobj) {
1641 			vp = NULL;
1642 			switch (lobj->type) {
1643 			case OBJT_DEFAULT:
1644 				kve->kve_type = KVME_TYPE_DEFAULT;
1645 				break;
1646 			case OBJT_VNODE:
1647 				kve->kve_type = KVME_TYPE_VNODE;
1648 				vp = lobj->handle;
1649 				vref(vp);
1650 				break;
1651 			case OBJT_SWAP:
1652 				kve->kve_type = KVME_TYPE_SWAP;
1653 				break;
1654 			case OBJT_DEVICE:
1655 				kve->kve_type = KVME_TYPE_DEVICE;
1656 				break;
1657 			case OBJT_PHYS:
1658 				kve->kve_type = KVME_TYPE_PHYS;
1659 				break;
1660 			case OBJT_DEAD:
1661 				kve->kve_type = KVME_TYPE_DEAD;
1662 				break;
1663 			default:
1664 				kve->kve_type = KVME_TYPE_UNKNOWN;
1665 				break;
1666 			}
1667 			if (lobj != obj)
1668 				VM_OBJECT_UNLOCK(lobj);
1669 
1670 			kve->kve_ref_count = obj->ref_count;
1671 			kve->kve_shadow_count = obj->shadow_count;
1672 			VM_OBJECT_UNLOCK(obj);
1673 			if (vp != NULL) {
1674 				vn_fullpath(curthread, vp, &fullpath,
1675 				    &freepath);
1676 				cred = curthread->td_ucred;
1677 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1678 				vn_lock(vp, LK_SHARED | LK_RETRY);
1679 				if (VOP_GETATTR(vp, &va, cred) == 0) {
1680 					kve->kve_fileid = va.va_fileid;
1681 					kve->kve_fsid = va.va_fsid;
1682 				}
1683 				vput(vp);
1684 				VFS_UNLOCK_GIANT(vfslocked);
1685 			}
1686 		} else {
1687 			kve->kve_type = KVME_TYPE_NONE;
1688 			kve->kve_ref_count = 0;
1689 			kve->kve_shadow_count = 0;
1690 		}
1691 
1692 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1693 		if (freepath != NULL)
1694 			free(freepath, M_TEMP);
1695 
1696 		/* Pack record size down */
1697 		kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
1698 		    strlen(kve->kve_path) + 1;
1699 		kve->kve_structsize = roundup(kve->kve_structsize,
1700 		    sizeof(uint64_t));
1701 		error = SYSCTL_OUT(req, kve, kve->kve_structsize);
1702 		vm_map_lock_read(map);
1703 		if (error)
1704 			break;
1705 		if (last_timestamp != map->timestamp) {
1706 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1707 			entry = tmp_entry;
1708 		}
1709 	}
1710 	vm_map_unlock_read(map);
1711 	vmspace_free(vm);
1712 	PRELE(p);
1713 	free(kve, M_TEMP);
1714 	return (error);
1715 }
1716 
1717 #if defined(STACK) || defined(DDB)
1718 static int
1719 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
1720 {
1721 	struct kinfo_kstack *kkstp;
1722 	int error, i, *name, numthreads;
1723 	lwpid_t *lwpidarray;
1724 	struct thread *td;
1725 	struct stack *st;
1726 	struct sbuf sb;
1727 	struct proc *p;
1728 
1729 	name = (int *)arg1;
1730 	if ((p = pfind((pid_t)name[0])) == NULL)
1731 		return (ESRCH);
1732 	/* XXXRW: Not clear ESRCH is the right error during proc execve(). */
1733 	if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) {
1734 		PROC_UNLOCK(p);
1735 		return (ESRCH);
1736 	}
1737 	if ((error = p_candebug(curthread, p))) {
1738 		PROC_UNLOCK(p);
1739 		return (error);
1740 	}
1741 	_PHOLD(p);
1742 	PROC_UNLOCK(p);
1743 
1744 	kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
1745 	st = stack_create();
1746 
1747 	lwpidarray = NULL;
1748 	numthreads = 0;
1749 	PROC_LOCK(p);
1750 repeat:
1751 	if (numthreads < p->p_numthreads) {
1752 		if (lwpidarray != NULL) {
1753 			free(lwpidarray, M_TEMP);
1754 			lwpidarray = NULL;
1755 		}
1756 		numthreads = p->p_numthreads;
1757 		PROC_UNLOCK(p);
1758 		lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
1759 		    M_WAITOK | M_ZERO);
1760 		PROC_LOCK(p);
1761 		goto repeat;
1762 	}
1763 	i = 0;
1764 
1765 	/*
1766 	 * XXXRW: During the below loop, execve(2) and countless other sorts
1767 	 * of changes could have taken place.  Should we check to see if the
1768 	 * vmspace has been replaced, or the like, in order to prevent
1769 	 * giving a snapshot that spans, say, execve(2), with some threads
1770 	 * before and some after?  Among other things, the credentials could
1771 	 * have changed, in which case the right to extract debug info might
1772 	 * no longer be assured.
1773 	 */
1774 	FOREACH_THREAD_IN_PROC(p, td) {
1775 		KASSERT(i < numthreads,
1776 		    ("sysctl_kern_proc_kstack: numthreads"));
1777 		lwpidarray[i] = td->td_tid;
1778 		i++;
1779 	}
1780 	numthreads = i;
1781 	for (i = 0; i < numthreads; i++) {
1782 		td = thread_find(p, lwpidarray[i]);
1783 		if (td == NULL) {
1784 			continue;
1785 		}
1786 		bzero(kkstp, sizeof(*kkstp));
1787 		(void)sbuf_new(&sb, kkstp->kkst_trace,
1788 		    sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
1789 		thread_lock(td);
1790 		kkstp->kkst_tid = td->td_tid;
1791 		if (TD_IS_SWAPPED(td))
1792 			kkstp->kkst_state = KKST_STATE_SWAPPED;
1793 		else if (TD_IS_RUNNING(td))
1794 			kkstp->kkst_state = KKST_STATE_RUNNING;
1795 		else {
1796 			kkstp->kkst_state = KKST_STATE_STACKOK;
1797 			stack_save_td(st, td);
1798 		}
1799 		thread_unlock(td);
1800 		PROC_UNLOCK(p);
1801 		stack_sbuf_print(&sb, st);
1802 		sbuf_finish(&sb);
1803 		sbuf_delete(&sb);
1804 		error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
1805 		PROC_LOCK(p);
1806 		if (error)
1807 			break;
1808 	}
1809 	_PRELE(p);
1810 	PROC_UNLOCK(p);
1811 	if (lwpidarray != NULL)
1812 		free(lwpidarray, M_TEMP);
1813 	stack_destroy(st);
1814 	free(kkstp, M_TEMP);
1815 	return (error);
1816 }
1817 #endif
1818 
1819 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1820 
1821 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
1822 	CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
1823 	"Return entire process table");
1824 
1825 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1826 	sysctl_kern_proc, "Process table");
1827 
1828 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
1829 	sysctl_kern_proc, "Process table");
1830 
1831 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1832 	sysctl_kern_proc, "Process table");
1833 
1834 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
1835 	CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1836 
1837 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
1838 	sysctl_kern_proc, "Process table");
1839 
1840 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1841 	sysctl_kern_proc, "Process table");
1842 
1843 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1844 	sysctl_kern_proc, "Process table");
1845 
1846 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1847 	sysctl_kern_proc, "Process table");
1848 
1849 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
1850 	sysctl_kern_proc, "Return process table, no threads");
1851 
1852 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
1853 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
1854 	sysctl_kern_proc_args, "Process argument list");
1855 
1856 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
1857 	CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
1858 
1859 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
1860 	CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
1861 	"Process syscall vector name (ABI type)");
1862 
1863 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
1864 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1865 
1866 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
1867 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1868 
1869 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
1870 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1871 
1872 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
1873 	sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1874 
1875 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
1876 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1877 
1878 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
1879 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1880 
1881 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
1882 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1883 
1884 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
1885 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1886 
1887 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
1888 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
1889 	"Return process table, no threads");
1890 
1891 #ifdef COMPAT_FREEBSD7
1892 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
1893 	CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
1894 #endif
1895 
1896 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
1897 	CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
1898 
1899 #if defined(STACK) || defined(DDB)
1900 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
1901 	CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
1902 #endif
1903