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