xref: /freebsd/sys/kern/kern_proc.c (revision b2db760808f74bb53c232900091c9da801ebbfcc)
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);
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);
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);
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);
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);
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);
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 	PROC_LOCK_ASSERT(p, MA_OWNED);
846 
847 	thread_lock(td);
848 	if (td->td_wmesg != NULL)
849 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
850 	else
851 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
852 	strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
853 	if (TD_ON_LOCK(td)) {
854 		kp->ki_kiflag |= KI_LOCKBLOCK;
855 		strlcpy(kp->ki_lockname, td->td_lockname,
856 		    sizeof(kp->ki_lockname));
857 	} else {
858 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
859 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
860 	}
861 
862 	if (p->p_state == PRS_NORMAL) { /* approximate. */
863 		if (TD_ON_RUNQ(td) ||
864 		    TD_CAN_RUN(td) ||
865 		    TD_IS_RUNNING(td)) {
866 			kp->ki_stat = SRUN;
867 		} else if (P_SHOULDSTOP(p)) {
868 			kp->ki_stat = SSTOP;
869 		} else if (TD_IS_SLEEPING(td)) {
870 			kp->ki_stat = SSLEEP;
871 		} else if (TD_ON_LOCK(td)) {
872 			kp->ki_stat = SLOCK;
873 		} else {
874 			kp->ki_stat = SWAIT;
875 		}
876 	} else if (p->p_state == PRS_ZOMBIE) {
877 		kp->ki_stat = SZOMB;
878 	} else {
879 		kp->ki_stat = SIDL;
880 	}
881 
882 	/* Things in the thread */
883 	kp->ki_wchan = td->td_wchan;
884 	kp->ki_pri.pri_level = td->td_priority;
885 	kp->ki_pri.pri_native = td->td_base_pri;
886 	kp->ki_lastcpu = td->td_lastcpu;
887 	kp->ki_oncpu = td->td_oncpu;
888 	kp->ki_tdflags = td->td_flags;
889 	kp->ki_tid = td->td_tid;
890 	kp->ki_numthreads = p->p_numthreads;
891 	kp->ki_pcb = td->td_pcb;
892 	kp->ki_kstack = (void *)td->td_kstack;
893 	kp->ki_slptime = (ticks - td->td_slptick) / hz;
894 	kp->ki_pri.pri_class = td->td_pri_class;
895 	kp->ki_pri.pri_user = td->td_user_pri;
896 
897 	if (preferthread) {
898 		kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
899 		kp->ki_pctcpu = sched_pctcpu(td);
900 		kp->ki_estcpu = td->td_estcpu;
901 	}
902 
903 	/* We can't get this anymore but ps etc never used it anyway. */
904 	kp->ki_rqindex = 0;
905 
906 	if (preferthread)
907 		kp->ki_siglist = td->td_siglist;
908 	kp->ki_sigmask = td->td_sigmask;
909 	thread_unlock(td);
910 }
911 
912 /*
913  * Fill in a kinfo_proc structure for the specified process.
914  * Must be called with the target process locked.
915  */
916 void
917 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
918 {
919 
920 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
921 
922 	fill_kinfo_proc_only(p, kp);
923 	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
924 	fill_kinfo_aggregate(p, kp);
925 }
926 
927 struct pstats *
928 pstats_alloc(void)
929 {
930 
931 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
932 }
933 
934 /*
935  * Copy parts of p_stats; zero the rest of p_stats (statistics).
936  */
937 void
938 pstats_fork(struct pstats *src, struct pstats *dst)
939 {
940 
941 	bzero(&dst->pstat_startzero,
942 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
943 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
944 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
945 }
946 
947 void
948 pstats_free(struct pstats *ps)
949 {
950 
951 	free(ps, M_SUBPROC);
952 }
953 
954 /*
955  * Locate a zombie process by number
956  */
957 struct proc *
958 zpfind(pid_t pid)
959 {
960 	struct proc *p;
961 
962 	sx_slock(&allproc_lock);
963 	LIST_FOREACH(p, &zombproc, p_list)
964 		if (p->p_pid == pid) {
965 			PROC_LOCK(p);
966 			break;
967 		}
968 	sx_sunlock(&allproc_lock);
969 	return (p);
970 }
971 
972 #define KERN_PROC_ZOMBMASK	0x3
973 #define KERN_PROC_NOTHREADS	0x4
974 
975 #ifdef COMPAT_FREEBSD32
976 
977 /*
978  * This function is typically used to copy out the kernel address, so
979  * it can be replaced by assignment of zero.
980  */
981 static inline uint32_t
982 ptr32_trim(void *ptr)
983 {
984 	uintptr_t uptr;
985 
986 	uptr = (uintptr_t)ptr;
987 	return ((uptr > UINT_MAX) ? 0 : uptr);
988 }
989 
990 #define PTRTRIM_CP(src,dst,fld) \
991 	do { (dst).fld = ptr32_trim((src).fld); } while (0)
992 
993 static void
994 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
995 {
996 	int i;
997 
998 	bzero(ki32, sizeof(struct kinfo_proc32));
999 	ki32->ki_structsize = sizeof(struct kinfo_proc32);
1000 	CP(*ki, *ki32, ki_layout);
1001 	PTRTRIM_CP(*ki, *ki32, ki_args);
1002 	PTRTRIM_CP(*ki, *ki32, ki_paddr);
1003 	PTRTRIM_CP(*ki, *ki32, ki_addr);
1004 	PTRTRIM_CP(*ki, *ki32, ki_tracep);
1005 	PTRTRIM_CP(*ki, *ki32, ki_textvp);
1006 	PTRTRIM_CP(*ki, *ki32, ki_fd);
1007 	PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1008 	PTRTRIM_CP(*ki, *ki32, ki_wchan);
1009 	CP(*ki, *ki32, ki_pid);
1010 	CP(*ki, *ki32, ki_ppid);
1011 	CP(*ki, *ki32, ki_pgid);
1012 	CP(*ki, *ki32, ki_tpgid);
1013 	CP(*ki, *ki32, ki_sid);
1014 	CP(*ki, *ki32, ki_tsid);
1015 	CP(*ki, *ki32, ki_jobc);
1016 	CP(*ki, *ki32, ki_tdev);
1017 	CP(*ki, *ki32, ki_siglist);
1018 	CP(*ki, *ki32, ki_sigmask);
1019 	CP(*ki, *ki32, ki_sigignore);
1020 	CP(*ki, *ki32, ki_sigcatch);
1021 	CP(*ki, *ki32, ki_uid);
1022 	CP(*ki, *ki32, ki_ruid);
1023 	CP(*ki, *ki32, ki_svuid);
1024 	CP(*ki, *ki32, ki_rgid);
1025 	CP(*ki, *ki32, ki_svgid);
1026 	CP(*ki, *ki32, ki_ngroups);
1027 	for (i = 0; i < KI_NGROUPS; i++)
1028 		CP(*ki, *ki32, ki_groups[i]);
1029 	CP(*ki, *ki32, ki_size);
1030 	CP(*ki, *ki32, ki_rssize);
1031 	CP(*ki, *ki32, ki_swrss);
1032 	CP(*ki, *ki32, ki_tsize);
1033 	CP(*ki, *ki32, ki_dsize);
1034 	CP(*ki, *ki32, ki_ssize);
1035 	CP(*ki, *ki32, ki_xstat);
1036 	CP(*ki, *ki32, ki_acflag);
1037 	CP(*ki, *ki32, ki_pctcpu);
1038 	CP(*ki, *ki32, ki_estcpu);
1039 	CP(*ki, *ki32, ki_slptime);
1040 	CP(*ki, *ki32, ki_swtime);
1041 	CP(*ki, *ki32, ki_runtime);
1042 	TV_CP(*ki, *ki32, ki_start);
1043 	TV_CP(*ki, *ki32, ki_childtime);
1044 	CP(*ki, *ki32, ki_flag);
1045 	CP(*ki, *ki32, ki_kiflag);
1046 	CP(*ki, *ki32, ki_traceflag);
1047 	CP(*ki, *ki32, ki_stat);
1048 	CP(*ki, *ki32, ki_nice);
1049 	CP(*ki, *ki32, ki_lock);
1050 	CP(*ki, *ki32, ki_rqindex);
1051 	CP(*ki, *ki32, ki_oncpu);
1052 	CP(*ki, *ki32, ki_lastcpu);
1053 	bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1);
1054 	bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1055 	bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1056 	bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1057 	bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1058 	bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1059 	CP(*ki, *ki32, ki_cr_flags);
1060 	CP(*ki, *ki32, ki_jid);
1061 	CP(*ki, *ki32, ki_numthreads);
1062 	CP(*ki, *ki32, ki_tid);
1063 	CP(*ki, *ki32, ki_pri);
1064 	freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1065 	freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1066 	PTRTRIM_CP(*ki, *ki32, ki_pcb);
1067 	PTRTRIM_CP(*ki, *ki32, ki_kstack);
1068 	PTRTRIM_CP(*ki, *ki32, ki_udata);
1069 	CP(*ki, *ki32, ki_sflag);
1070 	CP(*ki, *ki32, ki_tdflags);
1071 }
1072 
1073 static int
1074 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1075 {
1076 	struct kinfo_proc32 ki32;
1077 	int error;
1078 
1079 	if (req->flags & SCTL_MASK32) {
1080 		freebsd32_kinfo_proc_out(ki, &ki32);
1081 		error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
1082 	} else
1083 		error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
1084 	return (error);
1085 }
1086 #else
1087 static int
1088 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1089 {
1090 
1091 	return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
1092 }
1093 #endif
1094 
1095 /*
1096  * Must be called with the process locked and will return with it unlocked.
1097  */
1098 static int
1099 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1100 {
1101 	struct thread *td;
1102 	struct kinfo_proc kinfo_proc;
1103 	int error = 0;
1104 	struct proc *np;
1105 	pid_t pid = p->p_pid;
1106 
1107 	PROC_LOCK_ASSERT(p, MA_OWNED);
1108 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1109 
1110 	fill_kinfo_proc(p, &kinfo_proc);
1111 	if (flags & KERN_PROC_NOTHREADS)
1112 		error = sysctl_out_proc_copyout(&kinfo_proc, req);
1113 	else {
1114 		FOREACH_THREAD_IN_PROC(p, td) {
1115 			fill_kinfo_thread(td, &kinfo_proc, 1);
1116 			error = sysctl_out_proc_copyout(&kinfo_proc, req);
1117 			if (error)
1118 				break;
1119 		}
1120 	}
1121 	PROC_UNLOCK(p);
1122 	if (error)
1123 		return (error);
1124 	if (flags & KERN_PROC_ZOMBMASK)
1125 		np = zpfind(pid);
1126 	else {
1127 		if (pid == 0)
1128 			return (0);
1129 		np = pfind(pid);
1130 	}
1131 	if (np == NULL)
1132 		return (ESRCH);
1133 	if (np != p) {
1134 		PROC_UNLOCK(np);
1135 		return (ESRCH);
1136 	}
1137 	PROC_UNLOCK(np);
1138 	return (0);
1139 }
1140 
1141 static int
1142 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1143 {
1144 	int *name = (int*) arg1;
1145 	u_int namelen = arg2;
1146 	struct proc *p;
1147 	int flags, doingzomb, oid_number;
1148 	int error = 0;
1149 
1150 	oid_number = oidp->oid_number;
1151 	if (oid_number != KERN_PROC_ALL &&
1152 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
1153 		flags = KERN_PROC_NOTHREADS;
1154 	else {
1155 		flags = 0;
1156 		oid_number &= ~KERN_PROC_INC_THREAD;
1157 	}
1158 	if (oid_number == KERN_PROC_PID) {
1159 		if (namelen != 1)
1160 			return (EINVAL);
1161 		error = sysctl_wire_old_buffer(req, 0);
1162 		if (error)
1163 			return (error);
1164 		p = pfind((pid_t)name[0]);
1165 		if (!p)
1166 			return (ESRCH);
1167 		if ((error = p_cansee(curthread, p))) {
1168 			PROC_UNLOCK(p);
1169 			return (error);
1170 		}
1171 		error = sysctl_out_proc(p, req, flags);
1172 		return (error);
1173 	}
1174 
1175 	switch (oid_number) {
1176 	case KERN_PROC_ALL:
1177 		if (namelen != 0)
1178 			return (EINVAL);
1179 		break;
1180 	case KERN_PROC_PROC:
1181 		if (namelen != 0 && namelen != 1)
1182 			return (EINVAL);
1183 		break;
1184 	default:
1185 		if (namelen != 1)
1186 			return (EINVAL);
1187 		break;
1188 	}
1189 
1190 	if (!req->oldptr) {
1191 		/* overestimate by 5 procs */
1192 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1193 		if (error)
1194 			return (error);
1195 	}
1196 	error = sysctl_wire_old_buffer(req, 0);
1197 	if (error != 0)
1198 		return (error);
1199 	sx_slock(&allproc_lock);
1200 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1201 		if (!doingzomb)
1202 			p = LIST_FIRST(&allproc);
1203 		else
1204 			p = LIST_FIRST(&zombproc);
1205 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1206 			/*
1207 			 * Skip embryonic processes.
1208 			 */
1209 			PROC_SLOCK(p);
1210 			if (p->p_state == PRS_NEW) {
1211 				PROC_SUNLOCK(p);
1212 				continue;
1213 			}
1214 			PROC_SUNLOCK(p);
1215 			PROC_LOCK(p);
1216 			KASSERT(p->p_ucred != NULL,
1217 			    ("process credential is NULL for non-NEW proc"));
1218 			/*
1219 			 * Show a user only appropriate processes.
1220 			 */
1221 			if (p_cansee(curthread, p)) {
1222 				PROC_UNLOCK(p);
1223 				continue;
1224 			}
1225 			/*
1226 			 * TODO - make more efficient (see notes below).
1227 			 * do by session.
1228 			 */
1229 			switch (oid_number) {
1230 
1231 			case KERN_PROC_GID:
1232 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1233 					PROC_UNLOCK(p);
1234 					continue;
1235 				}
1236 				break;
1237 
1238 			case KERN_PROC_PGRP:
1239 				/* could do this by traversing pgrp */
1240 				if (p->p_pgrp == NULL ||
1241 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1242 					PROC_UNLOCK(p);
1243 					continue;
1244 				}
1245 				break;
1246 
1247 			case KERN_PROC_RGID:
1248 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1249 					PROC_UNLOCK(p);
1250 					continue;
1251 				}
1252 				break;
1253 
1254 			case KERN_PROC_SESSION:
1255 				if (p->p_session == NULL ||
1256 				    p->p_session->s_sid != (pid_t)name[0]) {
1257 					PROC_UNLOCK(p);
1258 					continue;
1259 				}
1260 				break;
1261 
1262 			case KERN_PROC_TTY:
1263 				if ((p->p_flag & P_CONTROLT) == 0 ||
1264 				    p->p_session == NULL) {
1265 					PROC_UNLOCK(p);
1266 					continue;
1267 				}
1268 				/* XXX proctree_lock */
1269 				SESS_LOCK(p->p_session);
1270 				if (p->p_session->s_ttyp == NULL ||
1271 				    tty_udev(p->p_session->s_ttyp) !=
1272 				    (dev_t)name[0]) {
1273 					SESS_UNLOCK(p->p_session);
1274 					PROC_UNLOCK(p);
1275 					continue;
1276 				}
1277 				SESS_UNLOCK(p->p_session);
1278 				break;
1279 
1280 			case KERN_PROC_UID:
1281 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1282 					PROC_UNLOCK(p);
1283 					continue;
1284 				}
1285 				break;
1286 
1287 			case KERN_PROC_RUID:
1288 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1289 					PROC_UNLOCK(p);
1290 					continue;
1291 				}
1292 				break;
1293 
1294 			case KERN_PROC_PROC:
1295 				break;
1296 
1297 			default:
1298 				break;
1299 
1300 			}
1301 
1302 			error = sysctl_out_proc(p, req, flags | doingzomb);
1303 			if (error) {
1304 				sx_sunlock(&allproc_lock);
1305 				return (error);
1306 			}
1307 		}
1308 	}
1309 	sx_sunlock(&allproc_lock);
1310 	return (0);
1311 }
1312 
1313 struct pargs *
1314 pargs_alloc(int len)
1315 {
1316 	struct pargs *pa;
1317 
1318 	pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1319 		M_WAITOK);
1320 	refcount_init(&pa->ar_ref, 1);
1321 	pa->ar_length = len;
1322 	return (pa);
1323 }
1324 
1325 static void
1326 pargs_free(struct pargs *pa)
1327 {
1328 
1329 	free(pa, M_PARGS);
1330 }
1331 
1332 void
1333 pargs_hold(struct pargs *pa)
1334 {
1335 
1336 	if (pa == NULL)
1337 		return;
1338 	refcount_acquire(&pa->ar_ref);
1339 }
1340 
1341 void
1342 pargs_drop(struct pargs *pa)
1343 {
1344 
1345 	if (pa == NULL)
1346 		return;
1347 	if (refcount_release(&pa->ar_ref))
1348 		pargs_free(pa);
1349 }
1350 
1351 /*
1352  * This sysctl allows a process to retrieve the argument list or process
1353  * title for another process without groping around in the address space
1354  * of the other process.  It also allow a process to set its own "process
1355  * title to a string of its own choice.
1356  */
1357 static int
1358 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1359 {
1360 	int *name = (int*) arg1;
1361 	u_int namelen = arg2;
1362 	struct pargs *newpa, *pa;
1363 	struct proc *p;
1364 	int error = 0;
1365 
1366 	if (namelen != 1)
1367 		return (EINVAL);
1368 
1369 	p = pfind((pid_t)name[0]);
1370 	if (!p)
1371 		return (ESRCH);
1372 
1373 	if ((error = p_cansee(curthread, p)) != 0) {
1374 		PROC_UNLOCK(p);
1375 		return (error);
1376 	}
1377 
1378 	if (req->newptr && curproc != p) {
1379 		PROC_UNLOCK(p);
1380 		return (EPERM);
1381 	}
1382 
1383 	pa = p->p_args;
1384 	pargs_hold(pa);
1385 	PROC_UNLOCK(p);
1386 	if (req->oldptr != NULL && pa != NULL)
1387 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1388 	pargs_drop(pa);
1389 	if (error != 0 || req->newptr == NULL)
1390 		return (error);
1391 
1392 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1393 		return (ENOMEM);
1394 	newpa = pargs_alloc(req->newlen);
1395 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1396 	if (error != 0) {
1397 		pargs_free(newpa);
1398 		return (error);
1399 	}
1400 	PROC_LOCK(p);
1401 	pa = p->p_args;
1402 	p->p_args = newpa;
1403 	PROC_UNLOCK(p);
1404 	pargs_drop(pa);
1405 	return (0);
1406 }
1407 
1408 /*
1409  * This sysctl allows a process to retrieve the path of the executable for
1410  * itself or another process.
1411  */
1412 static int
1413 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1414 {
1415 	pid_t *pidp = (pid_t *)arg1;
1416 	unsigned int arglen = arg2;
1417 	struct proc *p;
1418 	struct vnode *vp;
1419 	char *retbuf, *freebuf;
1420 	int error, vfslocked;
1421 
1422 	if (arglen != 1)
1423 		return (EINVAL);
1424 	if (*pidp == -1) {	/* -1 means this process */
1425 		p = req->td->td_proc;
1426 	} else {
1427 		p = pfind(*pidp);
1428 		if (p == NULL)
1429 			return (ESRCH);
1430 		if ((error = p_cansee(curthread, p)) != 0) {
1431 			PROC_UNLOCK(p);
1432 			return (error);
1433 		}
1434 	}
1435 
1436 	vp = p->p_textvp;
1437 	if (vp == NULL) {
1438 		if (*pidp != -1)
1439 			PROC_UNLOCK(p);
1440 		return (0);
1441 	}
1442 	vref(vp);
1443 	if (*pidp != -1)
1444 		PROC_UNLOCK(p);
1445 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1446 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1447 	vrele(vp);
1448 	VFS_UNLOCK_GIANT(vfslocked);
1449 	if (error)
1450 		return (error);
1451 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1452 	free(freebuf, M_TEMP);
1453 	return (error);
1454 }
1455 
1456 static int
1457 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1458 {
1459 	struct proc *p;
1460 	char *sv_name;
1461 	int *name;
1462 	int namelen;
1463 	int error;
1464 
1465 	namelen = arg2;
1466 	if (namelen != 1)
1467 		return (EINVAL);
1468 
1469 	name = (int *)arg1;
1470 	if ((p = pfind((pid_t)name[0])) == NULL)
1471 		return (ESRCH);
1472 	if ((error = p_cansee(curthread, p))) {
1473 		PROC_UNLOCK(p);
1474 		return (error);
1475 	}
1476 	sv_name = p->p_sysent->sv_name;
1477 	PROC_UNLOCK(p);
1478 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1479 }
1480 
1481 #ifdef KINFO_OVMENTRY_SIZE
1482 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1483 #endif
1484 
1485 #ifdef COMPAT_FREEBSD7
1486 static int
1487 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1488 {
1489 	vm_map_entry_t entry, tmp_entry;
1490 	unsigned int last_timestamp;
1491 	char *fullpath, *freepath;
1492 	struct kinfo_ovmentry *kve;
1493 	struct vattr va;
1494 	struct ucred *cred;
1495 	int error, *name;
1496 	struct vnode *vp;
1497 	struct proc *p;
1498 	vm_map_t map;
1499 	struct vmspace *vm;
1500 
1501 	name = (int *)arg1;
1502 	if ((p = pfind((pid_t)name[0])) == NULL)
1503 		return (ESRCH);
1504 	if (p->p_flag & P_WEXIT) {
1505 		PROC_UNLOCK(p);
1506 		return (ESRCH);
1507 	}
1508 	if ((error = p_candebug(curthread, p))) {
1509 		PROC_UNLOCK(p);
1510 		return (error);
1511 	}
1512 	_PHOLD(p);
1513 	PROC_UNLOCK(p);
1514 	vm = vmspace_acquire_ref(p);
1515 	if (vm == NULL) {
1516 		PRELE(p);
1517 		return (ESRCH);
1518 	}
1519 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1520 
1521 	map = &p->p_vmspace->vm_map;	/* XXXRW: More locking required? */
1522 	vm_map_lock_read(map);
1523 	for (entry = map->header.next; entry != &map->header;
1524 	    entry = entry->next) {
1525 		vm_object_t obj, tobj, lobj;
1526 		vm_offset_t addr;
1527 		int vfslocked;
1528 
1529 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1530 			continue;
1531 
1532 		bzero(kve, sizeof(*kve));
1533 		kve->kve_structsize = sizeof(*kve);
1534 
1535 		kve->kve_private_resident = 0;
1536 		obj = entry->object.vm_object;
1537 		if (obj != NULL) {
1538 			VM_OBJECT_LOCK(obj);
1539 			if (obj->shadow_count == 1)
1540 				kve->kve_private_resident =
1541 				    obj->resident_page_count;
1542 		}
1543 		kve->kve_resident = 0;
1544 		addr = entry->start;
1545 		while (addr < entry->end) {
1546 			if (pmap_extract(map->pmap, addr))
1547 				kve->kve_resident++;
1548 			addr += PAGE_SIZE;
1549 		}
1550 
1551 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1552 			if (tobj != obj)
1553 				VM_OBJECT_LOCK(tobj);
1554 			if (lobj != obj)
1555 				VM_OBJECT_UNLOCK(lobj);
1556 			lobj = tobj;
1557 		}
1558 
1559 		kve->kve_start = (void*)entry->start;
1560 		kve->kve_end = (void*)entry->end;
1561 		kve->kve_offset = (off_t)entry->offset;
1562 
1563 		if (entry->protection & VM_PROT_READ)
1564 			kve->kve_protection |= KVME_PROT_READ;
1565 		if (entry->protection & VM_PROT_WRITE)
1566 			kve->kve_protection |= KVME_PROT_WRITE;
1567 		if (entry->protection & VM_PROT_EXECUTE)
1568 			kve->kve_protection |= KVME_PROT_EXEC;
1569 
1570 		if (entry->eflags & MAP_ENTRY_COW)
1571 			kve->kve_flags |= KVME_FLAG_COW;
1572 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1573 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1574 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1575 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1576 
1577 		last_timestamp = map->timestamp;
1578 		vm_map_unlock_read(map);
1579 
1580 		kve->kve_fileid = 0;
1581 		kve->kve_fsid = 0;
1582 		freepath = NULL;
1583 		fullpath = "";
1584 		if (lobj) {
1585 			vp = NULL;
1586 			switch (lobj->type) {
1587 			case OBJT_DEFAULT:
1588 				kve->kve_type = KVME_TYPE_DEFAULT;
1589 				break;
1590 			case OBJT_VNODE:
1591 				kve->kve_type = KVME_TYPE_VNODE;
1592 				vp = lobj->handle;
1593 				vref(vp);
1594 				break;
1595 			case OBJT_SWAP:
1596 				kve->kve_type = KVME_TYPE_SWAP;
1597 				break;
1598 			case OBJT_DEVICE:
1599 				kve->kve_type = KVME_TYPE_DEVICE;
1600 				break;
1601 			case OBJT_PHYS:
1602 				kve->kve_type = KVME_TYPE_PHYS;
1603 				break;
1604 			case OBJT_DEAD:
1605 				kve->kve_type = KVME_TYPE_DEAD;
1606 				break;
1607 			case OBJT_SG:
1608 				kve->kve_type = KVME_TYPE_SG;
1609 				break;
1610 			default:
1611 				kve->kve_type = KVME_TYPE_UNKNOWN;
1612 				break;
1613 			}
1614 			if (lobj != obj)
1615 				VM_OBJECT_UNLOCK(lobj);
1616 
1617 			kve->kve_ref_count = obj->ref_count;
1618 			kve->kve_shadow_count = obj->shadow_count;
1619 			VM_OBJECT_UNLOCK(obj);
1620 			if (vp != NULL) {
1621 				vn_fullpath(curthread, vp, &fullpath,
1622 				    &freepath);
1623 				cred = curthread->td_ucred;
1624 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1625 				vn_lock(vp, LK_SHARED | LK_RETRY);
1626 				if (VOP_GETATTR(vp, &va, cred) == 0) {
1627 					kve->kve_fileid = va.va_fileid;
1628 					kve->kve_fsid = va.va_fsid;
1629 				}
1630 				vput(vp);
1631 				VFS_UNLOCK_GIANT(vfslocked);
1632 			}
1633 		} else {
1634 			kve->kve_type = KVME_TYPE_NONE;
1635 			kve->kve_ref_count = 0;
1636 			kve->kve_shadow_count = 0;
1637 		}
1638 
1639 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1640 		if (freepath != NULL)
1641 			free(freepath, M_TEMP);
1642 
1643 		error = SYSCTL_OUT(req, kve, sizeof(*kve));
1644 		vm_map_lock_read(map);
1645 		if (error)
1646 			break;
1647 		if (last_timestamp != map->timestamp) {
1648 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1649 			entry = tmp_entry;
1650 		}
1651 	}
1652 	vm_map_unlock_read(map);
1653 	vmspace_free(vm);
1654 	PRELE(p);
1655 	free(kve, M_TEMP);
1656 	return (error);
1657 }
1658 #endif	/* COMPAT_FREEBSD7 */
1659 
1660 #ifdef KINFO_VMENTRY_SIZE
1661 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1662 #endif
1663 
1664 static int
1665 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
1666 {
1667 	vm_map_entry_t entry, tmp_entry;
1668 	unsigned int last_timestamp;
1669 	char *fullpath, *freepath;
1670 	struct kinfo_vmentry *kve;
1671 	struct vattr va;
1672 	struct ucred *cred;
1673 	int error, *name;
1674 	struct vnode *vp;
1675 	struct proc *p;
1676 	struct vmspace *vm;
1677 	vm_map_t map;
1678 
1679 	name = (int *)arg1;
1680 	if ((p = pfind((pid_t)name[0])) == NULL)
1681 		return (ESRCH);
1682 	if (p->p_flag & P_WEXIT) {
1683 		PROC_UNLOCK(p);
1684 		return (ESRCH);
1685 	}
1686 	if ((error = p_candebug(curthread, p))) {
1687 		PROC_UNLOCK(p);
1688 		return (error);
1689 	}
1690 	_PHOLD(p);
1691 	PROC_UNLOCK(p);
1692 	vm = vmspace_acquire_ref(p);
1693 	if (vm == NULL) {
1694 		PRELE(p);
1695 		return (ESRCH);
1696 	}
1697 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1698 
1699 	map = &vm->vm_map;	/* XXXRW: More locking required? */
1700 	vm_map_lock_read(map);
1701 	for (entry = map->header.next; entry != &map->header;
1702 	    entry = entry->next) {
1703 		vm_object_t obj, tobj, lobj;
1704 		vm_offset_t addr;
1705 		int vfslocked;
1706 
1707 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1708 			continue;
1709 
1710 		bzero(kve, sizeof(*kve));
1711 
1712 		kve->kve_private_resident = 0;
1713 		obj = entry->object.vm_object;
1714 		if (obj != NULL) {
1715 			VM_OBJECT_LOCK(obj);
1716 			if (obj->shadow_count == 1)
1717 				kve->kve_private_resident =
1718 				    obj->resident_page_count;
1719 		}
1720 		kve->kve_resident = 0;
1721 		addr = entry->start;
1722 		while (addr < entry->end) {
1723 			if (pmap_extract(map->pmap, addr))
1724 				kve->kve_resident++;
1725 			addr += PAGE_SIZE;
1726 		}
1727 
1728 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1729 			if (tobj != obj)
1730 				VM_OBJECT_LOCK(tobj);
1731 			if (lobj != obj)
1732 				VM_OBJECT_UNLOCK(lobj);
1733 			lobj = tobj;
1734 		}
1735 
1736 		kve->kve_start = entry->start;
1737 		kve->kve_end = entry->end;
1738 		kve->kve_offset = entry->offset;
1739 
1740 		if (entry->protection & VM_PROT_READ)
1741 			kve->kve_protection |= KVME_PROT_READ;
1742 		if (entry->protection & VM_PROT_WRITE)
1743 			kve->kve_protection |= KVME_PROT_WRITE;
1744 		if (entry->protection & VM_PROT_EXECUTE)
1745 			kve->kve_protection |= KVME_PROT_EXEC;
1746 
1747 		if (entry->eflags & MAP_ENTRY_COW)
1748 			kve->kve_flags |= KVME_FLAG_COW;
1749 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1750 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1751 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1752 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1753 
1754 		last_timestamp = map->timestamp;
1755 		vm_map_unlock_read(map);
1756 
1757 		kve->kve_fileid = 0;
1758 		kve->kve_fsid = 0;
1759 		freepath = NULL;
1760 		fullpath = "";
1761 		if (lobj) {
1762 			vp = NULL;
1763 			switch (lobj->type) {
1764 			case OBJT_DEFAULT:
1765 				kve->kve_type = KVME_TYPE_DEFAULT;
1766 				break;
1767 			case OBJT_VNODE:
1768 				kve->kve_type = KVME_TYPE_VNODE;
1769 				vp = lobj->handle;
1770 				vref(vp);
1771 				break;
1772 			case OBJT_SWAP:
1773 				kve->kve_type = KVME_TYPE_SWAP;
1774 				break;
1775 			case OBJT_DEVICE:
1776 				kve->kve_type = KVME_TYPE_DEVICE;
1777 				break;
1778 			case OBJT_PHYS:
1779 				kve->kve_type = KVME_TYPE_PHYS;
1780 				break;
1781 			case OBJT_DEAD:
1782 				kve->kve_type = KVME_TYPE_DEAD;
1783 				break;
1784 			case OBJT_SG:
1785 				kve->kve_type = KVME_TYPE_SG;
1786 				break;
1787 			default:
1788 				kve->kve_type = KVME_TYPE_UNKNOWN;
1789 				break;
1790 			}
1791 			if (lobj != obj)
1792 				VM_OBJECT_UNLOCK(lobj);
1793 
1794 			kve->kve_ref_count = obj->ref_count;
1795 			kve->kve_shadow_count = obj->shadow_count;
1796 			VM_OBJECT_UNLOCK(obj);
1797 			if (vp != NULL) {
1798 				vn_fullpath(curthread, vp, &fullpath,
1799 				    &freepath);
1800 				cred = curthread->td_ucred;
1801 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1802 				vn_lock(vp, LK_SHARED | LK_RETRY);
1803 				if (VOP_GETATTR(vp, &va, cred) == 0) {
1804 					kve->kve_fileid = va.va_fileid;
1805 					kve->kve_fsid = va.va_fsid;
1806 				}
1807 				vput(vp);
1808 				VFS_UNLOCK_GIANT(vfslocked);
1809 			}
1810 		} else {
1811 			kve->kve_type = KVME_TYPE_NONE;
1812 			kve->kve_ref_count = 0;
1813 			kve->kve_shadow_count = 0;
1814 		}
1815 
1816 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1817 		if (freepath != NULL)
1818 			free(freepath, M_TEMP);
1819 
1820 		/* Pack record size down */
1821 		kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
1822 		    strlen(kve->kve_path) + 1;
1823 		kve->kve_structsize = roundup(kve->kve_structsize,
1824 		    sizeof(uint64_t));
1825 		error = SYSCTL_OUT(req, kve, kve->kve_structsize);
1826 		vm_map_lock_read(map);
1827 		if (error)
1828 			break;
1829 		if (last_timestamp != map->timestamp) {
1830 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1831 			entry = tmp_entry;
1832 		}
1833 	}
1834 	vm_map_unlock_read(map);
1835 	vmspace_free(vm);
1836 	PRELE(p);
1837 	free(kve, M_TEMP);
1838 	return (error);
1839 }
1840 
1841 #if defined(STACK) || defined(DDB)
1842 static int
1843 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
1844 {
1845 	struct kinfo_kstack *kkstp;
1846 	int error, i, *name, numthreads;
1847 	lwpid_t *lwpidarray;
1848 	struct thread *td;
1849 	struct stack *st;
1850 	struct sbuf sb;
1851 	struct proc *p;
1852 
1853 	name = (int *)arg1;
1854 	if ((p = pfind((pid_t)name[0])) == NULL)
1855 		return (ESRCH);
1856 	/* XXXRW: Not clear ESRCH is the right error during proc execve(). */
1857 	if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) {
1858 		PROC_UNLOCK(p);
1859 		return (ESRCH);
1860 	}
1861 	if ((error = p_candebug(curthread, p))) {
1862 		PROC_UNLOCK(p);
1863 		return (error);
1864 	}
1865 	_PHOLD(p);
1866 	PROC_UNLOCK(p);
1867 
1868 	kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
1869 	st = stack_create();
1870 
1871 	lwpidarray = NULL;
1872 	numthreads = 0;
1873 	PROC_LOCK(p);
1874 repeat:
1875 	if (numthreads < p->p_numthreads) {
1876 		if (lwpidarray != NULL) {
1877 			free(lwpidarray, M_TEMP);
1878 			lwpidarray = NULL;
1879 		}
1880 		numthreads = p->p_numthreads;
1881 		PROC_UNLOCK(p);
1882 		lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
1883 		    M_WAITOK | M_ZERO);
1884 		PROC_LOCK(p);
1885 		goto repeat;
1886 	}
1887 	i = 0;
1888 
1889 	/*
1890 	 * XXXRW: During the below loop, execve(2) and countless other sorts
1891 	 * of changes could have taken place.  Should we check to see if the
1892 	 * vmspace has been replaced, or the like, in order to prevent
1893 	 * giving a snapshot that spans, say, execve(2), with some threads
1894 	 * before and some after?  Among other things, the credentials could
1895 	 * have changed, in which case the right to extract debug info might
1896 	 * no longer be assured.
1897 	 */
1898 	FOREACH_THREAD_IN_PROC(p, td) {
1899 		KASSERT(i < numthreads,
1900 		    ("sysctl_kern_proc_kstack: numthreads"));
1901 		lwpidarray[i] = td->td_tid;
1902 		i++;
1903 	}
1904 	numthreads = i;
1905 	for (i = 0; i < numthreads; i++) {
1906 		td = thread_find(p, lwpidarray[i]);
1907 		if (td == NULL) {
1908 			continue;
1909 		}
1910 		bzero(kkstp, sizeof(*kkstp));
1911 		(void)sbuf_new(&sb, kkstp->kkst_trace,
1912 		    sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
1913 		thread_lock(td);
1914 		kkstp->kkst_tid = td->td_tid;
1915 		if (TD_IS_SWAPPED(td))
1916 			kkstp->kkst_state = KKST_STATE_SWAPPED;
1917 		else if (TD_IS_RUNNING(td))
1918 			kkstp->kkst_state = KKST_STATE_RUNNING;
1919 		else {
1920 			kkstp->kkst_state = KKST_STATE_STACKOK;
1921 			stack_save_td(st, td);
1922 		}
1923 		thread_unlock(td);
1924 		PROC_UNLOCK(p);
1925 		stack_sbuf_print(&sb, st);
1926 		sbuf_finish(&sb);
1927 		sbuf_delete(&sb);
1928 		error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
1929 		PROC_LOCK(p);
1930 		if (error)
1931 			break;
1932 	}
1933 	_PRELE(p);
1934 	PROC_UNLOCK(p);
1935 	if (lwpidarray != NULL)
1936 		free(lwpidarray, M_TEMP);
1937 	stack_destroy(st);
1938 	free(kkstp, M_TEMP);
1939 	return (error);
1940 }
1941 #endif
1942 
1943 /*
1944  * This sysctl allows a process to retrieve the full list of groups from
1945  * itself or another process.
1946  */
1947 static int
1948 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
1949 {
1950 	pid_t *pidp = (pid_t *)arg1;
1951 	unsigned int arglen = arg2;
1952 	struct proc *p;
1953 	struct ucred *cred;
1954 	int error;
1955 
1956 	if (arglen != 1)
1957 		return (EINVAL);
1958 	if (*pidp == -1) {	/* -1 means this process */
1959 		p = req->td->td_proc;
1960 	} else {
1961 		p = pfind(*pidp);
1962 		if (p == NULL)
1963 			return (ESRCH);
1964 		if ((error = p_cansee(curthread, p)) != 0) {
1965 			PROC_UNLOCK(p);
1966 			return (error);
1967 		}
1968 	}
1969 
1970 	cred = crhold(p->p_ucred);
1971 	if (*pidp != -1)
1972 		PROC_UNLOCK(p);
1973 
1974 	error = SYSCTL_OUT(req, cred->cr_groups,
1975 	    cred->cr_ngroups * sizeof(gid_t));
1976 	crfree(cred);
1977 	return (error);
1978 }
1979 
1980 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1981 
1982 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
1983 	CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
1984 	"Return entire process table");
1985 
1986 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1987 	sysctl_kern_proc, "Process table");
1988 
1989 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
1990 	sysctl_kern_proc, "Process table");
1991 
1992 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1993 	sysctl_kern_proc, "Process table");
1994 
1995 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
1996 	CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1997 
1998 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
1999 	sysctl_kern_proc, "Process table");
2000 
2001 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2002 	sysctl_kern_proc, "Process table");
2003 
2004 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2005 	sysctl_kern_proc, "Process table");
2006 
2007 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2008 	sysctl_kern_proc, "Process table");
2009 
2010 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2011 	sysctl_kern_proc, "Return process table, no threads");
2012 
2013 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2014 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2015 	sysctl_kern_proc_args, "Process argument list");
2016 
2017 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2018 	CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2019 
2020 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2021 	CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2022 	"Process syscall vector name (ABI type)");
2023 
2024 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2025 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2026 
2027 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2028 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2029 
2030 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2031 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2032 
2033 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2034 	sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2035 
2036 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2037 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2038 
2039 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2040 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2041 
2042 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2043 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2044 
2045 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2046 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2047 
2048 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2049 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2050 	"Return process table, no threads");
2051 
2052 #ifdef COMPAT_FREEBSD7
2053 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2054 	CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2055 #endif
2056 
2057 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2058 	CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2059 
2060 #if defined(STACK) || defined(DDB)
2061 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2062 	CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2063 #endif
2064 
2065 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2066 	CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2067