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