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