Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.109
      1 /*	$NetBSD: kern_proc.c,v 1.109 2007/04/30 20:11:41 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2006, 2007 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, and by Andrew Doran.
     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.109 2007/04/30 20:11:41 dsl Exp $");
     73 
     74 #include "opt_kstack.h"
     75 #include "opt_maxuprc.h"
     76 #include "opt_multiprocessor.h"
     77 #include "opt_lockdebug.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/proc.h>
     83 #include <sys/resourcevar.h>
     84 #include <sys/buf.h>
     85 #include <sys/acct.h>
     86 #include <sys/wait.h>
     87 #include <sys/file.h>
     88 #include <ufs/ufs/quota.h>
     89 #include <sys/uio.h>
     90 #include <sys/malloc.h>
     91 #include <sys/pool.h>
     92 #include <sys/mbuf.h>
     93 #include <sys/ioctl.h>
     94 #include <sys/tty.h>
     95 #include <sys/signalvar.h>
     96 #include <sys/ras.h>
     97 #include <sys/filedesc.h>
     98 #include "sys/syscall_stats.h"
     99 #include <sys/kauth.h>
    100 #include <sys/sleepq.h>
    101 
    102 #include <uvm/uvm.h>
    103 #include <uvm/uvm_extern.h>
    104 
    105 /*
    106  * Other process lists
    107  */
    108 
    109 struct proclist allproc;
    110 struct proclist zombproc;	/* resources have been freed */
    111 
    112 /*
    113  * There are two locks on global process state.
    114  *
    115  * 1. proclist_lock is an adaptive mutex and is used when modifying
    116  * or examining process state from a process context.  It protects
    117  * the internal tables, all of the process lists, and a number of
    118  * members of struct proc.
    119  *
    120  * 2. proclist_mutex is used when allproc must be traversed from an
    121  * interrupt context, or when changing the state of processes.  The
    122  * proclist_lock should always be used in preference.  In some cases,
    123  * both locks need to be held.
    124  *
    125  *	proclist_lock	proclist_mutex	structure
    126  *	--------------- --------------- -----------------
    127  *	x				zombproc
    128  *	x		x		pid_table
    129  *	x				proc::p_pptr
    130  *	x				proc::p_sibling
    131  *	x				proc::p_children
    132  *	x		x		allproc
    133  *	x		x		proc::p_pgrp
    134  *	x		x		proc::p_pglist
    135  *	x		x		proc::p_session
    136  *	x		x		proc::p_list
    137  *			x		alllwp
    138  *			x		lwp::l_list
    139  *
    140  * The lock order for processes and LWPs is approximately as following:
    141  *
    142  * kernel_lock
    143  * -> proclist_lock
    144  *   -> proc::p_mutex
    145  *      -> proclist_mutex
    146  *         -> proc::p_smutex
    147  *           -> proc::p_stmutex
    148  *
    149  * XXX p_smutex can be run at IPL_VM once audio drivers on the x86
    150  * platform are made MP safe.  Currently it blocks interrupts at
    151  * IPL_SCHED and below.
    152  *
    153  * XXX The two process locks (p_smutex + p_mutex), and the two global
    154  * state locks (proclist_lock + proclist_mutex) should be merged
    155  * together.  However, to do so requires interrupts that interrupts
    156  * be run with LWP context.
    157  */
    158 kmutex_t	proclist_lock;
    159 kmutex_t	proclist_mutex;
    160 
    161 /*
    162  * pid to proc lookup is done by indexing the pid_table array.
    163  * Since pid numbers are only allocated when an empty slot
    164  * has been found, there is no need to search any lists ever.
    165  * (an orphaned pgrp will lock the slot, a session will lock
    166  * the pgrp with the same number.)
    167  * If the table is too small it is reallocated with twice the
    168  * previous size and the entries 'unzipped' into the two halves.
    169  * A linked list of free entries is passed through the pt_proc
    170  * field of 'free' items - set odd to be an invalid ptr.
    171  */
    172 
    173 struct pid_table {
    174 	struct proc	*pt_proc;
    175 	struct pgrp	*pt_pgrp;
    176 };
    177 #if 1	/* strongly typed cast - should be a noop */
    178 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
    179 #else
    180 #define p2u(p) ((uint)p)
    181 #endif
    182 #define P_VALID(p) (!(p2u(p) & 1))
    183 #define P_NEXT(p) (p2u(p) >> 1)
    184 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
    185 
    186 #define INITIAL_PID_TABLE_SIZE	(1 << 5)
    187 static struct pid_table *pid_table;
    188 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    189 static uint pid_alloc_lim;	/* max we allocate before growing table */
    190 static uint pid_alloc_cnt;	/* number of allocated pids */
    191 
    192 /* links through free slots - never empty! */
    193 static uint next_free_pt, last_free_pt;
    194 static pid_t pid_max = PID_MAX;		/* largest value we allocate */
    195 
    196 /* Components of the first process -- never freed. */
    197 struct session session0;
    198 struct pgrp pgrp0;
    199 struct proc proc0;
    200 struct lwp lwp0 __aligned(MIN_LWP_ALIGNMENT);
    201 kauth_cred_t cred0;
    202 struct filedesc0 filedesc0;
    203 struct cwdinfo cwdi0;
    204 struct plimit limit0;
    205 struct pstats pstat0;
    206 struct vmspace vmspace0;
    207 struct sigacts sigacts0;
    208 struct turnstile turnstile0;
    209 
    210 extern struct user *proc0paddr;
    211 
    212 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
    213 
    214 int nofile = NOFILE;
    215 int maxuprc = MAXUPRC;
    216 int cmask = CMASK;
    217 
    218 POOL_INIT(proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    219     &pool_allocator_nointr, IPL_NONE);
    220 POOL_INIT(pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    221     &pool_allocator_nointr, IPL_NONE);
    222 POOL_INIT(plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    223     &pool_allocator_nointr, IPL_NONE);
    224 POOL_INIT(pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    225     &pool_allocator_nointr, IPL_NONE);
    226 POOL_INIT(session_pool, sizeof(struct session), 0, 0, 0, "sessionpl",
    227     &pool_allocator_nointr, IPL_NONE);
    228 
    229 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
    230 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
    231 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
    232 
    233 /*
    234  * The process list descriptors, used during pid allocation and
    235  * by sysctl.  No locking on this data structure is needed since
    236  * it is completely static.
    237  */
    238 const struct proclist_desc proclists[] = {
    239 	{ &allproc	},
    240 	{ &zombproc	},
    241 	{ NULL		},
    242 };
    243 
    244 static void orphanpg(struct pgrp *);
    245 static void pg_delete(pid_t);
    246 
    247 static specificdata_domain_t proc_specificdata_domain;
    248 
    249 /*
    250  * Initialize global process hashing structures.
    251  */
    252 void
    253 procinit(void)
    254 {
    255 	const struct proclist_desc *pd;
    256 	int i;
    257 #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    258 
    259 	for (pd = proclists; pd->pd_list != NULL; pd++)
    260 		LIST_INIT(pd->pd_list);
    261 
    262 	mutex_init(&proclist_lock, MUTEX_DEFAULT, IPL_NONE);
    263 	mutex_init(&proclist_mutex, MUTEX_SPIN, IPL_SCHED);
    264 
    265 	pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table,
    266 			    M_PROC, M_WAITOK);
    267 	/* Set free list running through table...
    268 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    269 	for (i = 0; i <= pid_tbl_mask; i++) {
    270 		pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
    271 		pid_table[i].pt_pgrp = 0;
    272 	}
    273 	/* slot 0 is just grabbed */
    274 	next_free_pt = 1;
    275 	/* Need to fix last entry. */
    276 	last_free_pt = pid_tbl_mask;
    277 	pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
    278 	/* point at which we grow table - to avoid reusing pids too often */
    279 	pid_alloc_lim = pid_tbl_mask - 1;
    280 #undef LINK_EMPTY
    281 
    282 	LIST_INIT(&alllwp);
    283 
    284 	uihashtbl =
    285 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    286 
    287 	proc_specificdata_domain = specificdata_domain_create();
    288 	KASSERT(proc_specificdata_domain != NULL);
    289 }
    290 
    291 /*
    292  * Initialize process 0.
    293  */
    294 void
    295 proc0_init(void)
    296 {
    297 	struct proc *p;
    298 	struct pgrp *pg;
    299 	struct session *sess;
    300 	struct lwp *l;
    301 	u_int i;
    302 	rlim_t lim;
    303 
    304 	p = &proc0;
    305 	pg = &pgrp0;
    306 	sess = &session0;
    307 	l = &lwp0;
    308 
    309 	/*
    310 	 * XXX p_rasmutex is run at IPL_SCHED, because of lock order
    311 	 * issues (kernel_lock -> p_rasmutex).  Ideally ras_lookup
    312 	 * should operate "lock free".
    313 	 */
    314 	mutex_init(&p->p_smutex, MUTEX_SPIN, IPL_SCHED);
    315 	mutex_init(&p->p_stmutex, MUTEX_SPIN, IPL_STATCLOCK);
    316 	mutex_init(&p->p_rasmutex, MUTEX_SPIN, IPL_SCHED);
    317 	mutex_init(&p->p_mutex, MUTEX_DEFAULT, IPL_NONE);
    318 
    319 	cv_init(&p->p_refcv, "drainref");
    320 	cv_init(&p->p_waitcv, "wait");
    321 	cv_init(&p->p_lwpcv, "lwpwait");
    322 
    323 	LIST_INIT(&p->p_lwps);
    324 	LIST_INIT(&p->p_sigwaiters);
    325 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    326 
    327 	p->p_nlwps = 1;
    328 	p->p_nrlwps = 1;
    329 	p->p_refcnt = 1;
    330 
    331 	pid_table[0].pt_proc = p;
    332 	LIST_INSERT_HEAD(&allproc, p, p_list);
    333 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    334 
    335 	p->p_pgrp = pg;
    336 	pid_table[0].pt_pgrp = pg;
    337 	LIST_INIT(&pg->pg_members);
    338 	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
    339 
    340 	pg->pg_session = sess;
    341 	sess->s_count = 1;
    342 	sess->s_sid = 0;
    343 	sess->s_leader = p;
    344 
    345 	/*
    346 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    347 	 * init(8) when they exit.  init(8) can easily wait them out
    348 	 * for us.
    349 	 */
    350 	p->p_flag = PK_SYSTEM | PK_NOCLDWAIT;
    351 	p->p_stat = SACTIVE;
    352 	p->p_nice = NZERO;
    353 	p->p_emul = &emul_netbsd;
    354 #ifdef __HAVE_SYSCALL_INTERN
    355 	(*p->p_emul->e_syscall_intern)(p);
    356 #endif
    357 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
    358 
    359 	l->l_mutex = &sched_mutex;
    360 	l->l_flag = LW_INMEM | LW_SYSTEM;
    361 	l->l_stat = LSONPROC;
    362 	l->l_ts = &turnstile0;
    363 	l->l_syncobj = &sched_syncobj;
    364 	l->l_refcnt = 1;
    365 	l->l_cpu = curcpu();
    366 	l->l_priority = PRIBIO;
    367 	l->l_usrpri = PRIBIO;
    368 	l->l_inheritedprio = MAXPRI;
    369 	SLIST_INIT(&l->l_pi_lenders);
    370 
    371 	callout_init(&l->l_tsleep_ch);
    372 	cv_init(&l->l_sigcv, "sigwait");
    373 
    374 	/* Create credentials. */
    375 	cred0 = kauth_cred_alloc();
    376 	p->p_cred = cred0;
    377 	kauth_cred_hold(cred0);
    378 	l->l_cred = cred0;
    379 
    380 	/* Create the CWD info. */
    381 	p->p_cwdi = &cwdi0;
    382 	cwdi0.cwdi_cmask = cmask;
    383 	cwdi0.cwdi_refcnt = 1;
    384 	simple_lock_init(&cwdi0.cwdi_slock);
    385 
    386 	/* Create the limits structures. */
    387 	p->p_limit = &limit0;
    388 	simple_lock_init(&limit0.p_slock);
    389 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    390 		limit0.pl_rlimit[i].rlim_cur =
    391 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    392 
    393 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
    394 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
    395 	    maxfiles < nofile ? maxfiles : nofile;
    396 
    397 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
    398 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
    399 	    maxproc < maxuprc ? maxproc : maxuprc;
    400 
    401 	lim = ptoa(uvmexp.free);
    402 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
    403 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
    404 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    405 	limit0.pl_corename = defcorename;
    406 	limit0.p_refcnt = 1;
    407 
    408 	/* Configure virtual memory system, set vm rlimits. */
    409 	uvm_init_limits(p);
    410 
    411 	/* Initialize file descriptor table for proc0. */
    412 	p->p_fd = &filedesc0.fd_fd;
    413 	fdinit1(&filedesc0);
    414 
    415 	/*
    416 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    417 	 * All kernel processes (which never have user space mappings)
    418 	 * share proc0's vmspace, and thus, the kernel pmap.
    419 	 */
    420 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    421 	    trunc_page(VM_MAX_ADDRESS));
    422 	p->p_vmspace = &vmspace0;
    423 
    424 	l->l_addr = proc0paddr;				/* XXX */
    425 
    426 	p->p_stats = &pstat0;
    427 
    428 	/* Initialize signal state for proc0. */
    429 	p->p_sigacts = &sigacts0;
    430 	mutex_init(&p->p_sigacts->sa_mutex, MUTEX_SPIN, IPL_NONE);
    431 	siginit(p);
    432 
    433 	proc_initspecific(p);
    434 	lwp_initspecific(l);
    435 
    436 	SYSCALL_TIME_LWP_INIT(l);
    437 }
    438 
    439 /*
    440  * Check that the specified process group is in the session of the
    441  * specified process.
    442  * Treats -ve ids as process ids.
    443  * Used to validate TIOCSPGRP requests.
    444  */
    445 int
    446 pgid_in_session(struct proc *p, pid_t pg_id)
    447 {
    448 	struct pgrp *pgrp;
    449 	struct session *session;
    450 	int error;
    451 
    452 	mutex_enter(&proclist_lock);
    453 	if (pg_id < 0) {
    454 		struct proc *p1 = p_find(-pg_id, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    455 		if (p1 == NULL)
    456 			return EINVAL;
    457 		pgrp = p1->p_pgrp;
    458 	} else {
    459 		pgrp = pg_find(pg_id, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    460 		if (pgrp == NULL)
    461 			return EINVAL;
    462 	}
    463 	session = pgrp->pg_session;
    464 	if (session != p->p_pgrp->pg_session)
    465 		error = EPERM;
    466 	else
    467 		error = 0;
    468 	mutex_exit(&proclist_lock);
    469 
    470 	return error;
    471 }
    472 
    473 /*
    474  * Is p an inferior of q?
    475  *
    476  * Call with the proclist_lock held.
    477  */
    478 int
    479 inferior(struct proc *p, struct proc *q)
    480 {
    481 
    482 	for (; p != q; p = p->p_pptr)
    483 		if (p->p_pid == 0)
    484 			return 0;
    485 	return 1;
    486 }
    487 
    488 /*
    489  * Locate a process by number
    490  */
    491 struct proc *
    492 p_find(pid_t pid, uint flags)
    493 {
    494 	struct proc *p;
    495 	char stat;
    496 
    497 	if (!(flags & PFIND_LOCKED))
    498 		mutex_enter(&proclist_lock);
    499 
    500 	p = pid_table[pid & pid_tbl_mask].pt_proc;
    501 
    502 	/* Only allow live processes to be found by pid. */
    503 	/* XXXSMP p_stat */
    504 	if (P_VALID(p) && p->p_pid == pid && ((stat = p->p_stat) == SACTIVE ||
    505 	    stat == SSTOP || ((flags & PFIND_ZOMBIE) &&
    506 	    (stat == SZOMB || stat == SDEAD || stat == SDYING)))) {
    507 		if (flags & PFIND_UNLOCK_OK)
    508 			 mutex_exit(&proclist_lock);
    509 		return p;
    510 	}
    511 	if (flags & PFIND_UNLOCK_FAIL)
    512 		mutex_exit(&proclist_lock);
    513 	return NULL;
    514 }
    515 
    516 
    517 /*
    518  * Locate a process group by number
    519  */
    520 struct pgrp *
    521 pg_find(pid_t pgid, uint flags)
    522 {
    523 	struct pgrp *pg;
    524 
    525 	if (!(flags & PFIND_LOCKED))
    526 		mutex_enter(&proclist_lock);
    527 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    528 	/*
    529 	 * Can't look up a pgrp that only exists because the session
    530 	 * hasn't died yet (traditional)
    531 	 */
    532 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    533 		if (flags & PFIND_UNLOCK_FAIL)
    534 			 mutex_exit(&proclist_lock);
    535 		return NULL;
    536 	}
    537 
    538 	if (flags & PFIND_UNLOCK_OK)
    539 		mutex_exit(&proclist_lock);
    540 	return pg;
    541 }
    542 
    543 static void
    544 expand_pid_table(void)
    545 {
    546 	uint pt_size = pid_tbl_mask + 1;
    547 	struct pid_table *n_pt, *new_pt;
    548 	struct proc *proc;
    549 	struct pgrp *pgrp;
    550 	int i;
    551 	pid_t pid;
    552 
    553 	new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK);
    554 
    555 	mutex_enter(&proclist_lock);
    556 	if (pt_size != pid_tbl_mask + 1) {
    557 		/* Another process beat us to it... */
    558 		mutex_exit(&proclist_lock);
    559 		FREE(new_pt, M_PROC);
    560 		return;
    561 	}
    562 
    563 	/*
    564 	 * Copy entries from old table into new one.
    565 	 * If 'pid' is 'odd' we need to place in the upper half,
    566 	 * even pid's to the lower half.
    567 	 * Free items stay in the low half so we don't have to
    568 	 * fixup the reference to them.
    569 	 * We stuff free items on the front of the freelist
    570 	 * because we can't write to unmodified entries.
    571 	 * Processing the table backwards maintains a semblance
    572 	 * of issueing pid numbers that increase with time.
    573 	 */
    574 	i = pt_size - 1;
    575 	n_pt = new_pt + i;
    576 	for (; ; i--, n_pt--) {
    577 		proc = pid_table[i].pt_proc;
    578 		pgrp = pid_table[i].pt_pgrp;
    579 		if (!P_VALID(proc)) {
    580 			/* Up 'use count' so that link is valid */
    581 			pid = (P_NEXT(proc) + pt_size) & ~pt_size;
    582 			proc = P_FREE(pid);
    583 			if (pgrp)
    584 				pid = pgrp->pg_id;
    585 		} else
    586 			pid = proc->p_pid;
    587 
    588 		/* Save entry in appropriate half of table */
    589 		n_pt[pid & pt_size].pt_proc = proc;
    590 		n_pt[pid & pt_size].pt_pgrp = pgrp;
    591 
    592 		/* Put other piece on start of free list */
    593 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
    594 		n_pt[pid & pt_size].pt_proc =
    595 				    P_FREE((pid & ~pt_size) | next_free_pt);
    596 		n_pt[pid & pt_size].pt_pgrp = 0;
    597 		next_free_pt = i | (pid & pt_size);
    598 		if (i == 0)
    599 			break;
    600 	}
    601 
    602 	/* Switch tables */
    603 	mutex_enter(&proclist_mutex);
    604 	n_pt = pid_table;
    605 	pid_table = new_pt;
    606 	mutex_exit(&proclist_mutex);
    607 	pid_tbl_mask = pt_size * 2 - 1;
    608 
    609 	/*
    610 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
    611 	 * allocated pids we need it to be larger!
    612 	 */
    613 	if (pid_tbl_mask > PID_MAX) {
    614 		pid_max = pid_tbl_mask * 2 + 1;
    615 		pid_alloc_lim |= pid_alloc_lim << 1;
    616 	} else
    617 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
    618 
    619 	mutex_exit(&proclist_lock);
    620 	FREE(n_pt, M_PROC);
    621 }
    622 
    623 struct proc *
    624 proc_alloc(void)
    625 {
    626 	struct proc *p;
    627 	int nxt;
    628 	pid_t pid;
    629 	struct pid_table *pt;
    630 
    631 	p = pool_get(&proc_pool, PR_WAITOK);
    632 	p->p_stat = SIDL;			/* protect against others */
    633 
    634 	proc_initspecific(p);
    635 	/* allocate next free pid */
    636 
    637 	for (;;expand_pid_table()) {
    638 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
    639 			/* ensure pids cycle through 2000+ values */
    640 			continue;
    641 		mutex_enter(&proclist_lock);
    642 		pt = &pid_table[next_free_pt];
    643 #ifdef DIAGNOSTIC
    644 		if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
    645 			panic("proc_alloc: slot busy");
    646 #endif
    647 		nxt = P_NEXT(pt->pt_proc);
    648 		if (nxt & pid_tbl_mask)
    649 			break;
    650 		/* Table full - expand (NB last entry not used....) */
    651 		mutex_exit(&proclist_lock);
    652 	}
    653 
    654 	/* pid is 'saved use count' + 'size' + entry */
    655 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
    656 	if ((uint)pid > (uint)pid_max)
    657 		pid &= pid_tbl_mask;
    658 	p->p_pid = pid;
    659 	next_free_pt = nxt & pid_tbl_mask;
    660 
    661 	/* Grab table slot */
    662 	mutex_enter(&proclist_mutex);
    663 	pt->pt_proc = p;
    664 	mutex_exit(&proclist_mutex);
    665 	pid_alloc_cnt++;
    666 
    667 	mutex_exit(&proclist_lock);
    668 
    669 	return p;
    670 }
    671 
    672 /*
    673  * Free last resources of a process - called from proc_free (in kern_exit.c)
    674  *
    675  * Called with the proclist_lock held, and releases upon exit.
    676  */
    677 void
    678 proc_free_mem(struct proc *p)
    679 {
    680 	pid_t pid = p->p_pid;
    681 	struct pid_table *pt;
    682 
    683 	KASSERT(mutex_owned(&proclist_lock));
    684 
    685 	pt = &pid_table[pid & pid_tbl_mask];
    686 #ifdef DIAGNOSTIC
    687 	if (__predict_false(pt->pt_proc != p))
    688 		panic("proc_free: pid_table mismatch, pid %x, proc %p",
    689 			pid, p);
    690 #endif
    691 	mutex_enter(&proclist_mutex);
    692 	/* save pid use count in slot */
    693 	pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
    694 
    695 	if (pt->pt_pgrp == NULL) {
    696 		/* link last freed entry onto ours */
    697 		pid &= pid_tbl_mask;
    698 		pt = &pid_table[last_free_pt];
    699 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
    700 		last_free_pt = pid;
    701 		pid_alloc_cnt--;
    702 	}
    703 	mutex_exit(&proclist_mutex);
    704 
    705 	nprocs--;
    706 	mutex_exit(&proclist_lock);
    707 
    708 	pool_put(&proc_pool, p);
    709 }
    710 
    711 /*
    712  * Move p to a new or existing process group (and session)
    713  *
    714  * If we are creating a new pgrp, the pgid should equal
    715  * the calling process' pid.
    716  * If is only valid to enter a process group that is in the session
    717  * of the process.
    718  * Also mksess should only be set if we are creating a process group
    719  *
    720  * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the
    721  * SYSV setpgrp support for hpux.
    722  */
    723 int
    724 enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, int mksess)
    725 {
    726 	struct pgrp *new_pgrp, *pgrp;
    727 	struct session *sess;
    728 	struct proc *p;
    729 	int rval;
    730 	pid_t pg_id = NO_PGID;
    731 
    732 	if (mksess)
    733 		sess = pool_get(&session_pool, PR_WAITOK);
    734 	else
    735 		sess = NULL;
    736 
    737 	/* Allocate data areas we might need before doing any validity checks */
    738 	mutex_enter(&proclist_lock);		/* Because pid_table might change */
    739 	if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
    740 		mutex_exit(&proclist_lock);
    741 		new_pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    742 		mutex_enter(&proclist_lock);
    743 	} else
    744 		new_pgrp = NULL;
    745 	rval = EPERM;	/* most common error (to save typing) */
    746 
    747 	/* Check pgrp exists or can be created */
    748 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    749 	if (pgrp != NULL && pgrp->pg_id != pgid)
    750 		goto done;
    751 
    752 	/* Can only set another process under restricted circumstances. */
    753 	if (pid != curp->p_pid) {
    754 		/* must exist and be one of our children... */
    755 		if ((p = p_find(pid, PFIND_LOCKED)) == NULL ||
    756 		    !inferior(p, curp)) {
    757 			rval = ESRCH;
    758 			goto done;
    759 		}
    760 		/* ... in the same session... */
    761 		if (sess != NULL || p->p_session != curp->p_session)
    762 			goto done;
    763 		/* ... existing pgid must be in same session ... */
    764 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
    765 			goto done;
    766 		/* ... and not done an exec. */
    767 		if (p->p_flag & PK_EXEC) {
    768 			rval = EACCES;
    769 			goto done;
    770 		}
    771 	} else {
    772 		/* ... setsid() cannot re-enter a pgrp */
    773 		if (mksess && (curp->p_pgid == curp->p_pid ||
    774 		    pg_find(curp->p_pid, PFIND_LOCKED)))
    775 			goto done;
    776 		p = curp;
    777 	}
    778 
    779 	/* Changing the process group/session of a session
    780 	   leader is definitely off limits. */
    781 	if (SESS_LEADER(p)) {
    782 		if (sess == NULL && p->p_pgrp == pgrp)
    783 			/* unless it's a definite noop */
    784 			rval = 0;
    785 		goto done;
    786 	}
    787 
    788 	/* Can only create a process group with id of process */
    789 	if (pgrp == NULL && pgid != pid)
    790 		goto done;
    791 
    792 	/* Can only create a session if creating pgrp */
    793 	if (sess != NULL && pgrp != NULL)
    794 		goto done;
    795 
    796 	/* Check we allocated memory for a pgrp... */
    797 	if (pgrp == NULL && new_pgrp == NULL)
    798 		goto done;
    799 
    800 	/* Don't attach to 'zombie' pgrp */
    801 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
    802 		goto done;
    803 
    804 	/* Expect to succeed now */
    805 	rval = 0;
    806 
    807 	if (pgrp == p->p_pgrp)
    808 		/* nothing to do */
    809 		goto done;
    810 
    811 	/* Ok all setup, link up required structures */
    812 
    813 	if (pgrp == NULL) {
    814 		pgrp = new_pgrp;
    815 		new_pgrp = 0;
    816 		if (sess != NULL) {
    817 			sess->s_sid = p->p_pid;
    818 			sess->s_leader = p;
    819 			sess->s_count = 1;
    820 			sess->s_ttyvp = NULL;
    821 			sess->s_ttyp = NULL;
    822 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
    823 			memcpy(sess->s_login, p->p_session->s_login,
    824 			    sizeof(sess->s_login));
    825 			p->p_lflag &= ~PL_CONTROLT;
    826 		} else {
    827 			sess = p->p_pgrp->pg_session;
    828 			SESSHOLD(sess);
    829 		}
    830 		pgrp->pg_session = sess;
    831 		sess = 0;
    832 
    833 		pgrp->pg_id = pgid;
    834 		LIST_INIT(&pgrp->pg_members);
    835 #ifdef DIAGNOSTIC
    836 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
    837 			panic("enterpgrp: pgrp table slot in use");
    838 		if (__predict_false(mksess && p != curp))
    839 			panic("enterpgrp: mksession and p != curproc");
    840 #endif
    841 		mutex_enter(&proclist_mutex);
    842 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
    843 		pgrp->pg_jobc = 0;
    844 	} else
    845 		mutex_enter(&proclist_mutex);
    846 
    847 #ifdef notyet
    848 	/*
    849 	 * If there's a controlling terminal for the current session, we
    850 	 * have to interlock with it.  See ttread().
    851 	 */
    852 	if (p->p_session->s_ttyvp != NULL) {
    853 		tp = p->p_session->s_ttyp;
    854 		mutex_enter(&tp->t_mutex);
    855 	} else
    856 		tp = NULL;
    857 #endif
    858 
    859 	/*
    860 	 * Adjust eligibility of affected pgrps to participate in job control.
    861 	 * Increment eligibility counts before decrementing, otherwise we
    862 	 * could reach 0 spuriously during the first call.
    863 	 */
    864 	fixjobc(p, pgrp, 1);
    865 	fixjobc(p, p->p_pgrp, 0);
    866 
    867 	/* Move process to requested group. */
    868 	LIST_REMOVE(p, p_pglist);
    869 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    870 		/* defer delete until we've dumped the lock */
    871 		pg_id = p->p_pgrp->pg_id;
    872 	p->p_pgrp = pgrp;
    873 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    874 	mutex_exit(&proclist_mutex);
    875 
    876 #ifdef notyet
    877 	/* Done with the swap; we can release the tty mutex. */
    878 	if (tp != NULL)
    879 		mutex_exit(&tp->t_mutex);
    880 #endif
    881 
    882     done:
    883 	if (pg_id != NO_PGID)
    884 		pg_delete(pg_id);
    885 	mutex_exit(&proclist_lock);
    886 	if (sess != NULL)
    887 		pool_put(&session_pool, sess);
    888 	if (new_pgrp != NULL)
    889 		pool_put(&pgrp_pool, new_pgrp);
    890 #ifdef DEBUG_PGRP
    891 	if (__predict_false(rval))
    892 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
    893 			pid, pgid, mksess, curp->p_pid, rval);
    894 #endif
    895 	return rval;
    896 }
    897 
    898 /*
    899  * Remove a process from its process group.  Must be called with the
    900  * proclist_lock held.
    901  */
    902 void
    903 leavepgrp(struct proc *p)
    904 {
    905 	struct pgrp *pgrp;
    906 
    907 	KASSERT(mutex_owned(&proclist_lock));
    908 
    909 	/*
    910 	 * If there's a controlling terminal for the session, we have to
    911 	 * interlock with it.  See ttread().
    912 	 */
    913 	mutex_enter(&proclist_mutex);
    914 #ifdef notyet
    915 	if (p_>p_session->s_ttyvp != NULL) {
    916 		tp = p->p_session->s_ttyp;
    917 		mutex_enter(&tp->t_mutex);
    918 	} else
    919 		tp = NULL;
    920 #endif
    921 
    922 	pgrp = p->p_pgrp;
    923 	LIST_REMOVE(p, p_pglist);
    924 	p->p_pgrp = NULL;
    925 
    926 #ifdef notyet
    927 	if (tp != NULL)
    928 		mutex_exit(&tp->t_mutex);
    929 #endif
    930 	mutex_exit(&proclist_mutex);
    931 
    932 	if (LIST_EMPTY(&pgrp->pg_members))
    933 		pg_delete(pgrp->pg_id);
    934 }
    935 
    936 /*
    937  * Free a process group.  Must be called with the proclist_lock held.
    938  */
    939 static void
    940 pg_free(pid_t pg_id)
    941 {
    942 	struct pgrp *pgrp;
    943 	struct pid_table *pt;
    944 
    945 	KASSERT(mutex_owned(&proclist_lock));
    946 
    947 	pt = &pid_table[pg_id & pid_tbl_mask];
    948 	pgrp = pt->pt_pgrp;
    949 #ifdef DIAGNOSTIC
    950 	if (__predict_false(!pgrp || pgrp->pg_id != pg_id
    951 	    || !LIST_EMPTY(&pgrp->pg_members)))
    952 		panic("pg_free: process group absent or has members");
    953 #endif
    954 	pt->pt_pgrp = 0;
    955 
    956 	if (!P_VALID(pt->pt_proc)) {
    957 		/* orphaned pgrp, put slot onto free list */
    958 #ifdef DIAGNOSTIC
    959 		if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask))
    960 			panic("pg_free: process slot on free list");
    961 #endif
    962 		mutex_enter(&proclist_mutex);
    963 		pg_id &= pid_tbl_mask;
    964 		pt = &pid_table[last_free_pt];
    965 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
    966 		mutex_exit(&proclist_mutex);
    967 		last_free_pt = pg_id;
    968 		pid_alloc_cnt--;
    969 	}
    970 	pool_put(&pgrp_pool, pgrp);
    971 }
    972 
    973 /*
    974  * Delete a process group.  Must be called with the proclist_lock held.
    975  */
    976 static void
    977 pg_delete(pid_t pg_id)
    978 {
    979 	struct pgrp *pgrp;
    980 	struct tty *ttyp;
    981 	struct session *ss;
    982 	int is_pgrp_leader;
    983 
    984 	KASSERT(mutex_owned(&proclist_lock));
    985 
    986 	pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
    987 	if (pgrp == NULL || pgrp->pg_id != pg_id ||
    988 	    !LIST_EMPTY(&pgrp->pg_members))
    989 		return;
    990 
    991 	ss = pgrp->pg_session;
    992 
    993 	/* Remove reference (if any) from tty to this process group */
    994 	ttyp = ss->s_ttyp;
    995 	if (ttyp != NULL && ttyp->t_pgrp == pgrp) {
    996 		ttyp->t_pgrp = NULL;
    997 #ifdef DIAGNOSTIC
    998 		if (ttyp->t_session != ss)
    999 			panic("pg_delete: wrong session on terminal");
   1000 #endif
   1001 	}
   1002 
   1003 	/*
   1004 	 * The leading process group in a session is freed
   1005 	 * by sessdelete() if last reference.
   1006 	 */
   1007 	is_pgrp_leader = (ss->s_sid == pgrp->pg_id);
   1008 	SESSRELE(ss);
   1009 
   1010 	if (is_pgrp_leader)
   1011 		return;
   1012 
   1013 	pg_free(pg_id);
   1014 }
   1015 
   1016 /*
   1017  * Delete session - called from SESSRELE when s_count becomes zero.
   1018  * Must be called with the proclist_lock held.
   1019  */
   1020 void
   1021 sessdelete(struct session *ss)
   1022 {
   1023 
   1024 	KASSERT(mutex_owned(&proclist_lock));
   1025 
   1026 	/*
   1027 	 * We keep the pgrp with the same id as the session in
   1028 	 * order to stop a process being given the same pid.
   1029 	 * Since the pgrp holds a reference to the session, it
   1030 	 * must be a 'zombie' pgrp by now.
   1031 	 */
   1032 	pg_free(ss->s_sid);
   1033 	pool_put(&session_pool, ss);
   1034 }
   1035 
   1036 /*
   1037  * Adjust pgrp jobc counters when specified process changes process group.
   1038  * We count the number of processes in each process group that "qualify"
   1039  * the group for terminal job control (those with a parent in a different
   1040  * process group of the same session).  If that count reaches zero, the
   1041  * process group becomes orphaned.  Check both the specified process'
   1042  * process group and that of its children.
   1043  * entering == 0 => p is leaving specified group.
   1044  * entering == 1 => p is entering specified group.
   1045  *
   1046  * Call with proclist_lock held.
   1047  */
   1048 void
   1049 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
   1050 {
   1051 	struct pgrp *hispgrp;
   1052 	struct session *mysession = pgrp->pg_session;
   1053 	struct proc *child;
   1054 
   1055 	KASSERT(mutex_owned(&proclist_lock));
   1056 	KASSERT(mutex_owned(&proclist_mutex));
   1057 
   1058 	/*
   1059 	 * Check p's parent to see whether p qualifies its own process
   1060 	 * group; if so, adjust count for p's process group.
   1061 	 */
   1062 	hispgrp = p->p_pptr->p_pgrp;
   1063 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
   1064 		if (entering) {
   1065 			mutex_enter(&p->p_smutex);
   1066 			p->p_sflag &= ~PS_ORPHANPG;
   1067 			mutex_exit(&p->p_smutex);
   1068 			pgrp->pg_jobc++;
   1069 		} else if (--pgrp->pg_jobc == 0)
   1070 			orphanpg(pgrp);
   1071 	}
   1072 
   1073 	/*
   1074 	 * Check this process' children to see whether they qualify
   1075 	 * their process groups; if so, adjust counts for children's
   1076 	 * process groups.
   1077 	 */
   1078 	LIST_FOREACH(child, &p->p_children, p_sibling) {
   1079 		hispgrp = child->p_pgrp;
   1080 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
   1081 		    !P_ZOMBIE(child)) {
   1082 			if (entering) {
   1083 				mutex_enter(&child->p_smutex);
   1084 				child->p_sflag &= ~PS_ORPHANPG;
   1085 				mutex_exit(&child->p_smutex);
   1086 				hispgrp->pg_jobc++;
   1087 			} else if (--hispgrp->pg_jobc == 0)
   1088 				orphanpg(hispgrp);
   1089 		}
   1090 	}
   1091 }
   1092 
   1093 /*
   1094  * A process group has become orphaned;
   1095  * if there are any stopped processes in the group,
   1096  * hang-up all process in that group.
   1097  *
   1098  * Call with proclist_lock held.
   1099  */
   1100 static void
   1101 orphanpg(struct pgrp *pg)
   1102 {
   1103 	struct proc *p;
   1104 	int doit;
   1105 
   1106 	KASSERT(mutex_owned(&proclist_lock));
   1107 	KASSERT(mutex_owned(&proclist_mutex));
   1108 
   1109 	doit = 0;
   1110 
   1111 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1112 		mutex_enter(&p->p_smutex);
   1113 		if (p->p_stat == SSTOP) {
   1114 			doit = 1;
   1115 			p->p_sflag |= PS_ORPHANPG;
   1116 		}
   1117 		mutex_exit(&p->p_smutex);
   1118 	}
   1119 
   1120 	if (doit) {
   1121 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1122 			psignal(p, SIGHUP);
   1123 			psignal(p, SIGCONT);
   1124 		}
   1125 	}
   1126 }
   1127 
   1128 #ifdef DDB
   1129 #include <ddb/db_output.h>
   1130 void pidtbl_dump(void);
   1131 void
   1132 pidtbl_dump(void)
   1133 {
   1134 	struct pid_table *pt;
   1135 	struct proc *p;
   1136 	struct pgrp *pgrp;
   1137 	int id;
   1138 
   1139 	db_printf("pid table %p size %x, next %x, last %x\n",
   1140 		pid_table, pid_tbl_mask+1,
   1141 		next_free_pt, last_free_pt);
   1142 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1143 		p = pt->pt_proc;
   1144 		if (!P_VALID(p) && !pt->pt_pgrp)
   1145 			continue;
   1146 		db_printf("  id %x: ", id);
   1147 		if (P_VALID(p))
   1148 			db_printf("proc %p id %d (0x%x) %s\n",
   1149 				p, p->p_pid, p->p_pid, p->p_comm);
   1150 		else
   1151 			db_printf("next %x use %x\n",
   1152 				P_NEXT(p) & pid_tbl_mask,
   1153 				P_NEXT(p) & ~pid_tbl_mask);
   1154 		if ((pgrp = pt->pt_pgrp)) {
   1155 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1156 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1157 			    pgrp->pg_session->s_count,
   1158 			    pgrp->pg_session->s_login);
   1159 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1160 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1161 			    pgrp->pg_members.lh_first);
   1162 			for (p = pgrp->pg_members.lh_first; p != 0;
   1163 			    p = p->p_pglist.le_next) {
   1164 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1165 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1166 			}
   1167 		}
   1168 	}
   1169 }
   1170 #endif /* DDB */
   1171 
   1172 #ifdef KSTACK_CHECK_MAGIC
   1173 #include <sys/user.h>
   1174 
   1175 #define	KSTACK_MAGIC	0xdeadbeaf
   1176 
   1177 /* XXX should be per process basis? */
   1178 int kstackleftmin = KSTACK_SIZE;
   1179 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
   1180 					  less than this */
   1181 
   1182 void
   1183 kstack_setup_magic(const struct lwp *l)
   1184 {
   1185 	uint32_t *ip;
   1186 	uint32_t const *end;
   1187 
   1188 	KASSERT(l != NULL);
   1189 	KASSERT(l != &lwp0);
   1190 
   1191 	/*
   1192 	 * fill all the stack with magic number
   1193 	 * so that later modification on it can be detected.
   1194 	 */
   1195 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1196 	end = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1197 	for (; ip < end; ip++) {
   1198 		*ip = KSTACK_MAGIC;
   1199 	}
   1200 }
   1201 
   1202 void
   1203 kstack_check_magic(const struct lwp *l)
   1204 {
   1205 	uint32_t const *ip, *end;
   1206 	int stackleft;
   1207 
   1208 	KASSERT(l != NULL);
   1209 
   1210 	/* don't check proc0 */ /*XXX*/
   1211 	if (l == &lwp0)
   1212 		return;
   1213 
   1214 #ifdef __MACHINE_STACK_GROWS_UP
   1215 	/* stack grows upwards (eg. hppa) */
   1216 	ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1217 	end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1218 	for (ip--; ip >= end; ip--)
   1219 		if (*ip != KSTACK_MAGIC)
   1220 			break;
   1221 
   1222 	stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
   1223 #else /* __MACHINE_STACK_GROWS_UP */
   1224 	/* stack grows downwards (eg. i386) */
   1225 	ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
   1226 	end = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1227 	for (; ip < end; ip++)
   1228 		if (*ip != KSTACK_MAGIC)
   1229 			break;
   1230 
   1231 	stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
   1232 #endif /* __MACHINE_STACK_GROWS_UP */
   1233 
   1234 	if (kstackleftmin > stackleft) {
   1235 		kstackleftmin = stackleft;
   1236 		if (stackleft < kstackleftthres)
   1237 			printf("warning: kernel stack left %d bytes"
   1238 			    "(pid %u:lid %u)\n", stackleft,
   1239 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1240 	}
   1241 
   1242 	if (stackleft <= 0) {
   1243 		panic("magic on the top of kernel stack changed for "
   1244 		    "pid %u, lid %u: maybe kernel stack overflow",
   1245 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1246 	}
   1247 }
   1248 #endif /* KSTACK_CHECK_MAGIC */
   1249 
   1250 /*
   1251  * XXXSMP this is bust, it grabs a read lock and then messes about
   1252  * with allproc.
   1253  */
   1254 int
   1255 proclist_foreach_call(struct proclist *list,
   1256     int (*callback)(struct proc *, void *arg), void *arg)
   1257 {
   1258 	struct proc marker;
   1259 	struct proc *p;
   1260 	struct lwp * const l = curlwp;
   1261 	int ret = 0;
   1262 
   1263 	marker.p_flag = PK_MARKER;
   1264 	PHOLD(l);
   1265 	mutex_enter(&proclist_lock);
   1266 	for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
   1267 		if (p->p_flag & PK_MARKER) {
   1268 			p = LIST_NEXT(p, p_list);
   1269 			continue;
   1270 		}
   1271 		LIST_INSERT_AFTER(p, &marker, p_list);
   1272 		ret = (*callback)(p, arg);
   1273 		KASSERT(mutex_owned(&proclist_lock));
   1274 		p = LIST_NEXT(&marker, p_list);
   1275 		LIST_REMOVE(&marker, p_list);
   1276 	}
   1277 	mutex_exit(&proclist_lock);
   1278 	PRELE(l);
   1279 
   1280 	return ret;
   1281 }
   1282 
   1283 int
   1284 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
   1285 {
   1286 
   1287 	/* XXXCDC: how should locking work here? */
   1288 
   1289 	/* curproc exception is for coredump. */
   1290 
   1291 	if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
   1292 	    (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
   1293 		return EFAULT;
   1294 	}
   1295 
   1296 	uvmspace_addref(p->p_vmspace);
   1297 	*vm = p->p_vmspace;
   1298 
   1299 	return 0;
   1300 }
   1301 
   1302 /*
   1303  * Acquire a write lock on the process credential.
   1304  */
   1305 void
   1306 proc_crmod_enter(void)
   1307 {
   1308 	struct lwp *l = curlwp;
   1309 	struct proc *p = l->l_proc;
   1310 	struct plimit *lim;
   1311 	kauth_cred_t oc;
   1312 	char *cn;
   1313 
   1314 	mutex_enter(&p->p_mutex);
   1315 
   1316 	/* Ensure the LWP cached credentials are up to date. */
   1317 	if ((oc = l->l_cred) != p->p_cred) {
   1318 		kauth_cred_hold(p->p_cred);
   1319 		l->l_cred = p->p_cred;
   1320 		kauth_cred_free(oc);
   1321 	}
   1322 
   1323 	/* Reset what needs to be reset in plimit. */
   1324 	lim = p->p_limit;
   1325 	if (lim->pl_corename != defcorename) {
   1326 		if (lim->p_refcnt > 1 &&
   1327 		    (lim->p_lflags & PL_SHAREMOD) == 0) {
   1328 			p->p_limit = limcopy(p);
   1329 			limfree(lim);
   1330 			lim = p->p_limit;
   1331 		}
   1332 		simple_lock(&lim->p_slock);
   1333 		cn = lim->pl_corename;
   1334 		lim->pl_corename = defcorename;
   1335 		simple_unlock(&lim->p_slock);
   1336 		if (cn != defcorename)
   1337 			free(cn, M_TEMP);
   1338 	}
   1339 }
   1340 
   1341 /*
   1342  * Set in a new process credential, and drop the write lock.  The credential
   1343  * must have a reference already.  Optionally, free a no-longer required
   1344  * credential.  The scheduler also needs to inspect p_cred, so we also
   1345  * briefly acquire the sched state mutex.
   1346  */
   1347 void
   1348 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
   1349 {
   1350 	struct lwp *l = curlwp;
   1351 	struct proc *p = l->l_proc;
   1352 	kauth_cred_t oc;
   1353 
   1354 	/* Is there a new credential to set in? */
   1355 	if (scred != NULL) {
   1356 		mutex_enter(&p->p_smutex);
   1357 		p->p_cred = scred;
   1358 		mutex_exit(&p->p_smutex);
   1359 
   1360 		/* Ensure the LWP cached credentials are up to date. */
   1361 		if ((oc = l->l_cred) != scred) {
   1362 			kauth_cred_hold(scred);
   1363 			l->l_cred = scred;
   1364 		}
   1365 	} else
   1366 		oc = NULL;	/* XXXgcc */
   1367 
   1368 	if (sugid) {
   1369 		/*
   1370 		 * Mark process as having changed credentials, stops
   1371 		 * tracing etc.
   1372 		 */
   1373 		p->p_flag |= PK_SUGID;
   1374 	}
   1375 
   1376 	mutex_exit(&p->p_mutex);
   1377 
   1378 	/* If there is a credential to be released, free it now. */
   1379 	if (fcred != NULL) {
   1380 		KASSERT(scred != NULL);
   1381 		kauth_cred_free(fcred);
   1382 		if (oc != scred)
   1383 			kauth_cred_free(oc);
   1384 	}
   1385 }
   1386 
   1387 /*
   1388  * Acquire a reference on a process, to prevent it from exiting or execing.
   1389  */
   1390 int
   1391 proc_addref(struct proc *p)
   1392 {
   1393 
   1394 	KASSERT(mutex_owned(&p->p_mutex));
   1395 
   1396 	if (p->p_refcnt <= 0)
   1397 		return EAGAIN;
   1398 	p->p_refcnt++;
   1399 
   1400 	return 0;
   1401 }
   1402 
   1403 /*
   1404  * Release a reference on a process.
   1405  */
   1406 void
   1407 proc_delref(struct proc *p)
   1408 {
   1409 
   1410 	KASSERT(mutex_owned(&p->p_mutex));
   1411 
   1412 	if (p->p_refcnt < 0) {
   1413 		if (++p->p_refcnt == 0)
   1414 			cv_broadcast(&p->p_refcv);
   1415 	} else {
   1416 		p->p_refcnt--;
   1417 		KASSERT(p->p_refcnt != 0);
   1418 	}
   1419 }
   1420 
   1421 /*
   1422  * Wait for all references on the process to drain, and prevent new
   1423  * references from being acquired.
   1424  */
   1425 void
   1426 proc_drainrefs(struct proc *p)
   1427 {
   1428 
   1429 	KASSERT(mutex_owned(&p->p_mutex));
   1430 	KASSERT(p->p_refcnt > 0);
   1431 
   1432 	/*
   1433 	 * The process itself holds the last reference.  Once it's released,
   1434 	 * no new references will be granted.  If we have already locked out
   1435 	 * new references (refcnt <= 0), potentially due to a failed exec,
   1436 	 * there is nothing more to do.
   1437 	 */
   1438 	p->p_refcnt = 1 - p->p_refcnt;
   1439 	while (p->p_refcnt != 0)
   1440 		cv_wait(&p->p_refcv, &p->p_mutex);
   1441 }
   1442 
   1443 /*
   1444  * proc_specific_key_create --
   1445  *	Create a key for subsystem proc-specific data.
   1446  */
   1447 int
   1448 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1449 {
   1450 
   1451 	return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
   1452 }
   1453 
   1454 /*
   1455  * proc_specific_key_delete --
   1456  *	Delete a key for subsystem proc-specific data.
   1457  */
   1458 void
   1459 proc_specific_key_delete(specificdata_key_t key)
   1460 {
   1461 
   1462 	specificdata_key_delete(proc_specificdata_domain, key);
   1463 }
   1464 
   1465 /*
   1466  * proc_initspecific --
   1467  *	Initialize a proc's specificdata container.
   1468  */
   1469 void
   1470 proc_initspecific(struct proc *p)
   1471 {
   1472 	int error;
   1473 
   1474 	error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
   1475 	KASSERT(error == 0);
   1476 }
   1477 
   1478 /*
   1479  * proc_finispecific --
   1480  *	Finalize a proc's specificdata container.
   1481  */
   1482 void
   1483 proc_finispecific(struct proc *p)
   1484 {
   1485 
   1486 	specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
   1487 }
   1488 
   1489 /*
   1490  * proc_getspecific --
   1491  *	Return proc-specific data corresponding to the specified key.
   1492  */
   1493 void *
   1494 proc_getspecific(struct proc *p, specificdata_key_t key)
   1495 {
   1496 
   1497 	return (specificdata_getspecific(proc_specificdata_domain,
   1498 					 &p->p_specdataref, key));
   1499 }
   1500 
   1501 /*
   1502  * proc_setspecific --
   1503  *	Set proc-specific data corresponding to the specified key.
   1504  */
   1505 void
   1506 proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
   1507 {
   1508 
   1509 	specificdata_setspecific(proc_specificdata_domain,
   1510 				 &p->p_specdataref, key, data);
   1511 }
   1512