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