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