35 36#include "opt_ktrace.h" 37#include "opt_kstack_pages.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/sysent.h> 47#include <sys/sched.h> 48#include <sys/smp.h> 49#include <sys/sysctl.h> 50#include <sys/filedesc.h> 51#include <sys/tty.h> 52#include <sys/signalvar.h> 53#include <sys/sx.h> 54#include <sys/user.h> 55#include <sys/jail.h> 56#ifdef KTRACE 57#include <sys/uio.h> 58#include <sys/ktrace.h> 59#endif 60 61#include <vm/vm.h> 62#include <vm/vm_extern.h> 63#include <vm/pmap.h> 64#include <vm/vm_map.h> 65#include <vm/uma.h> 66#include <machine/critical.h> 67 68MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 69MALLOC_DEFINE(M_SESSION, "session", "session header"); 70static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 71MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 72 73static void doenterpgrp(struct proc *, struct pgrp *); 74static void orphanpg(struct pgrp *pg); 75static void pgadjustjobc(struct pgrp *pgrp, int entering); 76static void pgdelete(struct pgrp *); 77static void proc_ctor(void *mem, int size, void *arg); 78static void proc_dtor(void *mem, int size, void *arg); 79static void proc_init(void *mem, int size); 80static void proc_fini(void *mem, int size); 81 82/* 83 * Other process lists 84 */ 85struct pidhashhead *pidhashtbl; 86u_long pidhash; 87struct pgrphashhead *pgrphashtbl; 88u_long pgrphash; 89struct proclist allproc; 90struct proclist zombproc; 91struct sx allproc_lock; 92struct sx proctree_lock; 93struct mtx pargs_ref_lock; 94struct mtx ppeers_lock; 95uma_zone_t proc_zone; 96uma_zone_t ithread_zone; 97 98int kstack_pages = KSTACK_PAGES; 99int uarea_pages = UAREA_PAGES; 100SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 101SYSCTL_INT(_kern, OID_AUTO, uarea_pages, CTLFLAG_RD, &uarea_pages, 0, ""); 102 103#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 104 105CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 106 107/* 108 * Initialize global process hashing structures. 109 */ 110void 111procinit() 112{ 113 114 sx_init(&allproc_lock, "allproc"); 115 sx_init(&proctree_lock, "proctree"); 116 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF); 117 mtx_init(&ppeers_lock, "p_peers", 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", sched_sizeof_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 */ 131static void 132proc_ctor(void *mem, int size, void *arg) 133{ 134 struct proc *p; 135 136 p = (struct proc *)mem; 137} 138 139/* 140 * Reclaim a proc after use. 141 */ 142static void 143proc_dtor(void *mem, int size, void *arg) 144{ 145 struct proc *p; 146 struct thread *td; 147 struct ksegrp *kg; 148 struct kse *ke; 149 150 /* INVARIANTS checks go here */ 151 p = (struct proc *)mem; 152 KASSERT((p->p_numthreads == 1), 153 ("bad number of threads in exiting process")); 154 td = FIRST_THREAD_IN_PROC(p); 155 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 156 kg = FIRST_KSEGRP_IN_PROC(p); 157 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 158 ke = FIRST_KSE_IN_KSEGRP(kg); 159 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 160 161 /* Dispose of an alternate kstack, if it exists. 162 * XXX What if there are more than one thread in the proc? 163 * The first thread in the proc is special and not 164 * freed, so you gotta do this here. 165 */ 166 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0)) 167 vm_thread_dispose_altkstack(td); 168 169 /* 170 * We want to make sure we know the initial linkages. 171 * so for now tear them down and remake them. 172 * This is probably un-needed as we can probably rely 173 * on the state coming in here from wait4(). 174 */ 175 proc_linkup(p, kg, ke, td); 176} 177 178/* 179 * Initialize type-stable parts of a proc (when newly created). 180 */ 181static void 182proc_init(void *mem, int size) 183{ 184 struct proc *p; 185 struct thread *td; 186 struct ksegrp *kg; 187 struct kse *ke; 188 189 p = (struct proc *)mem; 190 p->p_sched = (struct p_sched *)&p[1]; 191 vm_proc_new(p); 192 td = thread_alloc(); 193 ke = kse_alloc(); 194 kg = ksegrp_alloc(); 195 proc_linkup(p, kg, ke, td); 196 bzero(&p->p_mtx, sizeof(struct mtx)); 197 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 198} 199 200/* 201 * Tear down type-stable parts of a proc (just before being discarded) 202 */ 203static void 204proc_fini(void *mem, int size) 205{ 206 struct proc *p; 207 struct thread *td; 208 struct ksegrp *kg; 209 struct kse *ke; 210 211 p = (struct proc *)mem; 212 KASSERT((p->p_numthreads == 1), 213 ("bad number of threads in freeing process")); 214 td = FIRST_THREAD_IN_PROC(p); 215 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 216 kg = FIRST_KSEGRP_IN_PROC(p); 217 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 218 ke = FIRST_KSE_IN_KSEGRP(kg); 219 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 220 vm_proc_dispose(p); 221 thread_free(td); 222 ksegrp_free(kg); 223 kse_free(ke); 224 mtx_destroy(&p->p_mtx); 225} 226 227/* 228 * Is p an inferior of the current process? 229 */ 230int 231inferior(p) 232 register struct proc *p; 233{ 234 235 sx_assert(&proctree_lock, SX_LOCKED); 236 for (; p != curproc; p = p->p_pptr) 237 if (p->p_pid == 0) 238 return (0); 239 return (1); 240} 241 242/* 243 * Locate a process by number 244 */ 245struct proc * 246pfind(pid) 247 register pid_t pid; 248{ 249 register struct proc *p; 250 251 sx_slock(&allproc_lock); 252 LIST_FOREACH(p, PIDHASH(pid), p_hash) 253 if (p->p_pid == pid) { 254 PROC_LOCK(p); 255 break; 256 } 257 sx_sunlock(&allproc_lock); 258 return (p); 259} 260 261/* 262 * Locate a process group by number. 263 * The caller must hold proctree_lock. 264 */ 265struct pgrp * 266pgfind(pgid) 267 register pid_t pgid; 268{ 269 register struct pgrp *pgrp; 270 271 sx_assert(&proctree_lock, SX_LOCKED); 272 273 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 274 if (pgrp->pg_id == pgid) { 275 PGRP_LOCK(pgrp); 276 return (pgrp); 277 } 278 } 279 return (NULL); 280} 281 282/* 283 * Create a new process group. 284 * pgid must be equal to the pid of p. 285 * Begin a new session if required. 286 */ 287int 288enterpgrp(p, pgid, pgrp, sess) 289 register struct proc *p; 290 pid_t pgid; 291 struct pgrp *pgrp; 292 struct session *sess; 293{ 294 struct pgrp *pgrp2; 295 296 sx_assert(&proctree_lock, SX_XLOCKED); 297 298 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 299 KASSERT(p->p_pid == pgid, 300 ("enterpgrp: new pgrp and pid != pgid")); 301 302 pgrp2 = pgfind(pgid); 303 304 KASSERT(pgrp2 == NULL, 305 ("enterpgrp: pgrp with pgid exists")); 306 KASSERT(!SESS_LEADER(p), 307 ("enterpgrp: session leader attempted setpgrp")); 308 309 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 310 311 if (sess != NULL) { 312 /* 313 * new session 314 */ 315 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 316 PROC_LOCK(p); 317 p->p_flag &= ~P_CONTROLT; 318 PROC_UNLOCK(p); 319 PGRP_LOCK(pgrp); 320 sess->s_leader = p; 321 sess->s_sid = p->p_pid; 322 sess->s_count = 1; 323 sess->s_ttyvp = NULL; 324 sess->s_ttyp = NULL; 325 bcopy(p->p_session->s_login, sess->s_login, 326 sizeof(sess->s_login)); 327 pgrp->pg_session = sess; 328 KASSERT(p == curproc, 329 ("enterpgrp: mksession and p != curproc")); 330 } else { 331 pgrp->pg_session = p->p_session; 332 SESS_LOCK(pgrp->pg_session); 333 pgrp->pg_session->s_count++; 334 SESS_UNLOCK(pgrp->pg_session); 335 PGRP_LOCK(pgrp); 336 } 337 pgrp->pg_id = pgid; 338 LIST_INIT(&pgrp->pg_members); 339 340 /* 341 * As we have an exclusive lock of proctree_lock, 342 * this should not deadlock. 343 */ 344 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 345 pgrp->pg_jobc = 0; 346 SLIST_INIT(&pgrp->pg_sigiolst); 347 PGRP_UNLOCK(pgrp); 348 349 doenterpgrp(p, pgrp); 350 351 return (0); 352} 353 354/* 355 * Move p to an existing process group 356 */ 357int 358enterthispgrp(p, pgrp) 359 register struct proc *p; 360 struct pgrp *pgrp; 361{ 362 363 sx_assert(&proctree_lock, SX_XLOCKED); 364 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 365 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 366 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 367 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 368 KASSERT(pgrp->pg_session == p->p_session, 369 ("%s: pgrp's session %p, p->p_session %p.\n", 370 __func__, 371 pgrp->pg_session, 372 p->p_session)); 373 KASSERT(pgrp != p->p_pgrp, 374 ("%s: p belongs to pgrp.", __func__)); 375 376 doenterpgrp(p, pgrp); 377 378 return (0); 379} 380 381/* 382 * Move p to a process group 383 */ 384static void 385doenterpgrp(p, pgrp) 386 struct proc *p; 387 struct pgrp *pgrp; 388{ 389 struct pgrp *savepgrp; 390 391 sx_assert(&proctree_lock, SX_XLOCKED); 392 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 393 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 394 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 395 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 396 397 savepgrp = p->p_pgrp; 398 399 /* 400 * Adjust eligibility of affected pgrps to participate in job control. 401 * Increment eligibility counts before decrementing, otherwise we 402 * could reach 0 spuriously during the first call. 403 */ 404 fixjobc(p, pgrp, 1); 405 fixjobc(p, p->p_pgrp, 0); 406 407 PGRP_LOCK(pgrp); 408 PGRP_LOCK(savepgrp); 409 PROC_LOCK(p); 410 LIST_REMOVE(p, p_pglist); 411 p->p_pgrp = pgrp; 412 PROC_UNLOCK(p); 413 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 414 PGRP_UNLOCK(savepgrp); 415 PGRP_UNLOCK(pgrp); 416 if (LIST_EMPTY(&savepgrp->pg_members)) 417 pgdelete(savepgrp); 418} 419 420/* 421 * remove process from process group 422 */ 423int 424leavepgrp(p) 425 register struct proc *p; 426{ 427 struct pgrp *savepgrp; 428 429 sx_assert(&proctree_lock, SX_XLOCKED); 430 savepgrp = p->p_pgrp; 431 PGRP_LOCK(savepgrp); 432 PROC_LOCK(p); 433 LIST_REMOVE(p, p_pglist); 434 p->p_pgrp = NULL; 435 PROC_UNLOCK(p); 436 PGRP_UNLOCK(savepgrp); 437 if (LIST_EMPTY(&savepgrp->pg_members)) 438 pgdelete(savepgrp); 439 return (0); 440} 441 442/* 443 * delete a process group 444 */ 445static void 446pgdelete(pgrp) 447 register struct pgrp *pgrp; 448{ 449 struct session *savesess; 450 int i; 451 452 sx_assert(&proctree_lock, SX_XLOCKED); 453 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 454 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 455 456 /* 457 * Reset any sigio structures pointing to us as a result of 458 * F_SETOWN with our pgid. 459 */ 460 funsetownlst(&pgrp->pg_sigiolst); 461 462 PGRP_LOCK(pgrp); 463 if (pgrp->pg_session->s_ttyp != NULL && 464 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 465 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 466 LIST_REMOVE(pgrp, pg_hash); 467 savesess = pgrp->pg_session; 468 SESS_LOCK(savesess); 469 i = --savesess->s_count; 470 SESS_UNLOCK(savesess); 471 PGRP_UNLOCK(pgrp); 472 if (i == 0) { 473 if (savesess->s_ttyp != NULL) 474 ttyrel(savesess->s_ttyp); 475 mtx_destroy(&savesess->s_mtx); 476 FREE(savesess, M_SESSION); 477 } 478 mtx_destroy(&pgrp->pg_mtx); 479 FREE(pgrp, M_PGRP); 480} 481 482static void 483pgadjustjobc(pgrp, entering) 484 struct pgrp *pgrp; 485 int entering; 486{ 487 488 PGRP_LOCK(pgrp); 489 if (entering) 490 pgrp->pg_jobc++; 491 else { 492 --pgrp->pg_jobc; 493 if (pgrp->pg_jobc == 0) 494 orphanpg(pgrp); 495 } 496 PGRP_UNLOCK(pgrp); 497} 498 499/* 500 * Adjust pgrp jobc counters when specified process changes process group. 501 * We count the number of processes in each process group that "qualify" 502 * the group for terminal job control (those with a parent in a different 503 * process group of the same session). If that count reaches zero, the 504 * process group becomes orphaned. Check both the specified process' 505 * process group and that of its children. 506 * entering == 0 => p is leaving specified group. 507 * entering == 1 => p is entering specified group. 508 */ 509void 510fixjobc(p, pgrp, entering) 511 register struct proc *p; 512 register struct pgrp *pgrp; 513 int entering; 514{ 515 register struct pgrp *hispgrp; 516 register struct session *mysession; 517 518 sx_assert(&proctree_lock, SX_LOCKED); 519 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 520 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 521 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 522 523 /* 524 * Check p's parent to see whether p qualifies its own process 525 * group; if so, adjust count for p's process group. 526 */ 527 mysession = pgrp->pg_session; 528 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 529 hispgrp->pg_session == mysession) 530 pgadjustjobc(pgrp, entering); 531 532 /* 533 * Check this process' children to see whether they qualify 534 * their process groups; if so, adjust counts for children's 535 * process groups. 536 */ 537 LIST_FOREACH(p, &p->p_children, p_sibling) { 538 hispgrp = p->p_pgrp; 539 if (hispgrp == pgrp || 540 hispgrp->pg_session != mysession) 541 continue; 542 PROC_LOCK(p); 543 if (p->p_state == PRS_ZOMBIE) { 544 PROC_UNLOCK(p); 545 continue; 546 } 547 PROC_UNLOCK(p); 548 pgadjustjobc(hispgrp, entering); 549 } 550} 551 552/* 553 * A process group has become orphaned; 554 * if there are any stopped processes in the group, 555 * hang-up all process in that group. 556 */ 557static void 558orphanpg(pg) 559 struct pgrp *pg; 560{ 561 register struct proc *p; 562 563 PGRP_LOCK_ASSERT(pg, MA_OWNED); 564 565 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 566 PROC_LOCK(p); 567 if (P_SHOULDSTOP(p)) { 568 PROC_UNLOCK(p); 569 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 570 PROC_LOCK(p); 571 psignal(p, SIGHUP); 572 psignal(p, SIGCONT); 573 PROC_UNLOCK(p); 574 } 575 return; 576 } 577 PROC_UNLOCK(p); 578 } 579} 580 581#include "opt_ddb.h" 582#ifdef DDB 583#include <ddb/ddb.h> 584 585DB_SHOW_COMMAND(pgrpdump, pgrpdump) 586{ 587 register struct pgrp *pgrp; 588 register struct proc *p; 589 register int i; 590 591 for (i = 0; i <= pgrphash; i++) { 592 if (!LIST_EMPTY(&pgrphashtbl[i])) { 593 printf("\tindx %d\n", i); 594 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 595 printf( 596 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 597 (void *)pgrp, (long)pgrp->pg_id, 598 (void *)pgrp->pg_session, 599 pgrp->pg_session->s_count, 600 (void *)LIST_FIRST(&pgrp->pg_members)); 601 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 602 printf("\t\tpid %ld addr %p pgrp %p\n", 603 (long)p->p_pid, (void *)p, 604 (void *)p->p_pgrp); 605 } 606 } 607 } 608 } 609} 610#endif /* DDB */ 611void 612fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp); 613 614/* 615 * Fill in a kinfo_proc structure for the specified process. 616 * Must be called with the target process locked. 617 */ 618void 619fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 620{ 621 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp); 622} 623 624void 625fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp) 626{ 627 struct proc *p; 628 struct thread *td0; 629 struct kse *ke; 630 struct ksegrp *kg; 631 struct tty *tp; 632 struct session *sp; 633 struct timeval tv; 634 struct sigacts *ps; 635 636 p = td->td_proc; 637 638 bzero(kp, sizeof(*kp)); 639 640 kp->ki_structsize = sizeof(*kp); 641 kp->ki_paddr = p; 642 PROC_LOCK_ASSERT(p, MA_OWNED); 643 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 644 kp->ki_args = p->p_args; 645 kp->ki_textvp = p->p_textvp; 646#ifdef KTRACE 647 kp->ki_tracep = p->p_tracevp; 648 mtx_lock(&ktrace_mtx); 649 kp->ki_traceflag = p->p_traceflag; 650 mtx_unlock(&ktrace_mtx); 651#endif 652 kp->ki_fd = p->p_fd; 653 kp->ki_vmspace = p->p_vmspace; 654 if (p->p_ucred) { 655 kp->ki_uid = p->p_ucred->cr_uid; 656 kp->ki_ruid = p->p_ucred->cr_ruid; 657 kp->ki_svuid = p->p_ucred->cr_svuid; 658 /* XXX bde doesn't like KI_NGROUPS */ 659 kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS); 660 bcopy(p->p_ucred->cr_groups, kp->ki_groups, 661 kp->ki_ngroups * sizeof(gid_t)); 662 kp->ki_rgid = p->p_ucred->cr_rgid; 663 kp->ki_svgid = p->p_ucred->cr_svgid; 664 } 665 if (p->p_sigacts) { 666 ps = p->p_sigacts; 667 mtx_lock(&ps->ps_mtx); 668 kp->ki_sigignore = ps->ps_sigignore; 669 kp->ki_sigcatch = ps->ps_sigcatch; 670 mtx_unlock(&ps->ps_mtx); 671 } 672 mtx_lock_spin(&sched_lock); 673 if (p->p_state != PRS_NEW && 674 p->p_state != PRS_ZOMBIE && 675 p->p_vmspace != NULL) { 676 struct vmspace *vm = p->p_vmspace; 677 678 kp->ki_size = vm->vm_map.size; 679 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 680 if (p->p_sflag & PS_INMEM) 681 kp->ki_rssize += UAREA_PAGES; 682 FOREACH_THREAD_IN_PROC(p, td0) { 683 if (!TD_IS_SWAPPED(td0)) 684 kp->ki_rssize += td0->td_kstack_pages; 685 if (td0->td_altkstack_obj != NULL) 686 kp->ki_rssize += td0->td_altkstack_pages; 687 } 688 kp->ki_swrss = vm->vm_swrss; 689 kp->ki_tsize = vm->vm_tsize; 690 kp->ki_dsize = vm->vm_dsize; 691 kp->ki_ssize = vm->vm_ssize; 692 } 693 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 694 kp->ki_start = p->p_stats->p_start; 695 timevaladd(&kp->ki_start, &boottime); 696 kp->ki_rusage = p->p_stats->p_ru; 697 kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec + 698 p->p_stats->p_cru.ru_stime.tv_sec; 699 kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec + 700 p->p_stats->p_cru.ru_stime.tv_usec; 701 } 702 if (p->p_state != PRS_ZOMBIE) { 703#if 0 704 if (td == NULL) { 705 /* XXXKSE: This should never happen. */ 706 printf("fill_kinfo_proc(): pid %d has no threads!\n", 707 p->p_pid); 708 mtx_unlock_spin(&sched_lock); 709 return; 710 } 711#endif 712 if (td->td_wmesg != NULL) { 713 strlcpy(kp->ki_wmesg, td->td_wmesg, 714 sizeof(kp->ki_wmesg)); 715 } 716 if (TD_ON_LOCK(td)) { 717 kp->ki_kiflag |= KI_LOCKBLOCK; 718 strlcpy(kp->ki_lockname, td->td_lockname, 719 sizeof(kp->ki_lockname)); 720 } 721 722 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 723 if (TD_ON_RUNQ(td) || 724 TD_CAN_RUN(td) || 725 TD_IS_RUNNING(td)) { 726 kp->ki_stat = SRUN; 727 } else if (P_SHOULDSTOP(p)) { 728 kp->ki_stat = SSTOP; 729 } else if (TD_IS_SLEEPING(td)) { 730 kp->ki_stat = SSLEEP; 731 } else if (TD_ON_LOCK(td)) { 732 kp->ki_stat = SLOCK; 733 } else { 734 kp->ki_stat = SWAIT; 735 } 736 } else { 737 kp->ki_stat = SIDL; 738 } 739 740 kp->ki_sflag = p->p_sflag; 741 kp->ki_swtime = p->p_swtime; 742 kp->ki_pid = p->p_pid; 743 kp->ki_nice = p->p_nice; 744 kg = td->td_ksegrp; 745 ke = td->td_kse; 746 bintime2timeval(&p->p_runtime, &tv); 747 kp->ki_runtime = 748 tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec; 749 750 /* things in the KSE GROUP */ 751 kp->ki_estcpu = kg->kg_estcpu; 752 kp->ki_slptime = kg->kg_slptime; 753 kp->ki_pri.pri_user = kg->kg_user_pri; 754 kp->ki_pri.pri_class = kg->kg_pri_class; 755 756 /* Things in the thread */ 757 kp->ki_wchan = td->td_wchan; 758 kp->ki_pri.pri_level = td->td_priority; 759 kp->ki_pri.pri_native = td->td_base_pri; 760 kp->ki_lastcpu = td->td_lastcpu; 761 kp->ki_oncpu = td->td_oncpu; 762 kp->ki_tdflags = td->td_flags; 763 kp->ki_pcb = td->td_pcb; 764 kp->ki_kstack = (void *)td->td_kstack; 765 kp->ki_pctcpu = sched_pctcpu(td); 766 767 /* Things in the kse */ 768 if (ke) 769 kp->ki_rqindex = ke->ke_rqindex; 770 else 771 kp->ki_rqindex = 0; 772 773 } else { 774 kp->ki_stat = SZOMB; 775 } 776 mtx_unlock_spin(&sched_lock); 777 sp = NULL; 778 tp = NULL; 779 if (p->p_pgrp) { 780 kp->ki_pgid = p->p_pgrp->pg_id; 781 kp->ki_jobc = p->p_pgrp->pg_jobc; 782 sp = p->p_pgrp->pg_session; 783 784 if (sp != NULL) { 785 kp->ki_sid = sp->s_sid; 786 SESS_LOCK(sp); 787 strlcpy(kp->ki_login, sp->s_login, 788 sizeof(kp->ki_login)); 789 if (sp->s_ttyvp) 790 kp->ki_kiflag |= KI_CTTY; 791 if (SESS_LEADER(p)) 792 kp->ki_kiflag |= KI_SLEADER; 793 tp = sp->s_ttyp; 794 SESS_UNLOCK(sp); 795 } 796 } 797 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 798 kp->ki_tdev = dev2udev(tp->t_dev); 799 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 800 if (tp->t_session) 801 kp->ki_tsid = tp->t_session->s_sid; 802 } else
| 35 36#include "opt_ktrace.h" 37#include "opt_kstack_pages.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/sysent.h> 47#include <sys/sched.h> 48#include <sys/smp.h> 49#include <sys/sysctl.h> 50#include <sys/filedesc.h> 51#include <sys/tty.h> 52#include <sys/signalvar.h> 53#include <sys/sx.h> 54#include <sys/user.h> 55#include <sys/jail.h> 56#ifdef KTRACE 57#include <sys/uio.h> 58#include <sys/ktrace.h> 59#endif 60 61#include <vm/vm.h> 62#include <vm/vm_extern.h> 63#include <vm/pmap.h> 64#include <vm/vm_map.h> 65#include <vm/uma.h> 66#include <machine/critical.h> 67 68MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 69MALLOC_DEFINE(M_SESSION, "session", "session header"); 70static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 71MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 72 73static void doenterpgrp(struct proc *, struct pgrp *); 74static void orphanpg(struct pgrp *pg); 75static void pgadjustjobc(struct pgrp *pgrp, int entering); 76static void pgdelete(struct pgrp *); 77static void proc_ctor(void *mem, int size, void *arg); 78static void proc_dtor(void *mem, int size, void *arg); 79static void proc_init(void *mem, int size); 80static void proc_fini(void *mem, int size); 81 82/* 83 * Other process lists 84 */ 85struct pidhashhead *pidhashtbl; 86u_long pidhash; 87struct pgrphashhead *pgrphashtbl; 88u_long pgrphash; 89struct proclist allproc; 90struct proclist zombproc; 91struct sx allproc_lock; 92struct sx proctree_lock; 93struct mtx pargs_ref_lock; 94struct mtx ppeers_lock; 95uma_zone_t proc_zone; 96uma_zone_t ithread_zone; 97 98int kstack_pages = KSTACK_PAGES; 99int uarea_pages = UAREA_PAGES; 100SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 101SYSCTL_INT(_kern, OID_AUTO, uarea_pages, CTLFLAG_RD, &uarea_pages, 0, ""); 102 103#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 104 105CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 106 107/* 108 * Initialize global process hashing structures. 109 */ 110void 111procinit() 112{ 113 114 sx_init(&allproc_lock, "allproc"); 115 sx_init(&proctree_lock, "proctree"); 116 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF); 117 mtx_init(&ppeers_lock, "p_peers", 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", sched_sizeof_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 */ 131static void 132proc_ctor(void *mem, int size, void *arg) 133{ 134 struct proc *p; 135 136 p = (struct proc *)mem; 137} 138 139/* 140 * Reclaim a proc after use. 141 */ 142static void 143proc_dtor(void *mem, int size, void *arg) 144{ 145 struct proc *p; 146 struct thread *td; 147 struct ksegrp *kg; 148 struct kse *ke; 149 150 /* INVARIANTS checks go here */ 151 p = (struct proc *)mem; 152 KASSERT((p->p_numthreads == 1), 153 ("bad number of threads in exiting process")); 154 td = FIRST_THREAD_IN_PROC(p); 155 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 156 kg = FIRST_KSEGRP_IN_PROC(p); 157 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 158 ke = FIRST_KSE_IN_KSEGRP(kg); 159 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 160 161 /* Dispose of an alternate kstack, if it exists. 162 * XXX What if there are more than one thread in the proc? 163 * The first thread in the proc is special and not 164 * freed, so you gotta do this here. 165 */ 166 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0)) 167 vm_thread_dispose_altkstack(td); 168 169 /* 170 * We want to make sure we know the initial linkages. 171 * so for now tear them down and remake them. 172 * This is probably un-needed as we can probably rely 173 * on the state coming in here from wait4(). 174 */ 175 proc_linkup(p, kg, ke, td); 176} 177 178/* 179 * Initialize type-stable parts of a proc (when newly created). 180 */ 181static void 182proc_init(void *mem, int size) 183{ 184 struct proc *p; 185 struct thread *td; 186 struct ksegrp *kg; 187 struct kse *ke; 188 189 p = (struct proc *)mem; 190 p->p_sched = (struct p_sched *)&p[1]; 191 vm_proc_new(p); 192 td = thread_alloc(); 193 ke = kse_alloc(); 194 kg = ksegrp_alloc(); 195 proc_linkup(p, kg, ke, td); 196 bzero(&p->p_mtx, sizeof(struct mtx)); 197 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 198} 199 200/* 201 * Tear down type-stable parts of a proc (just before being discarded) 202 */ 203static void 204proc_fini(void *mem, int size) 205{ 206 struct proc *p; 207 struct thread *td; 208 struct ksegrp *kg; 209 struct kse *ke; 210 211 p = (struct proc *)mem; 212 KASSERT((p->p_numthreads == 1), 213 ("bad number of threads in freeing process")); 214 td = FIRST_THREAD_IN_PROC(p); 215 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 216 kg = FIRST_KSEGRP_IN_PROC(p); 217 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 218 ke = FIRST_KSE_IN_KSEGRP(kg); 219 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 220 vm_proc_dispose(p); 221 thread_free(td); 222 ksegrp_free(kg); 223 kse_free(ke); 224 mtx_destroy(&p->p_mtx); 225} 226 227/* 228 * Is p an inferior of the current process? 229 */ 230int 231inferior(p) 232 register struct proc *p; 233{ 234 235 sx_assert(&proctree_lock, SX_LOCKED); 236 for (; p != curproc; p = p->p_pptr) 237 if (p->p_pid == 0) 238 return (0); 239 return (1); 240} 241 242/* 243 * Locate a process by number 244 */ 245struct proc * 246pfind(pid) 247 register pid_t pid; 248{ 249 register struct proc *p; 250 251 sx_slock(&allproc_lock); 252 LIST_FOREACH(p, PIDHASH(pid), p_hash) 253 if (p->p_pid == pid) { 254 PROC_LOCK(p); 255 break; 256 } 257 sx_sunlock(&allproc_lock); 258 return (p); 259} 260 261/* 262 * Locate a process group by number. 263 * The caller must hold proctree_lock. 264 */ 265struct pgrp * 266pgfind(pgid) 267 register pid_t pgid; 268{ 269 register struct pgrp *pgrp; 270 271 sx_assert(&proctree_lock, SX_LOCKED); 272 273 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 274 if (pgrp->pg_id == pgid) { 275 PGRP_LOCK(pgrp); 276 return (pgrp); 277 } 278 } 279 return (NULL); 280} 281 282/* 283 * Create a new process group. 284 * pgid must be equal to the pid of p. 285 * Begin a new session if required. 286 */ 287int 288enterpgrp(p, pgid, pgrp, sess) 289 register struct proc *p; 290 pid_t pgid; 291 struct pgrp *pgrp; 292 struct session *sess; 293{ 294 struct pgrp *pgrp2; 295 296 sx_assert(&proctree_lock, SX_XLOCKED); 297 298 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 299 KASSERT(p->p_pid == pgid, 300 ("enterpgrp: new pgrp and pid != pgid")); 301 302 pgrp2 = pgfind(pgid); 303 304 KASSERT(pgrp2 == NULL, 305 ("enterpgrp: pgrp with pgid exists")); 306 KASSERT(!SESS_LEADER(p), 307 ("enterpgrp: session leader attempted setpgrp")); 308 309 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 310 311 if (sess != NULL) { 312 /* 313 * new session 314 */ 315 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 316 PROC_LOCK(p); 317 p->p_flag &= ~P_CONTROLT; 318 PROC_UNLOCK(p); 319 PGRP_LOCK(pgrp); 320 sess->s_leader = p; 321 sess->s_sid = p->p_pid; 322 sess->s_count = 1; 323 sess->s_ttyvp = NULL; 324 sess->s_ttyp = NULL; 325 bcopy(p->p_session->s_login, sess->s_login, 326 sizeof(sess->s_login)); 327 pgrp->pg_session = sess; 328 KASSERT(p == curproc, 329 ("enterpgrp: mksession and p != curproc")); 330 } else { 331 pgrp->pg_session = p->p_session; 332 SESS_LOCK(pgrp->pg_session); 333 pgrp->pg_session->s_count++; 334 SESS_UNLOCK(pgrp->pg_session); 335 PGRP_LOCK(pgrp); 336 } 337 pgrp->pg_id = pgid; 338 LIST_INIT(&pgrp->pg_members); 339 340 /* 341 * As we have an exclusive lock of proctree_lock, 342 * this should not deadlock. 343 */ 344 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 345 pgrp->pg_jobc = 0; 346 SLIST_INIT(&pgrp->pg_sigiolst); 347 PGRP_UNLOCK(pgrp); 348 349 doenterpgrp(p, pgrp); 350 351 return (0); 352} 353 354/* 355 * Move p to an existing process group 356 */ 357int 358enterthispgrp(p, pgrp) 359 register struct proc *p; 360 struct pgrp *pgrp; 361{ 362 363 sx_assert(&proctree_lock, SX_XLOCKED); 364 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 365 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 366 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 367 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 368 KASSERT(pgrp->pg_session == p->p_session, 369 ("%s: pgrp's session %p, p->p_session %p.\n", 370 __func__, 371 pgrp->pg_session, 372 p->p_session)); 373 KASSERT(pgrp != p->p_pgrp, 374 ("%s: p belongs to pgrp.", __func__)); 375 376 doenterpgrp(p, pgrp); 377 378 return (0); 379} 380 381/* 382 * Move p to a process group 383 */ 384static void 385doenterpgrp(p, pgrp) 386 struct proc *p; 387 struct pgrp *pgrp; 388{ 389 struct pgrp *savepgrp; 390 391 sx_assert(&proctree_lock, SX_XLOCKED); 392 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 393 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 394 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 395 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 396 397 savepgrp = p->p_pgrp; 398 399 /* 400 * Adjust eligibility of affected pgrps to participate in job control. 401 * Increment eligibility counts before decrementing, otherwise we 402 * could reach 0 spuriously during the first call. 403 */ 404 fixjobc(p, pgrp, 1); 405 fixjobc(p, p->p_pgrp, 0); 406 407 PGRP_LOCK(pgrp); 408 PGRP_LOCK(savepgrp); 409 PROC_LOCK(p); 410 LIST_REMOVE(p, p_pglist); 411 p->p_pgrp = pgrp; 412 PROC_UNLOCK(p); 413 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 414 PGRP_UNLOCK(savepgrp); 415 PGRP_UNLOCK(pgrp); 416 if (LIST_EMPTY(&savepgrp->pg_members)) 417 pgdelete(savepgrp); 418} 419 420/* 421 * remove process from process group 422 */ 423int 424leavepgrp(p) 425 register struct proc *p; 426{ 427 struct pgrp *savepgrp; 428 429 sx_assert(&proctree_lock, SX_XLOCKED); 430 savepgrp = p->p_pgrp; 431 PGRP_LOCK(savepgrp); 432 PROC_LOCK(p); 433 LIST_REMOVE(p, p_pglist); 434 p->p_pgrp = NULL; 435 PROC_UNLOCK(p); 436 PGRP_UNLOCK(savepgrp); 437 if (LIST_EMPTY(&savepgrp->pg_members)) 438 pgdelete(savepgrp); 439 return (0); 440} 441 442/* 443 * delete a process group 444 */ 445static void 446pgdelete(pgrp) 447 register struct pgrp *pgrp; 448{ 449 struct session *savesess; 450 int i; 451 452 sx_assert(&proctree_lock, SX_XLOCKED); 453 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 454 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 455 456 /* 457 * Reset any sigio structures pointing to us as a result of 458 * F_SETOWN with our pgid. 459 */ 460 funsetownlst(&pgrp->pg_sigiolst); 461 462 PGRP_LOCK(pgrp); 463 if (pgrp->pg_session->s_ttyp != NULL && 464 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 465 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 466 LIST_REMOVE(pgrp, pg_hash); 467 savesess = pgrp->pg_session; 468 SESS_LOCK(savesess); 469 i = --savesess->s_count; 470 SESS_UNLOCK(savesess); 471 PGRP_UNLOCK(pgrp); 472 if (i == 0) { 473 if (savesess->s_ttyp != NULL) 474 ttyrel(savesess->s_ttyp); 475 mtx_destroy(&savesess->s_mtx); 476 FREE(savesess, M_SESSION); 477 } 478 mtx_destroy(&pgrp->pg_mtx); 479 FREE(pgrp, M_PGRP); 480} 481 482static void 483pgadjustjobc(pgrp, entering) 484 struct pgrp *pgrp; 485 int entering; 486{ 487 488 PGRP_LOCK(pgrp); 489 if (entering) 490 pgrp->pg_jobc++; 491 else { 492 --pgrp->pg_jobc; 493 if (pgrp->pg_jobc == 0) 494 orphanpg(pgrp); 495 } 496 PGRP_UNLOCK(pgrp); 497} 498 499/* 500 * Adjust pgrp jobc counters when specified process changes process group. 501 * We count the number of processes in each process group that "qualify" 502 * the group for terminal job control (those with a parent in a different 503 * process group of the same session). If that count reaches zero, the 504 * process group becomes orphaned. Check both the specified process' 505 * process group and that of its children. 506 * entering == 0 => p is leaving specified group. 507 * entering == 1 => p is entering specified group. 508 */ 509void 510fixjobc(p, pgrp, entering) 511 register struct proc *p; 512 register struct pgrp *pgrp; 513 int entering; 514{ 515 register struct pgrp *hispgrp; 516 register struct session *mysession; 517 518 sx_assert(&proctree_lock, SX_LOCKED); 519 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 520 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 521 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 522 523 /* 524 * Check p's parent to see whether p qualifies its own process 525 * group; if so, adjust count for p's process group. 526 */ 527 mysession = pgrp->pg_session; 528 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 529 hispgrp->pg_session == mysession) 530 pgadjustjobc(pgrp, entering); 531 532 /* 533 * Check this process' children to see whether they qualify 534 * their process groups; if so, adjust counts for children's 535 * process groups. 536 */ 537 LIST_FOREACH(p, &p->p_children, p_sibling) { 538 hispgrp = p->p_pgrp; 539 if (hispgrp == pgrp || 540 hispgrp->pg_session != mysession) 541 continue; 542 PROC_LOCK(p); 543 if (p->p_state == PRS_ZOMBIE) { 544 PROC_UNLOCK(p); 545 continue; 546 } 547 PROC_UNLOCK(p); 548 pgadjustjobc(hispgrp, entering); 549 } 550} 551 552/* 553 * A process group has become orphaned; 554 * if there are any stopped processes in the group, 555 * hang-up all process in that group. 556 */ 557static void 558orphanpg(pg) 559 struct pgrp *pg; 560{ 561 register struct proc *p; 562 563 PGRP_LOCK_ASSERT(pg, MA_OWNED); 564 565 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 566 PROC_LOCK(p); 567 if (P_SHOULDSTOP(p)) { 568 PROC_UNLOCK(p); 569 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 570 PROC_LOCK(p); 571 psignal(p, SIGHUP); 572 psignal(p, SIGCONT); 573 PROC_UNLOCK(p); 574 } 575 return; 576 } 577 PROC_UNLOCK(p); 578 } 579} 580 581#include "opt_ddb.h" 582#ifdef DDB 583#include <ddb/ddb.h> 584 585DB_SHOW_COMMAND(pgrpdump, pgrpdump) 586{ 587 register struct pgrp *pgrp; 588 register struct proc *p; 589 register int i; 590 591 for (i = 0; i <= pgrphash; i++) { 592 if (!LIST_EMPTY(&pgrphashtbl[i])) { 593 printf("\tindx %d\n", i); 594 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 595 printf( 596 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 597 (void *)pgrp, (long)pgrp->pg_id, 598 (void *)pgrp->pg_session, 599 pgrp->pg_session->s_count, 600 (void *)LIST_FIRST(&pgrp->pg_members)); 601 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 602 printf("\t\tpid %ld addr %p pgrp %p\n", 603 (long)p->p_pid, (void *)p, 604 (void *)p->p_pgrp); 605 } 606 } 607 } 608 } 609} 610#endif /* DDB */ 611void 612fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp); 613 614/* 615 * Fill in a kinfo_proc structure for the specified process. 616 * Must be called with the target process locked. 617 */ 618void 619fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 620{ 621 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp); 622} 623 624void 625fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp) 626{ 627 struct proc *p; 628 struct thread *td0; 629 struct kse *ke; 630 struct ksegrp *kg; 631 struct tty *tp; 632 struct session *sp; 633 struct timeval tv; 634 struct sigacts *ps; 635 636 p = td->td_proc; 637 638 bzero(kp, sizeof(*kp)); 639 640 kp->ki_structsize = sizeof(*kp); 641 kp->ki_paddr = p; 642 PROC_LOCK_ASSERT(p, MA_OWNED); 643 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 644 kp->ki_args = p->p_args; 645 kp->ki_textvp = p->p_textvp; 646#ifdef KTRACE 647 kp->ki_tracep = p->p_tracevp; 648 mtx_lock(&ktrace_mtx); 649 kp->ki_traceflag = p->p_traceflag; 650 mtx_unlock(&ktrace_mtx); 651#endif 652 kp->ki_fd = p->p_fd; 653 kp->ki_vmspace = p->p_vmspace; 654 if (p->p_ucred) { 655 kp->ki_uid = p->p_ucred->cr_uid; 656 kp->ki_ruid = p->p_ucred->cr_ruid; 657 kp->ki_svuid = p->p_ucred->cr_svuid; 658 /* XXX bde doesn't like KI_NGROUPS */ 659 kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS); 660 bcopy(p->p_ucred->cr_groups, kp->ki_groups, 661 kp->ki_ngroups * sizeof(gid_t)); 662 kp->ki_rgid = p->p_ucred->cr_rgid; 663 kp->ki_svgid = p->p_ucred->cr_svgid; 664 } 665 if (p->p_sigacts) { 666 ps = p->p_sigacts; 667 mtx_lock(&ps->ps_mtx); 668 kp->ki_sigignore = ps->ps_sigignore; 669 kp->ki_sigcatch = ps->ps_sigcatch; 670 mtx_unlock(&ps->ps_mtx); 671 } 672 mtx_lock_spin(&sched_lock); 673 if (p->p_state != PRS_NEW && 674 p->p_state != PRS_ZOMBIE && 675 p->p_vmspace != NULL) { 676 struct vmspace *vm = p->p_vmspace; 677 678 kp->ki_size = vm->vm_map.size; 679 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 680 if (p->p_sflag & PS_INMEM) 681 kp->ki_rssize += UAREA_PAGES; 682 FOREACH_THREAD_IN_PROC(p, td0) { 683 if (!TD_IS_SWAPPED(td0)) 684 kp->ki_rssize += td0->td_kstack_pages; 685 if (td0->td_altkstack_obj != NULL) 686 kp->ki_rssize += td0->td_altkstack_pages; 687 } 688 kp->ki_swrss = vm->vm_swrss; 689 kp->ki_tsize = vm->vm_tsize; 690 kp->ki_dsize = vm->vm_dsize; 691 kp->ki_ssize = vm->vm_ssize; 692 } 693 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 694 kp->ki_start = p->p_stats->p_start; 695 timevaladd(&kp->ki_start, &boottime); 696 kp->ki_rusage = p->p_stats->p_ru; 697 kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec + 698 p->p_stats->p_cru.ru_stime.tv_sec; 699 kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec + 700 p->p_stats->p_cru.ru_stime.tv_usec; 701 } 702 if (p->p_state != PRS_ZOMBIE) { 703#if 0 704 if (td == NULL) { 705 /* XXXKSE: This should never happen. */ 706 printf("fill_kinfo_proc(): pid %d has no threads!\n", 707 p->p_pid); 708 mtx_unlock_spin(&sched_lock); 709 return; 710 } 711#endif 712 if (td->td_wmesg != NULL) { 713 strlcpy(kp->ki_wmesg, td->td_wmesg, 714 sizeof(kp->ki_wmesg)); 715 } 716 if (TD_ON_LOCK(td)) { 717 kp->ki_kiflag |= KI_LOCKBLOCK; 718 strlcpy(kp->ki_lockname, td->td_lockname, 719 sizeof(kp->ki_lockname)); 720 } 721 722 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 723 if (TD_ON_RUNQ(td) || 724 TD_CAN_RUN(td) || 725 TD_IS_RUNNING(td)) { 726 kp->ki_stat = SRUN; 727 } else if (P_SHOULDSTOP(p)) { 728 kp->ki_stat = SSTOP; 729 } else if (TD_IS_SLEEPING(td)) { 730 kp->ki_stat = SSLEEP; 731 } else if (TD_ON_LOCK(td)) { 732 kp->ki_stat = SLOCK; 733 } else { 734 kp->ki_stat = SWAIT; 735 } 736 } else { 737 kp->ki_stat = SIDL; 738 } 739 740 kp->ki_sflag = p->p_sflag; 741 kp->ki_swtime = p->p_swtime; 742 kp->ki_pid = p->p_pid; 743 kp->ki_nice = p->p_nice; 744 kg = td->td_ksegrp; 745 ke = td->td_kse; 746 bintime2timeval(&p->p_runtime, &tv); 747 kp->ki_runtime = 748 tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec; 749 750 /* things in the KSE GROUP */ 751 kp->ki_estcpu = kg->kg_estcpu; 752 kp->ki_slptime = kg->kg_slptime; 753 kp->ki_pri.pri_user = kg->kg_user_pri; 754 kp->ki_pri.pri_class = kg->kg_pri_class; 755 756 /* Things in the thread */ 757 kp->ki_wchan = td->td_wchan; 758 kp->ki_pri.pri_level = td->td_priority; 759 kp->ki_pri.pri_native = td->td_base_pri; 760 kp->ki_lastcpu = td->td_lastcpu; 761 kp->ki_oncpu = td->td_oncpu; 762 kp->ki_tdflags = td->td_flags; 763 kp->ki_pcb = td->td_pcb; 764 kp->ki_kstack = (void *)td->td_kstack; 765 kp->ki_pctcpu = sched_pctcpu(td); 766 767 /* Things in the kse */ 768 if (ke) 769 kp->ki_rqindex = ke->ke_rqindex; 770 else 771 kp->ki_rqindex = 0; 772 773 } else { 774 kp->ki_stat = SZOMB; 775 } 776 mtx_unlock_spin(&sched_lock); 777 sp = NULL; 778 tp = NULL; 779 if (p->p_pgrp) { 780 kp->ki_pgid = p->p_pgrp->pg_id; 781 kp->ki_jobc = p->p_pgrp->pg_jobc; 782 sp = p->p_pgrp->pg_session; 783 784 if (sp != NULL) { 785 kp->ki_sid = sp->s_sid; 786 SESS_LOCK(sp); 787 strlcpy(kp->ki_login, sp->s_login, 788 sizeof(kp->ki_login)); 789 if (sp->s_ttyvp) 790 kp->ki_kiflag |= KI_CTTY; 791 if (SESS_LEADER(p)) 792 kp->ki_kiflag |= KI_SLEADER; 793 tp = sp->s_ttyp; 794 SESS_UNLOCK(sp); 795 } 796 } 797 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 798 kp->ki_tdev = dev2udev(tp->t_dev); 799 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 800 if (tp->t_session) 801 kp->ki_tsid = tp->t_session->s_sid; 802 } else
|
1016 SESS_UNLOCK(p->p_session); 1017 PROC_UNLOCK(p); 1018 continue; 1019 } 1020 SESS_UNLOCK(p->p_session); 1021 break; 1022 1023 case KERN_PROC_UID: 1024 if (p->p_ucred == NULL || 1025 p->p_ucred->cr_uid != (uid_t)name[0]) { 1026 PROC_UNLOCK(p); 1027 continue; 1028 } 1029 break; 1030 1031 case KERN_PROC_RUID: 1032 if (p->p_ucred == NULL || 1033 p->p_ucred->cr_ruid != (uid_t)name[0]) { 1034 PROC_UNLOCK(p); 1035 continue; 1036 } 1037 break; 1038 1039 case KERN_PROC_PROC: 1040 break; 1041 1042 default: 1043 break; 1044 1045 } 1046 1047 error = sysctl_out_proc(p, req, flags | doingzomb); 1048 if (error) { 1049 sx_sunlock(&allproc_lock); 1050 return (error); 1051 } 1052 } 1053 } 1054 sx_sunlock(&allproc_lock); 1055 return (0); 1056} 1057 1058struct pargs * 1059pargs_alloc(int len) 1060{ 1061 struct pargs *pa; 1062 1063 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1064 M_WAITOK); 1065 pa->ar_ref = 1; 1066 pa->ar_length = len; 1067 return (pa); 1068} 1069 1070void 1071pargs_free(struct pargs *pa) 1072{ 1073 1074 FREE(pa, M_PARGS); 1075} 1076 1077void 1078pargs_hold(struct pargs *pa) 1079{ 1080 1081 if (pa == NULL) 1082 return; 1083 PARGS_LOCK(pa); 1084 pa->ar_ref++; 1085 PARGS_UNLOCK(pa); 1086} 1087 1088void 1089pargs_drop(struct pargs *pa) 1090{ 1091 1092 if (pa == NULL) 1093 return; 1094 PARGS_LOCK(pa); 1095 if (--pa->ar_ref == 0) { 1096 PARGS_UNLOCK(pa); 1097 pargs_free(pa); 1098 } else 1099 PARGS_UNLOCK(pa); 1100} 1101 1102/* 1103 * This sysctl allows a process to retrieve the argument list or process 1104 * title for another process without groping around in the address space 1105 * of the other process. It also allow a process to set its own "process 1106 * title to a string of its own choice. 1107 */ 1108static int 1109sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1110{ 1111 int *name = (int*) arg1; 1112 u_int namelen = arg2; 1113 struct pargs *newpa, *pa; 1114 struct proc *p; 1115 int error = 0; 1116 1117 if (namelen != 1) 1118 return (EINVAL); 1119 1120 p = pfind((pid_t)name[0]); 1121 if (!p) 1122 return (ESRCH); 1123 1124 if ((error = p_cansee(curthread, p)) != 0) { 1125 PROC_UNLOCK(p); 1126 return (error); 1127 } 1128 1129 if (req->newptr && curproc != p) { 1130 PROC_UNLOCK(p); 1131 return (EPERM); 1132 } 1133 1134 pa = p->p_args; 1135 pargs_hold(pa); 1136 PROC_UNLOCK(p); 1137 if (req->oldptr != NULL && pa != NULL) 1138 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1139 pargs_drop(pa); 1140 if (error != 0 || req->newptr == NULL) 1141 return (error); 1142 1143 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1144 return (ENOMEM); 1145 newpa = pargs_alloc(req->newlen); 1146 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1147 if (error != 0) { 1148 pargs_free(newpa); 1149 return (error); 1150 } 1151 PROC_LOCK(p); 1152 pa = p->p_args; 1153 p->p_args = newpa; 1154 PROC_UNLOCK(p); 1155 pargs_drop(pa); 1156 return (0); 1157} 1158 1159static int 1160sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1161{ 1162 struct proc *p; 1163 char *sv_name; 1164 int *name; 1165 int namelen; 1166 int error; 1167 1168 namelen = arg2; 1169 if (namelen != 1) 1170 return (EINVAL); 1171 1172 name = (int *)arg1; 1173 if ((p = pfind((pid_t)name[0])) == NULL) 1174 return (ESRCH); 1175 if ((error = p_cansee(curthread, p))) { 1176 PROC_UNLOCK(p); 1177 return (error); 1178 } 1179 sv_name = p->p_sysent->sv_name; 1180 PROC_UNLOCK(p); 1181 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1182} 1183 1184 1185SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1186 1187SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1188 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1189 1190SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1191 sysctl_kern_proc, "Process table"); 1192 1193SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD, 1194 sysctl_kern_proc, "Process table"); 1195 1196SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD, 1197 sysctl_kern_proc, "Process table"); 1198 1199SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1200 sysctl_kern_proc, "Process table"); 1201 1202SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1203 sysctl_kern_proc, "Process table"); 1204 1205SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1206 sysctl_kern_proc, "Process table"); 1207 1208SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1209 sysctl_kern_proc, "Process table"); 1210 1211SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD, 1212 sysctl_kern_proc, "Return process table, no threads"); 1213 1214SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1215 sysctl_kern_proc_args, "Process argument list"); 1216 1217SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD, 1218 sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); 1219 1220SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 1221 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1222 1223SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 1224 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1225 1226SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), sid_td, 1227 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1228 1229SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 1230 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1231 1232SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 1233 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1234 1235SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 1236 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1237 1238SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 1239 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1240 1241SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 1242 CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");
| 1016 SESS_UNLOCK(p->p_session); 1017 PROC_UNLOCK(p); 1018 continue; 1019 } 1020 SESS_UNLOCK(p->p_session); 1021 break; 1022 1023 case KERN_PROC_UID: 1024 if (p->p_ucred == NULL || 1025 p->p_ucred->cr_uid != (uid_t)name[0]) { 1026 PROC_UNLOCK(p); 1027 continue; 1028 } 1029 break; 1030 1031 case KERN_PROC_RUID: 1032 if (p->p_ucred == NULL || 1033 p->p_ucred->cr_ruid != (uid_t)name[0]) { 1034 PROC_UNLOCK(p); 1035 continue; 1036 } 1037 break; 1038 1039 case KERN_PROC_PROC: 1040 break; 1041 1042 default: 1043 break; 1044 1045 } 1046 1047 error = sysctl_out_proc(p, req, flags | doingzomb); 1048 if (error) { 1049 sx_sunlock(&allproc_lock); 1050 return (error); 1051 } 1052 } 1053 } 1054 sx_sunlock(&allproc_lock); 1055 return (0); 1056} 1057 1058struct pargs * 1059pargs_alloc(int len) 1060{ 1061 struct pargs *pa; 1062 1063 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1064 M_WAITOK); 1065 pa->ar_ref = 1; 1066 pa->ar_length = len; 1067 return (pa); 1068} 1069 1070void 1071pargs_free(struct pargs *pa) 1072{ 1073 1074 FREE(pa, M_PARGS); 1075} 1076 1077void 1078pargs_hold(struct pargs *pa) 1079{ 1080 1081 if (pa == NULL) 1082 return; 1083 PARGS_LOCK(pa); 1084 pa->ar_ref++; 1085 PARGS_UNLOCK(pa); 1086} 1087 1088void 1089pargs_drop(struct pargs *pa) 1090{ 1091 1092 if (pa == NULL) 1093 return; 1094 PARGS_LOCK(pa); 1095 if (--pa->ar_ref == 0) { 1096 PARGS_UNLOCK(pa); 1097 pargs_free(pa); 1098 } else 1099 PARGS_UNLOCK(pa); 1100} 1101 1102/* 1103 * This sysctl allows a process to retrieve the argument list or process 1104 * title for another process without groping around in the address space 1105 * of the other process. It also allow a process to set its own "process 1106 * title to a string of its own choice. 1107 */ 1108static int 1109sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1110{ 1111 int *name = (int*) arg1; 1112 u_int namelen = arg2; 1113 struct pargs *newpa, *pa; 1114 struct proc *p; 1115 int error = 0; 1116 1117 if (namelen != 1) 1118 return (EINVAL); 1119 1120 p = pfind((pid_t)name[0]); 1121 if (!p) 1122 return (ESRCH); 1123 1124 if ((error = p_cansee(curthread, p)) != 0) { 1125 PROC_UNLOCK(p); 1126 return (error); 1127 } 1128 1129 if (req->newptr && curproc != p) { 1130 PROC_UNLOCK(p); 1131 return (EPERM); 1132 } 1133 1134 pa = p->p_args; 1135 pargs_hold(pa); 1136 PROC_UNLOCK(p); 1137 if (req->oldptr != NULL && pa != NULL) 1138 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1139 pargs_drop(pa); 1140 if (error != 0 || req->newptr == NULL) 1141 return (error); 1142 1143 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1144 return (ENOMEM); 1145 newpa = pargs_alloc(req->newlen); 1146 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1147 if (error != 0) { 1148 pargs_free(newpa); 1149 return (error); 1150 } 1151 PROC_LOCK(p); 1152 pa = p->p_args; 1153 p->p_args = newpa; 1154 PROC_UNLOCK(p); 1155 pargs_drop(pa); 1156 return (0); 1157} 1158 1159static int 1160sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1161{ 1162 struct proc *p; 1163 char *sv_name; 1164 int *name; 1165 int namelen; 1166 int error; 1167 1168 namelen = arg2; 1169 if (namelen != 1) 1170 return (EINVAL); 1171 1172 name = (int *)arg1; 1173 if ((p = pfind((pid_t)name[0])) == NULL) 1174 return (ESRCH); 1175 if ((error = p_cansee(curthread, p))) { 1176 PROC_UNLOCK(p); 1177 return (error); 1178 } 1179 sv_name = p->p_sysent->sv_name; 1180 PROC_UNLOCK(p); 1181 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1182} 1183 1184 1185SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1186 1187SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1188 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1189 1190SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1191 sysctl_kern_proc, "Process table"); 1192 1193SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD, 1194 sysctl_kern_proc, "Process table"); 1195 1196SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD, 1197 sysctl_kern_proc, "Process table"); 1198 1199SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1200 sysctl_kern_proc, "Process table"); 1201 1202SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1203 sysctl_kern_proc, "Process table"); 1204 1205SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1206 sysctl_kern_proc, "Process table"); 1207 1208SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1209 sysctl_kern_proc, "Process table"); 1210 1211SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD, 1212 sysctl_kern_proc, "Return process table, no threads"); 1213 1214SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1215 sysctl_kern_proc_args, "Process argument list"); 1216 1217SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD, 1218 sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); 1219 1220SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 1221 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1222 1223SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 1224 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1225 1226SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), sid_td, 1227 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1228 1229SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 1230 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1231 1232SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 1233 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1234 1235SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 1236 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1237 1238SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 1239 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1240 1241SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 1242 CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");
|