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