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