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