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