xref: /freebsd/sys/kern/kern_proc.c (revision dfd5dee1b0cf927c9d59de8e423325603f86b500)
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
34dfd5dee1SPeter Wemm  * $Id: kern_proc.c,v 1.48 1999/05/03 23:57:21 billf Exp $
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/proc.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.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 
58e8fb0b2cSDavid Greenman struct prochd qs[NQS];		/* as good a place as any... */
59e8fb0b2cSDavid Greenman struct prochd rtqs[NQS];	/* Space for REALTIME queues too */
607216391eSDavid Greenman struct prochd idqs[NQS];	/* Space for IDLE queues too */
61e8fb0b2cSDavid Greenman 
6287b6de2bSPoul-Henning Kamp static void pgdelete	__P((struct pgrp *));
6326f9a767SRodney W. Grimes 
64df8bae1dSRodney W. Grimes /*
65df8bae1dSRodney W. Grimes  * Structure associated with user cacheing.
66df8bae1dSRodney W. Grimes  */
67b75356e1SJeffrey Hsu struct uidinfo {
68b75356e1SJeffrey Hsu 	LIST_ENTRY(uidinfo) ui_hash;
69df8bae1dSRodney W. Grimes 	uid_t	ui_uid;
70df8bae1dSRodney W. Grimes 	long	ui_proccnt;
71b75356e1SJeffrey Hsu };
72b75356e1SJeffrey Hsu #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
73303b270bSEivind Eklund static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
7487b6de2bSPoul-Henning Kamp static u_long uihash;		/* size of hash table - 1 */
75df8bae1dSRodney W. Grimes 
7698d93822SBruce Evans static void	orphanpg __P((struct pgrp *pg));
7798d93822SBruce Evans 
78df8bae1dSRodney W. Grimes /*
79b75356e1SJeffrey Hsu  * Other process lists
80b75356e1SJeffrey Hsu  */
81b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl;
82b75356e1SJeffrey Hsu u_long pidhash;
83b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl;
84b75356e1SJeffrey Hsu u_long pgrphash;
85b75356e1SJeffrey Hsu struct proclist allproc;
86b75356e1SJeffrey Hsu struct proclist zombproc;
872d8acc0fSJohn Dyson vm_zone_t proc_zone;
88b75356e1SJeffrey Hsu 
89b75356e1SJeffrey Hsu /*
90b75356e1SJeffrey Hsu  * Initialize global process hashing structures.
91df8bae1dSRodney W. Grimes  */
9226f9a767SRodney W. Grimes void
93b75356e1SJeffrey Hsu procinit()
94df8bae1dSRodney W. Grimes {
95df8bae1dSRodney W. Grimes 
96b75356e1SJeffrey Hsu 	LIST_INIT(&allproc);
97b75356e1SJeffrey Hsu 	LIST_INIT(&zombproc);
98b75356e1SJeffrey Hsu 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
99b75356e1SJeffrey Hsu 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
100df8bae1dSRodney W. Grimes 	uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
1012d8acc0fSJohn Dyson 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
102df8bae1dSRodney W. Grimes }
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes /*
105df8bae1dSRodney W. Grimes  * Change the count associated with number of processes
106df8bae1dSRodney W. Grimes  * a given user is using.
107df8bae1dSRodney W. Grimes  */
108df8bae1dSRodney W. Grimes int
109df8bae1dSRodney W. Grimes chgproccnt(uid, diff)
110df8bae1dSRodney W. Grimes 	uid_t	uid;
111df8bae1dSRodney W. Grimes 	int	diff;
112df8bae1dSRodney W. Grimes {
113b75356e1SJeffrey Hsu 	register struct uidinfo *uip;
114b75356e1SJeffrey Hsu 	register struct uihashhead *uipp;
115df8bae1dSRodney W. Grimes 
116b75356e1SJeffrey Hsu 	uipp = UIHASH(uid);
117b75356e1SJeffrey Hsu 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
118df8bae1dSRodney W. Grimes 		if (uip->ui_uid == uid)
119df8bae1dSRodney W. Grimes 			break;
120df8bae1dSRodney W. Grimes 	if (uip) {
121df8bae1dSRodney W. Grimes 		uip->ui_proccnt += diff;
122df8bae1dSRodney W. Grimes 		if (uip->ui_proccnt > 0)
123df8bae1dSRodney W. Grimes 			return (uip->ui_proccnt);
124df8bae1dSRodney W. Grimes 		if (uip->ui_proccnt < 0)
125df8bae1dSRodney W. Grimes 			panic("chgproccnt: procs < 0");
126b75356e1SJeffrey Hsu 		LIST_REMOVE(uip, ui_hash);
127df8bae1dSRodney W. Grimes 		FREE(uip, M_PROC);
128df8bae1dSRodney W. Grimes 		return (0);
129df8bae1dSRodney W. Grimes 	}
130df8bae1dSRodney W. Grimes 	if (diff <= 0) {
131df8bae1dSRodney W. Grimes 		if (diff == 0)
132df8bae1dSRodney W. Grimes 			return(0);
133df8bae1dSRodney W. Grimes 		panic("chgproccnt: lost user");
134df8bae1dSRodney W. Grimes 	}
135df8bae1dSRodney W. Grimes 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
136b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
137df8bae1dSRodney W. Grimes 	uip->ui_uid = uid;
138df8bae1dSRodney W. Grimes 	uip->ui_proccnt = diff;
139df8bae1dSRodney W. Grimes 	return (diff);
140df8bae1dSRodney W. Grimes }
141df8bae1dSRodney W. Grimes 
142df8bae1dSRodney W. Grimes /*
143df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
144df8bae1dSRodney W. Grimes  */
14526f9a767SRodney W. Grimes int
146df8bae1dSRodney W. Grimes inferior(p)
147df8bae1dSRodney W. Grimes 	register struct proc *p;
148df8bae1dSRodney W. Grimes {
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes 	for (; p != curproc; p = p->p_pptr)
151df8bae1dSRodney W. Grimes 		if (p->p_pid == 0)
152df8bae1dSRodney W. Grimes 			return (0);
153df8bae1dSRodney W. Grimes 	return (1);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes /*
157df8bae1dSRodney W. Grimes  * Locate a process by number
158df8bae1dSRodney W. Grimes  */
159df8bae1dSRodney W. Grimes struct proc *
160df8bae1dSRodney W. Grimes pfind(pid)
161df8bae1dSRodney W. Grimes 	register pid_t pid;
162df8bae1dSRodney W. Grimes {
163df8bae1dSRodney W. Grimes 	register struct proc *p;
164df8bae1dSRodney W. Grimes 
165b75356e1SJeffrey Hsu 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
166df8bae1dSRodney W. Grimes 		if (p->p_pid == pid)
167df8bae1dSRodney W. Grimes 			return (p);
168df8bae1dSRodney W. Grimes 	return (NULL);
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes /*
172df8bae1dSRodney W. Grimes  * Locate a process group by number
173df8bae1dSRodney W. Grimes  */
174df8bae1dSRodney W. Grimes struct pgrp *
175df8bae1dSRodney W. Grimes pgfind(pgid)
176df8bae1dSRodney W. Grimes 	register pid_t pgid;
177df8bae1dSRodney W. Grimes {
178df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
179df8bae1dSRodney W. Grimes 
180b75356e1SJeffrey Hsu 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0;
181b75356e1SJeffrey Hsu 	     pgrp = pgrp->pg_hash.le_next)
182df8bae1dSRodney W. Grimes 		if (pgrp->pg_id == pgid)
183df8bae1dSRodney W. Grimes 			return (pgrp);
184df8bae1dSRodney W. Grimes 	return (NULL);
185df8bae1dSRodney W. Grimes }
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes /*
188df8bae1dSRodney W. Grimes  * Move p to a new or existing process group (and session)
189df8bae1dSRodney W. Grimes  */
19026f9a767SRodney W. Grimes int
191df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess)
192df8bae1dSRodney W. Grimes 	register struct proc *p;
193df8bae1dSRodney W. Grimes 	pid_t pgid;
194df8bae1dSRodney W. Grimes 	int mksess;
195df8bae1dSRodney W. Grimes {
196df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp = pgfind(pgid);
197df8bae1dSRodney W. Grimes 
1985526d2d9SEivind Eklund 	KASSERT(pgrp == NULL || !mksess,
1995526d2d9SEivind Eklund 	    ("enterpgrp: setsid into non-empty pgrp"));
2005526d2d9SEivind Eklund 	KASSERT(!SESS_LEADER(p),
2015526d2d9SEivind Eklund 	    ("enterpgrp: session leader attempted setpgrp"));
202219cbf59SEivind Eklund 
203df8bae1dSRodney W. Grimes 	if (pgrp == NULL) {
204df8bae1dSRodney W. Grimes 		pid_t savepid = p->p_pid;
205df8bae1dSRodney W. Grimes 		struct proc *np;
206df8bae1dSRodney W. Grimes 		/*
207df8bae1dSRodney W. Grimes 		 * new process group
208df8bae1dSRodney W. Grimes 		 */
2095526d2d9SEivind Eklund 		KASSERT(p->p_pid == pgid,
2105526d2d9SEivind Eklund 		    ("enterpgrp: new pgrp and pid != pgid"));
211df8bae1dSRodney W. Grimes 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
212df8bae1dSRodney W. Grimes 		    M_WAITOK);
213df8bae1dSRodney W. Grimes 		if ((np = pfind(savepid)) == NULL || np != p)
214df8bae1dSRodney W. Grimes 			return (ESRCH);
215df8bae1dSRodney W. Grimes 		if (mksess) {
216df8bae1dSRodney W. Grimes 			register struct session *sess;
217df8bae1dSRodney W. Grimes 
218df8bae1dSRodney W. Grimes 			/*
219df8bae1dSRodney W. Grimes 			 * new session
220df8bae1dSRodney W. Grimes 			 */
221df8bae1dSRodney W. Grimes 			MALLOC(sess, struct session *, sizeof(struct session),
222df8bae1dSRodney W. Grimes 			    M_SESSION, M_WAITOK);
223df8bae1dSRodney W. Grimes 			sess->s_leader = p;
224643a8daaSDon Lewis 			sess->s_sid = p->p_pid;
225df8bae1dSRodney W. Grimes 			sess->s_count = 1;
226df8bae1dSRodney W. Grimes 			sess->s_ttyvp = NULL;
227df8bae1dSRodney W. Grimes 			sess->s_ttyp = NULL;
228df8bae1dSRodney W. Grimes 			bcopy(p->p_session->s_login, sess->s_login,
229df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
230df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_CONTROLT;
231df8bae1dSRodney W. Grimes 			pgrp->pg_session = sess;
2325526d2d9SEivind Eklund 			KASSERT(p == curproc,
2335526d2d9SEivind Eklund 			    ("enterpgrp: mksession and p != curproc"));
234df8bae1dSRodney W. Grimes 		} else {
235df8bae1dSRodney W. Grimes 			pgrp->pg_session = p->p_session;
236df8bae1dSRodney W. Grimes 			pgrp->pg_session->s_count++;
237df8bae1dSRodney W. Grimes 		}
238df8bae1dSRodney W. Grimes 		pgrp->pg_id = pgid;
239b75356e1SJeffrey Hsu 		LIST_INIT(&pgrp->pg_members);
240b75356e1SJeffrey Hsu 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
241df8bae1dSRodney W. Grimes 		pgrp->pg_jobc = 0;
242831d27a9SDon Lewis 		SLIST_INIT(&pgrp->pg_sigiolst);
243df8bae1dSRodney W. Grimes 	} else if (pgrp == p->p_pgrp)
244df8bae1dSRodney W. Grimes 		return (0);
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	/*
247df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
248df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
249df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
250df8bae1dSRodney W. Grimes 	 */
251df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
252df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
253df8bae1dSRodney W. Grimes 
254b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
255b75356e1SJeffrey Hsu 	if (p->p_pgrp->pg_members.lh_first == 0)
256df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
257df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
258b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
259df8bae1dSRodney W. Grimes 	return (0);
260df8bae1dSRodney W. Grimes }
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes /*
263df8bae1dSRodney W. Grimes  * remove process from process group
264df8bae1dSRodney W. Grimes  */
26526f9a767SRodney W. Grimes int
266df8bae1dSRodney W. Grimes leavepgrp(p)
267df8bae1dSRodney W. Grimes 	register struct proc *p;
268df8bae1dSRodney W. Grimes {
269df8bae1dSRodney W. Grimes 
270b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
271b75356e1SJeffrey Hsu 	if (p->p_pgrp->pg_members.lh_first == 0)
272df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
273df8bae1dSRodney W. Grimes 	p->p_pgrp = 0;
274df8bae1dSRodney W. Grimes 	return (0);
275df8bae1dSRodney W. Grimes }
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes /*
278df8bae1dSRodney W. Grimes  * delete a process group
279df8bae1dSRodney W. Grimes  */
28087b6de2bSPoul-Henning Kamp static void
281df8bae1dSRodney W. Grimes pgdelete(pgrp)
282df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
283df8bae1dSRodney W. Grimes {
284df8bae1dSRodney W. Grimes 
285831d27a9SDon Lewis 	/*
286831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
287831d27a9SDon Lewis 	 * F_SETOWN with our pgid.
288831d27a9SDon Lewis 	 */
289831d27a9SDon Lewis 	funsetownlst(&pgrp->pg_sigiolst);
290831d27a9SDon Lewis 
291df8bae1dSRodney W. Grimes 	if (pgrp->pg_session->s_ttyp != NULL &&
292df8bae1dSRodney W. Grimes 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
293df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
294b75356e1SJeffrey Hsu 	LIST_REMOVE(pgrp, pg_hash);
295df8bae1dSRodney W. Grimes 	if (--pgrp->pg_session->s_count == 0)
296df8bae1dSRodney W. Grimes 		FREE(pgrp->pg_session, M_SESSION);
297df8bae1dSRodney W. Grimes 	FREE(pgrp, M_PGRP);
298df8bae1dSRodney W. Grimes }
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes /*
301df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
302df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
303df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
304df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
305df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
306df8bae1dSRodney W. Grimes  * process group and that of its children.
307df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
308df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
309df8bae1dSRodney W. Grimes  */
31026f9a767SRodney W. Grimes void
311df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
312df8bae1dSRodney W. Grimes 	register struct proc *p;
313df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
314df8bae1dSRodney W. Grimes 	int entering;
315df8bae1dSRodney W. Grimes {
316df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
317df8bae1dSRodney W. Grimes 	register struct session *mysession = pgrp->pg_session;
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	/*
320df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
321df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
322df8bae1dSRodney W. Grimes 	 */
323df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
324dfd5dee1SPeter Wemm 	    hispgrp->pg_session == mysession) {
325df8bae1dSRodney W. Grimes 		if (entering)
326df8bae1dSRodney W. Grimes 			pgrp->pg_jobc++;
327df8bae1dSRodney W. Grimes 		else if (--pgrp->pg_jobc == 0)
328df8bae1dSRodney W. Grimes 			orphanpg(pgrp);
329dfd5dee1SPeter Wemm 	}
330df8bae1dSRodney W. Grimes 
331df8bae1dSRodney W. Grimes 	/*
332df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
333df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
334df8bae1dSRodney W. Grimes 	 * process groups.
335df8bae1dSRodney W. Grimes 	 */
336b75356e1SJeffrey Hsu 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
337df8bae1dSRodney W. Grimes 		if ((hispgrp = p->p_pgrp) != pgrp &&
338df8bae1dSRodney W. Grimes 		    hispgrp->pg_session == mysession &&
339dfd5dee1SPeter Wemm 		    p->p_stat != SZOMB) {
340df8bae1dSRodney W. Grimes 			if (entering)
341df8bae1dSRodney W. Grimes 				hispgrp->pg_jobc++;
342df8bae1dSRodney W. Grimes 			else if (--hispgrp->pg_jobc == 0)
343df8bae1dSRodney W. Grimes 				orphanpg(hispgrp);
344df8bae1dSRodney W. Grimes 		}
345dfd5dee1SPeter Wemm }
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes /*
348df8bae1dSRodney W. Grimes  * A process group has become orphaned;
349df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
350df8bae1dSRodney W. Grimes  * hang-up all process in that group.
351df8bae1dSRodney W. Grimes  */
352df8bae1dSRodney W. Grimes static void
353df8bae1dSRodney W. Grimes orphanpg(pg)
354df8bae1dSRodney W. Grimes 	struct pgrp *pg;
355df8bae1dSRodney W. Grimes {
356df8bae1dSRodney W. Grimes 	register struct proc *p;
357df8bae1dSRodney W. Grimes 
358b75356e1SJeffrey Hsu 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
359df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP) {
360b75356e1SJeffrey Hsu 			for (p = pg->pg_members.lh_first; p != 0;
361b75356e1SJeffrey Hsu 			    p = p->p_pglist.le_next) {
362df8bae1dSRodney W. Grimes 				psignal(p, SIGHUP);
363df8bae1dSRodney W. Grimes 				psignal(p, SIGCONT);
364df8bae1dSRodney W. Grimes 			}
365df8bae1dSRodney W. Grimes 			return;
366df8bae1dSRodney W. Grimes 		}
367df8bae1dSRodney W. Grimes 	}
368df8bae1dSRodney W. Grimes }
369df8bae1dSRodney W. Grimes 
370831031ceSBruce Evans #include "opt_ddb.h"
371831031ceSBruce Evans #ifdef DDB
372831031ceSBruce Evans #include <ddb/ddb.h>
373831031ceSBruce Evans 
374831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump)
375df8bae1dSRodney W. Grimes {
376df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
377df8bae1dSRodney W. Grimes 	register struct proc *p;
378876a94eeSBruce Evans 	register int i;
379df8bae1dSRodney W. Grimes 
380b75356e1SJeffrey Hsu 	for (i = 0; i <= pgrphash; i++) {
3818aef1712SMatthew Dillon 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
382df8bae1dSRodney W. Grimes 			printf("\tindx %d\n", i);
383b75356e1SJeffrey Hsu 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
384ac1e407bSBruce Evans 				printf(
385ac1e407bSBruce Evans 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
386ac1e407bSBruce Evans 				    (void *)pgrp, (long)pgrp->pg_id,
387ac1e407bSBruce Evans 				    (void *)pgrp->pg_session,
388b75356e1SJeffrey Hsu 				    pgrp->pg_session->s_count,
389ac1e407bSBruce Evans 				    (void *)pgrp->pg_members.lh_first);
390b75356e1SJeffrey Hsu 				for (p = pgrp->pg_members.lh_first; p != 0;
391b75356e1SJeffrey Hsu 				    p = p->p_pglist.le_next) {
392ac1e407bSBruce Evans 					printf("\t\tpid %ld addr %p pgrp %p\n",
393ac1e407bSBruce Evans 					    (long)p->p_pid, (void *)p,
394ac1e407bSBruce Evans 					    (void *)p->p_pgrp);
395df8bae1dSRodney W. Grimes 				}
396df8bae1dSRodney W. Grimes 			}
397df8bae1dSRodney W. Grimes 		}
398df8bae1dSRodney W. Grimes 	}
399df8bae1dSRodney W. Grimes }
400831031ceSBruce Evans #endif /* DDB */
401972f9b20SPoul-Henning Kamp 
402972f9b20SPoul-Henning Kamp /*
403972f9b20SPoul-Henning Kamp  * Fill in an eproc structure for the specified process.
404972f9b20SPoul-Henning Kamp  */
405972f9b20SPoul-Henning Kamp void
406972f9b20SPoul-Henning Kamp fill_eproc(p, ep)
407972f9b20SPoul-Henning Kamp 	register struct proc *p;
408972f9b20SPoul-Henning Kamp 	register struct eproc *ep;
409972f9b20SPoul-Henning Kamp {
410972f9b20SPoul-Henning Kamp 	register struct tty *tp;
411972f9b20SPoul-Henning Kamp 
412972f9b20SPoul-Henning Kamp 	bzero(ep, sizeof(*ep));
413972f9b20SPoul-Henning Kamp 
414972f9b20SPoul-Henning Kamp 	ep->e_paddr = p;
415972f9b20SPoul-Henning Kamp 	if (p->p_cred) {
416972f9b20SPoul-Henning Kamp 		ep->e_pcred = *p->p_cred;
417972f9b20SPoul-Henning Kamp 		if (p->p_ucred)
418972f9b20SPoul-Henning Kamp 			ep->e_ucred = *p->p_ucred;
419972f9b20SPoul-Henning Kamp 	}
420d8c85307SJulian Elischer 	if (p->p_procsig){
421d8c85307SJulian Elischer 		ep->e_procsig = *p->p_procsig;
422d8c85307SJulian Elischer 	}
423972f9b20SPoul-Henning Kamp 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
424972f9b20SPoul-Henning Kamp 		register struct vmspace *vm = p->p_vmspace;
425b1028ad1SLuoqi Chen 		ep->e_vm = *vm;
426b1028ad1SLuoqi Chen 		ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
427972f9b20SPoul-Henning Kamp 	}
428972f9b20SPoul-Henning Kamp 	if (p->p_pptr)
429972f9b20SPoul-Henning Kamp 		ep->e_ppid = p->p_pptr->p_pid;
430972f9b20SPoul-Henning Kamp 	if (p->p_pgrp) {
431972f9b20SPoul-Henning Kamp 		ep->e_pgid = p->p_pgrp->pg_id;
432972f9b20SPoul-Henning Kamp 		ep->e_jobc = p->p_pgrp->pg_jobc;
433cd73303cSDavid Greenman 		ep->e_sess = p->p_pgrp->pg_session;
434cd73303cSDavid Greenman 
435cd73303cSDavid Greenman 		if (ep->e_sess) {
4368735cebcSPeter Wemm 			bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
437cd73303cSDavid Greenman 			if (ep->e_sess->s_ttyvp)
438cd73303cSDavid Greenman 				ep->e_flag = EPROC_CTTY;
439cd73303cSDavid Greenman 			if (p->p_session && SESS_LEADER(p))
440cd73303cSDavid Greenman 				ep->e_flag |= EPROC_SLEADER;
441cd73303cSDavid Greenman 		}
442cd73303cSDavid Greenman 	}
443972f9b20SPoul-Henning Kamp 	if ((p->p_flag & P_CONTROLT) &&
444972f9b20SPoul-Henning Kamp 	    (ep->e_sess != NULL) &&
445972f9b20SPoul-Henning Kamp 	    ((tp = ep->e_sess->s_ttyp) != NULL)) {
446972f9b20SPoul-Henning Kamp 		ep->e_tdev = tp->t_dev;
447972f9b20SPoul-Henning Kamp 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
448972f9b20SPoul-Henning Kamp 		ep->e_tsess = tp->t_session;
449972f9b20SPoul-Henning Kamp 	} else
450972f9b20SPoul-Henning Kamp 		ep->e_tdev = NODEV;
451972f9b20SPoul-Henning Kamp 	if (p->p_wmesg) {
452972f9b20SPoul-Henning Kamp 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
453972f9b20SPoul-Henning Kamp 		ep->e_wmesg[WMESGLEN] = 0;
454972f9b20SPoul-Henning Kamp 	}
455972f9b20SPoul-Henning Kamp }
456972f9b20SPoul-Henning Kamp 
4573ce93e4eSPoul-Henning Kamp static struct proc *
4583ce93e4eSPoul-Henning Kamp zpfind(pid_t pid)
4593ce93e4eSPoul-Henning Kamp {
4603ce93e4eSPoul-Henning Kamp 	struct proc *p;
4613ce93e4eSPoul-Henning Kamp 
4623ce93e4eSPoul-Henning Kamp 	for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
4633ce93e4eSPoul-Henning Kamp 		if (p->p_pid == pid)
4643ce93e4eSPoul-Henning Kamp 			return (p);
4653ce93e4eSPoul-Henning Kamp 	return (NULL);
4663ce93e4eSPoul-Henning Kamp }
4673ce93e4eSPoul-Henning Kamp 
4683ce93e4eSPoul-Henning Kamp 
4693ce93e4eSPoul-Henning Kamp static int
4703ce93e4eSPoul-Henning Kamp sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
4713ce93e4eSPoul-Henning Kamp {
4723ce93e4eSPoul-Henning Kamp 	struct eproc eproc;
4733ce93e4eSPoul-Henning Kamp 	int error;
4743ce93e4eSPoul-Henning Kamp 	pid_t pid = p->p_pid;
4753ce93e4eSPoul-Henning Kamp 
4763ce93e4eSPoul-Henning Kamp 	fill_eproc(p, &eproc);
4773ce93e4eSPoul-Henning Kamp 	error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc));
4783ce93e4eSPoul-Henning Kamp 	if (error)
4793ce93e4eSPoul-Henning Kamp 		return (error);
4803ce93e4eSPoul-Henning Kamp 	error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
4813ce93e4eSPoul-Henning Kamp 	if (error)
4823ce93e4eSPoul-Henning Kamp 		return (error);
4833ce93e4eSPoul-Henning Kamp 	if (!doingzomb && pid && (pfind(pid) != p))
4843ce93e4eSPoul-Henning Kamp 		return EAGAIN;
4853ce93e4eSPoul-Henning Kamp 	if (doingzomb && zpfind(pid) != p)
4863ce93e4eSPoul-Henning Kamp 		return EAGAIN;
4873ce93e4eSPoul-Henning Kamp 	return (0);
4883ce93e4eSPoul-Henning Kamp }
4893ce93e4eSPoul-Henning Kamp 
490972f9b20SPoul-Henning Kamp static int
491972f9b20SPoul-Henning Kamp sysctl_kern_proc SYSCTL_HANDLER_ARGS
492972f9b20SPoul-Henning Kamp {
493972f9b20SPoul-Henning Kamp 	int *name = (int*) arg1;
494972f9b20SPoul-Henning Kamp 	u_int namelen = arg2;
495972f9b20SPoul-Henning Kamp 	struct proc *p;
496972f9b20SPoul-Henning Kamp 	int doingzomb;
497972f9b20SPoul-Henning Kamp 	int error = 0;
498972f9b20SPoul-Henning Kamp 
4993ce93e4eSPoul-Henning Kamp 	if (oidp->oid_number == KERN_PROC_PID) {
5003ce93e4eSPoul-Henning Kamp 		if (namelen != 1)
501972f9b20SPoul-Henning Kamp 			return (EINVAL);
5023ce93e4eSPoul-Henning Kamp 		p = pfind((pid_t)name[0]);
5033ce93e4eSPoul-Henning Kamp 		if (!p)
5043ce93e4eSPoul-Henning Kamp 			return (0);
50575c13541SPoul-Henning Kamp 		if (!PRISON_CHECK(curproc, p))
50675c13541SPoul-Henning Kamp 			return (0);
5073ce93e4eSPoul-Henning Kamp 		error = sysctl_out_proc(p, req, 0);
5083ce93e4eSPoul-Henning Kamp 		return (error);
5093ce93e4eSPoul-Henning Kamp 	}
5103ce93e4eSPoul-Henning Kamp 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
5113ce93e4eSPoul-Henning Kamp 		;
5123ce93e4eSPoul-Henning Kamp 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
5133ce93e4eSPoul-Henning Kamp 		;
5143ce93e4eSPoul-Henning Kamp 	else
5153ce93e4eSPoul-Henning Kamp 		return (EINVAL);
5163ce93e4eSPoul-Henning Kamp 
517972f9b20SPoul-Henning Kamp 	if (!req->oldptr) {
5183ce93e4eSPoul-Henning Kamp 		/* overestimate by 5 procs */
519972f9b20SPoul-Henning Kamp 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
520972f9b20SPoul-Henning Kamp 		if (error)
521972f9b20SPoul-Henning Kamp 			return (error);
522972f9b20SPoul-Henning Kamp 	}
5233ce93e4eSPoul-Henning Kamp 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
5243ce93e4eSPoul-Henning Kamp 		if (!doingzomb)
525b75356e1SJeffrey Hsu 			p = allproc.lh_first;
5263ce93e4eSPoul-Henning Kamp 		else
5273ce93e4eSPoul-Henning Kamp 			p = zombproc.lh_first;
528b75356e1SJeffrey Hsu 		for (; p != 0; p = p->p_list.le_next) {
529972f9b20SPoul-Henning Kamp 			/*
530972f9b20SPoul-Henning Kamp 			 * Skip embryonic processes.
531972f9b20SPoul-Henning Kamp 			 */
532972f9b20SPoul-Henning Kamp 			if (p->p_stat == SIDL)
533972f9b20SPoul-Henning Kamp 				continue;
534972f9b20SPoul-Henning Kamp 			/*
535972f9b20SPoul-Henning Kamp 			 * TODO - make more efficient (see notes below).
536972f9b20SPoul-Henning Kamp 			 * do by session.
537972f9b20SPoul-Henning Kamp 			 */
5383ce93e4eSPoul-Henning Kamp 			switch (oidp->oid_number) {
539972f9b20SPoul-Henning Kamp 
540972f9b20SPoul-Henning Kamp 			case KERN_PROC_PGRP:
541972f9b20SPoul-Henning Kamp 				/* could do this by traversing pgrp */
5423ce93e4eSPoul-Henning Kamp 				if (p->p_pgrp == NULL ||
5433ce93e4eSPoul-Henning Kamp 				    p->p_pgrp->pg_id != (pid_t)name[0])
544972f9b20SPoul-Henning Kamp 					continue;
545972f9b20SPoul-Henning Kamp 				break;
546972f9b20SPoul-Henning Kamp 
547972f9b20SPoul-Henning Kamp 			case KERN_PROC_TTY:
548972f9b20SPoul-Henning Kamp 				if ((p->p_flag & P_CONTROLT) == 0 ||
549972f9b20SPoul-Henning Kamp 				    p->p_session == NULL ||
550972f9b20SPoul-Henning Kamp 				    p->p_session->s_ttyp == NULL ||
5513ce93e4eSPoul-Henning Kamp 				    p->p_session->s_ttyp->t_dev != (dev_t)name[0])
552972f9b20SPoul-Henning Kamp 					continue;
553972f9b20SPoul-Henning Kamp 				break;
554972f9b20SPoul-Henning Kamp 
555972f9b20SPoul-Henning Kamp 			case KERN_PROC_UID:
5563ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
5573ce93e4eSPoul-Henning Kamp 				    p->p_ucred->cr_uid != (uid_t)name[0])
558972f9b20SPoul-Henning Kamp 					continue;
559972f9b20SPoul-Henning Kamp 				break;
560972f9b20SPoul-Henning Kamp 
561972f9b20SPoul-Henning Kamp 			case KERN_PROC_RUID:
5623ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
5633ce93e4eSPoul-Henning Kamp 				    p->p_cred->p_ruid != (uid_t)name[0])
564972f9b20SPoul-Henning Kamp 					continue;
565972f9b20SPoul-Henning Kamp 				break;
566972f9b20SPoul-Henning Kamp 			}
567972f9b20SPoul-Henning Kamp 
56875c13541SPoul-Henning Kamp 			if (!PRISON_CHECK(curproc, p))
56975c13541SPoul-Henning Kamp 				continue;
57075c13541SPoul-Henning Kamp 
5713ce93e4eSPoul-Henning Kamp 			error = sysctl_out_proc(p, req, doingzomb);
572972f9b20SPoul-Henning Kamp 			if (error)
573972f9b20SPoul-Henning Kamp 				return (error);
574972f9b20SPoul-Henning Kamp 		}
575972f9b20SPoul-Henning Kamp 	}
576972f9b20SPoul-Henning Kamp 	return (0);
577972f9b20SPoul-Henning Kamp }
578972f9b20SPoul-Henning Kamp 
5793ce93e4eSPoul-Henning Kamp 
5803ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
5813ce93e4eSPoul-Henning Kamp 
5823ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
5833d177f46SBill Fumerola 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
5843ce93e4eSPoul-Henning Kamp 
5853ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
5863ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5873ce93e4eSPoul-Henning Kamp 
5883ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
5893ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5903ce93e4eSPoul-Henning Kamp 
5913ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
5923ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5933ce93e4eSPoul-Henning Kamp 
5943ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
5953ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
5963ce93e4eSPoul-Henning Kamp 
5973ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
598972f9b20SPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
599