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