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 if (p->p_state == PRS_NEW) { 295 p = NULL; 296 break; 297 } 298 PROC_LOCK(p); 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 PROC_SLOCK(p); 760 if (p->p_state != PRS_NEW && 761 p->p_state != PRS_ZOMBIE && 762 p->p_vmspace != NULL) { 763 struct vmspace *vm = p->p_vmspace; 764 765 kp->ki_size = vm->vm_map.size; 766 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 767 FOREACH_THREAD_IN_PROC(p, td0) { 768 if (!TD_IS_SWAPPED(td0)) 769 kp->ki_rssize += td0->td_kstack_pages; 770 } 771 kp->ki_swrss = vm->vm_swrss; 772 kp->ki_tsize = vm->vm_tsize; 773 kp->ki_dsize = vm->vm_dsize; 774 kp->ki_ssize = vm->vm_ssize; 775 } else if (p->p_state == PRS_ZOMBIE) 776 kp->ki_stat = SZOMB; 777 if (kp->ki_flag & P_INMEM) 778 kp->ki_sflag = PS_INMEM; 779 else 780 kp->ki_sflag = 0; 781 /* Calculate legacy swtime as seconds since 'swtick'. */ 782 kp->ki_swtime = (ticks - p->p_swtick) / hz; 783 kp->ki_pid = p->p_pid; 784 kp->ki_nice = p->p_nice; 785 rufetch(p, &kp->ki_rusage); 786 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 787 PROC_SUNLOCK(p); 788 kp->ki_start = p->p_stats->p_start; 789 timevaladd(&kp->ki_start, &boottime); 790 PROC_SLOCK(p); 791 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 792 PROC_SUNLOCK(p); 793 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 794 /* Some callers want child times in a single value. */ 795 kp->ki_childtime = kp->ki_childstime; 796 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 797 798 tp = NULL; 799 if (p->p_pgrp) { 800 kp->ki_pgid = p->p_pgrp->pg_id; 801 kp->ki_jobc = p->p_pgrp->pg_jobc; 802 sp = p->p_pgrp->pg_session; 803 804 if (sp != NULL) { 805 kp->ki_sid = sp->s_sid; 806 SESS_LOCK(sp); 807 strlcpy(kp->ki_login, sp->s_login, 808 sizeof(kp->ki_login)); 809 if (sp->s_ttyvp) 810 kp->ki_kiflag |= KI_CTTY; 811 if (SESS_LEADER(p)) 812 kp->ki_kiflag |= KI_SLEADER; 813 /* XXX proctree_lock */ 814 tp = sp->s_ttyp; 815 SESS_UNLOCK(sp); 816 } 817 } 818 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 819 kp->ki_tdev = tty_udev(tp); 820 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 821 if (tp->t_session) 822 kp->ki_tsid = tp->t_session->s_sid; 823 } else 824 kp->ki_tdev = NODEV; 825 if (p->p_comm[0] != '\0') 826 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 827 if (p->p_sysent && p->p_sysent->sv_name != NULL && 828 p->p_sysent->sv_name[0] != '\0') 829 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 830 kp->ki_siglist = p->p_siglist; 831 kp->ki_xstat = p->p_xstat; 832 kp->ki_acflag = p->p_acflag; 833 kp->ki_lock = p->p_lock; 834 if (p->p_pptr) 835 kp->ki_ppid = p->p_pptr->p_pid; 836 } 837 838 /* 839 * Fill in information that is thread specific. Must be called with 840 * target process locked. If 'preferthread' is set, overwrite certain 841 * process-related fields that are maintained for both threads and 842 * processes. 843 */ 844 static void 845 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 846 { 847 struct proc *p; 848 849 p = td->td_proc; 850 kp->ki_tdaddr = td; 851 PROC_LOCK_ASSERT(p, MA_OWNED); 852 853 thread_lock(td); 854 if (td->td_wmesg != NULL) 855 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 856 else 857 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 858 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 859 if (TD_ON_LOCK(td)) { 860 kp->ki_kiflag |= KI_LOCKBLOCK; 861 strlcpy(kp->ki_lockname, td->td_lockname, 862 sizeof(kp->ki_lockname)); 863 } else { 864 kp->ki_kiflag &= ~KI_LOCKBLOCK; 865 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 866 } 867 868 if (p->p_state == PRS_NORMAL) { /* approximate. */ 869 if (TD_ON_RUNQ(td) || 870 TD_CAN_RUN(td) || 871 TD_IS_RUNNING(td)) { 872 kp->ki_stat = SRUN; 873 } else if (P_SHOULDSTOP(p)) { 874 kp->ki_stat = SSTOP; 875 } else if (TD_IS_SLEEPING(td)) { 876 kp->ki_stat = SSLEEP; 877 } else if (TD_ON_LOCK(td)) { 878 kp->ki_stat = SLOCK; 879 } else { 880 kp->ki_stat = SWAIT; 881 } 882 } else if (p->p_state == PRS_ZOMBIE) { 883 kp->ki_stat = SZOMB; 884 } else { 885 kp->ki_stat = SIDL; 886 } 887 888 /* Things in the thread */ 889 kp->ki_wchan = td->td_wchan; 890 kp->ki_pri.pri_level = td->td_priority; 891 kp->ki_pri.pri_native = td->td_base_pri; 892 kp->ki_lastcpu = td->td_lastcpu; 893 kp->ki_oncpu = td->td_oncpu; 894 kp->ki_tdflags = td->td_flags; 895 kp->ki_tid = td->td_tid; 896 kp->ki_numthreads = p->p_numthreads; 897 kp->ki_pcb = td->td_pcb; 898 kp->ki_kstack = (void *)td->td_kstack; 899 kp->ki_slptime = (ticks - td->td_slptick) / hz; 900 kp->ki_pri.pri_class = td->td_pri_class; 901 kp->ki_pri.pri_user = td->td_user_pri; 902 903 if (preferthread) { 904 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 905 kp->ki_pctcpu = sched_pctcpu(td); 906 kp->ki_estcpu = td->td_estcpu; 907 } 908 909 /* We can't get this anymore but ps etc never used it anyway. */ 910 kp->ki_rqindex = 0; 911 912 if (preferthread) 913 kp->ki_siglist = td->td_siglist; 914 kp->ki_sigmask = td->td_sigmask; 915 thread_unlock(td); 916 } 917 918 /* 919 * Fill in a kinfo_proc structure for the specified process. 920 * Must be called with the target process locked. 921 */ 922 void 923 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 924 { 925 926 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 927 928 fill_kinfo_proc_only(p, kp); 929 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 930 fill_kinfo_aggregate(p, kp); 931 } 932 933 struct pstats * 934 pstats_alloc(void) 935 { 936 937 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 938 } 939 940 /* 941 * Copy parts of p_stats; zero the rest of p_stats (statistics). 942 */ 943 void 944 pstats_fork(struct pstats *src, struct pstats *dst) 945 { 946 947 bzero(&dst->pstat_startzero, 948 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 949 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 950 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 951 } 952 953 void 954 pstats_free(struct pstats *ps) 955 { 956 957 free(ps, M_SUBPROC); 958 } 959 960 /* 961 * Locate a zombie process by number 962 */ 963 struct proc * 964 zpfind(pid_t pid) 965 { 966 struct proc *p; 967 968 sx_slock(&allproc_lock); 969 LIST_FOREACH(p, &zombproc, p_list) 970 if (p->p_pid == pid) { 971 PROC_LOCK(p); 972 break; 973 } 974 sx_sunlock(&allproc_lock); 975 return (p); 976 } 977 978 #define KERN_PROC_ZOMBMASK 0x3 979 #define KERN_PROC_NOTHREADS 0x4 980 981 #ifdef COMPAT_FREEBSD32 982 983 /* 984 * This function is typically used to copy out the kernel address, so 985 * it can be replaced by assignment of zero. 986 */ 987 static inline uint32_t 988 ptr32_trim(void *ptr) 989 { 990 uintptr_t uptr; 991 992 uptr = (uintptr_t)ptr; 993 return ((uptr > UINT_MAX) ? 0 : uptr); 994 } 995 996 #define PTRTRIM_CP(src,dst,fld) \ 997 do { (dst).fld = ptr32_trim((src).fld); } while (0) 998 999 static void 1000 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 1001 { 1002 int i; 1003 1004 bzero(ki32, sizeof(struct kinfo_proc32)); 1005 ki32->ki_structsize = sizeof(struct kinfo_proc32); 1006 CP(*ki, *ki32, ki_layout); 1007 PTRTRIM_CP(*ki, *ki32, ki_args); 1008 PTRTRIM_CP(*ki, *ki32, ki_paddr); 1009 PTRTRIM_CP(*ki, *ki32, ki_addr); 1010 PTRTRIM_CP(*ki, *ki32, ki_tracep); 1011 PTRTRIM_CP(*ki, *ki32, ki_textvp); 1012 PTRTRIM_CP(*ki, *ki32, ki_fd); 1013 PTRTRIM_CP(*ki, *ki32, ki_vmspace); 1014 PTRTRIM_CP(*ki, *ki32, ki_wchan); 1015 CP(*ki, *ki32, ki_pid); 1016 CP(*ki, *ki32, ki_ppid); 1017 CP(*ki, *ki32, ki_pgid); 1018 CP(*ki, *ki32, ki_tpgid); 1019 CP(*ki, *ki32, ki_sid); 1020 CP(*ki, *ki32, ki_tsid); 1021 CP(*ki, *ki32, ki_jobc); 1022 CP(*ki, *ki32, ki_tdev); 1023 CP(*ki, *ki32, ki_siglist); 1024 CP(*ki, *ki32, ki_sigmask); 1025 CP(*ki, *ki32, ki_sigignore); 1026 CP(*ki, *ki32, ki_sigcatch); 1027 CP(*ki, *ki32, ki_uid); 1028 CP(*ki, *ki32, ki_ruid); 1029 CP(*ki, *ki32, ki_svuid); 1030 CP(*ki, *ki32, ki_rgid); 1031 CP(*ki, *ki32, ki_svgid); 1032 CP(*ki, *ki32, ki_ngroups); 1033 for (i = 0; i < KI_NGROUPS; i++) 1034 CP(*ki, *ki32, ki_groups[i]); 1035 CP(*ki, *ki32, ki_size); 1036 CP(*ki, *ki32, ki_rssize); 1037 CP(*ki, *ki32, ki_swrss); 1038 CP(*ki, *ki32, ki_tsize); 1039 CP(*ki, *ki32, ki_dsize); 1040 CP(*ki, *ki32, ki_ssize); 1041 CP(*ki, *ki32, ki_xstat); 1042 CP(*ki, *ki32, ki_acflag); 1043 CP(*ki, *ki32, ki_pctcpu); 1044 CP(*ki, *ki32, ki_estcpu); 1045 CP(*ki, *ki32, ki_slptime); 1046 CP(*ki, *ki32, ki_swtime); 1047 CP(*ki, *ki32, ki_runtime); 1048 TV_CP(*ki, *ki32, ki_start); 1049 TV_CP(*ki, *ki32, ki_childtime); 1050 CP(*ki, *ki32, ki_flag); 1051 CP(*ki, *ki32, ki_kiflag); 1052 CP(*ki, *ki32, ki_traceflag); 1053 CP(*ki, *ki32, ki_stat); 1054 CP(*ki, *ki32, ki_nice); 1055 CP(*ki, *ki32, ki_lock); 1056 CP(*ki, *ki32, ki_rqindex); 1057 CP(*ki, *ki32, ki_oncpu); 1058 CP(*ki, *ki32, ki_lastcpu); 1059 bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1); 1060 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 1061 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 1062 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 1063 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 1064 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 1065 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); 1066 CP(*ki, *ki32, ki_cr_flags); 1067 CP(*ki, *ki32, ki_jid); 1068 CP(*ki, *ki32, ki_numthreads); 1069 CP(*ki, *ki32, ki_tid); 1070 CP(*ki, *ki32, ki_pri); 1071 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 1072 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 1073 PTRTRIM_CP(*ki, *ki32, ki_pcb); 1074 PTRTRIM_CP(*ki, *ki32, ki_kstack); 1075 PTRTRIM_CP(*ki, *ki32, ki_udata); 1076 CP(*ki, *ki32, ki_sflag); 1077 CP(*ki, *ki32, ki_tdflags); 1078 } 1079 1080 static int 1081 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1082 { 1083 struct kinfo_proc32 ki32; 1084 int error; 1085 1086 if (req->flags & SCTL_MASK32) { 1087 freebsd32_kinfo_proc_out(ki, &ki32); 1088 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32)); 1089 } else 1090 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)); 1091 return (error); 1092 } 1093 #else 1094 static int 1095 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1096 { 1097 1098 return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc))); 1099 } 1100 #endif 1101 1102 /* 1103 * Must be called with the process locked and will return with it unlocked. 1104 */ 1105 static int 1106 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 1107 { 1108 struct thread *td; 1109 struct kinfo_proc kinfo_proc; 1110 int error = 0; 1111 struct proc *np; 1112 pid_t pid = p->p_pid; 1113 1114 PROC_LOCK_ASSERT(p, MA_OWNED); 1115 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1116 1117 fill_kinfo_proc(p, &kinfo_proc); 1118 if (flags & KERN_PROC_NOTHREADS) 1119 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1120 else { 1121 FOREACH_THREAD_IN_PROC(p, td) { 1122 fill_kinfo_thread(td, &kinfo_proc, 1); 1123 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1124 if (error) 1125 break; 1126 } 1127 } 1128 PROC_UNLOCK(p); 1129 if (error) 1130 return (error); 1131 if (flags & KERN_PROC_ZOMBMASK) 1132 np = zpfind(pid); 1133 else { 1134 if (pid == 0) 1135 return (0); 1136 np = pfind(pid); 1137 } 1138 if (np == NULL) 1139 return (ESRCH); 1140 if (np != p) { 1141 PROC_UNLOCK(np); 1142 return (ESRCH); 1143 } 1144 PROC_UNLOCK(np); 1145 return (0); 1146 } 1147 1148 static int 1149 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1150 { 1151 int *name = (int*) arg1; 1152 u_int namelen = arg2; 1153 struct proc *p; 1154 int flags, doingzomb, oid_number; 1155 int error = 0; 1156 1157 oid_number = oidp->oid_number; 1158 if (oid_number != KERN_PROC_ALL && 1159 (oid_number & KERN_PROC_INC_THREAD) == 0) 1160 flags = KERN_PROC_NOTHREADS; 1161 else { 1162 flags = 0; 1163 oid_number &= ~KERN_PROC_INC_THREAD; 1164 } 1165 if (oid_number == KERN_PROC_PID) { 1166 if (namelen != 1) 1167 return (EINVAL); 1168 error = sysctl_wire_old_buffer(req, 0); 1169 if (error) 1170 return (error); 1171 p = pfind((pid_t)name[0]); 1172 if (!p) 1173 return (ESRCH); 1174 if ((error = p_cansee(curthread, p))) { 1175 PROC_UNLOCK(p); 1176 return (error); 1177 } 1178 error = sysctl_out_proc(p, req, flags); 1179 return (error); 1180 } 1181 1182 switch (oid_number) { 1183 case KERN_PROC_ALL: 1184 if (namelen != 0) 1185 return (EINVAL); 1186 break; 1187 case KERN_PROC_PROC: 1188 if (namelen != 0 && namelen != 1) 1189 return (EINVAL); 1190 break; 1191 default: 1192 if (namelen != 1) 1193 return (EINVAL); 1194 break; 1195 } 1196 1197 if (!req->oldptr) { 1198 /* overestimate by 5 procs */ 1199 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1200 if (error) 1201 return (error); 1202 } 1203 error = sysctl_wire_old_buffer(req, 0); 1204 if (error != 0) 1205 return (error); 1206 sx_slock(&allproc_lock); 1207 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1208 if (!doingzomb) 1209 p = LIST_FIRST(&allproc); 1210 else 1211 p = LIST_FIRST(&zombproc); 1212 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1213 /* 1214 * Skip embryonic processes. 1215 */ 1216 PROC_SLOCK(p); 1217 if (p->p_state == PRS_NEW) { 1218 PROC_SUNLOCK(p); 1219 continue; 1220 } 1221 PROC_SUNLOCK(p); 1222 PROC_LOCK(p); 1223 KASSERT(p->p_ucred != NULL, 1224 ("process credential is NULL for non-NEW proc")); 1225 /* 1226 * Show a user only appropriate processes. 1227 */ 1228 if (p_cansee(curthread, p)) { 1229 PROC_UNLOCK(p); 1230 continue; 1231 } 1232 /* 1233 * TODO - make more efficient (see notes below). 1234 * do by session. 1235 */ 1236 switch (oid_number) { 1237 1238 case KERN_PROC_GID: 1239 if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1240 PROC_UNLOCK(p); 1241 continue; 1242 } 1243 break; 1244 1245 case KERN_PROC_PGRP: 1246 /* could do this by traversing pgrp */ 1247 if (p->p_pgrp == NULL || 1248 p->p_pgrp->pg_id != (pid_t)name[0]) { 1249 PROC_UNLOCK(p); 1250 continue; 1251 } 1252 break; 1253 1254 case KERN_PROC_RGID: 1255 if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1256 PROC_UNLOCK(p); 1257 continue; 1258 } 1259 break; 1260 1261 case KERN_PROC_SESSION: 1262 if (p->p_session == NULL || 1263 p->p_session->s_sid != (pid_t)name[0]) { 1264 PROC_UNLOCK(p); 1265 continue; 1266 } 1267 break; 1268 1269 case KERN_PROC_TTY: 1270 if ((p->p_flag & P_CONTROLT) == 0 || 1271 p->p_session == NULL) { 1272 PROC_UNLOCK(p); 1273 continue; 1274 } 1275 /* XXX proctree_lock */ 1276 SESS_LOCK(p->p_session); 1277 if (p->p_session->s_ttyp == NULL || 1278 tty_udev(p->p_session->s_ttyp) != 1279 (dev_t)name[0]) { 1280 SESS_UNLOCK(p->p_session); 1281 PROC_UNLOCK(p); 1282 continue; 1283 } 1284 SESS_UNLOCK(p->p_session); 1285 break; 1286 1287 case KERN_PROC_UID: 1288 if (p->p_ucred->cr_uid != (uid_t)name[0]) { 1289 PROC_UNLOCK(p); 1290 continue; 1291 } 1292 break; 1293 1294 case KERN_PROC_RUID: 1295 if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 1296 PROC_UNLOCK(p); 1297 continue; 1298 } 1299 break; 1300 1301 case KERN_PROC_PROC: 1302 break; 1303 1304 default: 1305 break; 1306 1307 } 1308 1309 error = sysctl_out_proc(p, req, flags | doingzomb); 1310 if (error) { 1311 sx_sunlock(&allproc_lock); 1312 return (error); 1313 } 1314 } 1315 } 1316 sx_sunlock(&allproc_lock); 1317 return (0); 1318 } 1319 1320 struct pargs * 1321 pargs_alloc(int len) 1322 { 1323 struct pargs *pa; 1324 1325 pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1326 M_WAITOK); 1327 refcount_init(&pa->ar_ref, 1); 1328 pa->ar_length = len; 1329 return (pa); 1330 } 1331 1332 static void 1333 pargs_free(struct pargs *pa) 1334 { 1335 1336 free(pa, M_PARGS); 1337 } 1338 1339 void 1340 pargs_hold(struct pargs *pa) 1341 { 1342 1343 if (pa == NULL) 1344 return; 1345 refcount_acquire(&pa->ar_ref); 1346 } 1347 1348 void 1349 pargs_drop(struct pargs *pa) 1350 { 1351 1352 if (pa == NULL) 1353 return; 1354 if (refcount_release(&pa->ar_ref)) 1355 pargs_free(pa); 1356 } 1357 1358 /* 1359 * This sysctl allows a process to retrieve the argument list or process 1360 * title for another process without groping around in the address space 1361 * of the other process. It also allow a process to set its own "process 1362 * title to a string of its own choice. 1363 */ 1364 static int 1365 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1366 { 1367 int *name = (int*) arg1; 1368 u_int namelen = arg2; 1369 struct pargs *newpa, *pa; 1370 struct proc *p; 1371 int error = 0; 1372 1373 if (namelen != 1) 1374 return (EINVAL); 1375 1376 p = pfind((pid_t)name[0]); 1377 if (!p) 1378 return (ESRCH); 1379 1380 if ((error = p_cansee(curthread, p)) != 0) { 1381 PROC_UNLOCK(p); 1382 return (error); 1383 } 1384 1385 if (req->newptr && curproc != p) { 1386 PROC_UNLOCK(p); 1387 return (EPERM); 1388 } 1389 1390 pa = p->p_args; 1391 pargs_hold(pa); 1392 PROC_UNLOCK(p); 1393 if (req->oldptr != NULL && pa != NULL) 1394 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1395 pargs_drop(pa); 1396 if (error != 0 || req->newptr == NULL) 1397 return (error); 1398 1399 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1400 return (ENOMEM); 1401 newpa = pargs_alloc(req->newlen); 1402 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1403 if (error != 0) { 1404 pargs_free(newpa); 1405 return (error); 1406 } 1407 PROC_LOCK(p); 1408 pa = p->p_args; 1409 p->p_args = newpa; 1410 PROC_UNLOCK(p); 1411 pargs_drop(pa); 1412 return (0); 1413 } 1414 1415 /* 1416 * This sysctl allows a process to retrieve the path of the executable for 1417 * itself or another process. 1418 */ 1419 static int 1420 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1421 { 1422 pid_t *pidp = (pid_t *)arg1; 1423 unsigned int arglen = arg2; 1424 struct proc *p; 1425 struct vnode *vp; 1426 char *retbuf, *freebuf; 1427 int error, vfslocked; 1428 1429 if (arglen != 1) 1430 return (EINVAL); 1431 if (*pidp == -1) { /* -1 means this process */ 1432 p = req->td->td_proc; 1433 } else { 1434 p = pfind(*pidp); 1435 if (p == NULL) 1436 return (ESRCH); 1437 if ((error = p_cansee(curthread, p)) != 0) { 1438 PROC_UNLOCK(p); 1439 return (error); 1440 } 1441 } 1442 1443 vp = p->p_textvp; 1444 if (vp == NULL) { 1445 if (*pidp != -1) 1446 PROC_UNLOCK(p); 1447 return (0); 1448 } 1449 vref(vp); 1450 if (*pidp != -1) 1451 PROC_UNLOCK(p); 1452 error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1453 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1454 vrele(vp); 1455 VFS_UNLOCK_GIANT(vfslocked); 1456 if (error) 1457 return (error); 1458 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1459 free(freebuf, M_TEMP); 1460 return (error); 1461 } 1462 1463 static int 1464 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1465 { 1466 struct proc *p; 1467 char *sv_name; 1468 int *name; 1469 int namelen; 1470 int error; 1471 1472 namelen = arg2; 1473 if (namelen != 1) 1474 return (EINVAL); 1475 1476 name = (int *)arg1; 1477 if ((p = pfind((pid_t)name[0])) == NULL) 1478 return (ESRCH); 1479 if ((error = p_cansee(curthread, p))) { 1480 PROC_UNLOCK(p); 1481 return (error); 1482 } 1483 sv_name = p->p_sysent->sv_name; 1484 PROC_UNLOCK(p); 1485 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1486 } 1487 1488 #ifdef KINFO_OVMENTRY_SIZE 1489 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 1490 #endif 1491 1492 #ifdef COMPAT_FREEBSD7 1493 static int 1494 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 1495 { 1496 vm_map_entry_t entry, tmp_entry; 1497 unsigned int last_timestamp; 1498 char *fullpath, *freepath; 1499 struct kinfo_ovmentry *kve; 1500 struct vattr va; 1501 struct ucred *cred; 1502 int error, *name; 1503 struct vnode *vp; 1504 struct proc *p; 1505 vm_map_t map; 1506 struct vmspace *vm; 1507 1508 name = (int *)arg1; 1509 if ((p = pfind((pid_t)name[0])) == NULL) 1510 return (ESRCH); 1511 if (p->p_flag & P_WEXIT) { 1512 PROC_UNLOCK(p); 1513 return (ESRCH); 1514 } 1515 if ((error = p_candebug(curthread, p))) { 1516 PROC_UNLOCK(p); 1517 return (error); 1518 } 1519 _PHOLD(p); 1520 PROC_UNLOCK(p); 1521 vm = vmspace_acquire_ref(p); 1522 if (vm == NULL) { 1523 PRELE(p); 1524 return (ESRCH); 1525 } 1526 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1527 1528 map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */ 1529 vm_map_lock_read(map); 1530 for (entry = map->header.next; entry != &map->header; 1531 entry = entry->next) { 1532 vm_object_t obj, tobj, lobj; 1533 vm_offset_t addr; 1534 int vfslocked; 1535 1536 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1537 continue; 1538 1539 bzero(kve, sizeof(*kve)); 1540 kve->kve_structsize = sizeof(*kve); 1541 1542 kve->kve_private_resident = 0; 1543 obj = entry->object.vm_object; 1544 if (obj != NULL) { 1545 VM_OBJECT_LOCK(obj); 1546 if (obj->shadow_count == 1) 1547 kve->kve_private_resident = 1548 obj->resident_page_count; 1549 } 1550 kve->kve_resident = 0; 1551 addr = entry->start; 1552 while (addr < entry->end) { 1553 if (pmap_extract(map->pmap, addr)) 1554 kve->kve_resident++; 1555 addr += PAGE_SIZE; 1556 } 1557 1558 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1559 if (tobj != obj) 1560 VM_OBJECT_LOCK(tobj); 1561 if (lobj != obj) 1562 VM_OBJECT_UNLOCK(lobj); 1563 lobj = tobj; 1564 } 1565 1566 kve->kve_start = (void*)entry->start; 1567 kve->kve_end = (void*)entry->end; 1568 kve->kve_offset = (off_t)entry->offset; 1569 1570 if (entry->protection & VM_PROT_READ) 1571 kve->kve_protection |= KVME_PROT_READ; 1572 if (entry->protection & VM_PROT_WRITE) 1573 kve->kve_protection |= KVME_PROT_WRITE; 1574 if (entry->protection & VM_PROT_EXECUTE) 1575 kve->kve_protection |= KVME_PROT_EXEC; 1576 1577 if (entry->eflags & MAP_ENTRY_COW) 1578 kve->kve_flags |= KVME_FLAG_COW; 1579 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1580 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1581 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 1582 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 1583 1584 last_timestamp = map->timestamp; 1585 vm_map_unlock_read(map); 1586 1587 kve->kve_fileid = 0; 1588 kve->kve_fsid = 0; 1589 freepath = NULL; 1590 fullpath = ""; 1591 if (lobj) { 1592 vp = NULL; 1593 switch (lobj->type) { 1594 case OBJT_DEFAULT: 1595 kve->kve_type = KVME_TYPE_DEFAULT; 1596 break; 1597 case OBJT_VNODE: 1598 kve->kve_type = KVME_TYPE_VNODE; 1599 vp = lobj->handle; 1600 vref(vp); 1601 break; 1602 case OBJT_SWAP: 1603 kve->kve_type = KVME_TYPE_SWAP; 1604 break; 1605 case OBJT_DEVICE: 1606 kve->kve_type = KVME_TYPE_DEVICE; 1607 break; 1608 case OBJT_PHYS: 1609 kve->kve_type = KVME_TYPE_PHYS; 1610 break; 1611 case OBJT_DEAD: 1612 kve->kve_type = KVME_TYPE_DEAD; 1613 break; 1614 case OBJT_SG: 1615 kve->kve_type = KVME_TYPE_SG; 1616 break; 1617 default: 1618 kve->kve_type = KVME_TYPE_UNKNOWN; 1619 break; 1620 } 1621 if (lobj != obj) 1622 VM_OBJECT_UNLOCK(lobj); 1623 1624 kve->kve_ref_count = obj->ref_count; 1625 kve->kve_shadow_count = obj->shadow_count; 1626 VM_OBJECT_UNLOCK(obj); 1627 if (vp != NULL) { 1628 vn_fullpath(curthread, vp, &fullpath, 1629 &freepath); 1630 cred = curthread->td_ucred; 1631 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1632 vn_lock(vp, LK_SHARED | LK_RETRY); 1633 if (VOP_GETATTR(vp, &va, cred) == 0) { 1634 kve->kve_fileid = va.va_fileid; 1635 kve->kve_fsid = va.va_fsid; 1636 } 1637 vput(vp); 1638 VFS_UNLOCK_GIANT(vfslocked); 1639 } 1640 } else { 1641 kve->kve_type = KVME_TYPE_NONE; 1642 kve->kve_ref_count = 0; 1643 kve->kve_shadow_count = 0; 1644 } 1645 1646 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1647 if (freepath != NULL) 1648 free(freepath, M_TEMP); 1649 1650 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 1651 vm_map_lock_read(map); 1652 if (error) 1653 break; 1654 if (last_timestamp != map->timestamp) { 1655 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1656 entry = tmp_entry; 1657 } 1658 } 1659 vm_map_unlock_read(map); 1660 vmspace_free(vm); 1661 PRELE(p); 1662 free(kve, M_TEMP); 1663 return (error); 1664 } 1665 #endif /* COMPAT_FREEBSD7 */ 1666 1667 #ifdef KINFO_VMENTRY_SIZE 1668 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 1669 #endif 1670 1671 static int 1672 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 1673 { 1674 vm_map_entry_t entry, tmp_entry; 1675 unsigned int last_timestamp; 1676 char *fullpath, *freepath; 1677 struct kinfo_vmentry *kve; 1678 struct vattr va; 1679 struct ucred *cred; 1680 int error, *name; 1681 struct vnode *vp; 1682 struct proc *p; 1683 struct vmspace *vm; 1684 vm_map_t map; 1685 1686 name = (int *)arg1; 1687 if ((p = pfind((pid_t)name[0])) == NULL) 1688 return (ESRCH); 1689 if (p->p_flag & P_WEXIT) { 1690 PROC_UNLOCK(p); 1691 return (ESRCH); 1692 } 1693 if ((error = p_candebug(curthread, p))) { 1694 PROC_UNLOCK(p); 1695 return (error); 1696 } 1697 _PHOLD(p); 1698 PROC_UNLOCK(p); 1699 vm = vmspace_acquire_ref(p); 1700 if (vm == NULL) { 1701 PRELE(p); 1702 return (ESRCH); 1703 } 1704 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1705 1706 map = &vm->vm_map; /* XXXRW: More locking required? */ 1707 vm_map_lock_read(map); 1708 for (entry = map->header.next; entry != &map->header; 1709 entry = entry->next) { 1710 vm_object_t obj, tobj, lobj; 1711 vm_offset_t addr; 1712 int vfslocked; 1713 1714 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1715 continue; 1716 1717 bzero(kve, sizeof(*kve)); 1718 1719 kve->kve_private_resident = 0; 1720 obj = entry->object.vm_object; 1721 if (obj != NULL) { 1722 VM_OBJECT_LOCK(obj); 1723 if (obj->shadow_count == 1) 1724 kve->kve_private_resident = 1725 obj->resident_page_count; 1726 } 1727 kve->kve_resident = 0; 1728 addr = entry->start; 1729 while (addr < entry->end) { 1730 if (pmap_extract(map->pmap, addr)) 1731 kve->kve_resident++; 1732 addr += PAGE_SIZE; 1733 } 1734 1735 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1736 if (tobj != obj) 1737 VM_OBJECT_LOCK(tobj); 1738 if (lobj != obj) 1739 VM_OBJECT_UNLOCK(lobj); 1740 lobj = tobj; 1741 } 1742 1743 kve->kve_start = entry->start; 1744 kve->kve_end = entry->end; 1745 kve->kve_offset = entry->offset; 1746 1747 if (entry->protection & VM_PROT_READ) 1748 kve->kve_protection |= KVME_PROT_READ; 1749 if (entry->protection & VM_PROT_WRITE) 1750 kve->kve_protection |= KVME_PROT_WRITE; 1751 if (entry->protection & VM_PROT_EXECUTE) 1752 kve->kve_protection |= KVME_PROT_EXEC; 1753 1754 if (entry->eflags & MAP_ENTRY_COW) 1755 kve->kve_flags |= KVME_FLAG_COW; 1756 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1757 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1758 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 1759 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 1760 1761 last_timestamp = map->timestamp; 1762 vm_map_unlock_read(map); 1763 1764 kve->kve_fileid = 0; 1765 kve->kve_fsid = 0; 1766 freepath = NULL; 1767 fullpath = ""; 1768 if (lobj) { 1769 vp = NULL; 1770 switch (lobj->type) { 1771 case OBJT_DEFAULT: 1772 kve->kve_type = KVME_TYPE_DEFAULT; 1773 break; 1774 case OBJT_VNODE: 1775 kve->kve_type = KVME_TYPE_VNODE; 1776 vp = lobj->handle; 1777 vref(vp); 1778 break; 1779 case OBJT_SWAP: 1780 kve->kve_type = KVME_TYPE_SWAP; 1781 break; 1782 case OBJT_DEVICE: 1783 kve->kve_type = KVME_TYPE_DEVICE; 1784 break; 1785 case OBJT_PHYS: 1786 kve->kve_type = KVME_TYPE_PHYS; 1787 break; 1788 case OBJT_DEAD: 1789 kve->kve_type = KVME_TYPE_DEAD; 1790 break; 1791 case OBJT_SG: 1792 kve->kve_type = KVME_TYPE_SG; 1793 break; 1794 default: 1795 kve->kve_type = KVME_TYPE_UNKNOWN; 1796 break; 1797 } 1798 if (lobj != obj) 1799 VM_OBJECT_UNLOCK(lobj); 1800 1801 kve->kve_ref_count = obj->ref_count; 1802 kve->kve_shadow_count = obj->shadow_count; 1803 VM_OBJECT_UNLOCK(obj); 1804 if (vp != NULL) { 1805 vn_fullpath(curthread, vp, &fullpath, 1806 &freepath); 1807 cred = curthread->td_ucred; 1808 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1809 vn_lock(vp, LK_SHARED | LK_RETRY); 1810 if (VOP_GETATTR(vp, &va, cred) == 0) { 1811 kve->kve_fileid = va.va_fileid; 1812 kve->kve_fsid = va.va_fsid; 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