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