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