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