1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_ktrace.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mutex.h> 45 #include <sys/proc.h> 46 #include <sys/sysproto.h> 47 #include <sys/kse.h> 48 #include <sys/sysctl.h> 49 #include <sys/filedesc.h> 50 #include <sys/tty.h> 51 #include <sys/signalvar.h> 52 #include <sys/sx.h> 53 #include <sys/user.h> 54 #include <sys/jail.h> 55 #ifdef KTRACE 56 #include <sys/uio.h> 57 #include <sys/ktrace.h> 58 #endif 59 60 #include <vm/vm.h> 61 #include <vm/vm_extern.h> 62 #include <vm/pmap.h> 63 #include <vm/vm_map.h> 64 #include <vm/uma.h> 65 #include <machine/critical.h> 66 67 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 68 MALLOC_DEFINE(M_SESSION, "session", "session header"); 69 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 70 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 71 72 static struct proc *dopfind(register pid_t); 73 74 static void doenterpgrp(struct proc *, struct pgrp *); 75 76 static void pgdelete(struct pgrp *); 77 78 static void orphanpg(struct pgrp *pg); 79 80 static void proc_ctor(void *mem, int size, void *arg); 81 static void proc_dtor(void *mem, int size, void *arg); 82 static void proc_init(void *mem, int size); 83 static void proc_fini(void *mem, int size); 84 85 /* 86 * Other process lists 87 */ 88 struct pidhashhead *pidhashtbl; 89 u_long pidhash; 90 struct pgrphashhead *pgrphashtbl; 91 u_long pgrphash; 92 struct proclist allproc; 93 struct proclist zombproc; 94 struct sx allproc_lock; 95 struct sx proctree_lock; 96 struct mtx pargs_ref_lock; 97 uma_zone_t proc_zone; 98 uma_zone_t ithread_zone; 99 100 static int active_procs; 101 static int cached_procs; 102 static int allocated_procs; 103 104 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 105 106 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 107 108 /* 109 * Initialize global process hashing structures. 110 */ 111 void 112 procinit() 113 { 114 115 sx_init(&allproc_lock, "allproc"); 116 sx_init(&proctree_lock, "proctree"); 117 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF); 118 LIST_INIT(&allproc); 119 LIST_INIT(&zombproc); 120 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 121 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 122 proc_zone = uma_zcreate("PROC", sizeof (struct proc), 123 proc_ctor, proc_dtor, proc_init, proc_fini, 124 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 125 uihashinit(); 126 } 127 128 /* 129 * Prepare a proc for use. 130 */ 131 static void 132 proc_ctor(void *mem, int size, void *arg) 133 { 134 struct proc *p; 135 136 KASSERT((size == sizeof(struct proc)), 137 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 138 p = (struct proc *)mem; 139 #if 0 140 /* 141 * Maybe move these from process creation, but maybe not. 142 * Moving them here takes them away from their "natural" place 143 * in the fork process. 144 */ 145 bzero(&p->p_startzero, 146 (unsigned) RANGEOF(struct proc, p_startzero, p_endzero)); 147 p->p_state = PRS_NEW; 148 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 149 LIST_INIT(&p->p_children); 150 callout_init(&p->p_itcallout, 0); 151 #endif 152 cached_procs--; 153 active_procs++; 154 } 155 156 /* 157 * Reclaim a proc after use. 158 */ 159 static void 160 proc_dtor(void *mem, int size, void *arg) 161 { 162 struct proc *p; 163 164 KASSERT((size == sizeof(struct proc)), 165 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 166 p = (struct proc *)mem; 167 /* INVARIANTS checks go here */ 168 #if 0 /* See comment in proc_ctor about seperating things */ 169 mtx_destroy(&p->p_mtx); 170 #endif 171 active_procs--; 172 cached_procs++; 173 } 174 175 /* 176 * Initialize type-stable parts of a proc (when newly created). 177 */ 178 static void 179 proc_init(void *mem, int size) 180 { 181 struct proc *p; 182 183 KASSERT((size == sizeof(struct proc)), 184 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 185 p = (struct proc *)mem; 186 vm_proc_new(p); 187 cached_procs++; 188 allocated_procs++; 189 } 190 191 /* 192 * Tear down type-stable parts of a proc (just before being discarded) 193 */ 194 static void 195 proc_fini(void *mem, int size) 196 { 197 struct proc *p; 198 199 KASSERT((size == sizeof(struct proc)), 200 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 201 p = (struct proc *)mem; 202 vm_proc_dispose(p); 203 cached_procs--; 204 allocated_procs--; 205 } 206 207 /* 208 * KSE is linked onto the idle queue. 209 */ 210 void 211 kse_link(struct kse *ke, struct ksegrp *kg) 212 { 213 struct proc *p = kg->kg_proc; 214 215 KASSERT((ke->ke_state != KES_ONRUNQ), ("linking suspect kse on run queue")); 216 TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist); 217 kg->kg_kses++; 218 KASSERT((ke->ke_state != KES_IDLE), ("already on idle queue")); 219 ke->ke_state = KES_IDLE; 220 TAILQ_INSERT_HEAD(&kg->kg_iq, ke, ke_kgrlist); 221 kg->kg_idle_kses++; 222 ke->ke_proc = p; 223 ke->ke_ksegrp = kg; 224 ke->ke_thread = NULL; 225 ke->ke_oncpu = NOCPU; 226 } 227 228 void 229 ksegrp_link(struct ksegrp *kg, struct proc *p) 230 { 231 232 TAILQ_INIT(&kg->kg_threads); 233 TAILQ_INIT(&kg->kg_runq); /* links with td_runq */ 234 TAILQ_INIT(&kg->kg_slpq); /* links with td_runq */ 235 TAILQ_INIT(&kg->kg_kseq); /* all kses in ksegrp */ 236 TAILQ_INIT(&kg->kg_iq); /* all kses in ksegrp */ 237 kg->kg_proc = p; 238 /* the following counters are in the -zero- section and may not need clearing */ 239 kg->kg_numthreads = 0; 240 kg->kg_runnable = 0; 241 kg->kg_kses = 0; 242 kg->kg_idle_kses = 0; 243 kg->kg_runq_kses = 0; /* XXXKSE change name */ 244 /* link it in now that it's consitant */ 245 p->p_numksegrps++; 246 TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp); 247 } 248 249 /* 250 * for a newly created process, 251 * link up a the structure and its initial threads etc. 252 */ 253 void 254 proc_linkup(struct proc *p, struct ksegrp *kg, 255 struct kse *ke, struct thread *td) 256 { 257 258 TAILQ_INIT(&p->p_ksegrps); /* all ksegrps in proc */ 259 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 260 TAILQ_INIT(&p->p_suspended); /* Threads suspended */ 261 262 ksegrp_link(kg, p); 263 kse_link(ke, kg); 264 thread_link(td, kg); 265 } 266 267 int 268 thread_wakeup(struct thread *td, struct thread_wakeup_args *uap) 269 { 270 271 return(ENOSYS); 272 } 273 274 int 275 kse_exit(struct thread *td, struct kse_exit_args *uap) 276 { 277 278 return(ENOSYS); 279 } 280 281 int 282 kse_yield(struct thread *td, struct kse_yield_args *uap) 283 { 284 285 PROC_LOCK(td->td_proc); 286 mtx_lock_spin(&sched_lock); 287 thread_exit(); 288 /* NOTREACHED */ 289 return(0); 290 } 291 292 int kse_wakeup(struct thread *td, struct kse_wakeup_args *uap) 293 { 294 295 return(ENOSYS); 296 } 297 298 /* 299 * No new KSEG: first call: use current KSE, don't schedule an upcall 300 * All other situations, do alloate a new KSE and schedule an upcall on it. 301 */ 302 /* struct kse_new_args { 303 struct kse_mailbox *mbx; 304 int new_grp_flag; 305 }; */ 306 int 307 kse_new(struct thread *td, struct kse_new_args *uap) 308 { 309 struct kse *newkse; 310 struct proc *p; 311 struct kse_mailbox mbx; 312 int err; 313 314 p = td->td_proc; 315 if ((err = copyin(uap->mbx, &mbx, sizeof(mbx)))) 316 return (err); 317 PROC_LOCK(p); 318 /* 319 * If we have no KSE mode set, just set it, and skip KSE and KSEGRP 320 * creation. You cannot request a new group with the first one as 321 * you are effectively getting one. Instead, go directly to saving 322 * the upcall info. 323 */ 324 if ((td->td_proc->p_flag & P_KSES) || (uap->new_grp_flag)) { 325 326 return (EINVAL); /* XXX */ 327 /* 328 * If newgroup then create the new group. 329 * Check we have the resources for this. 330 */ 331 /* Copy lots of fields from the current KSEGRP. */ 332 /* Create the new KSE */ 333 /* Copy lots of fields from the current KSE. */ 334 } else { 335 /* 336 * We are switching to KSEs so just 337 * use the preallocated ones for this call. 338 * XXXKSE if we have to initialise any fields for KSE 339 * mode operation, do it here. 340 */ 341 newkse = td->td_kse; 342 } 343 /* 344 * Fill out the KSE-mode specific fields of the new kse. 345 */ 346 PROC_UNLOCK(p); 347 mtx_lock_spin(&sched_lock); 348 mi_switch(); /* Save current registers to PCB. */ 349 mtx_unlock_spin(&sched_lock); 350 newkse->ke_upcall = mbx.kmbx_upcall; 351 newkse->ke_stackbase = mbx.kmbx_stackbase; 352 newkse->ke_stacksize = mbx.kmbx_stacksize; 353 newkse->ke_mailbox = uap->mbx; 354 cpu_save_upcall(td, newkse); 355 /* Note that we are the returning syscall */ 356 td->td_retval[0] = 0; 357 td->td_retval[1] = 0; 358 359 if ((td->td_proc->p_flag & P_KSES) || (uap->new_grp_flag)) { 360 thread_schedule_upcall(td, newkse); 361 } else { 362 /* 363 * Don't set this until we are truely ready, because 364 * things will start acting differently. Return to the 365 * calling code for the first time. Assuming we set up 366 * the mailboxes right, all syscalls after this will be 367 * asynchronous. 368 */ 369 td->td_proc->p_flag |= P_KSES; 370 } 371 return (0); 372 } 373 374 /* 375 * Is p an inferior of the current process? 376 */ 377 int 378 inferior(p) 379 register struct proc *p; 380 { 381 382 sx_assert(&proctree_lock, SX_LOCKED); 383 for (; p != curproc; p = p->p_pptr) 384 if (p->p_pid == 0) 385 return (0); 386 return (1); 387 } 388 389 /* 390 * Locate a process by number 391 */ 392 struct proc * 393 pfind(pid) 394 register pid_t pid; 395 { 396 register struct proc *p; 397 398 sx_slock(&allproc_lock); 399 p = dopfind(pid); 400 sx_sunlock(&allproc_lock); 401 return (p); 402 } 403 404 static struct proc * 405 dopfind(pid) 406 register pid_t pid; 407 { 408 register struct proc *p; 409 410 sx_assert(&allproc_lock, SX_LOCKED); 411 412 LIST_FOREACH(p, PIDHASH(pid), p_hash) 413 if (p->p_pid == pid) { 414 PROC_LOCK(p); 415 break; 416 } 417 return (p); 418 } 419 420 /* 421 * Locate a process group by number. 422 * The caller must hold proctree_lock. 423 */ 424 struct pgrp * 425 pgfind(pgid) 426 register pid_t pgid; 427 { 428 register struct pgrp *pgrp; 429 430 sx_assert(&proctree_lock, SX_LOCKED); 431 432 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 433 if (pgrp->pg_id == pgid) { 434 PGRP_LOCK(pgrp); 435 return (pgrp); 436 } 437 } 438 return (NULL); 439 } 440 441 /* 442 * Create a new process group. 443 * pgid must be equal to the pid of p. 444 * Begin a new session if required. 445 */ 446 int 447 enterpgrp(p, pgid, pgrp, sess) 448 register struct proc *p; 449 pid_t pgid; 450 struct pgrp *pgrp; 451 struct session *sess; 452 { 453 struct pgrp *pgrp2; 454 455 sx_assert(&proctree_lock, SX_XLOCKED); 456 457 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 458 KASSERT(p->p_pid == pgid, 459 ("enterpgrp: new pgrp and pid != pgid")); 460 461 pgrp2 = pgfind(pgid); 462 463 KASSERT(pgrp2 == NULL, 464 ("enterpgrp: pgrp with pgid exists")); 465 KASSERT(!SESS_LEADER(p), 466 ("enterpgrp: session leader attempted setpgrp")); 467 468 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 469 470 if (sess != NULL) { 471 /* 472 * new session 473 */ 474 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 475 PROC_LOCK(p); 476 p->p_flag &= ~P_CONTROLT; 477 PROC_UNLOCK(p); 478 PGRP_LOCK(pgrp); 479 sess->s_leader = p; 480 sess->s_sid = p->p_pid; 481 sess->s_count = 1; 482 sess->s_ttyvp = NULL; 483 sess->s_ttyp = NULL; 484 bcopy(p->p_session->s_login, sess->s_login, 485 sizeof(sess->s_login)); 486 pgrp->pg_session = sess; 487 KASSERT(p == curproc, 488 ("enterpgrp: mksession and p != curproc")); 489 } else { 490 pgrp->pg_session = p->p_session; 491 SESS_LOCK(pgrp->pg_session); 492 pgrp->pg_session->s_count++; 493 SESS_UNLOCK(pgrp->pg_session); 494 PGRP_LOCK(pgrp); 495 } 496 pgrp->pg_id = pgid; 497 LIST_INIT(&pgrp->pg_members); 498 499 /* 500 * As we have an exclusive lock of proctree_lock, 501 * this should not deadlock. 502 */ 503 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 504 pgrp->pg_jobc = 0; 505 SLIST_INIT(&pgrp->pg_sigiolst); 506 PGRP_UNLOCK(pgrp); 507 508 doenterpgrp(p, pgrp); 509 510 return (0); 511 } 512 513 /* 514 * Move p to an existing process group 515 */ 516 int 517 enterthispgrp(p, pgrp) 518 register struct proc *p; 519 struct pgrp *pgrp; 520 { 521 522 sx_assert(&proctree_lock, SX_XLOCKED); 523 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 524 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 525 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 526 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 527 KASSERT(pgrp->pg_session == p->p_session, 528 ("%s: pgrp's session %p, p->p_session %p.\n", 529 __func__, 530 pgrp->pg_session, 531 p->p_session)); 532 KASSERT(pgrp != p->p_pgrp, 533 ("%s: p belongs to pgrp.", __func__)); 534 535 doenterpgrp(p, pgrp); 536 537 return (0); 538 } 539 540 /* 541 * Move p to a process group 542 */ 543 static void 544 doenterpgrp(p, pgrp) 545 struct proc *p; 546 struct pgrp *pgrp; 547 { 548 struct pgrp *savepgrp; 549 550 sx_assert(&proctree_lock, SX_XLOCKED); 551 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 552 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 553 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 554 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 555 556 savepgrp = p->p_pgrp; 557 558 /* 559 * Adjust eligibility of affected pgrps to participate in job control. 560 * Increment eligibility counts before decrementing, otherwise we 561 * could reach 0 spuriously during the first call. 562 */ 563 fixjobc(p, pgrp, 1); 564 fixjobc(p, p->p_pgrp, 0); 565 566 PGRP_LOCK(pgrp); 567 PGRP_LOCK(savepgrp); 568 PROC_LOCK(p); 569 LIST_REMOVE(p, p_pglist); 570 p->p_pgrp = pgrp; 571 PROC_UNLOCK(p); 572 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 573 PGRP_UNLOCK(savepgrp); 574 PGRP_UNLOCK(pgrp); 575 if (LIST_EMPTY(&savepgrp->pg_members)) 576 pgdelete(savepgrp); 577 } 578 579 /* 580 * remove process from process group 581 */ 582 int 583 leavepgrp(p) 584 register struct proc *p; 585 { 586 struct pgrp *savepgrp; 587 588 sx_assert(&proctree_lock, SX_XLOCKED); 589 savepgrp = p->p_pgrp; 590 PGRP_LOCK(savepgrp); 591 PROC_LOCK(p); 592 LIST_REMOVE(p, p_pglist); 593 p->p_pgrp = NULL; 594 PROC_UNLOCK(p); 595 PGRP_UNLOCK(savepgrp); 596 if (LIST_EMPTY(&savepgrp->pg_members)) 597 pgdelete(savepgrp); 598 return (0); 599 } 600 601 /* 602 * delete a process group 603 */ 604 static void 605 pgdelete(pgrp) 606 register struct pgrp *pgrp; 607 { 608 struct session *savesess; 609 610 sx_assert(&proctree_lock, SX_XLOCKED); 611 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 612 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 613 614 /* 615 * Reset any sigio structures pointing to us as a result of 616 * F_SETOWN with our pgid. 617 */ 618 funsetownlst(&pgrp->pg_sigiolst); 619 620 PGRP_LOCK(pgrp); 621 if (pgrp->pg_session->s_ttyp != NULL && 622 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 623 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 624 LIST_REMOVE(pgrp, pg_hash); 625 savesess = pgrp->pg_session; 626 SESS_LOCK(savesess); 627 savesess->s_count--; 628 SESS_UNLOCK(savesess); 629 PGRP_UNLOCK(pgrp); 630 if (savesess->s_count == 0) { 631 mtx_destroy(&savesess->s_mtx); 632 FREE(pgrp->pg_session, M_SESSION); 633 } 634 mtx_destroy(&pgrp->pg_mtx); 635 FREE(pgrp, M_PGRP); 636 } 637 638 /* 639 * Adjust pgrp jobc counters when specified process changes process group. 640 * We count the number of processes in each process group that "qualify" 641 * the group for terminal job control (those with a parent in a different 642 * process group of the same session). If that count reaches zero, the 643 * process group becomes orphaned. Check both the specified process' 644 * process group and that of its children. 645 * entering == 0 => p is leaving specified group. 646 * entering == 1 => p is entering specified group. 647 */ 648 void 649 fixjobc(p, pgrp, entering) 650 register struct proc *p; 651 register struct pgrp *pgrp; 652 int entering; 653 { 654 register struct pgrp *hispgrp; 655 register struct session *mysession; 656 657 sx_assert(&proctree_lock, SX_LOCKED); 658 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 659 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 660 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 661 662 /* 663 * Check p's parent to see whether p qualifies its own process 664 * group; if so, adjust count for p's process group. 665 */ 666 mysession = pgrp->pg_session; 667 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 668 hispgrp->pg_session == mysession) { 669 PGRP_LOCK(pgrp); 670 if (entering) 671 pgrp->pg_jobc++; 672 else { 673 --pgrp->pg_jobc; 674 if (pgrp->pg_jobc == 0) 675 orphanpg(pgrp); 676 } 677 PGRP_UNLOCK(pgrp); 678 } 679 680 /* 681 * Check this process' children to see whether they qualify 682 * their process groups; if so, adjust counts for children's 683 * process groups. 684 */ 685 LIST_FOREACH(p, &p->p_children, p_sibling) { 686 if ((hispgrp = p->p_pgrp) != pgrp && 687 hispgrp->pg_session == mysession && 688 p->p_state != PRS_ZOMBIE) { 689 PGRP_LOCK(hispgrp); 690 if (entering) 691 hispgrp->pg_jobc++; 692 else { 693 --hispgrp->pg_jobc; 694 if (hispgrp->pg_jobc == 0) 695 orphanpg(hispgrp); 696 } 697 PGRP_UNLOCK(hispgrp); 698 } 699 } 700 } 701 702 /* 703 * A process group has become orphaned; 704 * if there are any stopped processes in the group, 705 * hang-up all process in that group. 706 */ 707 static void 708 orphanpg(pg) 709 struct pgrp *pg; 710 { 711 register struct proc *p; 712 713 PGRP_LOCK_ASSERT(pg, MA_OWNED); 714 715 mtx_lock_spin(&sched_lock); 716 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 717 if (P_SHOULDSTOP(p)) { 718 mtx_unlock_spin(&sched_lock); 719 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 720 PROC_LOCK(p); 721 psignal(p, SIGHUP); 722 psignal(p, SIGCONT); 723 PROC_UNLOCK(p); 724 } 725 return; 726 } 727 } 728 mtx_unlock_spin(&sched_lock); 729 } 730 731 #include "opt_ddb.h" 732 #ifdef DDB 733 #include <ddb/ddb.h> 734 735 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 736 { 737 register struct pgrp *pgrp; 738 register struct proc *p; 739 register int i; 740 741 for (i = 0; i <= pgrphash; i++) { 742 if (!LIST_EMPTY(&pgrphashtbl[i])) { 743 printf("\tindx %d\n", i); 744 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 745 printf( 746 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 747 (void *)pgrp, (long)pgrp->pg_id, 748 (void *)pgrp->pg_session, 749 pgrp->pg_session->s_count, 750 (void *)LIST_FIRST(&pgrp->pg_members)); 751 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 752 printf("\t\tpid %ld addr %p pgrp %p\n", 753 (long)p->p_pid, (void *)p, 754 (void *)p->p_pgrp); 755 } 756 } 757 } 758 } 759 } 760 #endif /* DDB */ 761 762 /* 763 * Fill in an kinfo_proc structure for the specified process. 764 * Must be called with the target process locked. 765 */ 766 void 767 fill_kinfo_proc(p, kp) 768 struct proc *p; 769 struct kinfo_proc *kp; 770 { 771 struct thread *td; 772 struct tty *tp; 773 struct session *sp; 774 struct timeval tv; 775 776 bzero(kp, sizeof(*kp)); 777 778 kp->ki_structsize = sizeof(*kp); 779 kp->ki_paddr = p; 780 PROC_LOCK_ASSERT(p, MA_OWNED); 781 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 782 kp->ki_args = p->p_args; 783 kp->ki_textvp = p->p_textvp; 784 #ifdef KTRACE 785 kp->ki_tracep = p->p_tracep; 786 mtx_lock(&ktrace_mtx); 787 kp->ki_traceflag = p->p_traceflag; 788 mtx_unlock(&ktrace_mtx); 789 #endif 790 kp->ki_fd = p->p_fd; 791 kp->ki_vmspace = p->p_vmspace; 792 if (p->p_ucred) { 793 kp->ki_uid = p->p_ucred->cr_uid; 794 kp->ki_ruid = p->p_ucred->cr_ruid; 795 kp->ki_svuid = p->p_ucred->cr_svuid; 796 /* XXX bde doesn't like KI_NGROUPS */ 797 kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS); 798 bcopy(p->p_ucred->cr_groups, kp->ki_groups, 799 kp->ki_ngroups * sizeof(gid_t)); 800 kp->ki_rgid = p->p_ucred->cr_rgid; 801 kp->ki_svgid = p->p_ucred->cr_svgid; 802 } 803 if (p->p_procsig) { 804 kp->ki_sigignore = p->p_procsig->ps_sigignore; 805 kp->ki_sigcatch = p->p_procsig->ps_sigcatch; 806 } 807 mtx_lock_spin(&sched_lock); 808 if (p->p_state != PRS_NEW && 809 p->p_state != PRS_ZOMBIE && 810 p->p_vmspace != NULL) { 811 struct vmspace *vm = p->p_vmspace; 812 813 kp->ki_size = vm->vm_map.size; 814 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 815 if (p->p_sflag & PS_INMEM) 816 kp->ki_rssize += UAREA_PAGES; 817 FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */ 818 kp->ki_rssize += KSTACK_PAGES; 819 kp->ki_swrss = vm->vm_swrss; 820 kp->ki_tsize = vm->vm_tsize; 821 kp->ki_dsize = vm->vm_dsize; 822 kp->ki_ssize = vm->vm_ssize; 823 } 824 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 825 kp->ki_start = p->p_stats->p_start; 826 kp->ki_rusage = p->p_stats->p_ru; 827 kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec + 828 p->p_stats->p_cru.ru_stime.tv_sec; 829 kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec + 830 p->p_stats->p_cru.ru_stime.tv_usec; 831 } 832 if (p->p_state != PRS_ZOMBIE) { 833 td = FIRST_THREAD_IN_PROC(p); 834 if (!(p->p_flag & P_KSES)) { 835 if (td->td_wmesg != NULL) { 836 strncpy(kp->ki_wmesg, td->td_wmesg, 837 sizeof(kp->ki_wmesg) - 1); 838 } 839 if (td->td_state == TDS_MTX) { 840 kp->ki_kiflag |= KI_MTXBLOCK; 841 strncpy(kp->ki_mtxname, td->td_mtxname, 842 sizeof(kp->ki_mtxname) - 1); 843 } 844 } 845 846 if (p->p_state == PRS_NORMAL) { /* XXXKSE very aproximate */ 847 if ((td->td_state == TDS_RUNQ) || 848 (td->td_state == TDS_RUNNING)) { 849 kp->ki_stat = SRUN; 850 } else if (td->td_state == TDS_SLP) { 851 kp->ki_stat = SSLEEP; 852 } else if (P_SHOULDSTOP(p)) { 853 kp->ki_stat = SSTOP; 854 } else if (td->td_state == TDS_MTX) { 855 kp->ki_stat = SMTX; 856 } else { 857 kp->ki_stat = SWAIT; 858 } 859 } else { 860 kp->ki_stat = SIDL; 861 } 862 863 kp->ki_sflag = p->p_sflag; 864 kp->ki_swtime = p->p_swtime; 865 kp->ki_pid = p->p_pid; 866 /* vvv XXXKSE */ 867 if (!(p->p_flag & P_KSES)) { 868 bintime2timeval(&p->p_runtime, &tv); 869 kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec; 870 kp->ki_pctcpu = p->p_kse.ke_pctcpu; 871 kp->ki_estcpu = p->p_ksegrp.kg_estcpu; 872 kp->ki_slptime = p->p_ksegrp.kg_slptime; 873 kp->ki_wchan = td->td_wchan; 874 kp->ki_pri.pri_level = td->td_priority; 875 kp->ki_pri.pri_user = p->p_ksegrp.kg_user_pri; 876 kp->ki_pri.pri_class = p->p_ksegrp.kg_pri_class; 877 kp->ki_pri.pri_native = td->td_base_pri; 878 kp->ki_nice = p->p_ksegrp.kg_nice; 879 kp->ki_rqindex = p->p_kse.ke_rqindex; 880 kp->ki_oncpu = p->p_kse.ke_oncpu; 881 kp->ki_lastcpu = td->td_lastcpu; 882 kp->ki_tdflags = td->td_flags; 883 kp->ki_pcb = td->td_pcb; 884 kp->ki_kstack = (void *)td->td_kstack; 885 } else { 886 kp->ki_oncpu = -1; 887 kp->ki_lastcpu = -1; 888 kp->ki_tdflags = -1; 889 /* All the reast are 0 */ 890 } 891 } else { 892 kp->ki_stat = SZOMB; 893 } 894 /* ^^^ XXXKSE */ 895 mtx_unlock_spin(&sched_lock); 896 sp = NULL; 897 tp = NULL; 898 if (p->p_pgrp) { 899 kp->ki_pgid = p->p_pgrp->pg_id; 900 kp->ki_jobc = p->p_pgrp->pg_jobc; 901 sp = p->p_pgrp->pg_session; 902 903 if (sp != NULL) { 904 kp->ki_sid = sp->s_sid; 905 SESS_LOCK(sp); 906 strncpy(kp->ki_login, sp->s_login, 907 sizeof(kp->ki_login) - 1); 908 if (sp->s_ttyvp) 909 kp->ki_kiflag |= KI_CTTY; 910 if (SESS_LEADER(p)) 911 kp->ki_kiflag |= KI_SLEADER; 912 tp = sp->s_ttyp; 913 SESS_UNLOCK(sp); 914 } 915 } 916 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 917 kp->ki_tdev = dev2udev(tp->t_dev); 918 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 919 if (tp->t_session) 920 kp->ki_tsid = tp->t_session->s_sid; 921 } else 922 kp->ki_tdev = NOUDEV; 923 if (p->p_comm[0] != '\0') { 924 strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1); 925 strncpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm) - 1); 926 } 927 kp->ki_siglist = p->p_siglist; 928 kp->ki_sigmask = p->p_sigmask; 929 kp->ki_xstat = p->p_xstat; 930 kp->ki_acflag = p->p_acflag; 931 kp->ki_flag = p->p_flag; 932 /* If jailed(p->p_ucred), emulate the old P_JAILED flag. */ 933 if (jailed(p->p_ucred)) 934 kp->ki_flag |= P_JAILED; 935 kp->ki_lock = p->p_lock; 936 if (p->p_pptr) 937 kp->ki_ppid = p->p_pptr->p_pid; 938 } 939 940 /* 941 * Locate a zombie process by number 942 */ 943 struct proc * 944 zpfind(pid_t pid) 945 { 946 struct proc *p; 947 948 sx_slock(&allproc_lock); 949 LIST_FOREACH(p, &zombproc, p_list) 950 if (p->p_pid == pid) { 951 PROC_LOCK(p); 952 break; 953 } 954 sx_sunlock(&allproc_lock); 955 return (p); 956 } 957 958 959 /* 960 * Must be called with the process locked and will return with it unlocked. 961 */ 962 static int 963 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 964 { 965 struct kinfo_proc kinfo_proc; 966 int error; 967 struct proc *np; 968 pid_t pid = p->p_pid; 969 970 PROC_LOCK_ASSERT(p, MA_OWNED); 971 fill_kinfo_proc(p, &kinfo_proc); 972 PROC_UNLOCK(p); 973 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc)); 974 if (error) 975 return (error); 976 if (doingzomb) 977 np = zpfind(pid); 978 else { 979 if (pid == 0) 980 return (0); 981 np = pfind(pid); 982 } 983 if (np == NULL) 984 return EAGAIN; 985 if (np != p) { 986 PROC_UNLOCK(np); 987 return EAGAIN; 988 } 989 PROC_UNLOCK(np); 990 return (0); 991 } 992 993 static int 994 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 995 { 996 int *name = (int*) arg1; 997 u_int namelen = arg2; 998 struct proc *p; 999 int doingzomb; 1000 int error = 0; 1001 1002 if (oidp->oid_number == KERN_PROC_PID) { 1003 if (namelen != 1) 1004 return (EINVAL); 1005 p = pfind((pid_t)name[0]); 1006 if (!p) 1007 return (0); 1008 if (p_cansee(curthread, p)) { 1009 PROC_UNLOCK(p); 1010 return (0); 1011 } 1012 error = sysctl_out_proc(p, req, 0); 1013 return (error); 1014 } 1015 if (oidp->oid_number == KERN_PROC_ALL && !namelen) 1016 ; 1017 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 1018 ; 1019 else 1020 return (EINVAL); 1021 1022 if (!req->oldptr) { 1023 /* overestimate by 5 procs */ 1024 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1025 if (error) 1026 return (error); 1027 } 1028 sx_slock(&allproc_lock); 1029 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1030 if (!doingzomb) 1031 p = LIST_FIRST(&allproc); 1032 else 1033 p = LIST_FIRST(&zombproc); 1034 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1035 PROC_LOCK(p); 1036 /* 1037 * Show a user only appropriate processes. 1038 */ 1039 if (p_cansee(curthread, p)) { 1040 PROC_UNLOCK(p); 1041 continue; 1042 } 1043 /* 1044 * Skip embryonic processes. 1045 */ 1046 if (p->p_state == PRS_NEW) { 1047 PROC_UNLOCK(p); 1048 continue; 1049 } 1050 /* 1051 * TODO - make more efficient (see notes below). 1052 * do by session. 1053 */ 1054 switch (oidp->oid_number) { 1055 1056 case KERN_PROC_PGRP: 1057 /* could do this by traversing pgrp */ 1058 if (p->p_pgrp == NULL || 1059 p->p_pgrp->pg_id != (pid_t)name[0]) { 1060 PROC_UNLOCK(p); 1061 continue; 1062 } 1063 break; 1064 1065 case KERN_PROC_TTY: 1066 if ((p->p_flag & P_CONTROLT) == 0 || 1067 p->p_session == NULL) { 1068 PROC_UNLOCK(p); 1069 continue; 1070 } 1071 SESS_LOCK(p->p_session); 1072 if (p->p_session->s_ttyp == NULL || 1073 dev2udev(p->p_session->s_ttyp->t_dev) != 1074 (udev_t)name[0]) { 1075 SESS_UNLOCK(p->p_session); 1076 PROC_UNLOCK(p); 1077 continue; 1078 } 1079 SESS_UNLOCK(p->p_session); 1080 break; 1081 1082 case KERN_PROC_UID: 1083 if (p->p_ucred == NULL || 1084 p->p_ucred->cr_uid != (uid_t)name[0]) { 1085 PROC_UNLOCK(p); 1086 continue; 1087 } 1088 break; 1089 1090 case KERN_PROC_RUID: 1091 if (p->p_ucred == NULL || 1092 p->p_ucred->cr_ruid != (uid_t)name[0]) { 1093 PROC_UNLOCK(p); 1094 continue; 1095 } 1096 break; 1097 } 1098 1099 error = sysctl_out_proc(p, req, doingzomb); 1100 if (error) { 1101 sx_sunlock(&allproc_lock); 1102 return (error); 1103 } 1104 } 1105 } 1106 sx_sunlock(&allproc_lock); 1107 return (0); 1108 } 1109 1110 struct pargs * 1111 pargs_alloc(int len) 1112 { 1113 struct pargs *pa; 1114 1115 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1116 M_WAITOK); 1117 pa->ar_ref = 1; 1118 pa->ar_length = len; 1119 return (pa); 1120 } 1121 1122 void 1123 pargs_free(struct pargs *pa) 1124 { 1125 1126 FREE(pa, M_PARGS); 1127 } 1128 1129 void 1130 pargs_hold(struct pargs *pa) 1131 { 1132 1133 if (pa == NULL) 1134 return; 1135 PARGS_LOCK(pa); 1136 pa->ar_ref++; 1137 PARGS_UNLOCK(pa); 1138 } 1139 1140 void 1141 pargs_drop(struct pargs *pa) 1142 { 1143 1144 if (pa == NULL) 1145 return; 1146 PARGS_LOCK(pa); 1147 if (--pa->ar_ref == 0) { 1148 PARGS_UNLOCK(pa); 1149 pargs_free(pa); 1150 } else 1151 PARGS_UNLOCK(pa); 1152 } 1153 1154 /* 1155 * This sysctl allows a process to retrieve the argument list or process 1156 * title for another process without groping around in the address space 1157 * of the other process. It also allow a process to set its own "process 1158 * title to a string of its own choice. 1159 */ 1160 static int 1161 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1162 { 1163 int *name = (int*) arg1; 1164 u_int namelen = arg2; 1165 struct proc *p; 1166 struct pargs *pa; 1167 int error = 0; 1168 1169 if (namelen != 1) 1170 return (EINVAL); 1171 1172 p = pfind((pid_t)name[0]); 1173 if (!p) 1174 return (0); 1175 1176 if ((!ps_argsopen) && p_cansee(curthread, p)) { 1177 PROC_UNLOCK(p); 1178 return (0); 1179 } 1180 PROC_UNLOCK(p); 1181 1182 if (req->newptr && curproc != p) 1183 return (EPERM); 1184 1185 PROC_LOCK(p); 1186 pa = p->p_args; 1187 pargs_hold(pa); 1188 PROC_UNLOCK(p); 1189 if (req->oldptr && pa != NULL) { 1190 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1191 } 1192 pargs_drop(pa); 1193 if (req->newptr == NULL) 1194 return (error); 1195 1196 PROC_LOCK(p); 1197 pa = p->p_args; 1198 p->p_args = NULL; 1199 PROC_UNLOCK(p); 1200 pargs_drop(pa); 1201 1202 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1203 return (error); 1204 1205 pa = pargs_alloc(req->newlen); 1206 error = SYSCTL_IN(req, pa->ar_args, req->newlen); 1207 if (!error) { 1208 PROC_LOCK(p); 1209 p->p_args = pa; 1210 PROC_UNLOCK(p); 1211 } else 1212 pargs_free(pa); 1213 return (error); 1214 } 1215 1216 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1217 1218 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1219 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1220 1221 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1222 sysctl_kern_proc, "Process table"); 1223 1224 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1225 sysctl_kern_proc, "Process table"); 1226 1227 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1228 sysctl_kern_proc, "Process table"); 1229 1230 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1231 sysctl_kern_proc, "Process table"); 1232 1233 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1234 sysctl_kern_proc, "Process table"); 1235 1236 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1237 sysctl_kern_proc_args, "Process argument list"); 1238 1239 SYSCTL_INT(_kern_proc, OID_AUTO, active, CTLFLAG_RD, 1240 &active_procs, 0, "Number of active procs in system."); 1241 1242 SYSCTL_INT(_kern_proc, OID_AUTO, cached, CTLFLAG_RD, 1243 &cached_procs, 0, "Number of procs in proc cache."); 1244 1245 SYSCTL_INT(_kern_proc, OID_AUTO, allocated, CTLFLAG_RD, 1246 &allocated_procs, 0, "Number of procs in zone."); 1247