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