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