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