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