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