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