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