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