xref: /freebsd/sys/kern/kern_proc.c (revision ebceaf6dc73f541949cc3ab9f35924d762395692)
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"));
159ed062c8dSJulian Elischer #endif
160316ec49aSScott Long 
161316ec49aSScott Long 	/* Dispose of an alternate kstack, if it exists.
162316ec49aSScott Long 	 * XXX What if there are more than one thread in the proc?
163316ec49aSScott Long 	 *     The first thread in the proc is special and not
164316ec49aSScott Long 	 *     freed, so you gotta do this here.
165316ec49aSScott Long 	 */
166316ec49aSScott Long 	if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
16789f4fca2SAlan Cox 		vm_thread_dispose_altkstack(td);
168ebceaf6dSDavid Xu 	if (p->p_ksi != NULL)
169ebceaf6dSDavid Xu 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
170a136efe9SPeter Wemm }
171a136efe9SPeter Wemm 
172a136efe9SPeter Wemm /*
173a136efe9SPeter Wemm  * Initialize type-stable parts of a proc (when newly created).
174a136efe9SPeter Wemm  */
175b23f72e9SBrian Feldman static int
176b23f72e9SBrian Feldman proc_init(void *mem, int size, int flags)
177a136efe9SPeter Wemm {
178a136efe9SPeter Wemm 	struct proc *p;
1791faf202eSJulian Elischer 	struct thread *td;
1801faf202eSJulian Elischer 	struct ksegrp *kg;
181a136efe9SPeter Wemm 
182a136efe9SPeter Wemm 	p = (struct proc *)mem;
183de028f5aSJeff Roberson 	p->p_sched = (struct p_sched *)&p[1];
1841faf202eSJulian Elischer 	td = thread_alloc();
1854f0db5e0SJulian Elischer 	kg = ksegrp_alloc();
1867d447c95SJohn Baldwin 	bzero(&p->p_mtx, sizeof(struct mtx));
1877d447c95SJohn Baldwin 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1888b059651SDavid Schultz 	p->p_stats = pstats_alloc();
189ed062c8dSJulian Elischer 	proc_linkup(p, kg, td);
190ed062c8dSJulian Elischer 	sched_newproc(p, kg, td);
191b23f72e9SBrian Feldman 	return (0);
192a136efe9SPeter Wemm }
193a136efe9SPeter Wemm 
194a136efe9SPeter Wemm /*
1958daa8c60SDavid Schultz  * UMA should ensure that this function is never called.
1968daa8c60SDavid Schultz  * Freeing a proc structure would violate type stability.
197a136efe9SPeter Wemm  */
198a136efe9SPeter Wemm static void
199a136efe9SPeter Wemm proc_fini(void *mem, int size)
200a136efe9SPeter Wemm {
201f55ab994SJohn Baldwin #ifdef notnow
202f55ab994SJohn Baldwin 	struct proc *p;
203a136efe9SPeter Wemm 
204f55ab994SJohn Baldwin 	p = (struct proc *)mem;
205f55ab994SJohn Baldwin 	pstats_free(p->p_stats);
206f55ab994SJohn Baldwin 	ksegrp_free(FIRST_KSEGRP_IN_PROC(p));
207f55ab994SJohn Baldwin 	thread_free(FIRST_THREAD_IN_PROC(p));
208f55ab994SJohn Baldwin 	mtx_destroy(&p->p_mtx);
209ebceaf6dSDavid Xu 	if (p->p_ksi != NULL)
210ebceaf6dSDavid Xu 		ksiginfo_free(p->p_ksi);
211f55ab994SJohn Baldwin #else
2128daa8c60SDavid Schultz 	panic("proc reclaimed");
213f55ab994SJohn Baldwin #endif
214a136efe9SPeter Wemm }
215a136efe9SPeter Wemm 
216a136efe9SPeter Wemm /*
217df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
218df8bae1dSRodney W. Grimes  */
2191a432a2fSDima Ruban int
220df8bae1dSRodney W. Grimes inferior(p)
221df8bae1dSRodney W. Grimes 	register struct proc *p;
222df8bae1dSRodney W. Grimes {
223df8bae1dSRodney W. Grimes 
2245b29d6e9SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
2255b29d6e9SJohn Baldwin 	for (; p != curproc; p = p->p_pptr)
22600f13cb3SJohn Baldwin 		if (p->p_pid == 0)
22700f13cb3SJohn Baldwin 			return (0);
22800f13cb3SJohn Baldwin 	return (1);
229df8bae1dSRodney W. Grimes }
230df8bae1dSRodney W. Grimes 
231df8bae1dSRodney W. Grimes /*
2326cbea71cSRobert Watson  * Locate a process by number; return only "live" processes -- i.e., neither
2336cbea71cSRobert Watson  * zombies nor newly born but incompletely initialized processes.  By not
2346cbea71cSRobert Watson  * returning processes in the PRS_NEW state, we allow callers to avoid
2356cbea71cSRobert Watson  * testing for that condition to avoid dereferencing p_ucred, et al.
236df8bae1dSRodney W. Grimes  */
237df8bae1dSRodney W. Grimes struct proc *
238df8bae1dSRodney W. Grimes pfind(pid)
239df8bae1dSRodney W. Grimes 	register pid_t pid;
240df8bae1dSRodney W. Grimes {
241df8bae1dSRodney W. Grimes 	register struct proc *p;
242df8bae1dSRodney W. Grimes 
2431005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2441b727751SPoul-Henning Kamp 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
24533a9ed9dSJohn Baldwin 		if (p->p_pid == pid) {
2466cbea71cSRobert Watson 			if (p->p_state == PRS_NEW) {
2476cbea71cSRobert Watson 				p = NULL;
2486cbea71cSRobert Watson 				break;
2496cbea71cSRobert Watson 			}
25033a9ed9dSJohn Baldwin 			PROC_LOCK(p);
251553629ebSJake Burkholder 			break;
25233a9ed9dSJohn Baldwin 		}
25398ab1489SJeffrey Hsu 	sx_sunlock(&allproc_lock);
254df8bae1dSRodney W. Grimes 	return (p);
255df8bae1dSRodney W. Grimes }
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes /*
258f591779bSSeigo Tanimura  * Locate a process group by number.
259f089b570SJohn Baldwin  * The caller must hold proctree_lock.
260df8bae1dSRodney W. Grimes  */
261df8bae1dSRodney W. Grimes struct pgrp *
262df8bae1dSRodney W. Grimes pgfind(pgid)
263df8bae1dSRodney W. Grimes 	register pid_t pgid;
264df8bae1dSRodney W. Grimes {
265df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
266df8bae1dSRodney W. Grimes 
267f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
268f591779bSSeigo Tanimura 
269f591779bSSeigo Tanimura 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
270f591779bSSeigo Tanimura 		if (pgrp->pg_id == pgid) {
271f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
272df8bae1dSRodney W. Grimes 			return (pgrp);
273f591779bSSeigo Tanimura 		}
274f591779bSSeigo Tanimura 	}
275df8bae1dSRodney W. Grimes 	return (NULL);
276df8bae1dSRodney W. Grimes }
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes /*
279f591779bSSeigo Tanimura  * Create a new process group.
280f591779bSSeigo Tanimura  * pgid must be equal to the pid of p.
281f591779bSSeigo Tanimura  * Begin a new session if required.
282df8bae1dSRodney W. Grimes  */
28326f9a767SRodney W. Grimes int
284f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess)
285df8bae1dSRodney W. Grimes 	register struct proc *p;
286df8bae1dSRodney W. Grimes 	pid_t pgid;
287f591779bSSeigo Tanimura 	struct pgrp *pgrp;
288f591779bSSeigo Tanimura 	struct session *sess;
289df8bae1dSRodney W. Grimes {
290f591779bSSeigo Tanimura 	struct pgrp *pgrp2;
291df8bae1dSRodney W. Grimes 
292f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
293f591779bSSeigo Tanimura 
294f591779bSSeigo Tanimura 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
295f591779bSSeigo Tanimura 	KASSERT(p->p_pid == pgid,
296f591779bSSeigo Tanimura 	    ("enterpgrp: new pgrp and pid != pgid"));
297f591779bSSeigo Tanimura 
298f591779bSSeigo Tanimura 	pgrp2 = pgfind(pgid);
299f591779bSSeigo Tanimura 
300f591779bSSeigo Tanimura 	KASSERT(pgrp2 == NULL,
301f591779bSSeigo Tanimura 	    ("enterpgrp: pgrp with pgid exists"));
3025526d2d9SEivind Eklund 	KASSERT(!SESS_LEADER(p),
3035526d2d9SEivind Eklund 	    ("enterpgrp: session leader attempted setpgrp"));
304219cbf59SEivind Eklund 
3056008862bSJohn Baldwin 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
306df8bae1dSRodney W. Grimes 
307f591779bSSeigo Tanimura 	if (sess != NULL) {
308df8bae1dSRodney W. Grimes 		/*
309df8bae1dSRodney W. Grimes 		 * new session
310df8bae1dSRodney W. Grimes 		 */
3116008862bSJohn Baldwin 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
312f591779bSSeigo Tanimura 		PROC_LOCK(p);
313f591779bSSeigo Tanimura 		p->p_flag &= ~P_CONTROLT;
314f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
315f591779bSSeigo Tanimura 		PGRP_LOCK(pgrp);
316df8bae1dSRodney W. Grimes 		sess->s_leader = p;
317643a8daaSDon Lewis 		sess->s_sid = p->p_pid;
318df8bae1dSRodney W. Grimes 		sess->s_count = 1;
319df8bae1dSRodney W. Grimes 		sess->s_ttyvp = NULL;
320df8bae1dSRodney W. Grimes 		sess->s_ttyp = NULL;
321df8bae1dSRodney W. Grimes 		bcopy(p->p_session->s_login, sess->s_login,
322df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
323df8bae1dSRodney W. Grimes 		pgrp->pg_session = sess;
3245526d2d9SEivind Eklund 		KASSERT(p == curproc,
3255526d2d9SEivind Eklund 		    ("enterpgrp: mksession and p != curproc"));
326df8bae1dSRodney W. Grimes 	} else {
327df8bae1dSRodney W. Grimes 		pgrp->pg_session = p->p_session;
328f591779bSSeigo Tanimura 		SESS_LOCK(pgrp->pg_session);
329df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_count++;
330f591779bSSeigo Tanimura 		SESS_UNLOCK(pgrp->pg_session);
331f591779bSSeigo Tanimura 		PGRP_LOCK(pgrp);
332df8bae1dSRodney W. Grimes 	}
333df8bae1dSRodney W. Grimes 	pgrp->pg_id = pgid;
334b75356e1SJeffrey Hsu 	LIST_INIT(&pgrp->pg_members);
335f591779bSSeigo Tanimura 
336f591779bSSeigo Tanimura 	/*
337f089b570SJohn Baldwin 	 * As we have an exclusive lock of proctree_lock,
338f591779bSSeigo Tanimura 	 * this should not deadlock.
339f591779bSSeigo Tanimura 	 */
340b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
341df8bae1dSRodney W. Grimes 	pgrp->pg_jobc = 0;
342831d27a9SDon Lewis 	SLIST_INIT(&pgrp->pg_sigiolst);
343f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
344f591779bSSeigo Tanimura 
345f591779bSSeigo Tanimura 	doenterpgrp(p, pgrp);
346f591779bSSeigo Tanimura 
347df8bae1dSRodney W. Grimes 	return (0);
348f591779bSSeigo Tanimura }
349f591779bSSeigo Tanimura 
350f591779bSSeigo Tanimura /*
351f591779bSSeigo Tanimura  * Move p to an existing process group
352f591779bSSeigo Tanimura  */
353f591779bSSeigo Tanimura int
354f591779bSSeigo Tanimura enterthispgrp(p, pgrp)
355f591779bSSeigo Tanimura 	register struct proc *p;
356f591779bSSeigo Tanimura 	struct pgrp *pgrp;
357f591779bSSeigo Tanimura {
358f089b570SJohn Baldwin 
359f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
360f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
361f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
362f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
363f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
364f591779bSSeigo Tanimura 	KASSERT(pgrp->pg_session == p->p_session,
365f591779bSSeigo Tanimura 		("%s: pgrp's session %p, p->p_session %p.\n",
366f591779bSSeigo Tanimura 		__func__,
367f591779bSSeigo Tanimura 		pgrp->pg_session,
368f591779bSSeigo Tanimura 		p->p_session));
369f591779bSSeigo Tanimura 	KASSERT(pgrp != p->p_pgrp,
370f591779bSSeigo Tanimura 		("%s: p belongs to pgrp.", __func__));
371f591779bSSeigo Tanimura 
372f591779bSSeigo Tanimura 	doenterpgrp(p, pgrp);
373f591779bSSeigo Tanimura 
374f591779bSSeigo Tanimura 	return (0);
375f591779bSSeigo Tanimura }
376f591779bSSeigo Tanimura 
377f591779bSSeigo Tanimura /*
378f591779bSSeigo Tanimura  * Move p to a process group
379f591779bSSeigo Tanimura  */
380f591779bSSeigo Tanimura static void
381f591779bSSeigo Tanimura doenterpgrp(p, pgrp)
382f591779bSSeigo Tanimura 	struct proc *p;
383f591779bSSeigo Tanimura 	struct pgrp *pgrp;
384f591779bSSeigo Tanimura {
385f591779bSSeigo Tanimura 	struct pgrp *savepgrp;
386f591779bSSeigo Tanimura 
387f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
388f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
389f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
390f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
391f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
392f591779bSSeigo Tanimura 
393f591779bSSeigo Tanimura 	savepgrp = p->p_pgrp;
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes 	/*
396df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
397df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
398df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
399df8bae1dSRodney W. Grimes 	 */
400df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
401df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
402df8bae1dSRodney W. Grimes 
403f591779bSSeigo Tanimura 	PGRP_LOCK(pgrp);
404f591779bSSeigo Tanimura 	PGRP_LOCK(savepgrp);
40515e9ec51SJohn Baldwin 	PROC_LOCK(p);
406b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
407df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
40815e9ec51SJohn Baldwin 	PROC_UNLOCK(p);
409f591779bSSeigo Tanimura 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
410f591779bSSeigo Tanimura 	PGRP_UNLOCK(savepgrp);
411f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
412f591779bSSeigo Tanimura 	if (LIST_EMPTY(&savepgrp->pg_members))
413f591779bSSeigo Tanimura 		pgdelete(savepgrp);
414df8bae1dSRodney W. Grimes }
415df8bae1dSRodney W. Grimes 
416df8bae1dSRodney W. Grimes /*
417df8bae1dSRodney W. Grimes  * remove process from process group
418df8bae1dSRodney W. Grimes  */
41926f9a767SRodney W. Grimes int
420df8bae1dSRodney W. Grimes leavepgrp(p)
421df8bae1dSRodney W. Grimes 	register struct proc *p;
422df8bae1dSRodney W. Grimes {
423f591779bSSeigo Tanimura 	struct pgrp *savepgrp;
424df8bae1dSRodney W. Grimes 
425f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
426f591779bSSeigo Tanimura 	savepgrp = p->p_pgrp;
427f591779bSSeigo Tanimura 	PGRP_LOCK(savepgrp);
42815e9ec51SJohn Baldwin 	PROC_LOCK(p);
429b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
43015e9ec51SJohn Baldwin 	p->p_pgrp = NULL;
43115e9ec51SJohn Baldwin 	PROC_UNLOCK(p);
432f591779bSSeigo Tanimura 	PGRP_UNLOCK(savepgrp);
433f591779bSSeigo Tanimura 	if (LIST_EMPTY(&savepgrp->pg_members))
434f591779bSSeigo Tanimura 		pgdelete(savepgrp);
435df8bae1dSRodney W. Grimes 	return (0);
436df8bae1dSRodney W. Grimes }
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes /*
439df8bae1dSRodney W. Grimes  * delete a process group
440df8bae1dSRodney W. Grimes  */
44187b6de2bSPoul-Henning Kamp static void
442df8bae1dSRodney W. Grimes pgdelete(pgrp)
443df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
444df8bae1dSRodney W. Grimes {
445f591779bSSeigo Tanimura 	struct session *savesess;
446f591779bSSeigo Tanimura 
447f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
448f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
449f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
450f591779bSSeigo Tanimura 
451831d27a9SDon Lewis 	/*
452831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
453831d27a9SDon Lewis 	 * F_SETOWN with our pgid.
454831d27a9SDon Lewis 	 */
455831d27a9SDon Lewis 	funsetownlst(&pgrp->pg_sigiolst);
456831d27a9SDon Lewis 
457e649887bSAlfred Perlstein 	PGRP_LOCK(pgrp);
458df8bae1dSRodney W. Grimes 	if (pgrp->pg_session->s_ttyp != NULL &&
459df8bae1dSRodney W. Grimes 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
460df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
461b75356e1SJeffrey Hsu 	LIST_REMOVE(pgrp, pg_hash);
462f591779bSSeigo Tanimura 	savesess = pgrp->pg_session;
463572b4402SPoul-Henning Kamp 	SESSRELE(savesess);
464f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
4656041fa0aSSeigo Tanimura 	mtx_destroy(&pgrp->pg_mtx);
466df8bae1dSRodney W. Grimes 	FREE(pgrp, M_PGRP);
467df8bae1dSRodney W. Grimes }
468df8bae1dSRodney W. Grimes 
46902e878d9SJohn Baldwin static void
47002e878d9SJohn Baldwin pgadjustjobc(pgrp, entering)
47102e878d9SJohn Baldwin 	struct pgrp *pgrp;
47202e878d9SJohn Baldwin 	int entering;
47302e878d9SJohn Baldwin {
47402e878d9SJohn Baldwin 
47502e878d9SJohn Baldwin 	PGRP_LOCK(pgrp);
47602e878d9SJohn Baldwin 	if (entering)
47702e878d9SJohn Baldwin 		pgrp->pg_jobc++;
47802e878d9SJohn Baldwin 	else {
47902e878d9SJohn Baldwin 		--pgrp->pg_jobc;
48002e878d9SJohn Baldwin 		if (pgrp->pg_jobc == 0)
48102e878d9SJohn Baldwin 			orphanpg(pgrp);
48202e878d9SJohn Baldwin 	}
48302e878d9SJohn Baldwin 	PGRP_UNLOCK(pgrp);
48402e878d9SJohn Baldwin }
48502e878d9SJohn Baldwin 
486df8bae1dSRodney W. Grimes /*
487df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
488df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
489df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
490df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
491df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
492df8bae1dSRodney W. Grimes  * process group and that of its children.
493df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
494df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
495df8bae1dSRodney W. Grimes  */
49626f9a767SRodney W. Grimes void
497df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
498df8bae1dSRodney W. Grimes 	register struct proc *p;
499df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
500df8bae1dSRodney W. Grimes 	int entering;
501df8bae1dSRodney W. Grimes {
502df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
503f591779bSSeigo Tanimura 	register struct session *mysession;
504f591779bSSeigo Tanimura 
505f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
506f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
507f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
508f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 	/*
511df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
512df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
513df8bae1dSRodney W. Grimes 	 */
514f591779bSSeigo Tanimura 	mysession = pgrp->pg_session;
515df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
51602e878d9SJohn Baldwin 	    hispgrp->pg_session == mysession)
51702e878d9SJohn Baldwin 		pgadjustjobc(pgrp, entering);
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 	/*
520df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
521df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
522df8bae1dSRodney W. Grimes 	 * process groups.
523df8bae1dSRodney W. Grimes 	 */
524f591779bSSeigo Tanimura 	LIST_FOREACH(p, &p->p_children, p_sibling) {
52502e878d9SJohn Baldwin 		hispgrp = p->p_pgrp;
52602e878d9SJohn Baldwin 		if (hispgrp == pgrp ||
52702e878d9SJohn Baldwin 		    hispgrp->pg_session != mysession)
52802e878d9SJohn Baldwin 			continue;
52902e878d9SJohn Baldwin 		PROC_LOCK(p);
53002e878d9SJohn Baldwin 		if (p->p_state == PRS_ZOMBIE) {
53102e878d9SJohn Baldwin 			PROC_UNLOCK(p);
53202e878d9SJohn Baldwin 			continue;
533df8bae1dSRodney W. Grimes 		}
53402e878d9SJohn Baldwin 		PROC_UNLOCK(p);
53502e878d9SJohn Baldwin 		pgadjustjobc(hispgrp, entering);
536f591779bSSeigo Tanimura 	}
537dfd5dee1SPeter Wemm }
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes /*
540df8bae1dSRodney W. Grimes  * A process group has become orphaned;
541df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
542df8bae1dSRodney W. Grimes  * hang-up all process in that group.
543df8bae1dSRodney W. Grimes  */
544df8bae1dSRodney W. Grimes static void
545df8bae1dSRodney W. Grimes orphanpg(pg)
546df8bae1dSRodney W. Grimes 	struct pgrp *pg;
547df8bae1dSRodney W. Grimes {
548df8bae1dSRodney W. Grimes 	register struct proc *p;
549df8bae1dSRodney W. Grimes 
550f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
551f591779bSSeigo Tanimura 
5521b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
55302e878d9SJohn Baldwin 		PROC_LOCK(p);
554e602ba25SJulian Elischer 		if (P_SHOULDSTOP(p)) {
55502e878d9SJohn Baldwin 			PROC_UNLOCK(p);
5561b727751SPoul-Henning Kamp 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
55715e9ec51SJohn Baldwin 				PROC_LOCK(p);
558df8bae1dSRodney W. Grimes 				psignal(p, SIGHUP);
559df8bae1dSRodney W. Grimes 				psignal(p, SIGCONT);
56015e9ec51SJohn Baldwin 				PROC_UNLOCK(p);
561df8bae1dSRodney W. Grimes 			}
562df8bae1dSRodney W. Grimes 			return;
563df8bae1dSRodney W. Grimes 		}
56402e878d9SJohn Baldwin 		PROC_UNLOCK(p);
565df8bae1dSRodney W. Grimes 	}
566df8bae1dSRodney W. Grimes }
567df8bae1dSRodney W. Grimes 
568572b4402SPoul-Henning Kamp void
569572b4402SPoul-Henning Kamp sessrele(struct session *s)
570572b4402SPoul-Henning Kamp {
571572b4402SPoul-Henning Kamp 	int i;
572572b4402SPoul-Henning Kamp 
573572b4402SPoul-Henning Kamp 	SESS_LOCK(s);
574572b4402SPoul-Henning Kamp 	i = --s->s_count;
575572b4402SPoul-Henning Kamp 	SESS_UNLOCK(s);
576572b4402SPoul-Henning Kamp 	if (i == 0) {
577572b4402SPoul-Henning Kamp 		if (s->s_ttyp != NULL)
578572b4402SPoul-Henning Kamp 			ttyrel(s->s_ttyp);
579572b4402SPoul-Henning Kamp 		mtx_destroy(&s->s_mtx);
580572b4402SPoul-Henning Kamp 		FREE(s, M_SESSION);
581572b4402SPoul-Henning Kamp 	}
582572b4402SPoul-Henning Kamp }
583572b4402SPoul-Henning Kamp 
584831031ceSBruce Evans #include "opt_ddb.h"
585831031ceSBruce Evans #ifdef DDB
586831031ceSBruce Evans #include <ddb/ddb.h>
587831031ceSBruce Evans 
588831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump)
589df8bae1dSRodney W. Grimes {
590df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
591df8bae1dSRodney W. Grimes 	register struct proc *p;
592876a94eeSBruce Evans 	register int i;
593df8bae1dSRodney W. Grimes 
594b75356e1SJeffrey Hsu 	for (i = 0; i <= pgrphash; i++) {
5951b727751SPoul-Henning Kamp 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
596df8bae1dSRodney W. Grimes 			printf("\tindx %d\n", i);
5971b727751SPoul-Henning Kamp 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
598ac1e407bSBruce Evans 				printf(
599ac1e407bSBruce Evans 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
600ac1e407bSBruce Evans 				    (void *)pgrp, (long)pgrp->pg_id,
601ac1e407bSBruce Evans 				    (void *)pgrp->pg_session,
602b75356e1SJeffrey Hsu 				    pgrp->pg_session->s_count,
6031b727751SPoul-Henning Kamp 				    (void *)LIST_FIRST(&pgrp->pg_members));
6041b727751SPoul-Henning Kamp 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
605ac1e407bSBruce Evans 					printf("\t\tpid %ld addr %p pgrp %p\n",
606ac1e407bSBruce Evans 					    (long)p->p_pid, (void *)p,
607ac1e407bSBruce Evans 					    (void *)p->p_pgrp);
608df8bae1dSRodney W. Grimes 				}
609df8bae1dSRodney W. Grimes 			}
610df8bae1dSRodney W. Grimes 		}
611df8bae1dSRodney W. Grimes 	}
612df8bae1dSRodney W. Grimes }
613831031ceSBruce Evans #endif /* DDB */
614972f9b20SPoul-Henning Kamp 
615972f9b20SPoul-Henning Kamp /*
6165032ff81SDon Lewis  * Clear kinfo_proc and fill in any information that is common
6175032ff81SDon Lewis  * to all threads in the process.
61865c9b430SJohn Baldwin  * Must be called with the target process locked.
619972f9b20SPoul-Henning Kamp  */
6205032ff81SDon Lewis static void
6215032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
622972f9b20SPoul-Henning Kamp {
6234093529dSJeff Roberson 	struct thread *td0;
6241f7d2501SKirk McKusick 	struct tty *tp;
6251f7d2501SKirk McKusick 	struct session *sp;
6261cbb9c3bSPoul-Henning Kamp 	struct timeval tv;
627d079d0a0SPawel Jakub Dawidek 	struct ucred *cred;
62890af4afaSJohn Baldwin 	struct sigacts *ps;
629972f9b20SPoul-Henning Kamp 
6301f7d2501SKirk McKusick 	bzero(kp, sizeof(*kp));
631972f9b20SPoul-Henning Kamp 
6321f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(*kp);
6331f7d2501SKirk McKusick 	kp->ki_paddr = p;
63465c9b430SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
635b40ce416SJulian Elischer 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
6361f7d2501SKirk McKusick 	kp->ki_args = p->p_args;
6371f7d2501SKirk McKusick 	kp->ki_textvp = p->p_textvp;
6386c84de02SJohn Baldwin #ifdef KTRACE
639a5881ea5SJohn Baldwin 	kp->ki_tracep = p->p_tracevp;
6406c84de02SJohn Baldwin 	mtx_lock(&ktrace_mtx);
6416c84de02SJohn Baldwin 	kp->ki_traceflag = p->p_traceflag;
6426c84de02SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
6436c84de02SJohn Baldwin #endif
6441f7d2501SKirk McKusick 	kp->ki_fd = p->p_fd;
6451f7d2501SKirk McKusick 	kp->ki_vmspace = p->p_vmspace;
646cefcecbeSPawel Jakub Dawidek 	kp->ki_flag = p->p_flag;
647d079d0a0SPawel Jakub Dawidek 	cred = p->p_ucred;
648d079d0a0SPawel Jakub Dawidek 	if (cred) {
649d079d0a0SPawel Jakub Dawidek 		kp->ki_uid = cred->cr_uid;
650d079d0a0SPawel Jakub Dawidek 		kp->ki_ruid = cred->cr_ruid;
651d079d0a0SPawel Jakub Dawidek 		kp->ki_svuid = cred->cr_svuid;
6520ecd57adSPeter Wemm 		/* XXX bde doesn't like KI_NGROUPS */
653d079d0a0SPawel Jakub Dawidek 		kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
654d079d0a0SPawel Jakub Dawidek 		bcopy(cred->cr_groups, kp->ki_groups,
6550ecd57adSPeter Wemm 		    kp->ki_ngroups * sizeof(gid_t));
656d079d0a0SPawel Jakub Dawidek 		kp->ki_rgid = cred->cr_rgid;
657d079d0a0SPawel Jakub Dawidek 		kp->ki_svgid = cred->cr_svgid;
658cefcecbeSPawel Jakub Dawidek 		/* If jailed(cred), emulate the old P_JAILED flag. */
659c78941e6SPawel Jakub Dawidek 		if (jailed(cred)) {
660cefcecbeSPawel Jakub Dawidek 			kp->ki_flag |= P_JAILED;
661c78941e6SPawel Jakub Dawidek 			/* If inside a jail, use 0 as a jail ID. */
662c78941e6SPawel Jakub Dawidek 			if (!jailed(curthread->td_ucred))
663c78941e6SPawel Jakub Dawidek 				kp->ki_jid = cred->cr_prison->pr_id;
664c78941e6SPawel Jakub Dawidek 		}
665972f9b20SPoul-Henning Kamp 	}
66690af4afaSJohn Baldwin 	ps = p->p_sigacts;
667d079d0a0SPawel Jakub Dawidek 	if (ps) {
66890af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
66990af4afaSJohn Baldwin 		kp->ki_sigignore = ps->ps_sigignore;
67090af4afaSJohn Baldwin 		kp->ki_sigcatch = ps->ps_sigcatch;
67190af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
672d8c85307SJulian Elischer 	}
6739ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
674e602ba25SJulian Elischer 	if (p->p_state != PRS_NEW &&
675e602ba25SJulian Elischer 	    p->p_state != PRS_ZOMBIE &&
676e602ba25SJulian Elischer 	    p->p_vmspace != NULL) {
6771f7d2501SKirk McKusick 		struct vmspace *vm = p->p_vmspace;
678cd73303cSDavid Greenman 
6791f7d2501SKirk McKusick 		kp->ki_size = vm->vm_map.size;
6801f7d2501SKirk McKusick 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
681ceff7f2aSTim J. Robbins 		FOREACH_THREAD_IN_PROC(p, td0) {
682ceff7f2aSTim J. Robbins 			if (!TD_IS_SWAPPED(td0))
683ceff7f2aSTim J. Robbins 				kp->ki_rssize += td0->td_kstack_pages;
684ceff7f2aSTim J. Robbins 			if (td0->td_altkstack_obj != NULL)
685913fc94dSTim J. Robbins 				kp->ki_rssize += td0->td_altkstack_pages;
686ceff7f2aSTim J. Robbins 		}
6871f7d2501SKirk McKusick 		kp->ki_swrss = vm->vm_swrss;
6881f7d2501SKirk McKusick 		kp->ki_tsize = vm->vm_tsize;
6891f7d2501SKirk McKusick 		kp->ki_dsize = vm->vm_dsize;
6901f7d2501SKirk McKusick 		kp->ki_ssize = vm->vm_ssize;
6915032ff81SDon Lewis 	} else if (p->p_state == PRS_ZOMBIE)
6925032ff81SDon Lewis 		kp->ki_stat = SZOMB;
693cebabef0SPawel Jakub Dawidek 	kp->ki_sflag = p->p_sflag;
694cebabef0SPawel Jakub Dawidek 	kp->ki_swtime = p->p_swtime;
695cebabef0SPawel Jakub Dawidek 	kp->ki_pid = p->p_pid;
696cebabef0SPawel Jakub Dawidek 	kp->ki_nice = p->p_nice;
69778c85e8dSJohn Baldwin 	bintime2timeval(&p->p_rux.rux_runtime, &tv);
698cebabef0SPawel Jakub Dawidek 	kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
6999ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
70078c85e8dSJohn Baldwin 	if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) {
70178c85e8dSJohn Baldwin 		kp->ki_start = p->p_stats->p_start;
70278c85e8dSJohn Baldwin 		timevaladd(&kp->ki_start, &boottime);
70378c85e8dSJohn Baldwin 		kp->ki_rusage = p->p_stats->p_ru;
70478c85e8dSJohn Baldwin 		calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
70578c85e8dSJohn Baldwin 		calccru(p, &kp->ki_childutime, &kp->ki_childstime);
70678c85e8dSJohn Baldwin 
70778c85e8dSJohn Baldwin 		/* Some callers want child-times in a single value */
70878c85e8dSJohn Baldwin 		kp->ki_childtime = kp->ki_childstime;
70978c85e8dSJohn Baldwin 		timevaladd(&kp->ki_childtime, &kp->ki_childutime);
71078c85e8dSJohn Baldwin 	}
711f591779bSSeigo Tanimura 	tp = NULL;
7121f7d2501SKirk McKusick 	if (p->p_pgrp) {
7131f7d2501SKirk McKusick 		kp->ki_pgid = p->p_pgrp->pg_id;
7141f7d2501SKirk McKusick 		kp->ki_jobc = p->p_pgrp->pg_jobc;
7151f7d2501SKirk McKusick 		sp = p->p_pgrp->pg_session;
7161f7d2501SKirk McKusick 
7171f7d2501SKirk McKusick 		if (sp != NULL) {
7181f7d2501SKirk McKusick 			kp->ki_sid = sp->s_sid;
719f591779bSSeigo Tanimura 			SESS_LOCK(sp);
720e80fb434SRobert Drehmel 			strlcpy(kp->ki_login, sp->s_login,
721e80fb434SRobert Drehmel 			    sizeof(kp->ki_login));
7221f7d2501SKirk McKusick 			if (sp->s_ttyvp)
723b8e6bf1eSJohn Baldwin 				kp->ki_kiflag |= KI_CTTY;
7241f7d2501SKirk McKusick 			if (SESS_LEADER(p))
7251f7d2501SKirk McKusick 				kp->ki_kiflag |= KI_SLEADER;
726f591779bSSeigo Tanimura 			tp = sp->s_ttyp;
727f591779bSSeigo Tanimura 			SESS_UNLOCK(sp);
728cd73303cSDavid Greenman 		}
729cd73303cSDavid Greenman 	}
730f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
7311f7d2501SKirk McKusick 		kp->ki_tdev = dev2udev(tp->t_dev);
7321f7d2501SKirk McKusick 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
7331f7d2501SKirk McKusick 		if (tp->t_session)
7341f7d2501SKirk McKusick 			kp->ki_tsid = tp->t_session->s_sid;
735972f9b20SPoul-Henning Kamp 	} else
736f3732fd1SPoul-Henning Kamp 		kp->ki_tdev = NODEV;
73777330eebSPeter Wemm 	if (p->p_comm[0] != '\0') {
738e80fb434SRobert Drehmel 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
739e80fb434SRobert Drehmel 		strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
74062ca2477SJohn Baldwin 	}
741078842c5SGarance A Drosehn 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
742078842c5SGarance A Drosehn 	    p->p_sysent->sv_name[0] != '\0')
743078842c5SGarance A Drosehn 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
7441d9c5696SJuli Mallett 	kp->ki_siglist = p->p_siglist;
7451f7d2501SKirk McKusick 	kp->ki_xstat = p->p_xstat;
7461f7d2501SKirk McKusick 	kp->ki_acflag = p->p_acflag;
7471f7d2501SKirk McKusick 	kp->ki_lock = p->p_lock;
74842a4ed99SJohn Baldwin 	if (p->p_pptr)
74942a4ed99SJohn Baldwin 		kp->ki_ppid = p->p_pptr->p_pid;
750972f9b20SPoul-Henning Kamp }
751972f9b20SPoul-Henning Kamp 
7525032ff81SDon Lewis /*
7535032ff81SDon Lewis  * Fill in information that is thread specific.
7545032ff81SDon Lewis  * Must be called with sched_lock locked.
7555032ff81SDon Lewis  */
7565032ff81SDon Lewis static void
7575032ff81SDon Lewis fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp)
7585032ff81SDon Lewis {
7595032ff81SDon Lewis 	struct ksegrp *kg;
7605032ff81SDon Lewis 	struct proc *p;
7615032ff81SDon Lewis 
7625032ff81SDon Lewis 	p = td->td_proc;
7635032ff81SDon Lewis 
7645032ff81SDon Lewis 	if (td->td_wmesg != NULL)
7655032ff81SDon Lewis 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
7665032ff81SDon Lewis 	else
7675032ff81SDon Lewis 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
7685032ff81SDon Lewis 	if (TD_ON_LOCK(td)) {
7695032ff81SDon Lewis 		kp->ki_kiflag |= KI_LOCKBLOCK;
7705032ff81SDon Lewis 		strlcpy(kp->ki_lockname, td->td_lockname,
7715032ff81SDon Lewis 		    sizeof(kp->ki_lockname));
7725032ff81SDon Lewis 	} else {
7735032ff81SDon Lewis 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
7745032ff81SDon Lewis 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
7755032ff81SDon Lewis 	}
7765032ff81SDon Lewis 
7775032ff81SDon Lewis 	if (p->p_state == PRS_NORMAL) { /*  XXXKSE very approximate */
7785032ff81SDon Lewis 		if (TD_ON_RUNQ(td) ||
7795032ff81SDon Lewis 		    TD_CAN_RUN(td) ||
7805032ff81SDon Lewis 		    TD_IS_RUNNING(td)) {
7815032ff81SDon Lewis 			kp->ki_stat = SRUN;
7825032ff81SDon Lewis 		} else if (P_SHOULDSTOP(p)) {
7835032ff81SDon Lewis 			kp->ki_stat = SSTOP;
7845032ff81SDon Lewis 		} else if (TD_IS_SLEEPING(td)) {
7855032ff81SDon Lewis 			kp->ki_stat = SSLEEP;
7865032ff81SDon Lewis 		} else if (TD_ON_LOCK(td)) {
7875032ff81SDon Lewis 			kp->ki_stat = SLOCK;
7885032ff81SDon Lewis 		} else {
7895032ff81SDon Lewis 			kp->ki_stat = SWAIT;
7905032ff81SDon Lewis 		}
7915032ff81SDon Lewis 	} else {
7925032ff81SDon Lewis 		kp->ki_stat = SIDL;
7935032ff81SDon Lewis 	}
7945032ff81SDon Lewis 
7955032ff81SDon Lewis 	kg = td->td_ksegrp;
7965032ff81SDon Lewis 
7975032ff81SDon Lewis 	/* things in the KSE GROUP */
7985032ff81SDon Lewis 	kp->ki_estcpu = kg->kg_estcpu;
7995032ff81SDon Lewis 	kp->ki_slptime = kg->kg_slptime;
8005032ff81SDon Lewis 	kp->ki_pri.pri_user = kg->kg_user_pri;
8015032ff81SDon Lewis 	kp->ki_pri.pri_class = kg->kg_pri_class;
8025032ff81SDon Lewis 
8035032ff81SDon Lewis 	/* Things in the thread */
8045032ff81SDon Lewis 	kp->ki_wchan = td->td_wchan;
8055032ff81SDon Lewis 	kp->ki_pri.pri_level = td->td_priority;
8065032ff81SDon Lewis 	kp->ki_pri.pri_native = td->td_base_pri;
8075032ff81SDon Lewis 	kp->ki_lastcpu = td->td_lastcpu;
8085032ff81SDon Lewis 	kp->ki_oncpu = td->td_oncpu;
8095032ff81SDon Lewis 	kp->ki_tdflags = td->td_flags;
8105032ff81SDon Lewis 	kp->ki_tid = td->td_tid;
8115032ff81SDon Lewis 	kp->ki_numthreads = p->p_numthreads;
8125032ff81SDon Lewis 	kp->ki_pcb = td->td_pcb;
8135032ff81SDon Lewis 	kp->ki_kstack = (void *)td->td_kstack;
8145032ff81SDon Lewis 	kp->ki_pctcpu = sched_pctcpu(td);
8155032ff81SDon Lewis 
8165032ff81SDon Lewis 	/* We can't get this anymore but ps etc never used it anyway. */
8175032ff81SDon Lewis 	kp->ki_rqindex = 0;
8185032ff81SDon Lewis 
8195032ff81SDon Lewis 	SIGSETOR(kp->ki_siglist, td->td_siglist);
8205032ff81SDon Lewis 	kp->ki_sigmask = td->td_sigmask;
8215032ff81SDon Lewis }
8225032ff81SDon Lewis 
8235032ff81SDon Lewis /*
8245032ff81SDon Lewis  * Fill in a kinfo_proc structure for the specified process.
8255032ff81SDon Lewis  * Must be called with the target process locked.
8265032ff81SDon Lewis  */
8275032ff81SDon Lewis void
8285032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
8295032ff81SDon Lewis {
8305032ff81SDon Lewis 
8315032ff81SDon Lewis 	fill_kinfo_proc_only(p, kp);
8325032ff81SDon Lewis 	mtx_lock_spin(&sched_lock);
8335032ff81SDon Lewis 	if (FIRST_THREAD_IN_PROC(p) != NULL)
8345032ff81SDon Lewis 		fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
8355032ff81SDon Lewis 	mtx_unlock_spin(&sched_lock);
8365032ff81SDon Lewis }
8375032ff81SDon Lewis 
8388b059651SDavid Schultz struct pstats *
8398b059651SDavid Schultz pstats_alloc(void)
8408b059651SDavid Schultz {
8418b059651SDavid Schultz 
8428b059651SDavid Schultz 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
8438b059651SDavid Schultz }
8448b059651SDavid Schultz 
8458b059651SDavid Schultz /*
8468b059651SDavid Schultz  * Copy parts of p_stats; zero the rest of p_stats (statistics).
8478b059651SDavid Schultz  */
8488b059651SDavid Schultz void
8498b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst)
8508b059651SDavid Schultz {
8518b059651SDavid Schultz 
8528b059651SDavid Schultz 	bzero(&dst->pstat_startzero,
8536db36923SDavid Schultz 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
8548b059651SDavid Schultz 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
8556db36923SDavid Schultz 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
8568b059651SDavid Schultz }
8578b059651SDavid Schultz 
8588b059651SDavid Schultz void
8598b059651SDavid Schultz pstats_free(struct pstats *ps)
8608b059651SDavid Schultz {
8618b059651SDavid Schultz 
8628b059651SDavid Schultz 	free(ps, M_SUBPROC);
8638b059651SDavid Schultz }
8648b059651SDavid Schultz 
8658b059651SDavid Schultz /*
86642a4ed99SJohn Baldwin  * Locate a zombie process by number
86742a4ed99SJohn Baldwin  */
86842a4ed99SJohn Baldwin struct proc *
8693ce93e4eSPoul-Henning Kamp zpfind(pid_t pid)
8703ce93e4eSPoul-Henning Kamp {
8713ce93e4eSPoul-Henning Kamp 	struct proc *p;
8723ce93e4eSPoul-Henning Kamp 
8731005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8741b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &zombproc, p_list)
87533a9ed9dSJohn Baldwin 		if (p->p_pid == pid) {
87633a9ed9dSJohn Baldwin 			PROC_LOCK(p);
877c0c25570SJake Burkholder 			break;
87833a9ed9dSJohn Baldwin 		}
8791005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
8803ce93e4eSPoul-Henning Kamp 	return (p);
8813ce93e4eSPoul-Henning Kamp }
8823ce93e4eSPoul-Henning Kamp 
88330c6f34eSScott Long #define KERN_PROC_ZOMBMASK	0x3
88430c6f34eSScott Long #define KERN_PROC_NOTHREADS	0x4
8853ce93e4eSPoul-Henning Kamp 
88665c9b430SJohn Baldwin /*
88765c9b430SJohn Baldwin  * Must be called with the process locked and will return with it unlocked.
88865c9b430SJohn Baldwin  */
8893ce93e4eSPoul-Henning Kamp static int
89030c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
8913ce93e4eSPoul-Henning Kamp {
89230c6f34eSScott Long 	struct thread *td;
8931f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc;
89430c6f34eSScott Long 	int error = 0;
89533a9ed9dSJohn Baldwin 	struct proc *np;
8963ce93e4eSPoul-Henning Kamp 	pid_t pid = p->p_pid;
8973ce93e4eSPoul-Henning Kamp 
89865c9b430SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
89930c6f34eSScott Long 
9005032ff81SDon Lewis 	fill_kinfo_proc_only(p, &kinfo_proc);
90130c6f34eSScott Long 	if (flags & KERN_PROC_NOTHREADS) {
9025032ff81SDon Lewis 		mtx_lock_spin(&sched_lock);
9035032ff81SDon Lewis 		if (FIRST_THREAD_IN_PROC(p) != NULL)
9045032ff81SDon Lewis 			fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc);
9055032ff81SDon Lewis 		mtx_unlock_spin(&sched_lock);
90630c6f34eSScott Long 		error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
90730c6f34eSScott Long 				   sizeof(kinfo_proc));
90830c6f34eSScott Long 	} else {
9095032ff81SDon Lewis 		mtx_lock_spin(&sched_lock);
9105032ff81SDon Lewis 		if (FIRST_THREAD_IN_PROC(p) != NULL)
91130c6f34eSScott Long 			FOREACH_THREAD_IN_PROC(p, td) {
91230c6f34eSScott Long 				fill_kinfo_thread(td, &kinfo_proc);
91330c6f34eSScott Long 				error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
91430c6f34eSScott Long 						   sizeof(kinfo_proc));
91530c6f34eSScott Long 				if (error)
91630c6f34eSScott Long 					break;
91730c6f34eSScott Long 			}
9185032ff81SDon Lewis 		else
9195032ff81SDon Lewis 			error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
9205032ff81SDon Lewis 					   sizeof(kinfo_proc));
9215032ff81SDon Lewis 		mtx_unlock_spin(&sched_lock);
92230c6f34eSScott Long 	}
92330c6f34eSScott Long 	PROC_UNLOCK(p);
9243ce93e4eSPoul-Henning Kamp 	if (error)
9253ce93e4eSPoul-Henning Kamp 		return (error);
92630c6f34eSScott Long 	if (flags & KERN_PROC_ZOMBMASK)
92733a9ed9dSJohn Baldwin 		np = zpfind(pid);
92833a9ed9dSJohn Baldwin 	else {
92933a9ed9dSJohn Baldwin 		if (pid == 0)
93033a9ed9dSJohn Baldwin 			return (0);
93133a9ed9dSJohn Baldwin 		np = pfind(pid);
93233a9ed9dSJohn Baldwin 	}
93333a9ed9dSJohn Baldwin 	if (np == NULL)
9343ce93e4eSPoul-Henning Kamp 		return EAGAIN;
93533a9ed9dSJohn Baldwin 	if (np != p) {
93633a9ed9dSJohn Baldwin 		PROC_UNLOCK(np);
9373ce93e4eSPoul-Henning Kamp 		return EAGAIN;
93833a9ed9dSJohn Baldwin 	}
93933a9ed9dSJohn Baldwin 	PROC_UNLOCK(np);
9403ce93e4eSPoul-Henning Kamp 	return (0);
9413ce93e4eSPoul-Henning Kamp }
9423ce93e4eSPoul-Henning Kamp 
943972f9b20SPoul-Henning Kamp static int
94482d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
945972f9b20SPoul-Henning Kamp {
946972f9b20SPoul-Henning Kamp 	int *name = (int*) arg1;
947972f9b20SPoul-Henning Kamp 	u_int namelen = arg2;
948972f9b20SPoul-Henning Kamp 	struct proc *p;
9492648efa6SDaniel Eischen 	int flags, doingzomb, oid_number;
950972f9b20SPoul-Henning Kamp 	int error = 0;
951972f9b20SPoul-Henning Kamp 
9522648efa6SDaniel Eischen 	oid_number = oidp->oid_number;
9532648efa6SDaniel Eischen 	if (oid_number != KERN_PROC_ALL &&
9542648efa6SDaniel Eischen 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
9552648efa6SDaniel Eischen 		flags = KERN_PROC_NOTHREADS;
9562648efa6SDaniel Eischen 	else {
9572648efa6SDaniel Eischen 		flags = 0;
9582648efa6SDaniel Eischen 		oid_number &= ~KERN_PROC_INC_THREAD;
9592648efa6SDaniel Eischen 	}
9602648efa6SDaniel Eischen 	if (oid_number == KERN_PROC_PID) {
9613ce93e4eSPoul-Henning Kamp 		if (namelen != 1)
962972f9b20SPoul-Henning Kamp 			return (EINVAL);
9635032ff81SDon Lewis 		error = sysctl_wire_old_buffer(req, 0);
9645032ff81SDon Lewis 		if (error)
9655032ff81SDon Lewis 			return (error);
9663ce93e4eSPoul-Henning Kamp 		p = pfind((pid_t)name[0]);
9673ce93e4eSPoul-Henning Kamp 		if (!p)
968e76bad96SRobert Drehmel 			return (ESRCH);
969e76bad96SRobert Drehmel 		if ((error = p_cansee(curthread, p))) {
97033a9ed9dSJohn Baldwin 			PROC_UNLOCK(p);
971e76bad96SRobert Drehmel 			return (error);
97233a9ed9dSJohn Baldwin 		}
9732648efa6SDaniel Eischen 		error = sysctl_out_proc(p, req, flags);
9743ce93e4eSPoul-Henning Kamp 		return (error);
9753ce93e4eSPoul-Henning Kamp 	}
9763ddaef40STim J. Robbins 
9772648efa6SDaniel Eischen 	switch (oid_number) {
9783ddaef40STim J. Robbins 	case KERN_PROC_ALL:
9793ddaef40STim J. Robbins 		if (namelen != 0)
9803ce93e4eSPoul-Henning Kamp 			return (EINVAL);
9813ddaef40STim J. Robbins 		break;
98225e247afSPeter Wemm 	case KERN_PROC_PROC:
98325e247afSPeter Wemm 		if (namelen != 0 && namelen != 1)
98425e247afSPeter Wemm 			return (EINVAL);
98525e247afSPeter Wemm 		break;
9863ddaef40STim J. Robbins 	default:
9873ddaef40STim J. Robbins 		if (namelen != 1)
9883ddaef40STim J. Robbins 			return (EINVAL);
9893ddaef40STim J. Robbins 		break;
9903ddaef40STim J. Robbins 	}
9913ce93e4eSPoul-Henning Kamp 
992972f9b20SPoul-Henning Kamp 	if (!req->oldptr) {
9933ce93e4eSPoul-Henning Kamp 		/* overestimate by 5 procs */
994972f9b20SPoul-Henning Kamp 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
995972f9b20SPoul-Henning Kamp 		if (error)
996972f9b20SPoul-Henning Kamp 			return (error);
997972f9b20SPoul-Henning Kamp 	}
99847934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 0);
99947934cefSDon Lewis 	if (error != 0)
100047934cefSDon Lewis 		return (error);
10011005a129SJohn Baldwin 	sx_slock(&allproc_lock);
10023ce93e4eSPoul-Henning Kamp 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
10033ce93e4eSPoul-Henning Kamp 		if (!doingzomb)
10041b727751SPoul-Henning Kamp 			p = LIST_FIRST(&allproc);
10053ce93e4eSPoul-Henning Kamp 		else
10061b727751SPoul-Henning Kamp 			p = LIST_FIRST(&zombproc);
10071b727751SPoul-Henning Kamp 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
100802e878d9SJohn Baldwin 			/*
100902e878d9SJohn Baldwin 			 * Skip embryonic processes.
101002e878d9SJohn Baldwin 			 */
101102e878d9SJohn Baldwin 			mtx_lock_spin(&sched_lock);
101202e878d9SJohn Baldwin 			if (p->p_state == PRS_NEW) {
101302e878d9SJohn Baldwin 				mtx_unlock_spin(&sched_lock);
101402e878d9SJohn Baldwin 				continue;
101502e878d9SJohn Baldwin 			}
101602e878d9SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
101765c9b430SJohn Baldwin 			PROC_LOCK(p);
1018972f9b20SPoul-Henning Kamp 			/*
1019387d2c03SRobert Watson 			 * Show a user only appropriate processes.
102003f808c5SPaul Saab 			 */
1021f44d9e24SJohn Baldwin 			if (p_cansee(curthread, p)) {
102265c9b430SJohn Baldwin 				PROC_UNLOCK(p);
102303f808c5SPaul Saab 				continue;
102465c9b430SJohn Baldwin 			}
102503f808c5SPaul Saab 			/*
1026972f9b20SPoul-Henning Kamp 			 * TODO - make more efficient (see notes below).
1027972f9b20SPoul-Henning Kamp 			 * do by session.
1028972f9b20SPoul-Henning Kamp 			 */
10292648efa6SDaniel Eischen 			switch (oid_number) {
1030972f9b20SPoul-Henning Kamp 
1031078842c5SGarance A Drosehn 			case KERN_PROC_GID:
1032078842c5SGarance A Drosehn 				if (p->p_ucred == NULL ||
1033078842c5SGarance A Drosehn 				    p->p_ucred->cr_gid != (gid_t)name[0]) {
1034078842c5SGarance A Drosehn 					PROC_UNLOCK(p);
1035078842c5SGarance A Drosehn 					continue;
1036078842c5SGarance A Drosehn 				}
1037078842c5SGarance A Drosehn 				break;
1038078842c5SGarance A Drosehn 
1039972f9b20SPoul-Henning Kamp 			case KERN_PROC_PGRP:
1040972f9b20SPoul-Henning Kamp 				/* could do this by traversing pgrp */
10413ce93e4eSPoul-Henning Kamp 				if (p->p_pgrp == NULL ||
1042f591779bSSeigo Tanimura 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1043f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1044972f9b20SPoul-Henning Kamp 					continue;
1045f591779bSSeigo Tanimura 				}
1046972f9b20SPoul-Henning Kamp 				break;
1047972f9b20SPoul-Henning Kamp 
1048b8fdc89dSGarance A Drosehn 			case KERN_PROC_RGID:
1049b8fdc89dSGarance A Drosehn 				if (p->p_ucred == NULL ||
1050b8fdc89dSGarance A Drosehn 				    p->p_ucred->cr_rgid != (gid_t)name[0]) {
1051b8fdc89dSGarance A Drosehn 					PROC_UNLOCK(p);
1052b8fdc89dSGarance A Drosehn 					continue;
1053b8fdc89dSGarance A Drosehn 				}
1054b8fdc89dSGarance A Drosehn 				break;
1055b8fdc89dSGarance A Drosehn 
1056b8fdc89dSGarance A Drosehn 			case KERN_PROC_SESSION:
1057b8fdc89dSGarance A Drosehn 				if (p->p_session == NULL ||
1058b8fdc89dSGarance A Drosehn 				    p->p_session->s_sid != (pid_t)name[0]) {
1059b8fdc89dSGarance A Drosehn 					PROC_UNLOCK(p);
1060b8fdc89dSGarance A Drosehn 					continue;
1061b8fdc89dSGarance A Drosehn 				}
1062b8fdc89dSGarance A Drosehn 				break;
1063b8fdc89dSGarance A Drosehn 
1064972f9b20SPoul-Henning Kamp 			case KERN_PROC_TTY:
1065972f9b20SPoul-Henning Kamp 				if ((p->p_flag & P_CONTROLT) == 0 ||
1066f591779bSSeigo Tanimura 				    p->p_session == NULL) {
1067f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1068972f9b20SPoul-Henning Kamp 					continue;
1069f591779bSSeigo Tanimura 				}
1070f591779bSSeigo Tanimura 				SESS_LOCK(p->p_session);
1071f591779bSSeigo Tanimura 				if (p->p_session->s_ttyp == NULL ||
1072f591779bSSeigo Tanimura 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
1073f3732fd1SPoul-Henning Kamp 				    (dev_t)name[0]) {
1074f591779bSSeigo Tanimura 					SESS_UNLOCK(p->p_session);
1075f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1076f591779bSSeigo Tanimura 					continue;
1077f591779bSSeigo Tanimura 				}
1078f591779bSSeigo Tanimura 				SESS_UNLOCK(p->p_session);
1079972f9b20SPoul-Henning Kamp 				break;
1080972f9b20SPoul-Henning Kamp 
1081972f9b20SPoul-Henning Kamp 			case KERN_PROC_UID:
10823ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
108365c9b430SJohn Baldwin 				    p->p_ucred->cr_uid != (uid_t)name[0]) {
108465c9b430SJohn Baldwin 					PROC_UNLOCK(p);
1085972f9b20SPoul-Henning Kamp 					continue;
108665c9b430SJohn Baldwin 				}
1087972f9b20SPoul-Henning Kamp 				break;
1088972f9b20SPoul-Henning Kamp 
1089972f9b20SPoul-Henning Kamp 			case KERN_PROC_RUID:
10903ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
109165c9b430SJohn Baldwin 				    p->p_ucred->cr_ruid != (uid_t)name[0]) {
109265c9b430SJohn Baldwin 					PROC_UNLOCK(p);
1093972f9b20SPoul-Henning Kamp 					continue;
109465c9b430SJohn Baldwin 				}
1095972f9b20SPoul-Henning Kamp 				break;
109630c6f34eSScott Long 
109730c6f34eSScott Long 			case KERN_PROC_PROC:
109830c6f34eSScott Long 				break;
109930c6f34eSScott Long 
110030c6f34eSScott Long 			default:
110130c6f34eSScott Long 				break;
110230c6f34eSScott Long 
1103972f9b20SPoul-Henning Kamp 			}
1104972f9b20SPoul-Henning Kamp 
110530c6f34eSScott Long 			error = sysctl_out_proc(p, req, flags | doingzomb);
1106553629ebSJake Burkholder 			if (error) {
11071005a129SJohn Baldwin 				sx_sunlock(&allproc_lock);
1108972f9b20SPoul-Henning Kamp 				return (error);
1109972f9b20SPoul-Henning Kamp 			}
1110972f9b20SPoul-Henning Kamp 		}
1111553629ebSJake Burkholder 	}
11121005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
1113972f9b20SPoul-Henning Kamp 	return (0);
1114972f9b20SPoul-Henning Kamp }
1115972f9b20SPoul-Henning Kamp 
1116c1508b28SAlfred Perlstein struct pargs *
1117c1508b28SAlfred Perlstein pargs_alloc(int len)
1118c1508b28SAlfred Perlstein {
1119c1508b28SAlfred Perlstein 	struct pargs *pa;
1120c1508b28SAlfred Perlstein 
1121c1508b28SAlfred Perlstein 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
1122a163d034SWarner Losh 		M_WAITOK);
112355b4a5aeSJohn Baldwin 	refcount_init(&pa->ar_ref, 1);
1124c1508b28SAlfred Perlstein 	pa->ar_length = len;
1125c1508b28SAlfred Perlstein 	return (pa);
1126c1508b28SAlfred Perlstein }
1127c1508b28SAlfred Perlstein 
1128c1508b28SAlfred Perlstein void
1129c1508b28SAlfred Perlstein pargs_free(struct pargs *pa)
1130c1508b28SAlfred Perlstein {
1131c1508b28SAlfred Perlstein 
1132c1508b28SAlfred Perlstein 	FREE(pa, M_PARGS);
1133c1508b28SAlfred Perlstein }
1134c1508b28SAlfred Perlstein 
1135c1508b28SAlfred Perlstein void
1136c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa)
1137c1508b28SAlfred Perlstein {
1138c1508b28SAlfred Perlstein 
1139c1508b28SAlfred Perlstein 	if (pa == NULL)
1140c1508b28SAlfred Perlstein 		return;
114155b4a5aeSJohn Baldwin 	refcount_acquire(&pa->ar_ref);
1142c1508b28SAlfred Perlstein }
1143c1508b28SAlfred Perlstein 
1144c1508b28SAlfred Perlstein void
1145c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa)
1146c1508b28SAlfred Perlstein {
1147c1508b28SAlfred Perlstein 
1148c1508b28SAlfred Perlstein 	if (pa == NULL)
1149c1508b28SAlfred Perlstein 		return;
115055b4a5aeSJohn Baldwin 	if (refcount_release(&pa->ar_ref))
1151c1508b28SAlfred Perlstein 		pargs_free(pa);
1152c1508b28SAlfred Perlstein }
1153c1508b28SAlfred Perlstein 
1154b9df5231SPoul-Henning Kamp /*
1155b9df5231SPoul-Henning Kamp  * This sysctl allows a process to retrieve the argument list or process
1156b9df5231SPoul-Henning Kamp  * title for another process without groping around in the address space
1157b9df5231SPoul-Henning Kamp  * of the other process.  It also allow a process to set its own "process
1158b9df5231SPoul-Henning Kamp  * title to a string of its own choice.
1159b9df5231SPoul-Henning Kamp  */
1160b9df5231SPoul-Henning Kamp static int
116182d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1162b9df5231SPoul-Henning Kamp {
1163b9df5231SPoul-Henning Kamp 	int *name = (int*) arg1;
1164b9df5231SPoul-Henning Kamp 	u_int namelen = arg2;
11658510f2a8SJohn Baldwin 	struct pargs *newpa, *pa;
1166b9df5231SPoul-Henning Kamp 	struct proc *p;
1167b9df5231SPoul-Henning Kamp 	int error = 0;
1168b9df5231SPoul-Henning Kamp 
1169b9df5231SPoul-Henning Kamp 	if (namelen != 1)
1170b9df5231SPoul-Henning Kamp 		return (EINVAL);
1171b9df5231SPoul-Henning Kamp 
1172b9df5231SPoul-Henning Kamp 	p = pfind((pid_t)name[0]);
1173b9df5231SPoul-Henning Kamp 	if (!p)
1174e76bad96SRobert Drehmel 		return (ESRCH);
1175b9df5231SPoul-Henning Kamp 
11769cdb6216SPawel Jakub Dawidek 	if ((error = p_cansee(curthread, p)) != 0) {
117733a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
1178e76bad96SRobert Drehmel 		return (error);
117933a9ed9dSJohn Baldwin 	}
11804bc6471bSJohn Baldwin 
11814bc6471bSJohn Baldwin 	if (req->newptr && curproc != p) {
118233a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
1183b9df5231SPoul-Henning Kamp 		return (EPERM);
11844bc6471bSJohn Baldwin 	}
1185b9df5231SPoul-Henning Kamp 
11867b11fea6SAlfred Perlstein 	pa = p->p_args;
11877b11fea6SAlfred Perlstein 	pargs_hold(pa);
11887b11fea6SAlfred Perlstein 	PROC_UNLOCK(p);
11898510f2a8SJohn Baldwin 	if (req->oldptr != NULL && pa != NULL)
11907b11fea6SAlfred Perlstein 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
11917b11fea6SAlfred Perlstein 	pargs_drop(pa);
11928510f2a8SJohn Baldwin 	if (error != 0 || req->newptr == NULL)
1193b9df5231SPoul-Henning Kamp 		return (error);
1194b9df5231SPoul-Henning Kamp 
1195b9df5231SPoul-Henning Kamp 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
11968510f2a8SJohn Baldwin 		return (ENOMEM);
11974bc6471bSJohn Baldwin 	newpa = pargs_alloc(req->newlen);
11984bc6471bSJohn Baldwin 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
11998510f2a8SJohn Baldwin 	if (error != 0) {
12004bc6471bSJohn Baldwin 		pargs_free(newpa);
1201b9df5231SPoul-Henning Kamp 		return (error);
1202b9df5231SPoul-Henning Kamp 	}
12034bc6471bSJohn Baldwin 	PROC_LOCK(p);
12044bc6471bSJohn Baldwin 	pa = p->p_args;
12054bc6471bSJohn Baldwin 	p->p_args = newpa;
12064bc6471bSJohn Baldwin 	PROC_UNLOCK(p);
12074bc6471bSJohn Baldwin 	pargs_drop(pa);
12084bc6471bSJohn Baldwin 	return (0);
12094bc6471bSJohn Baldwin }
12104bc6471bSJohn Baldwin 
1211fe769cddSDavid Schultz /*
1212fe769cddSDavid Schultz  * This sysctl allows a process to retrieve the path of the executable for
1213fe769cddSDavid Schultz  * itself or another process.
1214fe769cddSDavid Schultz  */
1215fe769cddSDavid Schultz static int
1216fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1217fe769cddSDavid Schultz {
1218fe769cddSDavid Schultz 	pid_t *pidp = (pid_t *)arg1;
1219fe769cddSDavid Schultz 	unsigned int arglen = arg2;
1220fe769cddSDavid Schultz 	struct proc *p;
1221fe769cddSDavid Schultz 	struct vnode *vp;
1222fe769cddSDavid Schultz 	char *retbuf, *freebuf;
1223fe769cddSDavid Schultz 	int error;
1224fe769cddSDavid Schultz 
1225fe769cddSDavid Schultz 	if (arglen != 1)
1226fe769cddSDavid Schultz 		return (EINVAL);
1227fe769cddSDavid Schultz 	if (*pidp == -1) {	/* -1 means this process */
1228fe769cddSDavid Schultz 		p = req->td->td_proc;
1229fe769cddSDavid Schultz 	} else {
1230fe769cddSDavid Schultz 		p = pfind(*pidp);
1231fe769cddSDavid Schultz 		if (p == NULL)
1232fe769cddSDavid Schultz 			return (ESRCH);
1233fe769cddSDavid Schultz 		if ((error = p_cansee(curthread, p)) != 0) {
1234fe769cddSDavid Schultz 			PROC_UNLOCK(p);
1235fe769cddSDavid Schultz 			return (error);
1236fe769cddSDavid Schultz 		}
1237fe769cddSDavid Schultz 	}
1238fe769cddSDavid Schultz 
1239fe769cddSDavid Schultz 	vp = p->p_textvp;
1240fe769cddSDavid Schultz 	vref(vp);
1241fe769cddSDavid Schultz 	if (*pidp != -1)
1242fe769cddSDavid Schultz 		PROC_UNLOCK(p);
1243fe769cddSDavid Schultz 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1244fe769cddSDavid Schultz 	vrele(vp);
1245fe769cddSDavid Schultz 	if (error)
1246fe769cddSDavid Schultz 		return (error);
1247fe769cddSDavid Schultz 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1248fe769cddSDavid Schultz 	free(freebuf, M_TEMP);
1249fe769cddSDavid Schultz 	return (error);
1250fe769cddSDavid Schultz }
1251fe769cddSDavid Schultz 
1252baf731e6SRobert Drehmel static int
1253baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1254baf731e6SRobert Drehmel {
1255baf731e6SRobert Drehmel 	struct proc *p;
1256baf731e6SRobert Drehmel 	char *sv_name;
1257baf731e6SRobert Drehmel 	int *name;
1258baf731e6SRobert Drehmel 	int namelen;
1259e76bad96SRobert Drehmel 	int error;
1260baf731e6SRobert Drehmel 
1261baf731e6SRobert Drehmel 	namelen = arg2;
1262baf731e6SRobert Drehmel 	if (namelen != 1)
1263baf731e6SRobert Drehmel 		return (EINVAL);
1264baf731e6SRobert Drehmel 
1265baf731e6SRobert Drehmel 	name = (int *)arg1;
1266baf731e6SRobert Drehmel 	if ((p = pfind((pid_t)name[0])) == NULL)
1267e76bad96SRobert Drehmel 		return (ESRCH);
1268e76bad96SRobert Drehmel 	if ((error = p_cansee(curthread, p))) {
1269baf731e6SRobert Drehmel 		PROC_UNLOCK(p);
1270e76bad96SRobert Drehmel 		return (error);
1271baf731e6SRobert Drehmel 	}
1272baf731e6SRobert Drehmel 	sv_name = p->p_sysent->sv_name;
1273baf731e6SRobert Drehmel 	PROC_UNLOCK(p);
1274baf731e6SRobert Drehmel 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1275baf731e6SRobert Drehmel }
1276baf731e6SRobert Drehmel 
1277baf731e6SRobert Drehmel 
12780c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
12793ce93e4eSPoul-Henning Kamp 
12803ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
12813d177f46SBill Fumerola 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
12823ce93e4eSPoul-Henning Kamp 
12830c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
1284078842c5SGarance A Drosehn 	sysctl_kern_proc, "Process table");
1285078842c5SGarance A Drosehn 
12860c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
12873ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
12883ce93e4eSPoul-Henning Kamp 
12890c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
1290b8fdc89dSGarance A Drosehn 	sysctl_kern_proc, "Process table");
1291b8fdc89dSGarance A Drosehn 
12920c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
1293b8fdc89dSGarance A Drosehn 	sysctl_kern_proc, "Process table");
1294b8fdc89dSGarance A Drosehn 
12950c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
12963ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
12973ce93e4eSPoul-Henning Kamp 
12980c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
12993ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
13003ce93e4eSPoul-Henning Kamp 
13010c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
13023ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
13033ce93e4eSPoul-Henning Kamp 
13040c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1305972f9b20SPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
1306b9df5231SPoul-Henning Kamp 
13070c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
130830c6f34eSScott Long 	sysctl_kern_proc, "Return process table, no threads");
130930c6f34eSScott Long 
13100c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
13110c898376SPoul-Henning Kamp 	CTLFLAG_RW | CTLFLAG_ANYBODY,
13129b6d9dbaSPoul-Henning Kamp 	sysctl_kern_proc_args, "Process argument list");
1313baf731e6SRobert Drehmel 
1314fe769cddSDavid Schultz static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
1315fe769cddSDavid Schultz 	sysctl_kern_proc_pathname, "Process executable path");
1316fe769cddSDavid Schultz 
13170c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
1318baf731e6SRobert Drehmel 	sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
13192648efa6SDaniel Eischen 
13200c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
1321078842c5SGarance A Drosehn 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1322078842c5SGarance A Drosehn 
13230c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
13242648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13252648efa6SDaniel Eischen 
13260c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
1327b8fdc89dSGarance A Drosehn 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1328b8fdc89dSGarance A Drosehn 
13290c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
13300c898376SPoul-Henning Kamp 	sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table");
13310c898376SPoul-Henning Kamp 
13320c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
1333b8fdc89dSGarance A Drosehn 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1334b8fdc89dSGarance A Drosehn 
13350c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
13362648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13372648efa6SDaniel Eischen 
13380c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
13392648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13402648efa6SDaniel Eischen 
13410c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
13422648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
13432648efa6SDaniel Eischen 
13440c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
13452648efa6SDaniel Eischen 	CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");
1346