xref: /netbsd-src/sys/kern/kern_proc.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: kern_proc.c,v 1.78 2004/05/06 22:20:30 pk Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.78 2004/05/06 22:20:30 pk Exp $");
73 
74 #include "opt_kstack.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/proc.h>
80 #include <sys/resourcevar.h>
81 #include <sys/buf.h>
82 #include <sys/acct.h>
83 #include <sys/wait.h>
84 #include <sys/file.h>
85 #include <ufs/ufs/quota.h>
86 #include <sys/uio.h>
87 #include <sys/malloc.h>
88 #include <sys/pool.h>
89 #include <sys/mbuf.h>
90 #include <sys/ioctl.h>
91 #include <sys/tty.h>
92 #include <sys/signalvar.h>
93 #include <sys/ras.h>
94 #include <sys/sa.h>
95 #include <sys/savar.h>
96 
97 /*
98  * Other process lists
99  */
100 
101 struct proclist allproc;
102 struct proclist zombproc;	/* resources have been freed */
103 
104 
105 /*
106  * Process list locking:
107  *
108  * We have two types of locks on the proclists: read locks and write
109  * locks.  Read locks can be used in interrupt context, so while we
110  * hold the write lock, we must also block clock interrupts to
111  * lock out any scheduling changes that may happen in interrupt
112  * context.
113  *
114  * The proclist lock locks the following structures:
115  *
116  *	allproc
117  *	zombproc
118  *	pid_table
119  */
120 struct lock proclist_lock;
121 
122 /*
123  * pid to proc lookup is done by indexing the pid_table array.
124  * Since pid numbers are only allocated when an empty slot
125  * has been found, there is no need to search any lists ever.
126  * (an orphaned pgrp will lock the slot, a session will lock
127  * the pgrp with the same number.)
128  * If the table is too small it is reallocated with twice the
129  * previous size and the entries 'unzipped' into the two halves.
130  * A linked list of free entries is passed through the pt_proc
131  * field of 'free' items - set odd to be an invalid ptr.
132  */
133 
134 struct pid_table {
135 	struct proc	*pt_proc;
136 	struct pgrp	*pt_pgrp;
137 };
138 #if 1	/* strongly typed cast - should be a noop */
139 static __inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
140 #else
141 #define p2u(p) ((uint)p)
142 #endif
143 #define P_VALID(p) (!(p2u(p) & 1))
144 #define P_NEXT(p) (p2u(p) >> 1)
145 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
146 
147 #define INITIAL_PID_TABLE_SIZE	(1 << 5)
148 static struct pid_table *pid_table;
149 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
150 static uint pid_alloc_lim;	/* max we allocate before growing table */
151 static uint pid_alloc_cnt;	/* number of allocated pids */
152 
153 /* links through free slots - never empty! */
154 static uint next_free_pt, last_free_pt;
155 static pid_t pid_max = PID_MAX;		/* largest value we allocate */
156 
157 POOL_INIT(proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
158     &pool_allocator_nointr);
159 POOL_INIT(lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
160     &pool_allocator_nointr);
161 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
162     &pool_allocator_nointr);
163 POOL_INIT(pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
164     &pool_allocator_nointr);
165 POOL_INIT(pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
166     &pool_allocator_nointr);
167 POOL_INIT(plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
168     &pool_allocator_nointr);
169 POOL_INIT(pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
170     &pool_allocator_nointr);
171 POOL_INIT(rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
172     &pool_allocator_nointr);
173 POOL_INIT(ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
174     &pool_allocator_nointr);
175 POOL_INIT(sadata_pool, sizeof(struct sadata), 0, 0, 0, "sadatapl",
176     &pool_allocator_nointr);
177 POOL_INIT(saupcall_pool, sizeof(struct sadata_upcall), 0, 0, 0, "saupcpl",
178     &pool_allocator_nointr);
179 POOL_INIT(sastack_pool, sizeof(struct sastack), 0, 0, 0, "sastackpl",
180     &pool_allocator_nointr);
181 POOL_INIT(savp_pool, sizeof(struct sadata_vp), 0, 0, 0, "savppl",
182     &pool_allocator_nointr);
183 POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
184     &pool_allocator_nointr);
185 POOL_INIT(session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
186     &pool_allocator_nointr);
187 
188 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
189 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
190 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
191 
192 /*
193  * The process list descriptors, used during pid allocation and
194  * by sysctl.  No locking on this data structure is needed since
195  * it is completely static.
196  */
197 const struct proclist_desc proclists[] = {
198 	{ &allproc	},
199 	{ &zombproc	},
200 	{ NULL		},
201 };
202 
203 static void orphanpg(struct pgrp *);
204 static void pg_delete(pid_t);
205 
206 /*
207  * Initialize global process hashing structures.
208  */
209 void
210 procinit(void)
211 {
212 	const struct proclist_desc *pd;
213 	int i;
214 #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
215 
216 	for (pd = proclists; pd->pd_list != NULL; pd++)
217 		LIST_INIT(pd->pd_list);
218 
219 	spinlockinit(&proclist_lock, "proclk", 0);
220 
221 	pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table,
222 			    M_PROC, M_WAITOK);
223 	/* Set free list running through table...
224 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
225 	for (i = 0; i <= pid_tbl_mask; i++) {
226 		pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
227 		pid_table[i].pt_pgrp = 0;
228 	}
229 	/* slot 0 is just grabbed */
230 	next_free_pt = 1;
231 	/* Need to fix last entry. */
232 	last_free_pt = pid_tbl_mask;
233 	pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
234 	/* point at which we grow table - to avoid reusing pids too often */
235 	pid_alloc_lim = pid_tbl_mask - 1;
236 #undef LINK_EMPTY
237 
238 	LIST_INIT(&alllwp);
239 
240 	uihashtbl =
241 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
242 }
243 
244 /*
245  * Acquire a read lock on the proclist.
246  */
247 void
248 proclist_lock_read(void)
249 {
250 	int error;
251 
252 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
253 #ifdef DIAGNOSTIC
254 	if (__predict_false(error != 0))
255 		panic("proclist_lock_read: failed to acquire lock");
256 #endif
257 }
258 
259 /*
260  * Release a read lock on the proclist.
261  */
262 void
263 proclist_unlock_read(void)
264 {
265 
266 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
267 }
268 
269 /*
270  * Acquire a write lock on the proclist.
271  */
272 int
273 proclist_lock_write(void)
274 {
275 	int s, error;
276 
277 	s = splclock();
278 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
279 #ifdef DIAGNOSTIC
280 	if (__predict_false(error != 0))
281 		panic("proclist_lock: failed to acquire lock");
282 #endif
283 	return (s);
284 }
285 
286 /*
287  * Release a write lock on the proclist.
288  */
289 void
290 proclist_unlock_write(int s)
291 {
292 
293 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
294 	splx(s);
295 }
296 
297 /*
298  * Check that the specified process group is in the session of the
299  * specified process.
300  * Treats -ve ids as process ids.
301  * Used to validate TIOCSPGRP requests.
302  */
303 int
304 pgid_in_session(struct proc *p, pid_t pg_id)
305 {
306 	struct pgrp *pgrp;
307 
308 	if (pg_id < 0) {
309 		struct proc *p1 = pfind(-pg_id);
310 		if (p1 == NULL)
311 			return EINVAL;
312 		pgrp = p1->p_pgrp;
313 	} else {
314 		pgrp = pgfind(pg_id);
315 		if (pgrp == NULL)
316 			return EINVAL;
317 	}
318 	if (pgrp->pg_session != p->p_pgrp->pg_session)
319 		return EPERM;
320 	return 0;
321 }
322 
323 /*
324  * Is p an inferior of q?
325  */
326 int
327 inferior(struct proc *p, struct proc *q)
328 {
329 
330 	for (; p != q; p = p->p_pptr)
331 		if (p->p_pid == 0)
332 			return (0);
333 	return (1);
334 }
335 
336 /*
337  * Locate a process by number
338  */
339 struct proc *
340 p_find(pid_t pid, uint flags)
341 {
342 	struct proc *p;
343 	char stat;
344 
345 	if (!(flags & PFIND_LOCKED))
346 		proclist_lock_read();
347 	p = pid_table[pid & pid_tbl_mask].pt_proc;
348 	/* Only allow live processes to be found by pid. */
349 	if (P_VALID(p) && p->p_pid == pid &&
350 	    ((stat = p->p_stat) == SACTIVE || stat == SSTOP
351 		    || (stat == SZOMB && (flags & PFIND_ZOMBIE)))) {
352 		if (flags & PFIND_UNLOCK_OK)
353 			 proclist_unlock_read();
354 		return p;
355 	}
356 	if (flags & PFIND_UNLOCK_FAIL)
357 		 proclist_unlock_read();
358 	return NULL;
359 }
360 
361 
362 /*
363  * Locate a process group by number
364  */
365 struct pgrp *
366 pg_find(pid_t pgid, uint flags)
367 {
368 	struct pgrp *pg;
369 
370 	if (!(flags & PFIND_LOCKED))
371 		proclist_lock_read();
372 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
373 	/*
374 	 * Can't look up a pgrp that only exists because the session
375 	 * hasn't died yet (traditional)
376 	 */
377 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
378 		if (flags & PFIND_UNLOCK_FAIL)
379 			 proclist_unlock_read();
380 		return NULL;
381 	}
382 
383 	if (flags & PFIND_UNLOCK_OK)
384 		proclist_unlock_read();
385 	return pg;
386 }
387 
388 /*
389  * Set entry for process 0
390  */
391 void
392 proc0_insert(struct proc *p, struct lwp *l, struct pgrp *pgrp,
393 	struct session *sess)
394 {
395 	int s;
396 
397 	simple_lock_init(&p->p_lock);
398 	LIST_INIT(&p->p_lwps);
399 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
400 	p->p_nlwps = 1;
401 	simple_lock_init(&p->p_sigctx.ps_silock);
402 	CIRCLEQ_INIT(&p->p_sigctx.ps_siginfo);
403 
404 	s = proclist_lock_write();
405 
406 	pid_table[0].pt_proc = p;
407 	LIST_INSERT_HEAD(&allproc, p, p_list);
408 	LIST_INSERT_HEAD(&alllwp, l, l_list);
409 
410 	p->p_pgrp = pgrp;
411 	pid_table[0].pt_pgrp = pgrp;
412 	LIST_INIT(&pgrp->pg_members);
413 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
414 
415 	pgrp->pg_session = sess;
416 	sess->s_count = 1;
417 	sess->s_sid = 0;
418 	sess->s_leader = p;
419 
420 	proclist_unlock_write(s);
421 }
422 
423 static void
424 expand_pid_table(void)
425 {
426 	uint pt_size = pid_tbl_mask + 1;
427 	struct pid_table *n_pt, *new_pt;
428 	struct proc *proc;
429 	struct pgrp *pgrp;
430 	int i;
431 	int s;
432 	pid_t pid;
433 
434 	new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK);
435 
436 	s = proclist_lock_write();
437 	if (pt_size != pid_tbl_mask + 1) {
438 		/* Another process beat us to it... */
439 		proclist_unlock_write(s);
440 		FREE(new_pt, M_PROC);
441 		return;
442 	}
443 
444 	/*
445 	 * Copy entries from old table into new one.
446 	 * If 'pid' is 'odd' we need to place in the upper half,
447 	 * even pid's to the lower half.
448 	 * Free items stay in the low half so we don't have to
449 	 * fixup the reference to them.
450 	 * We stuff free items on the front of the freelist
451 	 * because we can't write to unmodified entries.
452 	 * Processing the table backwards maintains a semblance
453 	 * of issueing pid numbers that increase with time.
454 	 */
455 	i = pt_size - 1;
456 	n_pt = new_pt + i;
457 	for (; ; i--, n_pt--) {
458 		proc = pid_table[i].pt_proc;
459 		pgrp = pid_table[i].pt_pgrp;
460 		if (!P_VALID(proc)) {
461 			/* Up 'use count' so that link is valid */
462 			pid = (P_NEXT(proc) + pt_size) & ~pt_size;
463 			proc = P_FREE(pid);
464 			if (pgrp)
465 				pid = pgrp->pg_id;
466 		} else
467 			pid = proc->p_pid;
468 
469 		/* Save entry in appropriate half of table */
470 		n_pt[pid & pt_size].pt_proc = proc;
471 		n_pt[pid & pt_size].pt_pgrp = pgrp;
472 
473 		/* Put other piece on start of free list */
474 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
475 		n_pt[pid & pt_size].pt_proc =
476 				    P_FREE((pid & ~pt_size) | next_free_pt);
477 		n_pt[pid & pt_size].pt_pgrp = 0;
478 		next_free_pt = i | (pid & pt_size);
479 		if (i == 0)
480 			break;
481 	}
482 
483 	/* Switch tables */
484 	n_pt = pid_table;
485 	pid_table = new_pt;
486 	pid_tbl_mask = pt_size * 2 - 1;
487 
488 	/*
489 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
490 	 * allocated pids we need it to be larger!
491 	 */
492 	if (pid_tbl_mask > PID_MAX) {
493 		pid_max = pid_tbl_mask * 2 + 1;
494 		pid_alloc_lim |= pid_alloc_lim << 1;
495 	} else
496 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
497 
498 	proclist_unlock_write(s);
499 	FREE(n_pt, M_PROC);
500 }
501 
502 struct proc *
503 proc_alloc(void)
504 {
505 	struct proc *p;
506 	int s;
507 	int nxt;
508 	pid_t pid;
509 	struct pid_table *pt;
510 
511 	p = pool_get(&proc_pool, PR_WAITOK);
512 	p->p_stat = SIDL;			/* protect against others */
513 
514 	/* allocate next free pid */
515 
516 	for (;;expand_pid_table()) {
517 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
518 			/* ensure pids cycle through 2000+ values */
519 			continue;
520 		s = proclist_lock_write();
521 		pt = &pid_table[next_free_pt];
522 #ifdef DIAGNOSTIC
523 		if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
524 			panic("proc_alloc: slot busy");
525 #endif
526 		nxt = P_NEXT(pt->pt_proc);
527 		if (nxt & pid_tbl_mask)
528 			break;
529 		/* Table full - expand (NB last entry not used....) */
530 		proclist_unlock_write(s);
531 	}
532 
533 	/* pid is 'saved use count' + 'size' + entry */
534 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
535 	if ((uint)pid > (uint)pid_max)
536 		pid &= pid_tbl_mask;
537 	p->p_pid = pid;
538 	next_free_pt = nxt & pid_tbl_mask;
539 
540 	/* Grab table slot */
541 	pt->pt_proc = p;
542 	pid_alloc_cnt++;
543 
544 	proclist_unlock_write(s);
545 
546 	return p;
547 }
548 
549 /*
550  * Free last resources of a process - called from proc_free (in kern_exit.c)
551  */
552 void
553 proc_free_mem(struct proc *p)
554 {
555 	int s;
556 	pid_t pid = p->p_pid;
557 	struct pid_table *pt;
558 
559 	s = proclist_lock_write();
560 
561 	pt = &pid_table[pid & pid_tbl_mask];
562 #ifdef DIAGNOSTIC
563 	if (__predict_false(pt->pt_proc != p))
564 		panic("proc_free: pid_table mismatch, pid %x, proc %p",
565 			pid, p);
566 #endif
567 	/* save pid use count in slot */
568 	pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
569 
570 	if (pt->pt_pgrp == NULL) {
571 		/* link last freed entry onto ours */
572 		pid &= pid_tbl_mask;
573 		pt = &pid_table[last_free_pt];
574 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
575 		last_free_pt = pid;
576 		pid_alloc_cnt--;
577 	}
578 
579 	nprocs--;
580 	proclist_unlock_write(s);
581 
582 	pool_put(&proc_pool, p);
583 }
584 
585 /*
586  * Move p to a new or existing process group (and session)
587  *
588  * If we are creating a new pgrp, the pgid should equal
589  * the calling process' pid.
590  * If is only valid to enter a process group that is in the session
591  * of the process.
592  * Also mksess should only be set if we are creating a process group
593  *
594  * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the
595  * SYSV setpgrp support for hpux == enterpgrp(curproc, curproc->p_pid)
596  */
597 int
598 enterpgrp(struct proc *p, pid_t pgid, int mksess)
599 {
600 	struct pgrp *new_pgrp, *pgrp;
601 	struct session *sess;
602 	struct proc *curp = curproc;
603 	pid_t pid = p->p_pid;
604 	int rval;
605 	int s;
606 	pid_t pg_id = NO_PGID;
607 
608 	/* Allocate data areas we might need before doing any validity checks */
609 	proclist_lock_read();		/* Because pid_table might change */
610 	if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
611 		proclist_unlock_read();
612 		new_pgrp = pool_get(&pgrp_pool, PR_WAITOK);
613 	} else {
614 		proclist_unlock_read();
615 		new_pgrp = NULL;
616 	}
617 	if (mksess)
618 		sess = pool_get(&session_pool, M_WAITOK);
619 	else
620 		sess = NULL;
621 
622 	s = proclist_lock_write();
623 	rval = EPERM;	/* most common error (to save typing) */
624 
625 	/* Check pgrp exists or can be created */
626 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
627 	if (pgrp != NULL && pgrp->pg_id != pgid)
628 		goto done;
629 
630 	/* Can only set another process under restricted circumstances. */
631 	if (p != curp) {
632 		/* must exist and be one of our children... */
633 		if (p != pid_table[pid & pid_tbl_mask].pt_proc
634 		    || !inferior(p, curp)) {
635 			rval = ESRCH;
636 			goto done;
637 		}
638 		/* ... in the same session... */
639 		if (sess != NULL || p->p_session != curp->p_session)
640 			goto done;
641 		/* ... existing pgid must be in same session ... */
642 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
643 			goto done;
644 		/* ... and not done an exec. */
645 		if (p->p_flag & P_EXEC) {
646 			rval = EACCES;
647 			goto done;
648 		}
649 	}
650 
651 	/* Changing the process group/session of a session
652 	   leader is definitely off limits. */
653 	if (SESS_LEADER(p)) {
654 		if (sess == NULL && p->p_pgrp == pgrp)
655 			/* unless it's a definite noop */
656 			rval = 0;
657 		goto done;
658 	}
659 
660 	/* Can only create a process group with id of process */
661 	if (pgrp == NULL && pgid != pid)
662 		goto done;
663 
664 	/* Can only create a session if creating pgrp */
665 	if (sess != NULL && pgrp != NULL)
666 		goto done;
667 
668 	/* Check we allocated memory for a pgrp... */
669 	if (pgrp == NULL && new_pgrp == NULL)
670 		goto done;
671 
672 	/* Don't attach to 'zombie' pgrp */
673 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
674 		goto done;
675 
676 	/* Expect to succeed now */
677 	rval = 0;
678 
679 	if (pgrp == p->p_pgrp)
680 		/* nothing to do */
681 		goto done;
682 
683 	/* Ok all setup, link up required structures */
684 	if (pgrp == NULL) {
685 		pgrp = new_pgrp;
686 		new_pgrp = 0;
687 		if (sess != NULL) {
688 			sess->s_sid = p->p_pid;
689 			sess->s_leader = p;
690 			sess->s_count = 1;
691 			sess->s_ttyvp = NULL;
692 			sess->s_ttyp = NULL;
693 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
694 			memcpy(sess->s_login, p->p_session->s_login,
695 			    sizeof(sess->s_login));
696 			p->p_flag &= ~P_CONTROLT;
697 		} else {
698 			sess = p->p_pgrp->pg_session;
699 			SESSHOLD(sess);
700 		}
701 		pgrp->pg_session = sess;
702 		sess = 0;
703 
704 		pgrp->pg_id = pgid;
705 		LIST_INIT(&pgrp->pg_members);
706 #ifdef DIAGNOSTIC
707 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
708 			panic("enterpgrp: pgrp table slot in use");
709 		if (__predict_false(mksess && p != curp))
710 			panic("enterpgrp: mksession and p != curproc");
711 #endif
712 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
713 		pgrp->pg_jobc = 0;
714 	}
715 
716 	/*
717 	 * Adjust eligibility of affected pgrps to participate in job control.
718 	 * Increment eligibility counts before decrementing, otherwise we
719 	 * could reach 0 spuriously during the first call.
720 	 */
721 	fixjobc(p, pgrp, 1);
722 	fixjobc(p, p->p_pgrp, 0);
723 
724 	/* Move process to requested group */
725 	LIST_REMOVE(p, p_pglist);
726 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
727 		/* defer delete until we've dumped the lock */
728 		pg_id = p->p_pgrp->pg_id;
729 	p->p_pgrp = pgrp;
730 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
731 
732     done:
733 	proclist_unlock_write(s);
734 	if (sess != NULL)
735 		pool_put(&session_pool, sess);
736 	if (new_pgrp != NULL)
737 		pool_put(&pgrp_pool, new_pgrp);
738 	if (pg_id != NO_PGID)
739 		pg_delete(pg_id);
740 #ifdef DEBUG_PGRP
741 	if (__predict_false(rval))
742 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
743 			pid, pgid, mksess, curp->p_pid, rval);
744 #endif
745 	return rval;
746 }
747 
748 /*
749  * remove process from process group
750  */
751 int
752 leavepgrp(struct proc *p)
753 {
754 	int s;
755 	struct pgrp *pgrp;
756 	pid_t pg_id;
757 
758 	s = proclist_lock_write();
759 	pgrp = p->p_pgrp;
760 	LIST_REMOVE(p, p_pglist);
761 	p->p_pgrp = 0;
762 	pg_id = LIST_EMPTY(&pgrp->pg_members) ? pgrp->pg_id : NO_PGID;
763 	proclist_unlock_write(s);
764 
765 	if (pg_id != NO_PGID)
766 		pg_delete(pg_id);
767 	return 0;
768 }
769 
770 static void
771 pg_free(pid_t pg_id)
772 {
773 	struct pgrp *pgrp;
774 	struct pid_table *pt;
775 	int s;
776 
777 	s = proclist_lock_write();
778 	pt = &pid_table[pg_id & pid_tbl_mask];
779 	pgrp = pt->pt_pgrp;
780 #ifdef DIAGNOSTIC
781 	if (__predict_false(!pgrp || pgrp->pg_id != pg_id
782 	    || !LIST_EMPTY(&pgrp->pg_members)))
783 		panic("pg_free: process group absent or has members");
784 #endif
785 	pt->pt_pgrp = 0;
786 
787 	if (!P_VALID(pt->pt_proc)) {
788 		/* orphaned pgrp, put slot onto free list */
789 #ifdef DIAGNOSTIC
790 		if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask))
791 			panic("pg_free: process slot on free list");
792 #endif
793 
794 		pg_id &= pid_tbl_mask;
795 		pt = &pid_table[last_free_pt];
796 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
797 		last_free_pt = pg_id;
798 		pid_alloc_cnt--;
799 	}
800 	proclist_unlock_write(s);
801 
802 	pool_put(&pgrp_pool, pgrp);
803 }
804 
805 /*
806  * delete a process group
807  */
808 static void
809 pg_delete(pid_t pg_id)
810 {
811 	struct pgrp *pgrp;
812 	struct tty *ttyp;
813 	struct session *ss;
814 	int s, is_pgrp_leader;
815 
816 	s = proclist_lock_write();
817 	pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
818 	if (pgrp == NULL || pgrp->pg_id != pg_id ||
819 	    !LIST_EMPTY(&pgrp->pg_members)) {
820 		proclist_unlock_write(s);
821 		return;
822 	}
823 
824 	ss = pgrp->pg_session;
825 
826 	/* Remove reference (if any) from tty to this process group */
827 	ttyp = ss->s_ttyp;
828 	if (ttyp != NULL && ttyp->t_pgrp == pgrp) {
829 		ttyp->t_pgrp = NULL;
830 #ifdef DIAGNOSTIC
831 		if (ttyp->t_session != ss)
832 			panic("pg_delete: wrong session on terminal");
833 #endif
834 	}
835 
836 	/*
837 	 * The leading process group in a session is freed
838 	 * by sessdelete() if last reference.
839 	 */
840 	is_pgrp_leader = (ss->s_sid == pgrp->pg_id);
841 	proclist_unlock_write(s);
842 	SESSRELE(ss);
843 
844 	if (is_pgrp_leader)
845 		return;
846 
847 	pg_free(pg_id);
848 }
849 
850 /*
851  * Delete session - called from SESSRELE when s_count becomes zero.
852  */
853 void
854 sessdelete(struct session *ss)
855 {
856 	/*
857 	 * We keep the pgrp with the same id as the session in
858 	 * order to stop a process being given the same pid.
859 	 * Since the pgrp holds a reference to the session, it
860 	 * must be a 'zombie' pgrp by now.
861 	 */
862 
863 	pg_free(ss->s_sid);
864 
865 	pool_put(&session_pool, ss);
866 }
867 
868 /*
869  * Adjust pgrp jobc counters when specified process changes process group.
870  * We count the number of processes in each process group that "qualify"
871  * the group for terminal job control (those with a parent in a different
872  * process group of the same session).  If that count reaches zero, the
873  * process group becomes orphaned.  Check both the specified process'
874  * process group and that of its children.
875  * entering == 0 => p is leaving specified group.
876  * entering == 1 => p is entering specified group.
877  *
878  * Call with proclist_lock held.
879  */
880 void
881 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
882 {
883 	struct pgrp *hispgrp;
884 	struct session *mysession = pgrp->pg_session;
885 	struct proc *child;
886 
887 	/*
888 	 * Check p's parent to see whether p qualifies its own process
889 	 * group; if so, adjust count for p's process group.
890 	 */
891 	hispgrp = p->p_pptr->p_pgrp;
892 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
893 		if (entering)
894 			pgrp->pg_jobc++;
895 		else if (--pgrp->pg_jobc == 0)
896 			orphanpg(pgrp);
897 	}
898 
899 	/*
900 	 * Check this process' children to see whether they qualify
901 	 * their process groups; if so, adjust counts for children's
902 	 * process groups.
903 	 */
904 	LIST_FOREACH(child, &p->p_children, p_sibling) {
905 		hispgrp = child->p_pgrp;
906 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
907 		    !P_ZOMBIE(child)) {
908 			if (entering)
909 				hispgrp->pg_jobc++;
910 			else if (--hispgrp->pg_jobc == 0)
911 				orphanpg(hispgrp);
912 		}
913 	}
914 }
915 
916 /*
917  * A process group has become orphaned;
918  * if there are any stopped processes in the group,
919  * hang-up all process in that group.
920  *
921  * Call with proclist_lock held.
922  */
923 static void
924 orphanpg(struct pgrp *pg)
925 {
926 	struct proc *p;
927 
928 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
929 		if (p->p_stat == SSTOP) {
930 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
931 				psignal(p, SIGHUP);
932 				psignal(p, SIGCONT);
933 			}
934 			return;
935 		}
936 	}
937 }
938 
939 /* mark process as suid/sgid, reset some values to defaults */
940 void
941 p_sugid(struct proc *p)
942 {
943 	struct plimit *lim;
944 	char *cn;
945 
946 	p->p_flag |= P_SUGID;
947 	/* reset what needs to be reset in plimit */
948 	lim = p->p_limit;
949 	if (lim->pl_corename != defcorename) {
950 		if (lim->p_refcnt > 1 &&
951 		    (lim->p_lflags & PL_SHAREMOD) == 0) {
952 			p->p_limit = limcopy(lim);
953 			limfree(lim);
954 			lim = p->p_limit;
955 		}
956 		simple_lock(&lim->p_slock);
957 		cn = lim->pl_corename;
958 		lim->pl_corename = defcorename;
959 		simple_unlock(&lim->p_slock);
960 		if (cn != defcorename)
961 			free(cn, M_TEMP);
962 	}
963 }
964 
965 #ifdef DDB
966 #include <ddb/db_output.h>
967 void pidtbl_dump(void);
968 void
969 pidtbl_dump(void)
970 {
971 	struct pid_table *pt;
972 	struct proc *p;
973 	struct pgrp *pgrp;
974 	int id;
975 
976 	db_printf("pid table %p size %x, next %x, last %x\n",
977 		pid_table, pid_tbl_mask+1,
978 		next_free_pt, last_free_pt);
979 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
980 		p = pt->pt_proc;
981 		if (!P_VALID(p) && !pt->pt_pgrp)
982 			continue;
983 		db_printf("  id %x: ", id);
984 		if (P_VALID(p))
985 			db_printf("proc %p id %d (0x%x) %s\n",
986 				p, p->p_pid, p->p_pid, p->p_comm);
987 		else
988 			db_printf("next %x use %x\n",
989 				P_NEXT(p) & pid_tbl_mask,
990 				P_NEXT(p) & ~pid_tbl_mask);
991 		if ((pgrp = pt->pt_pgrp)) {
992 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
993 			    pgrp->pg_session, pgrp->pg_session->s_sid,
994 			    pgrp->pg_session->s_count,
995 			    pgrp->pg_session->s_login);
996 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
997 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
998 			    pgrp->pg_members.lh_first);
999 			for (p = pgrp->pg_members.lh_first; p != 0;
1000 			    p = p->p_pglist.le_next) {
1001 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
1002 				    p->p_pid, p, p->p_pgrp, p->p_comm);
1003 			}
1004 		}
1005 	}
1006 }
1007 #endif /* DDB */
1008 
1009 #ifdef KSTACK_CHECK_MAGIC
1010 #include <sys/user.h>
1011 
1012 #define	KSTACK_MAGIC	0xdeadbeaf
1013 
1014 /* XXX should be per process basis? */
1015 int kstackleftmin = KSTACK_SIZE;
1016 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
1017 					  less than this */
1018 
1019 void
1020 kstack_setup_magic(const struct lwp *l)
1021 {
1022 	u_int32_t *ip;
1023 	u_int32_t const *end;
1024 
1025 	KASSERT(l != NULL);
1026 	KASSERT(l != &lwp0);
1027 
1028 	/*
1029 	 * fill all the stack with magic number
1030 	 * so that later modification on it can be detected.
1031 	 */
1032 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
1033 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1034 	for (; ip < end; ip++) {
1035 		*ip = KSTACK_MAGIC;
1036 	}
1037 }
1038 
1039 void
1040 kstack_check_magic(const struct lwp *l)
1041 {
1042 	u_int32_t const *ip, *end;
1043 	int stackleft;
1044 
1045 	KASSERT(l != NULL);
1046 
1047 	/* don't check proc0 */ /*XXX*/
1048 	if (l == &lwp0)
1049 		return;
1050 
1051 #ifdef __MACHINE_STACK_GROWS_UP
1052 	/* stack grows upwards (eg. hppa) */
1053 	ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1054 	end = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
1055 	for (ip--; ip >= end; ip--)
1056 		if (*ip != KSTACK_MAGIC)
1057 			break;
1058 
1059 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
1060 #else /* __MACHINE_STACK_GROWS_UP */
1061 	/* stack grows downwards (eg. i386) */
1062 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
1063 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1064 	for (; ip < end; ip++)
1065 		if (*ip != KSTACK_MAGIC)
1066 			break;
1067 
1068 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(l);
1069 #endif /* __MACHINE_STACK_GROWS_UP */
1070 
1071 	if (kstackleftmin > stackleft) {
1072 		kstackleftmin = stackleft;
1073 		if (stackleft < kstackleftthres)
1074 			printf("warning: kernel stack left %d bytes"
1075 			    "(pid %u:lid %u)\n", stackleft,
1076 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1077 	}
1078 
1079 	if (stackleft <= 0) {
1080 		panic("magic on the top of kernel stack changed for "
1081 		    "pid %u, lid %u: maybe kernel stack overflow",
1082 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1083 	}
1084 }
1085 #endif /* KSTACK_CHECK_MAGIC */
1086