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