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