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