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