kern_proc.c revision 1.30 1 /* $NetBSD: kern_proc.c,v 1.30 1998/09/01 01:02:33 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 struct pool pgrp_pool;
82 struct pool rusage_pool;
83
84 static void orphanpg __P((struct pgrp *));
85 #ifdef DEBUG
86 void pgrpdump __P((void));
87 #endif
88
89 /*
90 * Initialize global process hashing structures.
91 */
92 void
93 procinit()
94 {
95
96 LIST_INIT(&allproc);
97 LIST_INIT(&zombproc);
98 pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
99 pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
100 uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
101 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
102 0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
103 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
104 0, pool_page_alloc_nointr, pool_page_free_nointr, M_PGRP);
105 pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
106 0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
107 pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
108 0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
109 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
110 0, pool_page_alloc_nointr, pool_page_free_nointr, M_ZOMBIE);
111 }
112
113 /*
114 * Change the count associated with number of processes
115 * a given user is using.
116 */
117 int
118 chgproccnt(uid, diff)
119 uid_t uid;
120 int diff;
121 {
122 register struct uidinfo *uip;
123 register struct uihashhead *uipp;
124
125 uipp = UIHASH(uid);
126 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
127 if (uip->ui_uid == uid)
128 break;
129 if (uip) {
130 uip->ui_proccnt += diff;
131 if (uip->ui_proccnt > 0)
132 return (uip->ui_proccnt);
133 if (uip->ui_proccnt < 0)
134 panic("chgproccnt: procs < 0");
135 LIST_REMOVE(uip, ui_hash);
136 FREE(uip, M_PROC);
137 return (0);
138 }
139 if (diff <= 0) {
140 if (diff == 0)
141 return(0);
142 panic("chgproccnt: lost user");
143 }
144 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
145 LIST_INSERT_HEAD(uipp, uip, ui_hash);
146 uip->ui_uid = uid;
147 uip->ui_proccnt = diff;
148 return (diff);
149 }
150
151 /*
152 * Is p an inferior of the current process?
153 */
154 int
155 inferior(p)
156 register struct proc *p;
157 {
158
159 for (; p != curproc; p = p->p_pptr)
160 if (p->p_pid == 0)
161 return (0);
162 return (1);
163 }
164
165 /*
166 * Locate a process by number
167 */
168 struct proc *
169 pfind(pid)
170 register pid_t pid;
171 {
172 register struct proc *p;
173
174 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
175 if (p->p_pid == pid)
176 return (p);
177 return (NULL);
178 }
179
180 /*
181 * Locate a process group by number
182 */
183 struct pgrp *
184 pgfind(pgid)
185 register pid_t pgid;
186 {
187 register struct pgrp *pgrp;
188
189 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
190 if (pgrp->pg_id == pgid)
191 return (pgrp);
192 return (NULL);
193 }
194
195 /*
196 * Move p to a new or existing process group (and session)
197 */
198 int
199 enterpgrp(p, pgid, mksess)
200 register struct proc *p;
201 pid_t pgid;
202 int mksess;
203 {
204 register struct pgrp *pgrp = pgfind(pgid);
205
206 #ifdef DIAGNOSTIC
207 if (pgrp != NULL && mksess) /* firewalls */
208 panic("enterpgrp: setsid into non-empty pgrp");
209 if (SESS_LEADER(p))
210 panic("enterpgrp: session leader attempted setpgrp");
211 #endif
212 if (pgrp == NULL) {
213 pid_t savepid = p->p_pid;
214 struct proc *np;
215 /*
216 * new process group
217 */
218 #ifdef DIAGNOSTIC
219 if (p->p_pid != pgid)
220 panic("enterpgrp: new pgrp and pid != pgid");
221 #endif
222 pgrp = pool_get(&pgrp_pool, PR_WAITOK);
223 if ((np = pfind(savepid)) == NULL || np != p)
224 return (ESRCH);
225 if (mksess) {
226 register struct session *sess;
227
228 /*
229 * new session
230 */
231 MALLOC(sess, struct session *, sizeof(struct session),
232 M_SESSION, M_WAITOK);
233 sess->s_sid = p->p_pid;
234 sess->s_leader = p;
235 sess->s_count = 1;
236 sess->s_ttyvp = NULL;
237 sess->s_ttyp = NULL;
238 memcpy(sess->s_login, p->p_session->s_login,
239 sizeof(sess->s_login));
240 p->p_flag &= ~P_CONTROLT;
241 pgrp->pg_session = sess;
242 #ifdef DIAGNOSTIC
243 if (p != curproc)
244 panic("enterpgrp: mksession and p != curproc");
245 #endif
246 } else {
247 pgrp->pg_session = p->p_session;
248 pgrp->pg_session->s_count++;
249 }
250 pgrp->pg_id = pgid;
251 LIST_INIT(&pgrp->pg_members);
252 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
253 pgrp->pg_jobc = 0;
254 } else if (pgrp == p->p_pgrp)
255 return (0);
256
257 /*
258 * Adjust eligibility of affected pgrps to participate in job control.
259 * Increment eligibility counts before decrementing, otherwise we
260 * could reach 0 spuriously during the first call.
261 */
262 fixjobc(p, pgrp, 1);
263 fixjobc(p, p->p_pgrp, 0);
264
265 LIST_REMOVE(p, p_pglist);
266 if (p->p_pgrp->pg_members.lh_first == 0)
267 pgdelete(p->p_pgrp);
268 p->p_pgrp = pgrp;
269 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
270 return (0);
271 }
272
273 /*
274 * remove process from process group
275 */
276 int
277 leavepgrp(p)
278 register struct proc *p;
279 {
280
281 LIST_REMOVE(p, p_pglist);
282 if (p->p_pgrp->pg_members.lh_first == 0)
283 pgdelete(p->p_pgrp);
284 p->p_pgrp = 0;
285 return (0);
286 }
287
288 /*
289 * delete a process group
290 */
291 void
292 pgdelete(pgrp)
293 register struct pgrp *pgrp;
294 {
295
296 if (pgrp->pg_session->s_ttyp != NULL &&
297 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
298 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
299 LIST_REMOVE(pgrp, pg_hash);
300 if (--pgrp->pg_session->s_count == 0)
301 FREE(pgrp->pg_session, M_SESSION);
302 pool_put(&pgrp_pool, pgrp);
303 }
304
305 /*
306 * Adjust pgrp jobc counters when specified process changes process group.
307 * We count the number of processes in each process group that "qualify"
308 * the group for terminal job control (those with a parent in a different
309 * process group of the same session). If that count reaches zero, the
310 * process group becomes orphaned. Check both the specified process'
311 * process group and that of its children.
312 * entering == 0 => p is leaving specified group.
313 * entering == 1 => p is entering specified group.
314 */
315 void
316 fixjobc(p, pgrp, entering)
317 register struct proc *p;
318 register struct pgrp *pgrp;
319 int entering;
320 {
321 register struct pgrp *hispgrp;
322 register struct session *mysession = pgrp->pg_session;
323
324 /*
325 * Check p's parent to see whether p qualifies its own process
326 * group; if so, adjust count for p's process group.
327 */
328 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
329 hispgrp->pg_session == mysession) {
330 if (entering)
331 pgrp->pg_jobc++;
332 else if (--pgrp->pg_jobc == 0)
333 orphanpg(pgrp);
334 }
335
336 /*
337 * Check this process' children to see whether they qualify
338 * their process groups; if so, adjust counts for children's
339 * process groups.
340 */
341 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
342 if ((hispgrp = p->p_pgrp) != pgrp &&
343 hispgrp->pg_session == mysession &&
344 p->p_stat != SZOMB) {
345 if (entering)
346 hispgrp->pg_jobc++;
347 else if (--hispgrp->pg_jobc == 0)
348 orphanpg(hispgrp);
349 }
350 }
351 }
352
353 /*
354 * A process group has become orphaned;
355 * if there are any stopped processes in the group,
356 * hang-up all process in that group.
357 */
358 static void
359 orphanpg(pg)
360 struct pgrp *pg;
361 {
362 register struct proc *p;
363
364 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
365 if (p->p_stat == SSTOP) {
366 for (p = pg->pg_members.lh_first; p != 0;
367 p = p->p_pglist.le_next) {
368 psignal(p, SIGHUP);
369 psignal(p, SIGCONT);
370 }
371 return;
372 }
373 }
374 }
375
376 #ifdef DEBUG
377 void
378 pgrpdump()
379 {
380 register struct pgrp *pgrp;
381 register struct proc *p;
382 register int i;
383
384 for (i = 0; i <= pgrphash; i++) {
385 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
386 printf("\tindx %d\n", i);
387 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
388 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
389 pgrp, pgrp->pg_id, pgrp->pg_session,
390 pgrp->pg_session->s_count,
391 pgrp->pg_members.lh_first);
392 for (p = pgrp->pg_members.lh_first; p != 0;
393 p = p->p_pglist.le_next) {
394 printf("\t\tpid %d addr %p pgrp %p\n",
395 p->p_pid, p, p->p_pgrp);
396 }
397 }
398 }
399 }
400 }
401 #endif /* DEBUG */
402