1 /* $NetBSD: kern_proc.c,v 1.129 2008/01/02 11:48:51 ad 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.129 2008/01/02 11:48:51 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 103 #include <uvm/uvm.h> 104 #include <uvm/uvm_extern.h> 105 106 /* 107 * Other process lists 108 */ 109 110 struct proclist allproc; 111 struct proclist zombproc; /* resources have been freed */ 112 113 /* 114 * There are two locks on global process state. 115 * 116 * 1. proclist_lock is an adaptive mutex and is used when modifying 117 * or examining process state from a process context. It protects 118 * the internal tables, all of the process lists, and a number of 119 * members of struct proc. 120 * 121 * 2. proclist_mutex is used when allproc must be traversed from an 122 * interrupt context, or when changing the state of processes. The 123 * proclist_lock should always be used in preference. In some cases, 124 * both locks need to be held. 125 * 126 * proclist_lock proclist_mutex structure 127 * --------------- --------------- ----------------- 128 * x zombproc 129 * x x pid_table 130 * x proc::p_pptr 131 * x proc::p_sibling 132 * x proc::p_children 133 * x alllwp 134 * x x allproc 135 * x x proc::p_pgrp 136 * x x proc::p_pglist 137 * x x proc::p_session 138 * x x proc::p_list 139 * x lwp::l_list 140 * 141 * The lock order for processes and LWPs is approximately as following: 142 * 143 * kernel_lock 144 * -> proclist_lock 145 * -> proc::p_mutex 146 * -> proclist_mutex 147 * -> proc::p_smutex 148 * -> proc::p_stmutex 149 * 150 * XXX p_smutex can be run at IPL_VM once audio drivers on the x86 151 * platform are made MP safe. Currently it blocks interrupts at 152 * IPL_SCHED and below. 153 * 154 * XXX The two process locks (p_smutex + p_mutex), and the two global 155 * state locks (proclist_lock + proclist_mutex) should be merged 156 * together. However, to do so requires interrupts that interrupts 157 * be run with LWP context. 158 */ 159 kmutex_t proclist_lock; 160 kmutex_t proclist_mutex; 161 162 /* 163 * pid to proc lookup is done by indexing the pid_table array. 164 * Since pid numbers are only allocated when an empty slot 165 * has been found, there is no need to search any lists ever. 166 * (an orphaned pgrp will lock the slot, a session will lock 167 * the pgrp with the same number.) 168 * If the table is too small it is reallocated with twice the 169 * previous size and the entries 'unzipped' into the two halves. 170 * A linked list of free entries is passed through the pt_proc 171 * field of 'free' items - set odd to be an invalid ptr. 172 */ 173 174 struct pid_table { 175 struct proc *pt_proc; 176 struct pgrp *pt_pgrp; 177 }; 178 #if 1 /* strongly typed cast - should be a noop */ 179 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; } 180 #else 181 #define p2u(p) ((uint)p) 182 #endif 183 #define P_VALID(p) (!(p2u(p) & 1)) 184 #define P_NEXT(p) (p2u(p) >> 1) 185 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1)) 186 187 #define INITIAL_PID_TABLE_SIZE (1 << 5) 188 static struct pid_table *pid_table; 189 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1; 190 static uint pid_alloc_lim; /* max we allocate before growing table */ 191 static uint pid_alloc_cnt; /* number of allocated pids */ 192 193 /* links through free slots - never empty! */ 194 static uint next_free_pt, last_free_pt; 195 static pid_t pid_max = PID_MAX; /* largest value we allocate */ 196 197 /* Components of the first process -- never freed. */ 198 199 extern const struct emul emul_netbsd; /* defined in kern_exec.c */ 200 201 struct session session0 = { 202 .s_count = 1, 203 .s_sid = 0, 204 }; 205 struct pgrp pgrp0 = { 206 .pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members), 207 .pg_session = &session0, 208 }; 209 struct filedesc0 filedesc0; 210 struct cwdinfo cwdi0 = { 211 .cwdi_cmask = CMASK, /* see cmask below */ 212 .cwdi_refcnt = 1, 213 }; 214 struct plimit limit0 = { 215 .pl_corename = defcorename, 216 .pl_refcnt = 1, 217 .pl_rlimit = { 218 [0 ... __arraycount(limit0.pl_rlimit) - 1] = { 219 .rlim_cur = RLIM_INFINITY, 220 .rlim_max = RLIM_INFINITY, 221 }, 222 }, 223 }; 224 struct pstats pstat0; 225 struct vmspace vmspace0; 226 struct sigacts sigacts0; 227 struct turnstile turnstile0; 228 struct proc proc0 = { 229 .p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps), 230 .p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters), 231 .p_nlwps = 1, 232 .p_nrlwps = 1, 233 .p_nlwpid = 1, /* must match lwp0.l_lid */ 234 .p_pgrp = &pgrp0, 235 .p_comm = "system", 236 /* 237 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8) 238 * when they exit. init(8) can easily wait them out for us. 239 */ 240 .p_flag = PK_SYSTEM | PK_NOCLDWAIT, 241 .p_stat = SACTIVE, 242 .p_nice = NZERO, 243 .p_emul = &emul_netbsd, 244 .p_cwdi = &cwdi0, 245 .p_limit = &limit0, 246 .p_fd = &filedesc0.fd_fd, 247 .p_vmspace = &vmspace0, 248 .p_stats = &pstat0, 249 .p_sigacts = &sigacts0, 250 }; 251 struct lwp lwp0 __aligned(MIN_LWP_ALIGNMENT) = { 252 #ifdef LWP0_CPU_INFO 253 .l_cpu = LWP0_CPU_INFO, 254 #endif 255 .l_proc = &proc0, 256 .l_lid = 1, 257 .l_flag = LW_INMEM | LW_SYSTEM, 258 .l_stat = LSONPROC, 259 .l_ts = &turnstile0, 260 .l_syncobj = &sched_syncobj, 261 .l_refcnt = 1, 262 .l_priority = PRI_USER + NPRI_USER - 1, 263 .l_inheritedprio = -1, 264 .l_class = SCHED_OTHER, 265 .l_pi_lenders = SLIST_HEAD_INITIALIZER(&lwp0.l_pi_lenders), 266 .l_name = __UNCONST("swapper"), 267 }; 268 kauth_cred_t cred0; 269 270 extern struct user *proc0paddr; 271 272 int nofile = NOFILE; 273 int maxuprc = MAXUPRC; 274 int cmask = CMASK; 275 276 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data"); 277 MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 278 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 279 280 /* 281 * The process list descriptors, used during pid allocation and 282 * by sysctl. No locking on this data structure is needed since 283 * it is completely static. 284 */ 285 const struct proclist_desc proclists[] = { 286 { &allproc }, 287 { &zombproc }, 288 { NULL }, 289 }; 290 291 static void orphanpg(struct pgrp *); 292 static void pg_delete(pid_t); 293 294 static specificdata_domain_t proc_specificdata_domain; 295 296 static pool_cache_t proc_cache; 297 static pool_cache_t pgrp_cache; 298 static pool_cache_t session_cache; 299 300 /* 301 * Initialize global process hashing structures. 302 */ 303 void 304 procinit(void) 305 { 306 const struct proclist_desc *pd; 307 int i; 308 #define LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1)) 309 310 for (pd = proclists; pd->pd_list != NULL; pd++) 311 LIST_INIT(pd->pd_list); 312 313 mutex_init(&proclist_lock, MUTEX_DEFAULT, IPL_NONE); 314 mutex_init(&proclist_mutex, MUTEX_DEFAULT, IPL_SCHED); 315 316 pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table, 317 M_PROC, M_WAITOK); 318 /* Set free list running through table... 319 Preset 'use count' above PID_MAX so we allocate pid 1 next. */ 320 for (i = 0; i <= pid_tbl_mask; i++) { 321 pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1); 322 pid_table[i].pt_pgrp = 0; 323 } 324 /* slot 0 is just grabbed */ 325 next_free_pt = 1; 326 /* Need to fix last entry. */ 327 last_free_pt = pid_tbl_mask; 328 pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY); 329 /* point at which we grow table - to avoid reusing pids too often */ 330 pid_alloc_lim = pid_tbl_mask - 1; 331 #undef LINK_EMPTY 332 333 uihashtbl = 334 hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash); 335 336 proc_specificdata_domain = specificdata_domain_create(); 337 KASSERT(proc_specificdata_domain != NULL); 338 339 proc_cache = pool_cache_init(sizeof(struct proc), 0, 0, 0, 340 "procpl", NULL, IPL_NONE, NULL, NULL, NULL); 341 pgrp_cache = pool_cache_init(sizeof(struct pgrp), 0, 0, 0, 342 "pgrppl", NULL, IPL_NONE, NULL, NULL, NULL); 343 session_cache = pool_cache_init(sizeof(struct session), 0, 0, 0, 344 "sessionpl", NULL, IPL_NONE, NULL, NULL, NULL); 345 } 346 347 /* 348 * Initialize process 0. 349 */ 350 void 351 proc0_init(void) 352 { 353 struct proc *p; 354 struct pgrp *pg; 355 struct session *sess; 356 struct lwp *l; 357 rlim_t lim; 358 359 p = &proc0; 360 pg = &pgrp0; 361 sess = &session0; 362 l = &lwp0; 363 364 KASSERT(l->l_lid == p->p_nlwpid); 365 366 mutex_init(&p->p_smutex, MUTEX_DEFAULT, IPL_SCHED); 367 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH); 368 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE); 369 mutex_init(&p->p_mutex, MUTEX_DEFAULT, IPL_NONE); 370 mutex_init(&l->l_swaplock, MUTEX_DEFAULT, IPL_NONE); 371 372 rw_init(&p->p_reflock); 373 cv_init(&p->p_waitcv, "wait"); 374 cv_init(&p->p_lwpcv, "lwpwait"); 375 376 LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling); 377 378 pid_table[0].pt_proc = p; 379 LIST_INSERT_HEAD(&allproc, p, p_list); 380 LIST_INSERT_HEAD(&alllwp, l, l_list); 381 382 pid_table[0].pt_pgrp = pg; 383 LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist); 384 385 #ifdef __HAVE_SYSCALL_INTERN 386 (*p->p_emul->e_syscall_intern)(p); 387 #endif 388 389 callout_init(&l->l_timeout_ch, CALLOUT_MPSAFE); 390 callout_setfunc(&l->l_timeout_ch, sleepq_timeout, l); 391 cv_init(&l->l_sigcv, "sigwait"); 392 393 /* Create credentials. */ 394 cred0 = kauth_cred_alloc(); 395 p->p_cred = cred0; 396 kauth_cred_hold(cred0); 397 l->l_cred = cred0; 398 399 /* Create the CWD info. */ 400 rw_init(&cwdi0.cwdi_lock); 401 402 /* Create the limits structures. */ 403 mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE); 404 405 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 406 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 407 maxfiles < nofile ? maxfiles : nofile; 408 409 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 410 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 411 maxproc < maxuprc ? maxproc : maxuprc; 412 413 lim = ptoa(uvmexp.free); 414 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim; 415 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim; 416 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3; 417 418 /* Configure virtual memory system, set vm rlimits. */ 419 uvm_init_limits(p); 420 421 /* Initialize file descriptor table for proc0. */ 422 fdinit1(&filedesc0); 423 424 /* 425 * Initialize proc0's vmspace, which uses the kernel pmap. 426 * All kernel processes (which never have user space mappings) 427 * share proc0's vmspace, and thus, the kernel pmap. 428 */ 429 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 430 trunc_page(VM_MAX_ADDRESS)); 431 432 l->l_addr = proc0paddr; /* XXX */ 433 434 /* Initialize signal state for proc0. XXX IPL_SCHED */ 435 mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED); 436 siginit(p); 437 438 proc_initspecific(p); 439 lwp_initspecific(l); 440 441 SYSCALL_TIME_LWP_INIT(l); 442 } 443 444 /* 445 * Check that the specified process group is in the session of the 446 * specified process. 447 * Treats -ve ids as process ids. 448 * Used to validate TIOCSPGRP requests. 449 */ 450 int 451 pgid_in_session(struct proc *p, pid_t pg_id) 452 { 453 struct pgrp *pgrp; 454 struct session *session; 455 int error; 456 457 mutex_enter(&proclist_lock); 458 if (pg_id < 0) { 459 struct proc *p1 = p_find(-pg_id, PFIND_LOCKED | PFIND_UNLOCK_FAIL); 460 if (p1 == NULL) 461 return EINVAL; 462 pgrp = p1->p_pgrp; 463 } else { 464 pgrp = pg_find(pg_id, PFIND_LOCKED | PFIND_UNLOCK_FAIL); 465 if (pgrp == NULL) 466 return EINVAL; 467 } 468 session = pgrp->pg_session; 469 if (session != p->p_pgrp->pg_session) 470 error = EPERM; 471 else 472 error = 0; 473 mutex_exit(&proclist_lock); 474 475 return error; 476 } 477 478 /* 479 * Is p an inferior of q? 480 * 481 * Call with the proclist_lock held. 482 */ 483 int 484 inferior(struct proc *p, struct proc *q) 485 { 486 487 for (; p != q; p = p->p_pptr) 488 if (p->p_pid == 0) 489 return 0; 490 return 1; 491 } 492 493 /* 494 * Locate a process by number 495 */ 496 struct proc * 497 p_find(pid_t pid, uint flags) 498 { 499 struct proc *p; 500 char stat; 501 502 if (!(flags & PFIND_LOCKED)) 503 mutex_enter(&proclist_lock); 504 505 p = pid_table[pid & pid_tbl_mask].pt_proc; 506 507 /* Only allow live processes to be found by pid. */ 508 /* XXXSMP p_stat */ 509 if (P_VALID(p) && p->p_pid == pid && ((stat = p->p_stat) == SACTIVE || 510 stat == SSTOP || ((flags & PFIND_ZOMBIE) && 511 (stat == SZOMB || stat == SDEAD || stat == SDYING)))) { 512 if (flags & PFIND_UNLOCK_OK) 513 mutex_exit(&proclist_lock); 514 return p; 515 } 516 if (flags & PFIND_UNLOCK_FAIL) 517 mutex_exit(&proclist_lock); 518 return NULL; 519 } 520 521 522 /* 523 * Locate a process group by number 524 */ 525 struct pgrp * 526 pg_find(pid_t pgid, uint flags) 527 { 528 struct pgrp *pg; 529 530 if (!(flags & PFIND_LOCKED)) 531 mutex_enter(&proclist_lock); 532 pg = pid_table[pgid & pid_tbl_mask].pt_pgrp; 533 /* 534 * Can't look up a pgrp that only exists because the session 535 * hasn't died yet (traditional) 536 */ 537 if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) { 538 if (flags & PFIND_UNLOCK_FAIL) 539 mutex_exit(&proclist_lock); 540 return NULL; 541 } 542 543 if (flags & PFIND_UNLOCK_OK) 544 mutex_exit(&proclist_lock); 545 return pg; 546 } 547 548 static void 549 expand_pid_table(void) 550 { 551 uint pt_size = pid_tbl_mask + 1; 552 struct pid_table *n_pt, *new_pt; 553 struct proc *proc; 554 struct pgrp *pgrp; 555 int i; 556 pid_t pid; 557 558 new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK); 559 560 mutex_enter(&proclist_lock); 561 if (pt_size != pid_tbl_mask + 1) { 562 /* Another process beat us to it... */ 563 mutex_exit(&proclist_lock); 564 FREE(new_pt, M_PROC); 565 return; 566 } 567 568 /* 569 * Copy entries from old table into new one. 570 * If 'pid' is 'odd' we need to place in the upper half, 571 * even pid's to the lower half. 572 * Free items stay in the low half so we don't have to 573 * fixup the reference to them. 574 * We stuff free items on the front of the freelist 575 * because we can't write to unmodified entries. 576 * Processing the table backwards maintains a semblance 577 * of issueing pid numbers that increase with time. 578 */ 579 i = pt_size - 1; 580 n_pt = new_pt + i; 581 for (; ; i--, n_pt--) { 582 proc = pid_table[i].pt_proc; 583 pgrp = pid_table[i].pt_pgrp; 584 if (!P_VALID(proc)) { 585 /* Up 'use count' so that link is valid */ 586 pid = (P_NEXT(proc) + pt_size) & ~pt_size; 587 proc = P_FREE(pid); 588 if (pgrp) 589 pid = pgrp->pg_id; 590 } else 591 pid = proc->p_pid; 592 593 /* Save entry in appropriate half of table */ 594 n_pt[pid & pt_size].pt_proc = proc; 595 n_pt[pid & pt_size].pt_pgrp = pgrp; 596 597 /* Put other piece on start of free list */ 598 pid = (pid ^ pt_size) & ~pid_tbl_mask; 599 n_pt[pid & pt_size].pt_proc = 600 P_FREE((pid & ~pt_size) | next_free_pt); 601 n_pt[pid & pt_size].pt_pgrp = 0; 602 next_free_pt = i | (pid & pt_size); 603 if (i == 0) 604 break; 605 } 606 607 /* Switch tables */ 608 mutex_enter(&proclist_mutex); 609 n_pt = pid_table; 610 pid_table = new_pt; 611 mutex_exit(&proclist_mutex); 612 pid_tbl_mask = pt_size * 2 - 1; 613 614 /* 615 * pid_max starts as PID_MAX (= 30000), once we have 16384 616 * allocated pids we need it to be larger! 617 */ 618 if (pid_tbl_mask > PID_MAX) { 619 pid_max = pid_tbl_mask * 2 + 1; 620 pid_alloc_lim |= pid_alloc_lim << 1; 621 } else 622 pid_alloc_lim <<= 1; /* doubles number of free slots... */ 623 624 mutex_exit(&proclist_lock); 625 FREE(n_pt, M_PROC); 626 } 627 628 struct proc * 629 proc_alloc(void) 630 { 631 struct proc *p; 632 int nxt; 633 pid_t pid; 634 struct pid_table *pt; 635 636 p = pool_cache_get(proc_cache, PR_WAITOK); 637 p->p_stat = SIDL; /* protect against others */ 638 639 proc_initspecific(p); 640 /* allocate next free pid */ 641 642 for (;;expand_pid_table()) { 643 if (__predict_false(pid_alloc_cnt >= pid_alloc_lim)) 644 /* ensure pids cycle through 2000+ values */ 645 continue; 646 mutex_enter(&proclist_lock); 647 pt = &pid_table[next_free_pt]; 648 #ifdef DIAGNOSTIC 649 if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp)) 650 panic("proc_alloc: slot busy"); 651 #endif 652 nxt = P_NEXT(pt->pt_proc); 653 if (nxt & pid_tbl_mask) 654 break; 655 /* Table full - expand (NB last entry not used....) */ 656 mutex_exit(&proclist_lock); 657 } 658 659 /* pid is 'saved use count' + 'size' + entry */ 660 pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt; 661 if ((uint)pid > (uint)pid_max) 662 pid &= pid_tbl_mask; 663 p->p_pid = pid; 664 next_free_pt = nxt & pid_tbl_mask; 665 666 /* Grab table slot */ 667 mutex_enter(&proclist_mutex); 668 pt->pt_proc = p; 669 mutex_exit(&proclist_mutex); 670 pid_alloc_cnt++; 671 672 mutex_exit(&proclist_lock); 673 674 return p; 675 } 676 677 /* 678 * Free a process id - called from proc_free (in kern_exit.c) 679 * 680 * Called with the proclist_lock held. 681 */ 682 void 683 proc_free_pid(struct proc *p) 684 { 685 pid_t pid = p->p_pid; 686 struct pid_table *pt; 687 688 KASSERT(mutex_owned(&proclist_lock)); 689 690 pt = &pid_table[pid & pid_tbl_mask]; 691 #ifdef DIAGNOSTIC 692 if (__predict_false(pt->pt_proc != p)) 693 panic("proc_free: pid_table mismatch, pid %x, proc %p", 694 pid, p); 695 #endif 696 mutex_enter(&proclist_mutex); 697 /* save pid use count in slot */ 698 pt->pt_proc = P_FREE(pid & ~pid_tbl_mask); 699 700 if (pt->pt_pgrp == NULL) { 701 /* link last freed entry onto ours */ 702 pid &= pid_tbl_mask; 703 pt = &pid_table[last_free_pt]; 704 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid); 705 last_free_pt = pid; 706 pid_alloc_cnt--; 707 } 708 mutex_exit(&proclist_mutex); 709 710 atomic_dec_uint(&nprocs); 711 } 712 713 void 714 proc_free_mem(struct proc *p) 715 { 716 717 pool_cache_put(proc_cache, p); 718 } 719 720 /* 721 * Move p to a new or existing process group (and session) 722 * 723 * If we are creating a new pgrp, the pgid should equal 724 * the calling process' pid. 725 * If is only valid to enter a process group that is in the session 726 * of the process. 727 * Also mksess should only be set if we are creating a process group 728 * 729 * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the 730 * SYSV setpgrp support for hpux. 731 */ 732 int 733 enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, int mksess) 734 { 735 struct pgrp *new_pgrp, *pgrp; 736 struct session *sess; 737 struct proc *p; 738 int rval; 739 pid_t pg_id = NO_PGID; 740 741 if (mksess) 742 sess = pool_cache_get(session_cache, PR_WAITOK); 743 else 744 sess = NULL; 745 746 /* Allocate data areas we might need before doing any validity checks */ 747 mutex_enter(&proclist_lock); /* Because pid_table might change */ 748 if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) { 749 mutex_exit(&proclist_lock); 750 new_pgrp = pool_cache_get(pgrp_cache, PR_WAITOK); 751 mutex_enter(&proclist_lock); 752 } else 753 new_pgrp = NULL; 754 rval = EPERM; /* most common error (to save typing) */ 755 756 /* Check pgrp exists or can be created */ 757 pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp; 758 if (pgrp != NULL && pgrp->pg_id != pgid) 759 goto done; 760 761 /* Can only set another process under restricted circumstances. */ 762 if (pid != curp->p_pid) { 763 /* must exist and be one of our children... */ 764 if ((p = p_find(pid, PFIND_LOCKED)) == NULL || 765 !inferior(p, curp)) { 766 rval = ESRCH; 767 goto done; 768 } 769 /* ... in the same session... */ 770 if (sess != NULL || p->p_session != curp->p_session) 771 goto done; 772 /* ... existing pgid must be in same session ... */ 773 if (pgrp != NULL && pgrp->pg_session != p->p_session) 774 goto done; 775 /* ... and not done an exec. */ 776 if (p->p_flag & PK_EXEC) { 777 rval = EACCES; 778 goto done; 779 } 780 } else { 781 /* ... setsid() cannot re-enter a pgrp */ 782 if (mksess && (curp->p_pgid == curp->p_pid || 783 pg_find(curp->p_pid, PFIND_LOCKED))) 784 goto done; 785 p = curp; 786 } 787 788 /* Changing the process group/session of a session 789 leader is definitely off limits. */ 790 if (SESS_LEADER(p)) { 791 if (sess == NULL && p->p_pgrp == pgrp) 792 /* unless it's a definite noop */ 793 rval = 0; 794 goto done; 795 } 796 797 /* Can only create a process group with id of process */ 798 if (pgrp == NULL && pgid != pid) 799 goto done; 800 801 /* Can only create a session if creating pgrp */ 802 if (sess != NULL && pgrp != NULL) 803 goto done; 804 805 /* Check we allocated memory for a pgrp... */ 806 if (pgrp == NULL && new_pgrp == NULL) 807 goto done; 808 809 /* Don't attach to 'zombie' pgrp */ 810 if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members)) 811 goto done; 812 813 /* Expect to succeed now */ 814 rval = 0; 815 816 if (pgrp == p->p_pgrp) 817 /* nothing to do */ 818 goto done; 819 820 /* Ok all setup, link up required structures */ 821 822 if (pgrp == NULL) { 823 pgrp = new_pgrp; 824 new_pgrp = 0; 825 if (sess != NULL) { 826 sess->s_sid = p->p_pid; 827 sess->s_leader = p; 828 sess->s_count = 1; 829 sess->s_ttyvp = NULL; 830 sess->s_ttyp = NULL; 831 sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET; 832 memcpy(sess->s_login, p->p_session->s_login, 833 sizeof(sess->s_login)); 834 p->p_lflag &= ~PL_CONTROLT; 835 } else { 836 sess = p->p_pgrp->pg_session; 837 SESSHOLD(sess); 838 } 839 pgrp->pg_session = sess; 840 sess = 0; 841 842 pgrp->pg_id = pgid; 843 LIST_INIT(&pgrp->pg_members); 844 #ifdef DIAGNOSTIC 845 if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp)) 846 panic("enterpgrp: pgrp table slot in use"); 847 if (__predict_false(mksess && p != curp)) 848 panic("enterpgrp: mksession and p != curproc"); 849 #endif 850 mutex_enter(&proclist_mutex); 851 pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp; 852 pgrp->pg_jobc = 0; 853 } else 854 mutex_enter(&proclist_mutex); 855 856 /* Interlock with tty subsystem. */ 857 mutex_spin_enter(&tty_lock); 858 859 /* 860 * Adjust eligibility of affected pgrps to participate in job control. 861 * Increment eligibility counts before decrementing, otherwise we 862 * could reach 0 spuriously during the first call. 863 */ 864 fixjobc(p, pgrp, 1); 865 fixjobc(p, p->p_pgrp, 0); 866 867 /* Move process to requested group. */ 868 LIST_REMOVE(p, p_pglist); 869 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 870 /* defer delete until we've dumped the lock */ 871 pg_id = p->p_pgrp->pg_id; 872 p->p_pgrp = pgrp; 873 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 874 875 /* Done with the swap; we can release the tty mutex. */ 876 mutex_spin_exit(&tty_lock); 877 878 mutex_exit(&proclist_mutex); 879 880 done: 881 if (pg_id != NO_PGID) 882 pg_delete(pg_id); 883 mutex_exit(&proclist_lock); 884 if (sess != NULL) 885 pool_cache_put(session_cache, sess); 886 if (new_pgrp != NULL) 887 pool_cache_put(pgrp_cache, new_pgrp); 888 #ifdef DEBUG_PGRP 889 if (__predict_false(rval)) 890 printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n", 891 pid, pgid, mksess, curp->p_pid, rval); 892 #endif 893 return rval; 894 } 895 896 /* 897 * Remove a process from its process group. Must be called with the 898 * proclist_lock held. 899 */ 900 void 901 leavepgrp(struct proc *p) 902 { 903 struct pgrp *pgrp; 904 905 KASSERT(mutex_owned(&proclist_lock)); 906 907 mutex_enter(&proclist_mutex); 908 mutex_spin_enter(&tty_lock); 909 pgrp = p->p_pgrp; 910 LIST_REMOVE(p, p_pglist); 911 p->p_pgrp = NULL; 912 mutex_spin_exit(&tty_lock); 913 mutex_exit(&proclist_mutex); 914 915 if (LIST_EMPTY(&pgrp->pg_members)) 916 pg_delete(pgrp->pg_id); 917 } 918 919 /* 920 * Free a process group. Must be called with the proclist_lock held. 921 */ 922 static void 923 pg_free(pid_t pg_id) 924 { 925 struct pgrp *pgrp; 926 struct pid_table *pt; 927 928 KASSERT(mutex_owned(&proclist_lock)); 929 930 pt = &pid_table[pg_id & pid_tbl_mask]; 931 pgrp = pt->pt_pgrp; 932 #ifdef DIAGNOSTIC 933 if (__predict_false(!pgrp || pgrp->pg_id != pg_id 934 || !LIST_EMPTY(&pgrp->pg_members))) 935 panic("pg_free: process group absent or has members"); 936 #endif 937 pt->pt_pgrp = 0; 938 939 if (!P_VALID(pt->pt_proc)) { 940 /* orphaned pgrp, put slot onto free list */ 941 #ifdef DIAGNOSTIC 942 if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask)) 943 panic("pg_free: process slot on free list"); 944 #endif 945 mutex_enter(&proclist_mutex); 946 pg_id &= pid_tbl_mask; 947 pt = &pid_table[last_free_pt]; 948 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id); 949 mutex_exit(&proclist_mutex); 950 last_free_pt = pg_id; 951 pid_alloc_cnt--; 952 } 953 pool_cache_put(pgrp_cache, pgrp); 954 } 955 956 /* 957 * Delete a process group. Must be called with the proclist_lock held. 958 */ 959 static void 960 pg_delete(pid_t pg_id) 961 { 962 struct pgrp *pgrp; 963 struct tty *ttyp; 964 struct session *ss; 965 int is_pgrp_leader; 966 967 KASSERT(mutex_owned(&proclist_lock)); 968 969 pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp; 970 if (pgrp == NULL || pgrp->pg_id != pg_id || 971 !LIST_EMPTY(&pgrp->pg_members)) 972 return; 973 974 ss = pgrp->pg_session; 975 976 /* Remove reference (if any) from tty to this process group */ 977 mutex_spin_enter(&tty_lock); 978 ttyp = ss->s_ttyp; 979 if (ttyp != NULL && ttyp->t_pgrp == pgrp) { 980 ttyp->t_pgrp = NULL; 981 #ifdef DIAGNOSTIC 982 if (ttyp->t_session != ss) 983 panic("pg_delete: wrong session on terminal"); 984 #endif 985 } 986 mutex_spin_exit(&tty_lock); 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 held. 1004 */ 1005 void 1006 sessdelete(struct session *ss) 1007 { 1008 1009 KASSERT(mutex_owned(&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_cache_put(session_cache, 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 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 KASSERT(mutex_owned(&proclist_lock)); 1041 KASSERT(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 held. 1084 */ 1085 static void 1086 orphanpg(struct pgrp *pg) 1087 { 1088 struct proc *p; 1089 int doit; 1090 1091 KASSERT(mutex_owned(&proclist_lock)); 1092 KASSERT(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 *)((char *)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 *)((void *)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 = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)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 *)((char *)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 uvm_lwp_hold(l); 1250 mutex_enter(&proclist_lock); 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(mutex_owned(&proclist_lock)); 1259 p = LIST_NEXT(&marker, p_list); 1260 LIST_REMOVE(&marker, p_list); 1261 } 1262 mutex_exit(&proclist_lock); 1263 uvm_lwp_rele(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 /* Reset what needs to be reset in plimit. */ 1300 if (p->p_limit->pl_corename != defcorename) { 1301 lim_privatise(p, false); 1302 lim = p->p_limit; 1303 mutex_enter(&lim->pl_lock); 1304 cn = lim->pl_corename; 1305 lim->pl_corename = defcorename; 1306 mutex_exit(&lim->pl_lock); 1307 if (cn != defcorename) 1308 free(cn, M_TEMP); 1309 } 1310 1311 mutex_enter(&p->p_mutex); 1312 1313 /* Ensure the LWP cached credentials are up to date. */ 1314 if ((oc = l->l_cred) != p->p_cred) { 1315 kauth_cred_hold(p->p_cred); 1316 l->l_cred = p->p_cred; 1317 kauth_cred_free(oc); 1318 } 1319 1320 } 1321 1322 /* 1323 * Set in a new process credential, and drop the write lock. The credential 1324 * must have a reference already. Optionally, free a no-longer required 1325 * credential. The scheduler also needs to inspect p_cred, so we also 1326 * briefly acquire the sched state mutex. 1327 */ 1328 void 1329 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid) 1330 { 1331 struct lwp *l = curlwp; 1332 struct proc *p = l->l_proc; 1333 kauth_cred_t oc; 1334 1335 /* Is there a new credential to set in? */ 1336 if (scred != NULL) { 1337 mutex_enter(&p->p_smutex); 1338 p->p_cred = scred; 1339 mutex_exit(&p->p_smutex); 1340 1341 /* Ensure the LWP cached credentials are up to date. */ 1342 if ((oc = l->l_cred) != scred) { 1343 kauth_cred_hold(scred); 1344 l->l_cred = scred; 1345 } 1346 } else 1347 oc = NULL; /* XXXgcc */ 1348 1349 if (sugid) { 1350 /* 1351 * Mark process as having changed credentials, stops 1352 * tracing etc. 1353 */ 1354 p->p_flag |= PK_SUGID; 1355 } 1356 1357 mutex_exit(&p->p_mutex); 1358 1359 /* If there is a credential to be released, free it now. */ 1360 if (fcred != NULL) { 1361 KASSERT(scred != NULL); 1362 kauth_cred_free(fcred); 1363 if (oc != scred) 1364 kauth_cred_free(oc); 1365 } 1366 } 1367 1368 /* 1369 * proc_specific_key_create -- 1370 * Create a key for subsystem proc-specific data. 1371 */ 1372 int 1373 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 1374 { 1375 1376 return (specificdata_key_create(proc_specificdata_domain, keyp, dtor)); 1377 } 1378 1379 /* 1380 * proc_specific_key_delete -- 1381 * Delete a key for subsystem proc-specific data. 1382 */ 1383 void 1384 proc_specific_key_delete(specificdata_key_t key) 1385 { 1386 1387 specificdata_key_delete(proc_specificdata_domain, key); 1388 } 1389 1390 /* 1391 * proc_initspecific -- 1392 * Initialize a proc's specificdata container. 1393 */ 1394 void 1395 proc_initspecific(struct proc *p) 1396 { 1397 int error; 1398 1399 error = specificdata_init(proc_specificdata_domain, &p->p_specdataref); 1400 KASSERT(error == 0); 1401 } 1402 1403 /* 1404 * proc_finispecific -- 1405 * Finalize a proc's specificdata container. 1406 */ 1407 void 1408 proc_finispecific(struct proc *p) 1409 { 1410 1411 specificdata_fini(proc_specificdata_domain, &p->p_specdataref); 1412 } 1413 1414 /* 1415 * proc_getspecific -- 1416 * Return proc-specific data corresponding to the specified key. 1417 */ 1418 void * 1419 proc_getspecific(struct proc *p, specificdata_key_t key) 1420 { 1421 1422 return (specificdata_getspecific(proc_specificdata_domain, 1423 &p->p_specdataref, key)); 1424 } 1425 1426 /* 1427 * proc_setspecific -- 1428 * Set proc-specific data corresponding to the specified key. 1429 */ 1430 void 1431 proc_setspecific(struct proc *p, specificdata_key_t key, void *data) 1432 { 1433 1434 specificdata_setspecific(proc_specificdata_domain, 1435 &p->p_specdataref, key, data); 1436 } 1437