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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_ddb.h" 36 #include "opt_ktrace.h" 37 #include "opt_kstack_pages.h" 38 #include "opt_stack.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/mount.h> 46 #include <sys/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/refcount.h> 49 #include <sys/sbuf.h> 50 #include <sys/sysent.h> 51 #include <sys/sched.h> 52 #include <sys/smp.h> 53 #include <sys/stack.h> 54 #include <sys/sysctl.h> 55 #include <sys/filedesc.h> 56 #include <sys/tty.h> 57 #include <sys/signalvar.h> 58 #include <sys/sx.h> 59 #include <sys/user.h> 60 #include <sys/jail.h> 61 #include <sys/vnode.h> 62 #include <sys/eventhandler.h> 63 #ifdef KTRACE 64 #include <sys/uio.h> 65 #include <sys/ktrace.h> 66 #endif 67 68 #ifdef DDB 69 #include <ddb/ddb.h> 70 #endif 71 72 #include <vm/vm.h> 73 #include <vm/vm_extern.h> 74 #include <vm/pmap.h> 75 #include <vm/vm_map.h> 76 #include <vm/vm_object.h> 77 #include <vm/uma.h> 78 79 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 80 MALLOC_DEFINE(M_SESSION, "session", "session header"); 81 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 82 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 83 84 static void doenterpgrp(struct proc *, struct pgrp *); 85 static void orphanpg(struct pgrp *pg); 86 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 87 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 88 int preferthread); 89 static void pgadjustjobc(struct pgrp *pgrp, int entering); 90 static void pgdelete(struct pgrp *); 91 static int proc_ctor(void *mem, int size, void *arg, int flags); 92 static void proc_dtor(void *mem, int size, void *arg); 93 static int proc_init(void *mem, int size, int flags); 94 static void proc_fini(void *mem, int size); 95 96 /* 97 * Other process lists 98 */ 99 struct pidhashhead *pidhashtbl; 100 u_long pidhash; 101 struct pgrphashhead *pgrphashtbl; 102 u_long pgrphash; 103 struct proclist allproc; 104 struct proclist zombproc; 105 struct sx allproc_lock; 106 struct sx proctree_lock; 107 struct mtx ppeers_lock; 108 uma_zone_t proc_zone; 109 uma_zone_t ithread_zone; 110 111 int kstack_pages = KSTACK_PAGES; 112 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 113 114 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 115 116 /* 117 * Initialize global process hashing structures. 118 */ 119 void 120 procinit() 121 { 122 123 sx_init(&allproc_lock, "allproc"); 124 sx_init(&proctree_lock, "proctree"); 125 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 126 LIST_INIT(&allproc); 127 LIST_INIT(&zombproc); 128 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 129 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 130 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 131 proc_ctor, proc_dtor, proc_init, proc_fini, 132 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 133 uihashinit(); 134 } 135 136 /* 137 * Prepare a proc for use. 138 */ 139 static int 140 proc_ctor(void *mem, int size, void *arg, int flags) 141 { 142 struct proc *p; 143 144 p = (struct proc *)mem; 145 EVENTHANDLER_INVOKE(process_ctor, p); 146 return (0); 147 } 148 149 /* 150 * Reclaim a proc after use. 151 */ 152 static void 153 proc_dtor(void *mem, int size, void *arg) 154 { 155 struct proc *p; 156 struct thread *td; 157 158 /* INVARIANTS checks go here */ 159 p = (struct proc *)mem; 160 td = FIRST_THREAD_IN_PROC(p); 161 if (td != NULL) { 162 #ifdef INVARIANTS 163 KASSERT((p->p_numthreads == 1), 164 ("bad number of threads in exiting process")); 165 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 166 #endif 167 /* Dispose of an alternate kstack, if it exists. 168 * XXX What if there are more than one thread in the proc? 169 * The first thread in the proc is special and not 170 * freed, so you gotta do this here. 171 */ 172 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0)) 173 vm_thread_dispose_altkstack(td); 174 } 175 EVENTHANDLER_INVOKE(process_dtor, p); 176 if (p->p_ksi != NULL) 177 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 178 } 179 180 /* 181 * Initialize type-stable parts of a proc (when newly created). 182 */ 183 static int 184 proc_init(void *mem, int size, int flags) 185 { 186 struct proc *p; 187 188 p = (struct proc *)mem; 189 p->p_sched = (struct p_sched *)&p[1]; 190 bzero(&p->p_mtx, sizeof(struct mtx)); 191 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 192 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 193 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 194 EVENTHANDLER_INVOKE(process_init, p); 195 p->p_stats = pstats_alloc(); 196 return (0); 197 } 198 199 /* 200 * UMA should ensure that this function is never called. 201 * Freeing a proc structure would violate type stability. 202 */ 203 static void 204 proc_fini(void *mem, int size) 205 { 206 #ifdef notnow 207 struct proc *p; 208 209 p = (struct proc *)mem; 210 EVENTHANDLER_INVOKE(process_fini, p); 211 pstats_free(p->p_stats); 212 thread_free(FIRST_THREAD_IN_PROC(p)); 213 mtx_destroy(&p->p_mtx); 214 if (p->p_ksi != NULL) 215 ksiginfo_free(p->p_ksi); 216 #else 217 panic("proc reclaimed"); 218 #endif 219 } 220 221 /* 222 * Is p an inferior of the current process? 223 */ 224 int 225 inferior(p) 226 register struct proc *p; 227 { 228 229 sx_assert(&proctree_lock, SX_LOCKED); 230 for (; p != curproc; p = p->p_pptr) 231 if (p->p_pid == 0) 232 return (0); 233 return (1); 234 } 235 236 /* 237 * Locate a process by number; return only "live" processes -- i.e., neither 238 * zombies nor newly born but incompletely initialized processes. By not 239 * returning processes in the PRS_NEW state, we allow callers to avoid 240 * testing for that condition to avoid dereferencing p_ucred, et al. 241 */ 242 struct proc * 243 pfind(pid) 244 register pid_t pid; 245 { 246 register struct proc *p; 247 248 sx_slock(&allproc_lock); 249 LIST_FOREACH(p, PIDHASH(pid), p_hash) 250 if (p->p_pid == pid) { 251 if (p->p_state == PRS_NEW) { 252 p = NULL; 253 break; 254 } 255 PROC_LOCK(p); 256 break; 257 } 258 sx_sunlock(&allproc_lock); 259 return (p); 260 } 261 262 /* 263 * Locate a process group by number. 264 * The caller must hold proctree_lock. 265 */ 266 struct pgrp * 267 pgfind(pgid) 268 register pid_t pgid; 269 { 270 register struct pgrp *pgrp; 271 272 sx_assert(&proctree_lock, SX_LOCKED); 273 274 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 275 if (pgrp->pg_id == pgid) { 276 PGRP_LOCK(pgrp); 277 return (pgrp); 278 } 279 } 280 return (NULL); 281 } 282 283 /* 284 * Create a new process group. 285 * pgid must be equal to the pid of p. 286 * Begin a new session if required. 287 */ 288 int 289 enterpgrp(p, pgid, pgrp, sess) 290 register struct proc *p; 291 pid_t pgid; 292 struct pgrp *pgrp; 293 struct session *sess; 294 { 295 struct pgrp *pgrp2; 296 297 sx_assert(&proctree_lock, SX_XLOCKED); 298 299 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 300 KASSERT(p->p_pid == pgid, 301 ("enterpgrp: new pgrp and pid != pgid")); 302 303 pgrp2 = pgfind(pgid); 304 305 KASSERT(pgrp2 == NULL, 306 ("enterpgrp: pgrp with pgid exists")); 307 KASSERT(!SESS_LEADER(p), 308 ("enterpgrp: session leader attempted setpgrp")); 309 310 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 311 312 if (sess != NULL) { 313 /* 314 * new session 315 */ 316 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 317 mtx_lock(&Giant); /* XXX TTY */ 318 PROC_LOCK(p); 319 p->p_flag &= ~P_CONTROLT; 320 PROC_UNLOCK(p); 321 PGRP_LOCK(pgrp); 322 sess->s_leader = p; 323 sess->s_sid = p->p_pid; 324 sess->s_count = 1; 325 sess->s_ttyvp = NULL; 326 sess->s_ttyp = NULL; 327 bcopy(p->p_session->s_login, sess->s_login, 328 sizeof(sess->s_login)); 329 pgrp->pg_session = sess; 330 KASSERT(p == curproc, 331 ("enterpgrp: mksession and p != curproc")); 332 } else { 333 mtx_lock(&Giant); /* XXX TTY */ 334 pgrp->pg_session = p->p_session; 335 SESS_LOCK(pgrp->pg_session); 336 pgrp->pg_session->s_count++; 337 SESS_UNLOCK(pgrp->pg_session); 338 PGRP_LOCK(pgrp); 339 } 340 pgrp->pg_id = pgid; 341 LIST_INIT(&pgrp->pg_members); 342 343 /* 344 * As we have an exclusive lock of proctree_lock, 345 * this should not deadlock. 346 */ 347 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 348 pgrp->pg_jobc = 0; 349 SLIST_INIT(&pgrp->pg_sigiolst); 350 PGRP_UNLOCK(pgrp); 351 mtx_unlock(&Giant); /* XXX TTY */ 352 353 doenterpgrp(p, pgrp); 354 355 return (0); 356 } 357 358 /* 359 * Move p to an existing process group 360 */ 361 int 362 enterthispgrp(p, pgrp) 363 register struct proc *p; 364 struct pgrp *pgrp; 365 { 366 367 sx_assert(&proctree_lock, SX_XLOCKED); 368 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 369 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 370 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 371 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 372 KASSERT(pgrp->pg_session == p->p_session, 373 ("%s: pgrp's session %p, p->p_session %p.\n", 374 __func__, 375 pgrp->pg_session, 376 p->p_session)); 377 KASSERT(pgrp != p->p_pgrp, 378 ("%s: p belongs to pgrp.", __func__)); 379 380 doenterpgrp(p, pgrp); 381 382 return (0); 383 } 384 385 /* 386 * Move p to a process group 387 */ 388 static void 389 doenterpgrp(p, pgrp) 390 struct proc *p; 391 struct pgrp *pgrp; 392 { 393 struct pgrp *savepgrp; 394 395 sx_assert(&proctree_lock, SX_XLOCKED); 396 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 397 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 398 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 399 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 400 401 savepgrp = p->p_pgrp; 402 403 /* 404 * Adjust eligibility of affected pgrps to participate in job control. 405 * Increment eligibility counts before decrementing, otherwise we 406 * could reach 0 spuriously during the first call. 407 */ 408 fixjobc(p, pgrp, 1); 409 fixjobc(p, p->p_pgrp, 0); 410 411 mtx_lock(&Giant); /* XXX TTY */ 412 PGRP_LOCK(pgrp); 413 PGRP_LOCK(savepgrp); 414 PROC_LOCK(p); 415 LIST_REMOVE(p, p_pglist); 416 p->p_pgrp = pgrp; 417 PROC_UNLOCK(p); 418 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 419 PGRP_UNLOCK(savepgrp); 420 PGRP_UNLOCK(pgrp); 421 mtx_unlock(&Giant); /* XXX TTY */ 422 if (LIST_EMPTY(&savepgrp->pg_members)) 423 pgdelete(savepgrp); 424 } 425 426 /* 427 * remove process from process group 428 */ 429 int 430 leavepgrp(p) 431 register struct proc *p; 432 { 433 struct pgrp *savepgrp; 434 435 sx_assert(&proctree_lock, SX_XLOCKED); 436 savepgrp = p->p_pgrp; 437 mtx_lock(&Giant); /* XXX TTY */ 438 PGRP_LOCK(savepgrp); 439 PROC_LOCK(p); 440 LIST_REMOVE(p, p_pglist); 441 p->p_pgrp = NULL; 442 PROC_UNLOCK(p); 443 PGRP_UNLOCK(savepgrp); 444 mtx_unlock(&Giant); /* XXX TTY */ 445 if (LIST_EMPTY(&savepgrp->pg_members)) 446 pgdelete(savepgrp); 447 return (0); 448 } 449 450 /* 451 * delete a process group 452 */ 453 static void 454 pgdelete(pgrp) 455 register struct pgrp *pgrp; 456 { 457 struct session *savesess; 458 459 sx_assert(&proctree_lock, SX_XLOCKED); 460 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 461 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 462 463 /* 464 * Reset any sigio structures pointing to us as a result of 465 * F_SETOWN with our pgid. 466 */ 467 funsetownlst(&pgrp->pg_sigiolst); 468 469 mtx_lock(&Giant); /* XXX TTY */ 470 PGRP_LOCK(pgrp); 471 if (pgrp->pg_session->s_ttyp != NULL && 472 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 473 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 474 LIST_REMOVE(pgrp, pg_hash); 475 savesess = pgrp->pg_session; 476 SESSRELE(savesess); 477 PGRP_UNLOCK(pgrp); 478 mtx_destroy(&pgrp->pg_mtx); 479 FREE(pgrp, M_PGRP); 480 mtx_unlock(&Giant); /* XXX TTY */ 481 } 482 483 static void 484 pgadjustjobc(pgrp, entering) 485 struct pgrp *pgrp; 486 int entering; 487 { 488 489 PGRP_LOCK(pgrp); 490 if (entering) 491 pgrp->pg_jobc++; 492 else { 493 --pgrp->pg_jobc; 494 if (pgrp->pg_jobc == 0) 495 orphanpg(pgrp); 496 } 497 PGRP_UNLOCK(pgrp); 498 } 499 500 /* 501 * Adjust pgrp jobc counters when specified process changes process group. 502 * We count the number of processes in each process group that "qualify" 503 * the group for terminal job control (those with a parent in a different 504 * process group of the same session). If that count reaches zero, the 505 * process group becomes orphaned. Check both the specified process' 506 * process group and that of its children. 507 * entering == 0 => p is leaving specified group. 508 * entering == 1 => p is entering specified group. 509 */ 510 void 511 fixjobc(p, pgrp, entering) 512 register struct proc *p; 513 register struct pgrp *pgrp; 514 int entering; 515 { 516 register struct pgrp *hispgrp; 517 register struct session *mysession; 518 519 sx_assert(&proctree_lock, SX_LOCKED); 520 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 521 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 522 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 523 524 /* 525 * Check p's parent to see whether p qualifies its own process 526 * group; if so, adjust count for p's process group. 527 */ 528 mysession = pgrp->pg_session; 529 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 530 hispgrp->pg_session == mysession) 531 pgadjustjobc(pgrp, entering); 532 533 /* 534 * Check this process' children to see whether they qualify 535 * their process groups; if so, adjust counts for children's 536 * process groups. 537 */ 538 LIST_FOREACH(p, &p->p_children, p_sibling) { 539 hispgrp = p->p_pgrp; 540 if (hispgrp == pgrp || 541 hispgrp->pg_session != mysession) 542 continue; 543 PROC_LOCK(p); 544 if (p->p_state == PRS_ZOMBIE) { 545 PROC_UNLOCK(p); 546 continue; 547 } 548 PROC_UNLOCK(p); 549 pgadjustjobc(hispgrp, entering); 550 } 551 } 552 553 /* 554 * A process group has become orphaned; 555 * if there are any stopped processes in the group, 556 * hang-up all process in that group. 557 */ 558 static void 559 orphanpg(pg) 560 struct pgrp *pg; 561 { 562 register struct proc *p; 563 564 PGRP_LOCK_ASSERT(pg, MA_OWNED); 565 566 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 567 PROC_LOCK(p); 568 if (P_SHOULDSTOP(p)) { 569 PROC_UNLOCK(p); 570 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 571 PROC_LOCK(p); 572 psignal(p, SIGHUP); 573 psignal(p, SIGCONT); 574 PROC_UNLOCK(p); 575 } 576 return; 577 } 578 PROC_UNLOCK(p); 579 } 580 } 581 582 void 583 sessrele(struct session *s) 584 { 585 int i; 586 587 SESS_LOCK(s); 588 i = --s->s_count; 589 SESS_UNLOCK(s); 590 if (i == 0) { 591 if (s->s_ttyp != NULL) 592 ttyrel(s->s_ttyp); 593 mtx_destroy(&s->s_mtx); 594 FREE(s, M_SESSION); 595 } 596 } 597 598 #include "opt_ddb.h" 599 #ifdef DDB 600 #include <ddb/ddb.h> 601 602 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 603 { 604 register struct pgrp *pgrp; 605 register struct proc *p; 606 register int i; 607 608 for (i = 0; i <= pgrphash; i++) { 609 if (!LIST_EMPTY(&pgrphashtbl[i])) { 610 printf("\tindx %d\n", i); 611 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 612 printf( 613 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 614 (void *)pgrp, (long)pgrp->pg_id, 615 (void *)pgrp->pg_session, 616 pgrp->pg_session->s_count, 617 (void *)LIST_FIRST(&pgrp->pg_members)); 618 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 619 printf("\t\tpid %ld addr %p pgrp %p\n", 620 (long)p->p_pid, (void *)p, 621 (void *)p->p_pgrp); 622 } 623 } 624 } 625 } 626 } 627 #endif /* DDB */ 628 629 /* 630 * Clear kinfo_proc and fill in any information that is common 631 * to all threads in the process. 632 * Must be called with the target process locked. 633 */ 634 static void 635 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 636 { 637 struct thread *td0; 638 struct tty *tp; 639 struct session *sp; 640 struct ucred *cred; 641 struct sigacts *ps; 642 643 bzero(kp, sizeof(*kp)); 644 645 kp->ki_structsize = sizeof(*kp); 646 kp->ki_paddr = p; 647 PROC_LOCK_ASSERT(p, MA_OWNED); 648 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 649 kp->ki_args = p->p_args; 650 kp->ki_textvp = p->p_textvp; 651 #ifdef KTRACE 652 kp->ki_tracep = p->p_tracevp; 653 mtx_lock(&ktrace_mtx); 654 kp->ki_traceflag = p->p_traceflag; 655 mtx_unlock(&ktrace_mtx); 656 #endif 657 kp->ki_fd = p->p_fd; 658 kp->ki_vmspace = p->p_vmspace; 659 kp->ki_flag = p->p_flag; 660 cred = p->p_ucred; 661 if (cred) { 662 kp->ki_uid = cred->cr_uid; 663 kp->ki_ruid = cred->cr_ruid; 664 kp->ki_svuid = cred->cr_svuid; 665 /* XXX bde doesn't like KI_NGROUPS */ 666 kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS); 667 bcopy(cred->cr_groups, kp->ki_groups, 668 kp->ki_ngroups * sizeof(gid_t)); 669 kp->ki_rgid = cred->cr_rgid; 670 kp->ki_svgid = cred->cr_svgid; 671 /* If jailed(cred), emulate the old P_JAILED flag. */ 672 if (jailed(cred)) { 673 kp->ki_flag |= P_JAILED; 674 /* If inside a jail, use 0 as a jail ID. */ 675 if (!jailed(curthread->td_ucred)) 676 kp->ki_jid = cred->cr_prison->pr_id; 677 } 678 } 679 ps = p->p_sigacts; 680 if (ps) { 681 mtx_lock(&ps->ps_mtx); 682 kp->ki_sigignore = ps->ps_sigignore; 683 kp->ki_sigcatch = ps->ps_sigcatch; 684 mtx_unlock(&ps->ps_mtx); 685 } 686 PROC_SLOCK(p); 687 if (p->p_state != PRS_NEW && 688 p->p_state != PRS_ZOMBIE && 689 p->p_vmspace != NULL) { 690 struct vmspace *vm = p->p_vmspace; 691 692 kp->ki_size = vm->vm_map.size; 693 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 694 FOREACH_THREAD_IN_PROC(p, td0) { 695 if (!TD_IS_SWAPPED(td0)) 696 kp->ki_rssize += td0->td_kstack_pages; 697 if (td0->td_altkstack_obj != NULL) 698 kp->ki_rssize += td0->td_altkstack_pages; 699 } 700 kp->ki_swrss = vm->vm_swrss; 701 kp->ki_tsize = vm->vm_tsize; 702 kp->ki_dsize = vm->vm_dsize; 703 kp->ki_ssize = vm->vm_ssize; 704 } else if (p->p_state == PRS_ZOMBIE) 705 kp->ki_stat = SZOMB; 706 if (kp->ki_flag & P_INMEM) 707 kp->ki_sflag = PS_INMEM; 708 else 709 kp->ki_sflag = 0; 710 /* Calculate legacy swtime as seconds since 'swtick'. */ 711 kp->ki_swtime = (ticks - p->p_swtick) / hz; 712 kp->ki_pid = p->p_pid; 713 kp->ki_nice = p->p_nice; 714 rufetch(p, &kp->ki_rusage); 715 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 716 PROC_SUNLOCK(p); 717 if ((p->p_flag & P_INMEM) && p->p_stats != NULL) { 718 kp->ki_start = p->p_stats->p_start; 719 timevaladd(&kp->ki_start, &boottime); 720 PROC_SLOCK(p); 721 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 722 PROC_SUNLOCK(p); 723 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 724 725 /* Some callers want child-times in a single value */ 726 kp->ki_childtime = kp->ki_childstime; 727 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 728 } 729 tp = NULL; 730 if (p->p_pgrp) { 731 kp->ki_pgid = p->p_pgrp->pg_id; 732 kp->ki_jobc = p->p_pgrp->pg_jobc; 733 sp = p->p_pgrp->pg_session; 734 735 if (sp != NULL) { 736 kp->ki_sid = sp->s_sid; 737 SESS_LOCK(sp); 738 strlcpy(kp->ki_login, sp->s_login, 739 sizeof(kp->ki_login)); 740 if (sp->s_ttyvp) 741 kp->ki_kiflag |= KI_CTTY; 742 if (SESS_LEADER(p)) 743 kp->ki_kiflag |= KI_SLEADER; 744 tp = sp->s_ttyp; 745 SESS_UNLOCK(sp); 746 } 747 } 748 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 749 kp->ki_tdev = dev2udev(tp->t_dev); 750 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 751 if (tp->t_session) 752 kp->ki_tsid = tp->t_session->s_sid; 753 } else 754 kp->ki_tdev = NODEV; 755 if (p->p_comm[0] != '\0') 756 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 757 if (p->p_sysent && p->p_sysent->sv_name != NULL && 758 p->p_sysent->sv_name[0] != '\0') 759 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 760 kp->ki_siglist = p->p_siglist; 761 kp->ki_xstat = p->p_xstat; 762 kp->ki_acflag = p->p_acflag; 763 kp->ki_lock = p->p_lock; 764 if (p->p_pptr) 765 kp->ki_ppid = p->p_pptr->p_pid; 766 } 767 768 /* 769 * Fill in information that is thread specific. Must be called with p_slock 770 * locked. If 'preferthread' is set, overwrite certain process-related 771 * fields that are maintained for both threads and processes. 772 */ 773 static void 774 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 775 { 776 struct proc *p; 777 778 p = td->td_proc; 779 PROC_SLOCK_ASSERT(p, MA_OWNED); 780 781 thread_lock(td); 782 if (td->td_wmesg != NULL) 783 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 784 else 785 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 786 if (td->td_name[0] != '\0') 787 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 788 if (TD_ON_LOCK(td)) { 789 kp->ki_kiflag |= KI_LOCKBLOCK; 790 strlcpy(kp->ki_lockname, td->td_lockname, 791 sizeof(kp->ki_lockname)); 792 } else { 793 kp->ki_kiflag &= ~KI_LOCKBLOCK; 794 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 795 } 796 797 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 798 if (TD_ON_RUNQ(td) || 799 TD_CAN_RUN(td) || 800 TD_IS_RUNNING(td)) { 801 kp->ki_stat = SRUN; 802 } else if (P_SHOULDSTOP(p)) { 803 kp->ki_stat = SSTOP; 804 } else if (TD_IS_SLEEPING(td)) { 805 kp->ki_stat = SSLEEP; 806 } else if (TD_ON_LOCK(td)) { 807 kp->ki_stat = SLOCK; 808 } else { 809 kp->ki_stat = SWAIT; 810 } 811 } else if (p->p_state == PRS_ZOMBIE) { 812 kp->ki_stat = SZOMB; 813 } else { 814 kp->ki_stat = SIDL; 815 } 816 817 /* Things in the thread */ 818 kp->ki_wchan = td->td_wchan; 819 kp->ki_pri.pri_level = td->td_priority; 820 kp->ki_pri.pri_native = td->td_base_pri; 821 kp->ki_lastcpu = td->td_lastcpu; 822 kp->ki_oncpu = td->td_oncpu; 823 kp->ki_tdflags = td->td_flags; 824 kp->ki_tid = td->td_tid; 825 kp->ki_numthreads = p->p_numthreads; 826 kp->ki_pcb = td->td_pcb; 827 kp->ki_kstack = (void *)td->td_kstack; 828 kp->ki_pctcpu = sched_pctcpu(td); 829 kp->ki_estcpu = td->td_estcpu; 830 kp->ki_slptime = (ticks - td->td_slptick) / hz; 831 kp->ki_pri.pri_class = td->td_pri_class; 832 kp->ki_pri.pri_user = td->td_user_pri; 833 834 if (preferthread) 835 kp->ki_runtime = cputick2usec(td->td_runtime); 836 837 /* We can't get this anymore but ps etc never used it anyway. */ 838 kp->ki_rqindex = 0; 839 840 SIGSETOR(kp->ki_siglist, td->td_siglist); 841 kp->ki_sigmask = td->td_sigmask; 842 thread_unlock(td); 843 } 844 845 /* 846 * Fill in a kinfo_proc structure for the specified process. 847 * Must be called with the target process locked. 848 */ 849 void 850 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 851 { 852 853 fill_kinfo_proc_only(p, kp); 854 PROC_SLOCK(p); 855 if (FIRST_THREAD_IN_PROC(p) != NULL) 856 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 857 PROC_SUNLOCK(p); 858 } 859 860 struct pstats * 861 pstats_alloc(void) 862 { 863 864 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 865 } 866 867 /* 868 * Copy parts of p_stats; zero the rest of p_stats (statistics). 869 */ 870 void 871 pstats_fork(struct pstats *src, struct pstats *dst) 872 { 873 874 bzero(&dst->pstat_startzero, 875 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 876 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 877 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 878 } 879 880 void 881 pstats_free(struct pstats *ps) 882 { 883 884 free(ps, M_SUBPROC); 885 } 886 887 /* 888 * Locate a zombie process by number 889 */ 890 struct proc * 891 zpfind(pid_t pid) 892 { 893 struct proc *p; 894 895 sx_slock(&allproc_lock); 896 LIST_FOREACH(p, &zombproc, p_list) 897 if (p->p_pid == pid) { 898 PROC_LOCK(p); 899 break; 900 } 901 sx_sunlock(&allproc_lock); 902 return (p); 903 } 904 905 #define KERN_PROC_ZOMBMASK 0x3 906 #define KERN_PROC_NOTHREADS 0x4 907 908 /* 909 * Must be called with the process locked and will return with it unlocked. 910 */ 911 static int 912 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 913 { 914 struct thread *td; 915 struct kinfo_proc kinfo_proc; 916 int error = 0; 917 struct proc *np; 918 pid_t pid = p->p_pid; 919 920 PROC_LOCK_ASSERT(p, MA_OWNED); 921 922 fill_kinfo_proc_only(p, &kinfo_proc); 923 if (flags & KERN_PROC_NOTHREADS) { 924 PROC_SLOCK(p); 925 if (FIRST_THREAD_IN_PROC(p) != NULL) 926 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), 927 &kinfo_proc, 0); 928 PROC_SUNLOCK(p); 929 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 930 sizeof(kinfo_proc)); 931 } else { 932 PROC_SLOCK(p); 933 if (FIRST_THREAD_IN_PROC(p) != NULL) 934 FOREACH_THREAD_IN_PROC(p, td) { 935 fill_kinfo_thread(td, &kinfo_proc, 1); 936 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 937 sizeof(kinfo_proc)); 938 if (error) 939 break; 940 } 941 else 942 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 943 sizeof(kinfo_proc)); 944 PROC_SUNLOCK(p); 945 } 946 PROC_UNLOCK(p); 947 if (error) 948 return (error); 949 if (flags & KERN_PROC_ZOMBMASK) 950 np = zpfind(pid); 951 else { 952 if (pid == 0) 953 return (0); 954 np = pfind(pid); 955 } 956 if (np == NULL) 957 return EAGAIN; 958 if (np != p) { 959 PROC_UNLOCK(np); 960 return EAGAIN; 961 } 962 PROC_UNLOCK(np); 963 return (0); 964 } 965 966 static int 967 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 968 { 969 int *name = (int*) arg1; 970 u_int namelen = arg2; 971 struct proc *p; 972 int flags, doingzomb, oid_number; 973 int error = 0; 974 975 oid_number = oidp->oid_number; 976 if (oid_number != KERN_PROC_ALL && 977 (oid_number & KERN_PROC_INC_THREAD) == 0) 978 flags = KERN_PROC_NOTHREADS; 979 else { 980 flags = 0; 981 oid_number &= ~KERN_PROC_INC_THREAD; 982 } 983 if (oid_number == KERN_PROC_PID) { 984 if (namelen != 1) 985 return (EINVAL); 986 error = sysctl_wire_old_buffer(req, 0); 987 if (error) 988 return (error); 989 p = pfind((pid_t)name[0]); 990 if (!p) 991 return (ESRCH); 992 if ((error = p_cansee(curthread, p))) { 993 PROC_UNLOCK(p); 994 return (error); 995 } 996 error = sysctl_out_proc(p, req, flags); 997 return (error); 998 } 999 1000 switch (oid_number) { 1001 case KERN_PROC_ALL: 1002 if (namelen != 0) 1003 return (EINVAL); 1004 break; 1005 case KERN_PROC_PROC: 1006 if (namelen != 0 && namelen != 1) 1007 return (EINVAL); 1008 break; 1009 default: 1010 if (namelen != 1) 1011 return (EINVAL); 1012 break; 1013 } 1014 1015 if (!req->oldptr) { 1016 /* overestimate by 5 procs */ 1017 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1018 if (error) 1019 return (error); 1020 } 1021 error = sysctl_wire_old_buffer(req, 0); 1022 if (error != 0) 1023 return (error); 1024 sx_slock(&allproc_lock); 1025 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1026 if (!doingzomb) 1027 p = LIST_FIRST(&allproc); 1028 else 1029 p = LIST_FIRST(&zombproc); 1030 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1031 /* 1032 * Skip embryonic processes. 1033 */ 1034 PROC_SLOCK(p); 1035 if (p->p_state == PRS_NEW) { 1036 PROC_SUNLOCK(p); 1037 continue; 1038 } 1039 PROC_SUNLOCK(p); 1040 PROC_LOCK(p); 1041 KASSERT(p->p_ucred != NULL, 1042 ("process credential is NULL for non-NEW proc")); 1043 /* 1044 * Show a user only appropriate processes. 1045 */ 1046 if (p_cansee(curthread, p)) { 1047 PROC_UNLOCK(p); 1048 continue; 1049 } 1050 /* 1051 * TODO - make more efficient (see notes below). 1052 * do by session. 1053 */ 1054 switch (oid_number) { 1055 1056 case KERN_PROC_GID: 1057 if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1058 PROC_UNLOCK(p); 1059 continue; 1060 } 1061 break; 1062 1063 case KERN_PROC_PGRP: 1064 /* could do this by traversing pgrp */ 1065 if (p->p_pgrp == NULL || 1066 p->p_pgrp->pg_id != (pid_t)name[0]) { 1067 PROC_UNLOCK(p); 1068 continue; 1069 } 1070 break; 1071 1072 case KERN_PROC_RGID: 1073 if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1074 PROC_UNLOCK(p); 1075 continue; 1076 } 1077 break; 1078 1079 case KERN_PROC_SESSION: 1080 if (p->p_session == NULL || 1081 p->p_session->s_sid != (pid_t)name[0]) { 1082 PROC_UNLOCK(p); 1083 continue; 1084 } 1085 break; 1086 1087 case KERN_PROC_TTY: 1088 if ((p->p_flag & P_CONTROLT) == 0 || 1089 p->p_session == NULL) { 1090 PROC_UNLOCK(p); 1091 continue; 1092 } 1093 SESS_LOCK(p->p_session); 1094 if (p->p_session->s_ttyp == NULL || 1095 dev2udev(p->p_session->s_ttyp->t_dev) != 1096 (dev_t)name[0]) { 1097 SESS_UNLOCK(p->p_session); 1098 PROC_UNLOCK(p); 1099 continue; 1100 } 1101 SESS_UNLOCK(p->p_session); 1102 break; 1103 1104 case KERN_PROC_UID: 1105 if (p->p_ucred->cr_uid != (uid_t)name[0]) { 1106 PROC_UNLOCK(p); 1107 continue; 1108 } 1109 break; 1110 1111 case KERN_PROC_RUID: 1112 if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 1113 PROC_UNLOCK(p); 1114 continue; 1115 } 1116 break; 1117 1118 case KERN_PROC_PROC: 1119 break; 1120 1121 default: 1122 break; 1123 1124 } 1125 1126 error = sysctl_out_proc(p, req, flags | doingzomb); 1127 if (error) { 1128 sx_sunlock(&allproc_lock); 1129 return (error); 1130 } 1131 } 1132 } 1133 sx_sunlock(&allproc_lock); 1134 return (0); 1135 } 1136 1137 struct pargs * 1138 pargs_alloc(int len) 1139 { 1140 struct pargs *pa; 1141 1142 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1143 M_WAITOK); 1144 refcount_init(&pa->ar_ref, 1); 1145 pa->ar_length = len; 1146 return (pa); 1147 } 1148 1149 void 1150 pargs_free(struct pargs *pa) 1151 { 1152 1153 FREE(pa, M_PARGS); 1154 } 1155 1156 void 1157 pargs_hold(struct pargs *pa) 1158 { 1159 1160 if (pa == NULL) 1161 return; 1162 refcount_acquire(&pa->ar_ref); 1163 } 1164 1165 void 1166 pargs_drop(struct pargs *pa) 1167 { 1168 1169 if (pa == NULL) 1170 return; 1171 if (refcount_release(&pa->ar_ref)) 1172 pargs_free(pa); 1173 } 1174 1175 /* 1176 * This sysctl allows a process to retrieve the argument list or process 1177 * title for another process without groping around in the address space 1178 * of the other process. It also allow a process to set its own "process 1179 * title to a string of its own choice. 1180 */ 1181 static int 1182 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1183 { 1184 int *name = (int*) arg1; 1185 u_int namelen = arg2; 1186 struct pargs *newpa, *pa; 1187 struct proc *p; 1188 int error = 0; 1189 1190 if (namelen != 1) 1191 return (EINVAL); 1192 1193 p = pfind((pid_t)name[0]); 1194 if (!p) 1195 return (ESRCH); 1196 1197 if ((error = p_cansee(curthread, p)) != 0) { 1198 PROC_UNLOCK(p); 1199 return (error); 1200 } 1201 1202 if (req->newptr && curproc != p) { 1203 PROC_UNLOCK(p); 1204 return (EPERM); 1205 } 1206 1207 pa = p->p_args; 1208 pargs_hold(pa); 1209 PROC_UNLOCK(p); 1210 if (req->oldptr != NULL && pa != NULL) 1211 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1212 pargs_drop(pa); 1213 if (error != 0 || req->newptr == NULL) 1214 return (error); 1215 1216 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1217 return (ENOMEM); 1218 newpa = pargs_alloc(req->newlen); 1219 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1220 if (error != 0) { 1221 pargs_free(newpa); 1222 return (error); 1223 } 1224 PROC_LOCK(p); 1225 pa = p->p_args; 1226 p->p_args = newpa; 1227 PROC_UNLOCK(p); 1228 pargs_drop(pa); 1229 return (0); 1230 } 1231 1232 /* 1233 * This sysctl allows a process to retrieve the path of the executable for 1234 * itself or another process. 1235 */ 1236 static int 1237 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1238 { 1239 pid_t *pidp = (pid_t *)arg1; 1240 unsigned int arglen = arg2; 1241 struct proc *p; 1242 struct vnode *vp; 1243 char *retbuf, *freebuf; 1244 int error; 1245 1246 if (arglen != 1) 1247 return (EINVAL); 1248 if (*pidp == -1) { /* -1 means this process */ 1249 p = req->td->td_proc; 1250 } else { 1251 p = pfind(*pidp); 1252 if (p == NULL) 1253 return (ESRCH); 1254 if ((error = p_cansee(curthread, p)) != 0) { 1255 PROC_UNLOCK(p); 1256 return (error); 1257 } 1258 } 1259 1260 vp = p->p_textvp; 1261 if (vp == NULL) { 1262 if (*pidp != -1) 1263 PROC_UNLOCK(p); 1264 return (0); 1265 } 1266 vref(vp); 1267 if (*pidp != -1) 1268 PROC_UNLOCK(p); 1269 error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1270 vrele(vp); 1271 if (error) 1272 return (error); 1273 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1274 free(freebuf, M_TEMP); 1275 return (error); 1276 } 1277 1278 static int 1279 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1280 { 1281 struct proc *p; 1282 char *sv_name; 1283 int *name; 1284 int namelen; 1285 int error; 1286 1287 namelen = arg2; 1288 if (namelen != 1) 1289 return (EINVAL); 1290 1291 name = (int *)arg1; 1292 if ((p = pfind((pid_t)name[0])) == NULL) 1293 return (ESRCH); 1294 if ((error = p_cansee(curthread, p))) { 1295 PROC_UNLOCK(p); 1296 return (error); 1297 } 1298 sv_name = p->p_sysent->sv_name; 1299 PROC_UNLOCK(p); 1300 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1301 } 1302 1303 static int 1304 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 1305 { 1306 vm_map_entry_t entry, tmp_entry; 1307 unsigned int last_timestamp; 1308 char *fullpath, *freepath; 1309 struct kinfo_vmentry *kve; 1310 int error, *name; 1311 struct vnode *vp; 1312 struct proc *p; 1313 vm_map_t map; 1314 1315 name = (int *)arg1; 1316 if ((p = pfind((pid_t)name[0])) == NULL) 1317 return (ESRCH); 1318 if (p->p_flag & P_WEXIT) { 1319 PROC_UNLOCK(p); 1320 return (ESRCH); 1321 } 1322 if ((error = p_candebug(curthread, p))) { 1323 PROC_UNLOCK(p); 1324 return (error); 1325 } 1326 _PHOLD(p); 1327 PROC_UNLOCK(p); 1328 1329 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1330 1331 map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */ 1332 vm_map_lock_read(map); 1333 for (entry = map->header.next; entry != &map->header; 1334 entry = entry->next) { 1335 vm_object_t obj, tobj, lobj; 1336 vm_offset_t addr; 1337 int vfslocked; 1338 1339 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1340 continue; 1341 1342 bzero(kve, sizeof(*kve)); 1343 kve->kve_structsize = sizeof(*kve); 1344 1345 kve->kve_private_resident = 0; 1346 obj = entry->object.vm_object; 1347 if (obj != NULL) { 1348 VM_OBJECT_LOCK(obj); 1349 if (obj->shadow_count == 1) 1350 kve->kve_private_resident = 1351 obj->resident_page_count; 1352 } 1353 kve->kve_resident = 0; 1354 addr = entry->start; 1355 while (addr < entry->end) { 1356 if (pmap_extract(map->pmap, addr)) 1357 kve->kve_resident++; 1358 addr += PAGE_SIZE; 1359 } 1360 1361 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1362 if (tobj != obj) 1363 VM_OBJECT_LOCK(tobj); 1364 if (lobj != obj) 1365 VM_OBJECT_UNLOCK(lobj); 1366 lobj = tobj; 1367 } 1368 1369 freepath = NULL; 1370 fullpath = ""; 1371 if (lobj) { 1372 vp = NULL; 1373 switch(lobj->type) { 1374 case OBJT_DEFAULT: 1375 kve->kve_type = KVME_TYPE_DEFAULT; 1376 break; 1377 case OBJT_VNODE: 1378 kve->kve_type = KVME_TYPE_VNODE; 1379 vp = lobj->handle; 1380 vref(vp); 1381 break; 1382 case OBJT_SWAP: 1383 kve->kve_type = KVME_TYPE_SWAP; 1384 break; 1385 case OBJT_DEVICE: 1386 kve->kve_type = KVME_TYPE_DEVICE; 1387 break; 1388 case OBJT_PHYS: 1389 kve->kve_type = KVME_TYPE_PHYS; 1390 break; 1391 case OBJT_DEAD: 1392 kve->kve_type = KVME_TYPE_DEAD; 1393 break; 1394 default: 1395 kve->kve_type = KVME_TYPE_UNKNOWN; 1396 break; 1397 } 1398 if (lobj != obj) 1399 VM_OBJECT_UNLOCK(lobj); 1400 1401 kve->kve_ref_count = obj->ref_count; 1402 kve->kve_shadow_count = obj->shadow_count; 1403 VM_OBJECT_UNLOCK(obj); 1404 if (vp != NULL) { 1405 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1406 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1407 vn_fullpath(curthread, vp, &fullpath, 1408 &freepath); 1409 vput(vp); 1410 VFS_UNLOCK_GIANT(vfslocked); 1411 } 1412 } else { 1413 kve->kve_type = KVME_TYPE_NONE; 1414 kve->kve_ref_count = 0; 1415 kve->kve_shadow_count = 0; 1416 } 1417 1418 kve->kve_start = (void*)entry->start; 1419 kve->kve_end = (void*)entry->end; 1420 1421 if (entry->protection & VM_PROT_READ) 1422 kve->kve_protection |= KVME_PROT_READ; 1423 if (entry->protection & VM_PROT_WRITE) 1424 kve->kve_protection |= KVME_PROT_WRITE; 1425 if (entry->protection & VM_PROT_EXECUTE) 1426 kve->kve_protection |= KVME_PROT_EXEC; 1427 1428 if (entry->eflags & MAP_ENTRY_COW) 1429 kve->kve_flags |= KVME_FLAG_COW; 1430 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1431 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1432 1433 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1434 if (freepath != NULL) 1435 free(freepath, M_TEMP); 1436 1437 last_timestamp = map->timestamp; 1438 vm_map_unlock_read(map); 1439 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 1440 vm_map_lock_read(map); 1441 if (error) 1442 break; 1443 if (last_timestamp + 1 != map->timestamp) { 1444 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1445 entry = tmp_entry; 1446 } 1447 } 1448 vm_map_unlock_read(map); 1449 PRELE(p); 1450 free(kve, M_TEMP); 1451 return (error); 1452 } 1453 1454 #if defined(STACK) || defined(DDB) 1455 static int 1456 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 1457 { 1458 struct kinfo_kstack *kkstp; 1459 int error, i, *name, numthreads; 1460 lwpid_t *lwpidarray; 1461 struct thread *td; 1462 struct stack *st; 1463 struct sbuf sb; 1464 struct proc *p; 1465 1466 name = (int *)arg1; 1467 if ((p = pfind((pid_t)name[0])) == NULL) 1468 return (ESRCH); 1469 /* XXXRW: Not clear ESRCH is the right error during proc execve(). */ 1470 if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) { 1471 PROC_UNLOCK(p); 1472 return (ESRCH); 1473 } 1474 if ((error = p_candebug(curthread, p))) { 1475 PROC_UNLOCK(p); 1476 return (error); 1477 } 1478 _PHOLD(p); 1479 PROC_UNLOCK(p); 1480 1481 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 1482 st = stack_create(); 1483 1484 lwpidarray = NULL; 1485 numthreads = 0; 1486 PROC_SLOCK(p); 1487 repeat: 1488 if (numthreads < p->p_numthreads) { 1489 if (lwpidarray != NULL) { 1490 free(lwpidarray, M_TEMP); 1491 lwpidarray = NULL; 1492 } 1493 numthreads = p->p_numthreads; 1494 PROC_SUNLOCK(p); 1495 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 1496 M_WAITOK | M_ZERO); 1497 PROC_SLOCK(p); 1498 goto repeat; 1499 } 1500 PROC_SUNLOCK(p); 1501 i = 0; 1502 1503 /* 1504 * XXXRW: During the below loop, execve(2) and countless other sorts 1505 * of changes could have taken place. Should we check to see if the 1506 * vmspace has been replaced, or the like, in order to prevent 1507 * giving a snapshot that spans, say, execve(2), with some threads 1508 * before and some after? Among other things, the credentials could 1509 * have changed, in which case the right to extract debug info might 1510 * no longer be assured. 1511 */ 1512 PROC_LOCK(p); 1513 FOREACH_THREAD_IN_PROC(p, td) { 1514 KASSERT(i < numthreads, 1515 ("sysctl_kern_proc_kstack: numthreads")); 1516 lwpidarray[i] = td->td_tid; 1517 i++; 1518 } 1519 numthreads = i; 1520 for (i = 0; i < numthreads; i++) { 1521 td = thread_find(p, lwpidarray[i]); 1522 if (td == NULL) { 1523 continue; 1524 } 1525 bzero(kkstp, sizeof(*kkstp)); 1526 (void)sbuf_new(&sb, kkstp->kkst_trace, 1527 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 1528 thread_lock(td); 1529 kkstp->kkst_tid = td->td_tid; 1530 if (TD_IS_SWAPPED(td)) 1531 kkstp->kkst_state = KKST_STATE_SWAPPED; 1532 else if (TD_IS_RUNNING(td)) 1533 kkstp->kkst_state = KKST_STATE_RUNNING; 1534 else { 1535 kkstp->kkst_state = KKST_STATE_STACKOK; 1536 stack_save_td(st, td); 1537 } 1538 thread_unlock(td); 1539 PROC_UNLOCK(p); 1540 stack_sbuf_print(&sb, st); 1541 sbuf_finish(&sb); 1542 sbuf_delete(&sb); 1543 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 1544 PROC_LOCK(p); 1545 if (error) 1546 break; 1547 } 1548 _PRELE(p); 1549 PROC_UNLOCK(p); 1550 if (lwpidarray != NULL) 1551 free(lwpidarray, M_TEMP); 1552 stack_destroy(st); 1553 free(kkstp, M_TEMP); 1554 return (error); 1555 } 1556 #endif 1557 1558 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1559 1560 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1561 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1562 1563 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD, 1564 sysctl_kern_proc, "Process table"); 1565 1566 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1567 sysctl_kern_proc, "Process table"); 1568 1569 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD, 1570 sysctl_kern_proc, "Process table"); 1571 1572 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD, 1573 sysctl_kern_proc, "Process table"); 1574 1575 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1576 sysctl_kern_proc, "Process table"); 1577 1578 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1579 sysctl_kern_proc, "Process table"); 1580 1581 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1582 sysctl_kern_proc, "Process table"); 1583 1584 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1585 sysctl_kern_proc, "Process table"); 1586 1587 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD, 1588 sysctl_kern_proc, "Return process table, no threads"); 1589 1590 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 1591 CTLFLAG_RW | CTLFLAG_ANYBODY, 1592 sysctl_kern_proc_args, "Process argument list"); 1593 1594 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD, 1595 sysctl_kern_proc_pathname, "Process executable path"); 1596 1597 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD, 1598 sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); 1599 1600 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 1601 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1602 1603 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 1604 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1605 1606 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 1607 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1608 1609 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 1610 sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1611 1612 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 1613 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1614 1615 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 1616 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1617 1618 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 1619 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1620 1621 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 1622 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1623 1624 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 1625 CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads"); 1626 1627 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD, 1628 sysctl_kern_proc_vmmap, "Process vm map entries"); 1629 1630 #if defined(STACK) || defined(DDB) 1631 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD, 1632 sysctl_kern_proc_kstack, "Process kernel stacks"); 1633 #endif 1634