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