kern_proc.c revision 1.5 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.5 mycroft * $Id: kern_proc.c,v 1.5 1993/12/18 04:20:57 mycroft Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.5 mycroft #include <sys/param.h>
38 1.5 mycroft #include <sys/systm.h>
39 1.5 mycroft #include <sys/kernel.h>
40 1.5 mycroft #include <sys/proc.h>
41 1.5 mycroft #include <sys/buf.h>
42 1.5 mycroft #include <sys/acct.h>
43 1.5 mycroft #include <sys/wait.h>
44 1.5 mycroft #include <sys/file.h>
45 1.5 mycroft #include <sys/uio.h>
46 1.5 mycroft #include <sys/malloc.h>
47 1.5 mycroft #include <sys/mbuf.h>
48 1.5 mycroft #include <sys/ioctl.h>
49 1.5 mycroft #include <sys/tty.h>
50 1.5 mycroft
51 1.5 mycroft #include <ufs/quota.h>
52 1.1 cgd
53 1.4 andrew /* static */ void pgdelete __P((struct pgrp *pgrp));
54 1.4 andrew
55 1.1 cgd /*
56 1.1 cgd * Is p an inferior of the current process?
57 1.1 cgd */
58 1.4 andrew int
59 1.1 cgd inferior(p)
60 1.1 cgd register struct proc *p;
61 1.1 cgd {
62 1.1 cgd
63 1.1 cgd for (; p != curproc; p = p->p_pptr)
64 1.1 cgd if (p->p_pid == 0)
65 1.1 cgd return (0);
66 1.1 cgd return (1);
67 1.1 cgd }
68 1.1 cgd
69 1.1 cgd /*
70 1.1 cgd * Locate a process by number
71 1.1 cgd */
72 1.1 cgd struct proc *
73 1.1 cgd pfind(pid)
74 1.1 cgd register pid;
75 1.1 cgd {
76 1.1 cgd register struct proc *p = pidhash[PIDHASH(pid)];
77 1.1 cgd
78 1.1 cgd for (; p; p = p->p_hash)
79 1.1 cgd if (p->p_pid == pid)
80 1.1 cgd return (p);
81 1.1 cgd return ((struct proc *)0);
82 1.1 cgd }
83 1.1 cgd
84 1.1 cgd /*
85 1.1 cgd * Locate a process group by number
86 1.1 cgd */
87 1.1 cgd struct pgrp *
88 1.1 cgd pgfind(pgid)
89 1.1 cgd register pid_t pgid;
90 1.1 cgd {
91 1.1 cgd register struct pgrp *pgrp = pgrphash[PIDHASH(pgid)];
92 1.1 cgd
93 1.1 cgd for (; pgrp; pgrp = pgrp->pg_hforw)
94 1.1 cgd if (pgrp->pg_id == pgid)
95 1.1 cgd return (pgrp);
96 1.1 cgd return ((struct pgrp *)0);
97 1.1 cgd }
98 1.1 cgd
99 1.1 cgd /*
100 1.1 cgd * Move p to a new or existing process group (and session)
101 1.1 cgd */
102 1.4 andrew void
103 1.1 cgd enterpgrp(p, pgid, mksess)
104 1.1 cgd register struct proc *p;
105 1.1 cgd pid_t pgid;
106 1.4 andrew int mksess;
107 1.1 cgd {
108 1.1 cgd register struct pgrp *pgrp = pgfind(pgid);
109 1.1 cgd register struct proc **pp;
110 1.1 cgd int n;
111 1.1 cgd
112 1.1 cgd #ifdef DIAGNOSTIC
113 1.1 cgd if (pgrp && mksess) /* firewalls */
114 1.1 cgd panic("enterpgrp: setsid into non-empty pgrp");
115 1.1 cgd if (SESS_LEADER(p))
116 1.1 cgd panic("enterpgrp: session leader attempted setpgrp");
117 1.1 cgd #endif
118 1.1 cgd if (pgrp == NULL) {
119 1.1 cgd /*
120 1.1 cgd * new process group
121 1.1 cgd */
122 1.1 cgd #ifdef DIAGNOSTIC
123 1.1 cgd if (p->p_pid != pgid)
124 1.1 cgd panic("enterpgrp: new pgrp and pid != pgid");
125 1.1 cgd #endif
126 1.1 cgd MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
127 1.1 cgd M_WAITOK);
128 1.1 cgd if (mksess) {
129 1.1 cgd register struct session *sess;
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * new session
133 1.1 cgd */
134 1.1 cgd MALLOC(sess, struct session *, sizeof(struct session),
135 1.1 cgd M_SESSION, M_WAITOK);
136 1.1 cgd sess->s_leader = p;
137 1.1 cgd sess->s_count = 1;
138 1.1 cgd sess->s_ttyvp = NULL;
139 1.1 cgd sess->s_ttyp = NULL;
140 1.1 cgd bcopy(p->p_session->s_login, sess->s_login,
141 1.1 cgd sizeof(sess->s_login));
142 1.1 cgd p->p_flag &= ~SCTTY;
143 1.1 cgd pgrp->pg_session = sess;
144 1.1 cgd #ifdef DIAGNOSTIC
145 1.1 cgd if (p != curproc)
146 1.1 cgd panic("enterpgrp: mksession and p != curproc");
147 1.1 cgd #endif
148 1.1 cgd } else {
149 1.1 cgd pgrp->pg_session = p->p_session;
150 1.1 cgd pgrp->pg_session->s_count++;
151 1.1 cgd }
152 1.1 cgd pgrp->pg_id = pgid;
153 1.1 cgd pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)];
154 1.1 cgd pgrphash[n] = pgrp;
155 1.1 cgd pgrp->pg_jobc = 0;
156 1.1 cgd pgrp->pg_mem = NULL;
157 1.1 cgd } else if (pgrp == p->p_pgrp)
158 1.1 cgd return;
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Adjust eligibility of affected pgrps to participate in job control.
162 1.1 cgd * Increment eligibility counts before decrementing, otherwise we
163 1.1 cgd * could reach 0 spuriously during the first call.
164 1.1 cgd */
165 1.1 cgd fixjobc(p, pgrp, 1);
166 1.1 cgd fixjobc(p, p->p_pgrp, 0);
167 1.1 cgd
168 1.1 cgd /*
169 1.1 cgd * unlink p from old process group
170 1.1 cgd */
171 1.1 cgd for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt)
172 1.1 cgd if (*pp == p) {
173 1.1 cgd *pp = p->p_pgrpnxt;
174 1.1 cgd goto done;
175 1.1 cgd }
176 1.1 cgd panic("enterpgrp: can't find p on old pgrp");
177 1.1 cgd done:
178 1.1 cgd /*
179 1.1 cgd * delete old if empty
180 1.1 cgd */
181 1.1 cgd if (p->p_pgrp->pg_mem == 0)
182 1.1 cgd pgdelete(p->p_pgrp);
183 1.1 cgd /*
184 1.1 cgd * link into new one
185 1.1 cgd */
186 1.1 cgd p->p_pgrp = pgrp;
187 1.1 cgd p->p_pgrpnxt = pgrp->pg_mem;
188 1.1 cgd pgrp->pg_mem = p;
189 1.1 cgd }
190 1.1 cgd
191 1.1 cgd /*
192 1.1 cgd * remove process from process group
193 1.1 cgd */
194 1.4 andrew void
195 1.1 cgd leavepgrp(p)
196 1.1 cgd register struct proc *p;
197 1.1 cgd {
198 1.1 cgd register struct proc **pp = &p->p_pgrp->pg_mem;
199 1.1 cgd
200 1.1 cgd for (; *pp; pp = &(*pp)->p_pgrpnxt)
201 1.1 cgd if (*pp == p) {
202 1.1 cgd *pp = p->p_pgrpnxt;
203 1.1 cgd goto done;
204 1.1 cgd }
205 1.1 cgd panic("leavepgrp: can't find p in pgrp");
206 1.1 cgd done:
207 1.1 cgd if (!p->p_pgrp->pg_mem)
208 1.1 cgd pgdelete(p->p_pgrp);
209 1.1 cgd p->p_pgrp = 0;
210 1.1 cgd }
211 1.1 cgd
212 1.1 cgd /*
213 1.4 andrew * delete a process group [internal]
214 1.1 cgd */
215 1.4 andrew void
216 1.1 cgd pgdelete(pgrp)
217 1.1 cgd register struct pgrp *pgrp;
218 1.1 cgd {
219 1.1 cgd register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)];
220 1.1 cgd
221 1.1 cgd if (pgrp->pg_session->s_ttyp != NULL &&
222 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
223 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp = NULL;
224 1.1 cgd for (; *pgp; pgp = &(*pgp)->pg_hforw)
225 1.1 cgd if (*pgp == pgrp) {
226 1.1 cgd *pgp = pgrp->pg_hforw;
227 1.1 cgd goto done;
228 1.1 cgd }
229 1.1 cgd panic("pgdelete: can't find pgrp on hash chain");
230 1.1 cgd done:
231 1.1 cgd if (--pgrp->pg_session->s_count == 0)
232 1.1 cgd FREE(pgrp->pg_session, M_SESSION);
233 1.1 cgd FREE(pgrp, M_PGRP);
234 1.1 cgd }
235 1.1 cgd
236 1.4 andrew static void orphanpg();
237 1.1 cgd
238 1.1 cgd /*
239 1.1 cgd * Adjust pgrp jobc counters when specified process changes process group.
240 1.1 cgd * We count the number of processes in each process group that "qualify"
241 1.1 cgd * the group for terminal job control (those with a parent in a different
242 1.1 cgd * process group of the same session). If that count reaches zero, the
243 1.1 cgd * process group becomes orphaned. Check both the specified process'
244 1.1 cgd * process group and that of its children.
245 1.1 cgd * entering == 0 => p is leaving specified group.
246 1.1 cgd * entering == 1 => p is entering specified group.
247 1.1 cgd */
248 1.4 andrew void
249 1.1 cgd fixjobc(p, pgrp, entering)
250 1.1 cgd register struct proc *p;
251 1.1 cgd register struct pgrp *pgrp;
252 1.1 cgd int entering;
253 1.1 cgd {
254 1.1 cgd register struct pgrp *hispgrp;
255 1.1 cgd register struct session *mysession = pgrp->pg_session;
256 1.1 cgd
257 1.1 cgd /*
258 1.1 cgd * Check p's parent to see whether p qualifies its own process
259 1.1 cgd * group; if so, adjust count for p's process group.
260 1.1 cgd */
261 1.1 cgd if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
262 1.1 cgd hispgrp->pg_session == mysession)
263 1.1 cgd if (entering)
264 1.1 cgd pgrp->pg_jobc++;
265 1.1 cgd else if (--pgrp->pg_jobc == 0)
266 1.1 cgd orphanpg(pgrp);
267 1.1 cgd
268 1.1 cgd /*
269 1.1 cgd * Check this process' children to see whether they qualify
270 1.1 cgd * their process groups; if so, adjust counts for children's
271 1.1 cgd * process groups.
272 1.1 cgd */
273 1.1 cgd for (p = p->p_cptr; p; p = p->p_osptr)
274 1.1 cgd if ((hispgrp = p->p_pgrp) != pgrp &&
275 1.1 cgd hispgrp->pg_session == mysession &&
276 1.1 cgd p->p_stat != SZOMB)
277 1.1 cgd if (entering)
278 1.1 cgd hispgrp->pg_jobc++;
279 1.1 cgd else if (--hispgrp->pg_jobc == 0)
280 1.1 cgd orphanpg(hispgrp);
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd /*
284 1.1 cgd * A process group has become orphaned;
285 1.1 cgd * if there are any stopped processes in the group,
286 1.1 cgd * hang-up all process in that group.
287 1.1 cgd */
288 1.4 andrew static void
289 1.1 cgd orphanpg(pg)
290 1.1 cgd struct pgrp *pg;
291 1.1 cgd {
292 1.1 cgd register struct proc *p;
293 1.1 cgd
294 1.1 cgd for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
295 1.1 cgd if (p->p_stat == SSTOP) {
296 1.1 cgd for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
297 1.1 cgd psignal(p, SIGHUP);
298 1.1 cgd psignal(p, SIGCONT);
299 1.1 cgd }
300 1.1 cgd return;
301 1.1 cgd }
302 1.1 cgd }
303 1.1 cgd }
304 1.1 cgd
305 1.1 cgd #ifdef debug
306 1.1 cgd /* DEBUG */
307 1.4 andrew void
308 1.1 cgd pgrpdump()
309 1.1 cgd {
310 1.1 cgd register struct pgrp *pgrp;
311 1.1 cgd register struct proc *p;
312 1.1 cgd register i;
313 1.1 cgd
314 1.1 cgd for (i=0; i<PIDHSZ; i++) {
315 1.1 cgd if (pgrphash[i]) {
316 1.1 cgd printf("\tindx %d\n", i);
317 1.1 cgd for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) {
318 1.1 cgd printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n",
319 1.1 cgd pgrp, pgrp->pg_id, pgrp->pg_session,
320 1.1 cgd pgrp->pg_session->s_count, pgrp->pg_mem);
321 1.1 cgd for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) {
322 1.1 cgd printf("\t\tpid %d addr %x pgrp %x\n",
323 1.1 cgd p->p_pid, p, p->p_pgrp);
324 1.1 cgd }
325 1.1 cgd }
326 1.1 cgd
327 1.1 cgd }
328 1.1 cgd }
329 1.1 cgd }
330 1.1 cgd #endif /* debug */
331