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