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