xref: /freebsd/sys/kern/kern_proc.c (revision f535380cb6129e205bfa36db821a10ef3c1ac7e2)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33b75356e1SJeffrey Hsu  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/kernel.h>
40972f9b20SPoul-Henning Kamp #include <sys/sysctl.h>
41df8bae1dSRodney W. Grimes #include <sys/malloc.h>
42b9df5231SPoul-Henning Kamp #include <sys/proc.h>
4362d6ce3aSDon Lewis #include <sys/filedesc.h>
44df8bae1dSRodney W. Grimes #include <sys/tty.h>
45bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h>
46efeaf95aSDavid Greenman #include <vm/vm.h>
47996c772fSJohn Dyson #include <sys/lock.h>
48efeaf95aSDavid Greenman #include <vm/pmap.h>
49efeaf95aSDavid Greenman #include <vm/vm_map.h>
50efeaf95aSDavid Greenman #include <sys/user.h>
512d8acc0fSJohn Dyson #include <vm/vm_zone.h>
52df8bae1dSRodney W. Grimes 
53a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
54a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header");
55876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
56a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
5755166637SPoul-Henning Kamp 
58387d2c03SRobert Watson int ps_showallprocs = 1;
5903f808c5SPaul Saab SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
6003f808c5SPaul Saab     &ps_showallprocs, 0, "");
6103f808c5SPaul Saab 
6287b6de2bSPoul-Henning Kamp static void pgdelete	__P((struct pgrp *));
6326f9a767SRodney W. Grimes 
6498d93822SBruce Evans static void	orphanpg __P((struct pgrp *pg));
6598d93822SBruce Evans 
66df8bae1dSRodney W. Grimes /*
67b75356e1SJeffrey Hsu  * Other process lists
68b75356e1SJeffrey Hsu  */
69b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl;
70b75356e1SJeffrey Hsu u_long pidhash;
71b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl;
72b75356e1SJeffrey Hsu u_long pgrphash;
73b75356e1SJeffrey Hsu struct proclist allproc;
74b75356e1SJeffrey Hsu struct proclist zombproc;
752d8acc0fSJohn Dyson vm_zone_t proc_zone;
76b75356e1SJeffrey Hsu 
77b75356e1SJeffrey Hsu /*
78b75356e1SJeffrey Hsu  * Initialize global process hashing structures.
79df8bae1dSRodney W. Grimes  */
8026f9a767SRodney W. Grimes void
81b75356e1SJeffrey Hsu procinit()
82df8bae1dSRodney W. Grimes {
83df8bae1dSRodney W. Grimes 
84b75356e1SJeffrey Hsu 	LIST_INIT(&allproc);
85b75356e1SJeffrey Hsu 	LIST_INIT(&zombproc);
86b75356e1SJeffrey Hsu 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
87b75356e1SJeffrey Hsu 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
882d8acc0fSJohn Dyson 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
89f535380cSDon Lewis 	uihashinit();
90df8bae1dSRodney W. Grimes }
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
94df8bae1dSRodney W. Grimes  */
951a432a2fSDima Ruban int
96df8bae1dSRodney W. Grimes inferior(p)
97df8bae1dSRodney W. Grimes 	register struct proc *p;
98df8bae1dSRodney W. Grimes {
99df8bae1dSRodney W. Grimes 
100df8bae1dSRodney W. Grimes 	for (; p != curproc; p = p->p_pptr)
101df8bae1dSRodney W. Grimes 		if (p->p_pid == 0)
102df8bae1dSRodney W. Grimes 			return (0);
103df8bae1dSRodney W. Grimes 	return (1);
104df8bae1dSRodney W. Grimes }
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes /*
107df8bae1dSRodney W. Grimes  * Locate a process by number
108df8bae1dSRodney W. Grimes  */
109df8bae1dSRodney W. Grimes struct proc *
110df8bae1dSRodney W. Grimes pfind(pid)
111df8bae1dSRodney W. Grimes 	register pid_t pid;
112df8bae1dSRodney W. Grimes {
113df8bae1dSRodney W. Grimes 	register struct proc *p;
114df8bae1dSRodney W. Grimes 
1151b727751SPoul-Henning Kamp 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
116df8bae1dSRodney W. Grimes 		if (p->p_pid == pid)
117df8bae1dSRodney W. Grimes 			return (p);
118df8bae1dSRodney W. Grimes 	return (NULL);
119df8bae1dSRodney W. Grimes }
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * Locate a process group by number
123df8bae1dSRodney W. Grimes  */
124df8bae1dSRodney W. Grimes struct pgrp *
125df8bae1dSRodney W. Grimes pgfind(pgid)
126df8bae1dSRodney W. Grimes 	register pid_t pgid;
127df8bae1dSRodney W. Grimes {
128df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
129df8bae1dSRodney W. Grimes 
1301b727751SPoul-Henning Kamp 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
131df8bae1dSRodney W. Grimes 		if (pgrp->pg_id == pgid)
132df8bae1dSRodney W. Grimes 			return (pgrp);
133df8bae1dSRodney W. Grimes 	return (NULL);
134df8bae1dSRodney W. Grimes }
135df8bae1dSRodney W. Grimes 
136df8bae1dSRodney W. Grimes /*
137df8bae1dSRodney W. Grimes  * Move p to a new or existing process group (and session)
138df8bae1dSRodney W. Grimes  */
13926f9a767SRodney W. Grimes int
140df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess)
141df8bae1dSRodney W. Grimes 	register struct proc *p;
142df8bae1dSRodney W. Grimes 	pid_t pgid;
143df8bae1dSRodney W. Grimes 	int mksess;
144df8bae1dSRodney W. Grimes {
145df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp = pgfind(pgid);
146df8bae1dSRodney W. Grimes 
1475526d2d9SEivind Eklund 	KASSERT(pgrp == NULL || !mksess,
1485526d2d9SEivind Eklund 	    ("enterpgrp: setsid into non-empty pgrp"));
1495526d2d9SEivind Eklund 	KASSERT(!SESS_LEADER(p),
1505526d2d9SEivind Eklund 	    ("enterpgrp: session leader attempted setpgrp"));
151219cbf59SEivind Eklund 
152df8bae1dSRodney W. Grimes 	if (pgrp == NULL) {
153df8bae1dSRodney W. Grimes 		pid_t savepid = p->p_pid;
154df8bae1dSRodney W. Grimes 		struct proc *np;
155df8bae1dSRodney W. Grimes 		/*
156df8bae1dSRodney W. Grimes 		 * new process group
157df8bae1dSRodney W. Grimes 		 */
1585526d2d9SEivind Eklund 		KASSERT(p->p_pid == pgid,
1595526d2d9SEivind Eklund 		    ("enterpgrp: new pgrp and pid != pgid"));
160df8bae1dSRodney W. Grimes 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
161df8bae1dSRodney W. Grimes 		    M_WAITOK);
162df8bae1dSRodney W. Grimes 		if ((np = pfind(savepid)) == NULL || np != p)
163df8bae1dSRodney W. Grimes 			return (ESRCH);
164df8bae1dSRodney W. Grimes 		if (mksess) {
165df8bae1dSRodney W. Grimes 			register struct session *sess;
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 			/*
168df8bae1dSRodney W. Grimes 			 * new session
169df8bae1dSRodney W. Grimes 			 */
170df8bae1dSRodney W. Grimes 			MALLOC(sess, struct session *, sizeof(struct session),
171df8bae1dSRodney W. Grimes 			    M_SESSION, M_WAITOK);
172df8bae1dSRodney W. Grimes 			sess->s_leader = p;
173643a8daaSDon Lewis 			sess->s_sid = p->p_pid;
174df8bae1dSRodney W. Grimes 			sess->s_count = 1;
175df8bae1dSRodney W. Grimes 			sess->s_ttyvp = NULL;
176df8bae1dSRodney W. Grimes 			sess->s_ttyp = NULL;
177df8bae1dSRodney W. Grimes 			bcopy(p->p_session->s_login, sess->s_login,
178df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
179df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_CONTROLT;
180df8bae1dSRodney W. Grimes 			pgrp->pg_session = sess;
1815526d2d9SEivind Eklund 			KASSERT(p == curproc,
1825526d2d9SEivind Eklund 			    ("enterpgrp: mksession and p != curproc"));
183df8bae1dSRodney W. Grimes 		} else {
184df8bae1dSRodney W. Grimes 			pgrp->pg_session = p->p_session;
185df8bae1dSRodney W. Grimes 			pgrp->pg_session->s_count++;
186df8bae1dSRodney W. Grimes 		}
187df8bae1dSRodney W. Grimes 		pgrp->pg_id = pgid;
188b75356e1SJeffrey Hsu 		LIST_INIT(&pgrp->pg_members);
189b75356e1SJeffrey Hsu 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
190df8bae1dSRodney W. Grimes 		pgrp->pg_jobc = 0;
191831d27a9SDon Lewis 		SLIST_INIT(&pgrp->pg_sigiolst);
192df8bae1dSRodney W. Grimes 	} else if (pgrp == p->p_pgrp)
193df8bae1dSRodney W. Grimes 		return (0);
194df8bae1dSRodney W. Grimes 
195df8bae1dSRodney W. Grimes 	/*
196df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
197df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
198df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
199df8bae1dSRodney W. Grimes 	 */
200df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
201df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
202df8bae1dSRodney W. Grimes 
203b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
2041b727751SPoul-Henning Kamp 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
205df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
206df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
207b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
208df8bae1dSRodney W. Grimes 	return (0);
209df8bae1dSRodney W. Grimes }
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes /*
212df8bae1dSRodney W. Grimes  * remove process from process group
213df8bae1dSRodney W. Grimes  */
21426f9a767SRodney W. Grimes int
215df8bae1dSRodney W. Grimes leavepgrp(p)
216df8bae1dSRodney W. Grimes 	register struct proc *p;
217df8bae1dSRodney W. Grimes {
218df8bae1dSRodney W. Grimes 
219b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
2201b727751SPoul-Henning Kamp 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
221df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
222df8bae1dSRodney W. Grimes 	p->p_pgrp = 0;
223df8bae1dSRodney W. Grimes 	return (0);
224df8bae1dSRodney W. Grimes }
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes /*
227df8bae1dSRodney W. Grimes  * delete a process group
228df8bae1dSRodney W. Grimes  */
22987b6de2bSPoul-Henning Kamp static void
230df8bae1dSRodney W. Grimes pgdelete(pgrp)
231df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
232df8bae1dSRodney W. Grimes {
233df8bae1dSRodney W. Grimes 
234831d27a9SDon Lewis 	/*
235831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
236831d27a9SDon Lewis 	 * F_SETOWN with our pgid.
237831d27a9SDon Lewis 	 */
238831d27a9SDon Lewis 	funsetownlst(&pgrp->pg_sigiolst);
239831d27a9SDon Lewis 
240df8bae1dSRodney W. Grimes 	if (pgrp->pg_session->s_ttyp != NULL &&
241df8bae1dSRodney W. Grimes 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
242df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
243b75356e1SJeffrey Hsu 	LIST_REMOVE(pgrp, pg_hash);
244df8bae1dSRodney W. Grimes 	if (--pgrp->pg_session->s_count == 0)
245df8bae1dSRodney W. Grimes 		FREE(pgrp->pg_session, M_SESSION);
246df8bae1dSRodney W. Grimes 	FREE(pgrp, M_PGRP);
247df8bae1dSRodney W. Grimes }
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes /*
250df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
251df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
252df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
253df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
254df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
255df8bae1dSRodney W. Grimes  * process group and that of its children.
256df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
257df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
258df8bae1dSRodney W. Grimes  */
25926f9a767SRodney W. Grimes void
260df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
261df8bae1dSRodney W. Grimes 	register struct proc *p;
262df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
263df8bae1dSRodney W. Grimes 	int entering;
264df8bae1dSRodney W. Grimes {
265df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
266df8bae1dSRodney W. Grimes 	register struct session *mysession = pgrp->pg_session;
267df8bae1dSRodney W. Grimes 
268df8bae1dSRodney W. Grimes 	/*
269df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
270df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
271df8bae1dSRodney W. Grimes 	 */
272df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
273dfd5dee1SPeter Wemm 	    hispgrp->pg_session == mysession) {
274df8bae1dSRodney W. Grimes 		if (entering)
275df8bae1dSRodney W. Grimes 			pgrp->pg_jobc++;
276df8bae1dSRodney W. Grimes 		else if (--pgrp->pg_jobc == 0)
277df8bae1dSRodney W. Grimes 			orphanpg(pgrp);
278dfd5dee1SPeter Wemm 	}
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes 	/*
281df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
282df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
283df8bae1dSRodney W. Grimes 	 * process groups.
284df8bae1dSRodney W. Grimes 	 */
2851b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &p->p_children, p_sibling)
286df8bae1dSRodney W. Grimes 		if ((hispgrp = p->p_pgrp) != pgrp &&
287df8bae1dSRodney W. Grimes 		    hispgrp->pg_session == mysession &&
288dfd5dee1SPeter Wemm 		    p->p_stat != SZOMB) {
289df8bae1dSRodney W. Grimes 			if (entering)
290df8bae1dSRodney W. Grimes 				hispgrp->pg_jobc++;
291df8bae1dSRodney W. Grimes 			else if (--hispgrp->pg_jobc == 0)
292df8bae1dSRodney W. Grimes 				orphanpg(hispgrp);
293df8bae1dSRodney W. Grimes 		}
294dfd5dee1SPeter Wemm }
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes /*
297df8bae1dSRodney W. Grimes  * A process group has become orphaned;
298df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
299df8bae1dSRodney W. Grimes  * hang-up all process in that group.
300df8bae1dSRodney W. Grimes  */
301df8bae1dSRodney W. Grimes static void
302df8bae1dSRodney W. Grimes orphanpg(pg)
303df8bae1dSRodney W. Grimes 	struct pgrp *pg;
304df8bae1dSRodney W. Grimes {
305df8bae1dSRodney W. Grimes 	register struct proc *p;
306df8bae1dSRodney W. Grimes 
3071b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
308df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP) {
3091b727751SPoul-Henning Kamp 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
310df8bae1dSRodney W. Grimes 				psignal(p, SIGHUP);
311df8bae1dSRodney W. Grimes 				psignal(p, SIGCONT);
312df8bae1dSRodney W. Grimes 			}
313df8bae1dSRodney W. Grimes 			return;
314df8bae1dSRodney W. Grimes 		}
315df8bae1dSRodney W. Grimes 	}
316df8bae1dSRodney W. Grimes }
317df8bae1dSRodney W. Grimes 
318831031ceSBruce Evans #include "opt_ddb.h"
319831031ceSBruce Evans #ifdef DDB
320831031ceSBruce Evans #include <ddb/ddb.h>
321831031ceSBruce Evans 
322831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump)
323df8bae1dSRodney W. Grimes {
324df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
325df8bae1dSRodney W. Grimes 	register struct proc *p;
326876a94eeSBruce Evans 	register int i;
327df8bae1dSRodney W. Grimes 
328b75356e1SJeffrey Hsu 	for (i = 0; i <= pgrphash; i++) {
3291b727751SPoul-Henning Kamp 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
330df8bae1dSRodney W. Grimes 			printf("\tindx %d\n", i);
3311b727751SPoul-Henning Kamp 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
332ac1e407bSBruce Evans 				printf(
333ac1e407bSBruce Evans 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
334ac1e407bSBruce Evans 				    (void *)pgrp, (long)pgrp->pg_id,
335ac1e407bSBruce Evans 				    (void *)pgrp->pg_session,
336b75356e1SJeffrey Hsu 				    pgrp->pg_session->s_count,
3371b727751SPoul-Henning Kamp 				    (void *)LIST_FIRST(&pgrp->pg_members));
3381b727751SPoul-Henning Kamp 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
339ac1e407bSBruce Evans 					printf("\t\tpid %ld addr %p pgrp %p\n",
340ac1e407bSBruce Evans 					    (long)p->p_pid, (void *)p,
341ac1e407bSBruce Evans 					    (void *)p->p_pgrp);
342df8bae1dSRodney W. Grimes 				}
343df8bae1dSRodney W. Grimes 			}
344df8bae1dSRodney W. Grimes 		}
345df8bae1dSRodney W. Grimes 	}
346df8bae1dSRodney W. Grimes }
347831031ceSBruce Evans #endif /* DDB */
348972f9b20SPoul-Henning Kamp 
349972f9b20SPoul-Henning Kamp /*
350972f9b20SPoul-Henning Kamp  * Fill in an eproc structure for the specified process.
351972f9b20SPoul-Henning Kamp  */
352972f9b20SPoul-Henning Kamp void
353972f9b20SPoul-Henning Kamp fill_eproc(p, ep)
354972f9b20SPoul-Henning Kamp 	register struct proc *p;
355972f9b20SPoul-Henning Kamp 	register struct eproc *ep;
356972f9b20SPoul-Henning Kamp {
357972f9b20SPoul-Henning Kamp 	register struct tty *tp;
358972f9b20SPoul-Henning Kamp 
359972f9b20SPoul-Henning Kamp 	bzero(ep, sizeof(*ep));
360972f9b20SPoul-Henning Kamp 
361972f9b20SPoul-Henning Kamp 	ep->e_paddr = p;
362972f9b20SPoul-Henning Kamp 	if (p->p_cred) {
363972f9b20SPoul-Henning Kamp 		ep->e_pcred = *p->p_cred;
364972f9b20SPoul-Henning Kamp 		if (p->p_ucred)
365972f9b20SPoul-Henning Kamp 			ep->e_ucred = *p->p_ucred;
366972f9b20SPoul-Henning Kamp 	}
367d8c85307SJulian Elischer 	if (p->p_procsig) {
368d8c85307SJulian Elischer 		ep->e_procsig = *p->p_procsig;
369d8c85307SJulian Elischer 	}
370972f9b20SPoul-Henning Kamp 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
371972f9b20SPoul-Henning Kamp 		register struct vmspace *vm = p->p_vmspace;
372b1028ad1SLuoqi Chen 		ep->e_vm = *vm;
373b1028ad1SLuoqi Chen 		ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
374972f9b20SPoul-Henning Kamp 	}
375e29e202cSPeter Wemm 	if ((p->p_flag & P_INMEM) && p->p_stats)
376e29e202cSPeter Wemm 		ep->e_stats = *p->p_stats;
377972f9b20SPoul-Henning Kamp 	if (p->p_pptr)
378972f9b20SPoul-Henning Kamp 		ep->e_ppid = p->p_pptr->p_pid;
379972f9b20SPoul-Henning Kamp 	if (p->p_pgrp) {
380972f9b20SPoul-Henning Kamp 		ep->e_pgid = p->p_pgrp->pg_id;
381972f9b20SPoul-Henning Kamp 		ep->e_jobc = p->p_pgrp->pg_jobc;
382cd73303cSDavid Greenman 		ep->e_sess = p->p_pgrp->pg_session;
383cd73303cSDavid Greenman 
384cd73303cSDavid Greenman 		if (ep->e_sess) {
3858735cebcSPeter Wemm 			bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
386cd73303cSDavid Greenman 			if (ep->e_sess->s_ttyvp)
387cd73303cSDavid Greenman 				ep->e_flag = EPROC_CTTY;
388cd73303cSDavid Greenman 			if (p->p_session && SESS_LEADER(p))
389cd73303cSDavid Greenman 				ep->e_flag |= EPROC_SLEADER;
390cd73303cSDavid Greenman 		}
391cd73303cSDavid Greenman 	}
392972f9b20SPoul-Henning Kamp 	if ((p->p_flag & P_CONTROLT) &&
393972f9b20SPoul-Henning Kamp 	    (ep->e_sess != NULL) &&
394972f9b20SPoul-Henning Kamp 	    ((tp = ep->e_sess->s_ttyp) != NULL)) {
395f40ddd55SDoug Rabson 		ep->e_tdev = dev2udev(tp->t_dev);
396972f9b20SPoul-Henning Kamp 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
397972f9b20SPoul-Henning Kamp 		ep->e_tsess = tp->t_session;
398972f9b20SPoul-Henning Kamp 	} else
3996a0ce002SPoul-Henning Kamp 		ep->e_tdev = NOUDEV;
400972f9b20SPoul-Henning Kamp 	if (p->p_wmesg) {
401972f9b20SPoul-Henning Kamp 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
402972f9b20SPoul-Henning Kamp 		ep->e_wmesg[WMESGLEN] = 0;
403972f9b20SPoul-Henning Kamp 	}
404972f9b20SPoul-Henning Kamp }
405972f9b20SPoul-Henning Kamp 
4063ce93e4eSPoul-Henning Kamp static struct proc *
4073ce93e4eSPoul-Henning Kamp zpfind(pid_t pid)
4083ce93e4eSPoul-Henning Kamp {
4093ce93e4eSPoul-Henning Kamp 	struct proc *p;
4103ce93e4eSPoul-Henning Kamp 
4111b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &zombproc, p_list)
4123ce93e4eSPoul-Henning Kamp 		if (p->p_pid == pid)
4133ce93e4eSPoul-Henning Kamp 			return (p);
4143ce93e4eSPoul-Henning Kamp 	return (NULL);
4153ce93e4eSPoul-Henning Kamp }
4163ce93e4eSPoul-Henning Kamp 
4173ce93e4eSPoul-Henning Kamp 
4183ce93e4eSPoul-Henning Kamp static int
4193ce93e4eSPoul-Henning Kamp sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
4203ce93e4eSPoul-Henning Kamp {
4213ce93e4eSPoul-Henning Kamp 	struct eproc eproc;
4223ce93e4eSPoul-Henning Kamp 	int error;
4233ce93e4eSPoul-Henning Kamp 	pid_t pid = p->p_pid;
4243ce93e4eSPoul-Henning Kamp 
4253ce93e4eSPoul-Henning Kamp 	fill_eproc(p, &eproc);
4263ce93e4eSPoul-Henning Kamp 	error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc));
4273ce93e4eSPoul-Henning Kamp 	if (error)
4283ce93e4eSPoul-Henning Kamp 		return (error);
4293ce93e4eSPoul-Henning Kamp 	error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
4303ce93e4eSPoul-Henning Kamp 	if (error)
4313ce93e4eSPoul-Henning Kamp 		return (error);
4323ce93e4eSPoul-Henning Kamp 	if (!doingzomb && pid && (pfind(pid) != p))
4333ce93e4eSPoul-Henning Kamp 		return EAGAIN;
4343ce93e4eSPoul-Henning Kamp 	if (doingzomb && zpfind(pid) != p)
4353ce93e4eSPoul-Henning Kamp 		return EAGAIN;
4363ce93e4eSPoul-Henning Kamp 	return (0);
4373ce93e4eSPoul-Henning Kamp }
4383ce93e4eSPoul-Henning Kamp 
439972f9b20SPoul-Henning Kamp static int
44082d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
441972f9b20SPoul-Henning Kamp {
442972f9b20SPoul-Henning Kamp 	int *name = (int*) arg1;
443972f9b20SPoul-Henning Kamp 	u_int namelen = arg2;
444972f9b20SPoul-Henning Kamp 	struct proc *p;
445972f9b20SPoul-Henning Kamp 	int doingzomb;
446972f9b20SPoul-Henning Kamp 	int error = 0;
447972f9b20SPoul-Henning Kamp 
4483ce93e4eSPoul-Henning Kamp 	if (oidp->oid_number == KERN_PROC_PID) {
4493ce93e4eSPoul-Henning Kamp 		if (namelen != 1)
450972f9b20SPoul-Henning Kamp 			return (EINVAL);
4513ce93e4eSPoul-Henning Kamp 		p = pfind((pid_t)name[0]);
4523ce93e4eSPoul-Henning Kamp 		if (!p)
4533ce93e4eSPoul-Henning Kamp 			return (0);
454387d2c03SRobert Watson 		if (p_can(curproc, p, P_CAN_SEE, NULL))
45575c13541SPoul-Henning Kamp 			return (0);
4563ce93e4eSPoul-Henning Kamp 		error = sysctl_out_proc(p, req, 0);
4573ce93e4eSPoul-Henning Kamp 		return (error);
4583ce93e4eSPoul-Henning Kamp 	}
4593ce93e4eSPoul-Henning Kamp 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
4603ce93e4eSPoul-Henning Kamp 		;
4613ce93e4eSPoul-Henning Kamp 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
4623ce93e4eSPoul-Henning Kamp 		;
4633ce93e4eSPoul-Henning Kamp 	else
4643ce93e4eSPoul-Henning Kamp 		return (EINVAL);
4653ce93e4eSPoul-Henning Kamp 
466972f9b20SPoul-Henning Kamp 	if (!req->oldptr) {
4673ce93e4eSPoul-Henning Kamp 		/* overestimate by 5 procs */
468972f9b20SPoul-Henning Kamp 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
469972f9b20SPoul-Henning Kamp 		if (error)
470972f9b20SPoul-Henning Kamp 			return (error);
471972f9b20SPoul-Henning Kamp 	}
4723ce93e4eSPoul-Henning Kamp 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
4733ce93e4eSPoul-Henning Kamp 		if (!doingzomb)
4741b727751SPoul-Henning Kamp 			p = LIST_FIRST(&allproc);
4753ce93e4eSPoul-Henning Kamp 		else
4761b727751SPoul-Henning Kamp 			p = LIST_FIRST(&zombproc);
4771b727751SPoul-Henning Kamp 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
478972f9b20SPoul-Henning Kamp 			/*
479387d2c03SRobert Watson 			 * Show a user only appropriate processes.
48003f808c5SPaul Saab 			 */
481387d2c03SRobert Watson 			if (p_can(curproc, p, P_CAN_SEE, NULL))
48203f808c5SPaul Saab 				continue;
48303f808c5SPaul Saab 			/*
484972f9b20SPoul-Henning Kamp 			 * Skip embryonic processes.
485972f9b20SPoul-Henning Kamp 			 */
486972f9b20SPoul-Henning Kamp 			if (p->p_stat == SIDL)
487972f9b20SPoul-Henning Kamp 				continue;
488972f9b20SPoul-Henning Kamp 			/*
489972f9b20SPoul-Henning Kamp 			 * TODO - make more efficient (see notes below).
490972f9b20SPoul-Henning Kamp 			 * do by session.
491972f9b20SPoul-Henning Kamp 			 */
4923ce93e4eSPoul-Henning Kamp 			switch (oidp->oid_number) {
493972f9b20SPoul-Henning Kamp 
494972f9b20SPoul-Henning Kamp 			case KERN_PROC_PGRP:
495972f9b20SPoul-Henning Kamp 				/* could do this by traversing pgrp */
4963ce93e4eSPoul-Henning Kamp 				if (p->p_pgrp == NULL ||
4973ce93e4eSPoul-Henning Kamp 				    p->p_pgrp->pg_id != (pid_t)name[0])
498972f9b20SPoul-Henning Kamp 					continue;
499972f9b20SPoul-Henning Kamp 				break;
500972f9b20SPoul-Henning Kamp 
501972f9b20SPoul-Henning Kamp 			case KERN_PROC_TTY:
502972f9b20SPoul-Henning Kamp 				if ((p->p_flag & P_CONTROLT) == 0 ||
503972f9b20SPoul-Henning Kamp 				    p->p_session == NULL ||
504972f9b20SPoul-Henning Kamp 				    p->p_session->s_ttyp == NULL ||
50568f7448fSPoul-Henning Kamp 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
50668f7448fSPoul-Henning Kamp 					(udev_t)name[0])
507972f9b20SPoul-Henning Kamp 					continue;
508972f9b20SPoul-Henning Kamp 				break;
509972f9b20SPoul-Henning Kamp 
510972f9b20SPoul-Henning Kamp 			case KERN_PROC_UID:
5113ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
5123ce93e4eSPoul-Henning Kamp 				    p->p_ucred->cr_uid != (uid_t)name[0])
513972f9b20SPoul-Henning Kamp 					continue;
514972f9b20SPoul-Henning Kamp 				break;
515972f9b20SPoul-Henning Kamp 
516972f9b20SPoul-Henning Kamp 			case KERN_PROC_RUID:
5173ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
5183ce93e4eSPoul-Henning Kamp 				    p->p_cred->p_ruid != (uid_t)name[0])
519972f9b20SPoul-Henning Kamp 					continue;
520972f9b20SPoul-Henning Kamp 				break;
521972f9b20SPoul-Henning Kamp 			}
522972f9b20SPoul-Henning Kamp 
523387d2c03SRobert Watson 			if (p_can(curproc, p, P_CAN_SEE, NULL))
52475c13541SPoul-Henning Kamp 				continue;
52575c13541SPoul-Henning Kamp 
5263ce93e4eSPoul-Henning Kamp 			error = sysctl_out_proc(p, req, doingzomb);
527972f9b20SPoul-Henning Kamp 			if (error)
528972f9b20SPoul-Henning Kamp 				return (error);
529972f9b20SPoul-Henning Kamp 		}
530972f9b20SPoul-Henning Kamp 	}
531972f9b20SPoul-Henning Kamp 	return (0);
532972f9b20SPoul-Henning Kamp }
533972f9b20SPoul-Henning Kamp 
534b9df5231SPoul-Henning Kamp /*
535b9df5231SPoul-Henning Kamp  * This sysctl allows a process to retrieve the argument list or process
536b9df5231SPoul-Henning Kamp  * title for another process without groping around in the address space
537b9df5231SPoul-Henning Kamp  * of the other process.  It also allow a process to set its own "process
538b9df5231SPoul-Henning Kamp  * title to a string of its own choice.
539b9df5231SPoul-Henning Kamp  */
540b9df5231SPoul-Henning Kamp static int
54182d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
542b9df5231SPoul-Henning Kamp {
543b9df5231SPoul-Henning Kamp 	int *name = (int*) arg1;
544b9df5231SPoul-Henning Kamp 	u_int namelen = arg2;
545b9df5231SPoul-Henning Kamp 	struct proc *p;
546b9df5231SPoul-Henning Kamp 	struct pargs *pa;
547b9df5231SPoul-Henning Kamp 	int error = 0;
548b9df5231SPoul-Henning Kamp 
549b9df5231SPoul-Henning Kamp 	if (namelen != 1)
550b9df5231SPoul-Henning Kamp 		return (EINVAL);
551b9df5231SPoul-Henning Kamp 
552b9df5231SPoul-Henning Kamp 	p = pfind((pid_t)name[0]);
553b9df5231SPoul-Henning Kamp 	if (!p)
554b9df5231SPoul-Henning Kamp 		return (0);
555b9df5231SPoul-Henning Kamp 
556387d2c03SRobert Watson 	if ((!ps_argsopen) && p_can(curproc, p, P_CAN_SEE, NULL))
557b9df5231SPoul-Henning Kamp 		return (0);
558b9df5231SPoul-Henning Kamp 
559b9df5231SPoul-Henning Kamp 	if (req->newptr && curproc != p)
560b9df5231SPoul-Henning Kamp 		return (EPERM);
561b9df5231SPoul-Henning Kamp 
562b9df5231SPoul-Henning Kamp 	if (req->oldptr && p->p_args != NULL)
563b9df5231SPoul-Henning Kamp 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
564b9df5231SPoul-Henning Kamp 	if (req->newptr == NULL)
565b9df5231SPoul-Henning Kamp 		return (error);
566b9df5231SPoul-Henning Kamp 
567b9df5231SPoul-Henning Kamp 	if (p->p_args && --p->p_args->ar_ref == 0)
568b9df5231SPoul-Henning Kamp 		FREE(p->p_args, M_PARGS);
569b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
570b9df5231SPoul-Henning Kamp 
571b9df5231SPoul-Henning Kamp 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
572b9df5231SPoul-Henning Kamp 		return (error);
573b9df5231SPoul-Henning Kamp 
574b9df5231SPoul-Henning Kamp 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
575b9df5231SPoul-Henning Kamp 	    M_PARGS, M_WAITOK);
576b9df5231SPoul-Henning Kamp 	pa->ar_ref = 1;
577b9df5231SPoul-Henning Kamp 	pa->ar_length = req->newlen;
578b9df5231SPoul-Henning Kamp 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
579b9df5231SPoul-Henning Kamp 	if (!error)
580b9df5231SPoul-Henning Kamp 		p->p_args = pa;
581b9df5231SPoul-Henning Kamp 	else
582b9df5231SPoul-Henning Kamp 		FREE(pa, M_PARGS);
583b9df5231SPoul-Henning Kamp 	return (error);
584b9df5231SPoul-Henning Kamp }
5853ce93e4eSPoul-Henning Kamp 
5863ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
5873ce93e4eSPoul-Henning Kamp 
5883ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
5893d177f46SBill Fumerola 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
5903ce93e4eSPoul-Henning Kamp 
5913ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
5923ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5933ce93e4eSPoul-Henning Kamp 
5943ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
5953ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5963ce93e4eSPoul-Henning Kamp 
5973ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
5983ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5993ce93e4eSPoul-Henning Kamp 
6003ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
6013ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
6023ce93e4eSPoul-Henning Kamp 
6033ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
604972f9b20SPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
605b9df5231SPoul-Henning Kamp 
6069b6d9dbaSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
6079b6d9dbaSPoul-Henning Kamp 	sysctl_kern_proc_args, "Process argument list");
608