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