kern_proc.c revision 1.4 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.2 cgd * from: @(#)kern_proc.c 7.16 (Berkeley) 6/28/91
34 1.4 andrew * $Id: kern_proc.c,v 1.4 1993/06/27 06:01:40 andrew Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #include "param.h"
38 1.1 cgd #include "systm.h"
39 1.1 cgd #include "kernel.h"
40 1.1 cgd #include "proc.h"
41 1.1 cgd #include "buf.h"
42 1.1 cgd #include "acct.h"
43 1.1 cgd #include "wait.h"
44 1.1 cgd #include "file.h"
45 1.1 cgd #include "../ufs/quota.h"
46 1.1 cgd #include "uio.h"
47 1.1 cgd #include "malloc.h"
48 1.1 cgd #include "mbuf.h"
49 1.1 cgd #include "ioctl.h"
50 1.1 cgd #include "tty.h"
51 1.1 cgd
52 1.4 andrew /* static */ void pgdelete __P((struct pgrp *pgrp));
53 1.4 andrew
54 1.1 cgd /*
55 1.1 cgd * Is p an inferior of the current process?
56 1.1 cgd */
57 1.4 andrew int
58 1.1 cgd inferior(p)
59 1.1 cgd register struct proc *p;
60 1.1 cgd {
61 1.1 cgd
62 1.1 cgd for (; p != curproc; p = p->p_pptr)
63 1.1 cgd if (p->p_pid == 0)
64 1.1 cgd return (0);
65 1.1 cgd return (1);
66 1.1 cgd }
67 1.1 cgd
68 1.1 cgd /*
69 1.1 cgd * Locate a process by number
70 1.1 cgd */
71 1.1 cgd struct proc *
72 1.1 cgd pfind(pid)
73 1.1 cgd register pid;
74 1.1 cgd {
75 1.1 cgd register struct proc *p = pidhash[PIDHASH(pid)];
76 1.1 cgd
77 1.1 cgd for (; p; p = p->p_hash)
78 1.1 cgd if (p->p_pid == pid)
79 1.1 cgd return (p);
80 1.1 cgd return ((struct proc *)0);
81 1.1 cgd }
82 1.1 cgd
83 1.1 cgd /*
84 1.1 cgd * Locate a process group by number
85 1.1 cgd */
86 1.1 cgd struct pgrp *
87 1.1 cgd pgfind(pgid)
88 1.1 cgd register pid_t pgid;
89 1.1 cgd {
90 1.1 cgd register struct pgrp *pgrp = pgrphash[PIDHASH(pgid)];
91 1.1 cgd
92 1.1 cgd for (; pgrp; pgrp = pgrp->pg_hforw)
93 1.1 cgd if (pgrp->pg_id == pgid)
94 1.1 cgd return (pgrp);
95 1.1 cgd return ((struct pgrp *)0);
96 1.1 cgd }
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * Move p to a new or existing process group (and session)
100 1.1 cgd */
101 1.4 andrew void
102 1.1 cgd enterpgrp(p, pgid, mksess)
103 1.1 cgd register struct proc *p;
104 1.1 cgd pid_t pgid;
105 1.4 andrew int mksess;
106 1.1 cgd {
107 1.1 cgd register struct pgrp *pgrp = pgfind(pgid);
108 1.1 cgd register struct proc **pp;
109 1.1 cgd int n;
110 1.1 cgd
111 1.1 cgd #ifdef DIAGNOSTIC
112 1.1 cgd if (pgrp && mksess) /* firewalls */
113 1.1 cgd panic("enterpgrp: setsid into non-empty pgrp");
114 1.1 cgd if (SESS_LEADER(p))
115 1.1 cgd panic("enterpgrp: session leader attempted setpgrp");
116 1.1 cgd #endif
117 1.1 cgd if (pgrp == NULL) {
118 1.1 cgd /*
119 1.1 cgd * new process group
120 1.1 cgd */
121 1.1 cgd #ifdef DIAGNOSTIC
122 1.1 cgd if (p->p_pid != pgid)
123 1.1 cgd panic("enterpgrp: new pgrp and pid != pgid");
124 1.1 cgd #endif
125 1.1 cgd MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
126 1.1 cgd M_WAITOK);
127 1.1 cgd if (mksess) {
128 1.1 cgd register struct session *sess;
129 1.1 cgd
130 1.1 cgd /*
131 1.1 cgd * new session
132 1.1 cgd */
133 1.1 cgd MALLOC(sess, struct session *, sizeof(struct session),
134 1.1 cgd M_SESSION, M_WAITOK);
135 1.1 cgd sess->s_leader = p;
136 1.1 cgd sess->s_count = 1;
137 1.1 cgd sess->s_ttyvp = NULL;
138 1.1 cgd sess->s_ttyp = NULL;
139 1.1 cgd bcopy(p->p_session->s_login, sess->s_login,
140 1.1 cgd sizeof(sess->s_login));
141 1.1 cgd p->p_flag &= ~SCTTY;
142 1.1 cgd pgrp->pg_session = sess;
143 1.1 cgd #ifdef DIAGNOSTIC
144 1.1 cgd if (p != curproc)
145 1.1 cgd panic("enterpgrp: mksession and p != curproc");
146 1.1 cgd #endif
147 1.1 cgd } else {
148 1.1 cgd pgrp->pg_session = p->p_session;
149 1.1 cgd pgrp->pg_session->s_count++;
150 1.1 cgd }
151 1.1 cgd pgrp->pg_id = pgid;
152 1.1 cgd pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)];
153 1.1 cgd pgrphash[n] = pgrp;
154 1.1 cgd pgrp->pg_jobc = 0;
155 1.1 cgd pgrp->pg_mem = NULL;
156 1.1 cgd } else if (pgrp == p->p_pgrp)
157 1.1 cgd return;
158 1.1 cgd
159 1.1 cgd /*
160 1.1 cgd * Adjust eligibility of affected pgrps to participate in job control.
161 1.1 cgd * Increment eligibility counts before decrementing, otherwise we
162 1.1 cgd * could reach 0 spuriously during the first call.
163 1.1 cgd */
164 1.1 cgd fixjobc(p, pgrp, 1);
165 1.1 cgd fixjobc(p, p->p_pgrp, 0);
166 1.1 cgd
167 1.1 cgd /*
168 1.1 cgd * unlink p from old process group
169 1.1 cgd */
170 1.1 cgd for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt)
171 1.1 cgd if (*pp == p) {
172 1.1 cgd *pp = p->p_pgrpnxt;
173 1.1 cgd goto done;
174 1.1 cgd }
175 1.1 cgd panic("enterpgrp: can't find p on old pgrp");
176 1.1 cgd done:
177 1.1 cgd /*
178 1.1 cgd * delete old if empty
179 1.1 cgd */
180 1.1 cgd if (p->p_pgrp->pg_mem == 0)
181 1.1 cgd pgdelete(p->p_pgrp);
182 1.1 cgd /*
183 1.1 cgd * link into new one
184 1.1 cgd */
185 1.1 cgd p->p_pgrp = pgrp;
186 1.1 cgd p->p_pgrpnxt = pgrp->pg_mem;
187 1.1 cgd pgrp->pg_mem = p;
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * remove process from process group
192 1.1 cgd */
193 1.4 andrew void
194 1.1 cgd leavepgrp(p)
195 1.1 cgd register struct proc *p;
196 1.1 cgd {
197 1.1 cgd register struct proc **pp = &p->p_pgrp->pg_mem;
198 1.1 cgd
199 1.1 cgd for (; *pp; pp = &(*pp)->p_pgrpnxt)
200 1.1 cgd if (*pp == p) {
201 1.1 cgd *pp = p->p_pgrpnxt;
202 1.1 cgd goto done;
203 1.1 cgd }
204 1.1 cgd panic("leavepgrp: can't find p in pgrp");
205 1.1 cgd done:
206 1.1 cgd if (!p->p_pgrp->pg_mem)
207 1.1 cgd pgdelete(p->p_pgrp);
208 1.1 cgd p->p_pgrp = 0;
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd /*
212 1.4 andrew * delete a process group [internal]
213 1.1 cgd */
214 1.4 andrew void
215 1.1 cgd pgdelete(pgrp)
216 1.1 cgd register struct pgrp *pgrp;
217 1.1 cgd {
218 1.1 cgd register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)];
219 1.1 cgd
220 1.1 cgd if (pgrp->pg_session->s_ttyp != NULL &&
221 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
222 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp = NULL;
223 1.1 cgd for (; *pgp; pgp = &(*pgp)->pg_hforw)
224 1.1 cgd if (*pgp == pgrp) {
225 1.1 cgd *pgp = pgrp->pg_hforw;
226 1.1 cgd goto done;
227 1.1 cgd }
228 1.1 cgd panic("pgdelete: can't find pgrp on hash chain");
229 1.1 cgd done:
230 1.1 cgd if (--pgrp->pg_session->s_count == 0)
231 1.1 cgd FREE(pgrp->pg_session, M_SESSION);
232 1.1 cgd FREE(pgrp, M_PGRP);
233 1.1 cgd }
234 1.1 cgd
235 1.4 andrew static void orphanpg();
236 1.1 cgd
237 1.1 cgd /*
238 1.1 cgd * Adjust pgrp jobc counters when specified process changes process group.
239 1.1 cgd * We count the number of processes in each process group that "qualify"
240 1.1 cgd * the group for terminal job control (those with a parent in a different
241 1.1 cgd * process group of the same session). If that count reaches zero, the
242 1.1 cgd * process group becomes orphaned. Check both the specified process'
243 1.1 cgd * process group and that of its children.
244 1.1 cgd * entering == 0 => p is leaving specified group.
245 1.1 cgd * entering == 1 => p is entering specified group.
246 1.1 cgd */
247 1.4 andrew void
248 1.1 cgd fixjobc(p, pgrp, entering)
249 1.1 cgd register struct proc *p;
250 1.1 cgd register struct pgrp *pgrp;
251 1.1 cgd int entering;
252 1.1 cgd {
253 1.1 cgd register struct pgrp *hispgrp;
254 1.1 cgd register struct session *mysession = pgrp->pg_session;
255 1.1 cgd
256 1.1 cgd /*
257 1.1 cgd * Check p's parent to see whether p qualifies its own process
258 1.1 cgd * group; if so, adjust count for p's process group.
259 1.1 cgd */
260 1.1 cgd if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
261 1.1 cgd hispgrp->pg_session == mysession)
262 1.1 cgd if (entering)
263 1.1 cgd pgrp->pg_jobc++;
264 1.1 cgd else if (--pgrp->pg_jobc == 0)
265 1.1 cgd orphanpg(pgrp);
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * Check this process' children to see whether they qualify
269 1.1 cgd * their process groups; if so, adjust counts for children's
270 1.1 cgd * process groups.
271 1.1 cgd */
272 1.1 cgd for (p = p->p_cptr; p; p = p->p_osptr)
273 1.1 cgd if ((hispgrp = p->p_pgrp) != pgrp &&
274 1.1 cgd hispgrp->pg_session == mysession &&
275 1.1 cgd p->p_stat != SZOMB)
276 1.1 cgd if (entering)
277 1.1 cgd hispgrp->pg_jobc++;
278 1.1 cgd else if (--hispgrp->pg_jobc == 0)
279 1.1 cgd orphanpg(hispgrp);
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd /*
283 1.1 cgd * A process group has become orphaned;
284 1.1 cgd * if there are any stopped processes in the group,
285 1.1 cgd * hang-up all process in that group.
286 1.1 cgd */
287 1.4 andrew static void
288 1.1 cgd orphanpg(pg)
289 1.1 cgd struct pgrp *pg;
290 1.1 cgd {
291 1.1 cgd register struct proc *p;
292 1.1 cgd
293 1.1 cgd for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
294 1.1 cgd if (p->p_stat == SSTOP) {
295 1.1 cgd for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
296 1.1 cgd psignal(p, SIGHUP);
297 1.1 cgd psignal(p, SIGCONT);
298 1.1 cgd }
299 1.1 cgd return;
300 1.1 cgd }
301 1.1 cgd }
302 1.1 cgd }
303 1.1 cgd
304 1.1 cgd #ifdef debug
305 1.1 cgd /* DEBUG */
306 1.4 andrew void
307 1.1 cgd pgrpdump()
308 1.1 cgd {
309 1.1 cgd register struct pgrp *pgrp;
310 1.1 cgd register struct proc *p;
311 1.1 cgd register i;
312 1.1 cgd
313 1.1 cgd for (i=0; i<PIDHSZ; i++) {
314 1.1 cgd if (pgrphash[i]) {
315 1.1 cgd printf("\tindx %d\n", i);
316 1.1 cgd for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) {
317 1.1 cgd printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n",
318 1.1 cgd pgrp, pgrp->pg_id, pgrp->pg_session,
319 1.1 cgd pgrp->pg_session->s_count, pgrp->pg_mem);
320 1.1 cgd for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) {
321 1.1 cgd printf("\t\tpid %d addr %x pgrp %x\n",
322 1.1 cgd p->p_pid, p, p->p_pgrp);
323 1.1 cgd }
324 1.1 cgd }
325 1.1 cgd
326 1.1 cgd }
327 1.1 cgd }
328 1.1 cgd }
329 1.1 cgd #endif /* debug */
330