kern_proc.c revision 1.53 1 1.53 thorpej /* $NetBSD: kern_proc.c,v 1.53 2002/09/25 22:21:43 thorpej 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.45 lukem
75 1.45 lukem #include <sys/cdefs.h>
76 1.53 thorpej __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.53 2002/09/25 22:21:43 thorpej Exp $");
77 1.48 yamt
78 1.48 yamt #include "opt_kstack.h"
79 1.1 cgd
80 1.5 mycroft #include <sys/param.h>
81 1.5 mycroft #include <sys/systm.h>
82 1.5 mycroft #include <sys/kernel.h>
83 1.5 mycroft #include <sys/proc.h>
84 1.28 thorpej #include <sys/resourcevar.h>
85 1.5 mycroft #include <sys/buf.h>
86 1.5 mycroft #include <sys/acct.h>
87 1.5 mycroft #include <sys/wait.h>
88 1.5 mycroft #include <sys/file.h>
89 1.8 mycroft #include <ufs/ufs/quota.h>
90 1.5 mycroft #include <sys/uio.h>
91 1.5 mycroft #include <sys/malloc.h>
92 1.24 thorpej #include <sys/pool.h>
93 1.5 mycroft #include <sys/mbuf.h>
94 1.5 mycroft #include <sys/ioctl.h>
95 1.5 mycroft #include <sys/tty.h>
96 1.11 cgd #include <sys/signalvar.h>
97 1.51 gmcgarry #include <sys/ras.h>
98 1.5 mycroft
99 1.7 cgd /*
100 1.7 cgd * Structure associated with user cacheing.
101 1.7 cgd */
102 1.7 cgd struct uidinfo {
103 1.10 mycroft LIST_ENTRY(uidinfo) ui_hash;
104 1.7 cgd uid_t ui_uid;
105 1.7 cgd long ui_proccnt;
106 1.10 mycroft };
107 1.10 mycroft #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
108 1.10 mycroft LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
109 1.10 mycroft u_long uihash; /* size of hash table - 1 */
110 1.7 cgd
111 1.7 cgd /*
112 1.10 mycroft * Other process lists
113 1.7 cgd */
114 1.10 mycroft struct pidhashhead *pidhashtbl;
115 1.10 mycroft u_long pidhash;
116 1.10 mycroft struct pgrphashhead *pgrphashtbl;
117 1.10 mycroft u_long pgrphash;
118 1.31 thorpej
119 1.10 mycroft struct proclist allproc;
120 1.32 thorpej struct proclist zombproc; /* resources have been freed */
121 1.32 thorpej
122 1.32 thorpej /*
123 1.33 thorpej * Process list locking:
124 1.33 thorpej *
125 1.33 thorpej * We have two types of locks on the proclists: read locks and write
126 1.33 thorpej * locks. Read locks can be used in interrupt context, so while we
127 1.38 thorpej * hold the write lock, we must also block clock interrupts to
128 1.37 thorpej * lock out any scheduling changes that may happen in interrupt
129 1.37 thorpej * context.
130 1.33 thorpej *
131 1.33 thorpej * The proclist lock locks the following structures:
132 1.33 thorpej *
133 1.33 thorpej * allproc
134 1.33 thorpej * zombproc
135 1.33 thorpej * pidhashtbl
136 1.33 thorpej */
137 1.33 thorpej struct lock proclist_lock;
138 1.33 thorpej
139 1.33 thorpej /*
140 1.32 thorpej * Locking of this proclist is special; it's accessed in a
141 1.32 thorpej * critical section of process exit, and thus locking it can't
142 1.32 thorpej * modify interrupt state. We use a simple spin lock for this
143 1.32 thorpej * proclist. Processes on this proclist are also on zombproc;
144 1.32 thorpej * we use the p_hash member to linkup to deadproc.
145 1.32 thorpej */
146 1.32 thorpej struct simplelock deadproc_slock;
147 1.31 thorpej struct proclist deadproc; /* dead, but not yet undead */
148 1.31 thorpej
149 1.24 thorpej struct pool proc_pool;
150 1.28 thorpej struct pool pcred_pool;
151 1.28 thorpej struct pool plimit_pool;
152 1.29 thorpej struct pool pgrp_pool;
153 1.30 thorpej struct pool rusage_pool;
154 1.51 gmcgarry struct pool ras_pool;
155 1.10 mycroft
156 1.31 thorpej /*
157 1.31 thorpej * The process list descriptors, used during pid allocation and
158 1.31 thorpej * by sysctl. No locking on this data structure is needed since
159 1.31 thorpej * it is completely static.
160 1.31 thorpej */
161 1.31 thorpej const struct proclist_desc proclists[] = {
162 1.31 thorpej { &allproc },
163 1.31 thorpej { &zombproc },
164 1.31 thorpej { NULL },
165 1.31 thorpej };
166 1.31 thorpej
167 1.13 christos static void orphanpg __P((struct pgrp *));
168 1.14 christos #ifdef DEBUG
169 1.14 christos void pgrpdump __P((void));
170 1.14 christos #endif
171 1.13 christos
172 1.10 mycroft /*
173 1.10 mycroft * Initialize global process hashing structures.
174 1.10 mycroft */
175 1.11 cgd void
176 1.10 mycroft procinit()
177 1.7 cgd {
178 1.31 thorpej const struct proclist_desc *pd;
179 1.31 thorpej
180 1.31 thorpej for (pd = proclists; pd->pd_list != NULL; pd++)
181 1.31 thorpej LIST_INIT(pd->pd_list);
182 1.7 cgd
183 1.34 thorpej spinlockinit(&proclist_lock, "proclk", 0);
184 1.33 thorpej
185 1.32 thorpej LIST_INIT(&deadproc);
186 1.32 thorpej simple_lock_init(&deadproc_slock);
187 1.32 thorpej
188 1.43 ad pidhashtbl =
189 1.43 ad hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
190 1.43 ad pgrphashtbl =
191 1.43 ad hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
192 1.43 ad uihashtbl =
193 1.43 ad hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
194 1.31 thorpej
195 1.24 thorpej pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
196 1.46 thorpej &pool_allocator_nointr);
197 1.29 thorpej pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
198 1.46 thorpej &pool_allocator_nointr);
199 1.28 thorpej pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
200 1.46 thorpej &pool_allocator_nointr);
201 1.28 thorpej pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
202 1.46 thorpej &pool_allocator_nointr);
203 1.30 thorpej pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
204 1.51 gmcgarry &pool_allocator_nointr);
205 1.51 gmcgarry pool_init(&ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
206 1.46 thorpej &pool_allocator_nointr);
207 1.7 cgd }
208 1.1 cgd
209 1.7 cgd /*
210 1.33 thorpej * Acquire a read lock on the proclist.
211 1.33 thorpej */
212 1.33 thorpej void
213 1.34 thorpej proclist_lock_read()
214 1.33 thorpej {
215 1.42 thorpej int error;
216 1.33 thorpej
217 1.34 thorpej error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
218 1.33 thorpej #ifdef DIAGNOSTIC
219 1.40 thorpej if (__predict_false(error != 0))
220 1.34 thorpej panic("proclist_lock_read: failed to acquire lock");
221 1.33 thorpej #endif
222 1.33 thorpej }
223 1.33 thorpej
224 1.33 thorpej /*
225 1.33 thorpej * Release a read lock on the proclist.
226 1.33 thorpej */
227 1.33 thorpej void
228 1.33 thorpej proclist_unlock_read()
229 1.33 thorpej {
230 1.33 thorpej
231 1.34 thorpej (void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
232 1.33 thorpej }
233 1.33 thorpej
234 1.33 thorpej /*
235 1.33 thorpej * Acquire a write lock on the proclist.
236 1.33 thorpej */
237 1.33 thorpej int
238 1.33 thorpej proclist_lock_write()
239 1.33 thorpej {
240 1.42 thorpej int s, error;
241 1.33 thorpej
242 1.38 thorpej s = splclock();
243 1.34 thorpej error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
244 1.33 thorpej #ifdef DIAGNOSTIC
245 1.40 thorpej if (__predict_false(error != 0))
246 1.33 thorpej panic("proclist_lock: failed to acquire lock");
247 1.33 thorpej #endif
248 1.33 thorpej return (s);
249 1.33 thorpej }
250 1.33 thorpej
251 1.33 thorpej /*
252 1.33 thorpej * Release a write lock on the proclist.
253 1.33 thorpej */
254 1.33 thorpej void
255 1.33 thorpej proclist_unlock_write(s)
256 1.33 thorpej int s;
257 1.33 thorpej {
258 1.33 thorpej
259 1.34 thorpej (void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
260 1.33 thorpej splx(s);
261 1.33 thorpej }
262 1.33 thorpej
263 1.33 thorpej /*
264 1.7 cgd * Change the count associated with number of processes
265 1.7 cgd * a given user is using.
266 1.7 cgd */
267 1.7 cgd int
268 1.7 cgd chgproccnt(uid, diff)
269 1.7 cgd uid_t uid;
270 1.7 cgd int diff;
271 1.7 cgd {
272 1.39 augustss struct uidinfo *uip;
273 1.39 augustss struct uihashhead *uipp;
274 1.7 cgd
275 1.10 mycroft uipp = UIHASH(uid);
276 1.52 matt
277 1.52 matt LIST_FOREACH(uip, uipp, ui_hash)
278 1.7 cgd if (uip->ui_uid == uid)
279 1.7 cgd break;
280 1.52 matt
281 1.7 cgd if (uip) {
282 1.7 cgd uip->ui_proccnt += diff;
283 1.7 cgd if (uip->ui_proccnt > 0)
284 1.7 cgd return (uip->ui_proccnt);
285 1.7 cgd if (uip->ui_proccnt < 0)
286 1.7 cgd panic("chgproccnt: procs < 0");
287 1.10 mycroft LIST_REMOVE(uip, ui_hash);
288 1.7 cgd FREE(uip, M_PROC);
289 1.7 cgd return (0);
290 1.7 cgd }
291 1.7 cgd if (diff <= 0) {
292 1.7 cgd if (diff == 0)
293 1.7 cgd return(0);
294 1.7 cgd panic("chgproccnt: lost user");
295 1.7 cgd }
296 1.7 cgd MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
297 1.10 mycroft LIST_INSERT_HEAD(uipp, uip, ui_hash);
298 1.7 cgd uip->ui_uid = uid;
299 1.7 cgd uip->ui_proccnt = diff;
300 1.7 cgd return (diff);
301 1.7 cgd }
302 1.4 andrew
303 1.1 cgd /*
304 1.41 sommerfe * Is p an inferior of q?
305 1.1 cgd */
306 1.11 cgd int
307 1.41 sommerfe inferior(p, q)
308 1.39 augustss struct proc *p;
309 1.41 sommerfe struct proc *q;
310 1.1 cgd {
311 1.1 cgd
312 1.41 sommerfe for (; p != q; p = p->p_pptr)
313 1.1 cgd if (p->p_pid == 0)
314 1.1 cgd return (0);
315 1.1 cgd return (1);
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd /*
319 1.1 cgd * Locate a process by number
320 1.1 cgd */
321 1.1 cgd struct proc *
322 1.1 cgd pfind(pid)
323 1.39 augustss pid_t pid;
324 1.1 cgd {
325 1.33 thorpej struct proc *p;
326 1.1 cgd
327 1.34 thorpej proclist_lock_read();
328 1.52 matt LIST_FOREACH(p, PIDHASH(pid), p_hash)
329 1.1 cgd if (p->p_pid == pid)
330 1.33 thorpej goto out;
331 1.33 thorpej out:
332 1.33 thorpej proclist_unlock_read();
333 1.33 thorpej return (p);
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd /*
337 1.1 cgd * Locate a process group by number
338 1.1 cgd */
339 1.1 cgd struct pgrp *
340 1.1 cgd pgfind(pgid)
341 1.39 augustss pid_t pgid;
342 1.1 cgd {
343 1.39 augustss struct pgrp *pgrp;
344 1.1 cgd
345 1.52 matt LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
346 1.1 cgd if (pgrp->pg_id == pgid)
347 1.1 cgd return (pgrp);
348 1.7 cgd return (NULL);
349 1.1 cgd }
350 1.1 cgd
351 1.1 cgd /*
352 1.1 cgd * Move p to a new or existing process group (and session)
353 1.1 cgd */
354 1.11 cgd int
355 1.1 cgd enterpgrp(p, pgid, mksess)
356 1.39 augustss struct proc *p;
357 1.1 cgd pid_t pgid;
358 1.4 andrew int mksess;
359 1.1 cgd {
360 1.39 augustss struct pgrp *pgrp = pgfind(pgid);
361 1.1 cgd
362 1.1 cgd #ifdef DIAGNOSTIC
363 1.40 thorpej if (__predict_false(pgrp != NULL && mksess)) /* firewalls */
364 1.1 cgd panic("enterpgrp: setsid into non-empty pgrp");
365 1.40 thorpej if (__predict_false(SESS_LEADER(p)))
366 1.1 cgd panic("enterpgrp: session leader attempted setpgrp");
367 1.1 cgd #endif
368 1.1 cgd if (pgrp == NULL) {
369 1.7 cgd pid_t savepid = p->p_pid;
370 1.7 cgd struct proc *np;
371 1.1 cgd /*
372 1.1 cgd * new process group
373 1.1 cgd */
374 1.1 cgd #ifdef DIAGNOSTIC
375 1.40 thorpej if (__predict_false(p->p_pid != pgid))
376 1.1 cgd panic("enterpgrp: new pgrp and pid != pgid");
377 1.1 cgd #endif
378 1.29 thorpej pgrp = pool_get(&pgrp_pool, PR_WAITOK);
379 1.49 enami if ((np = pfind(savepid)) == NULL || np != p) {
380 1.49 enami pool_put(&pgrp_pool, pgrp);
381 1.7 cgd return (ESRCH);
382 1.49 enami }
383 1.1 cgd if (mksess) {
384 1.39 augustss struct session *sess;
385 1.1 cgd
386 1.1 cgd /*
387 1.1 cgd * new session
388 1.1 cgd */
389 1.1 cgd MALLOC(sess, struct session *, sizeof(struct session),
390 1.10 mycroft M_SESSION, M_WAITOK);
391 1.49 enami if ((np = pfind(savepid)) == NULL || np != p) {
392 1.49 enami FREE(sess, M_SESSION);
393 1.49 enami pool_put(&pgrp_pool, pgrp);
394 1.49 enami return (ESRCH);
395 1.49 enami }
396 1.21 thorpej sess->s_sid = p->p_pid;
397 1.1 cgd sess->s_leader = p;
398 1.1 cgd sess->s_count = 1;
399 1.1 cgd sess->s_ttyvp = NULL;
400 1.1 cgd sess->s_ttyp = NULL;
401 1.25 perry memcpy(sess->s_login, p->p_session->s_login,
402 1.1 cgd sizeof(sess->s_login));
403 1.6 cgd p->p_flag &= ~P_CONTROLT;
404 1.1 cgd pgrp->pg_session = sess;
405 1.1 cgd #ifdef DIAGNOSTIC
406 1.40 thorpej if (__predict_false(p != curproc))
407 1.1 cgd panic("enterpgrp: mksession and p != curproc");
408 1.1 cgd #endif
409 1.1 cgd } else {
410 1.47 christos SESSHOLD(p->p_session);
411 1.1 cgd pgrp->pg_session = p->p_session;
412 1.1 cgd }
413 1.1 cgd pgrp->pg_id = pgid;
414 1.10 mycroft LIST_INIT(&pgrp->pg_members);
415 1.10 mycroft LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
416 1.1 cgd pgrp->pg_jobc = 0;
417 1.1 cgd } else if (pgrp == p->p_pgrp)
418 1.7 cgd return (0);
419 1.1 cgd
420 1.1 cgd /*
421 1.1 cgd * Adjust eligibility of affected pgrps to participate in job control.
422 1.1 cgd * Increment eligibility counts before decrementing, otherwise we
423 1.1 cgd * could reach 0 spuriously during the first call.
424 1.1 cgd */
425 1.1 cgd fixjobc(p, pgrp, 1);
426 1.1 cgd fixjobc(p, p->p_pgrp, 0);
427 1.1 cgd
428 1.10 mycroft LIST_REMOVE(p, p_pglist);
429 1.52 matt if (LIST_EMPTY(&p->p_pgrp->pg_members))
430 1.1 cgd pgdelete(p->p_pgrp);
431 1.1 cgd p->p_pgrp = pgrp;
432 1.10 mycroft LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
433 1.7 cgd return (0);
434 1.1 cgd }
435 1.1 cgd
436 1.1 cgd /*
437 1.1 cgd * remove process from process group
438 1.1 cgd */
439 1.11 cgd int
440 1.1 cgd leavepgrp(p)
441 1.39 augustss struct proc *p;
442 1.1 cgd {
443 1.1 cgd
444 1.10 mycroft LIST_REMOVE(p, p_pglist);
445 1.52 matt if (LIST_EMPTY(&p->p_pgrp->pg_members))
446 1.1 cgd pgdelete(p->p_pgrp);
447 1.1 cgd p->p_pgrp = 0;
448 1.7 cgd return (0);
449 1.1 cgd }
450 1.1 cgd
451 1.1 cgd /*
452 1.7 cgd * delete a process group
453 1.1 cgd */
454 1.11 cgd void
455 1.1 cgd pgdelete(pgrp)
456 1.39 augustss struct pgrp *pgrp;
457 1.1 cgd {
458 1.1 cgd
459 1.44 pk /* Remove reference (if any) from tty to this process group */
460 1.1 cgd if (pgrp->pg_session->s_ttyp != NULL &&
461 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
462 1.1 cgd pgrp->pg_session->s_ttyp->t_pgrp = NULL;
463 1.10 mycroft LIST_REMOVE(pgrp, pg_hash);
464 1.47 christos SESSRELE(pgrp->pg_session);
465 1.29 thorpej pool_put(&pgrp_pool, pgrp);
466 1.1 cgd }
467 1.1 cgd
468 1.1 cgd /*
469 1.1 cgd * Adjust pgrp jobc counters when specified process changes process group.
470 1.1 cgd * We count the number of processes in each process group that "qualify"
471 1.1 cgd * the group for terminal job control (those with a parent in a different
472 1.1 cgd * process group of the same session). If that count reaches zero, the
473 1.1 cgd * process group becomes orphaned. Check both the specified process'
474 1.1 cgd * process group and that of its children.
475 1.1 cgd * entering == 0 => p is leaving specified group.
476 1.1 cgd * entering == 1 => p is entering specified group.
477 1.1 cgd */
478 1.4 andrew void
479 1.1 cgd fixjobc(p, pgrp, entering)
480 1.39 augustss struct proc *p;
481 1.39 augustss struct pgrp *pgrp;
482 1.1 cgd int entering;
483 1.1 cgd {
484 1.39 augustss struct pgrp *hispgrp;
485 1.39 augustss struct session *mysession = pgrp->pg_session;
486 1.1 cgd
487 1.1 cgd /*
488 1.1 cgd * Check p's parent to see whether p qualifies its own process
489 1.1 cgd * group; if so, adjust count for p's process group.
490 1.1 cgd */
491 1.1 cgd if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
492 1.26 thorpej hispgrp->pg_session == mysession) {
493 1.1 cgd if (entering)
494 1.1 cgd pgrp->pg_jobc++;
495 1.1 cgd else if (--pgrp->pg_jobc == 0)
496 1.1 cgd orphanpg(pgrp);
497 1.26 thorpej }
498 1.1 cgd
499 1.1 cgd /*
500 1.1 cgd * Check this process' children to see whether they qualify
501 1.1 cgd * their process groups; if so, adjust counts for children's
502 1.1 cgd * process groups.
503 1.1 cgd */
504 1.52 matt LIST_FOREACH(p, &p->p_children, p_sibling) {
505 1.1 cgd if ((hispgrp = p->p_pgrp) != pgrp &&
506 1.1 cgd hispgrp->pg_session == mysession &&
507 1.32 thorpej P_ZOMBIE(p) == 0) {
508 1.1 cgd if (entering)
509 1.1 cgd hispgrp->pg_jobc++;
510 1.1 cgd else if (--hispgrp->pg_jobc == 0)
511 1.1 cgd orphanpg(hispgrp);
512 1.26 thorpej }
513 1.26 thorpej }
514 1.1 cgd }
515 1.1 cgd
516 1.1 cgd /*
517 1.1 cgd * A process group has become orphaned;
518 1.1 cgd * if there are any stopped processes in the group,
519 1.1 cgd * hang-up all process in that group.
520 1.1 cgd */
521 1.4 andrew static void
522 1.1 cgd orphanpg(pg)
523 1.1 cgd struct pgrp *pg;
524 1.1 cgd {
525 1.39 augustss struct proc *p;
526 1.1 cgd
527 1.52 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
528 1.1 cgd if (p->p_stat == SSTOP) {
529 1.52 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
530 1.1 cgd psignal(p, SIGHUP);
531 1.1 cgd psignal(p, SIGCONT);
532 1.1 cgd }
533 1.1 cgd return;
534 1.1 cgd }
535 1.1 cgd }
536 1.1 cgd }
537 1.35 bouyer
538 1.35 bouyer /* mark process as suid/sgid, reset some values do defaults */
539 1.35 bouyer void
540 1.35 bouyer p_sugid(p)
541 1.35 bouyer struct proc *p;
542 1.35 bouyer {
543 1.35 bouyer struct plimit *newlim;
544 1.35 bouyer
545 1.35 bouyer p->p_flag |= P_SUGID;
546 1.35 bouyer /* reset what needs to be reset in plimit */
547 1.35 bouyer if (p->p_limit->pl_corename != defcorename) {
548 1.35 bouyer if (p->p_limit->p_refcnt > 1 &&
549 1.35 bouyer (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
550 1.35 bouyer newlim = limcopy(p->p_limit);
551 1.35 bouyer limfree(p->p_limit);
552 1.35 bouyer p->p_limit = newlim;
553 1.35 bouyer }
554 1.49 enami free(p->p_limit->pl_corename, M_TEMP);
555 1.35 bouyer p->p_limit->pl_corename = defcorename;
556 1.35 bouyer }
557 1.35 bouyer }
558 1.1 cgd
559 1.10 mycroft #ifdef DEBUG
560 1.14 christos void
561 1.1 cgd pgrpdump()
562 1.1 cgd {
563 1.39 augustss struct pgrp *pgrp;
564 1.39 augustss struct proc *p;
565 1.39 augustss int i;
566 1.1 cgd
567 1.10 mycroft for (i = 0; i <= pgrphash; i++) {
568 1.52 matt if ((pgrp = LIST_FIRST(&pgrphashtbl[i])) != NULL) {
569 1.16 christos printf("\tindx %d\n", i);
570 1.10 mycroft for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
571 1.50 enami printf("\tpgrp %p, pgid %d, sess %p, "
572 1.50 enami "sesscnt %d, mem %p\n",
573 1.10 mycroft pgrp, pgrp->pg_id, pgrp->pg_session,
574 1.10 mycroft pgrp->pg_session->s_count,
575 1.52 matt LIST_FIRST(&pgrp->pg_members));
576 1.52 matt LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
577 1.16 christos printf("\t\tpid %d addr %p pgrp %p\n",
578 1.10 mycroft p->p_pid, p, p->p_pgrp);
579 1.10 mycroft }
580 1.10 mycroft }
581 1.1 cgd }
582 1.1 cgd }
583 1.1 cgd }
584 1.10 mycroft #endif /* DEBUG */
585 1.48 yamt
586 1.48 yamt #ifdef KSTACK_CHECK_MAGIC
587 1.48 yamt #include <sys/user.h>
588 1.48 yamt
589 1.48 yamt #define KSTACK_MAGIC 0xdeadbeaf
590 1.48 yamt
591 1.48 yamt /* XXX should be per process basis? */
592 1.48 yamt int kstackleftmin = KSTACK_SIZE;
593 1.50 enami int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
594 1.50 enami less than this */
595 1.48 yamt
596 1.48 yamt void
597 1.48 yamt kstack_setup_magic(const struct proc *p)
598 1.48 yamt {
599 1.48 yamt u_int32_t *ip;
600 1.48 yamt u_int32_t const *end;
601 1.48 yamt
602 1.48 yamt KASSERT(p != 0);
603 1.48 yamt KASSERT(p != &proc0);
604 1.48 yamt
605 1.48 yamt /*
606 1.48 yamt * fill all the stack with magic number
607 1.48 yamt * so that later modification on it can be detected.
608 1.48 yamt */
609 1.48 yamt ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
610 1.48 yamt end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
611 1.48 yamt for (; ip < end; ip++) {
612 1.48 yamt *ip = KSTACK_MAGIC;
613 1.48 yamt }
614 1.48 yamt }
615 1.48 yamt
616 1.48 yamt void
617 1.48 yamt kstack_check_magic(const struct proc *p)
618 1.48 yamt {
619 1.48 yamt u_int32_t const *ip, *end;
620 1.48 yamt int stackleft;
621 1.48 yamt
622 1.48 yamt KASSERT(p != 0);
623 1.48 yamt
624 1.48 yamt /* don't check proc0 */ /*XXX*/
625 1.48 yamt if (p == &proc0)
626 1.48 yamt return;
627 1.48 yamt
628 1.48 yamt #ifdef __MACHINE_STACK_GROWS_UP
629 1.48 yamt /* stack grows upwards (eg. hppa) */
630 1.48 yamt ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
631 1.48 yamt end = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
632 1.48 yamt for (ip--; ip >= end; ip--)
633 1.48 yamt if (*ip != KSTACK_MAGIC)
634 1.48 yamt break;
635 1.48 yamt
636 1.48 yamt stackleft = (caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE - (caddr_t)ip;
637 1.48 yamt #else /* __MACHINE_STACK_GROWS_UP */
638 1.48 yamt /* stack grows downwards (eg. i386) */
639 1.48 yamt ip = (u_int32_t *)KSTACK_LOWEST_ADDR(p);
640 1.48 yamt end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(p) + KSTACK_SIZE);
641 1.48 yamt for (; ip < end; ip++)
642 1.48 yamt if (*ip != KSTACK_MAGIC)
643 1.48 yamt break;
644 1.48 yamt
645 1.48 yamt stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(p);
646 1.48 yamt #endif /* __MACHINE_STACK_GROWS_UP */
647 1.48 yamt
648 1.48 yamt if (kstackleftmin > stackleft) {
649 1.48 yamt kstackleftmin = stackleft;
650 1.48 yamt if (stackleft < kstackleftthres)
651 1.48 yamt printf("warning: kernel stack left %d bytes(pid %u)\n",
652 1.50 enami stackleft, p->p_pid);
653 1.48 yamt }
654 1.48 yamt
655 1.48 yamt if (stackleft <= 0) {
656 1.48 yamt panic("magic on the top of kernel stack changed for pid %u: "
657 1.48 yamt "maybe kernel stack overflow\n", p->p_pid);
658 1.48 yamt }
659 1.48 yamt }
660 1.50 enami #endif /* KSTACK_CHECK_MAGIC */
661