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