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