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