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