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