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