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