kern_proc.c revision 1.12 1 1.12 mycroft /* $NetBSD: kern_proc.c,v 1.12 1995/03/19 23:44:49 mycroft 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.9 cgd * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
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.5 mycroft #include <sys/buf.h>
44 1.5 mycroft #include <sys/acct.h>
45 1.5 mycroft #include <sys/wait.h>
46 1.5 mycroft #include <sys/file.h>
47 1.8 mycroft #include <ufs/ufs/quota.h>
48 1.5 mycroft #include <sys/uio.h>
49 1.5 mycroft #include <sys/malloc.h>
50 1.5 mycroft #include <sys/mbuf.h>
51 1.5 mycroft #include <sys/ioctl.h>
52 1.5 mycroft #include <sys/tty.h>
53 1.11 cgd #include <sys/signalvar.h>
54 1.5 mycroft
55 1.7 cgd /*
56 1.7 cgd * Structure associated with user cacheing.
57 1.7 cgd */
58 1.7 cgd struct uidinfo {
59 1.10 mycroft LIST_ENTRY(uidinfo) ui_hash;
60 1.7 cgd uid_t ui_uid;
61 1.7 cgd long ui_proccnt;
62 1.10 mycroft };
63 1.10 mycroft #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
64 1.10 mycroft LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
65 1.10 mycroft u_long uihash; /* size of hash table - 1 */
66 1.7 cgd
67 1.7 cgd /*
68 1.10 mycroft * Other process lists
69 1.7 cgd */
70 1.10 mycroft struct pidhashhead *pidhashtbl;
71 1.10 mycroft u_long pidhash;
72 1.10 mycroft struct pgrphashhead *pgrphashtbl;
73 1.10 mycroft u_long pgrphash;
74 1.10 mycroft struct proclist allproc;
75 1.10 mycroft struct proclist zombproc;
76 1.10 mycroft
77 1.10 mycroft /*
78 1.10 mycroft * Initialize global process hashing structures.
79 1.10 mycroft */
80 1.11 cgd void
81 1.10 mycroft procinit()
82 1.7 cgd {
83 1.7 cgd
84 1.10 mycroft LIST_INIT(&allproc);
85 1.10 mycroft LIST_INIT(&zombproc);
86 1.10 mycroft pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
87 1.10 mycroft pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
88 1.7 cgd uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
89 1.7 cgd }
90 1.1 cgd
91 1.7 cgd /*
92 1.7 cgd * Change the count associated with number of processes
93 1.7 cgd * a given user is using.
94 1.7 cgd */
95 1.7 cgd int
96 1.7 cgd chgproccnt(uid, diff)
97 1.7 cgd uid_t uid;
98 1.7 cgd int diff;
99 1.7 cgd {
100 1.10 mycroft register struct uidinfo *uip;
101 1.10 mycroft register struct uihashhead *uipp;
102 1.7 cgd
103 1.10 mycroft uipp = UIHASH(uid);
104 1.10 mycroft for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
105 1.7 cgd if (uip->ui_uid == uid)
106 1.7 cgd break;
107 1.7 cgd if (uip) {
108 1.7 cgd uip->ui_proccnt += diff;
109 1.7 cgd if (uip->ui_proccnt > 0)
110 1.7 cgd return (uip->ui_proccnt);
111 1.7 cgd if (uip->ui_proccnt < 0)
112 1.7 cgd panic("chgproccnt: procs < 0");
113 1.10 mycroft LIST_REMOVE(uip, ui_hash);
114 1.7 cgd FREE(uip, M_PROC);
115 1.7 cgd return (0);
116 1.7 cgd }
117 1.7 cgd if (diff <= 0) {
118 1.7 cgd if (diff == 0)
119 1.7 cgd return(0);
120 1.7 cgd panic("chgproccnt: lost user");
121 1.7 cgd }
122 1.7 cgd MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
123 1.10 mycroft LIST_INSERT_HEAD(uipp, uip, ui_hash);
124 1.7 cgd uip->ui_uid = uid;
125 1.7 cgd uip->ui_proccnt = diff;
126 1.7 cgd return (diff);
127 1.7 cgd }
128 1.4 andrew
129 1.1 cgd /*
130 1.1 cgd * Is p an inferior of the current process?
131 1.1 cgd */
132 1.11 cgd int
133 1.1 cgd inferior(p)
134 1.1 cgd register struct proc *p;
135 1.1 cgd {
136 1.1 cgd
137 1.1 cgd for (; p != curproc; p = p->p_pptr)
138 1.1 cgd if (p->p_pid == 0)
139 1.1 cgd return (0);
140 1.1 cgd return (1);
141 1.1 cgd }
142 1.1 cgd
143 1.1 cgd /*
144 1.1 cgd * Locate a process by number
145 1.1 cgd */
146 1.1 cgd struct proc *
147 1.1 cgd pfind(pid)
148 1.6 cgd register pid_t pid;
149 1.1 cgd {
150 1.7 cgd register struct proc *p;
151 1.1 cgd
152 1.10 mycroft for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
153 1.1 cgd if (p->p_pid == pid)
154 1.1 cgd return (p);
155 1.7 cgd return (NULL);
156 1.1 cgd }
157 1.1 cgd
158 1.1 cgd /*
159 1.1 cgd * Locate a process group by number
160 1.1 cgd */
161 1.1 cgd struct pgrp *
162 1.1 cgd pgfind(pgid)
163 1.1 cgd register pid_t pgid;
164 1.1 cgd {
165 1.7 cgd register struct pgrp *pgrp;
166 1.1 cgd
167 1.10 mycroft for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
168 1.1 cgd if (pgrp->pg_id == pgid)
169 1.1 cgd return (pgrp);
170 1.7 cgd return (NULL);
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * Move p to a new or existing process group (and session)
175 1.1 cgd */
176 1.11 cgd int
177 1.1 cgd enterpgrp(p, pgid, mksess)
178 1.1 cgd register struct proc *p;
179 1.1 cgd pid_t pgid;
180 1.4 andrew int mksess;
181 1.1 cgd {
182 1.1 cgd register struct pgrp *pgrp = pgfind(pgid);
183 1.1 cgd
184 1.1 cgd #ifdef DIAGNOSTIC
185 1.7 cgd if (pgrp != NULL && mksess) /* firewalls */
186 1.1 cgd panic("enterpgrp: setsid into non-empty pgrp");
187 1.1 cgd if (SESS_LEADER(p))
188 1.1 cgd panic("enterpgrp: session leader attempted setpgrp");
189 1.1 cgd #endif
190 1.1 cgd if (pgrp == NULL) {
191 1.7 cgd pid_t savepid = p->p_pid;
192 1.7 cgd struct proc *np;
193 1.1 cgd /*
194 1.1 cgd * new process group
195 1.1 cgd */
196 1.1 cgd #ifdef DIAGNOSTIC
197 1.1 cgd if (p->p_pid != pgid)
198 1.1 cgd panic("enterpgrp: new pgrp and pid != pgid");
199 1.1 cgd #endif
200 1.1 cgd MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
201 1.10 mycroft M_WAITOK);
202 1.7 cgd if ((np = pfind(savepid)) == NULL || np != p)
203 1.7 cgd return (ESRCH);
204 1.1 cgd if (mksess) {
205 1.1 cgd register struct session *sess;
206 1.1 cgd
207 1.1 cgd /*
208 1.1 cgd * new session
209 1.1 cgd */
210 1.1 cgd MALLOC(sess, struct session *, sizeof(struct session),
211 1.10 mycroft M_SESSION, M_WAITOK);
212 1.1 cgd sess->s_leader = p;
213 1.1 cgd sess->s_count = 1;
214 1.1 cgd sess->s_ttyvp = NULL;
215 1.1 cgd sess->s_ttyp = NULL;
216 1.1 cgd bcopy(p->p_session->s_login, sess->s_login,
217 1.1 cgd sizeof(sess->s_login));
218 1.6 cgd p->p_flag &= ~P_CONTROLT;
219 1.1 cgd pgrp->pg_session = sess;
220 1.1 cgd #ifdef DIAGNOSTIC
221 1.1 cgd if (p != curproc)
222 1.1 cgd panic("enterpgrp: mksession and p != curproc");
223 1.1 cgd #endif
224 1.1 cgd } else {
225 1.1 cgd pgrp->pg_session = p->p_session;
226 1.1 cgd pgrp->pg_session->s_count++;
227 1.1 cgd }
228 1.1 cgd pgrp->pg_id = pgid;
229 1.10 mycroft LIST_INIT(&pgrp->pg_members);
230 1.10 mycroft LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
231 1.1 cgd pgrp->pg_jobc = 0;
232 1.1 cgd } else if (pgrp == p->p_pgrp)
233 1.7 cgd return (0);
234 1.1 cgd
235 1.1 cgd /*
236 1.1 cgd * Adjust eligibility of affected pgrps to participate in job control.
237 1.1 cgd * Increment eligibility counts before decrementing, otherwise we
238 1.1 cgd * could reach 0 spuriously during the first call.
239 1.1 cgd */
240 1.1 cgd fixjobc(p, pgrp, 1);
241 1.1 cgd fixjobc(p, p->p_pgrp, 0);
242 1.1 cgd
243 1.10 mycroft LIST_REMOVE(p, p_pglist);
244 1.10 mycroft if (p->p_pgrp->pg_members.lh_first == 0)
245 1.1 cgd pgdelete(p->p_pgrp);
246 1.1 cgd p->p_pgrp = pgrp;
247 1.10 mycroft LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
248 1.7 cgd return (0);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd /*
252 1.1 cgd * remove process from process group
253 1.1 cgd */
254 1.11 cgd int
255 1.1 cgd leavepgrp(p)
256 1.1 cgd register struct proc *p;
257 1.1 cgd {
258 1.1 cgd
259 1.10 mycroft LIST_REMOVE(p, p_pglist);
260 1.10 mycroft if (p->p_pgrp->pg_members.lh_first == 0)
261 1.1 cgd pgdelete(p->p_pgrp);
262 1.1 cgd p->p_pgrp = 0;
263 1.7 cgd return (0);
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd /*
267 1.7 cgd * delete a process group
268 1.1 cgd */
269 1.11 cgd void
270 1.1 cgd pgdelete(pgrp)
271 1.1 cgd register struct pgrp *pgrp;
272 1.1 cgd {
273 1.1 cgd
274 1.1 cgd if (pgrp->pg_session->s_ttyp != NULL &&
275 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
276 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp = NULL;
277 1.10 mycroft LIST_REMOVE(pgrp, pg_hash);
278 1.1 cgd if (--pgrp->pg_session->s_count == 0)
279 1.1 cgd FREE(pgrp->pg_session, M_SESSION);
280 1.1 cgd FREE(pgrp, M_PGRP);
281 1.1 cgd }
282 1.1 cgd
283 1.4 andrew static void orphanpg();
284 1.1 cgd
285 1.1 cgd /*
286 1.1 cgd * Adjust pgrp jobc counters when specified process changes process group.
287 1.1 cgd * We count the number of processes in each process group that "qualify"
288 1.1 cgd * the group for terminal job control (those with a parent in a different
289 1.1 cgd * process group of the same session). If that count reaches zero, the
290 1.1 cgd * process group becomes orphaned. Check both the specified process'
291 1.1 cgd * process group and that of its children.
292 1.1 cgd * entering == 0 => p is leaving specified group.
293 1.1 cgd * entering == 1 => p is entering specified group.
294 1.1 cgd */
295 1.4 andrew void
296 1.1 cgd fixjobc(p, pgrp, entering)
297 1.1 cgd register struct proc *p;
298 1.1 cgd register struct pgrp *pgrp;
299 1.1 cgd int entering;
300 1.1 cgd {
301 1.1 cgd register struct pgrp *hispgrp;
302 1.1 cgd register struct session *mysession = pgrp->pg_session;
303 1.1 cgd
304 1.1 cgd /*
305 1.1 cgd * Check p's parent to see whether p qualifies its own process
306 1.1 cgd * group; if so, adjust count for p's process group.
307 1.1 cgd */
308 1.1 cgd if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
309 1.1 cgd hispgrp->pg_session == mysession)
310 1.1 cgd if (entering)
311 1.1 cgd pgrp->pg_jobc++;
312 1.1 cgd else if (--pgrp->pg_jobc == 0)
313 1.1 cgd orphanpg(pgrp);
314 1.1 cgd
315 1.1 cgd /*
316 1.1 cgd * Check this process' children to see whether they qualify
317 1.1 cgd * their process groups; if so, adjust counts for children's
318 1.1 cgd * process groups.
319 1.1 cgd */
320 1.10 mycroft for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
321 1.1 cgd if ((hispgrp = p->p_pgrp) != pgrp &&
322 1.1 cgd hispgrp->pg_session == mysession &&
323 1.1 cgd p->p_stat != SZOMB)
324 1.1 cgd if (entering)
325 1.1 cgd hispgrp->pg_jobc++;
326 1.1 cgd else if (--hispgrp->pg_jobc == 0)
327 1.1 cgd orphanpg(hispgrp);
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * A process group has become orphaned;
332 1.1 cgd * if there are any stopped processes in the group,
333 1.1 cgd * hang-up all process in that group.
334 1.1 cgd */
335 1.4 andrew static void
336 1.1 cgd orphanpg(pg)
337 1.1 cgd struct pgrp *pg;
338 1.1 cgd {
339 1.1 cgd register struct proc *p;
340 1.1 cgd
341 1.10 mycroft for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
342 1.1 cgd if (p->p_stat == SSTOP) {
343 1.10 mycroft for (p = pg->pg_members.lh_first; p != 0;
344 1.10 mycroft p = p->p_pglist.le_next) {
345 1.1 cgd psignal(p, SIGHUP);
346 1.1 cgd psignal(p, SIGCONT);
347 1.1 cgd }
348 1.1 cgd return;
349 1.1 cgd }
350 1.1 cgd }
351 1.1 cgd }
352 1.1 cgd
353 1.10 mycroft #ifdef DEBUG
354 1.1 cgd pgrpdump()
355 1.1 cgd {
356 1.1 cgd register struct pgrp *pgrp;
357 1.1 cgd register struct proc *p;
358 1.1 cgd register i;
359 1.1 cgd
360 1.10 mycroft for (i = 0; i <= pgrphash; i++) {
361 1.10 mycroft if (pgrp = pgrphashtbl[i].lh_first) {
362 1.10 mycroft printf("\tindx %d\n", i);
363 1.10 mycroft for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
364 1.12 mycroft printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
365 1.10 mycroft pgrp, pgrp->pg_id, pgrp->pg_session,
366 1.10 mycroft pgrp->pg_session->s_count,
367 1.10 mycroft pgrp->pg_members.lh_first);
368 1.10 mycroft for (p = pgrp->pg_members.lh_first; p != 0;
369 1.10 mycroft p = p->p_pglist.le_next) {
370 1.12 mycroft printf("\t\tpid %d addr %p pgrp %p\n",
371 1.10 mycroft p->p_pid, p, p->p_pgrp);
372 1.10 mycroft }
373 1.10 mycroft }
374 1.1 cgd }
375 1.1 cgd }
376 1.1 cgd }
377 1.10 mycroft #endif /* DEBUG */
378