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/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/refcount.h> 52 #include <sys/sbuf.h> 53 #include <sys/sysent.h> 54 #include <sys/sched.h> 55 #include <sys/smp.h> 56 #include <sys/stack.h> 57 #include <sys/sysctl.h> 58 #include <sys/filedesc.h> 59 #include <sys/tty.h> 60 #include <sys/signalvar.h> 61 #include <sys/sdt.h> 62 #include <sys/sx.h> 63 #include <sys/user.h> 64 #include <sys/jail.h> 65 #include <sys/vnode.h> 66 #include <sys/eventhandler.h> 67 68 #ifdef DDB 69 #include <ddb/ddb.h> 70 #endif 71 72 #include <vm/vm.h> 73 #include <vm/vm_extern.h> 74 #include <vm/pmap.h> 75 #include <vm/vm_map.h> 76 #include <vm/vm_object.h> 77 #include <vm/uma.h> 78 79 #ifdef COMPAT_FREEBSD32 80 #include <compat/freebsd32/freebsd32.h> 81 #include <compat/freebsd32/freebsd32_util.h> 82 #endif 83 84 SDT_PROVIDER_DEFINE(proc); 85 SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry); 86 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *"); 87 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int"); 88 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *"); 89 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int"); 90 SDT_PROBE_DEFINE(proc, kernel, ctor, return, return); 91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *"); 92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int"); 93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *"); 94 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int"); 95 SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry); 96 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *"); 97 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int"); 98 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *"); 99 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *"); 100 SDT_PROBE_DEFINE(proc, kernel, dtor, return, return); 101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *"); 102 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int"); 103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *"); 104 SDT_PROBE_DEFINE(proc, kernel, init, entry, entry); 105 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *"); 106 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int"); 107 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int"); 108 SDT_PROBE_DEFINE(proc, kernel, init, return, return); 109 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *"); 110 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int"); 111 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int"); 112 113 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 114 MALLOC_DEFINE(M_SESSION, "session", "session header"); 115 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 116 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 117 118 static void doenterpgrp(struct proc *, struct pgrp *); 119 static void orphanpg(struct pgrp *pg); 120 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); 121 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 122 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 123 int preferthread); 124 static void pgadjustjobc(struct pgrp *pgrp, int entering); 125 static void pgdelete(struct pgrp *); 126 static int proc_ctor(void *mem, int size, void *arg, int flags); 127 static void proc_dtor(void *mem, int size, void *arg); 128 static int proc_init(void *mem, int size, int flags); 129 static void proc_fini(void *mem, int size); 130 static void pargs_free(struct pargs *pa); 131 132 /* 133 * Other process lists 134 */ 135 struct pidhashhead *pidhashtbl; 136 u_long pidhash; 137 struct pgrphashhead *pgrphashtbl; 138 u_long pgrphash; 139 struct proclist allproc; 140 struct proclist zombproc; 141 struct sx allproc_lock; 142 struct sx proctree_lock; 143 struct mtx ppeers_lock; 144 uma_zone_t proc_zone; 145 146 int kstack_pages = KSTACK_PAGES; 147 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, 148 "Kernel stack size in pages"); 149 150 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 151 #ifdef COMPAT_FREEBSD32 152 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); 153 #endif 154 155 /* 156 * Initialize global process hashing structures. 157 */ 158 void 159 procinit() 160 { 161 162 sx_init(&allproc_lock, "allproc"); 163 sx_init(&proctree_lock, "proctree"); 164 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 165 LIST_INIT(&allproc); 166 LIST_INIT(&zombproc); 167 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 168 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 169 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 170 proc_ctor, proc_dtor, proc_init, proc_fini, 171 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 172 uihashinit(); 173 } 174 175 /* 176 * Prepare a proc for use. 177 */ 178 static int 179 proc_ctor(void *mem, int size, void *arg, int flags) 180 { 181 struct proc *p; 182 183 p = (struct proc *)mem; 184 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0); 185 EVENTHANDLER_INVOKE(process_ctor, p); 186 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0); 187 return (0); 188 } 189 190 /* 191 * Reclaim a proc after use. 192 */ 193 static void 194 proc_dtor(void *mem, int size, void *arg) 195 { 196 struct proc *p; 197 struct thread *td; 198 199 /* INVARIANTS checks go here */ 200 p = (struct proc *)mem; 201 td = FIRST_THREAD_IN_PROC(p); 202 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0); 203 if (td != NULL) { 204 #ifdef INVARIANTS 205 KASSERT((p->p_numthreads == 1), 206 ("bad number of threads in exiting process")); 207 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 208 #endif 209 /* Free all OSD associated to this thread. */ 210 osd_thread_exit(td); 211 } 212 EVENTHANDLER_INVOKE(process_dtor, p); 213 if (p->p_ksi != NULL) 214 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 215 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0); 216 } 217 218 /* 219 * Initialize type-stable parts of a proc (when newly created). 220 */ 221 static int 222 proc_init(void *mem, int size, int flags) 223 { 224 struct proc *p; 225 226 p = (struct proc *)mem; 227 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0); 228 p->p_sched = (struct p_sched *)&p[1]; 229 bzero(&p->p_mtx, sizeof(struct mtx)); 230 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 231 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 232 cv_init(&p->p_pwait, "ppwait"); 233 cv_init(&p->p_dbgwait, "dbgwait"); 234 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 235 EVENTHANDLER_INVOKE(process_init, p); 236 p->p_stats = pstats_alloc(); 237 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0); 238 return (0); 239 } 240 241 /* 242 * UMA should ensure that this function is never called. 243 * Freeing a proc structure would violate type stability. 244 */ 245 static void 246 proc_fini(void *mem, int size) 247 { 248 #ifdef notnow 249 struct proc *p; 250 251 p = (struct proc *)mem; 252 EVENTHANDLER_INVOKE(process_fini, p); 253 pstats_free(p->p_stats); 254 thread_free(FIRST_THREAD_IN_PROC(p)); 255 mtx_destroy(&p->p_mtx); 256 if (p->p_ksi != NULL) 257 ksiginfo_free(p->p_ksi); 258 #else 259 panic("proc reclaimed"); 260 #endif 261 } 262 263 /* 264 * Is p an inferior of the current process? 265 */ 266 int 267 inferior(p) 268 register struct proc *p; 269 { 270 271 sx_assert(&proctree_lock, SX_LOCKED); 272 for (; p != curproc; p = p->p_pptr) 273 if (p->p_pid == 0) 274 return (0); 275 return (1); 276 } 277 278 /* 279 * Locate a process by number; return only "live" processes -- i.e., neither 280 * zombies nor newly born but incompletely initialized processes. By not 281 * returning processes in the PRS_NEW state, we allow callers to avoid 282 * testing for that condition to avoid dereferencing p_ucred, et al. 283 */ 284 struct proc * 285 pfind(pid) 286 register pid_t pid; 287 { 288 register struct proc *p; 289 290 sx_slock(&allproc_lock); 291 LIST_FOREACH(p, PIDHASH(pid), p_hash) 292 if (p->p_pid == pid) { 293 if (p->p_state == PRS_NEW) { 294 p = NULL; 295 break; 296 } 297 PROC_LOCK(p); 298 break; 299 } 300 sx_sunlock(&allproc_lock); 301 return (p); 302 } 303 304 /* 305 * Locate a process group by number. 306 * The caller must hold proctree_lock. 307 */ 308 struct pgrp * 309 pgfind(pgid) 310 register pid_t pgid; 311 { 312 register struct pgrp *pgrp; 313 314 sx_assert(&proctree_lock, SX_LOCKED); 315 316 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 317 if (pgrp->pg_id == pgid) { 318 PGRP_LOCK(pgrp); 319 return (pgrp); 320 } 321 } 322 return (NULL); 323 } 324 325 /* 326 * Create a new process group. 327 * pgid must be equal to the pid of p. 328 * Begin a new session if required. 329 */ 330 int 331 enterpgrp(p, pgid, pgrp, sess) 332 register struct proc *p; 333 pid_t pgid; 334 struct pgrp *pgrp; 335 struct session *sess; 336 { 337 struct pgrp *pgrp2; 338 339 sx_assert(&proctree_lock, SX_XLOCKED); 340 341 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 342 KASSERT(p->p_pid == pgid, 343 ("enterpgrp: new pgrp and pid != pgid")); 344 345 pgrp2 = pgfind(pgid); 346 347 KASSERT(pgrp2 == NULL, 348 ("enterpgrp: pgrp with pgid exists")); 349 KASSERT(!SESS_LEADER(p), 350 ("enterpgrp: session leader attempted setpgrp")); 351 352 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 353 354 if (sess != NULL) { 355 /* 356 * new session 357 */ 358 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 359 PROC_LOCK(p); 360 p->p_flag &= ~P_CONTROLT; 361 PROC_UNLOCK(p); 362 PGRP_LOCK(pgrp); 363 sess->s_leader = p; 364 sess->s_sid = p->p_pid; 365 refcount_init(&sess->s_count, 1); 366 sess->s_ttyvp = NULL; 367 sess->s_ttydp = NULL; 368 sess->s_ttyp = NULL; 369 bcopy(p->p_session->s_login, sess->s_login, 370 sizeof(sess->s_login)); 371 pgrp->pg_session = sess; 372 KASSERT(p == curproc, 373 ("enterpgrp: mksession and p != curproc")); 374 } else { 375 pgrp->pg_session = p->p_session; 376 sess_hold(pgrp->pg_session); 377 PGRP_LOCK(pgrp); 378 } 379 pgrp->pg_id = pgid; 380 LIST_INIT(&pgrp->pg_members); 381 382 /* 383 * As we have an exclusive lock of proctree_lock, 384 * this should not deadlock. 385 */ 386 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 387 pgrp->pg_jobc = 0; 388 SLIST_INIT(&pgrp->pg_sigiolst); 389 PGRP_UNLOCK(pgrp); 390 391 doenterpgrp(p, pgrp); 392 393 return (0); 394 } 395 396 /* 397 * Move p to an existing process group 398 */ 399 int 400 enterthispgrp(p, pgrp) 401 register struct proc *p; 402 struct pgrp *pgrp; 403 { 404 405 sx_assert(&proctree_lock, SX_XLOCKED); 406 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 407 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 408 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 409 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 410 KASSERT(pgrp->pg_session == p->p_session, 411 ("%s: pgrp's session %p, p->p_session %p.\n", 412 __func__, 413 pgrp->pg_session, 414 p->p_session)); 415 KASSERT(pgrp != p->p_pgrp, 416 ("%s: p belongs to pgrp.", __func__)); 417 418 doenterpgrp(p, pgrp); 419 420 return (0); 421 } 422 423 /* 424 * Move p to a process group 425 */ 426 static void 427 doenterpgrp(p, pgrp) 428 struct proc *p; 429 struct pgrp *pgrp; 430 { 431 struct pgrp *savepgrp; 432 433 sx_assert(&proctree_lock, SX_XLOCKED); 434 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 435 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 436 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 437 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 438 439 savepgrp = p->p_pgrp; 440 441 /* 442 * Adjust eligibility of affected pgrps to participate in job control. 443 * Increment eligibility counts before decrementing, otherwise we 444 * could reach 0 spuriously during the first call. 445 */ 446 fixjobc(p, pgrp, 1); 447 fixjobc(p, p->p_pgrp, 0); 448 449 PGRP_LOCK(pgrp); 450 PGRP_LOCK(savepgrp); 451 PROC_LOCK(p); 452 LIST_REMOVE(p, p_pglist); 453 p->p_pgrp = pgrp; 454 PROC_UNLOCK(p); 455 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 456 PGRP_UNLOCK(savepgrp); 457 PGRP_UNLOCK(pgrp); 458 if (LIST_EMPTY(&savepgrp->pg_members)) 459 pgdelete(savepgrp); 460 } 461 462 /* 463 * remove process from process group 464 */ 465 int 466 leavepgrp(p) 467 register struct proc *p; 468 { 469 struct pgrp *savepgrp; 470 471 sx_assert(&proctree_lock, SX_XLOCKED); 472 savepgrp = p->p_pgrp; 473 PGRP_LOCK(savepgrp); 474 PROC_LOCK(p); 475 LIST_REMOVE(p, p_pglist); 476 p->p_pgrp = NULL; 477 PROC_UNLOCK(p); 478 PGRP_UNLOCK(savepgrp); 479 if (LIST_EMPTY(&savepgrp->pg_members)) 480 pgdelete(savepgrp); 481 return (0); 482 } 483 484 /* 485 * delete a process group 486 */ 487 static void 488 pgdelete(pgrp) 489 register struct pgrp *pgrp; 490 { 491 struct session *savesess; 492 struct tty *tp; 493 494 sx_assert(&proctree_lock, SX_XLOCKED); 495 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 496 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 497 498 /* 499 * Reset any sigio structures pointing to us as a result of 500 * F_SETOWN with our pgid. 501 */ 502 funsetownlst(&pgrp->pg_sigiolst); 503 504 PGRP_LOCK(pgrp); 505 tp = pgrp->pg_session->s_ttyp; 506 LIST_REMOVE(pgrp, pg_hash); 507 savesess = pgrp->pg_session; 508 PGRP_UNLOCK(pgrp); 509 510 /* Remove the reference to the pgrp before deallocating it. */ 511 if (tp != NULL) { 512 tty_lock(tp); 513 tty_rel_pgrp(tp, pgrp); 514 } 515 516 mtx_destroy(&pgrp->pg_mtx); 517 free(pgrp, M_PGRP); 518 sess_release(savesess); 519 } 520 521 static void 522 pgadjustjobc(pgrp, entering) 523 struct pgrp *pgrp; 524 int entering; 525 { 526 527 PGRP_LOCK(pgrp); 528 if (entering) 529 pgrp->pg_jobc++; 530 else { 531 --pgrp->pg_jobc; 532 if (pgrp->pg_jobc == 0) 533 orphanpg(pgrp); 534 } 535 PGRP_UNLOCK(pgrp); 536 } 537 538 /* 539 * Adjust pgrp jobc counters when specified process changes process group. 540 * We count the number of processes in each process group that "qualify" 541 * the group for terminal job control (those with a parent in a different 542 * process group of the same session). If that count reaches zero, the 543 * process group becomes orphaned. Check both the specified process' 544 * process group and that of its children. 545 * entering == 0 => p is leaving specified group. 546 * entering == 1 => p is entering specified group. 547 */ 548 void 549 fixjobc(p, pgrp, entering) 550 register struct proc *p; 551 register struct pgrp *pgrp; 552 int entering; 553 { 554 register struct pgrp *hispgrp; 555 register struct session *mysession; 556 557 sx_assert(&proctree_lock, SX_LOCKED); 558 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 559 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 560 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 561 562 /* 563 * Check p's parent to see whether p qualifies its own process 564 * group; if so, adjust count for p's process group. 565 */ 566 mysession = pgrp->pg_session; 567 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 568 hispgrp->pg_session == mysession) 569 pgadjustjobc(pgrp, entering); 570 571 /* 572 * Check this process' children to see whether they qualify 573 * their process groups; if so, adjust counts for children's 574 * process groups. 575 */ 576 LIST_FOREACH(p, &p->p_children, p_sibling) { 577 hispgrp = p->p_pgrp; 578 if (hispgrp == pgrp || 579 hispgrp->pg_session != mysession) 580 continue; 581 PROC_LOCK(p); 582 if (p->p_state == PRS_ZOMBIE) { 583 PROC_UNLOCK(p); 584 continue; 585 } 586 PROC_UNLOCK(p); 587 pgadjustjobc(hispgrp, entering); 588 } 589 } 590 591 /* 592 * A process group has become orphaned; 593 * if there are any stopped processes in the group, 594 * hang-up all process in that group. 595 */ 596 static void 597 orphanpg(pg) 598 struct pgrp *pg; 599 { 600 register struct proc *p; 601 602 PGRP_LOCK_ASSERT(pg, MA_OWNED); 603 604 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 605 PROC_LOCK(p); 606 if (P_SHOULDSTOP(p)) { 607 PROC_UNLOCK(p); 608 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 609 PROC_LOCK(p); 610 psignal(p, SIGHUP); 611 psignal(p, SIGCONT); 612 PROC_UNLOCK(p); 613 } 614 return; 615 } 616 PROC_UNLOCK(p); 617 } 618 } 619 620 void 621 sess_hold(struct session *s) 622 { 623 624 refcount_acquire(&s->s_count); 625 } 626 627 void 628 sess_release(struct session *s) 629 { 630 631 if (refcount_release(&s->s_count)) { 632 if (s->s_ttyp != NULL) { 633 tty_lock(s->s_ttyp); 634 tty_rel_sess(s->s_ttyp, s); 635 } 636 mtx_destroy(&s->s_mtx); 637 free(s, M_SESSION); 638 } 639 } 640 641 #include "opt_ddb.h" 642 #ifdef DDB 643 #include <ddb/ddb.h> 644 645 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 646 { 647 register struct pgrp *pgrp; 648 register struct proc *p; 649 register int i; 650 651 for (i = 0; i <= pgrphash; i++) { 652 if (!LIST_EMPTY(&pgrphashtbl[i])) { 653 printf("\tindx %d\n", i); 654 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 655 printf( 656 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 657 (void *)pgrp, (long)pgrp->pg_id, 658 (void *)pgrp->pg_session, 659 pgrp->pg_session->s_count, 660 (void *)LIST_FIRST(&pgrp->pg_members)); 661 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 662 printf("\t\tpid %ld addr %p pgrp %p\n", 663 (long)p->p_pid, (void *)p, 664 (void *)p->p_pgrp); 665 } 666 } 667 } 668 } 669 } 670 #endif /* DDB */ 671 672 /* 673 * Calculate the kinfo_proc members which contain process-wide 674 * informations. 675 * Must be called with the target process locked. 676 */ 677 static void 678 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) 679 { 680 struct thread *td; 681 682 PROC_LOCK_ASSERT(p, MA_OWNED); 683 684 kp->ki_estcpu = 0; 685 kp->ki_pctcpu = 0; 686 FOREACH_THREAD_IN_PROC(p, td) { 687 thread_lock(td); 688 kp->ki_pctcpu += sched_pctcpu(td); 689 kp->ki_estcpu += td->td_estcpu; 690 thread_unlock(td); 691 } 692 } 693 694 /* 695 * Clear kinfo_proc and fill in any information that is common 696 * to all threads in the process. 697 * Must be called with the target process locked. 698 */ 699 static void 700 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 701 { 702 struct thread *td0; 703 struct tty *tp; 704 struct session *sp; 705 struct ucred *cred; 706 struct sigacts *ps; 707 708 PROC_LOCK_ASSERT(p, MA_OWNED); 709 bzero(kp, sizeof(*kp)); 710 711 kp->ki_structsize = sizeof(*kp); 712 kp->ki_paddr = p; 713 kp->ki_addr =/* p->p_addr; */0; /* XXX */ 714 kp->ki_args = p->p_args; 715 kp->ki_textvp = p->p_textvp; 716 #ifdef KTRACE 717 kp->ki_tracep = p->p_tracevp; 718 kp->ki_traceflag = p->p_traceflag; 719 #endif 720 kp->ki_fd = p->p_fd; 721 kp->ki_vmspace = p->p_vmspace; 722 kp->ki_flag = p->p_flag; 723 cred = p->p_ucred; 724 if (cred) { 725 kp->ki_uid = cred->cr_uid; 726 kp->ki_ruid = cred->cr_ruid; 727 kp->ki_svuid = cred->cr_svuid; 728 kp->ki_cr_flags = 0; 729 if (cred->cr_flags & CRED_FLAG_CAPMODE) 730 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE; 731 /* XXX bde doesn't like KI_NGROUPS */ 732 if (cred->cr_ngroups > KI_NGROUPS) { 733 kp->ki_ngroups = KI_NGROUPS; 734 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 735 } else 736 kp->ki_ngroups = cred->cr_ngroups; 737 bcopy(cred->cr_groups, kp->ki_groups, 738 kp->ki_ngroups * sizeof(gid_t)); 739 kp->ki_rgid = cred->cr_rgid; 740 kp->ki_svgid = cred->cr_svgid; 741 /* If jailed(cred), emulate the old P_JAILED flag. */ 742 if (jailed(cred)) { 743 kp->ki_flag |= P_JAILED; 744 /* If inside the jail, use 0 as a jail ID. */ 745 if (cred->cr_prison != curthread->td_ucred->cr_prison) 746 kp->ki_jid = cred->cr_prison->pr_id; 747 } 748 } 749 ps = p->p_sigacts; 750 if (ps) { 751 mtx_lock(&ps->ps_mtx); 752 kp->ki_sigignore = ps->ps_sigignore; 753 kp->ki_sigcatch = ps->ps_sigcatch; 754 mtx_unlock(&ps->ps_mtx); 755 } 756 PROC_SLOCK(p); 757 if (p->p_state != PRS_NEW && 758 p->p_state != PRS_ZOMBIE && 759 p->p_vmspace != NULL) { 760 struct vmspace *vm = p->p_vmspace; 761 762 kp->ki_size = vm->vm_map.size; 763 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 764 FOREACH_THREAD_IN_PROC(p, td0) { 765 if (!TD_IS_SWAPPED(td0)) 766 kp->ki_rssize += td0->td_kstack_pages; 767 } 768 kp->ki_swrss = vm->vm_swrss; 769 kp->ki_tsize = vm->vm_tsize; 770 kp->ki_dsize = vm->vm_dsize; 771 kp->ki_ssize = vm->vm_ssize; 772 } else if (p->p_state == PRS_ZOMBIE) 773 kp->ki_stat = SZOMB; 774 if (kp->ki_flag & P_INMEM) 775 kp->ki_sflag = PS_INMEM; 776 else 777 kp->ki_sflag = 0; 778 /* Calculate legacy swtime as seconds since 'swtick'. */ 779 kp->ki_swtime = (ticks - p->p_swtick) / hz; 780 kp->ki_pid = p->p_pid; 781 kp->ki_nice = p->p_nice; 782 rufetch(p, &kp->ki_rusage); 783 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 784 PROC_SUNLOCK(p); 785 kp->ki_start = p->p_stats->p_start; 786 timevaladd(&kp->ki_start, &boottime); 787 PROC_SLOCK(p); 788 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 789 PROC_SUNLOCK(p); 790 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 791 /* Some callers want child times in a single value. */ 792 kp->ki_childtime = kp->ki_childstime; 793 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 794 795 tp = NULL; 796 if (p->p_pgrp) { 797 kp->ki_pgid = p->p_pgrp->pg_id; 798 kp->ki_jobc = p->p_pgrp->pg_jobc; 799 sp = p->p_pgrp->pg_session; 800 801 if (sp != NULL) { 802 kp->ki_sid = sp->s_sid; 803 SESS_LOCK(sp); 804 strlcpy(kp->ki_login, sp->s_login, 805 sizeof(kp->ki_login)); 806 if (sp->s_ttyvp) 807 kp->ki_kiflag |= KI_CTTY; 808 if (SESS_LEADER(p)) 809 kp->ki_kiflag |= KI_SLEADER; 810 /* XXX proctree_lock */ 811 tp = sp->s_ttyp; 812 SESS_UNLOCK(sp); 813 } 814 } 815 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 816 kp->ki_tdev = tty_udev(tp); 817 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 818 if (tp->t_session) 819 kp->ki_tsid = tp->t_session->s_sid; 820 } else 821 kp->ki_tdev = NODEV; 822 if (p->p_comm[0] != '\0') 823 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 824 if (p->p_sysent && p->p_sysent->sv_name != NULL && 825 p->p_sysent->sv_name[0] != '\0') 826 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 827 kp->ki_siglist = p->p_siglist; 828 kp->ki_xstat = p->p_xstat; 829 kp->ki_acflag = p->p_acflag; 830 kp->ki_lock = p->p_lock; 831 if (p->p_pptr) 832 kp->ki_ppid = p->p_pptr->p_pid; 833 } 834 835 /* 836 * Fill in information that is thread specific. Must be called with 837 * target process locked. If 'preferthread' is set, overwrite certain 838 * process-related fields that are maintained for both threads and 839 * processes. 840 */ 841 static void 842 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 843 { 844 struct proc *p; 845 846 p = td->td_proc; 847 kp->ki_tdaddr = td; 848 PROC_LOCK_ASSERT(p, MA_OWNED); 849 850 thread_lock(td); 851 if (td->td_wmesg != NULL) 852 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 853 else 854 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 855 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 856 if (TD_ON_LOCK(td)) { 857 kp->ki_kiflag |= KI_LOCKBLOCK; 858 strlcpy(kp->ki_lockname, td->td_lockname, 859 sizeof(kp->ki_lockname)); 860 } else { 861 kp->ki_kiflag &= ~KI_LOCKBLOCK; 862 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 863 } 864 865 if (p->p_state == PRS_NORMAL) { /* approximate. */ 866 if (TD_ON_RUNQ(td) || 867 TD_CAN_RUN(td) || 868 TD_IS_RUNNING(td)) { 869 kp->ki_stat = SRUN; 870 } else if (P_SHOULDSTOP(p)) { 871 kp->ki_stat = SSTOP; 872 } else if (TD_IS_SLEEPING(td)) { 873 kp->ki_stat = SSLEEP; 874 } else if (TD_ON_LOCK(td)) { 875 kp->ki_stat = SLOCK; 876 } else { 877 kp->ki_stat = SWAIT; 878 } 879 } else if (p->p_state == PRS_ZOMBIE) { 880 kp->ki_stat = SZOMB; 881 } else { 882 kp->ki_stat = SIDL; 883 } 884 885 /* Things in the thread */ 886 kp->ki_wchan = td->td_wchan; 887 kp->ki_pri.pri_level = td->td_priority; 888 kp->ki_pri.pri_native = td->td_base_pri; 889 kp->ki_lastcpu = td->td_lastcpu; 890 kp->ki_oncpu = td->td_oncpu; 891 kp->ki_tdflags = td->td_flags; 892 kp->ki_tid = td->td_tid; 893 kp->ki_numthreads = p->p_numthreads; 894 kp->ki_pcb = td->td_pcb; 895 kp->ki_kstack = (void *)td->td_kstack; 896 kp->ki_slptime = (ticks - td->td_slptick) / hz; 897 kp->ki_pri.pri_class = td->td_pri_class; 898 kp->ki_pri.pri_user = td->td_user_pri; 899 900 if (preferthread) { 901 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 902 kp->ki_pctcpu = sched_pctcpu(td); 903 kp->ki_estcpu = td->td_estcpu; 904 } 905 906 /* We can't get this anymore but ps etc never used it anyway. */ 907 kp->ki_rqindex = 0; 908 909 if (preferthread) 910 kp->ki_siglist = td->td_siglist; 911 kp->ki_sigmask = td->td_sigmask; 912 thread_unlock(td); 913 } 914 915 /* 916 * Fill in a kinfo_proc structure for the specified process. 917 * Must be called with the target process locked. 918 */ 919 void 920 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 921 { 922 923 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 924 925 fill_kinfo_proc_only(p, kp); 926 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 927 fill_kinfo_aggregate(p, kp); 928 } 929 930 struct pstats * 931 pstats_alloc(void) 932 { 933 934 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 935 } 936 937 /* 938 * Copy parts of p_stats; zero the rest of p_stats (statistics). 939 */ 940 void 941 pstats_fork(struct pstats *src, struct pstats *dst) 942 { 943 944 bzero(&dst->pstat_startzero, 945 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 946 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 947 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 948 } 949 950 void 951 pstats_free(struct pstats *ps) 952 { 953 954 free(ps, M_SUBPROC); 955 } 956 957 /* 958 * Locate a zombie process by number 959 */ 960 struct proc * 961 zpfind(pid_t pid) 962 { 963 struct proc *p; 964 965 sx_slock(&allproc_lock); 966 LIST_FOREACH(p, &zombproc, p_list) 967 if (p->p_pid == pid) { 968 PROC_LOCK(p); 969 break; 970 } 971 sx_sunlock(&allproc_lock); 972 return (p); 973 } 974 975 #define KERN_PROC_ZOMBMASK 0x3 976 #define KERN_PROC_NOTHREADS 0x4 977 978 #ifdef COMPAT_FREEBSD32 979 980 /* 981 * This function is typically used to copy out the kernel address, so 982 * it can be replaced by assignment of zero. 983 */ 984 static inline uint32_t 985 ptr32_trim(void *ptr) 986 { 987 uintptr_t uptr; 988 989 uptr = (uintptr_t)ptr; 990 return ((uptr > UINT_MAX) ? 0 : uptr); 991 } 992 993 #define PTRTRIM_CP(src,dst,fld) \ 994 do { (dst).fld = ptr32_trim((src).fld); } while (0) 995 996 static void 997 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 998 { 999 int i; 1000 1001 bzero(ki32, sizeof(struct kinfo_proc32)); 1002 ki32->ki_structsize = sizeof(struct kinfo_proc32); 1003 CP(*ki, *ki32, ki_layout); 1004 PTRTRIM_CP(*ki, *ki32, ki_args); 1005 PTRTRIM_CP(*ki, *ki32, ki_paddr); 1006 PTRTRIM_CP(*ki, *ki32, ki_addr); 1007 PTRTRIM_CP(*ki, *ki32, ki_tracep); 1008 PTRTRIM_CP(*ki, *ki32, ki_textvp); 1009 PTRTRIM_CP(*ki, *ki32, ki_fd); 1010 PTRTRIM_CP(*ki, *ki32, ki_vmspace); 1011 PTRTRIM_CP(*ki, *ki32, ki_wchan); 1012 CP(*ki, *ki32, ki_pid); 1013 CP(*ki, *ki32, ki_ppid); 1014 CP(*ki, *ki32, ki_pgid); 1015 CP(*ki, *ki32, ki_tpgid); 1016 CP(*ki, *ki32, ki_sid); 1017 CP(*ki, *ki32, ki_tsid); 1018 CP(*ki, *ki32, ki_jobc); 1019 CP(*ki, *ki32, ki_tdev); 1020 CP(*ki, *ki32, ki_siglist); 1021 CP(*ki, *ki32, ki_sigmask); 1022 CP(*ki, *ki32, ki_sigignore); 1023 CP(*ki, *ki32, ki_sigcatch); 1024 CP(*ki, *ki32, ki_uid); 1025 CP(*ki, *ki32, ki_ruid); 1026 CP(*ki, *ki32, ki_svuid); 1027 CP(*ki, *ki32, ki_rgid); 1028 CP(*ki, *ki32, ki_svgid); 1029 CP(*ki, *ki32, ki_ngroups); 1030 for (i = 0; i < KI_NGROUPS; i++) 1031 CP(*ki, *ki32, ki_groups[i]); 1032 CP(*ki, *ki32, ki_size); 1033 CP(*ki, *ki32, ki_rssize); 1034 CP(*ki, *ki32, ki_swrss); 1035 CP(*ki, *ki32, ki_tsize); 1036 CP(*ki, *ki32, ki_dsize); 1037 CP(*ki, *ki32, ki_ssize); 1038 CP(*ki, *ki32, ki_xstat); 1039 CP(*ki, *ki32, ki_acflag); 1040 CP(*ki, *ki32, ki_pctcpu); 1041 CP(*ki, *ki32, ki_estcpu); 1042 CP(*ki, *ki32, ki_slptime); 1043 CP(*ki, *ki32, ki_swtime); 1044 CP(*ki, *ki32, ki_runtime); 1045 TV_CP(*ki, *ki32, ki_start); 1046 TV_CP(*ki, *ki32, ki_childtime); 1047 CP(*ki, *ki32, ki_flag); 1048 CP(*ki, *ki32, ki_kiflag); 1049 CP(*ki, *ki32, ki_traceflag); 1050 CP(*ki, *ki32, ki_stat); 1051 CP(*ki, *ki32, ki_nice); 1052 CP(*ki, *ki32, ki_lock); 1053 CP(*ki, *ki32, ki_rqindex); 1054 CP(*ki, *ki32, ki_oncpu); 1055 CP(*ki, *ki32, ki_lastcpu); 1056 bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1); 1057 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 1058 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 1059 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 1060 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 1061 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 1062 CP(*ki, *ki32, ki_cr_flags); 1063 CP(*ki, *ki32, ki_jid); 1064 CP(*ki, *ki32, ki_numthreads); 1065 CP(*ki, *ki32, ki_tid); 1066 CP(*ki, *ki32, ki_pri); 1067 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 1068 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 1069 PTRTRIM_CP(*ki, *ki32, ki_pcb); 1070 PTRTRIM_CP(*ki, *ki32, ki_kstack); 1071 PTRTRIM_CP(*ki, *ki32, ki_udata); 1072 CP(*ki, *ki32, ki_sflag); 1073 CP(*ki, *ki32, ki_tdflags); 1074 } 1075 1076 static int 1077 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1078 { 1079 struct kinfo_proc32 ki32; 1080 int error; 1081 1082 if (req->flags & SCTL_MASK32) { 1083 freebsd32_kinfo_proc_out(ki, &ki32); 1084 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32)); 1085 } else 1086 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)); 1087 return (error); 1088 } 1089 #else 1090 static int 1091 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1092 { 1093 1094 return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc))); 1095 } 1096 #endif 1097 1098 /* 1099 * Must be called with the process locked and will return with it unlocked. 1100 */ 1101 static int 1102 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 1103 { 1104 struct thread *td; 1105 struct kinfo_proc kinfo_proc; 1106 int error = 0; 1107 struct proc *np; 1108 pid_t pid = p->p_pid; 1109 1110 PROC_LOCK_ASSERT(p, MA_OWNED); 1111 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1112 1113 fill_kinfo_proc(p, &kinfo_proc); 1114 if (flags & KERN_PROC_NOTHREADS) 1115 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1116 else { 1117 FOREACH_THREAD_IN_PROC(p, td) { 1118 fill_kinfo_thread(td, &kinfo_proc, 1); 1119 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1120 if (error) 1121 break; 1122 } 1123 } 1124 PROC_UNLOCK(p); 1125 if (error) 1126 return (error); 1127 if (flags & KERN_PROC_ZOMBMASK) 1128 np = zpfind(pid); 1129 else { 1130 if (pid == 0) 1131 return (0); 1132 np = pfind(pid); 1133 } 1134 if (np == NULL) 1135 return (ESRCH); 1136 if (np != p) { 1137 PROC_UNLOCK(np); 1138 return (ESRCH); 1139 } 1140 PROC_UNLOCK(np); 1141 return (0); 1142 } 1143 1144 static int 1145 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1146 { 1147 int *name = (int*) arg1; 1148 u_int namelen = arg2; 1149 struct proc *p; 1150 int flags, doingzomb, oid_number; 1151 int error = 0; 1152 1153 oid_number = oidp->oid_number; 1154 if (oid_number != KERN_PROC_ALL && 1155 (oid_number & KERN_PROC_INC_THREAD) == 0) 1156 flags = KERN_PROC_NOTHREADS; 1157 else { 1158 flags = 0; 1159 oid_number &= ~KERN_PROC_INC_THREAD; 1160 } 1161 if (oid_number == KERN_PROC_PID) { 1162 if (namelen != 1) 1163 return (EINVAL); 1164 error = sysctl_wire_old_buffer(req, 0); 1165 if (error) 1166 return (error); 1167 p = pfind((pid_t)name[0]); 1168 if (!p) 1169 return (ESRCH); 1170 if ((error = p_cansee(curthread, p))) { 1171 PROC_UNLOCK(p); 1172 return (error); 1173 } 1174 error = sysctl_out_proc(p, req, flags); 1175 return (error); 1176 } 1177 1178 switch (oid_number) { 1179 case KERN_PROC_ALL: 1180 if (namelen != 0) 1181 return (EINVAL); 1182 break; 1183 case KERN_PROC_PROC: 1184 if (namelen != 0 && namelen != 1) 1185 return (EINVAL); 1186 break; 1187 default: 1188 if (namelen != 1) 1189 return (EINVAL); 1190 break; 1191 } 1192 1193 if (!req->oldptr) { 1194 /* overestimate by 5 procs */ 1195 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1196 if (error) 1197 return (error); 1198 } 1199 error = sysctl_wire_old_buffer(req, 0); 1200 if (error != 0) 1201 return (error); 1202 sx_slock(&allproc_lock); 1203 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1204 if (!doingzomb) 1205 p = LIST_FIRST(&allproc); 1206 else 1207 p = LIST_FIRST(&zombproc); 1208 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1209 /* 1210 * Skip embryonic processes. 1211 */ 1212 PROC_SLOCK(p); 1213 if (p->p_state == PRS_NEW) { 1214 PROC_SUNLOCK(p); 1215 continue; 1216 } 1217 PROC_SUNLOCK(p); 1218 PROC_LOCK(p); 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 kve->kve_fileid = 0; 1761 kve->kve_fsid = 0; 1762 freepath = NULL; 1763 fullpath = ""; 1764 if (lobj) { 1765 vp = NULL; 1766 switch (lobj->type) { 1767 case OBJT_DEFAULT: 1768 kve->kve_type = KVME_TYPE_DEFAULT; 1769 break; 1770 case OBJT_VNODE: 1771 kve->kve_type = KVME_TYPE_VNODE; 1772 vp = lobj->handle; 1773 vref(vp); 1774 break; 1775 case OBJT_SWAP: 1776 kve->kve_type = KVME_TYPE_SWAP; 1777 break; 1778 case OBJT_DEVICE: 1779 kve->kve_type = KVME_TYPE_DEVICE; 1780 break; 1781 case OBJT_PHYS: 1782 kve->kve_type = KVME_TYPE_PHYS; 1783 break; 1784 case OBJT_DEAD: 1785 kve->kve_type = KVME_TYPE_DEAD; 1786 break; 1787 case OBJT_SG: 1788 kve->kve_type = KVME_TYPE_SG; 1789 break; 1790 default: 1791 kve->kve_type = KVME_TYPE_UNKNOWN; 1792 break; 1793 } 1794 if (lobj != obj) 1795 VM_OBJECT_UNLOCK(lobj); 1796 1797 kve->kve_ref_count = obj->ref_count; 1798 kve->kve_shadow_count = obj->shadow_count; 1799 VM_OBJECT_UNLOCK(obj); 1800 if (vp != NULL) { 1801 vn_fullpath(curthread, vp, &fullpath, 1802 &freepath); 1803 cred = curthread->td_ucred; 1804 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1805 vn_lock(vp, LK_SHARED | LK_RETRY); 1806 if (VOP_GETATTR(vp, &va, cred) == 0) { 1807 kve->kve_fileid = va.va_fileid; 1808 kve->kve_fsid = va.va_fsid; 1809 } 1810 vput(vp); 1811 VFS_UNLOCK_GIANT(vfslocked); 1812 } 1813 } else { 1814 kve->kve_type = KVME_TYPE_NONE; 1815 kve->kve_ref_count = 0; 1816 kve->kve_shadow_count = 0; 1817 } 1818 1819 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1820 if (freepath != NULL) 1821 free(freepath, M_TEMP); 1822 1823 /* Pack record size down */ 1824 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 1825 strlen(kve->kve_path) + 1; 1826 kve->kve_structsize = roundup(kve->kve_structsize, 1827 sizeof(uint64_t)); 1828 error = SYSCTL_OUT(req, kve, kve->kve_structsize); 1829 vm_map_lock_read(map); 1830 if (error) 1831 break; 1832 if (last_timestamp != map->timestamp) { 1833 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1834 entry = tmp_entry; 1835 } 1836 } 1837 vm_map_unlock_read(map); 1838 vmspace_free(vm); 1839 PRELE(p); 1840 free(kve, M_TEMP); 1841 return (error); 1842 } 1843 1844 #if defined(STACK) || defined(DDB) 1845 static int 1846 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 1847 { 1848 struct kinfo_kstack *kkstp; 1849 int error, i, *name, numthreads; 1850 lwpid_t *lwpidarray; 1851 struct thread *td; 1852 struct stack *st; 1853 struct sbuf sb; 1854 struct proc *p; 1855 1856 name = (int *)arg1; 1857 if ((p = pfind((pid_t)name[0])) == NULL) 1858 return (ESRCH); 1859 /* XXXRW: Not clear ESRCH is the right error during proc execve(). */ 1860 if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) { 1861 PROC_UNLOCK(p); 1862 return (ESRCH); 1863 } 1864 if ((error = p_candebug(curthread, p))) { 1865 PROC_UNLOCK(p); 1866 return (error); 1867 } 1868 _PHOLD(p); 1869 PROC_UNLOCK(p); 1870 1871 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 1872 st = stack_create(); 1873 1874 lwpidarray = NULL; 1875 numthreads = 0; 1876 PROC_LOCK(p); 1877 repeat: 1878 if (numthreads < p->p_numthreads) { 1879 if (lwpidarray != NULL) { 1880 free(lwpidarray, M_TEMP); 1881 lwpidarray = NULL; 1882 } 1883 numthreads = p->p_numthreads; 1884 PROC_UNLOCK(p); 1885 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 1886 M_WAITOK | M_ZERO); 1887 PROC_LOCK(p); 1888 goto repeat; 1889 } 1890 i = 0; 1891 1892 /* 1893 * XXXRW: During the below loop, execve(2) and countless other sorts 1894 * of changes could have taken place. Should we check to see if the 1895 * vmspace has been replaced, or the like, in order to prevent 1896 * giving a snapshot that spans, say, execve(2), with some threads 1897 * before and some after? Among other things, the credentials could 1898 * have changed, in which case the right to extract debug info might 1899 * no longer be assured. 1900 */ 1901 FOREACH_THREAD_IN_PROC(p, td) { 1902 KASSERT(i < numthreads, 1903 ("sysctl_kern_proc_kstack: numthreads")); 1904 lwpidarray[i] = td->td_tid; 1905 i++; 1906 } 1907 numthreads = i; 1908 for (i = 0; i < numthreads; i++) { 1909 td = thread_find(p, lwpidarray[i]); 1910 if (td == NULL) { 1911 continue; 1912 } 1913 bzero(kkstp, sizeof(*kkstp)); 1914 (void)sbuf_new(&sb, kkstp->kkst_trace, 1915 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 1916 thread_lock(td); 1917 kkstp->kkst_tid = td->td_tid; 1918 if (TD_IS_SWAPPED(td)) 1919 kkstp->kkst_state = KKST_STATE_SWAPPED; 1920 else if (TD_IS_RUNNING(td)) 1921 kkstp->kkst_state = KKST_STATE_RUNNING; 1922 else { 1923 kkstp->kkst_state = KKST_STATE_STACKOK; 1924 stack_save_td(st, td); 1925 } 1926 thread_unlock(td); 1927 PROC_UNLOCK(p); 1928 stack_sbuf_print(&sb, st); 1929 sbuf_finish(&sb); 1930 sbuf_delete(&sb); 1931 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 1932 PROC_LOCK(p); 1933 if (error) 1934 break; 1935 } 1936 _PRELE(p); 1937 PROC_UNLOCK(p); 1938 if (lwpidarray != NULL) 1939 free(lwpidarray, M_TEMP); 1940 stack_destroy(st); 1941 free(kkstp, M_TEMP); 1942 return (error); 1943 } 1944 #endif 1945 1946 /* 1947 * This sysctl allows a process to retrieve the full list of groups from 1948 * itself or another process. 1949 */ 1950 static int 1951 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 1952 { 1953 pid_t *pidp = (pid_t *)arg1; 1954 unsigned int arglen = arg2; 1955 struct proc *p; 1956 struct ucred *cred; 1957 int error; 1958 1959 if (arglen != 1) 1960 return (EINVAL); 1961 if (*pidp == -1) { /* -1 means this process */ 1962 p = req->td->td_proc; 1963 } else { 1964 p = pfind(*pidp); 1965 if (p == NULL) 1966 return (ESRCH); 1967 if ((error = p_cansee(curthread, p)) != 0) { 1968 PROC_UNLOCK(p); 1969 return (error); 1970 } 1971 } 1972 1973 cred = crhold(p->p_ucred); 1974 if (*pidp != -1) 1975 PROC_UNLOCK(p); 1976 1977 error = SYSCTL_OUT(req, cred->cr_groups, 1978 cred->cr_ngroups * sizeof(gid_t)); 1979 crfree(cred); 1980 return (error); 1981 } 1982 1983 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1984 1985 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 1986 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 1987 "Return entire process table"); 1988 1989 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 1990 sysctl_kern_proc, "Process table"); 1991 1992 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 1993 sysctl_kern_proc, "Process table"); 1994 1995 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 1996 sysctl_kern_proc, "Process table"); 1997 1998 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 1999 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2000 2001 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2002 sysctl_kern_proc, "Process table"); 2003 2004 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2005 sysctl_kern_proc, "Process table"); 2006 2007 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2008 sysctl_kern_proc, "Process table"); 2009 2010 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2011 sysctl_kern_proc, "Process table"); 2012 2013 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 2014 sysctl_kern_proc, "Return process table, no threads"); 2015 2016 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 2017 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2018 sysctl_kern_proc_args, "Process argument list"); 2019 2020 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 2021 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2022 2023 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 2024 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 2025 "Process syscall vector name (ABI type)"); 2026 2027 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 2028 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2029 2030 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 2031 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2032 2033 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 2034 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2035 2036 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 2037 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2038 2039 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 2040 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2041 2042 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 2043 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2044 2045 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 2046 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2047 2048 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 2049 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2050 2051 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 2052 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 2053 "Return process table, no threads"); 2054 2055 #ifdef COMPAT_FREEBSD7 2056 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 2057 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 2058 #endif 2059 2060 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 2061 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 2062 2063 #if defined(STACK) || defined(DDB) 2064 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 2065 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 2066 #endif 2067 2068 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2069 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 2070