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