xref: /freebsd/sys/kern/kern_proc.c (revision 3357835a462b15fb17d58e7c46a9a60be2ffb27b)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29b75356e1SJeffrey Hsu  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
3030c6f34eSScott Long  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33677b542eSDavid E. O'Brien #include <sys/cdefs.h>
34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
35677b542eSDavid E. O'Brien 
366c84de02SJohn Baldwin #include "opt_ktrace.h"
37b9f009b0SPeter Wemm #include "opt_kstack_pages.h"
386c84de02SJohn Baldwin 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41df8bae1dSRodney W. Grimes #include <sys/kernel.h>
42fb919e4dSMark Murray #include <sys/lock.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44fb919e4dSMark Murray #include <sys/mutex.h>
45b9df5231SPoul-Henning Kamp #include <sys/proc.h>
4655b4a5aeSJohn Baldwin #include <sys/refcount.h>
47baf731e6SRobert Drehmel #include <sys/sysent.h>
48de028f5aSJeff Roberson #include <sys/sched.h>
49165d2b99SJulian Elischer #include <sys/smp.h>
50fb919e4dSMark Murray #include <sys/sysctl.h>
5162d6ce3aSDon Lewis #include <sys/filedesc.h>
52df8bae1dSRodney W. Grimes #include <sys/tty.h>
53bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h>
541005a129SJohn Baldwin #include <sys/sx.h>
55fb919e4dSMark Murray #include <sys/user.h>
56fb919e4dSMark Murray #include <sys/jail.h>
57fe769cddSDavid Schultz #include <sys/vnode.h>
586c84de02SJohn Baldwin #ifdef KTRACE
596c84de02SJohn Baldwin #include <sys/uio.h>
606c84de02SJohn Baldwin #include <sys/ktrace.h>
616c84de02SJohn Baldwin #endif
62fb919e4dSMark Murray 
63efeaf95aSDavid Greenman #include <vm/vm.h>
64a136efe9SPeter Wemm #include <vm/vm_extern.h>
65efeaf95aSDavid Greenman #include <vm/pmap.h>
66efeaf95aSDavid Greenman #include <vm/vm_map.h>
67c897b813SJeff Roberson #include <vm/uma.h>
68df8bae1dSRodney W. Grimes 
69f591779bSSeigo Tanimura MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
70a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header");
71876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
72a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
7355166637SPoul-Henning Kamp 
744d77a549SAlfred Perlstein static void doenterpgrp(struct proc *, struct pgrp *);
754d77a549SAlfred Perlstein static void orphanpg(struct pgrp *pg);
765032ff81SDon Lewis static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
775032ff81SDon Lewis static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp);
7802e878d9SJohn Baldwin static void pgadjustjobc(struct pgrp *pgrp, int entering);
7902e878d9SJohn Baldwin static void pgdelete(struct pgrp *);
80b23f72e9SBrian Feldman static int proc_ctor(void *mem, int size, void *arg, int flags);
81a136efe9SPeter Wemm static void proc_dtor(void *mem, int size, void *arg);
82b23f72e9SBrian Feldman static int proc_init(void *mem, int size, int flags);
83a136efe9SPeter Wemm static void proc_fini(void *mem, int size);
84a136efe9SPeter Wemm 
85df8bae1dSRodney W. Grimes /*
86b75356e1SJeffrey Hsu  * Other process lists
87b75356e1SJeffrey Hsu  */
88b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl;
89b75356e1SJeffrey Hsu u_long pidhash;
90b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl;
91b75356e1SJeffrey Hsu u_long pgrphash;
92b75356e1SJeffrey Hsu struct proclist allproc;
93b75356e1SJeffrey Hsu struct proclist zombproc;
941005a129SJohn Baldwin struct sx allproc_lock;
951005a129SJohn Baldwin struct sx proctree_lock;
96c6544064SJohn Baldwin struct mtx ppeers_lock;
97c897b813SJeff Roberson uma_zone_t proc_zone;
98c897b813SJeff Roberson uma_zone_t ithread_zone;
99b75356e1SJeffrey Hsu 
100b9f009b0SPeter Wemm int kstack_pages = KSTACK_PAGES;
101b9f009b0SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
102b9f009b0SPeter Wemm 
103a30d7c60SJake Burkholder CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
104a30d7c60SJake Burkholder 
105b75356e1SJeffrey Hsu /*
106b75356e1SJeffrey Hsu  * Initialize global process hashing structures.
107df8bae1dSRodney W. Grimes  */
10826f9a767SRodney W. Grimes void
109b75356e1SJeffrey Hsu procinit()
110df8bae1dSRodney W. Grimes {
111df8bae1dSRodney W. Grimes 
1121005a129SJohn Baldwin 	sx_init(&allproc_lock, "allproc");
1131005a129SJohn Baldwin 	sx_init(&proctree_lock, "proctree");
114c6544064SJohn Baldwin 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
115b75356e1SJeffrey Hsu 	LIST_INIT(&allproc);
116b75356e1SJeffrey Hsu 	LIST_INIT(&zombproc);
117b75356e1SJeffrey Hsu 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
118b75356e1SJeffrey Hsu 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
119de028f5aSJeff Roberson 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
120a136efe9SPeter Wemm 	    proc_ctor, proc_dtor, proc_init, proc_fini,
121a136efe9SPeter Wemm 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
122f535380cSDon Lewis 	uihashinit();
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes /*
126a136efe9SPeter Wemm  * Prepare a proc for use.
127a136efe9SPeter Wemm  */
128b23f72e9SBrian Feldman static int
129b23f72e9SBrian Feldman proc_ctor(void *mem, int size, void *arg, int flags)
130a136efe9SPeter Wemm {
131a136efe9SPeter Wemm 	struct proc *p;
132a136efe9SPeter Wemm 
133a136efe9SPeter Wemm 	p = (struct proc *)mem;
134b23f72e9SBrian Feldman 	return (0);
135a136efe9SPeter Wemm }
136a136efe9SPeter Wemm 
137a136efe9SPeter Wemm /*
138a136efe9SPeter Wemm  * Reclaim a proc after use.
139a136efe9SPeter Wemm  */
140a136efe9SPeter Wemm static void
141a136efe9SPeter Wemm proc_dtor(void *mem, int size, void *arg)
142a136efe9SPeter Wemm {
143a136efe9SPeter Wemm 	struct proc *p;
1441faf202eSJulian Elischer 	struct thread *td;
145ed062c8dSJulian Elischer #ifdef INVARIANTS
1461faf202eSJulian Elischer 	struct ksegrp *kg;
147ed062c8dSJulian Elischer #endif
148a136efe9SPeter Wemm 
1491faf202eSJulian Elischer 	/* INVARIANTS checks go here */
150a136efe9SPeter Wemm 	p = (struct proc *)mem;
151ed062c8dSJulian Elischer         td = FIRST_THREAD_IN_PROC(p);
152ed062c8dSJulian Elischer #ifdef INVARIANTS
1531faf202eSJulian Elischer 	KASSERT((p->p_numthreads == 1),
1541faf202eSJulian Elischer 	    ("bad number of threads in exiting process"));
1558daa8c60SDavid Schultz 	KASSERT((p->p_numksegrps == 1), ("free proc with > 1 ksegrp"));
1561faf202eSJulian Elischer 	KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
1571faf202eSJulian Elischer         kg = FIRST_KSEGRP_IN_PROC(p);
1581faf202eSJulian Elischer 	KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
1592c255e9dSRobert Watson 	KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
160ed062c8dSJulian Elischer #endif
161316ec49aSScott Long 
162316ec49aSScott Long 	/* Dispose of an alternate kstack, if it exists.
163316ec49aSScott Long 	 * XXX What if there are more than one thread in the proc?
164316ec49aSScott Long 	 *     The first thread in the proc is special and not
165316ec49aSScott Long 	 *     freed, so you gotta do this here.
166316ec49aSScott Long 	 */
167316ec49aSScott Long 	if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
16889f4fca2SAlan Cox 		vm_thread_dispose_altkstack(td);
169ebceaf6dSDavid Xu 	if (p->p_ksi != NULL)
170ebceaf6dSDavid Xu 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
171a136efe9SPeter Wemm }
172a136efe9SPeter Wemm 
173a136efe9SPeter Wemm /*
174a136efe9SPeter Wemm  * Initialize type-stable parts of a proc (when newly created).
175a136efe9SPeter Wemm  */
176b23f72e9SBrian Feldman static int
177b23f72e9SBrian Feldman proc_init(void *mem, int size, int flags)
178a136efe9SPeter Wemm {
179a136efe9SPeter Wemm 	struct proc *p;
1801faf202eSJulian Elischer 	struct thread *td;
1811faf202eSJulian Elischer 	struct ksegrp *kg;
182a136efe9SPeter Wemm 
183a136efe9SPeter Wemm 	p = (struct proc *)mem;
184de028f5aSJeff Roberson 	p->p_sched = (struct p_sched *)&p[1];
1851faf202eSJulian Elischer 	td = thread_alloc();
1864f0db5e0SJulian Elischer 	kg = ksegrp_alloc();
1877d447c95SJohn Baldwin 	bzero(&p->p_mtx, sizeof(struct mtx));
1887d447c95SJohn Baldwin 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1898b059651SDavid Schultz 	p->p_stats = pstats_alloc();
190ed062c8dSJulian Elischer 	proc_linkup(p, kg, td);
191ed062c8dSJulian Elischer 	sched_newproc(p, kg, td);
192b23f72e9SBrian Feldman 	return (0);
193a136efe9SPeter Wemm }
194a136efe9SPeter Wemm 
195a136efe9SPeter Wemm /*
1968daa8c60SDavid Schultz  * UMA should ensure that this function is never called.
1978daa8c60SDavid Schultz  * Freeing a proc structure would violate type stability.
198a136efe9SPeter Wemm  */
199a136efe9SPeter Wemm static void
200a136efe9SPeter Wemm proc_fini(void *mem, int size)
201a136efe9SPeter Wemm {
202f55ab994SJohn Baldwin #ifdef notnow
203f55ab994SJohn Baldwin 	struct proc *p;
204a136efe9SPeter Wemm 
205f55ab994SJohn Baldwin 	p = (struct proc *)mem;
206f55ab994SJohn Baldwin 	pstats_free(p->p_stats);
207f55ab994SJohn Baldwin 	ksegrp_free(FIRST_KSEGRP_IN_PROC(p));
208f55ab994SJohn Baldwin 	thread_free(FIRST_THREAD_IN_PROC(p));
209f55ab994SJohn Baldwin 	mtx_destroy(&p->p_mtx);
210ebceaf6dSDavid Xu 	if (p->p_ksi != NULL)
211ebceaf6dSDavid Xu 		ksiginfo_free(p->p_ksi);
212f55ab994SJohn Baldwin #else
2138daa8c60SDavid Schultz 	panic("proc reclaimed");
214f55ab994SJohn Baldwin #endif
215a136efe9SPeter Wemm }
216a136efe9SPeter Wemm 
217a136efe9SPeter Wemm /*
218df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
219df8bae1dSRodney W. Grimes  */
2201a432a2fSDima Ruban int
221df8bae1dSRodney W. Grimes inferior(p)
222df8bae1dSRodney W. Grimes 	register struct proc *p;
223df8bae1dSRodney W. Grimes {
224df8bae1dSRodney W. Grimes 
2255b29d6e9SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
2265b29d6e9SJohn Baldwin 	for (; p != curproc; p = p->p_pptr)
22700f13cb3SJohn Baldwin 		if (p->p_pid == 0)
22800f13cb3SJohn Baldwin 			return (0);
22900f13cb3SJohn Baldwin 	return (1);
230df8bae1dSRodney W. Grimes }
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes /*
2336cbea71cSRobert Watson  * Locate a process by number; return only "live" processes -- i.e., neither
2346cbea71cSRobert Watson  * zombies nor newly born but incompletely initialized processes.  By not
2356cbea71cSRobert Watson  * returning processes in the PRS_NEW state, we allow callers to avoid
2366cbea71cSRobert Watson  * testing for that condition to avoid dereferencing p_ucred, et al.
237df8bae1dSRodney W. Grimes  */
238df8bae1dSRodney W. Grimes struct proc *
239df8bae1dSRodney W. Grimes pfind(pid)
240df8bae1dSRodney W. Grimes 	register pid_t pid;
241df8bae1dSRodney W. Grimes {
242df8bae1dSRodney W. Grimes 	register struct proc *p;
243df8bae1dSRodney W. Grimes 
2441005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2451b727751SPoul-Henning Kamp 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
24633a9ed9dSJohn Baldwin 		if (p->p_pid == pid) {
2476cbea71cSRobert Watson 			if (p->p_state == PRS_NEW) {
2486cbea71cSRobert Watson 				p = NULL;
2496cbea71cSRobert Watson 				break;
2506cbea71cSRobert Watson 			}
25133a9ed9dSJohn Baldwin 			PROC_LOCK(p);
252553629ebSJake Burkholder 			break;
25333a9ed9dSJohn Baldwin 		}
25498ab1489SJeffrey Hsu 	sx_sunlock(&allproc_lock);
255df8bae1dSRodney W. Grimes 	return (p);
256df8bae1dSRodney W. Grimes }
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes /*
259f591779bSSeigo Tanimura  * Locate a process group by number.
260f089b570SJohn Baldwin  * The caller must hold proctree_lock.
261df8bae1dSRodney W. Grimes  */
262df8bae1dSRodney W. Grimes struct pgrp *
263df8bae1dSRodney W. Grimes pgfind(pgid)
264df8bae1dSRodney W. Grimes 	register pid_t pgid;
265df8bae1dSRodney W. Grimes {
266df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
267df8bae1dSRodney W. Grimes 
268f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
269f591779bSSeigo Tanimura 
270f591779bSSeigo Tanimura 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
271f591779bSSeigo Tanimura 		if (pgrp->pg_id == pgid) {
272f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
273df8bae1dSRodney W. Grimes 			return (pgrp);
274f591779bSSeigo Tanimura 		}
275f591779bSSeigo Tanimura 	}
276df8bae1dSRodney W. Grimes 	return (NULL);
277df8bae1dSRodney W. Grimes }
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes /*
280f591779bSSeigo Tanimura  * Create a new process group.
281f591779bSSeigo Tanimura  * pgid must be equal to the pid of p.
282f591779bSSeigo Tanimura  * Begin a new session if required.
283df8bae1dSRodney W. Grimes  */
28426f9a767SRodney W. Grimes int
285f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess)
286df8bae1dSRodney W. Grimes 	register struct proc *p;
287df8bae1dSRodney W. Grimes 	pid_t pgid;
288f591779bSSeigo Tanimura 	struct pgrp *pgrp;
289f591779bSSeigo Tanimura 	struct session *sess;
290df8bae1dSRodney W. Grimes {
291f591779bSSeigo Tanimura 	struct pgrp *pgrp2;
292df8bae1dSRodney W. Grimes 
293f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
294f591779bSSeigo Tanimura 
295f591779bSSeigo Tanimura 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
296f591779bSSeigo Tanimura 	KASSERT(p->p_pid == pgid,
297f591779bSSeigo Tanimura 	    ("enterpgrp: new pgrp and pid != pgid"));
298f591779bSSeigo Tanimura 
299f591779bSSeigo Tanimura 	pgrp2 = pgfind(pgid);
300f591779bSSeigo Tanimura 
301f591779bSSeigo Tanimura 	KASSERT(pgrp2 == NULL,
302f591779bSSeigo Tanimura 	    ("enterpgrp: pgrp with pgid exists"));
3035526d2d9SEivind Eklund 	KASSERT(!SESS_LEADER(p),
3045526d2d9SEivind Eklund 	    ("enterpgrp: session leader attempted setpgrp"));
305219cbf59SEivind Eklund 
3066008862bSJohn Baldwin 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
307df8bae1dSRodney W. Grimes 
308f591779bSSeigo Tanimura 	if (sess != NULL) {
309df8bae1dSRodney W. Grimes 		/*
310df8bae1dSRodney W. Grimes 		 * new session
311df8bae1dSRodney W. Grimes 		 */
3126008862bSJohn Baldwin 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
313f591779bSSeigo Tanimura 		PROC_LOCK(p);
314f591779bSSeigo Tanimura 		p->p_flag &= ~P_CONTROLT;
315f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
316f591779bSSeigo Tanimura 		PGRP_LOCK(pgrp);
317df8bae1dSRodney W. Grimes 		sess->s_leader = p;
318643a8daaSDon Lewis 		sess->s_sid = p->p_pid;
319df8bae1dSRodney W. Grimes 		sess->s_count = 1;
320df8bae1dSRodney W. Grimes 		sess->s_ttyvp = NULL;
321df8bae1dSRodney W. Grimes 		sess->s_ttyp = NULL;
322df8bae1dSRodney W. Grimes 		bcopy(p->p_session->s_login, sess->s_login,
323df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
324df8bae1dSRodney W. Grimes 		pgrp->pg_session = sess;
3255526d2d9SEivind Eklund 		KASSERT(p == curproc,
3265526d2d9SEivind Eklund 		    ("enterpgrp: mksession and p != curproc"));
327df8bae1dSRodney W. Grimes 	} else {
328df8bae1dSRodney W. Grimes 		pgrp->pg_session = p->p_session;
329f591779bSSeigo Tanimura 		SESS_LOCK(pgrp->pg_session);
330df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_count++;
331f591779bSSeigo Tanimura 		SESS_UNLOCK(pgrp->pg_session);
332f591779bSSeigo Tanimura 		PGRP_LOCK(pgrp);
333df8bae1dSRodney W. Grimes 	}
334df8bae1dSRodney W. Grimes 	pgrp->pg_id = pgid;
335b75356e1SJeffrey Hsu 	LIST_INIT(&pgrp->pg_members);
336f591779bSSeigo Tanimura 
337f591779bSSeigo Tanimura 	/*
338f089b570SJohn Baldwin 	 * As we have an exclusive lock of proctree_lock,
339f591779bSSeigo Tanimura 	 * this should not deadlock.
340f591779bSSeigo Tanimura 	 */
341b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
342df8bae1dSRodney W. Grimes 	pgrp->pg_jobc = 0;
343831d27a9SDon Lewis 	SLIST_INIT(&pgrp->pg_sigiolst);
344f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
345f591779bSSeigo Tanimura 
346f591779bSSeigo Tanimura 	doenterpgrp(p, pgrp);
347f591779bSSeigo Tanimura 
348df8bae1dSRodney W. Grimes 	return (0);
349f591779bSSeigo Tanimura }
350f591779bSSeigo Tanimura 
351f591779bSSeigo Tanimura /*
352f591779bSSeigo Tanimura  * Move p to an existing process group
353f591779bSSeigo Tanimura  */
354f591779bSSeigo Tanimura int
355f591779bSSeigo Tanimura enterthispgrp(p, pgrp)
356f591779bSSeigo Tanimura 	register struct proc *p;
357f591779bSSeigo Tanimura 	struct pgrp *pgrp;
358f591779bSSeigo Tanimura {
359f089b570SJohn Baldwin 
360f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
361f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
362f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
363f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
364f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
365f591779bSSeigo Tanimura 	KASSERT(pgrp->pg_session == p->p_session,
366f591779bSSeigo Tanimura 		("%s: pgrp's session %p, p->p_session %p.\n",
367f591779bSSeigo Tanimura 		__func__,
368f591779bSSeigo Tanimura 		pgrp->pg_session,
369f591779bSSeigo Tanimura 		p->p_session));
370f591779bSSeigo Tanimura 	KASSERT(pgrp != p->p_pgrp,
371f591779bSSeigo Tanimura 		("%s: p belongs to pgrp.", __func__));
372f591779bSSeigo Tanimura 
373f591779bSSeigo Tanimura 	doenterpgrp(p, pgrp);
374f591779bSSeigo Tanimura 
375f591779bSSeigo Tanimura 	return (0);
376f591779bSSeigo Tanimura }
377f591779bSSeigo Tanimura 
378f591779bSSeigo Tanimura /*
379f591779bSSeigo Tanimura  * Move p to a process group
380f591779bSSeigo Tanimura  */
381f591779bSSeigo Tanimura static void
382f591779bSSeigo Tanimura doenterpgrp(p, pgrp)
383f591779bSSeigo Tanimura 	struct proc *p;
384f591779bSSeigo Tanimura 	struct pgrp *pgrp;
385f591779bSSeigo Tanimura {
386f591779bSSeigo Tanimura 	struct pgrp *savepgrp;
387f591779bSSeigo Tanimura 
388f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
389f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
390f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
391f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
392f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
393f591779bSSeigo Tanimura 
394f591779bSSeigo Tanimura 	savepgrp = p->p_pgrp;
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes 	/*
397df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
398df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
399df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
400df8bae1dSRodney W. Grimes 	 */
401df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
402df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
403df8bae1dSRodney W. Grimes 
404f591779bSSeigo Tanimura 	PGRP_LOCK(pgrp);
405f591779bSSeigo Tanimura 	PGRP_LOCK(savepgrp);
40615e9ec51SJohn Baldwin 	PROC_LOCK(p);
407b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
408df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
40915e9ec51SJohn Baldwin 	PROC_UNLOCK(p);
410f591779bSSeigo Tanimura 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
411f591779bSSeigo Tanimura 	PGRP_UNLOCK(savepgrp);
412f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
413f591779bSSeigo Tanimura 	if (LIST_EMPTY(&savepgrp->pg_members))
414f591779bSSeigo Tanimura 		pgdelete(savepgrp);
415df8bae1dSRodney W. Grimes }
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes /*
418df8bae1dSRodney W. Grimes  * remove process from process group
419df8bae1dSRodney W. Grimes  */
42026f9a767SRodney W. Grimes int
421df8bae1dSRodney W. Grimes leavepgrp(p)
422df8bae1dSRodney W. Grimes 	register struct proc *p;
423df8bae1dSRodney W. Grimes {
424f591779bSSeigo Tanimura 	struct pgrp *savepgrp;
425df8bae1dSRodney W. Grimes 
426f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
427f591779bSSeigo Tanimura 	savepgrp = p->p_pgrp;
428f591779bSSeigo Tanimura 	PGRP_LOCK(savepgrp);
42915e9ec51SJohn Baldwin 	PROC_LOCK(p);
430b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
43115e9ec51SJohn Baldwin 	p->p_pgrp = NULL;
43215e9ec51SJohn Baldwin 	PROC_UNLOCK(p);
433f591779bSSeigo Tanimura 	PGRP_UNLOCK(savepgrp);
434f591779bSSeigo Tanimura 	if (LIST_EMPTY(&savepgrp->pg_members))
435f591779bSSeigo Tanimura 		pgdelete(savepgrp);
436df8bae1dSRodney W. Grimes 	return (0);
437df8bae1dSRodney W. Grimes }
438df8bae1dSRodney W. Grimes 
439df8bae1dSRodney W. Grimes /*
440df8bae1dSRodney W. Grimes  * delete a process group
441df8bae1dSRodney W. Grimes  */
44287b6de2bSPoul-Henning Kamp static void
443df8bae1dSRodney W. Grimes pgdelete(pgrp)
444df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
445df8bae1dSRodney W. Grimes {
446f591779bSSeigo Tanimura 	struct session *savesess;
447f591779bSSeigo Tanimura 
448f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
449f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
450f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
451f591779bSSeigo Tanimura 
452831d27a9SDon Lewis 	/*
453831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
454831d27a9SDon Lewis 	 * F_SETOWN with our pgid.
455831d27a9SDon Lewis 	 */
456831d27a9SDon Lewis 	funsetownlst(&pgrp->pg_sigiolst);
457831d27a9SDon Lewis 
458e649887bSAlfred Perlstein 	PGRP_LOCK(pgrp);
459df8bae1dSRodney W. Grimes 	if (pgrp->pg_session->s_ttyp != NULL &&
460df8bae1dSRodney W. Grimes 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
461df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
462b75356e1SJeffrey Hsu 	LIST_REMOVE(pgrp, pg_hash);
463f591779bSSeigo Tanimura 	savesess = pgrp->pg_session;
464572b4402SPoul-Henning Kamp 	SESSRELE(savesess);
465f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
4666041fa0aSSeigo Tanimura 	mtx_destroy(&pgrp->pg_mtx);
467df8bae1dSRodney W. Grimes 	FREE(pgrp, M_PGRP);
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes 
47002e878d9SJohn Baldwin static void
47102e878d9SJohn Baldwin pgadjustjobc(pgrp, entering)
47202e878d9SJohn Baldwin 	struct pgrp *pgrp;
47302e878d9SJohn Baldwin 	int entering;
47402e878d9SJohn Baldwin {
47502e878d9SJohn Baldwin 
47602e878d9SJohn Baldwin 	PGRP_LOCK(pgrp);
47702e878d9SJohn Baldwin 	if (entering)
47802e878d9SJohn Baldwin 		pgrp->pg_jobc++;
47902e878d9SJohn Baldwin 	else {
48002e878d9SJohn Baldwin 		--pgrp->pg_jobc;
48102e878d9SJohn Baldwin 		if (pgrp->pg_jobc == 0)
48202e878d9SJohn Baldwin 			orphanpg(pgrp);
48302e878d9SJohn Baldwin 	}
48402e878d9SJohn Baldwin 	PGRP_UNLOCK(pgrp);
48502e878d9SJohn Baldwin }
48602e878d9SJohn Baldwin 
487df8bae1dSRodney W. Grimes /*
488df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
489df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
490df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
491df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
492df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
493df8bae1dSRodney W. Grimes  * process group and that of its children.
494df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
495df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
496df8bae1dSRodney W. Grimes  */
49726f9a767SRodney W. Grimes void
498df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
499df8bae1dSRodney W. Grimes 	register struct proc *p;
500df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
501df8bae1dSRodney W. Grimes 	int entering;
502df8bae1dSRodney W. Grimes {
503df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
504f591779bSSeigo Tanimura 	register struct session *mysession;
505f591779bSSeigo Tanimura 
506f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
507f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
508f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
509f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 	/*
512df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
513df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
514df8bae1dSRodney W. Grimes 	 */
515f591779bSSeigo Tanimura 	mysession = pgrp->pg_session;
516df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
51702e878d9SJohn Baldwin 	    hispgrp->pg_session == mysession)
51802e878d9SJohn Baldwin 		pgadjustjobc(pgrp, entering);
519df8bae1dSRodney W. Grimes 
520df8bae1dSRodney W. Grimes 	/*
521df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
522df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
523df8bae1dSRodney W. Grimes 	 * process groups.
524df8bae1dSRodney W. Grimes 	 */
525f591779bSSeigo Tanimura 	LIST_FOREACH(p, &p->p_children, p_sibling) {
52602e878d9SJohn Baldwin 		hispgrp = p->p_pgrp;
52702e878d9SJohn Baldwin 		if (hispgrp == pgrp ||
52802e878d9SJohn Baldwin 		    hispgrp->pg_session != mysession)
52902e878d9SJohn Baldwin 			continue;
53002e878d9SJohn Baldwin 		PROC_LOCK(p);
53102e878d9SJohn Baldwin 		if (p->p_state == PRS_ZOMBIE) {
53202e878d9SJohn Baldwin 			PROC_UNLOCK(p);
53302e878d9SJohn Baldwin 			continue;
534df8bae1dSRodney W. Grimes 		}
53502e878d9SJohn Baldwin 		PROC_UNLOCK(p);
53602e878d9SJohn Baldwin 		pgadjustjobc(hispgrp, entering);
537f591779bSSeigo Tanimura 	}
538dfd5dee1SPeter Wemm }
539df8bae1dSRodney W. Grimes 
540df8bae1dSRodney W. Grimes /*
541df8bae1dSRodney W. Grimes  * A process group has become orphaned;
542df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
543df8bae1dSRodney W. Grimes  * hang-up all process in that group.
544df8bae1dSRodney W. Grimes  */
545df8bae1dSRodney W. Grimes static void
546df8bae1dSRodney W. Grimes orphanpg(pg)
547df8bae1dSRodney W. Grimes 	struct pgrp *pg;
548df8bae1dSRodney W. Grimes {
549df8bae1dSRodney W. Grimes 	register struct proc *p;
550df8bae1dSRodney W. Grimes 
551f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
552f591779bSSeigo Tanimura 
5531b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
55402e878d9SJohn Baldwin 		PROC_LOCK(p);
555e602ba25SJulian Elischer 		if (P_SHOULDSTOP(p)) {
55602e878d9SJohn Baldwin 			PROC_UNLOCK(p);
5571b727751SPoul-Henning Kamp 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
55815e9ec51SJohn Baldwin 				PROC_LOCK(p);
559df8bae1dSRodney W. Grimes 				psignal(p, SIGHUP);
560df8bae1dSRodney W. Grimes 				psignal(p, SIGCONT);
56115e9ec51SJohn Baldwin 				PROC_UNLOCK(p);
562df8bae1dSRodney W. Grimes 			}
563df8bae1dSRodney W. Grimes 			return;
564df8bae1dSRodney W. Grimes 		}
56502e878d9SJohn Baldwin 		PROC_UNLOCK(p);
566df8bae1dSRodney W. Grimes 	}
567df8bae1dSRodney W. Grimes }
568df8bae1dSRodney W. Grimes 
569572b4402SPoul-Henning Kamp void
570572b4402SPoul-Henning Kamp sessrele(struct session *s)
571572b4402SPoul-Henning Kamp {
572572b4402SPoul-Henning Kamp 	int i;
573572b4402SPoul-Henning Kamp 
574572b4402SPoul-Henning Kamp 	SESS_LOCK(s);
575572b4402SPoul-Henning Kamp 	i = --s->s_count;
576572b4402SPoul-Henning Kamp 	SESS_UNLOCK(s);
577572b4402SPoul-Henning Kamp 	if (i == 0) {
578572b4402SPoul-Henning Kamp 		if (s->s_ttyp != NULL)
579572b4402SPoul-Henning Kamp 			ttyrel(s->s_ttyp);
580572b4402SPoul-Henning Kamp 		mtx_destroy(&s->s_mtx);
581572b4402SPoul-Henning Kamp 		FREE(s, M_SESSION);
582572b4402SPoul-Henning Kamp 	}
583572b4402SPoul-Henning Kamp }
584572b4402SPoul-Henning Kamp 
585831031ceSBruce Evans #include "opt_ddb.h"
586831031ceSBruce Evans #ifdef DDB
587831031ceSBruce Evans #include <ddb/ddb.h>
588831031ceSBruce Evans 
589831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump)
590df8bae1dSRodney W. Grimes {
591df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
592df8bae1dSRodney W. Grimes 	register struct proc *p;
593876a94eeSBruce Evans 	register int i;
594df8bae1dSRodney W. Grimes 
595b75356e1SJeffrey Hsu 	for (i = 0; i <= pgrphash; i++) {
5961b727751SPoul-Henning Kamp 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
597df8bae1dSRodney W. Grimes 			printf("\tindx %d\n", i);
5981b727751SPoul-Henning Kamp 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
599ac1e407bSBruce Evans 				printf(
600ac1e407bSBruce Evans 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
601ac1e407bSBruce Evans 				    (void *)pgrp, (long)pgrp->pg_id,
602ac1e407bSBruce Evans 				    (void *)pgrp->pg_session,
603b75356e1SJeffrey Hsu 				    pgrp->pg_session->s_count,
6041b727751SPoul-Henning Kamp 				    (void *)LIST_FIRST(&pgrp->pg_members));
6051b727751SPoul-Henning Kamp 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
606ac1e407bSBruce Evans 					printf("\t\tpid %ld addr %p pgrp %p\n",
607ac1e407bSBruce Evans 					    (long)p->p_pid, (void *)p,
608ac1e407bSBruce Evans 					    (void *)p->p_pgrp);
609df8bae1dSRodney W. Grimes 				}
610df8bae1dSRodney W. Grimes 			}
611df8bae1dSRodney W. Grimes 		}
612df8bae1dSRodney W. Grimes 	}
613df8bae1dSRodney W. Grimes }
614831031ceSBruce Evans #endif /* DDB */
615972f9b20SPoul-Henning Kamp 
616972f9b20SPoul-Henning Kamp /*
6175032ff81SDon Lewis  * Clear kinfo_proc and fill in any information that is common
6185032ff81SDon Lewis  * to all threads in the process.
61965c9b430SJohn Baldwin  * Must be called with the target process locked.
620972f9b20SPoul-Henning Kamp  */
6215032ff81SDon Lewis static void
6225032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
623972f9b20SPoul-Henning Kamp {
6244093529dSJeff Roberson 	struct thread *td0;
6251f7d2501SKirk McKusick 	struct tty *tp;
6261f7d2501SKirk McKusick 	struct session *sp;
6271cbb9c3bSPoul-Henning Kamp 	struct timeval tv;
628d079d0a0SPawel Jakub Dawidek 	struct ucred *cred;
62990af4afaSJohn Baldwin 	struct sigacts *ps;
630972f9b20SPoul-Henning Kamp 
6311f7d2501SKirk McKusick 	bzero(kp, sizeof(*kp));
632972f9b20SPoul-Henning Kamp 
6331f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(*kp);
6341f7d2501SKirk McKusick 	kp->ki_paddr = p;
63565c9b430SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
636b40ce416SJulian Elischer 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
6371f7d2501SKirk McKusick 	kp->ki_args = p->p_args;
6381f7d2501SKirk McKusick 	kp->ki_textvp = p->p_textvp;
6396c84de02SJohn Baldwin #ifdef KTRACE
640a5881ea5SJohn Baldwin 	kp->ki_tracep = p->p_tracevp;
6416c84de02SJohn Baldwin 	mtx_lock(&ktrace_mtx);
6426c84de02SJohn Baldwin 	kp->ki_traceflag = p->p_traceflag;
6436c84de02SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
6446c84de02SJohn Baldwin #endif
6451f7d2501SKirk McKusick 	kp->ki_fd = p->p_fd;
6461f7d2501SKirk McKusick 	kp->ki_vmspace = p->p_vmspace;
647cefcecbeSPawel Jakub Dawidek 	kp->ki_flag = p->p_flag;
648d079d0a0SPawel Jakub Dawidek 	cred = p->p_ucred;
649d079d0a0SPawel Jakub Dawidek 	if (cred) {
650d079d0a0SPawel Jakub Dawidek 		kp->ki_uid = cred->cr_uid;
651d079d0a0SPawel Jakub Dawidek 		kp->ki_ruid = cred->cr_ruid;
652d079d0a0SPawel Jakub Dawidek 		kp->ki_svuid = cred->cr_svuid;
6530ecd57adSPeter Wemm 		/* XXX bde doesn't like KI_NGROUPS */
654d079d0a0SPawel Jakub Dawidek 		kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
655d079d0a0SPawel Jakub Dawidek 		bcopy(cred->cr_groups, kp->ki_groups,
6560ecd57adSPeter Wemm 		    kp->ki_ngroups * sizeof(gid_t));
657d079d0a0SPawel Jakub Dawidek 		kp->ki_rgid = cred->cr_rgid;
658d079d0a0SPawel Jakub Dawidek 		kp->ki_svgid = cred->cr_svgid;
659cefcecbeSPawel Jakub Dawidek 		/* If jailed(cred), emulate the old P_JAILED flag. */
660c78941e6SPawel Jakub Dawidek 		if (jailed(cred)) {
661cefcecbeSPawel Jakub Dawidek 			kp->ki_flag |= P_JAILED;
662c78941e6SPawel Jakub Dawidek 			/* If inside a jail, use 0 as a jail ID. */
663c78941e6SPawel Jakub Dawidek 			if (!jailed(curthread->td_ucred))
664c78941e6SPawel Jakub Dawidek 				kp->ki_jid = cred->cr_prison->pr_id;
665c78941e6SPawel Jakub Dawidek 		}
666972f9b20SPoul-Henning Kamp 	}
66790af4afaSJohn Baldwin 	ps = p->p_sigacts;
668d079d0a0SPawel Jakub Dawidek 	if (ps) {
66990af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
67090af4afaSJohn Baldwin 		kp->ki_sigignore = ps->ps_sigignore;
67190af4afaSJohn Baldwin 		kp->ki_sigcatch = ps->ps_sigcatch;
67290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
673d8c85307SJulian Elischer 	}
6749ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
675e602ba25SJulian Elischer 	if (p->p_state != PRS_NEW &&
676e602ba25SJulian Elischer 	    p->p_state != PRS_ZOMBIE &&
677e602ba25SJulian Elischer 	    p->p_vmspace != NULL) {
6781f7d2501SKirk McKusick 		struct vmspace *vm = p->p_vmspace;
679cd73303cSDavid Greenman 
6801f7d2501SKirk McKusick 		kp->ki_size = vm->vm_map.size;
6811f7d2501SKirk McKusick 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
682ceff7f2aSTim J. Robbins 		FOREACH_THREAD_IN_PROC(p, td0) {
683ceff7f2aSTim J. Robbins 			if (!TD_IS_SWAPPED(td0))
684ceff7f2aSTim J. Robbins 				kp->ki_rssize += td0->td_kstack_pages;
685ceff7f2aSTim J. Robbins 			if (td0->td_altkstack_obj != NULL)
686913fc94dSTim J. Robbins 				kp->ki_rssize += td0->td_altkstack_pages;
687ceff7f2aSTim J. Robbins 		}
6881f7d2501SKirk McKusick 		kp->ki_swrss = vm->vm_swrss;
6891f7d2501SKirk McKusick 		kp->ki_tsize = vm->vm_tsize;
6901f7d2501SKirk McKusick 		kp->ki_dsize = vm->vm_dsize;
6911f7d2501SKirk McKusick 		kp->ki_ssize = vm->vm_ssize;
6925032ff81SDon Lewis 	} else if (p->p_state == PRS_ZOMBIE)
6935032ff81SDon Lewis 		kp->ki_stat = SZOMB;
694cebabef0SPawel Jakub Dawidek 	kp->ki_sflag = p->p_sflag;
695cebabef0SPawel Jakub Dawidek 	kp->ki_swtime = p->p_swtime;
696cebabef0SPawel Jakub Dawidek 	kp->ki_pid = p->p_pid;
697cebabef0SPawel Jakub Dawidek 	kp->ki_nice = p->p_nice;
69878c85e8dSJohn Baldwin 	bintime2timeval(&p->p_rux.rux_runtime, &tv);
699cebabef0SPawel Jakub Dawidek 	kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
7009ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
70178c85e8dSJohn Baldwin 	if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) {
70278c85e8dSJohn Baldwin 		kp->ki_start = p->p_stats->p_start;
70378c85e8dSJohn Baldwin 		timevaladd(&kp->ki_start, &boottime);
70478c85e8dSJohn Baldwin 		kp->ki_rusage = p->p_stats->p_ru;
70578c85e8dSJohn Baldwin 		calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
70678c85e8dSJohn Baldwin 		calccru(p, &kp->ki_childutime, &kp->ki_childstime);
70778c85e8dSJohn Baldwin 
70878c85e8dSJohn Baldwin 		/* Some callers want child-times in a single value */
70978c85e8dSJohn Baldwin 		kp->ki_childtime = kp->ki_childstime;
71078c85e8dSJohn Baldwin 		timevaladd(&kp->ki_childtime, &kp->ki_childutime);
71178c85e8dSJohn Baldwin 	}
712f591779bSSeigo Tanimura 	tp = NULL;
7131f7d2501SKirk McKusick 	if (p->p_pgrp) {
7141f7d2501SKirk McKusick 		kp->ki_pgid = p->p_pgrp->pg_id;
7151f7d2501SKirk McKusick 		kp->ki_jobc = p->p_pgrp->pg_jobc;
7161f7d2501SKirk McKusick 		sp = p->p_pgrp->pg_session;
7171f7d2501SKirk McKusick 
7181f7d2501SKirk McKusick 		if (sp != NULL) {
7191f7d2501SKirk McKusick 			kp->ki_sid = sp->s_sid;
720f591779bSSeigo Tanimura 			SESS_LOCK(sp);
721e80fb434SRobert Drehmel 			strlcpy(kp->ki_login, sp->s_login,
722e80fb434SRobert Drehmel 			    sizeof(kp->ki_login));
7231f7d2501SKirk McKusick 			if (sp->s_ttyvp)
724b8e6bf1eSJohn Baldwin 				kp->ki_kiflag |= KI_CTTY;
7251f7d2501SKirk McKusick 			if (SESS_LEADER(p))
7261f7d2501SKirk McKusick 				kp->ki_kiflag |= KI_SLEADER;
727f591779bSSeigo Tanimura 			tp = sp->s_ttyp;
728f591779bSSeigo Tanimura 			SESS_UNLOCK(sp);
729cd73303cSDavid Greenman 		}
730cd73303cSDavid Greenman 	}
731f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
7321f7d2501SKirk McKusick 		kp->ki_tdev = dev2udev(tp->t_dev);
7331f7d2501SKirk McKusick 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
7341f7d2501SKirk McKusick 		if (tp->t_session)
7351f7d2501SKirk McKusick 			kp->ki_tsid = tp->t_session->s_sid;
736972f9b20SPoul-Henning Kamp 	} else
737f3732fd1SPoul-Henning Kamp 		kp->ki_tdev = NODEV;
73877330eebSPeter Wemm 	if (p->p_comm[0] != '\0') {
739e80fb434SRobert Drehmel 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
740e80fb434SRobert Drehmel 		strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
74162ca2477SJohn Baldwin 	}
742078842c5SGarance A Drosehn 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
743078842c5SGarance A Drosehn 	    p->p_sysent->sv_name[0] != '\0')
744078842c5SGarance A Drosehn 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
7451d9c5696SJuli Mallett 	kp->ki_siglist = p->p_siglist;
7461f7d2501SKirk McKusick 	kp->ki_xstat = p->p_xstat;
7471f7d2501SKirk McKusick 	kp->ki_acflag = p->p_acflag;
7481f7d2501SKirk McKusick 	kp->ki_lock = p->p_lock;
74942a4ed99SJohn Baldwin 	if (p->p_pptr)
75042a4ed99SJohn Baldwin 		kp->ki_ppid = p->p_pptr->p_pid;
751972f9b20SPoul-Henning Kamp }
752972f9b20SPoul-Henning Kamp 
7535032ff81SDon Lewis /*
7545032ff81SDon Lewis  * Fill in information that is thread specific.
7555032ff81SDon Lewis  * Must be called with sched_lock locked.
7565032ff81SDon Lewis  */
7575032ff81SDon Lewis static void
7585032ff81SDon Lewis fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp)
7595032ff81SDon Lewis {
7605032ff81SDon Lewis 	struct ksegrp *kg;
7615032ff81SDon Lewis 	struct proc *p;
7625032ff81SDon Lewis 
7635032ff81SDon Lewis 	p = td->td_proc;
7645032ff81SDon Lewis 
7655032ff81SDon Lewis 	if (td->td_wmesg != NULL)
7665032ff81SDon Lewis 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
7675032ff81SDon Lewis 	else
7685032ff81SDon Lewis 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
7695032ff81SDon Lewis 	if (TD_ON_LOCK(td)) {
7705032ff81SDon Lewis 		kp->ki_kiflag |= KI_LOCKBLOCK;
7715032ff81SDon Lewis 		strlcpy(kp->ki_lockname, td->td_lockname,
7725032ff81SDon Lewis 		    sizeof(kp->ki_lockname));
7735032ff81SDon Lewis 	} else {
7745032ff81SDon Lewis 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
7755032ff81SDon Lewis 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
7765032ff81SDon Lewis 	}
7775032ff81SDon Lewis 
7785032ff81SDon Lewis 	if (p->p_state == PRS_NORMAL) { /*  XXXKSE very approximate */
7795032ff81SDon Lewis 		if (TD_ON_RUNQ(td) ||
7805032ff81SDon Lewis 		    TD_CAN_RUN(td) ||
7815032ff81SDon Lewis 		    TD_IS_RUNNING(td)) {
7825032ff81SDon Lewis 			kp->ki_stat = SRUN;
7835032ff81SDon Lewis 		} else if (P_SHOULDSTOP(p)) {
7845032ff81SDon Lewis 			kp->ki_stat = SSTOP;
7855032ff81SDon Lewis 		} else if (TD_IS_SLEEPING(td)) {
7865032ff81SDon Lewis 			kp->ki_stat = SSLEEP;
7875032ff81SDon Lewis 		} else if (TD_ON_LOCK(td)) {
7885032ff81SDon Lewis 			kp->ki_stat = SLOCK;
7895032ff81SDon Lewis 		} else {
7905032ff81SDon Lewis 			kp->ki_stat = SWAIT;
7915032ff81SDon Lewis 		}
7923357835aSDavid Xu 	} else if (p->p_state == PRS_ZOMBIE) {
7933357835aSDavid Xu 		kp->ki_stat = SZOMB;
7945032ff81SDon Lewis 	} else {
7955032ff81SDon Lewis 		kp->ki_stat = SIDL;
7965032ff81SDon Lewis 	}
7975032ff81SDon Lewis 
7985032ff81SDon Lewis 	kg = td->td_ksegrp;
7995032ff81SDon Lewis 
8005032ff81SDon Lewis 	/* things in the KSE GROUP */
8015032ff81SDon Lewis 	kp->ki_estcpu = kg->kg_estcpu;
8025032ff81SDon Lewis 	kp->ki_slptime = kg->kg_slptime;
8035032ff81SDon Lewis 	kp->ki_pri.pri_user = kg->kg_user_pri;
8045032ff81SDon Lewis 	kp->ki_pri.pri_class = kg->kg_pri_class;
8055032ff81SDon Lewis 
8065032ff81SDon Lewis 	/* Things in the thread */
8075032ff81SDon Lewis 	kp->ki_wchan = td->td_wchan;
8085032ff81SDon Lewis 	kp->ki_pri.pri_level = td->td_priority;
8095032ff81SDon Lewis 	kp->ki_pri.pri_native = td->td_base_pri;
8105032ff81SDon Lewis 	kp->ki_lastcpu = td->td_lastcpu;
8115032ff81SDon Lewis 	kp->ki_oncpu = td->td_oncpu;
8125032ff81SDon Lewis 	kp->ki_tdflags = td->td_flags;
8135032ff81SDon Lewis 	kp->ki_tid = td->td_tid;
8145032ff81SDon Lewis 	kp->ki_numthreads = p->p_numthreads;
8155032ff81SDon Lewis 	kp->ki_pcb = td->td_pcb;
8165032ff81SDon Lewis 	kp->ki_kstack = (void *)td->td_kstack;
8175032ff81SDon Lewis 	kp->ki_pctcpu = sched_pctcpu(td);
8185032ff81SDon Lewis 
8195032ff81SDon Lewis 	/* We can't get this anymore but ps etc never used it anyway. */
8205032ff81SDon Lewis 	kp->ki_rqindex = 0;
8215032ff81SDon Lewis 
8225032ff81SDon Lewis 	SIGSETOR(kp->ki_siglist, td->td_siglist);
8235032ff81SDon Lewis 	kp->ki_sigmask = td->td_sigmask;
8245032ff81SDon Lewis }
8255032ff81SDon Lewis 
8265032ff81SDon Lewis /*
8275032ff81SDon Lewis  * Fill in a kinfo_proc structure for the specified process.
8285032ff81SDon Lewis  * Must be called with the target process locked.
8295032ff81SDon Lewis  */
8305032ff81SDon Lewis void
8315032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
8325032ff81SDon Lewis {
8335032ff81SDon Lewis 
8345032ff81SDon Lewis 	fill_kinfo_proc_only(p, kp);
8355032ff81SDon Lewis 	mtx_lock_spin(&sched_lock);
8365032ff81SDon Lewis 	if (FIRST_THREAD_IN_PROC(p) != NULL)
8375032ff81SDon Lewis 		fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
8385032ff81SDon Lewis 	mtx_unlock_spin(&sched_lock);
8395032ff81SDon Lewis }
8405032ff81SDon Lewis 
8418b059651SDavid Schultz struct pstats *
8428b059651SDavid Schultz pstats_alloc(void)
8438b059651SDavid Schultz {
8448b059651SDavid Schultz 
8458b059651SDavid Schultz 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
8468b059651SDavid Schultz }
8478b059651SDavid Schultz 
8488b059651SDavid Schultz /*
8498b059651SDavid Schultz  * Copy parts of p_stats; zero the rest of p_stats (statistics).
8508b059651SDavid Schultz  */
8518b059651SDavid Schultz void
8528b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst)
8538b059651SDavid Schultz {
8548b059651SDavid Schultz 
8558b059651SDavid Schultz 	bzero(&dst->pstat_startzero,
8566db36923SDavid Schultz 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
8578b059651SDavid Schultz 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
8586db36923SDavid Schultz 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
8598b059651SDavid Schultz }
8608b059651SDavid Schultz 
8618b059651SDavid Schultz void
8628b059651SDavid Schultz pstats_free(struct pstats *ps)
8638b059651SDavid Schultz {
8648b059651SDavid Schultz 
8658b059651SDavid Schultz 	free(ps, M_SUBPROC);
8668b059651SDavid Schultz }
8678b059651SDavid Schultz 
8688b059651SDavid Schultz /*
86942a4ed99SJohn Baldwin  * Locate a zombie process by number
87042a4ed99SJohn Baldwin  */
87142a4ed99SJohn Baldwin struct proc *
8723ce93e4eSPoul-Henning Kamp zpfind(pid_t pid)
8733ce93e4eSPoul-Henning Kamp {
8743ce93e4eSPoul-Henning Kamp 	struct proc *p;
8753ce93e4eSPoul-Henning Kamp 
8761005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8771b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &zombproc, p_list)
87833a9ed9dSJohn Baldwin 		if (p->p_pid == pid) {
87933a9ed9dSJohn Baldwin 			PROC_LOCK(p);
880c0c25570SJake Burkholder 			break;
88133a9ed9dSJohn Baldwin 		}
8821005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
8833ce93e4eSPoul-Henning Kamp 	return (p);
8843ce93e4eSPoul-Henning Kamp }
8853ce93e4eSPoul-Henning Kamp 
88630c6f34eSScott Long #define KERN_PROC_ZOMBMASK	0x3
88730c6f34eSScott Long #define KERN_PROC_NOTHREADS	0x4
8883ce93e4eSPoul-Henning Kamp 
88965c9b430SJohn Baldwin /*
89065c9b430SJohn Baldwin  * Must be called with the process locked and will return with it unlocked.
89165c9b430SJohn Baldwin  */
8923ce93e4eSPoul-Henning Kamp static int
89330c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
8943ce93e4eSPoul-Henning Kamp {
89530c6f34eSScott Long 	struct thread *td;
8961f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc;
89730c6f34eSScott Long 	int error = 0;
89833a9ed9dSJohn Baldwin 	struct proc *np;
8993ce93e4eSPoul-Henning Kamp 	pid_t pid = p->p_pid;
9003ce93e4eSPoul-Henning Kamp 
90165c9b430SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
90230c6f34eSScott Long 
9035032ff81SDon Lewis 	fill_kinfo_proc_only(p, &kinfo_proc);
90430c6f34eSScott Long 	if (flags & KERN_PROC_NOTHREADS) {
9055032ff81SDon Lewis 		mtx_lock_spin(&sched_lock);
9065032ff81SDon Lewis 		if (FIRST_THREAD_IN_PROC(p) != NULL)
9075032ff81SDon Lewis 			fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc);
9085032ff81SDon Lewis 		mtx_unlock_spin(&sched_lock);
90930c6f34eSScott Long 		error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
91030c6f34eSScott Long 				   sizeof(kinfo_proc));
91130c6f34eSScott Long 	} else {
9125032ff81SDon Lewis 		mtx_lock_spin(&sched_lock);
9135032ff81SDon Lewis 		if (FIRST_THREAD_IN_PROC(p) != NULL)
91430c6f34eSScott Long 			FOREACH_THREAD_IN_PROC(p, td) {
91530c6f34eSScott Long 				fill_kinfo_thread(td, &kinfo_proc);
91630c6f34eSScott Long 				error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
91730c6f34eSScott Long 						   sizeof(kinfo_proc));
91830c6f34eSScott Long 				if (error)
91930c6f34eSScott Long 					break;
92030c6f34eSScott Long 			}
9215032ff81SDon Lewis 		else
9225032ff81SDon Lewis 			error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
9235032ff81SDon Lewis 					   sizeof(kinfo_proc));
9245032ff81SDon Lewis 		mtx_unlock_spin(&sched_lock);
92530c6f34eSScott Long 	}
92630c6f34eSScott Long 	PROC_UNLOCK(p);
9273ce93e4eSPoul-Henning Kamp 	if (error)
9283ce93e4eSPoul-Henning Kamp 		return (error);
92930c6f34eSScott Long 	if (flags & KERN_PROC_ZOMBMASK)
93033a9ed9dSJohn Baldwin 		np = zpfind(pid);
93133a9ed9dSJohn Baldwin 	else {
93233a9ed9dSJohn Baldwin 		if (pid == 0)
93333a9ed9dSJohn Baldwin 			return (0);
93433a9ed9dSJohn Baldwin 		np = pfind(pid);
93533a9ed9dSJohn Baldwin 	}
93633a9ed9dSJohn Baldwin 	if (np == NULL)
9373ce93e4eSPoul-Henning Kamp 		return EAGAIN;
93833a9ed9dSJohn Baldwin 	if (np != p) {
93933a9ed9dSJohn Baldwin 		PROC_UNLOCK(np);
9403ce93e4eSPoul-Henning Kamp 		return EAGAIN;
94133a9ed9dSJohn Baldwin 	}
94233a9ed9dSJohn Baldwin 	PROC_UNLOCK(np);
9433ce93e4eSPoul-Henning Kamp 	return (0);
9443ce93e4eSPoul-Henning Kamp }
9453ce93e4eSPoul-Henning Kamp 
946972f9b20SPoul-Henning Kamp static int
94782d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
948972f9b20SPoul-Henning Kamp {
949972f9b20SPoul-Henning Kamp 	int *name = (int*) arg1;
950972f9b20SPoul-Henning Kamp 	u_int namelen = arg2;
951972f9b20SPoul-Henning Kamp 	struct proc *p;
9522648efa6SDaniel Eischen 	int flags, doingzomb, oid_number;
953972f9b20SPoul-Henning Kamp 	int error = 0;
954972f9b20SPoul-Henning Kamp 
9552648efa6SDaniel Eischen 	oid_number = oidp->oid_number;
9562648efa6SDaniel Eischen 	if (oid_number != KERN_PROC_ALL &&
9572648efa6SDaniel Eischen 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
9582648efa6SDaniel Eischen 		flags = KERN_PROC_NOTHREADS;
9592648efa6SDaniel Eischen 	else {
9602648efa6SDaniel Eischen 		flags = 0;
9612648efa6SDaniel Eischen 		oid_number &= ~KERN_PROC_INC_THREAD;
9622648efa6SDaniel Eischen 	}
9632648efa6SDaniel Eischen 	if (oid_number == KERN_PROC_PID) {
9643ce93e4eSPoul-Henning Kamp 		if (namelen != 1)
965972f9b20SPoul-Henning Kamp 			return (EINVAL);
9665032ff81SDon Lewis 		error = sysctl_wire_old_buffer(req, 0);
9675032ff81SDon Lewis 		if (error)
9685032ff81SDon Lewis 			return (error);
9693ce93e4eSPoul-Henning Kamp 		p = pfind((pid_t)name[0]);
9703ce93e4eSPoul-Henning Kamp 		if (!p)
971e76bad96SRobert Drehmel 			return (ESRCH);
972e76bad96SRobert Drehmel 		if ((error = p_cansee(curthread, p))) {
97333a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
974e76bad96SRobert Drehmel 			return (error);
97533a9ed9dSJohn Baldwin 		}
9762648efa6SDaniel Eischen 		error = sysctl_out_proc(p, req, flags);
9773ce93e4eSPoul-Henning Kamp 		return (error);
9783ce93e4eSPoul-Henning Kamp 	}
9793ddaef40STim J. Robbins 
9802648efa6SDaniel Eischen 	switch (oid_number) {
9813ddaef40STim J. Robbins 	case KERN_PROC_ALL:
9823ddaef40STim J. Robbins 		if (namelen != 0)
9833ce93e4eSPoul-Henning Kamp 			return (EINVAL);
9843ddaef40STim J. Robbins 		break;
98525e247afSPeter Wemm 	case KERN_PROC_PROC:
98625e247afSPeter Wemm 		if (namelen != 0 && namelen != 1)
98725e247afSPeter Wemm 			return (EINVAL);
98825e247afSPeter Wemm 		break;
9893ddaef40STim J. Robbins 	default:
9903ddaef40STim J. Robbins 		if (namelen != 1)
9913ddaef40STim J. Robbins 			return (EINVAL);
9923ddaef40STim J. Robbins 		break;
9933ddaef40STim J. Robbins 	}
9943ce93e4eSPoul-Henning Kamp 
995972f9b20SPoul-Henning Kamp 	if (!req->oldptr) {
9963ce93e4eSPoul-Henning Kamp 		/* overestimate by 5 procs */
997972f9b20SPoul-Henning Kamp 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
998972f9b20SPoul-Henning Kamp 		if (error)
999972f9b20SPoul-Henning Kamp 			return (error);
1000972f9b20SPoul-Henning Kamp 	}
100147934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 0);
100247934cefSDon Lewis 	if (error != 0)
100347934cefSDon Lewis 		return (error);
10041005a129SJohn Baldwin 	sx_slock(&allproc_lock);
10053ce93e4eSPoul-Henning Kamp 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
10063ce93e4eSPoul-Henning Kamp 		if (!doingzomb)
10071b727751SPoul-Henning Kamp 			p = LIST_FIRST(&allproc);
10083ce93e4eSPoul-Henning Kamp 		else
10091b727751SPoul-Henning Kamp 			p = LIST_FIRST(&zombproc);
10101b727751SPoul-Henning Kamp 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
101102e878d9SJohn Baldwin 			/*
101202e878d9SJohn Baldwin 			 * Skip embryonic processes.
101302e878d9SJohn Baldwin 			 */
101402e878d9SJohn Baldwin 			mtx_lock_spin(&sched_lock);
101502e878d9SJohn Baldwin 			if (p->p_state == PRS_NEW) {
101602e878d9SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
101702e878d9SJohn Baldwin 				continue;
101802e878d9SJohn Baldwin 			}
101902e878d9SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
102065c9b430SJohn Baldwin 			PROC_LOCK(p);
1021972f9b20SPoul-Henning Kamp 			/*
1022387d2c03SRobert Watson 			 * Show a user only appropriate processes.
102303f808c5SPaul Saab 			 */
1024f44d9e24SJohn Baldwin 			if (p_cansee(curthread, p)) {
102565c9b430SJohn Baldwin 				PROC_UNLOCK(p);
102603f808c5SPaul Saab 				continue;
102765c9b430SJohn Baldwin 			}
102803f808c5SPaul Saab 			/*
1029972f9b20SPoul-Henning Kamp 			 * TODO - make more efficient (see notes below).
1030972f9b20SPoul-Henning Kamp 			 * do by session.
1031972f9b20SPoul-Henning Kamp 			 */
10322648efa6SDaniel Eischen 			switch (oid_number) {
1033972f9b20SPoul-Henning Kamp 
1034078842c5SGarance A Drosehn 			case KERN_PROC_GID:
1035078842c5SGarance A Drosehn 				if (p->p_ucred == NULL ||
1036078842c5SGarance A Drosehn 				    p->p_ucred->cr_gid != (gid_t)name[0]) {
1037078842c5SGarance A Drosehn 					PROC_UNLOCK(p);
1038078842c5SGarance A Drosehn 					continue;
1039078842c5SGarance A Drosehn 				}
1040078842c5SGarance A Drosehn 				break;
1041078842c5SGarance A Drosehn 
1042972f9b20SPoul-Henning Kamp 			case KERN_PROC_PGRP:
1043972f9b20SPoul-Henning Kamp 				/* could do this by traversing pgrp */
10443ce93e4eSPoul-Henning Kamp 				if (p->p_pgrp == NULL ||
1045f591779bSSeigo Tanimura 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1046f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1047972f9b20SPoul-Henning Kamp 					continue;
1048f591779bSSeigo Tanimura 				}
1049972f9b20SPoul-Henning Kamp 				break;
1050972f9b20SPoul-Henning Kamp 
1051b8fdc89dSGarance A Drosehn 			case KERN_PROC_RGID:
1052b8fdc89dSGarance A Drosehn 				if (p->p_ucred == NULL ||
1053b8fdc89dSGarance A Drosehn 				    p->p_ucred->cr_rgid != (gid_t)name[0]) {
1054b8fdc89dSGarance A Drosehn 					PROC_UNLOCK(p);
1055b8fdc89dSGarance A Drosehn 					continue;
1056b8fdc89dSGarance A Drosehn 				}
1057b8fdc89dSGarance A Drosehn 				break;
1058b8fdc89dSGarance A Drosehn 
1059b8fdc89dSGarance A Drosehn 			case KERN_PROC_SESSION:
1060b8fdc89dSGarance A Drosehn 				if (p->p_session == NULL ||
1061b8fdc89dSGarance A Drosehn 				    p->p_session->s_sid != (pid_t)name[0]) {
1062b8fdc89dSGarance A Drosehn 					PROC_UNLOCK(p);
1063b8fdc89dSGarance A Drosehn 					continue;
1064b8fdc89dSGarance A Drosehn 				}
1065b8fdc89dSGarance A Drosehn 				break;
1066b8fdc89dSGarance A Drosehn 
1067972f9b20SPoul-Henning Kamp 			case KERN_PROC_TTY:
1068972f9b20SPoul-Henning Kamp 				if ((p->p_flag & P_CONTROLT) == 0 ||
1069f591779bSSeigo Tanimura 				    p->p_session == NULL) {
1070f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1071972f9b20SPoul-Henning Kamp 					continue;
1072f591779bSSeigo Tanimura 				}
1073f591779bSSeigo Tanimura 				SESS_LOCK(p->p_session);
1074f591779bSSeigo Tanimura 				if (p->p_session->s_ttyp == NULL ||
1075f591779bSSeigo Tanimura 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
1076f3732fd1SPoul-Henning Kamp 				    (dev_t)name[0]) {
1077f591779bSSeigo Tanimura 					SESS_UNLOCK(p->p_session);
1078f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1079f591779bSSeigo Tanimura 					continue;
1080f591779bSSeigo Tanimura 				}
1081f591779bSSeigo Tanimura 				SESS_UNLOCK(p->p_session);
1082972f9b20SPoul-Henning Kamp 				break;
1083972f9b20SPoul-Henning Kamp 
1084972f9b20SPoul-Henning Kamp 			case KERN_PROC_UID:
10853ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
108665c9b430SJohn Baldwin 				    p->p_ucred->cr_uid != (uid_t)name[0]) {
108765c9b430SJohn Baldwin 					PROC_UNLOCK(p);
1088972f9b20SPoul-Henning Kamp 					continue;
108965c9b430SJohn Baldwin 				}
1090972f9b20SPoul-Henning Kamp 				break;
1091972f9b20SPoul-Henning Kamp 
1092972f9b20SPoul-Henning Kamp 			case KERN_PROC_RUID:
10933ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
109465c9b430SJohn Baldwin 				    p->p_ucred->cr_ruid != (uid_t)name[0]) {
109565c9b430SJohn Baldwin 					PROC_UNLOCK(p);
1096972f9b20SPoul-Henning Kamp 					continue;
109765c9b430SJohn Baldwin 				}
1098972f9b20SPoul-Henning Kamp 				break;
109930c6f34eSScott Long 
110030c6f34eSScott Long 			case KERN_PROC_PROC:
110130c6f34eSScott Long 				break;
110230c6f34eSScott Long 
110330c6f34eSScott Long 			default:
110430c6f34eSScott Long 				break;
110530c6f34eSScott Long 
1106972f9b20SPoul-Henning Kamp 			}
1107972f9b20SPoul-Henning Kamp 
110830c6f34eSScott Long 			error = sysctl_out_proc(p, req, flags | doingzomb);
1109553629ebSJake Burkholder 			if (error) {
11101005a129SJohn Baldwin 				sx_sunlock(&allproc_lock);
1111972f9b20SPoul-Henning Kamp 				return (error);
1112972f9b20SPoul-Henning Kamp 			}
1113972f9b20SPoul-Henning Kamp 		}
1114553629ebSJake Burkholder 	}
11151005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
1116972f9b20SPoul-Henning Kamp 	return (0);
1117972f9b20SPoul-Henning Kamp }
1118972f9b20SPoul-Henning Kamp 
1119c1508b28SAlfred Perlstein struct pargs *
1120c1508b28SAlfred Perlstein pargs_alloc(int len)
1121c1508b28SAlfred Perlstein {
1122c1508b28SAlfred Perlstein 	struct pargs *pa;
1123c1508b28SAlfred Perlstein 
1124c1508b28SAlfred Perlstein 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
1125a163d034SWarner Losh 		M_WAITOK);
112655b4a5aeSJohn Baldwin 	refcount_init(&pa->ar_ref, 1);
1127c1508b28SAlfred Perlstein 	pa->ar_length = len;
1128c1508b28SAlfred Perlstein 	return (pa);
1129c1508b28SAlfred Perlstein }
1130c1508b28SAlfred Perlstein 
1131c1508b28SAlfred Perlstein void
1132c1508b28SAlfred Perlstein pargs_free(struct pargs *pa)
1133c1508b28SAlfred Perlstein {
1134c1508b28SAlfred Perlstein 
1135c1508b28SAlfred Perlstein 	FREE(pa, M_PARGS);
1136c1508b28SAlfred Perlstein }
1137c1508b28SAlfred Perlstein 
1138c1508b28SAlfred Perlstein void
1139c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa)
1140c1508b28SAlfred Perlstein {
1141c1508b28SAlfred Perlstein 
1142c1508b28SAlfred Perlstein 	if (pa == NULL)
1143c1508b28SAlfred Perlstein 		return;
114455b4a5aeSJohn Baldwin 	refcount_acquire(&pa->ar_ref);
1145c1508b28SAlfred Perlstein }
1146c1508b28SAlfred Perlstein 
1147c1508b28SAlfred Perlstein void
1148c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa)
1149c1508b28SAlfred Perlstein {
1150c1508b28SAlfred Perlstein 
1151c1508b28SAlfred Perlstein 	if (pa == NULL)
1152c1508b28SAlfred Perlstein 		return;
115355b4a5aeSJohn Baldwin 	if (refcount_release(&pa->ar_ref))
1154c1508b28SAlfred Perlstein 		pargs_free(pa);
1155c1508b28SAlfred Perlstein }
1156c1508b28SAlfred Perlstein 
1157b9df5231SPoul-Henning Kamp /*
1158b9df5231SPoul-Henning Kamp  * This sysctl allows a process to retrieve the argument list or process
1159b9df5231SPoul-Henning Kamp  * title for another process without groping around in the address space
1160b9df5231SPoul-Henning Kamp  * of the other process.  It also allow a process to set its own "process
1161b9df5231SPoul-Henning Kamp  * title to a string of its own choice.
1162b9df5231SPoul-Henning Kamp  */
1163b9df5231SPoul-Henning Kamp static int
116482d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1165b9df5231SPoul-Henning Kamp {
1166b9df5231SPoul-Henning Kamp 	int *name = (int*) arg1;
1167b9df5231SPoul-Henning Kamp 	u_int namelen = arg2;
11688510f2a8SJohn Baldwin 	struct pargs *newpa, *pa;
1169b9df5231SPoul-Henning Kamp 	struct proc *p;
1170b9df5231SPoul-Henning Kamp 	int error = 0;
1171b9df5231SPoul-Henning Kamp 
1172b9df5231SPoul-Henning Kamp 	if (namelen != 1)
1173b9df5231SPoul-Henning Kamp 		return (EINVAL);
1174b9df5231SPoul-Henning Kamp 
1175b9df5231SPoul-Henning Kamp 	p = pfind((pid_t)name[0]);
1176b9df5231SPoul-Henning Kamp 	if (!p)
1177e76bad96SRobert Drehmel 		return (ESRCH);
1178b9df5231SPoul-Henning Kamp 
11799cdb6216SPawel Jakub Dawidek 	if ((error = p_cansee(curthread, p)) != 0) {
118033a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
1181e76bad96SRobert Drehmel 		return (error);
118233a9ed9dSJohn Baldwin 	}
11834bc6471bSJohn Baldwin 
11844bc6471bSJohn Baldwin 	if (req->newptr && curproc != p) {
118533a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
1186b9df5231SPoul-Henning Kamp 		return (EPERM);
11874bc6471bSJohn Baldwin 	}
1188b9df5231SPoul-Henning Kamp 
11897b11fea6SAlfred Perlstein 	pa = p->p_args;
11907b11fea6SAlfred Perlstein 	pargs_hold(pa);
11917b11fea6SAlfred Perlstein 	PROC_UNLOCK(p);
11928510f2a8SJohn Baldwin 	if (req->oldptr != NULL && pa != NULL)
11937b11fea6SAlfred Perlstein 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
11947b11fea6SAlfred Perlstein 	pargs_drop(pa);
11958510f2a8SJohn Baldwin 	if (error != 0 || req->newptr == NULL)
1196b9df5231SPoul-Henning Kamp 		return (error);
1197b9df5231SPoul-Henning Kamp 
1198b9df5231SPoul-Henning Kamp 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
11998510f2a8SJohn Baldwin 		return (ENOMEM);
12004bc6471bSJohn Baldwin 	newpa = pargs_alloc(req->newlen);
12014bc6471bSJohn Baldwin 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
12028510f2a8SJohn Baldwin 	if (error != 0) {
12034bc6471bSJohn Baldwin 		pargs_free(newpa);
1204b9df5231SPoul-Henning Kamp 		return (error);
1205b9df5231SPoul-Henning Kamp 	}
12064bc6471bSJohn Baldwin 	PROC_LOCK(p);
12074bc6471bSJohn Baldwin 	pa = p->p_args;
12084bc6471bSJohn Baldwin 	p->p_args = newpa;
12094bc6471bSJohn Baldwin 	PROC_UNLOCK(p);
12104bc6471bSJohn Baldwin 	pargs_drop(pa);
12114bc6471bSJohn Baldwin 	return (0);
12124bc6471bSJohn Baldwin }
12134bc6471bSJohn Baldwin 
1214fe769cddSDavid Schultz /*
1215fe769cddSDavid Schultz  * This sysctl allows a process to retrieve the path of the executable for
1216fe769cddSDavid Schultz  * itself or another process.
1217fe769cddSDavid Schultz  */
1218fe769cddSDavid Schultz static int
1219fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1220fe769cddSDavid Schultz {
1221fe769cddSDavid Schultz 	pid_t *pidp = (pid_t *)arg1;
1222fe769cddSDavid Schultz 	unsigned int arglen = arg2;
1223fe769cddSDavid Schultz 	struct proc *p;
1224fe769cddSDavid Schultz 	struct vnode *vp;
1225fe769cddSDavid Schultz 	char *retbuf, *freebuf;
1226fe769cddSDavid Schultz 	int error;
1227fe769cddSDavid Schultz 
1228fe769cddSDavid Schultz 	if (arglen != 1)
1229fe769cddSDavid Schultz 		return (EINVAL);
1230fe769cddSDavid Schultz 	if (*pidp == -1) {	/* -1 means this process */
1231fe769cddSDavid Schultz 		p = req->td->td_proc;
1232fe769cddSDavid Schultz 	} else {
1233fe769cddSDavid Schultz 		p = pfind(*pidp);
1234fe769cddSDavid Schultz 		if (p == NULL)
1235fe769cddSDavid Schultz 			return (ESRCH);
1236fe769cddSDavid Schultz 		if ((error = p_cansee(curthread, p)) != 0) {
1237fe769cddSDavid Schultz 			PROC_UNLOCK(p);
1238fe769cddSDavid Schultz 			return (error);
1239fe769cddSDavid Schultz 		}
1240fe769cddSDavid Schultz 	}
1241fe769cddSDavid Schultz 
1242fe769cddSDavid Schultz 	vp = p->p_textvp;
1243fe769cddSDavid Schultz 	vref(vp);
1244fe769cddSDavid Schultz 	if (*pidp != -1)
1245fe769cddSDavid Schultz 		PROC_UNLOCK(p);
1246fe769cddSDavid Schultz 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1247fe769cddSDavid Schultz 	vrele(vp);
1248fe769cddSDavid Schultz 	if (error)
1249fe769cddSDavid Schultz 		return (error);
1250fe769cddSDavid Schultz 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1251fe769cddSDavid Schultz 	free(freebuf, M_TEMP);
1252fe769cddSDavid Schultz 	return (error);
1253fe769cddSDavid Schultz }
1254fe769cddSDavid Schultz 
1255baf731e6SRobert Drehmel static int
1256baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1257baf731e6SRobert Drehmel {
1258baf731e6SRobert Drehmel 	struct proc *p;
1259baf731e6SRobert Drehmel 	char *sv_name;
1260baf731e6SRobert Drehmel 	int *name;
1261baf731e6SRobert Drehmel 	int namelen;
1262e76bad96SRobert Drehmel 	int error;
1263baf731e6SRobert Drehmel 
1264baf731e6SRobert Drehmel 	namelen = arg2;
1265baf731e6SRobert Drehmel 	if (namelen != 1)
1266baf731e6SRobert Drehmel 		return (EINVAL);
1267baf731e6SRobert Drehmel 
1268baf731e6SRobert Drehmel 	name = (int *)arg1;
1269baf731e6SRobert Drehmel 	if ((p = pfind((pid_t)name[0])) == NULL)
1270e76bad96SRobert Drehmel 		return (ESRCH);
1271e76bad96SRobert Drehmel 	if ((error = p_cansee(curthread, p))) {
1272baf731e6SRobert Drehmel 		PROC_UNLOCK(p);
1273e76bad96SRobert Drehmel 		return (error);
1274baf731e6SRobert Drehmel 	}
1275baf731e6SRobert Drehmel 	sv_name = p->p_sysent->sv_name;
1276baf731e6SRobert Drehmel 	PROC_UNLOCK(p);
1277baf731e6SRobert Drehmel 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1278baf731e6SRobert Drehmel }
1279baf731e6SRobert Drehmel 
1280baf731e6SRobert Drehmel 
12810c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
12823ce93e4eSPoul-Henning Kamp 
12833ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
12843d177f46SBill Fumerola 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
12853ce93e4eSPoul-Henning Kamp 
12860c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
1287078842c5SGarance A Drosehn 	sysctl_kern_proc, "Process table");
1288078842c5SGarance A Drosehn 
12890c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
12903ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
12913ce93e4eSPoul-Henning Kamp 
12920c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
1293b8fdc89dSGarance A Drosehn 	sysctl_kern_proc, "Process table");
1294b8fdc89dSGarance A Drosehn 
12950c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
1296b8fdc89dSGarance A Drosehn 	sysctl_kern_proc, "Process table");
1297b8fdc89dSGarance A Drosehn 
12980c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
12993ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
13003ce93e4eSPoul-Henning Kamp 
13010c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
13023ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
13033ce93e4eSPoul-Henning Kamp 
13040c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
13053ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
13063ce93e4eSPoul-Henning Kamp 
13070c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1308972f9b20SPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
1309b9df5231SPoul-Henning Kamp 
13100c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
131130c6f34eSScott Long 	sysctl_kern_proc, "Return process table, no threads");
131230c6f34eSScott Long 
13130c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
13140c898376SPoul-Henning Kamp 	CTLFLAG_RW | CTLFLAG_ANYBODY,
13159b6d9dbaSPoul-Henning Kamp 	sysctl_kern_proc_args, "Process argument list");
1316baf731e6SRobert Drehmel 
1317fe769cddSDavid Schultz static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
1318fe769cddSDavid Schultz 	sysctl_kern_proc_pathname, "Process executable path");
1319fe769cddSDavid Schultz 
13200c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
1321baf731e6SRobert Drehmel 	sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
13222648efa6SDaniel Eischen 
13230c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
1324078842c5SGarance A Drosehn 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1325078842c5SGarance A Drosehn 
13260c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
13272648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13282648efa6SDaniel Eischen 
13290c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
1330b8fdc89dSGarance A Drosehn 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1331b8fdc89dSGarance A Drosehn 
13320c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
13330c898376SPoul-Henning Kamp 	sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table");
13340c898376SPoul-Henning Kamp 
13350c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
1336b8fdc89dSGarance A Drosehn 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1337b8fdc89dSGarance A Drosehn 
13380c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
13392648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13402648efa6SDaniel Eischen 
13410c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
13422648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13432648efa6SDaniel Eischen 
13440c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
13452648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13462648efa6SDaniel Eischen 
13470c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
13482648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");
1349