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/elf.h> 45 #include <sys/exec.h> 46 #include <sys/kernel.h> 47 #include <sys/limits.h> 48 #include <sys/lock.h> 49 #include <sys/loginclass.h> 50 #include <sys/malloc.h> 51 #include <sys/mman.h> 52 #include <sys/mount.h> 53 #include <sys/mutex.h> 54 #include <sys/proc.h> 55 #include <sys/ptrace.h> 56 #include <sys/refcount.h> 57 #include <sys/resourcevar.h> 58 #include <sys/rwlock.h> 59 #include <sys/sbuf.h> 60 #include <sys/sysent.h> 61 #include <sys/sched.h> 62 #include <sys/smp.h> 63 #include <sys/stack.h> 64 #include <sys/stat.h> 65 #include <sys/sysctl.h> 66 #include <sys/filedesc.h> 67 #include <sys/tty.h> 68 #include <sys/signalvar.h> 69 #include <sys/sdt.h> 70 #include <sys/sx.h> 71 #include <sys/user.h> 72 #include <sys/jail.h> 73 #include <sys/vnode.h> 74 #include <sys/eventhandler.h> 75 76 #ifdef DDB 77 #include <ddb/ddb.h> 78 #endif 79 80 #include <vm/vm.h> 81 #include <vm/vm_param.h> 82 #include <vm/vm_extern.h> 83 #include <vm/pmap.h> 84 #include <vm/vm_map.h> 85 #include <vm/vm_object.h> 86 #include <vm/vm_page.h> 87 #include <vm/uma.h> 88 89 #ifdef COMPAT_FREEBSD32 90 #include <compat/freebsd32/freebsd32.h> 91 #include <compat/freebsd32/freebsd32_util.h> 92 #endif 93 94 SDT_PROVIDER_DEFINE(proc); 95 SDT_PROBE_DEFINE4(proc, kernel, ctor, entry, entry, "struct proc *", "int", 96 "void *", "int"); 97 SDT_PROBE_DEFINE4(proc, kernel, ctor, return, return, "struct proc *", "int", 98 "void *", "int"); 99 SDT_PROBE_DEFINE4(proc, kernel, dtor, entry, entry, "struct proc *", "int", 100 "void *", "struct thread *"); 101 SDT_PROBE_DEFINE3(proc, kernel, dtor, return, return, "struct proc *", "int", 102 "void *"); 103 SDT_PROBE_DEFINE3(proc, kernel, init, entry, entry, "struct proc *", "int", 104 "int"); 105 SDT_PROBE_DEFINE3(proc, kernel, init, return, return, "struct proc *", "int", 106 "int"); 107 108 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 109 MALLOC_DEFINE(M_SESSION, "session", "session header"); 110 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 111 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 112 113 static void doenterpgrp(struct proc *, struct pgrp *); 114 static void orphanpg(struct pgrp *pg); 115 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); 116 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 117 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 118 int preferthread); 119 static void pgadjustjobc(struct pgrp *pgrp, int entering); 120 static void pgdelete(struct pgrp *); 121 static int proc_ctor(void *mem, int size, void *arg, int flags); 122 static void proc_dtor(void *mem, int size, void *arg); 123 static int proc_init(void *mem, int size, int flags); 124 static void proc_fini(void *mem, int size); 125 static void pargs_free(struct pargs *pa); 126 static struct proc *zpfind_locked(pid_t pid); 127 128 /* 129 * Other process lists 130 */ 131 struct pidhashhead *pidhashtbl; 132 u_long pidhash; 133 struct pgrphashhead *pgrphashtbl; 134 u_long pgrphash; 135 struct proclist allproc; 136 struct proclist zombproc; 137 struct sx allproc_lock; 138 struct sx proctree_lock; 139 struct mtx ppeers_lock; 140 uma_zone_t proc_zone; 141 142 int kstack_pages = KSTACK_PAGES; 143 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, 144 "Kernel stack size in pages"); 145 146 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 147 #ifdef COMPAT_FREEBSD32 148 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); 149 #endif 150 151 /* 152 * Initialize global process hashing structures. 153 */ 154 void 155 procinit() 156 { 157 158 sx_init(&allproc_lock, "allproc"); 159 sx_init(&proctree_lock, "proctree"); 160 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 161 LIST_INIT(&allproc); 162 LIST_INIT(&zombproc); 163 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 164 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 165 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 166 proc_ctor, proc_dtor, proc_init, proc_fini, 167 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 168 uihashinit(); 169 } 170 171 /* 172 * Prepare a proc for use. 173 */ 174 static int 175 proc_ctor(void *mem, int size, void *arg, int flags) 176 { 177 struct proc *p; 178 179 p = (struct proc *)mem; 180 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0); 181 EVENTHANDLER_INVOKE(process_ctor, p); 182 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0); 183 return (0); 184 } 185 186 /* 187 * Reclaim a proc after use. 188 */ 189 static void 190 proc_dtor(void *mem, int size, void *arg) 191 { 192 struct proc *p; 193 struct thread *td; 194 195 /* INVARIANTS checks go here */ 196 p = (struct proc *)mem; 197 td = FIRST_THREAD_IN_PROC(p); 198 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0); 199 if (td != NULL) { 200 #ifdef INVARIANTS 201 KASSERT((p->p_numthreads == 1), 202 ("bad number of threads in exiting process")); 203 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 204 #endif 205 /* Free all OSD associated to this thread. */ 206 osd_thread_exit(td); 207 } 208 EVENTHANDLER_INVOKE(process_dtor, p); 209 if (p->p_ksi != NULL) 210 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 211 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0); 212 } 213 214 /* 215 * Initialize type-stable parts of a proc (when newly created). 216 */ 217 static int 218 proc_init(void *mem, int size, int flags) 219 { 220 struct proc *p; 221 222 p = (struct proc *)mem; 223 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0); 224 p->p_sched = (struct p_sched *)&p[1]; 225 bzero(&p->p_mtx, sizeof(struct mtx)); 226 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 227 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 228 cv_init(&p->p_pwait, "ppwait"); 229 cv_init(&p->p_dbgwait, "dbgwait"); 230 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 231 EVENTHANDLER_INVOKE(process_init, p); 232 p->p_stats = pstats_alloc(); 233 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0); 234 return (0); 235 } 236 237 /* 238 * UMA should ensure that this function is never called. 239 * Freeing a proc structure would violate type stability. 240 */ 241 static void 242 proc_fini(void *mem, int size) 243 { 244 #ifdef notnow 245 struct proc *p; 246 247 p = (struct proc *)mem; 248 EVENTHANDLER_INVOKE(process_fini, p); 249 pstats_free(p->p_stats); 250 thread_free(FIRST_THREAD_IN_PROC(p)); 251 mtx_destroy(&p->p_mtx); 252 if (p->p_ksi != NULL) 253 ksiginfo_free(p->p_ksi); 254 #else 255 panic("proc reclaimed"); 256 #endif 257 } 258 259 /* 260 * Is p an inferior of the current process? 261 */ 262 int 263 inferior(p) 264 register struct proc *p; 265 { 266 267 sx_assert(&proctree_lock, SX_LOCKED); 268 for (; p != curproc; p = p->p_pptr) 269 if (p->p_pid == 0) 270 return (0); 271 return (1); 272 } 273 274 struct proc * 275 pfind_locked(pid_t pid) 276 { 277 struct proc *p; 278 279 sx_assert(&allproc_lock, SX_LOCKED); 280 LIST_FOREACH(p, PIDHASH(pid), p_hash) { 281 if (p->p_pid == pid) { 282 PROC_LOCK(p); 283 if (p->p_state == PRS_NEW) { 284 PROC_UNLOCK(p); 285 p = NULL; 286 } 287 break; 288 } 289 } 290 return (p); 291 } 292 293 /* 294 * Locate a process by number; return only "live" processes -- i.e., neither 295 * zombies nor newly born but incompletely initialized processes. By not 296 * returning processes in the PRS_NEW state, we allow callers to avoid 297 * testing for that condition to avoid dereferencing p_ucred, et al. 298 */ 299 struct proc * 300 pfind(pid_t pid) 301 { 302 struct proc *p; 303 304 sx_slock(&allproc_lock); 305 p = pfind_locked(pid); 306 sx_sunlock(&allproc_lock); 307 return (p); 308 } 309 310 static struct proc * 311 pfind_tid_locked(pid_t tid) 312 { 313 struct proc *p; 314 struct thread *td; 315 316 sx_assert(&allproc_lock, SX_LOCKED); 317 FOREACH_PROC_IN_SYSTEM(p) { 318 PROC_LOCK(p); 319 if (p->p_state == PRS_NEW) { 320 PROC_UNLOCK(p); 321 continue; 322 } 323 FOREACH_THREAD_IN_PROC(p, td) { 324 if (td->td_tid == tid) 325 goto found; 326 } 327 PROC_UNLOCK(p); 328 } 329 found: 330 return (p); 331 } 332 333 /* 334 * Locate a process group by number. 335 * The caller must hold proctree_lock. 336 */ 337 struct pgrp * 338 pgfind(pgid) 339 register pid_t pgid; 340 { 341 register struct pgrp *pgrp; 342 343 sx_assert(&proctree_lock, SX_LOCKED); 344 345 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 346 if (pgrp->pg_id == pgid) { 347 PGRP_LOCK(pgrp); 348 return (pgrp); 349 } 350 } 351 return (NULL); 352 } 353 354 /* 355 * Locate process and do additional manipulations, depending on flags. 356 */ 357 int 358 pget(pid_t pid, int flags, struct proc **pp) 359 { 360 struct proc *p; 361 int error; 362 363 sx_slock(&allproc_lock); 364 if (pid <= PID_MAX) { 365 p = pfind_locked(pid); 366 if (p == NULL && (flags & PGET_NOTWEXIT) == 0) 367 p = zpfind_locked(pid); 368 } else if ((flags & PGET_NOTID) == 0) { 369 p = pfind_tid_locked(pid); 370 } else { 371 p = NULL; 372 } 373 sx_sunlock(&allproc_lock); 374 if (p == NULL) 375 return (ESRCH); 376 if ((flags & PGET_CANSEE) != 0) { 377 error = p_cansee(curthread, p); 378 if (error != 0) 379 goto errout; 380 } 381 if ((flags & PGET_CANDEBUG) != 0) { 382 error = p_candebug(curthread, p); 383 if (error != 0) 384 goto errout; 385 } 386 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) { 387 error = EPERM; 388 goto errout; 389 } 390 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) { 391 error = ESRCH; 392 goto errout; 393 } 394 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) { 395 /* 396 * XXXRW: Not clear ESRCH is the right error during proc 397 * execve(). 398 */ 399 error = ESRCH; 400 goto errout; 401 } 402 if ((flags & PGET_HOLD) != 0) { 403 _PHOLD(p); 404 PROC_UNLOCK(p); 405 } 406 *pp = p; 407 return (0); 408 errout: 409 PROC_UNLOCK(p); 410 return (error); 411 } 412 413 /* 414 * Create a new process group. 415 * pgid must be equal to the pid of p. 416 * Begin a new session if required. 417 */ 418 int 419 enterpgrp(p, pgid, pgrp, sess) 420 register struct proc *p; 421 pid_t pgid; 422 struct pgrp *pgrp; 423 struct session *sess; 424 { 425 426 sx_assert(&proctree_lock, SX_XLOCKED); 427 428 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 429 KASSERT(p->p_pid == pgid, 430 ("enterpgrp: new pgrp and pid != pgid")); 431 KASSERT(pgfind(pgid) == NULL, 432 ("enterpgrp: pgrp with pgid exists")); 433 KASSERT(!SESS_LEADER(p), 434 ("enterpgrp: session leader attempted setpgrp")); 435 436 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 437 438 if (sess != NULL) { 439 /* 440 * new session 441 */ 442 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 443 PROC_LOCK(p); 444 p->p_flag &= ~P_CONTROLT; 445 PROC_UNLOCK(p); 446 PGRP_LOCK(pgrp); 447 sess->s_leader = p; 448 sess->s_sid = p->p_pid; 449 refcount_init(&sess->s_count, 1); 450 sess->s_ttyvp = NULL; 451 sess->s_ttydp = NULL; 452 sess->s_ttyp = NULL; 453 bcopy(p->p_session->s_login, sess->s_login, 454 sizeof(sess->s_login)); 455 pgrp->pg_session = sess; 456 KASSERT(p == curproc, 457 ("enterpgrp: mksession and p != curproc")); 458 } else { 459 pgrp->pg_session = p->p_session; 460 sess_hold(pgrp->pg_session); 461 PGRP_LOCK(pgrp); 462 } 463 pgrp->pg_id = pgid; 464 LIST_INIT(&pgrp->pg_members); 465 466 /* 467 * As we have an exclusive lock of proctree_lock, 468 * this should not deadlock. 469 */ 470 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 471 pgrp->pg_jobc = 0; 472 SLIST_INIT(&pgrp->pg_sigiolst); 473 PGRP_UNLOCK(pgrp); 474 475 doenterpgrp(p, pgrp); 476 477 return (0); 478 } 479 480 /* 481 * Move p to an existing process group 482 */ 483 int 484 enterthispgrp(p, pgrp) 485 register struct proc *p; 486 struct pgrp *pgrp; 487 { 488 489 sx_assert(&proctree_lock, SX_XLOCKED); 490 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 491 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 492 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 493 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 494 KASSERT(pgrp->pg_session == p->p_session, 495 ("%s: pgrp's session %p, p->p_session %p.\n", 496 __func__, 497 pgrp->pg_session, 498 p->p_session)); 499 KASSERT(pgrp != p->p_pgrp, 500 ("%s: p belongs to pgrp.", __func__)); 501 502 doenterpgrp(p, pgrp); 503 504 return (0); 505 } 506 507 /* 508 * Move p to a process group 509 */ 510 static void 511 doenterpgrp(p, pgrp) 512 struct proc *p; 513 struct pgrp *pgrp; 514 { 515 struct pgrp *savepgrp; 516 517 sx_assert(&proctree_lock, SX_XLOCKED); 518 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 519 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 520 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 521 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 522 523 savepgrp = p->p_pgrp; 524 525 /* 526 * Adjust eligibility of affected pgrps to participate in job control. 527 * Increment eligibility counts before decrementing, otherwise we 528 * could reach 0 spuriously during the first call. 529 */ 530 fixjobc(p, pgrp, 1); 531 fixjobc(p, p->p_pgrp, 0); 532 533 PGRP_LOCK(pgrp); 534 PGRP_LOCK(savepgrp); 535 PROC_LOCK(p); 536 LIST_REMOVE(p, p_pglist); 537 p->p_pgrp = pgrp; 538 PROC_UNLOCK(p); 539 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 540 PGRP_UNLOCK(savepgrp); 541 PGRP_UNLOCK(pgrp); 542 if (LIST_EMPTY(&savepgrp->pg_members)) 543 pgdelete(savepgrp); 544 } 545 546 /* 547 * remove process from process group 548 */ 549 int 550 leavepgrp(p) 551 register struct proc *p; 552 { 553 struct pgrp *savepgrp; 554 555 sx_assert(&proctree_lock, SX_XLOCKED); 556 savepgrp = p->p_pgrp; 557 PGRP_LOCK(savepgrp); 558 PROC_LOCK(p); 559 LIST_REMOVE(p, p_pglist); 560 p->p_pgrp = NULL; 561 PROC_UNLOCK(p); 562 PGRP_UNLOCK(savepgrp); 563 if (LIST_EMPTY(&savepgrp->pg_members)) 564 pgdelete(savepgrp); 565 return (0); 566 } 567 568 /* 569 * delete a process group 570 */ 571 static void 572 pgdelete(pgrp) 573 register struct pgrp *pgrp; 574 { 575 struct session *savesess; 576 struct tty *tp; 577 578 sx_assert(&proctree_lock, SX_XLOCKED); 579 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 580 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 581 582 /* 583 * Reset any sigio structures pointing to us as a result of 584 * F_SETOWN with our pgid. 585 */ 586 funsetownlst(&pgrp->pg_sigiolst); 587 588 PGRP_LOCK(pgrp); 589 tp = pgrp->pg_session->s_ttyp; 590 LIST_REMOVE(pgrp, pg_hash); 591 savesess = pgrp->pg_session; 592 PGRP_UNLOCK(pgrp); 593 594 /* Remove the reference to the pgrp before deallocating it. */ 595 if (tp != NULL) { 596 tty_lock(tp); 597 tty_rel_pgrp(tp, pgrp); 598 } 599 600 mtx_destroy(&pgrp->pg_mtx); 601 free(pgrp, M_PGRP); 602 sess_release(savesess); 603 } 604 605 static void 606 pgadjustjobc(pgrp, entering) 607 struct pgrp *pgrp; 608 int entering; 609 { 610 611 PGRP_LOCK(pgrp); 612 if (entering) 613 pgrp->pg_jobc++; 614 else { 615 --pgrp->pg_jobc; 616 if (pgrp->pg_jobc == 0) 617 orphanpg(pgrp); 618 } 619 PGRP_UNLOCK(pgrp); 620 } 621 622 /* 623 * Adjust pgrp jobc counters when specified process changes process group. 624 * We count the number of processes in each process group that "qualify" 625 * the group for terminal job control (those with a parent in a different 626 * process group of the same session). If that count reaches zero, the 627 * process group becomes orphaned. Check both the specified process' 628 * process group and that of its children. 629 * entering == 0 => p is leaving specified group. 630 * entering == 1 => p is entering specified group. 631 */ 632 void 633 fixjobc(p, pgrp, entering) 634 register struct proc *p; 635 register struct pgrp *pgrp; 636 int entering; 637 { 638 register struct pgrp *hispgrp; 639 register struct session *mysession; 640 641 sx_assert(&proctree_lock, SX_LOCKED); 642 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 643 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 644 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 645 646 /* 647 * Check p's parent to see whether p qualifies its own process 648 * group; if so, adjust count for p's process group. 649 */ 650 mysession = pgrp->pg_session; 651 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 652 hispgrp->pg_session == mysession) 653 pgadjustjobc(pgrp, entering); 654 655 /* 656 * Check this process' children to see whether they qualify 657 * their process groups; if so, adjust counts for children's 658 * process groups. 659 */ 660 LIST_FOREACH(p, &p->p_children, p_sibling) { 661 hispgrp = p->p_pgrp; 662 if (hispgrp == pgrp || 663 hispgrp->pg_session != mysession) 664 continue; 665 PROC_LOCK(p); 666 if (p->p_state == PRS_ZOMBIE) { 667 PROC_UNLOCK(p); 668 continue; 669 } 670 PROC_UNLOCK(p); 671 pgadjustjobc(hispgrp, entering); 672 } 673 } 674 675 /* 676 * A process group has become orphaned; 677 * if there are any stopped processes in the group, 678 * hang-up all process in that group. 679 */ 680 static void 681 orphanpg(pg) 682 struct pgrp *pg; 683 { 684 register struct proc *p; 685 686 PGRP_LOCK_ASSERT(pg, MA_OWNED); 687 688 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 689 PROC_LOCK(p); 690 if (P_SHOULDSTOP(p)) { 691 PROC_UNLOCK(p); 692 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 693 PROC_LOCK(p); 694 kern_psignal(p, SIGHUP); 695 kern_psignal(p, SIGCONT); 696 PROC_UNLOCK(p); 697 } 698 return; 699 } 700 PROC_UNLOCK(p); 701 } 702 } 703 704 void 705 sess_hold(struct session *s) 706 { 707 708 refcount_acquire(&s->s_count); 709 } 710 711 void 712 sess_release(struct session *s) 713 { 714 715 if (refcount_release(&s->s_count)) { 716 if (s->s_ttyp != NULL) { 717 tty_lock(s->s_ttyp); 718 tty_rel_sess(s->s_ttyp, s); 719 } 720 mtx_destroy(&s->s_mtx); 721 free(s, M_SESSION); 722 } 723 } 724 725 #ifdef DDB 726 727 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 728 { 729 register struct pgrp *pgrp; 730 register struct proc *p; 731 register int i; 732 733 for (i = 0; i <= pgrphash; i++) { 734 if (!LIST_EMPTY(&pgrphashtbl[i])) { 735 printf("\tindx %d\n", i); 736 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 737 printf( 738 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 739 (void *)pgrp, (long)pgrp->pg_id, 740 (void *)pgrp->pg_session, 741 pgrp->pg_session->s_count, 742 (void *)LIST_FIRST(&pgrp->pg_members)); 743 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 744 printf("\t\tpid %ld addr %p pgrp %p\n", 745 (long)p->p_pid, (void *)p, 746 (void *)p->p_pgrp); 747 } 748 } 749 } 750 } 751 } 752 #endif /* DDB */ 753 754 /* 755 * Calculate the kinfo_proc members which contain process-wide 756 * informations. 757 * Must be called with the target process locked. 758 */ 759 static void 760 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) 761 { 762 struct thread *td; 763 764 PROC_LOCK_ASSERT(p, MA_OWNED); 765 766 kp->ki_estcpu = 0; 767 kp->ki_pctcpu = 0; 768 FOREACH_THREAD_IN_PROC(p, td) { 769 thread_lock(td); 770 kp->ki_pctcpu += sched_pctcpu(td); 771 kp->ki_estcpu += td->td_estcpu; 772 thread_unlock(td); 773 } 774 } 775 776 /* 777 * Clear kinfo_proc and fill in any information that is common 778 * to all threads in the process. 779 * Must be called with the target process locked. 780 */ 781 static void 782 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 783 { 784 struct thread *td0; 785 struct tty *tp; 786 struct session *sp; 787 struct ucred *cred; 788 struct sigacts *ps; 789 790 PROC_LOCK_ASSERT(p, MA_OWNED); 791 bzero(kp, sizeof(*kp)); 792 793 kp->ki_structsize = sizeof(*kp); 794 kp->ki_paddr = p; 795 kp->ki_addr =/* p->p_addr; */0; /* XXX */ 796 kp->ki_args = p->p_args; 797 kp->ki_textvp = p->p_textvp; 798 #ifdef KTRACE 799 kp->ki_tracep = p->p_tracevp; 800 kp->ki_traceflag = p->p_traceflag; 801 #endif 802 kp->ki_fd = p->p_fd; 803 kp->ki_vmspace = p->p_vmspace; 804 kp->ki_flag = p->p_flag; 805 kp->ki_flag2 = p->p_flag2; 806 cred = p->p_ucred; 807 if (cred) { 808 kp->ki_uid = cred->cr_uid; 809 kp->ki_ruid = cred->cr_ruid; 810 kp->ki_svuid = cred->cr_svuid; 811 kp->ki_cr_flags = 0; 812 if (cred->cr_flags & CRED_FLAG_CAPMODE) 813 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE; 814 /* XXX bde doesn't like KI_NGROUPS */ 815 if (cred->cr_ngroups > KI_NGROUPS) { 816 kp->ki_ngroups = KI_NGROUPS; 817 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 818 } else 819 kp->ki_ngroups = cred->cr_ngroups; 820 bcopy(cred->cr_groups, kp->ki_groups, 821 kp->ki_ngroups * sizeof(gid_t)); 822 kp->ki_rgid = cred->cr_rgid; 823 kp->ki_svgid = cred->cr_svgid; 824 /* If jailed(cred), emulate the old P_JAILED flag. */ 825 if (jailed(cred)) { 826 kp->ki_flag |= P_JAILED; 827 /* If inside the jail, use 0 as a jail ID. */ 828 if (cred->cr_prison != curthread->td_ucred->cr_prison) 829 kp->ki_jid = cred->cr_prison->pr_id; 830 } 831 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name, 832 sizeof(kp->ki_loginclass)); 833 } 834 ps = p->p_sigacts; 835 if (ps) { 836 mtx_lock(&ps->ps_mtx); 837 kp->ki_sigignore = ps->ps_sigignore; 838 kp->ki_sigcatch = ps->ps_sigcatch; 839 mtx_unlock(&ps->ps_mtx); 840 } 841 if (p->p_state != PRS_NEW && 842 p->p_state != PRS_ZOMBIE && 843 p->p_vmspace != NULL) { 844 struct vmspace *vm = p->p_vmspace; 845 846 kp->ki_size = vm->vm_map.size; 847 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 848 FOREACH_THREAD_IN_PROC(p, td0) { 849 if (!TD_IS_SWAPPED(td0)) 850 kp->ki_rssize += td0->td_kstack_pages; 851 } 852 kp->ki_swrss = vm->vm_swrss; 853 kp->ki_tsize = vm->vm_tsize; 854 kp->ki_dsize = vm->vm_dsize; 855 kp->ki_ssize = vm->vm_ssize; 856 } else if (p->p_state == PRS_ZOMBIE) 857 kp->ki_stat = SZOMB; 858 if (kp->ki_flag & P_INMEM) 859 kp->ki_sflag = PS_INMEM; 860 else 861 kp->ki_sflag = 0; 862 /* Calculate legacy swtime as seconds since 'swtick'. */ 863 kp->ki_swtime = (ticks - p->p_swtick) / hz; 864 kp->ki_pid = p->p_pid; 865 kp->ki_nice = p->p_nice; 866 kp->ki_fibnum = p->p_fibnum; 867 kp->ki_start = p->p_stats->p_start; 868 timevaladd(&kp->ki_start, &boottime); 869 PROC_SLOCK(p); 870 rufetch(p, &kp->ki_rusage); 871 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 872 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 873 PROC_SUNLOCK(p); 874 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 875 /* Some callers want child times in a single value. */ 876 kp->ki_childtime = kp->ki_childstime; 877 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 878 879 FOREACH_THREAD_IN_PROC(p, td0) 880 kp->ki_cow += td0->td_cow; 881 882 tp = NULL; 883 if (p->p_pgrp) { 884 kp->ki_pgid = p->p_pgrp->pg_id; 885 kp->ki_jobc = p->p_pgrp->pg_jobc; 886 sp = p->p_pgrp->pg_session; 887 888 if (sp != NULL) { 889 kp->ki_sid = sp->s_sid; 890 SESS_LOCK(sp); 891 strlcpy(kp->ki_login, sp->s_login, 892 sizeof(kp->ki_login)); 893 if (sp->s_ttyvp) 894 kp->ki_kiflag |= KI_CTTY; 895 if (SESS_LEADER(p)) 896 kp->ki_kiflag |= KI_SLEADER; 897 /* XXX proctree_lock */ 898 tp = sp->s_ttyp; 899 SESS_UNLOCK(sp); 900 } 901 } 902 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 903 kp->ki_tdev = tty_udev(tp); 904 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 905 if (tp->t_session) 906 kp->ki_tsid = tp->t_session->s_sid; 907 } else 908 kp->ki_tdev = NODEV; 909 if (p->p_comm[0] != '\0') 910 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 911 if (p->p_sysent && p->p_sysent->sv_name != NULL && 912 p->p_sysent->sv_name[0] != '\0') 913 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 914 kp->ki_siglist = p->p_siglist; 915 kp->ki_xstat = p->p_xstat; 916 kp->ki_acflag = p->p_acflag; 917 kp->ki_lock = p->p_lock; 918 if (p->p_pptr) 919 kp->ki_ppid = p->p_pptr->p_pid; 920 } 921 922 /* 923 * Fill in information that is thread specific. Must be called with 924 * target process locked. If 'preferthread' is set, overwrite certain 925 * process-related fields that are maintained for both threads and 926 * processes. 927 */ 928 static void 929 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 930 { 931 struct proc *p; 932 933 p = td->td_proc; 934 kp->ki_tdaddr = td; 935 PROC_LOCK_ASSERT(p, MA_OWNED); 936 937 if (preferthread) 938 PROC_SLOCK(p); 939 thread_lock(td); 940 if (td->td_wmesg != NULL) 941 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 942 else 943 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 944 strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)); 945 if (TD_ON_LOCK(td)) { 946 kp->ki_kiflag |= KI_LOCKBLOCK; 947 strlcpy(kp->ki_lockname, td->td_lockname, 948 sizeof(kp->ki_lockname)); 949 } else { 950 kp->ki_kiflag &= ~KI_LOCKBLOCK; 951 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 952 } 953 954 if (p->p_state == PRS_NORMAL) { /* approximate. */ 955 if (TD_ON_RUNQ(td) || 956 TD_CAN_RUN(td) || 957 TD_IS_RUNNING(td)) { 958 kp->ki_stat = SRUN; 959 } else if (P_SHOULDSTOP(p)) { 960 kp->ki_stat = SSTOP; 961 } else if (TD_IS_SLEEPING(td)) { 962 kp->ki_stat = SSLEEP; 963 } else if (TD_ON_LOCK(td)) { 964 kp->ki_stat = SLOCK; 965 } else { 966 kp->ki_stat = SWAIT; 967 } 968 } else if (p->p_state == PRS_ZOMBIE) { 969 kp->ki_stat = SZOMB; 970 } else { 971 kp->ki_stat = SIDL; 972 } 973 974 /* Things in the thread */ 975 kp->ki_wchan = td->td_wchan; 976 kp->ki_pri.pri_level = td->td_priority; 977 kp->ki_pri.pri_native = td->td_base_pri; 978 kp->ki_lastcpu = td->td_lastcpu; 979 kp->ki_oncpu = td->td_oncpu; 980 kp->ki_tdflags = td->td_flags; 981 kp->ki_tid = td->td_tid; 982 kp->ki_numthreads = p->p_numthreads; 983 kp->ki_pcb = td->td_pcb; 984 kp->ki_kstack = (void *)td->td_kstack; 985 kp->ki_slptime = (ticks - td->td_slptick) / hz; 986 kp->ki_pri.pri_class = td->td_pri_class; 987 kp->ki_pri.pri_user = td->td_user_pri; 988 989 if (preferthread) { 990 rufetchtd(td, &kp->ki_rusage); 991 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 992 kp->ki_pctcpu = sched_pctcpu(td); 993 kp->ki_estcpu = td->td_estcpu; 994 kp->ki_cow = td->td_cow; 995 } 996 997 /* We can't get this anymore but ps etc never used it anyway. */ 998 kp->ki_rqindex = 0; 999 1000 if (preferthread) 1001 kp->ki_siglist = td->td_siglist; 1002 kp->ki_sigmask = td->td_sigmask; 1003 thread_unlock(td); 1004 if (preferthread) 1005 PROC_SUNLOCK(p); 1006 } 1007 1008 /* 1009 * Fill in a kinfo_proc structure for the specified process. 1010 * Must be called with the target process locked. 1011 */ 1012 void 1013 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 1014 { 1015 1016 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1017 1018 fill_kinfo_proc_only(p, kp); 1019 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 1020 fill_kinfo_aggregate(p, kp); 1021 } 1022 1023 struct pstats * 1024 pstats_alloc(void) 1025 { 1026 1027 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 1028 } 1029 1030 /* 1031 * Copy parts of p_stats; zero the rest of p_stats (statistics). 1032 */ 1033 void 1034 pstats_fork(struct pstats *src, struct pstats *dst) 1035 { 1036 1037 bzero(&dst->pstat_startzero, 1038 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 1039 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 1040 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 1041 } 1042 1043 void 1044 pstats_free(struct pstats *ps) 1045 { 1046 1047 free(ps, M_SUBPROC); 1048 } 1049 1050 static struct proc * 1051 zpfind_locked(pid_t pid) 1052 { 1053 struct proc *p; 1054 1055 sx_assert(&allproc_lock, SX_LOCKED); 1056 LIST_FOREACH(p, &zombproc, p_list) { 1057 if (p->p_pid == pid) { 1058 PROC_LOCK(p); 1059 break; 1060 } 1061 } 1062 return (p); 1063 } 1064 1065 /* 1066 * Locate a zombie process by number 1067 */ 1068 struct proc * 1069 zpfind(pid_t pid) 1070 { 1071 struct proc *p; 1072 1073 sx_slock(&allproc_lock); 1074 p = zpfind_locked(pid); 1075 sx_sunlock(&allproc_lock); 1076 return (p); 1077 } 1078 1079 #ifdef COMPAT_FREEBSD32 1080 1081 /* 1082 * This function is typically used to copy out the kernel address, so 1083 * it can be replaced by assignment of zero. 1084 */ 1085 static inline uint32_t 1086 ptr32_trim(void *ptr) 1087 { 1088 uintptr_t uptr; 1089 1090 uptr = (uintptr_t)ptr; 1091 return ((uptr > UINT_MAX) ? 0 : uptr); 1092 } 1093 1094 #define PTRTRIM_CP(src,dst,fld) \ 1095 do { (dst).fld = ptr32_trim((src).fld); } while (0) 1096 1097 static void 1098 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 1099 { 1100 int i; 1101 1102 bzero(ki32, sizeof(struct kinfo_proc32)); 1103 ki32->ki_structsize = sizeof(struct kinfo_proc32); 1104 CP(*ki, *ki32, ki_layout); 1105 PTRTRIM_CP(*ki, *ki32, ki_args); 1106 PTRTRIM_CP(*ki, *ki32, ki_paddr); 1107 PTRTRIM_CP(*ki, *ki32, ki_addr); 1108 PTRTRIM_CP(*ki, *ki32, ki_tracep); 1109 PTRTRIM_CP(*ki, *ki32, ki_textvp); 1110 PTRTRIM_CP(*ki, *ki32, ki_fd); 1111 PTRTRIM_CP(*ki, *ki32, ki_vmspace); 1112 PTRTRIM_CP(*ki, *ki32, ki_wchan); 1113 CP(*ki, *ki32, ki_pid); 1114 CP(*ki, *ki32, ki_ppid); 1115 CP(*ki, *ki32, ki_pgid); 1116 CP(*ki, *ki32, ki_tpgid); 1117 CP(*ki, *ki32, ki_sid); 1118 CP(*ki, *ki32, ki_tsid); 1119 CP(*ki, *ki32, ki_jobc); 1120 CP(*ki, *ki32, ki_tdev); 1121 CP(*ki, *ki32, ki_siglist); 1122 CP(*ki, *ki32, ki_sigmask); 1123 CP(*ki, *ki32, ki_sigignore); 1124 CP(*ki, *ki32, ki_sigcatch); 1125 CP(*ki, *ki32, ki_uid); 1126 CP(*ki, *ki32, ki_ruid); 1127 CP(*ki, *ki32, ki_svuid); 1128 CP(*ki, *ki32, ki_rgid); 1129 CP(*ki, *ki32, ki_svgid); 1130 CP(*ki, *ki32, ki_ngroups); 1131 for (i = 0; i < KI_NGROUPS; i++) 1132 CP(*ki, *ki32, ki_groups[i]); 1133 CP(*ki, *ki32, ki_size); 1134 CP(*ki, *ki32, ki_rssize); 1135 CP(*ki, *ki32, ki_swrss); 1136 CP(*ki, *ki32, ki_tsize); 1137 CP(*ki, *ki32, ki_dsize); 1138 CP(*ki, *ki32, ki_ssize); 1139 CP(*ki, *ki32, ki_xstat); 1140 CP(*ki, *ki32, ki_acflag); 1141 CP(*ki, *ki32, ki_pctcpu); 1142 CP(*ki, *ki32, ki_estcpu); 1143 CP(*ki, *ki32, ki_slptime); 1144 CP(*ki, *ki32, ki_swtime); 1145 CP(*ki, *ki32, ki_cow); 1146 CP(*ki, *ki32, ki_runtime); 1147 TV_CP(*ki, *ki32, ki_start); 1148 TV_CP(*ki, *ki32, ki_childtime); 1149 CP(*ki, *ki32, ki_flag); 1150 CP(*ki, *ki32, ki_kiflag); 1151 CP(*ki, *ki32, ki_traceflag); 1152 CP(*ki, *ki32, ki_stat); 1153 CP(*ki, *ki32, ki_nice); 1154 CP(*ki, *ki32, ki_lock); 1155 CP(*ki, *ki32, ki_rqindex); 1156 CP(*ki, *ki32, ki_oncpu); 1157 CP(*ki, *ki32, ki_lastcpu); 1158 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1); 1159 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 1160 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 1161 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 1162 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 1163 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 1164 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); 1165 CP(*ki, *ki32, ki_flag2); 1166 CP(*ki, *ki32, ki_fibnum); 1167 CP(*ki, *ki32, ki_cr_flags); 1168 CP(*ki, *ki32, ki_jid); 1169 CP(*ki, *ki32, ki_numthreads); 1170 CP(*ki, *ki32, ki_tid); 1171 CP(*ki, *ki32, ki_pri); 1172 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 1173 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 1174 PTRTRIM_CP(*ki, *ki32, ki_pcb); 1175 PTRTRIM_CP(*ki, *ki32, ki_kstack); 1176 PTRTRIM_CP(*ki, *ki32, ki_udata); 1177 CP(*ki, *ki32, ki_sflag); 1178 CP(*ki, *ki32, ki_tdflags); 1179 } 1180 #endif 1181 1182 int 1183 kern_proc_out(struct proc *p, struct sbuf *sb, int flags) 1184 { 1185 struct thread *td; 1186 struct kinfo_proc ki; 1187 #ifdef COMPAT_FREEBSD32 1188 struct kinfo_proc32 ki32; 1189 #endif 1190 int error; 1191 1192 PROC_LOCK_ASSERT(p, MA_OWNED); 1193 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1194 1195 error = 0; 1196 fill_kinfo_proc(p, &ki); 1197 if ((flags & KERN_PROC_NOTHREADS) != 0) { 1198 #ifdef COMPAT_FREEBSD32 1199 if ((flags & KERN_PROC_MASK32) != 0) { 1200 freebsd32_kinfo_proc_out(&ki, &ki32); 1201 error = sbuf_bcat(sb, &ki32, sizeof(ki32)); 1202 } else 1203 #endif 1204 error = sbuf_bcat(sb, &ki, sizeof(ki)); 1205 } else { 1206 FOREACH_THREAD_IN_PROC(p, td) { 1207 fill_kinfo_thread(td, &ki, 1); 1208 #ifdef COMPAT_FREEBSD32 1209 if ((flags & KERN_PROC_MASK32) != 0) { 1210 freebsd32_kinfo_proc_out(&ki, &ki32); 1211 error = sbuf_bcat(sb, &ki32, sizeof(ki32)); 1212 } else 1213 #endif 1214 error = sbuf_bcat(sb, &ki, sizeof(ki)); 1215 if (error) 1216 break; 1217 } 1218 } 1219 PROC_UNLOCK(p); 1220 return (error); 1221 } 1222 1223 static int 1224 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags, 1225 int doingzomb) 1226 { 1227 struct sbuf sb; 1228 struct kinfo_proc ki; 1229 struct proc *np; 1230 int error, error2; 1231 pid_t pid; 1232 1233 pid = p->p_pid; 1234 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req); 1235 error = kern_proc_out(p, &sb, flags); 1236 error2 = sbuf_finish(&sb); 1237 sbuf_delete(&sb); 1238 if (error != 0) 1239 return (error); 1240 else if (error2 != 0) 1241 return (error2); 1242 if (doingzomb) 1243 np = zpfind(pid); 1244 else { 1245 if (pid == 0) 1246 return (0); 1247 np = pfind(pid); 1248 } 1249 if (np == NULL) 1250 return (ESRCH); 1251 if (np != p) { 1252 PROC_UNLOCK(np); 1253 return (ESRCH); 1254 } 1255 PROC_UNLOCK(np); 1256 return (0); 1257 } 1258 1259 static int 1260 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1261 { 1262 int *name = (int *)arg1; 1263 u_int namelen = arg2; 1264 struct proc *p; 1265 int flags, doingzomb, oid_number; 1266 int error = 0; 1267 1268 oid_number = oidp->oid_number; 1269 if (oid_number != KERN_PROC_ALL && 1270 (oid_number & KERN_PROC_INC_THREAD) == 0) 1271 flags = KERN_PROC_NOTHREADS; 1272 else { 1273 flags = 0; 1274 oid_number &= ~KERN_PROC_INC_THREAD; 1275 } 1276 #ifdef COMPAT_FREEBSD32 1277 if (req->flags & SCTL_MASK32) 1278 flags |= KERN_PROC_MASK32; 1279 #endif 1280 if (oid_number == KERN_PROC_PID) { 1281 if (namelen != 1) 1282 return (EINVAL); 1283 error = sysctl_wire_old_buffer(req, 0); 1284 if (error) 1285 return (error); 1286 error = pget((pid_t)name[0], PGET_CANSEE, &p); 1287 if (error != 0) 1288 return (error); 1289 error = sysctl_out_proc(p, req, flags, 0); 1290 return (error); 1291 } 1292 1293 switch (oid_number) { 1294 case KERN_PROC_ALL: 1295 if (namelen != 0) 1296 return (EINVAL); 1297 break; 1298 case KERN_PROC_PROC: 1299 if (namelen != 0 && namelen != 1) 1300 return (EINVAL); 1301 break; 1302 default: 1303 if (namelen != 1) 1304 return (EINVAL); 1305 break; 1306 } 1307 1308 if (!req->oldptr) { 1309 /* overestimate by 5 procs */ 1310 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1311 if (error) 1312 return (error); 1313 } 1314 error = sysctl_wire_old_buffer(req, 0); 1315 if (error != 0) 1316 return (error); 1317 sx_slock(&allproc_lock); 1318 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1319 if (!doingzomb) 1320 p = LIST_FIRST(&allproc); 1321 else 1322 p = LIST_FIRST(&zombproc); 1323 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1324 /* 1325 * Skip embryonic processes. 1326 */ 1327 PROC_LOCK(p); 1328 if (p->p_state == PRS_NEW) { 1329 PROC_UNLOCK(p); 1330 continue; 1331 } 1332 KASSERT(p->p_ucred != NULL, 1333 ("process credential is NULL for non-NEW proc")); 1334 /* 1335 * Show a user only appropriate processes. 1336 */ 1337 if (p_cansee(curthread, p)) { 1338 PROC_UNLOCK(p); 1339 continue; 1340 } 1341 /* 1342 * TODO - make more efficient (see notes below). 1343 * do by session. 1344 */ 1345 switch (oid_number) { 1346 1347 case KERN_PROC_GID: 1348 if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1349 PROC_UNLOCK(p); 1350 continue; 1351 } 1352 break; 1353 1354 case KERN_PROC_PGRP: 1355 /* could do this by traversing pgrp */ 1356 if (p->p_pgrp == NULL || 1357 p->p_pgrp->pg_id != (pid_t)name[0]) { 1358 PROC_UNLOCK(p); 1359 continue; 1360 } 1361 break; 1362 1363 case KERN_PROC_RGID: 1364 if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1365 PROC_UNLOCK(p); 1366 continue; 1367 } 1368 break; 1369 1370 case KERN_PROC_SESSION: 1371 if (p->p_session == NULL || 1372 p->p_session->s_sid != (pid_t)name[0]) { 1373 PROC_UNLOCK(p); 1374 continue; 1375 } 1376 break; 1377 1378 case KERN_PROC_TTY: 1379 if ((p->p_flag & P_CONTROLT) == 0 || 1380 p->p_session == NULL) { 1381 PROC_UNLOCK(p); 1382 continue; 1383 } 1384 /* XXX proctree_lock */ 1385 SESS_LOCK(p->p_session); 1386 if (p->p_session->s_ttyp == NULL || 1387 tty_udev(p->p_session->s_ttyp) != 1388 (dev_t)name[0]) { 1389 SESS_UNLOCK(p->p_session); 1390 PROC_UNLOCK(p); 1391 continue; 1392 } 1393 SESS_UNLOCK(p->p_session); 1394 break; 1395 1396 case KERN_PROC_UID: 1397 if (p->p_ucred->cr_uid != (uid_t)name[0]) { 1398 PROC_UNLOCK(p); 1399 continue; 1400 } 1401 break; 1402 1403 case KERN_PROC_RUID: 1404 if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 1405 PROC_UNLOCK(p); 1406 continue; 1407 } 1408 break; 1409 1410 case KERN_PROC_PROC: 1411 break; 1412 1413 default: 1414 break; 1415 1416 } 1417 1418 error = sysctl_out_proc(p, req, flags, doingzomb); 1419 if (error) { 1420 sx_sunlock(&allproc_lock); 1421 return (error); 1422 } 1423 } 1424 } 1425 sx_sunlock(&allproc_lock); 1426 return (0); 1427 } 1428 1429 struct pargs * 1430 pargs_alloc(int len) 1431 { 1432 struct pargs *pa; 1433 1434 pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1435 M_WAITOK); 1436 refcount_init(&pa->ar_ref, 1); 1437 pa->ar_length = len; 1438 return (pa); 1439 } 1440 1441 static void 1442 pargs_free(struct pargs *pa) 1443 { 1444 1445 free(pa, M_PARGS); 1446 } 1447 1448 void 1449 pargs_hold(struct pargs *pa) 1450 { 1451 1452 if (pa == NULL) 1453 return; 1454 refcount_acquire(&pa->ar_ref); 1455 } 1456 1457 void 1458 pargs_drop(struct pargs *pa) 1459 { 1460 1461 if (pa == NULL) 1462 return; 1463 if (refcount_release(&pa->ar_ref)) 1464 pargs_free(pa); 1465 } 1466 1467 static int 1468 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf, 1469 size_t len) 1470 { 1471 struct iovec iov; 1472 struct uio uio; 1473 1474 iov.iov_base = (caddr_t)buf; 1475 iov.iov_len = len; 1476 uio.uio_iov = &iov; 1477 uio.uio_iovcnt = 1; 1478 uio.uio_offset = offset; 1479 uio.uio_resid = (ssize_t)len; 1480 uio.uio_segflg = UIO_SYSSPACE; 1481 uio.uio_rw = UIO_READ; 1482 uio.uio_td = td; 1483 1484 return (proc_rwmem(p, &uio)); 1485 } 1486 1487 static int 1488 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf, 1489 size_t len) 1490 { 1491 size_t i; 1492 int error; 1493 1494 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len); 1495 /* 1496 * Reading the chunk may validly return EFAULT if the string is shorter 1497 * than the chunk and is aligned at the end of the page, assuming the 1498 * next page is not mapped. So if EFAULT is returned do a fallback to 1499 * one byte read loop. 1500 */ 1501 if (error == EFAULT) { 1502 for (i = 0; i < len; i++, buf++, sptr++) { 1503 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1); 1504 if (error != 0) 1505 return (error); 1506 if (*buf == '\0') 1507 break; 1508 } 1509 error = 0; 1510 } 1511 return (error); 1512 } 1513 1514 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */ 1515 1516 enum proc_vector_type { 1517 PROC_ARG, 1518 PROC_ENV, 1519 PROC_AUX, 1520 }; 1521 1522 #ifdef COMPAT_FREEBSD32 1523 static int 1524 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp, 1525 size_t *vsizep, enum proc_vector_type type) 1526 { 1527 struct freebsd32_ps_strings pss; 1528 Elf32_Auxinfo aux; 1529 vm_offset_t vptr, ptr; 1530 uint32_t *proc_vector32; 1531 char **proc_vector; 1532 size_t vsize, size; 1533 int i, error; 1534 1535 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1536 &pss, sizeof(pss)); 1537 if (error != 0) 1538 return (error); 1539 switch (type) { 1540 case PROC_ARG: 1541 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr); 1542 vsize = pss.ps_nargvstr; 1543 if (vsize > ARG_MAX) 1544 return (ENOEXEC); 1545 size = vsize * sizeof(int32_t); 1546 break; 1547 case PROC_ENV: 1548 vptr = (vm_offset_t)PTRIN(pss.ps_envstr); 1549 vsize = pss.ps_nenvstr; 1550 if (vsize > ARG_MAX) 1551 return (ENOEXEC); 1552 size = vsize * sizeof(int32_t); 1553 break; 1554 case PROC_AUX: 1555 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) + 1556 (pss.ps_nenvstr + 1) * sizeof(int32_t); 1557 if (vptr % 4 != 0) 1558 return (ENOEXEC); 1559 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1560 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1561 if (error != 0) 1562 return (error); 1563 if (aux.a_type == AT_NULL) 1564 break; 1565 ptr += sizeof(aux); 1566 } 1567 if (aux.a_type != AT_NULL) 1568 return (ENOEXEC); 1569 vsize = i + 1; 1570 size = vsize * sizeof(aux); 1571 break; 1572 default: 1573 KASSERT(0, ("Wrong proc vector type: %d", type)); 1574 return (EINVAL); 1575 } 1576 proc_vector32 = malloc(size, M_TEMP, M_WAITOK); 1577 error = proc_read_mem(td, p, vptr, proc_vector32, size); 1578 if (error != 0) 1579 goto done; 1580 if (type == PROC_AUX) { 1581 *proc_vectorp = (char **)proc_vector32; 1582 *vsizep = vsize; 1583 return (0); 1584 } 1585 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK); 1586 for (i = 0; i < (int)vsize; i++) 1587 proc_vector[i] = PTRIN(proc_vector32[i]); 1588 *proc_vectorp = proc_vector; 1589 *vsizep = vsize; 1590 done: 1591 free(proc_vector32, M_TEMP); 1592 return (error); 1593 } 1594 #endif 1595 1596 static int 1597 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp, 1598 size_t *vsizep, enum proc_vector_type type) 1599 { 1600 struct ps_strings pss; 1601 Elf_Auxinfo aux; 1602 vm_offset_t vptr, ptr; 1603 char **proc_vector; 1604 size_t vsize, size; 1605 int error, i; 1606 1607 #ifdef COMPAT_FREEBSD32 1608 if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1609 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type)); 1610 #endif 1611 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1612 &pss, sizeof(pss)); 1613 if (error != 0) 1614 return (error); 1615 switch (type) { 1616 case PROC_ARG: 1617 vptr = (vm_offset_t)pss.ps_argvstr; 1618 vsize = pss.ps_nargvstr; 1619 if (vsize > ARG_MAX) 1620 return (ENOEXEC); 1621 size = vsize * sizeof(char *); 1622 break; 1623 case PROC_ENV: 1624 vptr = (vm_offset_t)pss.ps_envstr; 1625 vsize = pss.ps_nenvstr; 1626 if (vsize > ARG_MAX) 1627 return (ENOEXEC); 1628 size = vsize * sizeof(char *); 1629 break; 1630 case PROC_AUX: 1631 /* 1632 * The aux array is just above env array on the stack. Check 1633 * that the address is naturally aligned. 1634 */ 1635 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1) 1636 * sizeof(char *); 1637 #if __ELF_WORD_SIZE == 64 1638 if (vptr % sizeof(uint64_t) != 0) 1639 #else 1640 if (vptr % sizeof(uint32_t) != 0) 1641 #endif 1642 return (ENOEXEC); 1643 /* 1644 * We count the array size reading the aux vectors from the 1645 * stack until AT_NULL vector is returned. So (to keep the code 1646 * simple) we read the process stack twice: the first time here 1647 * to find the size and the second time when copying the vectors 1648 * to the allocated proc_vector. 1649 */ 1650 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1651 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1652 if (error != 0) 1653 return (error); 1654 if (aux.a_type == AT_NULL) 1655 break; 1656 ptr += sizeof(aux); 1657 } 1658 /* 1659 * If the PROC_AUXV_MAX entries are iterated over, and we have 1660 * not reached AT_NULL, it is most likely we are reading wrong 1661 * data: either the process doesn't have auxv array or data has 1662 * been modified. Return the error in this case. 1663 */ 1664 if (aux.a_type != AT_NULL) 1665 return (ENOEXEC); 1666 vsize = i + 1; 1667 size = vsize * sizeof(aux); 1668 break; 1669 default: 1670 KASSERT(0, ("Wrong proc vector type: %d", type)); 1671 return (EINVAL); /* In case we are built without INVARIANTS. */ 1672 } 1673 proc_vector = malloc(size, M_TEMP, M_WAITOK); 1674 if (proc_vector == NULL) 1675 return (ENOMEM); 1676 error = proc_read_mem(td, p, vptr, proc_vector, size); 1677 if (error != 0) { 1678 free(proc_vector, M_TEMP); 1679 return (error); 1680 } 1681 *proc_vectorp = proc_vector; 1682 *vsizep = vsize; 1683 1684 return (0); 1685 } 1686 1687 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */ 1688 1689 static int 1690 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb, 1691 enum proc_vector_type type) 1692 { 1693 size_t done, len, nchr, vsize; 1694 int error, i; 1695 char **proc_vector, *sptr; 1696 char pss_string[GET_PS_STRINGS_CHUNK_SZ]; 1697 1698 PROC_ASSERT_HELD(p); 1699 1700 /* 1701 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes. 1702 */ 1703 nchr = 2 * (PATH_MAX + ARG_MAX); 1704 1705 error = get_proc_vector(td, p, &proc_vector, &vsize, type); 1706 if (error != 0) 1707 return (error); 1708 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) { 1709 /* 1710 * The program may have scribbled into its argv array, e.g. to 1711 * remove some arguments. If that has happened, break out 1712 * before trying to read from NULL. 1713 */ 1714 if (proc_vector[i] == NULL) 1715 break; 1716 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) { 1717 error = proc_read_string(td, p, sptr, pss_string, 1718 sizeof(pss_string)); 1719 if (error != 0) 1720 goto done; 1721 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ); 1722 if (done + len >= nchr) 1723 len = nchr - done - 1; 1724 sbuf_bcat(sb, pss_string, len); 1725 if (len != GET_PS_STRINGS_CHUNK_SZ) 1726 break; 1727 done += GET_PS_STRINGS_CHUNK_SZ; 1728 } 1729 sbuf_bcat(sb, "", 1); 1730 done += len + 1; 1731 } 1732 done: 1733 free(proc_vector, M_TEMP); 1734 return (error); 1735 } 1736 1737 int 1738 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb) 1739 { 1740 1741 return (get_ps_strings(curthread, p, sb, PROC_ARG)); 1742 } 1743 1744 int 1745 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb) 1746 { 1747 1748 return (get_ps_strings(curthread, p, sb, PROC_ENV)); 1749 } 1750 1751 int 1752 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb) 1753 { 1754 size_t vsize, size; 1755 char **auxv; 1756 int error; 1757 1758 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX); 1759 if (error == 0) { 1760 #ifdef COMPAT_FREEBSD32 1761 if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1762 size = vsize * sizeof(Elf32_Auxinfo); 1763 else 1764 #endif 1765 size = vsize * sizeof(Elf_Auxinfo); 1766 error = sbuf_bcat(sb, auxv, size); 1767 free(auxv, M_TEMP); 1768 } 1769 return (error); 1770 } 1771 1772 /* 1773 * This sysctl allows a process to retrieve the argument list or process 1774 * title for another process without groping around in the address space 1775 * of the other process. It also allow a process to set its own "process 1776 * title to a string of its own choice. 1777 */ 1778 static int 1779 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1780 { 1781 int *name = (int *)arg1; 1782 u_int namelen = arg2; 1783 struct pargs *newpa, *pa; 1784 struct proc *p; 1785 struct sbuf sb; 1786 int flags, error = 0, error2; 1787 1788 if (namelen != 1) 1789 return (EINVAL); 1790 1791 flags = PGET_CANSEE; 1792 if (req->newptr != NULL) 1793 flags |= PGET_ISCURRENT; 1794 error = pget((pid_t)name[0], flags, &p); 1795 if (error) 1796 return (error); 1797 1798 pa = p->p_args; 1799 if (pa != NULL) { 1800 pargs_hold(pa); 1801 PROC_UNLOCK(p); 1802 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1803 pargs_drop(pa); 1804 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) { 1805 _PHOLD(p); 1806 PROC_UNLOCK(p); 1807 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1808 error = proc_getargv(curthread, p, &sb); 1809 error2 = sbuf_finish(&sb); 1810 PRELE(p); 1811 sbuf_delete(&sb); 1812 if (error == 0 && error2 != 0) 1813 error = error2; 1814 } else { 1815 PROC_UNLOCK(p); 1816 } 1817 if (error != 0 || req->newptr == NULL) 1818 return (error); 1819 1820 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1821 return (ENOMEM); 1822 newpa = pargs_alloc(req->newlen); 1823 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1824 if (error != 0) { 1825 pargs_free(newpa); 1826 return (error); 1827 } 1828 PROC_LOCK(p); 1829 pa = p->p_args; 1830 p->p_args = newpa; 1831 PROC_UNLOCK(p); 1832 pargs_drop(pa); 1833 return (0); 1834 } 1835 1836 /* 1837 * This sysctl allows a process to retrieve environment of another process. 1838 */ 1839 static int 1840 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS) 1841 { 1842 int *name = (int *)arg1; 1843 u_int namelen = arg2; 1844 struct proc *p; 1845 struct sbuf sb; 1846 int error, error2; 1847 1848 if (namelen != 1) 1849 return (EINVAL); 1850 1851 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1852 if (error != 0) 1853 return (error); 1854 if ((p->p_flag & P_SYSTEM) != 0) { 1855 PRELE(p); 1856 return (0); 1857 } 1858 1859 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1860 error = proc_getenvv(curthread, p, &sb); 1861 error2 = sbuf_finish(&sb); 1862 PRELE(p); 1863 sbuf_delete(&sb); 1864 return (error != 0 ? error : error2); 1865 } 1866 1867 /* 1868 * This sysctl allows a process to retrieve ELF auxiliary vector of 1869 * another process. 1870 */ 1871 static int 1872 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS) 1873 { 1874 int *name = (int *)arg1; 1875 u_int namelen = arg2; 1876 struct proc *p; 1877 struct sbuf sb; 1878 int error, error2; 1879 1880 if (namelen != 1) 1881 return (EINVAL); 1882 1883 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1884 if (error != 0) 1885 return (error); 1886 if ((p->p_flag & P_SYSTEM) != 0) { 1887 PRELE(p); 1888 return (0); 1889 } 1890 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1891 error = proc_getauxv(curthread, p, &sb); 1892 error2 = sbuf_finish(&sb); 1893 PRELE(p); 1894 sbuf_delete(&sb); 1895 return (error != 0 ? error : error2); 1896 } 1897 1898 /* 1899 * This sysctl allows a process to retrieve the path of the executable for 1900 * itself or another process. 1901 */ 1902 static int 1903 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1904 { 1905 pid_t *pidp = (pid_t *)arg1; 1906 unsigned int arglen = arg2; 1907 struct proc *p; 1908 struct vnode *vp; 1909 char *retbuf, *freebuf; 1910 int error; 1911 1912 if (arglen != 1) 1913 return (EINVAL); 1914 if (*pidp == -1) { /* -1 means this process */ 1915 p = req->td->td_proc; 1916 } else { 1917 error = pget(*pidp, PGET_CANSEE, &p); 1918 if (error != 0) 1919 return (error); 1920 } 1921 1922 vp = p->p_textvp; 1923 if (vp == NULL) { 1924 if (*pidp != -1) 1925 PROC_UNLOCK(p); 1926 return (0); 1927 } 1928 vref(vp); 1929 if (*pidp != -1) 1930 PROC_UNLOCK(p); 1931 error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1932 vrele(vp); 1933 if (error) 1934 return (error); 1935 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1936 free(freebuf, M_TEMP); 1937 return (error); 1938 } 1939 1940 static int 1941 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1942 { 1943 struct proc *p; 1944 char *sv_name; 1945 int *name; 1946 int namelen; 1947 int error; 1948 1949 namelen = arg2; 1950 if (namelen != 1) 1951 return (EINVAL); 1952 1953 name = (int *)arg1; 1954 error = pget((pid_t)name[0], PGET_CANSEE, &p); 1955 if (error != 0) 1956 return (error); 1957 sv_name = p->p_sysent->sv_name; 1958 PROC_UNLOCK(p); 1959 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1960 } 1961 1962 #ifdef KINFO_OVMENTRY_SIZE 1963 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 1964 #endif 1965 1966 #ifdef COMPAT_FREEBSD7 1967 static int 1968 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 1969 { 1970 vm_map_entry_t entry, tmp_entry; 1971 unsigned int last_timestamp; 1972 char *fullpath, *freepath; 1973 struct kinfo_ovmentry *kve; 1974 struct vattr va; 1975 struct ucred *cred; 1976 int error, *name; 1977 struct vnode *vp; 1978 struct proc *p; 1979 vm_map_t map; 1980 struct vmspace *vm; 1981 1982 name = (int *)arg1; 1983 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1984 if (error != 0) 1985 return (error); 1986 vm = vmspace_acquire_ref(p); 1987 if (vm == NULL) { 1988 PRELE(p); 1989 return (ESRCH); 1990 } 1991 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1992 1993 map = &vm->vm_map; 1994 vm_map_lock_read(map); 1995 for (entry = map->header.next; entry != &map->header; 1996 entry = entry->next) { 1997 vm_object_t obj, tobj, lobj; 1998 vm_offset_t addr; 1999 2000 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2001 continue; 2002 2003 bzero(kve, sizeof(*kve)); 2004 kve->kve_structsize = sizeof(*kve); 2005 2006 kve->kve_private_resident = 0; 2007 obj = entry->object.vm_object; 2008 if (obj != NULL) { 2009 VM_OBJECT_RLOCK(obj); 2010 if (obj->shadow_count == 1) 2011 kve->kve_private_resident = 2012 obj->resident_page_count; 2013 } 2014 kve->kve_resident = 0; 2015 addr = entry->start; 2016 while (addr < entry->end) { 2017 if (pmap_extract(map->pmap, addr)) 2018 kve->kve_resident++; 2019 addr += PAGE_SIZE; 2020 } 2021 2022 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 2023 if (tobj != obj) 2024 VM_OBJECT_RLOCK(tobj); 2025 if (lobj != obj) 2026 VM_OBJECT_RUNLOCK(lobj); 2027 lobj = tobj; 2028 } 2029 2030 kve->kve_start = (void*)entry->start; 2031 kve->kve_end = (void*)entry->end; 2032 kve->kve_offset = (off_t)entry->offset; 2033 2034 if (entry->protection & VM_PROT_READ) 2035 kve->kve_protection |= KVME_PROT_READ; 2036 if (entry->protection & VM_PROT_WRITE) 2037 kve->kve_protection |= KVME_PROT_WRITE; 2038 if (entry->protection & VM_PROT_EXECUTE) 2039 kve->kve_protection |= KVME_PROT_EXEC; 2040 2041 if (entry->eflags & MAP_ENTRY_COW) 2042 kve->kve_flags |= KVME_FLAG_COW; 2043 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2044 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2045 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2046 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2047 2048 last_timestamp = map->timestamp; 2049 vm_map_unlock_read(map); 2050 2051 kve->kve_fileid = 0; 2052 kve->kve_fsid = 0; 2053 freepath = NULL; 2054 fullpath = ""; 2055 if (lobj) { 2056 vp = NULL; 2057 switch (lobj->type) { 2058 case OBJT_DEFAULT: 2059 kve->kve_type = KVME_TYPE_DEFAULT; 2060 break; 2061 case OBJT_VNODE: 2062 kve->kve_type = KVME_TYPE_VNODE; 2063 vp = lobj->handle; 2064 vref(vp); 2065 break; 2066 case OBJT_SWAP: 2067 kve->kve_type = KVME_TYPE_SWAP; 2068 break; 2069 case OBJT_DEVICE: 2070 kve->kve_type = KVME_TYPE_DEVICE; 2071 break; 2072 case OBJT_PHYS: 2073 kve->kve_type = KVME_TYPE_PHYS; 2074 break; 2075 case OBJT_DEAD: 2076 kve->kve_type = KVME_TYPE_DEAD; 2077 break; 2078 case OBJT_SG: 2079 kve->kve_type = KVME_TYPE_SG; 2080 break; 2081 default: 2082 kve->kve_type = KVME_TYPE_UNKNOWN; 2083 break; 2084 } 2085 if (lobj != obj) 2086 VM_OBJECT_RUNLOCK(lobj); 2087 2088 kve->kve_ref_count = obj->ref_count; 2089 kve->kve_shadow_count = obj->shadow_count; 2090 VM_OBJECT_RUNLOCK(obj); 2091 if (vp != NULL) { 2092 vn_fullpath(curthread, vp, &fullpath, 2093 &freepath); 2094 cred = curthread->td_ucred; 2095 vn_lock(vp, LK_SHARED | LK_RETRY); 2096 if (VOP_GETATTR(vp, &va, cred) == 0) { 2097 kve->kve_fileid = va.va_fileid; 2098 kve->kve_fsid = va.va_fsid; 2099 } 2100 vput(vp); 2101 } 2102 } else { 2103 kve->kve_type = KVME_TYPE_NONE; 2104 kve->kve_ref_count = 0; 2105 kve->kve_shadow_count = 0; 2106 } 2107 2108 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2109 if (freepath != NULL) 2110 free(freepath, M_TEMP); 2111 2112 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 2113 vm_map_lock_read(map); 2114 if (error) 2115 break; 2116 if (last_timestamp != map->timestamp) { 2117 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2118 entry = tmp_entry; 2119 } 2120 } 2121 vm_map_unlock_read(map); 2122 vmspace_free(vm); 2123 PRELE(p); 2124 free(kve, M_TEMP); 2125 return (error); 2126 } 2127 #endif /* COMPAT_FREEBSD7 */ 2128 2129 #ifdef KINFO_VMENTRY_SIZE 2130 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2131 #endif 2132 2133 /* 2134 * Must be called with the process locked and will return unlocked. 2135 */ 2136 int 2137 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb) 2138 { 2139 vm_map_entry_t entry, tmp_entry; 2140 unsigned int last_timestamp; 2141 char *fullpath, *freepath; 2142 struct kinfo_vmentry *kve; 2143 struct vattr va; 2144 struct ucred *cred; 2145 int error; 2146 struct vnode *vp; 2147 struct vmspace *vm; 2148 vm_map_t map; 2149 2150 PROC_LOCK_ASSERT(p, MA_OWNED); 2151 2152 _PHOLD(p); 2153 PROC_UNLOCK(p); 2154 vm = vmspace_acquire_ref(p); 2155 if (vm == NULL) { 2156 PRELE(p); 2157 return (ESRCH); 2158 } 2159 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 2160 2161 error = 0; 2162 map = &vm->vm_map; 2163 vm_map_lock_read(map); 2164 for (entry = map->header.next; entry != &map->header; 2165 entry = entry->next) { 2166 vm_object_t obj, tobj, lobj; 2167 vm_offset_t addr; 2168 vm_paddr_t locked_pa; 2169 int mincoreinfo; 2170 2171 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2172 continue; 2173 2174 bzero(kve, sizeof(*kve)); 2175 2176 kve->kve_private_resident = 0; 2177 obj = entry->object.vm_object; 2178 if (obj != NULL) { 2179 VM_OBJECT_RLOCK(obj); 2180 if (obj->shadow_count == 1) 2181 kve->kve_private_resident = 2182 obj->resident_page_count; 2183 } 2184 kve->kve_resident = 0; 2185 addr = entry->start; 2186 while (addr < entry->end) { 2187 locked_pa = 0; 2188 mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa); 2189 if (locked_pa != 0) 2190 vm_page_unlock(PHYS_TO_VM_PAGE(locked_pa)); 2191 if (mincoreinfo & MINCORE_INCORE) 2192 kve->kve_resident++; 2193 if (mincoreinfo & MINCORE_SUPER) 2194 kve->kve_flags |= KVME_FLAG_SUPER; 2195 addr += PAGE_SIZE; 2196 } 2197 2198 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 2199 if (tobj != obj) 2200 VM_OBJECT_RLOCK(tobj); 2201 if (lobj != obj) 2202 VM_OBJECT_RUNLOCK(lobj); 2203 lobj = tobj; 2204 } 2205 2206 kve->kve_start = entry->start; 2207 kve->kve_end = entry->end; 2208 kve->kve_offset = entry->offset; 2209 2210 if (entry->protection & VM_PROT_READ) 2211 kve->kve_protection |= KVME_PROT_READ; 2212 if (entry->protection & VM_PROT_WRITE) 2213 kve->kve_protection |= KVME_PROT_WRITE; 2214 if (entry->protection & VM_PROT_EXECUTE) 2215 kve->kve_protection |= KVME_PROT_EXEC; 2216 2217 if (entry->eflags & MAP_ENTRY_COW) 2218 kve->kve_flags |= KVME_FLAG_COW; 2219 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2220 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2221 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2222 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2223 if (entry->eflags & MAP_ENTRY_GROWS_UP) 2224 kve->kve_flags |= KVME_FLAG_GROWS_UP; 2225 if (entry->eflags & MAP_ENTRY_GROWS_DOWN) 2226 kve->kve_flags |= KVME_FLAG_GROWS_DOWN; 2227 2228 last_timestamp = map->timestamp; 2229 vm_map_unlock_read(map); 2230 2231 freepath = NULL; 2232 fullpath = ""; 2233 if (lobj) { 2234 vp = NULL; 2235 switch (lobj->type) { 2236 case OBJT_DEFAULT: 2237 kve->kve_type = KVME_TYPE_DEFAULT; 2238 break; 2239 case OBJT_VNODE: 2240 kve->kve_type = KVME_TYPE_VNODE; 2241 vp = lobj->handle; 2242 vref(vp); 2243 break; 2244 case OBJT_SWAP: 2245 kve->kve_type = KVME_TYPE_SWAP; 2246 break; 2247 case OBJT_DEVICE: 2248 kve->kve_type = KVME_TYPE_DEVICE; 2249 break; 2250 case OBJT_PHYS: 2251 kve->kve_type = KVME_TYPE_PHYS; 2252 break; 2253 case OBJT_DEAD: 2254 kve->kve_type = KVME_TYPE_DEAD; 2255 break; 2256 case OBJT_SG: 2257 kve->kve_type = KVME_TYPE_SG; 2258 break; 2259 default: 2260 kve->kve_type = KVME_TYPE_UNKNOWN; 2261 break; 2262 } 2263 if (lobj != obj) 2264 VM_OBJECT_RUNLOCK(lobj); 2265 2266 kve->kve_ref_count = obj->ref_count; 2267 kve->kve_shadow_count = obj->shadow_count; 2268 VM_OBJECT_RUNLOCK(obj); 2269 if (vp != NULL) { 2270 vn_fullpath(curthread, vp, &fullpath, 2271 &freepath); 2272 kve->kve_vn_type = vntype_to_kinfo(vp->v_type); 2273 cred = curthread->td_ucred; 2274 vn_lock(vp, LK_SHARED | LK_RETRY); 2275 if (VOP_GETATTR(vp, &va, cred) == 0) { 2276 kve->kve_vn_fileid = va.va_fileid; 2277 kve->kve_vn_fsid = va.va_fsid; 2278 kve->kve_vn_mode = 2279 MAKEIMODE(va.va_type, va.va_mode); 2280 kve->kve_vn_size = va.va_size; 2281 kve->kve_vn_rdev = va.va_rdev; 2282 kve->kve_status = KF_ATTR_VALID; 2283 } 2284 vput(vp); 2285 } 2286 } else { 2287 kve->kve_type = KVME_TYPE_NONE; 2288 kve->kve_ref_count = 0; 2289 kve->kve_shadow_count = 0; 2290 } 2291 2292 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2293 if (freepath != NULL) 2294 free(freepath, M_TEMP); 2295 2296 /* Pack record size down */ 2297 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 2298 strlen(kve->kve_path) + 1; 2299 kve->kve_structsize = roundup(kve->kve_structsize, 2300 sizeof(uint64_t)); 2301 error = sbuf_bcat(sb, kve, kve->kve_structsize); 2302 vm_map_lock_read(map); 2303 if (error) 2304 break; 2305 if (last_timestamp != map->timestamp) { 2306 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2307 entry = tmp_entry; 2308 } 2309 } 2310 vm_map_unlock_read(map); 2311 vmspace_free(vm); 2312 PRELE(p); 2313 free(kve, M_TEMP); 2314 return (error); 2315 } 2316 2317 static int 2318 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 2319 { 2320 struct proc *p; 2321 struct sbuf sb; 2322 int error, error2, *name; 2323 2324 name = (int *)arg1; 2325 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req); 2326 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p); 2327 if (error != 0) { 2328 sbuf_delete(&sb); 2329 return (error); 2330 } 2331 error = kern_proc_vmmap_out(p, &sb); 2332 error2 = sbuf_finish(&sb); 2333 sbuf_delete(&sb); 2334 return (error != 0 ? error : error2); 2335 } 2336 2337 #if defined(STACK) || defined(DDB) 2338 static int 2339 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 2340 { 2341 struct kinfo_kstack *kkstp; 2342 int error, i, *name, numthreads; 2343 lwpid_t *lwpidarray; 2344 struct thread *td; 2345 struct stack *st; 2346 struct sbuf sb; 2347 struct proc *p; 2348 2349 name = (int *)arg1; 2350 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p); 2351 if (error != 0) 2352 return (error); 2353 2354 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 2355 st = stack_create(); 2356 2357 lwpidarray = NULL; 2358 numthreads = 0; 2359 PROC_LOCK(p); 2360 repeat: 2361 if (numthreads < p->p_numthreads) { 2362 if (lwpidarray != NULL) { 2363 free(lwpidarray, M_TEMP); 2364 lwpidarray = NULL; 2365 } 2366 numthreads = p->p_numthreads; 2367 PROC_UNLOCK(p); 2368 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 2369 M_WAITOK | M_ZERO); 2370 PROC_LOCK(p); 2371 goto repeat; 2372 } 2373 i = 0; 2374 2375 /* 2376 * XXXRW: During the below loop, execve(2) and countless other sorts 2377 * of changes could have taken place. Should we check to see if the 2378 * vmspace has been replaced, or the like, in order to prevent 2379 * giving a snapshot that spans, say, execve(2), with some threads 2380 * before and some after? Among other things, the credentials could 2381 * have changed, in which case the right to extract debug info might 2382 * no longer be assured. 2383 */ 2384 FOREACH_THREAD_IN_PROC(p, td) { 2385 KASSERT(i < numthreads, 2386 ("sysctl_kern_proc_kstack: numthreads")); 2387 lwpidarray[i] = td->td_tid; 2388 i++; 2389 } 2390 numthreads = i; 2391 for (i = 0; i < numthreads; i++) { 2392 td = thread_find(p, lwpidarray[i]); 2393 if (td == NULL) { 2394 continue; 2395 } 2396 bzero(kkstp, sizeof(*kkstp)); 2397 (void)sbuf_new(&sb, kkstp->kkst_trace, 2398 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 2399 thread_lock(td); 2400 kkstp->kkst_tid = td->td_tid; 2401 if (TD_IS_SWAPPED(td)) 2402 kkstp->kkst_state = KKST_STATE_SWAPPED; 2403 else if (TD_IS_RUNNING(td)) 2404 kkstp->kkst_state = KKST_STATE_RUNNING; 2405 else { 2406 kkstp->kkst_state = KKST_STATE_STACKOK; 2407 stack_save_td(st, td); 2408 } 2409 thread_unlock(td); 2410 PROC_UNLOCK(p); 2411 stack_sbuf_print(&sb, st); 2412 sbuf_finish(&sb); 2413 sbuf_delete(&sb); 2414 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 2415 PROC_LOCK(p); 2416 if (error) 2417 break; 2418 } 2419 _PRELE(p); 2420 PROC_UNLOCK(p); 2421 if (lwpidarray != NULL) 2422 free(lwpidarray, M_TEMP); 2423 stack_destroy(st); 2424 free(kkstp, M_TEMP); 2425 return (error); 2426 } 2427 #endif 2428 2429 /* 2430 * This sysctl allows a process to retrieve the full list of groups from 2431 * itself or another process. 2432 */ 2433 static int 2434 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 2435 { 2436 pid_t *pidp = (pid_t *)arg1; 2437 unsigned int arglen = arg2; 2438 struct proc *p; 2439 struct ucred *cred; 2440 int error; 2441 2442 if (arglen != 1) 2443 return (EINVAL); 2444 if (*pidp == -1) { /* -1 means this process */ 2445 p = req->td->td_proc; 2446 } else { 2447 error = pget(*pidp, PGET_CANSEE, &p); 2448 if (error != 0) 2449 return (error); 2450 } 2451 2452 cred = crhold(p->p_ucred); 2453 if (*pidp != -1) 2454 PROC_UNLOCK(p); 2455 2456 error = SYSCTL_OUT(req, cred->cr_groups, 2457 cred->cr_ngroups * sizeof(gid_t)); 2458 crfree(cred); 2459 return (error); 2460 } 2461 2462 /* 2463 * This sysctl allows a process to retrieve or/and set the resource limit for 2464 * another process. 2465 */ 2466 static int 2467 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS) 2468 { 2469 int *name = (int *)arg1; 2470 u_int namelen = arg2; 2471 struct rlimit rlim; 2472 struct proc *p; 2473 u_int which; 2474 int flags, error; 2475 2476 if (namelen != 2) 2477 return (EINVAL); 2478 2479 which = (u_int)name[1]; 2480 if (which >= RLIM_NLIMITS) 2481 return (EINVAL); 2482 2483 if (req->newptr != NULL && req->newlen != sizeof(rlim)) 2484 return (EINVAL); 2485 2486 flags = PGET_HOLD | PGET_NOTWEXIT; 2487 if (req->newptr != NULL) 2488 flags |= PGET_CANDEBUG; 2489 else 2490 flags |= PGET_CANSEE; 2491 error = pget((pid_t)name[0], flags, &p); 2492 if (error != 0) 2493 return (error); 2494 2495 /* 2496 * Retrieve limit. 2497 */ 2498 if (req->oldptr != NULL) { 2499 PROC_LOCK(p); 2500 lim_rlimit(p, which, &rlim); 2501 PROC_UNLOCK(p); 2502 } 2503 error = SYSCTL_OUT(req, &rlim, sizeof(rlim)); 2504 if (error != 0) 2505 goto errout; 2506 2507 /* 2508 * Set limit. 2509 */ 2510 if (req->newptr != NULL) { 2511 error = SYSCTL_IN(req, &rlim, sizeof(rlim)); 2512 if (error == 0) 2513 error = kern_proc_setrlimit(curthread, p, which, &rlim); 2514 } 2515 2516 errout: 2517 PRELE(p); 2518 return (error); 2519 } 2520 2521 /* 2522 * This sysctl allows a process to retrieve ps_strings structure location of 2523 * another process. 2524 */ 2525 static int 2526 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS) 2527 { 2528 int *name = (int *)arg1; 2529 u_int namelen = arg2; 2530 struct proc *p; 2531 vm_offset_t ps_strings; 2532 int error; 2533 #ifdef COMPAT_FREEBSD32 2534 uint32_t ps_strings32; 2535 #endif 2536 2537 if (namelen != 1) 2538 return (EINVAL); 2539 2540 error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 2541 if (error != 0) 2542 return (error); 2543 #ifdef COMPAT_FREEBSD32 2544 if ((req->flags & SCTL_MASK32) != 0) { 2545 /* 2546 * We return 0 if the 32 bit emulation request is for a 64 bit 2547 * process. 2548 */ 2549 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ? 2550 PTROUT(p->p_sysent->sv_psstrings) : 0; 2551 PROC_UNLOCK(p); 2552 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32)); 2553 return (error); 2554 } 2555 #endif 2556 ps_strings = p->p_sysent->sv_psstrings; 2557 PROC_UNLOCK(p); 2558 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings)); 2559 return (error); 2560 } 2561 2562 /* 2563 * This sysctl allows a process to retrieve umask of another process. 2564 */ 2565 static int 2566 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS) 2567 { 2568 int *name = (int *)arg1; 2569 u_int namelen = arg2; 2570 struct proc *p; 2571 int error; 2572 u_short fd_cmask; 2573 2574 if (namelen != 1) 2575 return (EINVAL); 2576 2577 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2578 if (error != 0) 2579 return (error); 2580 2581 FILEDESC_SLOCK(p->p_fd); 2582 fd_cmask = p->p_fd->fd_cmask; 2583 FILEDESC_SUNLOCK(p->p_fd); 2584 PRELE(p); 2585 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask)); 2586 return (error); 2587 } 2588 2589 /* 2590 * This sysctl allows a process to set and retrieve binary osreldate of 2591 * another process. 2592 */ 2593 static int 2594 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS) 2595 { 2596 int *name = (int *)arg1; 2597 u_int namelen = arg2; 2598 struct proc *p; 2599 int flags, error, osrel; 2600 2601 if (namelen != 1) 2602 return (EINVAL); 2603 2604 if (req->newptr != NULL && req->newlen != sizeof(osrel)) 2605 return (EINVAL); 2606 2607 flags = PGET_HOLD | PGET_NOTWEXIT; 2608 if (req->newptr != NULL) 2609 flags |= PGET_CANDEBUG; 2610 else 2611 flags |= PGET_CANSEE; 2612 error = pget((pid_t)name[0], flags, &p); 2613 if (error != 0) 2614 return (error); 2615 2616 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel)); 2617 if (error != 0) 2618 goto errout; 2619 2620 if (req->newptr != NULL) { 2621 error = SYSCTL_IN(req, &osrel, sizeof(osrel)); 2622 if (error != 0) 2623 goto errout; 2624 if (osrel < 0) { 2625 error = EINVAL; 2626 goto errout; 2627 } 2628 p->p_osrel = osrel; 2629 } 2630 errout: 2631 PRELE(p); 2632 return (error); 2633 } 2634 2635 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 2636 2637 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 2638 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 2639 "Return entire process table"); 2640 2641 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2642 sysctl_kern_proc, "Process table"); 2643 2644 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 2645 sysctl_kern_proc, "Process table"); 2646 2647 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2648 sysctl_kern_proc, "Process table"); 2649 2650 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 2651 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2652 2653 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2654 sysctl_kern_proc, "Process table"); 2655 2656 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2657 sysctl_kern_proc, "Process table"); 2658 2659 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2660 sysctl_kern_proc, "Process table"); 2661 2662 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2663 sysctl_kern_proc, "Process table"); 2664 2665 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 2666 sysctl_kern_proc, "Return process table, no threads"); 2667 2668 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 2669 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2670 sysctl_kern_proc_args, "Process argument list"); 2671 2672 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE, 2673 sysctl_kern_proc_env, "Process environment"); 2674 2675 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD | 2676 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector"); 2677 2678 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 2679 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2680 2681 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 2682 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 2683 "Process syscall vector name (ABI type)"); 2684 2685 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 2686 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2687 2688 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 2689 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2690 2691 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 2692 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2693 2694 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 2695 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2696 2697 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 2698 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2699 2700 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 2701 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2702 2703 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 2704 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2705 2706 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 2707 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2708 2709 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 2710 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 2711 "Return process table, no threads"); 2712 2713 #ifdef COMPAT_FREEBSD7 2714 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 2715 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 2716 #endif 2717 2718 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 2719 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 2720 2721 #if defined(STACK) || defined(DDB) 2722 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 2723 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 2724 #endif 2725 2726 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2727 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 2728 2729 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW | 2730 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, 2731 "Process resource limits"); 2732 2733 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD | 2734 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings, 2735 "Process ps_strings location"); 2736 2737 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD | 2738 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask"); 2739 2740 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW | 2741 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel, 2742 "Process binary osreldate"); 2743