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