xref: /freebsd/sys/kern/kern_proc.c (revision 26f9a76710a312a951848542b9ca1f44100450e2)
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  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_proc.c	8.4 (Berkeley) 1/4/94
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
37df8bae1dSRodney W. Grimes #include <sys/systm.h>
38df8bae1dSRodney W. Grimes #include <sys/map.h>
39df8bae1dSRodney W. Grimes #include <sys/kernel.h>
40df8bae1dSRodney W. Grimes #include <sys/proc.h>
41df8bae1dSRodney W. Grimes #include <sys/buf.h>
42df8bae1dSRodney W. Grimes #include <sys/acct.h>
43df8bae1dSRodney W. Grimes #include <sys/wait.h>
44df8bae1dSRodney W. Grimes #include <sys/file.h>
45df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
46df8bae1dSRodney W. Grimes #include <sys/uio.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
50df8bae1dSRodney W. Grimes #include <sys/tty.h>
51df8bae1dSRodney W. Grimes 
5226f9a767SRodney W. Grimes void pgdelete	__P((struct pgrp *));
5326f9a767SRodney W. Grimes void fixjobc	__P((struct proc *, struct pgrp *, int));
5426f9a767SRodney W. Grimes 
55df8bae1dSRodney W. Grimes /*
56df8bae1dSRodney W. Grimes  * Structure associated with user cacheing.
57df8bae1dSRodney W. Grimes  */
58df8bae1dSRodney W. Grimes struct uidinfo {
59df8bae1dSRodney W. Grimes 	struct	uidinfo *ui_next;
60df8bae1dSRodney W. Grimes 	struct	uidinfo **ui_prev;
61df8bae1dSRodney W. Grimes 	uid_t	ui_uid;
62df8bae1dSRodney W. Grimes 	long	ui_proccnt;
63df8bae1dSRodney W. Grimes } **uihashtbl;
64df8bae1dSRodney W. Grimes u_long	uihash;		/* size of hash table - 1 */
65df8bae1dSRodney W. Grimes #define	UIHASH(uid)	((uid) & uihash)
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  * Allocate a hash table.
69df8bae1dSRodney W. Grimes  */
7026f9a767SRodney W. Grimes void
71df8bae1dSRodney W. Grimes usrinfoinit()
72df8bae1dSRodney W. Grimes {
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes 	uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
75df8bae1dSRodney W. Grimes }
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes /*
78df8bae1dSRodney W. Grimes  * Change the count associated with number of processes
79df8bae1dSRodney W. Grimes  * a given user is using.
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes int
82df8bae1dSRodney W. Grimes chgproccnt(uid, diff)
83df8bae1dSRodney W. Grimes 	uid_t	uid;
84df8bae1dSRodney W. Grimes 	int	diff;
85df8bae1dSRodney W. Grimes {
86df8bae1dSRodney W. Grimes 	register struct uidinfo **uipp, *uip, *uiq;
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes 	uipp = &uihashtbl[UIHASH(uid)];
89df8bae1dSRodney W. Grimes 	for (uip = *uipp; uip; uip = uip->ui_next)
90df8bae1dSRodney W. Grimes 		if (uip->ui_uid == uid)
91df8bae1dSRodney W. Grimes 			break;
92df8bae1dSRodney W. Grimes 	if (uip) {
93df8bae1dSRodney W. Grimes 		uip->ui_proccnt += diff;
94df8bae1dSRodney W. Grimes 		if (uip->ui_proccnt > 0)
95df8bae1dSRodney W. Grimes 			return (uip->ui_proccnt);
96df8bae1dSRodney W. Grimes 		if (uip->ui_proccnt < 0)
97df8bae1dSRodney W. Grimes 			panic("chgproccnt: procs < 0");
98df8bae1dSRodney W. Grimes 		if (uiq = uip->ui_next)
99df8bae1dSRodney W. Grimes 			uiq->ui_prev = uip->ui_prev;
100df8bae1dSRodney W. Grimes 		*uip->ui_prev = uiq;
101df8bae1dSRodney W. Grimes 		FREE(uip, M_PROC);
102df8bae1dSRodney W. Grimes 		return (0);
103df8bae1dSRodney W. Grimes 	}
104df8bae1dSRodney W. Grimes 	if (diff <= 0) {
105df8bae1dSRodney W. Grimes 		if (diff == 0)
106df8bae1dSRodney W. Grimes 			return(0);
107df8bae1dSRodney W. Grimes 		panic("chgproccnt: lost user");
108df8bae1dSRodney W. Grimes 	}
109df8bae1dSRodney W. Grimes 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
110df8bae1dSRodney W. Grimes 	if (uiq = *uipp)
111df8bae1dSRodney W. Grimes 		uiq->ui_prev = &uip->ui_next;
112df8bae1dSRodney W. Grimes 	uip->ui_next = uiq;
113df8bae1dSRodney W. Grimes 	uip->ui_prev = uipp;
114df8bae1dSRodney W. Grimes 	*uipp = uip;
115df8bae1dSRodney W. Grimes 	uip->ui_uid = uid;
116df8bae1dSRodney W. Grimes 	uip->ui_proccnt = diff;
117df8bae1dSRodney W. Grimes 	return (diff);
118df8bae1dSRodney W. Grimes }
119df8bae1dSRodney W. Grimes 
120df8bae1dSRodney W. Grimes /*
121df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
122df8bae1dSRodney W. Grimes  */
12326f9a767SRodney W. Grimes int
124df8bae1dSRodney W. Grimes inferior(p)
125df8bae1dSRodney W. Grimes 	register struct proc *p;
126df8bae1dSRodney W. Grimes {
127df8bae1dSRodney W. Grimes 
128df8bae1dSRodney W. Grimes 	for (; p != curproc; p = p->p_pptr)
129df8bae1dSRodney W. Grimes 		if (p->p_pid == 0)
130df8bae1dSRodney W. Grimes 			return (0);
131df8bae1dSRodney W. Grimes 	return (1);
132df8bae1dSRodney W. Grimes }
133df8bae1dSRodney W. Grimes 
134df8bae1dSRodney W. Grimes /*
135df8bae1dSRodney W. Grimes  * Locate a process by number
136df8bae1dSRodney W. Grimes  */
137df8bae1dSRodney W. Grimes struct proc *
138df8bae1dSRodney W. Grimes pfind(pid)
139df8bae1dSRodney W. Grimes 	register pid_t pid;
140df8bae1dSRodney W. Grimes {
141df8bae1dSRodney W. Grimes 	register struct proc *p;
142df8bae1dSRodney W. Grimes 
143df8bae1dSRodney W. Grimes 	for (p = pidhash[PIDHASH(pid)]; p != NULL; p = p->p_hash)
144df8bae1dSRodney W. Grimes 		if (p->p_pid == pid)
145df8bae1dSRodney W. Grimes 			return (p);
146df8bae1dSRodney W. Grimes 	return (NULL);
147df8bae1dSRodney W. Grimes }
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes /*
150df8bae1dSRodney W. Grimes  * Locate a process group by number
151df8bae1dSRodney W. Grimes  */
152df8bae1dSRodney W. Grimes struct pgrp *
153df8bae1dSRodney W. Grimes pgfind(pgid)
154df8bae1dSRodney W. Grimes 	register pid_t pgid;
155df8bae1dSRodney W. Grimes {
156df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	for (pgrp = pgrphash[PIDHASH(pgid)];
159df8bae1dSRodney W. Grimes 	    pgrp != NULL; pgrp = pgrp->pg_hforw)
160df8bae1dSRodney W. Grimes 		if (pgrp->pg_id == pgid)
161df8bae1dSRodney W. Grimes 			return (pgrp);
162df8bae1dSRodney W. Grimes 	return (NULL);
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166df8bae1dSRodney W. Grimes  * Move p to a new or existing process group (and session)
167df8bae1dSRodney W. Grimes  */
16826f9a767SRodney W. Grimes int
169df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess)
170df8bae1dSRodney W. Grimes 	register struct proc *p;
171df8bae1dSRodney W. Grimes 	pid_t pgid;
172df8bae1dSRodney W. Grimes 	int mksess;
173df8bae1dSRodney W. Grimes {
174df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp = pgfind(pgid);
175df8bae1dSRodney W. Grimes 	register struct proc **pp;
176df8bae1dSRodney W. Grimes 	int n;
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
179df8bae1dSRodney W. Grimes 	if (pgrp != NULL && mksess)	/* firewalls */
180df8bae1dSRodney W. Grimes 		panic("enterpgrp: setsid into non-empty pgrp");
181df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p))
182df8bae1dSRodney W. Grimes 		panic("enterpgrp: session leader attempted setpgrp");
183df8bae1dSRodney W. Grimes #endif
184df8bae1dSRodney W. Grimes 	if (pgrp == NULL) {
185df8bae1dSRodney W. Grimes 		pid_t savepid = p->p_pid;
186df8bae1dSRodney W. Grimes 		struct proc *np;
187df8bae1dSRodney W. Grimes 		/*
188df8bae1dSRodney W. Grimes 		 * new process group
189df8bae1dSRodney W. Grimes 		 */
190df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
191df8bae1dSRodney W. Grimes 		if (p->p_pid != pgid)
192df8bae1dSRodney W. Grimes 			panic("enterpgrp: new pgrp and pid != pgid");
193df8bae1dSRodney W. Grimes #endif
194df8bae1dSRodney W. Grimes 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
195df8bae1dSRodney W. Grimes 		       M_WAITOK);
196df8bae1dSRodney W. Grimes 		if ((np = pfind(savepid)) == NULL || np != p)
197df8bae1dSRodney W. Grimes 			return (ESRCH);
198df8bae1dSRodney W. Grimes 		if (mksess) {
199df8bae1dSRodney W. Grimes 			register struct session *sess;
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 			/*
202df8bae1dSRodney W. Grimes 			 * new session
203df8bae1dSRodney W. Grimes 			 */
204df8bae1dSRodney W. Grimes 			MALLOC(sess, struct session *, sizeof(struct session),
205df8bae1dSRodney W. Grimes 				M_SESSION, M_WAITOK);
206df8bae1dSRodney W. Grimes 			sess->s_leader = p;
207df8bae1dSRodney W. Grimes 			sess->s_count = 1;
208df8bae1dSRodney W. Grimes 			sess->s_ttyvp = NULL;
209df8bae1dSRodney W. Grimes 			sess->s_ttyp = NULL;
210df8bae1dSRodney W. Grimes 			bcopy(p->p_session->s_login, sess->s_login,
211df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
212df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_CONTROLT;
213df8bae1dSRodney W. Grimes 			pgrp->pg_session = sess;
214df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
215df8bae1dSRodney W. Grimes 			if (p != curproc)
216df8bae1dSRodney W. Grimes 				panic("enterpgrp: mksession and p != curproc");
217df8bae1dSRodney W. Grimes #endif
218df8bae1dSRodney W. Grimes 		} else {
219df8bae1dSRodney W. Grimes 			pgrp->pg_session = p->p_session;
220df8bae1dSRodney W. Grimes 			pgrp->pg_session->s_count++;
221df8bae1dSRodney W. Grimes 		}
222df8bae1dSRodney W. Grimes 		pgrp->pg_id = pgid;
223df8bae1dSRodney W. Grimes 		pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)];
224df8bae1dSRodney W. Grimes 		pgrphash[n] = pgrp;
225df8bae1dSRodney W. Grimes 		pgrp->pg_jobc = 0;
226df8bae1dSRodney W. Grimes 		pgrp->pg_mem = NULL;
227df8bae1dSRodney W. Grimes 	} else if (pgrp == p->p_pgrp)
228df8bae1dSRodney W. Grimes 		return (0);
229df8bae1dSRodney W. Grimes 
230df8bae1dSRodney W. Grimes 	/*
231df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
232df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
233df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
234df8bae1dSRodney W. Grimes 	 */
235df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
236df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 	/*
239df8bae1dSRodney W. Grimes 	 * unlink p from old process group
240df8bae1dSRodney W. Grimes 	 */
241df8bae1dSRodney W. Grimes 	for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt) {
242df8bae1dSRodney W. Grimes 		if (*pp == p) {
243df8bae1dSRodney W. Grimes 			*pp = p->p_pgrpnxt;
244df8bae1dSRodney W. Grimes 			break;
245df8bae1dSRodney W. Grimes 		}
246df8bae1dSRodney W. Grimes 	}
247df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
248df8bae1dSRodney W. Grimes 	if (pp == NULL)
249df8bae1dSRodney W. Grimes 		panic("enterpgrp: can't find p on old pgrp");
250df8bae1dSRodney W. Grimes #endif
251df8bae1dSRodney W. Grimes 	/*
252df8bae1dSRodney W. Grimes 	 * delete old if empty
253df8bae1dSRodney W. Grimes 	 */
254df8bae1dSRodney W. Grimes 	if (p->p_pgrp->pg_mem == 0)
255df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
256df8bae1dSRodney W. Grimes 	/*
257df8bae1dSRodney W. Grimes 	 * link into new one
258df8bae1dSRodney W. Grimes 	 */
259df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
260df8bae1dSRodney W. Grimes 	p->p_pgrpnxt = pgrp->pg_mem;
261df8bae1dSRodney W. Grimes 	pgrp->pg_mem = p;
262df8bae1dSRodney W. Grimes 	return (0);
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes /*
266df8bae1dSRodney W. Grimes  * remove process from process group
267df8bae1dSRodney W. Grimes  */
26826f9a767SRodney W. Grimes int
269df8bae1dSRodney W. Grimes leavepgrp(p)
270df8bae1dSRodney W. Grimes 	register struct proc *p;
271df8bae1dSRodney W. Grimes {
272df8bae1dSRodney W. Grimes 	register struct proc **pp = &p->p_pgrp->pg_mem;
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes 	for (; *pp; pp = &(*pp)->p_pgrpnxt) {
275df8bae1dSRodney W. Grimes 		if (*pp == p) {
276df8bae1dSRodney W. Grimes 			*pp = p->p_pgrpnxt;
277df8bae1dSRodney W. Grimes 			break;
278df8bae1dSRodney W. Grimes 		}
279df8bae1dSRodney W. Grimes 	}
280df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
281df8bae1dSRodney W. Grimes 	if (pp == NULL)
282df8bae1dSRodney W. Grimes 		panic("leavepgrp: can't find p in pgrp");
283df8bae1dSRodney W. Grimes #endif
284df8bae1dSRodney W. Grimes 	if (!p->p_pgrp->pg_mem)
285df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
286df8bae1dSRodney W. Grimes 	p->p_pgrp = 0;
287df8bae1dSRodney W. Grimes 	return (0);
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes /*
291df8bae1dSRodney W. Grimes  * delete a process group
292df8bae1dSRodney W. Grimes  */
29326f9a767SRodney W. Grimes void
294df8bae1dSRodney W. Grimes pgdelete(pgrp)
295df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
296df8bae1dSRodney W. Grimes {
297df8bae1dSRodney W. Grimes 	register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)];
298df8bae1dSRodney W. Grimes 
299df8bae1dSRodney W. Grimes 	if (pgrp->pg_session->s_ttyp != NULL &&
300df8bae1dSRodney W. Grimes 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
301df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
302df8bae1dSRodney W. Grimes 	for (; *pgp; pgp = &(*pgp)->pg_hforw) {
303df8bae1dSRodney W. Grimes 		if (*pgp == pgrp) {
304df8bae1dSRodney W. Grimes 			*pgp = pgrp->pg_hforw;
305df8bae1dSRodney W. Grimes 			break;
306df8bae1dSRodney W. Grimes 		}
307df8bae1dSRodney W. Grimes 	}
308df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
309df8bae1dSRodney W. Grimes 	if (pgp == NULL)
310df8bae1dSRodney W. Grimes 		panic("pgdelete: can't find pgrp on hash chain");
311df8bae1dSRodney W. Grimes #endif
312df8bae1dSRodney W. Grimes 	if (--pgrp->pg_session->s_count == 0)
313df8bae1dSRodney W. Grimes 		FREE(pgrp->pg_session, M_SESSION);
314df8bae1dSRodney W. Grimes 	FREE(pgrp, M_PGRP);
315df8bae1dSRodney W. Grimes }
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes static void orphanpg();
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes /*
320df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
321df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
322df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
323df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
324df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
325df8bae1dSRodney W. Grimes  * process group and that of its children.
326df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
327df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
328df8bae1dSRodney W. Grimes  */
32926f9a767SRodney W. Grimes void
330df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
331df8bae1dSRodney W. Grimes 	register struct proc *p;
332df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
333df8bae1dSRodney W. Grimes 	int entering;
334df8bae1dSRodney W. Grimes {
335df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
336df8bae1dSRodney W. Grimes 	register struct session *mysession = pgrp->pg_session;
337df8bae1dSRodney W. Grimes 
338df8bae1dSRodney W. Grimes 	/*
339df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
340df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
341df8bae1dSRodney W. Grimes 	 */
342df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
343df8bae1dSRodney W. Grimes 	    hispgrp->pg_session == mysession)
344df8bae1dSRodney W. Grimes 		if (entering)
345df8bae1dSRodney W. Grimes 			pgrp->pg_jobc++;
346df8bae1dSRodney W. Grimes 		else if (--pgrp->pg_jobc == 0)
347df8bae1dSRodney W. Grimes 			orphanpg(pgrp);
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes 	/*
350df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
351df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
352df8bae1dSRodney W. Grimes 	 * process groups.
353df8bae1dSRodney W. Grimes 	 */
354df8bae1dSRodney W. Grimes 	for (p = p->p_cptr; p; p = p->p_osptr)
355df8bae1dSRodney W. Grimes 		if ((hispgrp = p->p_pgrp) != pgrp &&
356df8bae1dSRodney W. Grimes 		    hispgrp->pg_session == mysession &&
357df8bae1dSRodney W. Grimes 		    p->p_stat != SZOMB)
358df8bae1dSRodney W. Grimes 			if (entering)
359df8bae1dSRodney W. Grimes 				hispgrp->pg_jobc++;
360df8bae1dSRodney W. Grimes 			else if (--hispgrp->pg_jobc == 0)
361df8bae1dSRodney W. Grimes 				orphanpg(hispgrp);
362df8bae1dSRodney W. Grimes }
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes /*
365df8bae1dSRodney W. Grimes  * A process group has become orphaned;
366df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
367df8bae1dSRodney W. Grimes  * hang-up all process in that group.
368df8bae1dSRodney W. Grimes  */
369df8bae1dSRodney W. Grimes static void
370df8bae1dSRodney W. Grimes orphanpg(pg)
371df8bae1dSRodney W. Grimes 	struct pgrp *pg;
372df8bae1dSRodney W. Grimes {
373df8bae1dSRodney W. Grimes 	register struct proc *p;
374df8bae1dSRodney W. Grimes 
375df8bae1dSRodney W. Grimes 	for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
376df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP) {
377df8bae1dSRodney W. Grimes 			for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
378df8bae1dSRodney W. Grimes 				psignal(p, SIGHUP);
379df8bae1dSRodney W. Grimes 				psignal(p, SIGCONT);
380df8bae1dSRodney W. Grimes 			}
381df8bae1dSRodney W. Grimes 			return;
382df8bae1dSRodney W. Grimes 		}
383df8bae1dSRodney W. Grimes 	}
384df8bae1dSRodney W. Grimes }
385df8bae1dSRodney W. Grimes 
386df8bae1dSRodney W. Grimes #ifdef debug
387df8bae1dSRodney W. Grimes /* DEBUG */
388df8bae1dSRodney W. Grimes pgrpdump()
389df8bae1dSRodney W. Grimes {
390df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
391df8bae1dSRodney W. Grimes 	register struct proc *p;
392df8bae1dSRodney W. Grimes 	register i;
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 	for (i=0; i<PIDHSZ; i++) {
395df8bae1dSRodney W. Grimes 		if (pgrphash[i]) {
396df8bae1dSRodney W. Grimes 		  printf("\tindx %d\n", i);
397df8bae1dSRodney W. Grimes 		  for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) {
398df8bae1dSRodney W. Grimes 		    printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n",
399df8bae1dSRodney W. Grimes 			pgrp, pgrp->pg_id, pgrp->pg_session,
400df8bae1dSRodney W. Grimes 			pgrp->pg_session->s_count, pgrp->pg_mem);
401df8bae1dSRodney W. Grimes 		    for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) {
402df8bae1dSRodney W. Grimes 			printf("\t\tpid %d addr %x pgrp %x\n",
403df8bae1dSRodney W. Grimes 				p->p_pid, p, p->p_pgrp);
404df8bae1dSRodney W. Grimes 		    }
405df8bae1dSRodney W. Grimes 		  }
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 		}
408df8bae1dSRodney W. Grimes 	}
409df8bae1dSRodney W. Grimes }
410df8bae1dSRodney W. Grimes #endif /* debug */
411