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_compat.h" 36 #include "opt_ddb.h" 37 #include "opt_kdtrace.h" 38 #include "opt_ktrace.h" 39 #include "opt_kstack_pages.h" 40 #include "opt_stack.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/limits.h> 46 #include <sys/lock.h> 47 #include <sys/loginclass.h> 48 #include <sys/malloc.h> 49 #include <sys/mount.h> 50 #include <sys/mutex.h> 51 #include <sys/proc.h> 52 #include <sys/refcount.h> 53 #include <sys/sbuf.h> 54 #include <sys/sysent.h> 55 #include <sys/sched.h> 56 #include <sys/smp.h> 57 #include <sys/stack.h> 58 #include <sys/sysctl.h> 59 #include <sys/filedesc.h> 60 #include <sys/tty.h> 61 #include <sys/signalvar.h> 62 #include <sys/sdt.h> 63 #include <sys/sx.h> 64 #include <sys/user.h> 65 #include <sys/jail.h> 66 #include <sys/vnode.h> 67 #include <sys/eventhandler.h> 68 69 #ifdef DDB 70 #include <ddb/ddb.h> 71 #endif 72 73 #include <vm/vm.h> 74 #include <vm/vm_extern.h> 75 #include <vm/pmap.h> 76 #include <vm/vm_map.h> 77 #include <vm/vm_object.h> 78 #include <vm/uma.h> 79 80 #ifdef COMPAT_FREEBSD32 81 #include <compat/freebsd32/freebsd32.h> 82 #include <compat/freebsd32/freebsd32_util.h> 83 #endif 84 85 SDT_PROVIDER_DEFINE(proc); 86 SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry); 87 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *"); 88 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int"); 89 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *"); 90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int"); 91 SDT_PROBE_DEFINE(proc, kernel, ctor, return, return); 92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *"); 93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int"); 94 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *"); 95 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int"); 96 SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry); 97 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *"); 98 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int"); 99 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *"); 100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *"); 101 SDT_PROBE_DEFINE(proc, kernel, dtor, return, return); 102 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *"); 103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int"); 104 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *"); 105 SDT_PROBE_DEFINE(proc, kernel, init, entry, entry); 106 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *"); 107 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int"); 108 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int"); 109 SDT_PROBE_DEFINE(proc, kernel, init, return, return); 110 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *"); 111 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int"); 112 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int"); 113 114 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 115 MALLOC_DEFINE(M_SESSION, "session", "session header"); 116 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 117 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 118 119 static void doenterpgrp(struct proc *, struct pgrp *); 120 static void orphanpg(struct pgrp *pg); 121 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); 122 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 123 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 124 int preferthread); 125 static void pgadjustjobc(struct pgrp *pgrp, int entering); 126 static void pgdelete(struct pgrp *); 127 static int proc_ctor(void *mem, int size, void *arg, int flags); 128 static void proc_dtor(void *mem, int size, void *arg); 129 static int proc_init(void *mem, int size, int flags); 130 static void proc_fini(void *mem, int size); 131 static void pargs_free(struct pargs *pa); 132 133 /* 134 * Other process lists 135 */ 136 struct pidhashhead *pidhashtbl; 137 u_long pidhash; 138 struct pgrphashhead *pgrphashtbl; 139 u_long pgrphash; 140 struct proclist allproc; 141 struct proclist zombproc; 142 struct sx allproc_lock; 143 struct sx proctree_lock; 144 struct mtx ppeers_lock; 145 uma_zone_t proc_zone; 146 147 int kstack_pages = KSTACK_PAGES; 148 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, 149 "Kernel stack size in pages"); 150 151 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 152 #ifdef COMPAT_FREEBSD32 153 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); 154 #endif 155 156 /* 157 * Initialize global process hashing structures. 158 */ 159 void 160 procinit() 161 { 162 163 sx_init(&allproc_lock, "allproc"); 164 sx_init(&proctree_lock, "proctree"); 165 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 166 LIST_INIT(&allproc); 167 LIST_INIT(&zombproc); 168 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 169 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 170 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 171 proc_ctor, proc_dtor, proc_init, proc_fini, 172 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 173 uihashinit(); 174 } 175 176 /* 177 * Prepare a proc for use. 178 */ 179 static int 180 proc_ctor(void *mem, int size, void *arg, int flags) 181 { 182 struct proc *p; 183 184 p = (struct proc *)mem; 185 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0); 186 EVENTHANDLER_INVOKE(process_ctor, p); 187 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0); 188 return (0); 189 } 190 191 /* 192 * Reclaim a proc after use. 193 */ 194 static void 195 proc_dtor(void *mem, int size, void *arg) 196 { 197 struct proc *p; 198 struct thread *td; 199 200 /* INVARIANTS checks go here */ 201 p = (struct proc *)mem; 202 td = FIRST_THREAD_IN_PROC(p); 203 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0); 204 if (td != NULL) { 205 #ifdef INVARIANTS 206 KASSERT((p->p_numthreads == 1), 207 ("bad number of threads in exiting process")); 208 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 209 #endif 210 /* Free all OSD associated to this thread. */ 211 osd_thread_exit(td); 212 } 213 EVENTHANDLER_INVOKE(process_dtor, p); 214 if (p->p_ksi != NULL) 215 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 216 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0); 217 } 218 219 /* 220 * Initialize type-stable parts of a proc (when newly created). 221 */ 222 static int 223 proc_init(void *mem, int size, int flags) 224 { 225 struct proc *p; 226 227 p = (struct proc *)mem; 228 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0); 229 p->p_sched = (struct p_sched *)&p[1]; 230 bzero(&p->p_mtx, sizeof(struct mtx)); 231 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 232 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 233 cv_init(&p->p_pwait, "ppwait"); 234 cv_init(&p->p_dbgwait, "dbgwait"); 235 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 236 EVENTHANDLER_INVOKE(process_init, p); 237 p->p_stats = pstats_alloc(); 238 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0); 239 return (0); 240 } 241 242 /* 243 * UMA should ensure that this function is never called. 244 * Freeing a proc structure would violate type stability. 245 */ 246 static void 247 proc_fini(void *mem, int size) 248 { 249 #ifdef notnow 250 struct proc *p; 251 252 p = (struct proc *)mem; 253 EVENTHANDLER_INVOKE(process_fini, p); 254 pstats_free(p->p_stats); 255 thread_free(FIRST_THREAD_IN_PROC(p)); 256 mtx_destroy(&p->p_mtx); 257 if (p->p_ksi != NULL) 258 ksiginfo_free(p->p_ksi); 259 #else 260 panic("proc reclaimed"); 261 #endif 262 } 263 264 /* 265 * Is p an inferior of the current process? 266 */ 267 int 268 inferior(p) 269 register struct proc *p; 270 { 271 272 sx_assert(&proctree_lock, SX_LOCKED); 273 for (; p != curproc; p = p->p_pptr) 274 if (p->p_pid == 0) 275 return (0); 276 return (1); 277 } 278 279 /* 280 * Locate a process by number; return only "live" processes -- i.e., neither 281 * zombies nor newly born but incompletely initialized processes. By not 282 * returning processes in the PRS_NEW state, we allow callers to avoid 283 * testing for that condition to avoid dereferencing p_ucred, et al. 284 */ 285 struct proc * 286 pfind(pid) 287 register pid_t pid; 288 { 289 register struct proc *p; 290 291 sx_slock(&allproc_lock); 292 LIST_FOREACH(p, PIDHASH(pid), p_hash) 293 if (p->p_pid == pid) { 294 PROC_LOCK(p); 295 if (p->p_state == PRS_NEW) { 296 PROC_UNLOCK(p); 297 p = NULL; 298 } 299 break; 300 } 301 sx_sunlock(&allproc_lock); 302 return (p); 303 } 304 305 /* 306 * Locate a process group by number. 307 * The caller must hold proctree_lock. 308 */ 309 struct pgrp * 310 pgfind(pgid) 311 register pid_t pgid; 312 { 313 register struct pgrp *pgrp; 314 315 sx_assert(&proctree_lock, SX_LOCKED); 316 317 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 318 if (pgrp->pg_id == pgid) { 319 PGRP_LOCK(pgrp); 320 return (pgrp); 321 } 322 } 323 return (NULL); 324 } 325 326 /* 327 * Create a new process group. 328 * pgid must be equal to the pid of p. 329 * Begin a new session if required. 330 */ 331 int 332 enterpgrp(p, pgid, pgrp, sess) 333 register struct proc *p; 334 pid_t pgid; 335 struct pgrp *pgrp; 336 struct session *sess; 337 { 338 struct pgrp *pgrp2; 339 340 sx_assert(&proctree_lock, SX_XLOCKED); 341 342 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 343 KASSERT(p->p_pid == pgid, 344 ("enterpgrp: new pgrp and pid != pgid")); 345 346 pgrp2 = pgfind(pgid); 347 348 KASSERT(pgrp2 == NULL, 349 ("enterpgrp: pgrp with pgid exists")); 350 KASSERT(!SESS_LEADER(p), 351 ("enterpgrp: session leader attempted setpgrp")); 352 353 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 354 355 if (sess != NULL) { 356 /* 357 * new session 358 */ 359 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 360 PROC_LOCK(p); 361 p->p_flag &= ~P_CONTROLT; 362 PROC_UNLOCK(p); 363 PGRP_LOCK(pgrp); 364 sess->s_leader = p; 365 sess->s_sid = p->p_pid; 366 refcount_init(&sess->s_count, 1); 367 sess->s_ttyvp = NULL; 368 sess->s_ttydp = NULL; 369 sess->s_ttyp = NULL; 370 bcopy(p->p_session->s_login, sess->s_login, 371 sizeof(sess->s_login)); 372 pgrp->pg_session = sess; 373 KASSERT(p == curproc, 374 ("enterpgrp: mksession and p != curproc")); 375 } else { 376 pgrp->pg_session = p->p_session; 377 sess_hold(pgrp->pg_session); 378 PGRP_LOCK(pgrp); 379 } 380 pgrp->pg_id = pgid; 381 LIST_INIT(&pgrp->pg_members); 382 383 /* 384 * As we have an exclusive lock of proctree_lock, 385 * this should not deadlock. 386 */ 387 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 388 pgrp->pg_jobc = 0; 389 SLIST_INIT(&pgrp->pg_sigiolst); 390 PGRP_UNLOCK(pgrp); 391 392 doenterpgrp(p, pgrp); 393 394 return (0); 395 } 396 397 /* 398 * Move p to an existing process group 399 */ 400 int 401 enterthispgrp(p, pgrp) 402 register struct proc *p; 403 struct pgrp *pgrp; 404 { 405 406 sx_assert(&proctree_lock, SX_XLOCKED); 407 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 408 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 409 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 410 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 411 KASSERT(pgrp->pg_session == p->p_session, 412 ("%s: pgrp's session %p, p->p_session %p.\n", 413 __func__, 414 pgrp->pg_session, 415 p->p_session)); 416 KASSERT(pgrp != p->p_pgrp, 417 ("%s: p belongs to pgrp.", __func__)); 418 419 doenterpgrp(p, pgrp); 420 421 return (0); 422 } 423 424 /* 425 * Move p to a process group 426 */ 427 static void 428 doenterpgrp(p, pgrp) 429 struct proc *p; 430 struct pgrp *pgrp; 431 { 432 struct pgrp *savepgrp; 433 434 sx_assert(&proctree_lock, SX_XLOCKED); 435 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 436 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 437 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 438 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 439 440 savepgrp = p->p_pgrp; 441 442 /* 443 * Adjust eligibility of affected pgrps to participate in job control. 444 * Increment eligibility counts before decrementing, otherwise we 445 * could reach 0 spuriously during the first call. 446 */ 447 fixjobc(p, pgrp, 1); 448 fixjobc(p, p->p_pgrp, 0); 449 450 PGRP_LOCK(pgrp); 451 PGRP_LOCK(savepgrp); 452 PROC_LOCK(p); 453 LIST_REMOVE(p, p_pglist); 454 p->p_pgrp = pgrp; 455 PROC_UNLOCK(p); 456 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 457 PGRP_UNLOCK(savepgrp); 458 PGRP_UNLOCK(pgrp); 459 if (LIST_EMPTY(&savepgrp->pg_members)) 460 pgdelete(savepgrp); 461 } 462 463 /* 464 * remove process from process group 465 */ 466 int 467 leavepgrp(p) 468 register struct proc *p; 469 { 470 struct pgrp *savepgrp; 471 472 sx_assert(&proctree_lock, SX_XLOCKED); 473 savepgrp = p->p_pgrp; 474 PGRP_LOCK(savepgrp); 475 PROC_LOCK(p); 476 LIST_REMOVE(p, p_pglist); 477 p->p_pgrp = NULL; 478 PROC_UNLOCK(p); 479 PGRP_UNLOCK(savepgrp); 480 if (LIST_EMPTY(&savepgrp->pg_members)) 481 pgdelete(savepgrp); 482 return (0); 483 } 484 485 /* 486 * delete a process group 487 */ 488 static void 489 pgdelete(pgrp) 490 register struct pgrp *pgrp; 491 { 492 struct session *savesess; 493 struct tty *tp; 494 495 sx_assert(&proctree_lock, SX_XLOCKED); 496 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 497 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 498 499 /* 500 * Reset any sigio structures pointing to us as a result of 501 * F_SETOWN with our pgid. 502 */ 503 funsetownlst(&pgrp->pg_sigiolst); 504 505 PGRP_LOCK(pgrp); 506 tp = pgrp->pg_session->s_ttyp; 507 LIST_REMOVE(pgrp, pg_hash); 508 savesess = pgrp->pg_session; 509 PGRP_UNLOCK(pgrp); 510 511 /* Remove the reference to the pgrp before deallocating it. */ 512 if (tp != NULL) { 513 tty_lock(tp); 514 tty_rel_pgrp(tp, pgrp); 515 } 516 517 mtx_destroy(&pgrp->pg_mtx); 518 free(pgrp, M_PGRP); 519 sess_release(savesess); 520 } 521 522 static void 523 pgadjustjobc(pgrp, entering) 524 struct pgrp *pgrp; 525 int entering; 526 { 527 528 PGRP_LOCK(pgrp); 529 if (entering) 530 pgrp->pg_jobc++; 531 else { 532 --pgrp->pg_jobc; 533 if (pgrp->pg_jobc == 0) 534 orphanpg(pgrp); 535 } 536 PGRP_UNLOCK(pgrp); 537 } 538 539 /* 540 * Adjust pgrp jobc counters when specified process changes process group. 541 * We count the number of processes in each process group that "qualify" 542 * the group for terminal job control (those with a parent in a different 543 * process group of the same session). If that count reaches zero, the 544 * process group becomes orphaned. Check both the specified process' 545 * process group and that of its children. 546 * entering == 0 => p is leaving specified group. 547 * entering == 1 => p is entering specified group. 548 */ 549 void 550 fixjobc(p, pgrp, entering) 551 register struct proc *p; 552 register struct pgrp *pgrp; 553 int entering; 554 { 555 register struct pgrp *hispgrp; 556 register struct session *mysession; 557 558 sx_assert(&proctree_lock, SX_LOCKED); 559 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 560 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 561 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 562 563 /* 564 * Check p's parent to see whether p qualifies its own process 565 * group; if so, adjust count for p's process group. 566 */ 567 mysession = pgrp->pg_session; 568 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 569 hispgrp->pg_session == mysession) 570 pgadjustjobc(pgrp, entering); 571 572 /* 573 * Check this process' children to see whether they qualify 574 * their process groups; if so, adjust counts for children's 575 * process groups. 576 */ 577 LIST_FOREACH(p, &p->p_children, p_sibling) { 578 hispgrp = p->p_pgrp; 579 if (hispgrp == pgrp || 580 hispgrp->pg_session != mysession) 581 continue; 582 PROC_LOCK(p); 583 if (p->p_state == PRS_ZOMBIE) { 584 PROC_UNLOCK(p); 585 continue; 586 } 587 PROC_UNLOCK(p); 588 pgadjustjobc(hispgrp, entering); 589 } 590 } 591 592 /* 593 * A process group has become orphaned; 594 * if there are any stopped processes in the group, 595 * hang-up all process in that group. 596 */ 597 static void 598 orphanpg(pg) 599 struct pgrp *pg; 600 { 601 register struct proc *p; 602 603 PGRP_LOCK_ASSERT(pg, MA_OWNED); 604 605 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 606 PROC_LOCK(p); 607 if (P_SHOULDSTOP(p)) { 608 PROC_UNLOCK(p); 609 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 610 PROC_LOCK(p); 611 psignal(p, SIGHUP); 612 psignal(p, SIGCONT); 613 PROC_UNLOCK(p); 614 } 615 return; 616 } 617 PROC_UNLOCK(p); 618 } 619 } 620 621 void 622 sess_hold(struct session *s) 623 { 624 625 refcount_acquire(&s->s_count); 626 } 627 628 void 629 sess_release(struct session *s) 630 { 631 632 if (refcount_release(&s->s_count)) { 633 if (s->s_ttyp != NULL) { 634 tty_lock(s->s_ttyp); 635 tty_rel_sess(s->s_ttyp, s); 636 } 637 mtx_destroy(&s->s_mtx); 638 free(s, M_SESSION); 639 } 640 } 641 642 #include "opt_ddb.h" 643 #ifdef DDB 644 #include <ddb/ddb.h> 645 646 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 647 { 648 register struct pgrp *pgrp; 649 register struct proc *p; 650 register int i; 651 652 for (i = 0; i <= pgrphash; i++) { 653 if (!LIST_EMPTY(&pgrphashtbl[i])) { 654 printf("\tindx %d\n", i); 655 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 656 printf( 657 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 658 (void *)pgrp, (long)pgrp->pg_id, 659 (void *)pgrp->pg_session, 660 pgrp->pg_session->s_count, 661 (void *)LIST_FIRST(&pgrp->pg_members)); 662 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 663 printf("\t\tpid %ld addr %p pgrp %p\n", 664 (long)p->p_pid, (void *)p, 665 (void *)p->p_pgrp); 666 } 667 } 668 } 669 } 670 } 671 #endif /* DDB */ 672 673 /* 674 * Calculate the kinfo_proc members which contain process-wide 675 * informations. 676 * Must be called with the target process locked. 677 */ 678 static void 679 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) 680 { 681 struct thread *td; 682 683 PROC_LOCK_ASSERT(p, MA_OWNED); 684 685 kp->ki_estcpu = 0; 686 kp->ki_pctcpu = 0; 687 FOREACH_THREAD_IN_PROC(p, td) { 688 thread_lock(td); 689 kp->ki_pctcpu += sched_pctcpu(td); 690 kp->ki_estcpu += td->td_estcpu; 691 thread_unlock(td); 692 } 693 } 694 695 /* 696 * Clear kinfo_proc and fill in any information that is common 697 * to all threads in the process. 698 * Must be called with the target process locked. 699 */ 700 static void 701 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 702 { 703 struct thread *td0; 704 struct tty *tp; 705 struct session *sp; 706 struct ucred *cred; 707 struct sigacts *ps; 708 709 PROC_LOCK_ASSERT(p, MA_OWNED); 710 bzero(kp, sizeof(*kp)); 711 712 kp->ki_structsize = sizeof(*kp); 713 kp->ki_paddr = p; 714 kp->ki_addr =/* p->p_addr; */0; /* XXX */ 715 kp->ki_args = p->p_args; 716 kp->ki_textvp = p->p_textvp; 717 #ifdef KTRACE 718 kp->ki_tracep = p->p_tracevp; 719 kp->ki_traceflag = p->p_traceflag; 720 #endif 721 kp->ki_fd = p->p_fd; 722 kp->ki_vmspace = p->p_vmspace; 723 kp->ki_flag = p->p_flag; 724 cred = p->p_ucred; 725 if (cred) { 726 kp->ki_uid = cred->cr_uid; 727 kp->ki_ruid = cred->cr_ruid; 728 kp->ki_svuid = cred->cr_svuid; 729 kp->ki_cr_flags = 0; 730 if (cred->cr_flags & CRED_FLAG_CAPMODE) 731 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE; 732 /* XXX bde doesn't like KI_NGROUPS */ 733 if (cred->cr_ngroups > KI_NGROUPS) { 734 kp->ki_ngroups = KI_NGROUPS; 735 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 736 } else 737 kp->ki_ngroups = cred->cr_ngroups; 738 bcopy(cred->cr_groups, kp->ki_groups, 739 kp->ki_ngroups * sizeof(gid_t)); 740 kp->ki_rgid = cred->cr_rgid; 741 kp->ki_svgid = cred->cr_svgid; 742 /* If jailed(cred), emulate the old P_JAILED flag. */ 743 if (jailed(cred)) { 744 kp->ki_flag |= P_JAILED; 745 /* If inside the jail, use 0 as a jail ID. */ 746 if (cred->cr_prison != curthread->td_ucred->cr_prison) 747 kp->ki_jid = cred->cr_prison->pr_id; 748 } 749 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name, 750 sizeof(kp->ki_loginclass)); 751 } 752 ps = p->p_sigacts; 753 if (ps) { 754 mtx_lock(&ps->ps_mtx); 755 kp->ki_sigignore = ps->ps_sigignore; 756 kp->ki_sigcatch = ps->ps_sigcatch; 757 mtx_unlock(&ps->ps_mtx); 758 } 759 if (p->p_state != PRS_NEW && 760 p->p_state != PRS_ZOMBIE && 761 p->p_vmspace != NULL) { 762 struct vmspace *vm = p->p_vmspace; 763 764 kp->ki_size = vm->vm_map.size; 765 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 766 FOREACH_THREAD_IN_PROC(p, td0) { 767 if (!TD_IS_SWAPPED(td0)) 768 kp->ki_rssize += td0->td_kstack_pages; 769 } 770 kp->ki_swrss = vm->vm_swrss; 771 kp->ki_tsize = vm->vm_tsize; 772 kp->ki_dsize = vm->vm_dsize; 773 kp->ki_ssize = vm->vm_ssize; 774 } else if (p->p_state == PRS_ZOMBIE) 775 kp->ki_stat = SZOMB; 776 if (kp->ki_flag & P_INMEM) 777 kp->ki_sflag = PS_INMEM; 778 else 779 kp->ki_sflag = 0; 780 /* Calculate legacy swtime as seconds since 'swtick'. */ 781 kp->ki_swtime = (ticks - p->p_swtick) / hz; 782 kp->ki_pid = p->p_pid; 783 kp->ki_nice = p->p_nice; 784 kp->ki_start = p->p_stats->p_start; 785 timevaladd(&kp->ki_start, &boottime); 786 PROC_SLOCK(p); 787 rufetch(p, &kp->ki_rusage); 788 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 789 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 790 PROC_SUNLOCK(p); 791 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 792 /* Some callers want child times in a single value. */ 793 kp->ki_childtime = kp->ki_childstime; 794 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 795 796 tp = NULL; 797 if (p->p_pgrp) { 798 kp->ki_pgid = p->p_pgrp->pg_id; 799 kp->ki_jobc = p->p_pgrp->pg_jobc; 800 sp = p->p_pgrp->pg_session; 801 802 if (sp != NULL) { 803 kp->ki_sid = sp->s_sid; 804 SESS_LOCK(sp); 805 strlcpy(kp->ki_login, sp->s_login, 806 sizeof(kp->ki_login)); 807 if (sp->s_ttyvp) 808 kp->ki_kiflag |= KI_CTTY; 809 if (SESS_LEADER(p)) 810 kp->ki_kiflag |= KI_SLEADER; 811 /* XXX proctree_lock */ 812 tp = sp->s_ttyp; 813 SESS_UNLOCK(sp); 814 } 815 } 816 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 817 kp->ki_tdev = tty_udev(tp); 818 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 819 if (tp->t_session) 820 kp->ki_tsid = tp->t_session->s_sid; 821 } else 822 kp->ki_tdev = NODEV; 823 if (p->p_comm[0] != '\0') 824 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 825 if (p->p_sysent && p->p_sysent->sv_name != NULL && 826 p->p_sysent->sv_name[0] != '\0') 827 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 828 kp->ki_siglist = p->p_siglist; 829 kp->ki_xstat = p->p_xstat; 830 kp->ki_acflag = p->p_acflag; 831 kp->ki_lock = p->p_lock; 832 if (p->p_pptr) 833 kp->ki_ppid = p->p_pptr->p_pid; 834 } 835 836 /* 837 * Fill in information that is thread specific. Must be called with 838 * target process locked. If 'preferthread' is set, overwrite certain 839 * process-related fields that are maintained for both threads and 840 * processes. 841 */ 842 static void 843 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 844 { 845 struct proc *p; 846 847 p = td->td_proc; 848 kp->ki_tdaddr = td; 849 PROC_LOCK_ASSERT(p, MA_OWNED); 850 851 thread_lock(td); 852 if (td->td_wmesg != NULL) 853 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 854 else 855 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 856 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 857 if (TD_ON_LOCK(td)) { 858 kp->ki_kiflag |= KI_LOCKBLOCK; 859 strlcpy(kp->ki_lockname, td->td_lockname, 860 sizeof(kp->ki_lockname)); 861 } else { 862 kp->ki_kiflag &= ~KI_LOCKBLOCK; 863 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 864 } 865 866 if (p->p_state == PRS_NORMAL) { /* approximate. */ 867 if (TD_ON_RUNQ(td) || 868 TD_CAN_RUN(td) || 869 TD_IS_RUNNING(td)) { 870 kp->ki_stat = SRUN; 871 } else if (P_SHOULDSTOP(p)) { 872 kp->ki_stat = SSTOP; 873 } else if (TD_IS_SLEEPING(td)) { 874 kp->ki_stat = SSLEEP; 875 } else if (TD_ON_LOCK(td)) { 876 kp->ki_stat = SLOCK; 877 } else { 878 kp->ki_stat = SWAIT; 879 } 880 } else if (p->p_state == PRS_ZOMBIE) { 881 kp->ki_stat = SZOMB; 882 } else { 883 kp->ki_stat = SIDL; 884 } 885 886 /* Things in the thread */ 887 kp->ki_wchan = td->td_wchan; 888 kp->ki_pri.pri_level = td->td_priority; 889 kp->ki_pri.pri_native = td->td_base_pri; 890 kp->ki_lastcpu = td->td_lastcpu; 891 kp->ki_oncpu = td->td_oncpu; 892 kp->ki_tdflags = td->td_flags; 893 kp->ki_tid = td->td_tid; 894 kp->ki_numthreads = p->p_numthreads; 895 kp->ki_pcb = td->td_pcb; 896 kp->ki_kstack = (void *)td->td_kstack; 897 kp->ki_slptime = (ticks - td->td_slptick) / hz; 898 kp->ki_pri.pri_class = td->td_pri_class; 899 kp->ki_pri.pri_user = td->td_user_pri; 900 901 if (preferthread) { 902 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 903 kp->ki_pctcpu = sched_pctcpu(td); 904 kp->ki_estcpu = td->td_estcpu; 905 } 906 907 /* We can't get this anymore but ps etc never used it anyway. */ 908 kp->ki_rqindex = 0; 909 910 if (preferthread) 911 kp->ki_siglist = td->td_siglist; 912 kp->ki_sigmask = td->td_sigmask; 913 thread_unlock(td); 914 } 915 916 /* 917 * Fill in a kinfo_proc structure for the specified process. 918 * Must be called with the target process locked. 919 */ 920 void 921 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 922 { 923 924 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 925 926 fill_kinfo_proc_only(p, kp); 927 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 928 fill_kinfo_aggregate(p, kp); 929 } 930 931 struct pstats * 932 pstats_alloc(void) 933 { 934 935 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 936 } 937 938 /* 939 * Copy parts of p_stats; zero the rest of p_stats (statistics). 940 */ 941 void 942 pstats_fork(struct pstats *src, struct pstats *dst) 943 { 944 945 bzero(&dst->pstat_startzero, 946 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 947 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 948 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 949 } 950 951 void 952 pstats_free(struct pstats *ps) 953 { 954 955 free(ps, M_SUBPROC); 956 } 957 958 /* 959 * Locate a zombie process by number 960 */ 961 struct proc * 962 zpfind(pid_t pid) 963 { 964 struct proc *p; 965 966 sx_slock(&allproc_lock); 967 LIST_FOREACH(p, &zombproc, p_list) 968 if (p->p_pid == pid) { 969 PROC_LOCK(p); 970 break; 971 } 972 sx_sunlock(&allproc_lock); 973 return (p); 974 } 975 976 #define KERN_PROC_ZOMBMASK 0x3 977 #define KERN_PROC_NOTHREADS 0x4 978 979 #ifdef COMPAT_FREEBSD32 980 981 /* 982 * This function is typically used to copy out the kernel address, so 983 * it can be replaced by assignment of zero. 984 */ 985 static inline uint32_t 986 ptr32_trim(void *ptr) 987 { 988 uintptr_t uptr; 989 990 uptr = (uintptr_t)ptr; 991 return ((uptr > UINT_MAX) ? 0 : uptr); 992 } 993 994 #define PTRTRIM_CP(src,dst,fld) \ 995 do { (dst).fld = ptr32_trim((src).fld); } while (0) 996 997 static void 998 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 999 { 1000 int i; 1001 1002 bzero(ki32, sizeof(struct kinfo_proc32)); 1003 ki32->ki_structsize = sizeof(struct kinfo_proc32); 1004 CP(*ki, *ki32, ki_layout); 1005 PTRTRIM_CP(*ki, *ki32, ki_args); 1006 PTRTRIM_CP(*ki, *ki32, ki_paddr); 1007 PTRTRIM_CP(*ki, *ki32, ki_addr); 1008 PTRTRIM_CP(*ki, *ki32, ki_tracep); 1009 PTRTRIM_CP(*ki, *ki32, ki_textvp); 1010 PTRTRIM_CP(*ki, *ki32, ki_fd); 1011 PTRTRIM_CP(*ki, *ki32, ki_vmspace); 1012 PTRTRIM_CP(*ki, *ki32, ki_wchan); 1013 CP(*ki, *ki32, ki_pid); 1014 CP(*ki, *ki32, ki_ppid); 1015 CP(*ki, *ki32, ki_pgid); 1016 CP(*ki, *ki32, ki_tpgid); 1017 CP(*ki, *ki32, ki_sid); 1018 CP(*ki, *ki32, ki_tsid); 1019 CP(*ki, *ki32, ki_jobc); 1020 CP(*ki, *ki32, ki_tdev); 1021 CP(*ki, *ki32, ki_siglist); 1022 CP(*ki, *ki32, ki_sigmask); 1023 CP(*ki, *ki32, ki_sigignore); 1024 CP(*ki, *ki32, ki_sigcatch); 1025 CP(*ki, *ki32, ki_uid); 1026 CP(*ki, *ki32, ki_ruid); 1027 CP(*ki, *ki32, ki_svuid); 1028 CP(*ki, *ki32, ki_rgid); 1029 CP(*ki, *ki32, ki_svgid); 1030 CP(*ki, *ki32, ki_ngroups); 1031 for (i = 0; i < KI_NGROUPS; i++) 1032 CP(*ki, *ki32, ki_groups[i]); 1033 CP(*ki, *ki32, ki_size); 1034 CP(*ki, *ki32, ki_rssize); 1035 CP(*ki, *ki32, ki_swrss); 1036 CP(*ki, *ki32, ki_tsize); 1037 CP(*ki, *ki32, ki_dsize); 1038 CP(*ki, *ki32, ki_ssize); 1039 CP(*ki, *ki32, ki_xstat); 1040 CP(*ki, *ki32, ki_acflag); 1041 CP(*ki, *ki32, ki_pctcpu); 1042 CP(*ki, *ki32, ki_estcpu); 1043 CP(*ki, *ki32, ki_slptime); 1044 CP(*ki, *ki32, ki_swtime); 1045 CP(*ki, *ki32, ki_runtime); 1046 TV_CP(*ki, *ki32, ki_start); 1047 TV_CP(*ki, *ki32, ki_childtime); 1048 CP(*ki, *ki32, ki_flag); 1049 CP(*ki, *ki32, ki_kiflag); 1050 CP(*ki, *ki32, ki_traceflag); 1051 CP(*ki, *ki32, ki_stat); 1052 CP(*ki, *ki32, ki_nice); 1053 CP(*ki, *ki32, ki_lock); 1054 CP(*ki, *ki32, ki_rqindex); 1055 CP(*ki, *ki32, ki_oncpu); 1056 CP(*ki, *ki32, ki_lastcpu); 1057 bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1); 1058 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 1059 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 1060 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 1061 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 1062 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 1063 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); 1064 CP(*ki, *ki32, ki_cr_flags); 1065 CP(*ki, *ki32, ki_jid); 1066 CP(*ki, *ki32, ki_numthreads); 1067 CP(*ki, *ki32, ki_tid); 1068 CP(*ki, *ki32, ki_pri); 1069 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 1070 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 1071 PTRTRIM_CP(*ki, *ki32, ki_pcb); 1072 PTRTRIM_CP(*ki, *ki32, ki_kstack); 1073 PTRTRIM_CP(*ki, *ki32, ki_udata); 1074 CP(*ki, *ki32, ki_sflag); 1075 CP(*ki, *ki32, ki_tdflags); 1076 } 1077 1078 static int 1079 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1080 { 1081 struct kinfo_proc32 ki32; 1082 int error; 1083 1084 if (req->flags & SCTL_MASK32) { 1085 freebsd32_kinfo_proc_out(ki, &ki32); 1086 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32)); 1087 } else 1088 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)); 1089 return (error); 1090 } 1091 #else 1092 static int 1093 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1094 { 1095 1096 return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc))); 1097 } 1098 #endif 1099 1100 /* 1101 * Must be called with the process locked and will return with it unlocked. 1102 */ 1103 static int 1104 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 1105 { 1106 struct thread *td; 1107 struct kinfo_proc kinfo_proc; 1108 int error = 0; 1109 struct proc *np; 1110 pid_t pid = p->p_pid; 1111 1112 PROC_LOCK_ASSERT(p, MA_OWNED); 1113 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1114 1115 fill_kinfo_proc(p, &kinfo_proc); 1116 if (flags & KERN_PROC_NOTHREADS) 1117 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1118 else { 1119 FOREACH_THREAD_IN_PROC(p, td) { 1120 fill_kinfo_thread(td, &kinfo_proc, 1); 1121 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1122 if (error) 1123 break; 1124 } 1125 } 1126 PROC_UNLOCK(p); 1127 if (error) 1128 return (error); 1129 if (flags & KERN_PROC_ZOMBMASK) 1130 np = zpfind(pid); 1131 else { 1132 if (pid == 0) 1133 return (0); 1134 np = pfind(pid); 1135 } 1136 if (np == NULL) 1137 return (ESRCH); 1138 if (np != p) { 1139 PROC_UNLOCK(np); 1140 return (ESRCH); 1141 } 1142 PROC_UNLOCK(np); 1143 return (0); 1144 } 1145 1146 static int 1147 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1148 { 1149 int *name = (int*) arg1; 1150 u_int namelen = arg2; 1151 struct proc *p; 1152 int flags, doingzomb, oid_number; 1153 int error = 0; 1154 1155 oid_number = oidp->oid_number; 1156 if (oid_number != KERN_PROC_ALL && 1157 (oid_number & KERN_PROC_INC_THREAD) == 0) 1158 flags = KERN_PROC_NOTHREADS; 1159 else { 1160 flags = 0; 1161 oid_number &= ~KERN_PROC_INC_THREAD; 1162 } 1163 if (oid_number == KERN_PROC_PID) { 1164 if (namelen != 1) 1165 return (EINVAL); 1166 error = sysctl_wire_old_buffer(req, 0); 1167 if (error) 1168 return (error); 1169 p = pfind((pid_t)name[0]); 1170 if (!p) 1171 return (ESRCH); 1172 if ((error = p_cansee(curthread, p))) { 1173 PROC_UNLOCK(p); 1174 return (error); 1175 } 1176 error = sysctl_out_proc(p, req, flags); 1177 return (error); 1178 } 1179 1180 switch (oid_number) { 1181 case KERN_PROC_ALL: 1182 if (namelen != 0) 1183 return (EINVAL); 1184 break; 1185 case KERN_PROC_PROC: 1186 if (namelen != 0 && namelen != 1) 1187 return (EINVAL); 1188 break; 1189 default: 1190 if (namelen != 1) 1191 return (EINVAL); 1192 break; 1193 } 1194 1195 if (!req->oldptr) { 1196 /* overestimate by 5 procs */ 1197 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1198 if (error) 1199 return (error); 1200 } 1201 error = sysctl_wire_old_buffer(req, 0); 1202 if (error != 0) 1203 return (error); 1204 sx_slock(&allproc_lock); 1205 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1206 if (!doingzomb) 1207 p = LIST_FIRST(&allproc); 1208 else 1209 p = LIST_FIRST(&zombproc); 1210 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1211 /* 1212 * Skip embryonic processes. 1213 */ 1214 PROC_LOCK(p); 1215 if (p->p_state == PRS_NEW) { 1216 PROC_UNLOCK(p); 1217 continue; 1218 } 1219 KASSERT(p->p_ucred != NULL, 1220 ("process credential is NULL for non-NEW proc")); 1221 /* 1222 * Show a user only appropriate processes. 1223 */ 1224 if (p_cansee(curthread, p)) { 1225 PROC_UNLOCK(p); 1226 continue; 1227 } 1228 /* 1229 * TODO - make more efficient (see notes below). 1230 * do by session. 1231 */ 1232 switch (oid_number) { 1233 1234 case KERN_PROC_GID: 1235 if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1236 PROC_UNLOCK(p); 1237 continue; 1238 } 1239 break; 1240 1241 case KERN_PROC_PGRP: 1242 /* could do this by traversing pgrp */ 1243 if (p->p_pgrp == NULL || 1244 p->p_pgrp->pg_id != (pid_t)name[0]) { 1245 PROC_UNLOCK(p); 1246 continue; 1247 } 1248 break; 1249 1250 case KERN_PROC_RGID: 1251 if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1252 PROC_UNLOCK(p); 1253 continue; 1254 } 1255 break; 1256 1257 case KERN_PROC_SESSION: 1258 if (p->p_session == NULL || 1259 p->p_session->s_sid != (pid_t)name[0]) { 1260 PROC_UNLOCK(p); 1261 continue; 1262 } 1263 break; 1264 1265 case KERN_PROC_TTY: 1266 if ((p->p_flag & P_CONTROLT) == 0 || 1267 p->p_session == NULL) { 1268 PROC_UNLOCK(p); 1269 continue; 1270 } 1271 /* XXX proctree_lock */ 1272 SESS_LOCK(p->p_session); 1273 if (p->p_session->s_ttyp == NULL || 1274 tty_udev(p->p_session->s_ttyp) != 1275 (dev_t)name[0]) { 1276 SESS_UNLOCK(p->p_session); 1277 PROC_UNLOCK(p); 1278 continue; 1279 } 1280 SESS_UNLOCK(p->p_session); 1281 break; 1282 1283 case KERN_PROC_UID: 1284 if (p->p_ucred->cr_uid != (uid_t)name[0]) { 1285 PROC_UNLOCK(p); 1286 continue; 1287 } 1288 break; 1289 1290 case KERN_PROC_RUID: 1291 if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 1292 PROC_UNLOCK(p); 1293 continue; 1294 } 1295 break; 1296 1297 case KERN_PROC_PROC: 1298 break; 1299 1300 default: 1301 break; 1302 1303 } 1304 1305 error = sysctl_out_proc(p, req, flags | doingzomb); 1306 if (error) { 1307 sx_sunlock(&allproc_lock); 1308 return (error); 1309 } 1310 } 1311 } 1312 sx_sunlock(&allproc_lock); 1313 return (0); 1314 } 1315 1316 struct pargs * 1317 pargs_alloc(int len) 1318 { 1319 struct pargs *pa; 1320 1321 pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1322 M_WAITOK); 1323 refcount_init(&pa->ar_ref, 1); 1324 pa->ar_length = len; 1325 return (pa); 1326 } 1327 1328 static void 1329 pargs_free(struct pargs *pa) 1330 { 1331 1332 free(pa, M_PARGS); 1333 } 1334 1335 void 1336 pargs_hold(struct pargs *pa) 1337 { 1338 1339 if (pa == NULL) 1340 return; 1341 refcount_acquire(&pa->ar_ref); 1342 } 1343 1344 void 1345 pargs_drop(struct pargs *pa) 1346 { 1347 1348 if (pa == NULL) 1349 return; 1350 if (refcount_release(&pa->ar_ref)) 1351 pargs_free(pa); 1352 } 1353 1354 /* 1355 * This sysctl allows a process to retrieve the argument list or process 1356 * title for another process without groping around in the address space 1357 * of the other process. It also allow a process to set its own "process 1358 * title to a string of its own choice. 1359 */ 1360 static int 1361 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1362 { 1363 int *name = (int*) arg1; 1364 u_int namelen = arg2; 1365 struct pargs *newpa, *pa; 1366 struct proc *p; 1367 int error = 0; 1368 1369 if (namelen != 1) 1370 return (EINVAL); 1371 1372 p = pfind((pid_t)name[0]); 1373 if (!p) 1374 return (ESRCH); 1375 1376 if ((error = p_cansee(curthread, p)) != 0) { 1377 PROC_UNLOCK(p); 1378 return (error); 1379 } 1380 1381 if (req->newptr && curproc != p) { 1382 PROC_UNLOCK(p); 1383 return (EPERM); 1384 } 1385 1386 pa = p->p_args; 1387 pargs_hold(pa); 1388 PROC_UNLOCK(p); 1389 if (req->oldptr != NULL && pa != NULL) 1390 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1391 pargs_drop(pa); 1392 if (error != 0 || req->newptr == NULL) 1393 return (error); 1394 1395 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1396 return (ENOMEM); 1397 newpa = pargs_alloc(req->newlen); 1398 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1399 if (error != 0) { 1400 pargs_free(newpa); 1401 return (error); 1402 } 1403 PROC_LOCK(p); 1404 pa = p->p_args; 1405 p->p_args = newpa; 1406 PROC_UNLOCK(p); 1407 pargs_drop(pa); 1408 return (0); 1409 } 1410 1411 /* 1412 * This sysctl allows a process to retrieve the path of the executable for 1413 * itself or another process. 1414 */ 1415 static int 1416 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1417 { 1418 pid_t *pidp = (pid_t *)arg1; 1419 unsigned int arglen = arg2; 1420 struct proc *p; 1421 struct vnode *vp; 1422 char *retbuf, *freebuf; 1423 int error, vfslocked; 1424 1425 if (arglen != 1) 1426 return (EINVAL); 1427 if (*pidp == -1) { /* -1 means this process */ 1428 p = req->td->td_proc; 1429 } else { 1430 p = pfind(*pidp); 1431 if (p == NULL) 1432 return (ESRCH); 1433 if ((error = p_cansee(curthread, p)) != 0) { 1434 PROC_UNLOCK(p); 1435 return (error); 1436 } 1437 } 1438 1439 vp = p->p_textvp; 1440 if (vp == NULL) { 1441 if (*pidp != -1) 1442 PROC_UNLOCK(p); 1443 return (0); 1444 } 1445 vref(vp); 1446 if (*pidp != -1) 1447 PROC_UNLOCK(p); 1448 error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1449 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1450 vrele(vp); 1451 VFS_UNLOCK_GIANT(vfslocked); 1452 if (error) 1453 return (error); 1454 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1455 free(freebuf, M_TEMP); 1456 return (error); 1457 } 1458 1459 static int 1460 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1461 { 1462 struct proc *p; 1463 char *sv_name; 1464 int *name; 1465 int namelen; 1466 int error; 1467 1468 namelen = arg2; 1469 if (namelen != 1) 1470 return (EINVAL); 1471 1472 name = (int *)arg1; 1473 if ((p = pfind((pid_t)name[0])) == NULL) 1474 return (ESRCH); 1475 if ((error = p_cansee(curthread, p))) { 1476 PROC_UNLOCK(p); 1477 return (error); 1478 } 1479 sv_name = p->p_sysent->sv_name; 1480 PROC_UNLOCK(p); 1481 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1482 } 1483 1484 #ifdef KINFO_OVMENTRY_SIZE 1485 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 1486 #endif 1487 1488 #ifdef COMPAT_FREEBSD7 1489 static int 1490 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 1491 { 1492 vm_map_entry_t entry, tmp_entry; 1493 unsigned int last_timestamp; 1494 char *fullpath, *freepath; 1495 struct kinfo_ovmentry *kve; 1496 struct vattr va; 1497 struct ucred *cred; 1498 int error, *name; 1499 struct vnode *vp; 1500 struct proc *p; 1501 vm_map_t map; 1502 struct vmspace *vm; 1503 1504 name = (int *)arg1; 1505 if ((p = pfind((pid_t)name[0])) == NULL) 1506 return (ESRCH); 1507 if (p->p_flag & P_WEXIT) { 1508 PROC_UNLOCK(p); 1509 return (ESRCH); 1510 } 1511 if ((error = p_candebug(curthread, p))) { 1512 PROC_UNLOCK(p); 1513 return (error); 1514 } 1515 _PHOLD(p); 1516 PROC_UNLOCK(p); 1517 vm = vmspace_acquire_ref(p); 1518 if (vm == NULL) { 1519 PRELE(p); 1520 return (ESRCH); 1521 } 1522 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1523 1524 map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */ 1525 vm_map_lock_read(map); 1526 for (entry = map->header.next; entry != &map->header; 1527 entry = entry->next) { 1528 vm_object_t obj, tobj, lobj; 1529 vm_offset_t addr; 1530 int vfslocked; 1531 1532 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1533 continue; 1534 1535 bzero(kve, sizeof(*kve)); 1536 kve->kve_structsize = sizeof(*kve); 1537 1538 kve->kve_private_resident = 0; 1539 obj = entry->object.vm_object; 1540 if (obj != NULL) { 1541 VM_OBJECT_LOCK(obj); 1542 if (obj->shadow_count == 1) 1543 kve->kve_private_resident = 1544 obj->resident_page_count; 1545 } 1546 kve->kve_resident = 0; 1547 addr = entry->start; 1548 while (addr < entry->end) { 1549 if (pmap_extract(map->pmap, addr)) 1550 kve->kve_resident++; 1551 addr += PAGE_SIZE; 1552 } 1553 1554 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1555 if (tobj != obj) 1556 VM_OBJECT_LOCK(tobj); 1557 if (lobj != obj) 1558 VM_OBJECT_UNLOCK(lobj); 1559 lobj = tobj; 1560 } 1561 1562 kve->kve_start = (void*)entry->start; 1563 kve->kve_end = (void*)entry->end; 1564 kve->kve_offset = (off_t)entry->offset; 1565 1566 if (entry->protection & VM_PROT_READ) 1567 kve->kve_protection |= KVME_PROT_READ; 1568 if (entry->protection & VM_PROT_WRITE) 1569 kve->kve_protection |= KVME_PROT_WRITE; 1570 if (entry->protection & VM_PROT_EXECUTE) 1571 kve->kve_protection |= KVME_PROT_EXEC; 1572 1573 if (entry->eflags & MAP_ENTRY_COW) 1574 kve->kve_flags |= KVME_FLAG_COW; 1575 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1576 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1577 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 1578 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 1579 1580 last_timestamp = map->timestamp; 1581 vm_map_unlock_read(map); 1582 1583 kve->kve_fileid = 0; 1584 kve->kve_fsid = 0; 1585 freepath = NULL; 1586 fullpath = ""; 1587 if (lobj) { 1588 vp = NULL; 1589 switch (lobj->type) { 1590 case OBJT_DEFAULT: 1591 kve->kve_type = KVME_TYPE_DEFAULT; 1592 break; 1593 case OBJT_VNODE: 1594 kve->kve_type = KVME_TYPE_VNODE; 1595 vp = lobj->handle; 1596 vref(vp); 1597 break; 1598 case OBJT_SWAP: 1599 kve->kve_type = KVME_TYPE_SWAP; 1600 break; 1601 case OBJT_DEVICE: 1602 kve->kve_type = KVME_TYPE_DEVICE; 1603 break; 1604 case OBJT_PHYS: 1605 kve->kve_type = KVME_TYPE_PHYS; 1606 break; 1607 case OBJT_DEAD: 1608 kve->kve_type = KVME_TYPE_DEAD; 1609 break; 1610 case OBJT_SG: 1611 kve->kve_type = KVME_TYPE_SG; 1612 break; 1613 default: 1614 kve->kve_type = KVME_TYPE_UNKNOWN; 1615 break; 1616 } 1617 if (lobj != obj) 1618 VM_OBJECT_UNLOCK(lobj); 1619 1620 kve->kve_ref_count = obj->ref_count; 1621 kve->kve_shadow_count = obj->shadow_count; 1622 VM_OBJECT_UNLOCK(obj); 1623 if (vp != NULL) { 1624 vn_fullpath(curthread, vp, &fullpath, 1625 &freepath); 1626 cred = curthread->td_ucred; 1627 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1628 vn_lock(vp, LK_SHARED | LK_RETRY); 1629 if (VOP_GETATTR(vp, &va, cred) == 0) { 1630 kve->kve_fileid = va.va_fileid; 1631 kve->kve_fsid = va.va_fsid; 1632 } 1633 vput(vp); 1634 VFS_UNLOCK_GIANT(vfslocked); 1635 } 1636 } else { 1637 kve->kve_type = KVME_TYPE_NONE; 1638 kve->kve_ref_count = 0; 1639 kve->kve_shadow_count = 0; 1640 } 1641 1642 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1643 if (freepath != NULL) 1644 free(freepath, M_TEMP); 1645 1646 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 1647 vm_map_lock_read(map); 1648 if (error) 1649 break; 1650 if (last_timestamp != map->timestamp) { 1651 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1652 entry = tmp_entry; 1653 } 1654 } 1655 vm_map_unlock_read(map); 1656 vmspace_free(vm); 1657 PRELE(p); 1658 free(kve, M_TEMP); 1659 return (error); 1660 } 1661 #endif /* COMPAT_FREEBSD7 */ 1662 1663 #ifdef KINFO_VMENTRY_SIZE 1664 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 1665 #endif 1666 1667 static int 1668 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 1669 { 1670 vm_map_entry_t entry, tmp_entry; 1671 unsigned int last_timestamp; 1672 char *fullpath, *freepath; 1673 struct kinfo_vmentry *kve; 1674 struct vattr va; 1675 struct ucred *cred; 1676 int error, *name; 1677 struct vnode *vp; 1678 struct proc *p; 1679 struct vmspace *vm; 1680 vm_map_t map; 1681 1682 name = (int *)arg1; 1683 if ((p = pfind((pid_t)name[0])) == NULL) 1684 return (ESRCH); 1685 if (p->p_flag & P_WEXIT) { 1686 PROC_UNLOCK(p); 1687 return (ESRCH); 1688 } 1689 if ((error = p_candebug(curthread, p))) { 1690 PROC_UNLOCK(p); 1691 return (error); 1692 } 1693 _PHOLD(p); 1694 PROC_UNLOCK(p); 1695 vm = vmspace_acquire_ref(p); 1696 if (vm == NULL) { 1697 PRELE(p); 1698 return (ESRCH); 1699 } 1700 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1701 1702 map = &vm->vm_map; /* XXXRW: More locking required? */ 1703 vm_map_lock_read(map); 1704 for (entry = map->header.next; entry != &map->header; 1705 entry = entry->next) { 1706 vm_object_t obj, tobj, lobj; 1707 vm_offset_t addr; 1708 int vfslocked; 1709 1710 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1711 continue; 1712 1713 bzero(kve, sizeof(*kve)); 1714 1715 kve->kve_private_resident = 0; 1716 obj = entry->object.vm_object; 1717 if (obj != NULL) { 1718 VM_OBJECT_LOCK(obj); 1719 if (obj->shadow_count == 1) 1720 kve->kve_private_resident = 1721 obj->resident_page_count; 1722 } 1723 kve->kve_resident = 0; 1724 addr = entry->start; 1725 while (addr < entry->end) { 1726 if (pmap_extract(map->pmap, addr)) 1727 kve->kve_resident++; 1728 addr += PAGE_SIZE; 1729 } 1730 1731 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1732 if (tobj != obj) 1733 VM_OBJECT_LOCK(tobj); 1734 if (lobj != obj) 1735 VM_OBJECT_UNLOCK(lobj); 1736 lobj = tobj; 1737 } 1738 1739 kve->kve_start = entry->start; 1740 kve->kve_end = entry->end; 1741 kve->kve_offset = entry->offset; 1742 1743 if (entry->protection & VM_PROT_READ) 1744 kve->kve_protection |= KVME_PROT_READ; 1745 if (entry->protection & VM_PROT_WRITE) 1746 kve->kve_protection |= KVME_PROT_WRITE; 1747 if (entry->protection & VM_PROT_EXECUTE) 1748 kve->kve_protection |= KVME_PROT_EXEC; 1749 1750 if (entry->eflags & MAP_ENTRY_COW) 1751 kve->kve_flags |= KVME_FLAG_COW; 1752 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1753 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1754 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 1755 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 1756 1757 last_timestamp = map->timestamp; 1758 vm_map_unlock_read(map); 1759 1760 freepath = NULL; 1761 fullpath = ""; 1762 if (lobj) { 1763 vp = NULL; 1764 switch (lobj->type) { 1765 case OBJT_DEFAULT: 1766 kve->kve_type = KVME_TYPE_DEFAULT; 1767 break; 1768 case OBJT_VNODE: 1769 kve->kve_type = KVME_TYPE_VNODE; 1770 vp = lobj->handle; 1771 vref(vp); 1772 break; 1773 case OBJT_SWAP: 1774 kve->kve_type = KVME_TYPE_SWAP; 1775 break; 1776 case OBJT_DEVICE: 1777 kve->kve_type = KVME_TYPE_DEVICE; 1778 break; 1779 case OBJT_PHYS: 1780 kve->kve_type = KVME_TYPE_PHYS; 1781 break; 1782 case OBJT_DEAD: 1783 kve->kve_type = KVME_TYPE_DEAD; 1784 break; 1785 case OBJT_SG: 1786 kve->kve_type = KVME_TYPE_SG; 1787 break; 1788 default: 1789 kve->kve_type = KVME_TYPE_UNKNOWN; 1790 break; 1791 } 1792 if (lobj != obj) 1793 VM_OBJECT_UNLOCK(lobj); 1794 1795 kve->kve_ref_count = obj->ref_count; 1796 kve->kve_shadow_count = obj->shadow_count; 1797 VM_OBJECT_UNLOCK(obj); 1798 if (vp != NULL) { 1799 vn_fullpath(curthread, vp, &fullpath, 1800 &freepath); 1801 kve->kve_vn_type = vntype_to_kinfo(vp->v_type); 1802 cred = curthread->td_ucred; 1803 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1804 vn_lock(vp, LK_SHARED | LK_RETRY); 1805 if (VOP_GETATTR(vp, &va, cred) == 0) { 1806 kve->kve_vn_fileid = va.va_fileid; 1807 kve->kve_vn_fsid = va.va_fsid; 1808 kve->kve_vn_mode = 1809 MAKEIMODE(va.va_type, va.va_mode); 1810 kve->kve_vn_size = va.va_size; 1811 kve->kve_vn_rdev = va.va_rdev; 1812 kve->kve_status = KF_ATTR_VALID; 1813 } 1814 vput(vp); 1815 VFS_UNLOCK_GIANT(vfslocked); 1816 } 1817 } else { 1818 kve->kve_type = KVME_TYPE_NONE; 1819 kve->kve_ref_count = 0; 1820 kve->kve_shadow_count = 0; 1821 } 1822 1823 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1824 if (freepath != NULL) 1825 free(freepath, M_TEMP); 1826 1827 /* Pack record size down */ 1828 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 1829 strlen(kve->kve_path) + 1; 1830 kve->kve_structsize = roundup(kve->kve_structsize, 1831 sizeof(uint64_t)); 1832 error = SYSCTL_OUT(req, kve, kve->kve_structsize); 1833 vm_map_lock_read(map); 1834 if (error) 1835 break; 1836 if (last_timestamp != map->timestamp) { 1837 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1838 entry = tmp_entry; 1839 } 1840 } 1841 vm_map_unlock_read(map); 1842 vmspace_free(vm); 1843 PRELE(p); 1844 free(kve, M_TEMP); 1845 return (error); 1846 } 1847 1848 #if defined(STACK) || defined(DDB) 1849 static int 1850 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 1851 { 1852 struct kinfo_kstack *kkstp; 1853 int error, i, *name, numthreads; 1854 lwpid_t *lwpidarray; 1855 struct thread *td; 1856 struct stack *st; 1857 struct sbuf sb; 1858 struct proc *p; 1859 1860 name = (int *)arg1; 1861 if ((p = pfind((pid_t)name[0])) == NULL) 1862 return (ESRCH); 1863 /* XXXRW: Not clear ESRCH is the right error during proc execve(). */ 1864 if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) { 1865 PROC_UNLOCK(p); 1866 return (ESRCH); 1867 } 1868 if ((error = p_candebug(curthread, p))) { 1869 PROC_UNLOCK(p); 1870 return (error); 1871 } 1872 _PHOLD(p); 1873 PROC_UNLOCK(p); 1874 1875 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 1876 st = stack_create(); 1877 1878 lwpidarray = NULL; 1879 numthreads = 0; 1880 PROC_LOCK(p); 1881 repeat: 1882 if (numthreads < p->p_numthreads) { 1883 if (lwpidarray != NULL) { 1884 free(lwpidarray, M_TEMP); 1885 lwpidarray = NULL; 1886 } 1887 numthreads = p->p_numthreads; 1888 PROC_UNLOCK(p); 1889 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 1890 M_WAITOK | M_ZERO); 1891 PROC_LOCK(p); 1892 goto repeat; 1893 } 1894 i = 0; 1895 1896 /* 1897 * XXXRW: During the below loop, execve(2) and countless other sorts 1898 * of changes could have taken place. Should we check to see if the 1899 * vmspace has been replaced, or the like, in order to prevent 1900 * giving a snapshot that spans, say, execve(2), with some threads 1901 * before and some after? Among other things, the credentials could 1902 * have changed, in which case the right to extract debug info might 1903 * no longer be assured. 1904 */ 1905 FOREACH_THREAD_IN_PROC(p, td) { 1906 KASSERT(i < numthreads, 1907 ("sysctl_kern_proc_kstack: numthreads")); 1908 lwpidarray[i] = td->td_tid; 1909 i++; 1910 } 1911 numthreads = i; 1912 for (i = 0; i < numthreads; i++) { 1913 td = thread_find(p, lwpidarray[i]); 1914 if (td == NULL) { 1915 continue; 1916 } 1917 bzero(kkstp, sizeof(*kkstp)); 1918 (void)sbuf_new(&sb, kkstp->kkst_trace, 1919 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 1920 thread_lock(td); 1921 kkstp->kkst_tid = td->td_tid; 1922 if (TD_IS_SWAPPED(td)) 1923 kkstp->kkst_state = KKST_STATE_SWAPPED; 1924 else if (TD_IS_RUNNING(td)) 1925 kkstp->kkst_state = KKST_STATE_RUNNING; 1926 else { 1927 kkstp->kkst_state = KKST_STATE_STACKOK; 1928 stack_save_td(st, td); 1929 } 1930 thread_unlock(td); 1931 PROC_UNLOCK(p); 1932 stack_sbuf_print(&sb, st); 1933 sbuf_finish(&sb); 1934 sbuf_delete(&sb); 1935 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 1936 PROC_LOCK(p); 1937 if (error) 1938 break; 1939 } 1940 _PRELE(p); 1941 PROC_UNLOCK(p); 1942 if (lwpidarray != NULL) 1943 free(lwpidarray, M_TEMP); 1944 stack_destroy(st); 1945 free(kkstp, M_TEMP); 1946 return (error); 1947 } 1948 #endif 1949 1950 /* 1951 * This sysctl allows a process to retrieve the full list of groups from 1952 * itself or another process. 1953 */ 1954 static int 1955 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 1956 { 1957 pid_t *pidp = (pid_t *)arg1; 1958 unsigned int arglen = arg2; 1959 struct proc *p; 1960 struct ucred *cred; 1961 int error; 1962 1963 if (arglen != 1) 1964 return (EINVAL); 1965 if (*pidp == -1) { /* -1 means this process */ 1966 p = req->td->td_proc; 1967 } else { 1968 p = pfind(*pidp); 1969 if (p == NULL) 1970 return (ESRCH); 1971 if ((error = p_cansee(curthread, p)) != 0) { 1972 PROC_UNLOCK(p); 1973 return (error); 1974 } 1975 } 1976 1977 cred = crhold(p->p_ucred); 1978 if (*pidp != -1) 1979 PROC_UNLOCK(p); 1980 1981 error = SYSCTL_OUT(req, cred->cr_groups, 1982 cred->cr_ngroups * sizeof(gid_t)); 1983 crfree(cred); 1984 return (error); 1985 } 1986 1987 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1988 1989 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 1990 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 1991 "Return entire process table"); 1992 1993 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 1994 sysctl_kern_proc, "Process table"); 1995 1996 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 1997 sysctl_kern_proc, "Process table"); 1998 1999 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2000 sysctl_kern_proc, "Process table"); 2001 2002 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 2003 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2004 2005 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2006 sysctl_kern_proc, "Process table"); 2007 2008 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2009 sysctl_kern_proc, "Process table"); 2010 2011 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2012 sysctl_kern_proc, "Process table"); 2013 2014 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2015 sysctl_kern_proc, "Process table"); 2016 2017 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 2018 sysctl_kern_proc, "Return process table, no threads"); 2019 2020 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 2021 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2022 sysctl_kern_proc_args, "Process argument list"); 2023 2024 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 2025 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2026 2027 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 2028 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 2029 "Process syscall vector name (ABI type)"); 2030 2031 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 2032 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2033 2034 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 2035 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2036 2037 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 2038 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2039 2040 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 2041 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2042 2043 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 2044 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2045 2046 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 2047 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2048 2049 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 2050 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2051 2052 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 2053 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2054 2055 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 2056 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 2057 "Return process table, no threads"); 2058 2059 #ifdef COMPAT_FREEBSD7 2060 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 2061 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 2062 #endif 2063 2064 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 2065 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 2066 2067 #if defined(STACK) || defined(DDB) 2068 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 2069 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 2070 #endif 2071 2072 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2073 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 2074