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