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 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ddb.h" 38 #include "opt_ktrace.h" 39 #include "opt_kstack_pages.h" 40 #include "opt_stack.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/bitstring.h> 45 #include <sys/elf.h> 46 #include <sys/eventhandler.h> 47 #include <sys/exec.h> 48 #include <sys/fcntl.h> 49 #include <sys/jail.h> 50 #include <sys/kernel.h> 51 #include <sys/limits.h> 52 #include <sys/lock.h> 53 #include <sys/loginclass.h> 54 #include <sys/malloc.h> 55 #include <sys/mman.h> 56 #include <sys/mount.h> 57 #include <sys/mutex.h> 58 #include <sys/namei.h> 59 #include <sys/proc.h> 60 #include <sys/ptrace.h> 61 #include <sys/refcount.h> 62 #include <sys/resourcevar.h> 63 #include <sys/rwlock.h> 64 #include <sys/sbuf.h> 65 #include <sys/sysent.h> 66 #include <sys/sched.h> 67 #include <sys/smp.h> 68 #include <sys/stack.h> 69 #include <sys/stat.h> 70 #include <sys/dtrace_bsd.h> 71 #include <sys/sysctl.h> 72 #include <sys/filedesc.h> 73 #include <sys/tty.h> 74 #include <sys/signalvar.h> 75 #include <sys/sdt.h> 76 #include <sys/sx.h> 77 #include <sys/user.h> 78 #include <sys/vnode.h> 79 #include <sys/wait.h> 80 #ifdef KTRACE 81 #include <sys/ktrace.h> 82 #endif 83 84 #ifdef DDB 85 #include <ddb/ddb.h> 86 #endif 87 88 #include <vm/vm.h> 89 #include <vm/vm_param.h> 90 #include <vm/vm_extern.h> 91 #include <vm/pmap.h> 92 #include <vm/vm_map.h> 93 #include <vm/vm_object.h> 94 #include <vm/vm_page.h> 95 #include <vm/uma.h> 96 97 #include <fs/devfs/devfs.h> 98 99 #ifdef COMPAT_FREEBSD32 100 #include <compat/freebsd32/freebsd32.h> 101 #include <compat/freebsd32/freebsd32_util.h> 102 #endif 103 104 SDT_PROVIDER_DEFINE(proc); 105 106 MALLOC_DEFINE(M_SESSION, "session", "session header"); 107 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 108 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 109 110 static void doenterpgrp(struct proc *, struct pgrp *); 111 static void orphanpg(struct pgrp *pg); 112 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); 113 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 114 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 115 int preferthread); 116 static void pgdelete(struct pgrp *); 117 static int pgrp_init(void *mem, int size, int flags); 118 static int proc_ctor(void *mem, int size, void *arg, int flags); 119 static void proc_dtor(void *mem, int size, void *arg); 120 static int proc_init(void *mem, int size, int flags); 121 static void proc_fini(void *mem, int size); 122 static void pargs_free(struct pargs *pa); 123 124 /* 125 * Other process lists 126 */ 127 struct pidhashhead *pidhashtbl = NULL; 128 struct sx *pidhashtbl_lock; 129 u_long pidhash; 130 u_long pidhashlock; 131 struct pgrphashhead *pgrphashtbl; 132 u_long pgrphash; 133 struct proclist allproc = LIST_HEAD_INITIALIZER(allproc); 134 struct sx __exclusive_cache_line allproc_lock; 135 struct sx __exclusive_cache_line proctree_lock; 136 struct mtx __exclusive_cache_line ppeers_lock; 137 struct mtx __exclusive_cache_line procid_lock; 138 uma_zone_t proc_zone; 139 uma_zone_t pgrp_zone; 140 141 /* 142 * The offset of various fields in struct proc and struct thread. 143 * These are used by kernel debuggers to enumerate kernel threads and 144 * processes. 145 */ 146 const int proc_off_p_pid = offsetof(struct proc, p_pid); 147 const int proc_off_p_comm = offsetof(struct proc, p_comm); 148 const int proc_off_p_list = offsetof(struct proc, p_list); 149 const int proc_off_p_hash = offsetof(struct proc, p_hash); 150 const int proc_off_p_threads = offsetof(struct proc, p_threads); 151 const int thread_off_td_tid = offsetof(struct thread, td_tid); 152 const int thread_off_td_name = offsetof(struct thread, td_name); 153 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu); 154 const int thread_off_td_pcb = offsetof(struct thread, td_pcb); 155 const int thread_off_td_plist = offsetof(struct thread, td_plist); 156 157 EVENTHANDLER_LIST_DEFINE(process_ctor); 158 EVENTHANDLER_LIST_DEFINE(process_dtor); 159 EVENTHANDLER_LIST_DEFINE(process_init); 160 EVENTHANDLER_LIST_DEFINE(process_fini); 161 EVENTHANDLER_LIST_DEFINE(process_exit); 162 EVENTHANDLER_LIST_DEFINE(process_fork); 163 EVENTHANDLER_LIST_DEFINE(process_exec); 164 165 int kstack_pages = KSTACK_PAGES; 166 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, 167 "Kernel stack size in pages"); 168 static int vmmap_skip_res_cnt = 0; 169 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW, 170 &vmmap_skip_res_cnt, 0, 171 "Skip calculation of the pages resident count in kern.proc.vmmap"); 172 173 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 174 #ifdef COMPAT_FREEBSD32 175 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); 176 #endif 177 178 /* 179 * Initialize global process hashing structures. 180 */ 181 void 182 procinit(void) 183 { 184 u_long i; 185 186 sx_init(&allproc_lock, "allproc"); 187 sx_init(&proctree_lock, "proctree"); 188 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 189 mtx_init(&procid_lock, "procid", NULL, MTX_DEF); 190 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 191 pidhashlock = (pidhash + 1) / 64; 192 if (pidhashlock > 0) 193 pidhashlock--; 194 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1), 195 M_PROC, M_WAITOK | M_ZERO); 196 for (i = 0; i < pidhashlock + 1; i++) 197 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK); 198 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 199 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 200 proc_ctor, proc_dtor, proc_init, proc_fini, 201 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 202 pgrp_zone = uma_zcreate("PGRP", sizeof(struct pgrp), NULL, NULL, 203 pgrp_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 204 uihashinit(); 205 } 206 207 /* 208 * Prepare a proc for use. 209 */ 210 static int 211 proc_ctor(void *mem, int size, void *arg, int flags) 212 { 213 struct proc *p; 214 struct thread *td; 215 216 p = (struct proc *)mem; 217 #ifdef KDTRACE_HOOKS 218 kdtrace_proc_ctor(p); 219 #endif 220 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p); 221 td = FIRST_THREAD_IN_PROC(p); 222 if (td != NULL) { 223 /* Make sure all thread constructors are executed */ 224 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td); 225 } 226 return (0); 227 } 228 229 /* 230 * Reclaim a proc after use. 231 */ 232 static void 233 proc_dtor(void *mem, int size, void *arg) 234 { 235 struct proc *p; 236 struct thread *td; 237 238 /* INVARIANTS checks go here */ 239 p = (struct proc *)mem; 240 td = FIRST_THREAD_IN_PROC(p); 241 if (td != NULL) { 242 #ifdef INVARIANTS 243 KASSERT((p->p_numthreads == 1), 244 ("bad number of threads in exiting process")); 245 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 246 #endif 247 /* Free all OSD associated to this thread. */ 248 osd_thread_exit(td); 249 ast_kclear(td); 250 251 /* Make sure all thread destructors are executed */ 252 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td); 253 } 254 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p); 255 #ifdef KDTRACE_HOOKS 256 kdtrace_proc_dtor(p); 257 #endif 258 if (p->p_ksi != NULL) 259 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 260 } 261 262 /* 263 * Initialize type-stable parts of a proc (when newly created). 264 */ 265 static int 266 proc_init(void *mem, int size, int flags) 267 { 268 struct proc *p; 269 270 p = (struct proc *)mem; 271 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW); 272 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW); 273 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW); 274 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW); 275 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW); 276 cv_init(&p->p_pwait, "ppwait"); 277 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 278 EVENTHANDLER_DIRECT_INVOKE(process_init, p); 279 p->p_stats = pstats_alloc(); 280 p->p_pgrp = NULL; 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 if (!TD_IS_SWAPPED(td0)) 1147 kp->ki_rssize += td0->td_kstack_pages; 1148 } 1149 kp->ki_swrss = vm->vm_swrss; 1150 kp->ki_tsize = vm->vm_tsize; 1151 kp->ki_dsize = vm->vm_dsize; 1152 kp->ki_ssize = vm->vm_ssize; 1153 } else if (p->p_state == PRS_ZOMBIE) 1154 kp->ki_stat = SZOMB; 1155 if (kp->ki_flag & P_INMEM) 1156 kp->ki_sflag = PS_INMEM; 1157 else 1158 kp->ki_sflag = 0; 1159 /* Calculate legacy swtime as seconds since 'swtick'. */ 1160 kp->ki_swtime = (ticks - p->p_swtick) / hz; 1161 kp->ki_pid = p->p_pid; 1162 kp->ki_nice = p->p_nice; 1163 kp->ki_fibnum = p->p_fibnum; 1164 kp->ki_start = p->p_stats->p_start; 1165 getboottime(&boottime); 1166 timevaladd(&kp->ki_start, &boottime); 1167 PROC_STATLOCK(p); 1168 rufetch(p, &kp->ki_rusage); 1169 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 1170 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 1171 PROC_STATUNLOCK(p); 1172 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 1173 /* Some callers want child times in a single value. */ 1174 kp->ki_childtime = kp->ki_childstime; 1175 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 1176 1177 FOREACH_THREAD_IN_PROC(p, td0) 1178 kp->ki_cow += td0->td_cow; 1179 1180 if (p->p_comm[0] != '\0') 1181 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 1182 if (p->p_sysent && p->p_sysent->sv_name != NULL && 1183 p->p_sysent->sv_name[0] != '\0') 1184 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 1185 kp->ki_siglist = p->p_siglist; 1186 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig); 1187 kp->ki_acflag = p->p_acflag; 1188 kp->ki_lock = p->p_lock; 1189 if (p->p_pptr) { 1190 kp->ki_ppid = p->p_oppid; 1191 if (p->p_flag & P_TRACED) 1192 kp->ki_tracer = p->p_pptr->p_pid; 1193 } 1194 } 1195 1196 /* 1197 * Fill job-related process information. 1198 */ 1199 static void 1200 fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp) 1201 { 1202 struct tty *tp; 1203 struct session *sp; 1204 struct pgrp *pgrp; 1205 1206 sx_assert(&proctree_lock, SA_LOCKED); 1207 PROC_LOCK_ASSERT(p, MA_OWNED); 1208 1209 pgrp = p->p_pgrp; 1210 if (pgrp == NULL) 1211 return; 1212 1213 kp->ki_pgid = pgrp->pg_id; 1214 kp->ki_jobc = pgrp_calc_jobc(pgrp); 1215 1216 sp = pgrp->pg_session; 1217 tp = NULL; 1218 1219 if (sp != NULL) { 1220 kp->ki_sid = sp->s_sid; 1221 SESS_LOCK(sp); 1222 strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login)); 1223 if (sp->s_ttyvp) 1224 kp->ki_kiflag |= KI_CTTY; 1225 if (SESS_LEADER(p)) 1226 kp->ki_kiflag |= KI_SLEADER; 1227 tp = sp->s_ttyp; 1228 SESS_UNLOCK(sp); 1229 } 1230 1231 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 1232 kp->ki_tdev = tty_udev(tp); 1233 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */ 1234 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 1235 if (tp->t_session) 1236 kp->ki_tsid = tp->t_session->s_sid; 1237 } else { 1238 kp->ki_tdev = NODEV; 1239 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */ 1240 } 1241 } 1242 1243 /* 1244 * Fill in information that is thread specific. Must be called with 1245 * target process locked. If 'preferthread' is set, overwrite certain 1246 * process-related fields that are maintained for both threads and 1247 * processes. 1248 */ 1249 static void 1250 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 1251 { 1252 struct proc *p; 1253 1254 p = td->td_proc; 1255 kp->ki_tdaddr = td; 1256 PROC_LOCK_ASSERT(p, MA_OWNED); 1257 1258 if (preferthread) 1259 PROC_STATLOCK(p); 1260 thread_lock(td); 1261 if (td->td_wmesg != NULL) 1262 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 1263 else 1264 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 1265 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >= 1266 sizeof(kp->ki_tdname)) { 1267 strlcpy(kp->ki_moretdname, 1268 td->td_name + sizeof(kp->ki_tdname) - 1, 1269 sizeof(kp->ki_moretdname)); 1270 } else { 1271 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname)); 1272 } 1273 if (TD_ON_LOCK(td)) { 1274 kp->ki_kiflag |= KI_LOCKBLOCK; 1275 strlcpy(kp->ki_lockname, td->td_lockname, 1276 sizeof(kp->ki_lockname)); 1277 } else { 1278 kp->ki_kiflag &= ~KI_LOCKBLOCK; 1279 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 1280 } 1281 1282 if (p->p_state == PRS_NORMAL) { /* approximate. */ 1283 if (TD_ON_RUNQ(td) || 1284 TD_CAN_RUN(td) || 1285 TD_IS_RUNNING(td)) { 1286 kp->ki_stat = SRUN; 1287 } else if (P_SHOULDSTOP(p)) { 1288 kp->ki_stat = SSTOP; 1289 } else if (TD_IS_SLEEPING(td)) { 1290 kp->ki_stat = SSLEEP; 1291 } else if (TD_ON_LOCK(td)) { 1292 kp->ki_stat = SLOCK; 1293 } else { 1294 kp->ki_stat = SWAIT; 1295 } 1296 } else if (p->p_state == PRS_ZOMBIE) { 1297 kp->ki_stat = SZOMB; 1298 } else { 1299 kp->ki_stat = SIDL; 1300 } 1301 1302 /* Things in the thread */ 1303 kp->ki_wchan = td->td_wchan; 1304 kp->ki_pri.pri_level = td->td_priority; 1305 kp->ki_pri.pri_native = td->td_base_pri; 1306 1307 /* 1308 * Note: legacy fields; clamp at the old NOCPU value and/or 1309 * the maximum u_char CPU value. 1310 */ 1311 if (td->td_lastcpu == NOCPU) 1312 kp->ki_lastcpu_old = NOCPU_OLD; 1313 else if (td->td_lastcpu > MAXCPU_OLD) 1314 kp->ki_lastcpu_old = MAXCPU_OLD; 1315 else 1316 kp->ki_lastcpu_old = td->td_lastcpu; 1317 1318 if (td->td_oncpu == NOCPU) 1319 kp->ki_oncpu_old = NOCPU_OLD; 1320 else if (td->td_oncpu > MAXCPU_OLD) 1321 kp->ki_oncpu_old = MAXCPU_OLD; 1322 else 1323 kp->ki_oncpu_old = td->td_oncpu; 1324 1325 kp->ki_lastcpu = td->td_lastcpu; 1326 kp->ki_oncpu = td->td_oncpu; 1327 kp->ki_tdflags = td->td_flags; 1328 kp->ki_tid = td->td_tid; 1329 kp->ki_numthreads = p->p_numthreads; 1330 kp->ki_pcb = td->td_pcb; 1331 kp->ki_kstack = (void *)td->td_kstack; 1332 kp->ki_slptime = (ticks - td->td_slptick) / hz; 1333 kp->ki_pri.pri_class = td->td_pri_class; 1334 kp->ki_pri.pri_user = td->td_user_pri; 1335 1336 if (preferthread) { 1337 rufetchtd(td, &kp->ki_rusage); 1338 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 1339 kp->ki_pctcpu = sched_pctcpu(td); 1340 kp->ki_estcpu = sched_estcpu(td); 1341 kp->ki_cow = td->td_cow; 1342 } 1343 1344 /* We can't get this anymore but ps etc never used it anyway. */ 1345 kp->ki_rqindex = 0; 1346 1347 if (preferthread) 1348 kp->ki_siglist = td->td_siglist; 1349 kp->ki_sigmask = td->td_sigmask; 1350 thread_unlock(td); 1351 if (preferthread) 1352 PROC_STATUNLOCK(p); 1353 } 1354 1355 /* 1356 * Fill in a kinfo_proc structure for the specified process. 1357 * Must be called with the target process locked. 1358 */ 1359 void 1360 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 1361 { 1362 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1363 1364 bzero(kp, sizeof(*kp)); 1365 1366 fill_kinfo_proc_pgrp(p,kp); 1367 fill_kinfo_proc_only(p, kp); 1368 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 1369 fill_kinfo_aggregate(p, kp); 1370 } 1371 1372 struct pstats * 1373 pstats_alloc(void) 1374 { 1375 1376 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 1377 } 1378 1379 /* 1380 * Copy parts of p_stats; zero the rest of p_stats (statistics). 1381 */ 1382 void 1383 pstats_fork(struct pstats *src, struct pstats *dst) 1384 { 1385 1386 bzero(&dst->pstat_startzero, 1387 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 1388 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 1389 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 1390 } 1391 1392 void 1393 pstats_free(struct pstats *ps) 1394 { 1395 1396 free(ps, M_SUBPROC); 1397 } 1398 1399 #ifdef COMPAT_FREEBSD32 1400 1401 /* 1402 * This function is typically used to copy out the kernel address, so 1403 * it can be replaced by assignment of zero. 1404 */ 1405 static inline uint32_t 1406 ptr32_trim(const void *ptr) 1407 { 1408 uintptr_t uptr; 1409 1410 uptr = (uintptr_t)ptr; 1411 return ((uptr > UINT_MAX) ? 0 : uptr); 1412 } 1413 1414 #define PTRTRIM_CP(src,dst,fld) \ 1415 do { (dst).fld = ptr32_trim((src).fld); } while (0) 1416 1417 static void 1418 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 1419 { 1420 int i; 1421 1422 bzero(ki32, sizeof(struct kinfo_proc32)); 1423 ki32->ki_structsize = sizeof(struct kinfo_proc32); 1424 CP(*ki, *ki32, ki_layout); 1425 PTRTRIM_CP(*ki, *ki32, ki_args); 1426 PTRTRIM_CP(*ki, *ki32, ki_paddr); 1427 PTRTRIM_CP(*ki, *ki32, ki_addr); 1428 PTRTRIM_CP(*ki, *ki32, ki_tracep); 1429 PTRTRIM_CP(*ki, *ki32, ki_textvp); 1430 PTRTRIM_CP(*ki, *ki32, ki_fd); 1431 PTRTRIM_CP(*ki, *ki32, ki_vmspace); 1432 PTRTRIM_CP(*ki, *ki32, ki_wchan); 1433 CP(*ki, *ki32, ki_pid); 1434 CP(*ki, *ki32, ki_ppid); 1435 CP(*ki, *ki32, ki_pgid); 1436 CP(*ki, *ki32, ki_tpgid); 1437 CP(*ki, *ki32, ki_sid); 1438 CP(*ki, *ki32, ki_tsid); 1439 CP(*ki, *ki32, ki_jobc); 1440 CP(*ki, *ki32, ki_tdev); 1441 CP(*ki, *ki32, ki_tdev_freebsd11); 1442 CP(*ki, *ki32, ki_siglist); 1443 CP(*ki, *ki32, ki_sigmask); 1444 CP(*ki, *ki32, ki_sigignore); 1445 CP(*ki, *ki32, ki_sigcatch); 1446 CP(*ki, *ki32, ki_uid); 1447 CP(*ki, *ki32, ki_ruid); 1448 CP(*ki, *ki32, ki_svuid); 1449 CP(*ki, *ki32, ki_rgid); 1450 CP(*ki, *ki32, ki_svgid); 1451 CP(*ki, *ki32, ki_ngroups); 1452 for (i = 0; i < KI_NGROUPS; i++) 1453 CP(*ki, *ki32, ki_groups[i]); 1454 CP(*ki, *ki32, ki_size); 1455 CP(*ki, *ki32, ki_rssize); 1456 CP(*ki, *ki32, ki_swrss); 1457 CP(*ki, *ki32, ki_tsize); 1458 CP(*ki, *ki32, ki_dsize); 1459 CP(*ki, *ki32, ki_ssize); 1460 CP(*ki, *ki32, ki_xstat); 1461 CP(*ki, *ki32, ki_acflag); 1462 CP(*ki, *ki32, ki_pctcpu); 1463 CP(*ki, *ki32, ki_estcpu); 1464 CP(*ki, *ki32, ki_slptime); 1465 CP(*ki, *ki32, ki_swtime); 1466 CP(*ki, *ki32, ki_cow); 1467 CP(*ki, *ki32, ki_runtime); 1468 TV_CP(*ki, *ki32, ki_start); 1469 TV_CP(*ki, *ki32, ki_childtime); 1470 CP(*ki, *ki32, ki_flag); 1471 CP(*ki, *ki32, ki_kiflag); 1472 CP(*ki, *ki32, ki_traceflag); 1473 CP(*ki, *ki32, ki_stat); 1474 CP(*ki, *ki32, ki_nice); 1475 CP(*ki, *ki32, ki_lock); 1476 CP(*ki, *ki32, ki_rqindex); 1477 CP(*ki, *ki32, ki_oncpu); 1478 CP(*ki, *ki32, ki_lastcpu); 1479 1480 /* XXX TODO: wrap cpu value as appropriate */ 1481 CP(*ki, *ki32, ki_oncpu_old); 1482 CP(*ki, *ki32, ki_lastcpu_old); 1483 1484 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1); 1485 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 1486 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 1487 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 1488 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 1489 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 1490 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); 1491 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1); 1492 CP(*ki, *ki32, ki_tracer); 1493 CP(*ki, *ki32, ki_flag2); 1494 CP(*ki, *ki32, ki_fibnum); 1495 CP(*ki, *ki32, ki_cr_flags); 1496 CP(*ki, *ki32, ki_jid); 1497 CP(*ki, *ki32, ki_numthreads); 1498 CP(*ki, *ki32, ki_tid); 1499 CP(*ki, *ki32, ki_pri); 1500 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 1501 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 1502 PTRTRIM_CP(*ki, *ki32, ki_pcb); 1503 PTRTRIM_CP(*ki, *ki32, ki_kstack); 1504 PTRTRIM_CP(*ki, *ki32, ki_udata); 1505 PTRTRIM_CP(*ki, *ki32, ki_tdaddr); 1506 CP(*ki, *ki32, ki_sflag); 1507 CP(*ki, *ki32, ki_tdflags); 1508 } 1509 #endif 1510 1511 static ssize_t 1512 kern_proc_out_size(struct proc *p, int flags) 1513 { 1514 ssize_t size = 0; 1515 1516 PROC_LOCK_ASSERT(p, MA_OWNED); 1517 1518 if ((flags & KERN_PROC_NOTHREADS) != 0) { 1519 #ifdef COMPAT_FREEBSD32 1520 if ((flags & KERN_PROC_MASK32) != 0) { 1521 size += sizeof(struct kinfo_proc32); 1522 } else 1523 #endif 1524 size += sizeof(struct kinfo_proc); 1525 } else { 1526 #ifdef COMPAT_FREEBSD32 1527 if ((flags & KERN_PROC_MASK32) != 0) 1528 size += sizeof(struct kinfo_proc32) * p->p_numthreads; 1529 else 1530 #endif 1531 size += sizeof(struct kinfo_proc) * p->p_numthreads; 1532 } 1533 PROC_UNLOCK(p); 1534 return (size); 1535 } 1536 1537 int 1538 kern_proc_out(struct proc *p, struct sbuf *sb, int flags) 1539 { 1540 struct thread *td; 1541 struct kinfo_proc ki; 1542 #ifdef COMPAT_FREEBSD32 1543 struct kinfo_proc32 ki32; 1544 #endif 1545 int error; 1546 1547 PROC_LOCK_ASSERT(p, MA_OWNED); 1548 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1549 1550 error = 0; 1551 fill_kinfo_proc(p, &ki); 1552 if ((flags & KERN_PROC_NOTHREADS) != 0) { 1553 #ifdef COMPAT_FREEBSD32 1554 if ((flags & KERN_PROC_MASK32) != 0) { 1555 freebsd32_kinfo_proc_out(&ki, &ki32); 1556 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0) 1557 error = ENOMEM; 1558 } else 1559 #endif 1560 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0) 1561 error = ENOMEM; 1562 } else { 1563 FOREACH_THREAD_IN_PROC(p, td) { 1564 fill_kinfo_thread(td, &ki, 1); 1565 #ifdef COMPAT_FREEBSD32 1566 if ((flags & KERN_PROC_MASK32) != 0) { 1567 freebsd32_kinfo_proc_out(&ki, &ki32); 1568 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0) 1569 error = ENOMEM; 1570 } else 1571 #endif 1572 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0) 1573 error = ENOMEM; 1574 if (error != 0) 1575 break; 1576 } 1577 } 1578 PROC_UNLOCK(p); 1579 return (error); 1580 } 1581 1582 static int 1583 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 1584 { 1585 struct sbuf sb; 1586 struct kinfo_proc ki; 1587 int error, error2; 1588 1589 if (req->oldptr == NULL) 1590 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags))); 1591 1592 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req); 1593 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 1594 error = kern_proc_out(p, &sb, flags); 1595 error2 = sbuf_finish(&sb); 1596 sbuf_delete(&sb); 1597 if (error != 0) 1598 return (error); 1599 else if (error2 != 0) 1600 return (error2); 1601 return (0); 1602 } 1603 1604 int 1605 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg) 1606 { 1607 struct proc *p; 1608 int error, i, j; 1609 1610 for (i = 0; i < pidhashlock + 1; i++) { 1611 sx_slock(&proctree_lock); 1612 sx_slock(&pidhashtbl_lock[i]); 1613 for (j = i; j <= pidhash; j += pidhashlock + 1) { 1614 LIST_FOREACH(p, &pidhashtbl[j], p_hash) { 1615 if (p->p_state == PRS_NEW) 1616 continue; 1617 error = cb(p, cbarg); 1618 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 1619 if (error != 0) { 1620 sx_sunlock(&pidhashtbl_lock[i]); 1621 sx_sunlock(&proctree_lock); 1622 return (error); 1623 } 1624 } 1625 } 1626 sx_sunlock(&pidhashtbl_lock[i]); 1627 sx_sunlock(&proctree_lock); 1628 } 1629 return (0); 1630 } 1631 1632 struct kern_proc_out_args { 1633 struct sysctl_req *req; 1634 int flags; 1635 int oid_number; 1636 int *name; 1637 }; 1638 1639 static int 1640 sysctl_kern_proc_iterate(struct proc *p, void *origarg) 1641 { 1642 struct kern_proc_out_args *arg = origarg; 1643 int *name = arg->name; 1644 int oid_number = arg->oid_number; 1645 int flags = arg->flags; 1646 struct sysctl_req *req = arg->req; 1647 int error = 0; 1648 1649 PROC_LOCK(p); 1650 1651 KASSERT(p->p_ucred != NULL, 1652 ("process credential is NULL for non-NEW proc")); 1653 /* 1654 * Show a user only appropriate processes. 1655 */ 1656 if (p_cansee(curthread, p)) 1657 goto skip; 1658 /* 1659 * TODO - make more efficient (see notes below). 1660 * do by session. 1661 */ 1662 switch (oid_number) { 1663 case KERN_PROC_GID: 1664 if (p->p_ucred->cr_gid != (gid_t)name[0]) 1665 goto skip; 1666 break; 1667 1668 case KERN_PROC_PGRP: 1669 /* could do this by traversing pgrp */ 1670 if (p->p_pgrp == NULL || 1671 p->p_pgrp->pg_id != (pid_t)name[0]) 1672 goto skip; 1673 break; 1674 1675 case KERN_PROC_RGID: 1676 if (p->p_ucred->cr_rgid != (gid_t)name[0]) 1677 goto skip; 1678 break; 1679 1680 case KERN_PROC_SESSION: 1681 if (p->p_session == NULL || 1682 p->p_session->s_sid != (pid_t)name[0]) 1683 goto skip; 1684 break; 1685 1686 case KERN_PROC_TTY: 1687 if ((p->p_flag & P_CONTROLT) == 0 || 1688 p->p_session == NULL) 1689 goto skip; 1690 /* XXX proctree_lock */ 1691 SESS_LOCK(p->p_session); 1692 if (p->p_session->s_ttyp == NULL || 1693 tty_udev(p->p_session->s_ttyp) != 1694 (dev_t)name[0]) { 1695 SESS_UNLOCK(p->p_session); 1696 goto skip; 1697 } 1698 SESS_UNLOCK(p->p_session); 1699 break; 1700 1701 case KERN_PROC_UID: 1702 if (p->p_ucred->cr_uid != (uid_t)name[0]) 1703 goto skip; 1704 break; 1705 1706 case KERN_PROC_RUID: 1707 if (p->p_ucred->cr_ruid != (uid_t)name[0]) 1708 goto skip; 1709 break; 1710 1711 case KERN_PROC_PROC: 1712 break; 1713 1714 default: 1715 break; 1716 } 1717 error = sysctl_out_proc(p, req, flags); 1718 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 1719 return (error); 1720 skip: 1721 PROC_UNLOCK(p); 1722 return (0); 1723 } 1724 1725 static int 1726 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1727 { 1728 struct kern_proc_out_args iterarg; 1729 int *name = (int *)arg1; 1730 u_int namelen = arg2; 1731 struct proc *p; 1732 int flags, oid_number; 1733 int error = 0; 1734 1735 oid_number = oidp->oid_number; 1736 if (oid_number != KERN_PROC_ALL && 1737 (oid_number & KERN_PROC_INC_THREAD) == 0) 1738 flags = KERN_PROC_NOTHREADS; 1739 else { 1740 flags = 0; 1741 oid_number &= ~KERN_PROC_INC_THREAD; 1742 } 1743 #ifdef COMPAT_FREEBSD32 1744 if (req->flags & SCTL_MASK32) 1745 flags |= KERN_PROC_MASK32; 1746 #endif 1747 if (oid_number == KERN_PROC_PID) { 1748 if (namelen != 1) 1749 return (EINVAL); 1750 error = sysctl_wire_old_buffer(req, 0); 1751 if (error) 1752 return (error); 1753 sx_slock(&proctree_lock); 1754 error = pget((pid_t)name[0], PGET_CANSEE, &p); 1755 if (error == 0) 1756 error = sysctl_out_proc(p, req, flags); 1757 sx_sunlock(&proctree_lock); 1758 return (error); 1759 } 1760 1761 switch (oid_number) { 1762 case KERN_PROC_ALL: 1763 if (namelen != 0) 1764 return (EINVAL); 1765 break; 1766 case KERN_PROC_PROC: 1767 if (namelen != 0 && namelen != 1) 1768 return (EINVAL); 1769 break; 1770 default: 1771 if (namelen != 1) 1772 return (EINVAL); 1773 break; 1774 } 1775 1776 if (req->oldptr == NULL) { 1777 /* overestimate by 5 procs */ 1778 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1779 if (error) 1780 return (error); 1781 } else { 1782 error = sysctl_wire_old_buffer(req, 0); 1783 if (error != 0) 1784 return (error); 1785 } 1786 iterarg.flags = flags; 1787 iterarg.oid_number = oid_number; 1788 iterarg.req = req; 1789 iterarg.name = name; 1790 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg); 1791 return (error); 1792 } 1793 1794 struct pargs * 1795 pargs_alloc(int len) 1796 { 1797 struct pargs *pa; 1798 1799 pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1800 M_WAITOK); 1801 refcount_init(&pa->ar_ref, 1); 1802 pa->ar_length = len; 1803 return (pa); 1804 } 1805 1806 static void 1807 pargs_free(struct pargs *pa) 1808 { 1809 1810 free(pa, M_PARGS); 1811 } 1812 1813 void 1814 pargs_hold(struct pargs *pa) 1815 { 1816 1817 if (pa == NULL) 1818 return; 1819 refcount_acquire(&pa->ar_ref); 1820 } 1821 1822 void 1823 pargs_drop(struct pargs *pa) 1824 { 1825 1826 if (pa == NULL) 1827 return; 1828 if (refcount_release(&pa->ar_ref)) 1829 pargs_free(pa); 1830 } 1831 1832 static int 1833 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf, 1834 size_t len) 1835 { 1836 ssize_t n; 1837 1838 /* 1839 * This may return a short read if the string is shorter than the chunk 1840 * and is aligned at the end of the page, and the following page is not 1841 * mapped. 1842 */ 1843 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len); 1844 if (n <= 0) 1845 return (ENOMEM); 1846 return (0); 1847 } 1848 1849 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */ 1850 1851 enum proc_vector_type { 1852 PROC_ARG, 1853 PROC_ENV, 1854 PROC_AUX, 1855 }; 1856 1857 #ifdef COMPAT_FREEBSD32 1858 static int 1859 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp, 1860 size_t *vsizep, enum proc_vector_type type) 1861 { 1862 struct freebsd32_ps_strings pss; 1863 Elf32_Auxinfo aux; 1864 vm_offset_t vptr, ptr; 1865 uint32_t *proc_vector32; 1866 char **proc_vector; 1867 size_t vsize, size; 1868 int i, error; 1869 1870 error = 0; 1871 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) != 1872 sizeof(pss)) 1873 return (ENOMEM); 1874 switch (type) { 1875 case PROC_ARG: 1876 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr); 1877 vsize = pss.ps_nargvstr; 1878 if (vsize > ARG_MAX) 1879 return (ENOEXEC); 1880 size = vsize * sizeof(int32_t); 1881 break; 1882 case PROC_ENV: 1883 vptr = (vm_offset_t)PTRIN(pss.ps_envstr); 1884 vsize = pss.ps_nenvstr; 1885 if (vsize > ARG_MAX) 1886 return (ENOEXEC); 1887 size = vsize * sizeof(int32_t); 1888 break; 1889 case PROC_AUX: 1890 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) + 1891 (pss.ps_nenvstr + 1) * sizeof(int32_t); 1892 if (vptr % 4 != 0) 1893 return (ENOEXEC); 1894 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1895 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) != 1896 sizeof(aux)) 1897 return (ENOMEM); 1898 if (aux.a_type == AT_NULL) 1899 break; 1900 ptr += sizeof(aux); 1901 } 1902 if (aux.a_type != AT_NULL) 1903 return (ENOEXEC); 1904 vsize = i + 1; 1905 size = vsize * sizeof(aux); 1906 break; 1907 default: 1908 KASSERT(0, ("Wrong proc vector type: %d", type)); 1909 return (EINVAL); 1910 } 1911 proc_vector32 = malloc(size, M_TEMP, M_WAITOK); 1912 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) { 1913 error = ENOMEM; 1914 goto done; 1915 } 1916 if (type == PROC_AUX) { 1917 *proc_vectorp = (char **)proc_vector32; 1918 *vsizep = vsize; 1919 return (0); 1920 } 1921 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK); 1922 for (i = 0; i < (int)vsize; i++) 1923 proc_vector[i] = PTRIN(proc_vector32[i]); 1924 *proc_vectorp = proc_vector; 1925 *vsizep = vsize; 1926 done: 1927 free(proc_vector32, M_TEMP); 1928 return (error); 1929 } 1930 #endif 1931 1932 static int 1933 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp, 1934 size_t *vsizep, enum proc_vector_type type) 1935 { 1936 struct ps_strings pss; 1937 Elf_Auxinfo aux; 1938 vm_offset_t vptr, ptr; 1939 char **proc_vector; 1940 size_t vsize, size; 1941 int i; 1942 1943 #ifdef COMPAT_FREEBSD32 1944 if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1945 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type)); 1946 #endif 1947 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) != 1948 sizeof(pss)) 1949 return (ENOMEM); 1950 switch (type) { 1951 case PROC_ARG: 1952 vptr = (vm_offset_t)pss.ps_argvstr; 1953 vsize = pss.ps_nargvstr; 1954 if (vsize > ARG_MAX) 1955 return (ENOEXEC); 1956 size = vsize * sizeof(char *); 1957 break; 1958 case PROC_ENV: 1959 vptr = (vm_offset_t)pss.ps_envstr; 1960 vsize = pss.ps_nenvstr; 1961 if (vsize > ARG_MAX) 1962 return (ENOEXEC); 1963 size = vsize * sizeof(char *); 1964 break; 1965 case PROC_AUX: 1966 /* 1967 * The aux array is just above env array on the stack. Check 1968 * that the address is naturally aligned. 1969 */ 1970 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1) 1971 * sizeof(char *); 1972 #if __ELF_WORD_SIZE == 64 1973 if (vptr % sizeof(uint64_t) != 0) 1974 #else 1975 if (vptr % sizeof(uint32_t) != 0) 1976 #endif 1977 return (ENOEXEC); 1978 /* 1979 * We count the array size reading the aux vectors from the 1980 * stack until AT_NULL vector is returned. So (to keep the code 1981 * simple) we read the process stack twice: the first time here 1982 * to find the size and the second time when copying the vectors 1983 * to the allocated proc_vector. 1984 */ 1985 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1986 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) != 1987 sizeof(aux)) 1988 return (ENOMEM); 1989 if (aux.a_type == AT_NULL) 1990 break; 1991 ptr += sizeof(aux); 1992 } 1993 /* 1994 * If the PROC_AUXV_MAX entries are iterated over, and we have 1995 * not reached AT_NULL, it is most likely we are reading wrong 1996 * data: either the process doesn't have auxv array or data has 1997 * been modified. Return the error in this case. 1998 */ 1999 if (aux.a_type != AT_NULL) 2000 return (ENOEXEC); 2001 vsize = i + 1; 2002 size = vsize * sizeof(aux); 2003 break; 2004 default: 2005 KASSERT(0, ("Wrong proc vector type: %d", type)); 2006 return (EINVAL); /* In case we are built without INVARIANTS. */ 2007 } 2008 proc_vector = malloc(size, M_TEMP, M_WAITOK); 2009 if (proc_readmem(td, p, vptr, proc_vector, size) != size) { 2010 free(proc_vector, M_TEMP); 2011 return (ENOMEM); 2012 } 2013 *proc_vectorp = proc_vector; 2014 *vsizep = vsize; 2015 2016 return (0); 2017 } 2018 2019 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */ 2020 2021 static int 2022 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb, 2023 enum proc_vector_type type) 2024 { 2025 size_t done, len, nchr, vsize; 2026 int error, i; 2027 char **proc_vector, *sptr; 2028 char pss_string[GET_PS_STRINGS_CHUNK_SZ]; 2029 2030 PROC_ASSERT_HELD(p); 2031 2032 /* 2033 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes. 2034 */ 2035 nchr = 2 * (PATH_MAX + ARG_MAX); 2036 2037 error = get_proc_vector(td, p, &proc_vector, &vsize, type); 2038 if (error != 0) 2039 return (error); 2040 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) { 2041 /* 2042 * The program may have scribbled into its argv array, e.g. to 2043 * remove some arguments. If that has happened, break out 2044 * before trying to read from NULL. 2045 */ 2046 if (proc_vector[i] == NULL) 2047 break; 2048 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) { 2049 error = proc_read_string(td, p, sptr, pss_string, 2050 sizeof(pss_string)); 2051 if (error != 0) 2052 goto done; 2053 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ); 2054 if (done + len >= nchr) 2055 len = nchr - done - 1; 2056 sbuf_bcat(sb, pss_string, len); 2057 if (len != GET_PS_STRINGS_CHUNK_SZ) 2058 break; 2059 done += GET_PS_STRINGS_CHUNK_SZ; 2060 } 2061 sbuf_bcat(sb, "", 1); 2062 done += len + 1; 2063 } 2064 done: 2065 free(proc_vector, M_TEMP); 2066 return (error); 2067 } 2068 2069 int 2070 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb) 2071 { 2072 2073 return (get_ps_strings(curthread, p, sb, PROC_ARG)); 2074 } 2075 2076 int 2077 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb) 2078 { 2079 2080 return (get_ps_strings(curthread, p, sb, PROC_ENV)); 2081 } 2082 2083 int 2084 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb) 2085 { 2086 size_t vsize, size; 2087 char **auxv; 2088 int error; 2089 2090 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX); 2091 if (error == 0) { 2092 #ifdef COMPAT_FREEBSD32 2093 if (SV_PROC_FLAG(p, SV_ILP32) != 0) 2094 size = vsize * sizeof(Elf32_Auxinfo); 2095 else 2096 #endif 2097 size = vsize * sizeof(Elf_Auxinfo); 2098 if (sbuf_bcat(sb, auxv, size) != 0) 2099 error = ENOMEM; 2100 free(auxv, M_TEMP); 2101 } 2102 return (error); 2103 } 2104 2105 /* 2106 * This sysctl allows a process to retrieve the argument list or process 2107 * title for another process without groping around in the address space 2108 * of the other process. It also allow a process to set its own "process 2109 * title to a string of its own choice. 2110 */ 2111 static int 2112 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 2113 { 2114 int *name = (int *)arg1; 2115 u_int namelen = arg2; 2116 struct pargs *newpa, *pa; 2117 struct proc *p; 2118 struct sbuf sb; 2119 int flags, error = 0, error2; 2120 pid_t pid; 2121 2122 if (namelen != 1) 2123 return (EINVAL); 2124 2125 p = curproc; 2126 pid = (pid_t)name[0]; 2127 if (pid == -1) { 2128 pid = p->p_pid; 2129 } 2130 2131 /* 2132 * If the query is for this process and it is single-threaded, there 2133 * is nobody to modify pargs, thus we can just read. 2134 */ 2135 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL && 2136 (pa = p->p_args) != NULL) 2137 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length)); 2138 2139 flags = PGET_CANSEE; 2140 if (req->newptr != NULL) 2141 flags |= PGET_ISCURRENT; 2142 error = pget(pid, flags, &p); 2143 if (error) 2144 return (error); 2145 2146 pa = p->p_args; 2147 if (pa != NULL) { 2148 pargs_hold(pa); 2149 PROC_UNLOCK(p); 2150 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 2151 pargs_drop(pa); 2152 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) { 2153 _PHOLD(p); 2154 PROC_UNLOCK(p); 2155 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 2156 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 2157 error = proc_getargv(curthread, p, &sb); 2158 error2 = sbuf_finish(&sb); 2159 PRELE(p); 2160 sbuf_delete(&sb); 2161 if (error == 0 && error2 != 0) 2162 error = error2; 2163 } else { 2164 PROC_UNLOCK(p); 2165 } 2166 if (error != 0 || req->newptr == NULL) 2167 return (error); 2168 2169 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs)) 2170 return (ENOMEM); 2171 2172 if (req->newlen == 0) { 2173 /* 2174 * Clear the argument pointer, so that we'll fetch arguments 2175 * with proc_getargv() until further notice. 2176 */ 2177 newpa = NULL; 2178 } else { 2179 newpa = pargs_alloc(req->newlen); 2180 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 2181 if (error != 0) { 2182 pargs_free(newpa); 2183 return (error); 2184 } 2185 } 2186 PROC_LOCK(p); 2187 pa = p->p_args; 2188 p->p_args = newpa; 2189 PROC_UNLOCK(p); 2190 pargs_drop(pa); 2191 return (0); 2192 } 2193 2194 /* 2195 * This sysctl allows a process to retrieve environment of another process. 2196 */ 2197 static int 2198 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS) 2199 { 2200 int *name = (int *)arg1; 2201 u_int namelen = arg2; 2202 struct proc *p; 2203 struct sbuf sb; 2204 int error, error2; 2205 2206 if (namelen != 1) 2207 return (EINVAL); 2208 2209 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2210 if (error != 0) 2211 return (error); 2212 if ((p->p_flag & P_SYSTEM) != 0) { 2213 PRELE(p); 2214 return (0); 2215 } 2216 2217 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 2218 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 2219 error = proc_getenvv(curthread, p, &sb); 2220 error2 = sbuf_finish(&sb); 2221 PRELE(p); 2222 sbuf_delete(&sb); 2223 return (error != 0 ? error : error2); 2224 } 2225 2226 /* 2227 * This sysctl allows a process to retrieve ELF auxiliary vector of 2228 * another process. 2229 */ 2230 static int 2231 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS) 2232 { 2233 int *name = (int *)arg1; 2234 u_int namelen = arg2; 2235 struct proc *p; 2236 struct sbuf sb; 2237 int error, error2; 2238 2239 if (namelen != 1) 2240 return (EINVAL); 2241 2242 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2243 if (error != 0) 2244 return (error); 2245 if ((p->p_flag & P_SYSTEM) != 0) { 2246 PRELE(p); 2247 return (0); 2248 } 2249 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 2250 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 2251 error = proc_getauxv(curthread, p, &sb); 2252 error2 = sbuf_finish(&sb); 2253 PRELE(p); 2254 sbuf_delete(&sb); 2255 return (error != 0 ? error : error2); 2256 } 2257 2258 /* 2259 * Look up the canonical executable path running in the specified process. 2260 * It tries to return the same hardlink name as was used for execve(2). 2261 * This allows the programs that modify their behavior based on their progname, 2262 * to operate correctly. 2263 * 2264 * Result is returned in retbuf, it must not be freed, similar to vn_fullpath() 2265 * calling conventions. 2266 * binname is a pointer to temporary string buffer of length MAXPATHLEN, 2267 * allocated and freed by caller. 2268 * freebuf should be freed by caller, from the M_TEMP malloc type. 2269 */ 2270 int 2271 proc_get_binpath(struct proc *p, char *binname, char **retbuf, 2272 char **freebuf) 2273 { 2274 struct nameidata nd; 2275 struct vnode *vp, *dvp; 2276 size_t freepath_size; 2277 int error; 2278 bool do_fullpath; 2279 2280 PROC_LOCK_ASSERT(p, MA_OWNED); 2281 2282 vp = p->p_textvp; 2283 if (vp == NULL) { 2284 PROC_UNLOCK(p); 2285 *retbuf = ""; 2286 *freebuf = NULL; 2287 return (0); 2288 } 2289 vref(vp); 2290 dvp = p->p_textdvp; 2291 if (dvp != NULL) 2292 vref(dvp); 2293 if (p->p_binname != NULL) 2294 strlcpy(binname, p->p_binname, MAXPATHLEN); 2295 PROC_UNLOCK(p); 2296 2297 do_fullpath = true; 2298 *freebuf = NULL; 2299 if (dvp != NULL && binname[0] != '\0') { 2300 freepath_size = MAXPATHLEN; 2301 if (vn_fullpath_hardlink(vp, dvp, binname, strlen(binname), 2302 retbuf, freebuf, &freepath_size) == 0) { 2303 /* 2304 * Recheck the looked up path. The binary 2305 * might have been renamed or replaced, in 2306 * which case we should not report old name. 2307 */ 2308 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, *retbuf); 2309 error = namei(&nd); 2310 if (error == 0) { 2311 if (nd.ni_vp == vp) 2312 do_fullpath = false; 2313 vrele(nd.ni_vp); 2314 NDFREE_PNBUF(&nd); 2315 } 2316 } 2317 } 2318 if (do_fullpath) { 2319 free(*freebuf, M_TEMP); 2320 *freebuf = NULL; 2321 error = vn_fullpath(vp, retbuf, freebuf); 2322 } 2323 vrele(vp); 2324 if (dvp != NULL) 2325 vrele(dvp); 2326 return (error); 2327 } 2328 2329 /* 2330 * This sysctl allows a process to retrieve the path of the executable for 2331 * itself or another process. 2332 */ 2333 static int 2334 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 2335 { 2336 pid_t *pidp = (pid_t *)arg1; 2337 unsigned int arglen = arg2; 2338 struct proc *p; 2339 char *retbuf, *freebuf, *binname; 2340 int error; 2341 2342 if (arglen != 1) 2343 return (EINVAL); 2344 binname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 2345 binname[0] = '\0'; 2346 if (*pidp == -1) { /* -1 means this process */ 2347 error = 0; 2348 p = req->td->td_proc; 2349 PROC_LOCK(p); 2350 } else { 2351 error = pget(*pidp, PGET_CANSEE, &p); 2352 } 2353 2354 if (error == 0) 2355 error = proc_get_binpath(p, binname, &retbuf, &freebuf); 2356 free(binname, M_TEMP); 2357 if (error != 0) 2358 return (error); 2359 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 2360 free(freebuf, M_TEMP); 2361 return (error); 2362 } 2363 2364 static int 2365 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 2366 { 2367 struct proc *p; 2368 char *sv_name; 2369 int *name; 2370 int namelen; 2371 int error; 2372 2373 namelen = arg2; 2374 if (namelen != 1) 2375 return (EINVAL); 2376 2377 name = (int *)arg1; 2378 error = pget((pid_t)name[0], PGET_CANSEE, &p); 2379 if (error != 0) 2380 return (error); 2381 sv_name = p->p_sysent->sv_name; 2382 PROC_UNLOCK(p); 2383 return (sysctl_handle_string(oidp, sv_name, 0, req)); 2384 } 2385 2386 #ifdef KINFO_OVMENTRY_SIZE 2387 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 2388 #endif 2389 2390 #ifdef COMPAT_FREEBSD7 2391 static int 2392 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 2393 { 2394 vm_map_entry_t entry, tmp_entry; 2395 unsigned int last_timestamp, namelen; 2396 char *fullpath, *freepath; 2397 struct kinfo_ovmentry *kve; 2398 struct vattr va; 2399 struct ucred *cred; 2400 int error, *name; 2401 struct vnode *vp; 2402 struct proc *p; 2403 vm_map_t map; 2404 struct vmspace *vm; 2405 2406 namelen = arg2; 2407 if (namelen != 1) 2408 return (EINVAL); 2409 2410 name = (int *)arg1; 2411 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2412 if (error != 0) 2413 return (error); 2414 vm = vmspace_acquire_ref(p); 2415 if (vm == NULL) { 2416 PRELE(p); 2417 return (ESRCH); 2418 } 2419 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 2420 2421 map = &vm->vm_map; 2422 vm_map_lock_read(map); 2423 VM_MAP_ENTRY_FOREACH(entry, map) { 2424 vm_object_t obj, tobj, lobj; 2425 vm_offset_t addr; 2426 2427 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2428 continue; 2429 2430 bzero(kve, sizeof(*kve)); 2431 kve->kve_structsize = sizeof(*kve); 2432 2433 kve->kve_private_resident = 0; 2434 obj = entry->object.vm_object; 2435 if (obj != NULL) { 2436 VM_OBJECT_RLOCK(obj); 2437 if (obj->shadow_count == 1) 2438 kve->kve_private_resident = 2439 obj->resident_page_count; 2440 } 2441 kve->kve_resident = 0; 2442 addr = entry->start; 2443 while (addr < entry->end) { 2444 if (pmap_extract(map->pmap, addr)) 2445 kve->kve_resident++; 2446 addr += PAGE_SIZE; 2447 } 2448 2449 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 2450 if (tobj != obj) { 2451 VM_OBJECT_RLOCK(tobj); 2452 kve->kve_offset += tobj->backing_object_offset; 2453 } 2454 if (lobj != obj) 2455 VM_OBJECT_RUNLOCK(lobj); 2456 lobj = tobj; 2457 } 2458 2459 kve->kve_start = (void*)entry->start; 2460 kve->kve_end = (void*)entry->end; 2461 kve->kve_offset += (off_t)entry->offset; 2462 2463 if (entry->protection & VM_PROT_READ) 2464 kve->kve_protection |= KVME_PROT_READ; 2465 if (entry->protection & VM_PROT_WRITE) 2466 kve->kve_protection |= KVME_PROT_WRITE; 2467 if (entry->protection & VM_PROT_EXECUTE) 2468 kve->kve_protection |= KVME_PROT_EXEC; 2469 2470 if (entry->eflags & MAP_ENTRY_COW) 2471 kve->kve_flags |= KVME_FLAG_COW; 2472 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2473 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2474 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2475 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2476 2477 last_timestamp = map->timestamp; 2478 vm_map_unlock_read(map); 2479 2480 kve->kve_fileid = 0; 2481 kve->kve_fsid = 0; 2482 freepath = NULL; 2483 fullpath = ""; 2484 if (lobj) { 2485 kve->kve_type = vm_object_kvme_type(lobj, &vp); 2486 if (kve->kve_type == KVME_TYPE_MGTDEVICE) 2487 kve->kve_type = KVME_TYPE_UNKNOWN; 2488 if (vp != NULL) 2489 vref(vp); 2490 if (lobj != obj) 2491 VM_OBJECT_RUNLOCK(lobj); 2492 2493 kve->kve_ref_count = obj->ref_count; 2494 kve->kve_shadow_count = obj->shadow_count; 2495 VM_OBJECT_RUNLOCK(obj); 2496 if (vp != NULL) { 2497 vn_fullpath(vp, &fullpath, &freepath); 2498 cred = curthread->td_ucred; 2499 vn_lock(vp, LK_SHARED | LK_RETRY); 2500 if (VOP_GETATTR(vp, &va, cred) == 0) { 2501 kve->kve_fileid = va.va_fileid; 2502 /* truncate */ 2503 kve->kve_fsid = va.va_fsid; 2504 } 2505 vput(vp); 2506 } 2507 } else { 2508 kve->kve_type = KVME_TYPE_NONE; 2509 kve->kve_ref_count = 0; 2510 kve->kve_shadow_count = 0; 2511 } 2512 2513 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2514 if (freepath != NULL) 2515 free(freepath, M_TEMP); 2516 2517 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 2518 vm_map_lock_read(map); 2519 if (error) 2520 break; 2521 if (last_timestamp != map->timestamp) { 2522 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2523 entry = tmp_entry; 2524 } 2525 } 2526 vm_map_unlock_read(map); 2527 vmspace_free(vm); 2528 PRELE(p); 2529 free(kve, M_TEMP); 2530 return (error); 2531 } 2532 #endif /* COMPAT_FREEBSD7 */ 2533 2534 #ifdef KINFO_VMENTRY_SIZE 2535 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2536 #endif 2537 2538 void 2539 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry, 2540 int *resident_count, bool *super) 2541 { 2542 vm_object_t obj, tobj; 2543 vm_page_t m, m_adv; 2544 vm_offset_t addr; 2545 vm_paddr_t pa; 2546 vm_pindex_t pi, pi_adv, pindex; 2547 2548 *super = false; 2549 *resident_count = 0; 2550 if (vmmap_skip_res_cnt) 2551 return; 2552 2553 pa = 0; 2554 obj = entry->object.vm_object; 2555 addr = entry->start; 2556 m_adv = NULL; 2557 pi = OFF_TO_IDX(entry->offset); 2558 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) { 2559 if (m_adv != NULL) { 2560 m = m_adv; 2561 } else { 2562 pi_adv = atop(entry->end - addr); 2563 pindex = pi; 2564 for (tobj = obj;; tobj = tobj->backing_object) { 2565 m = vm_page_find_least(tobj, pindex); 2566 if (m != NULL) { 2567 if (m->pindex == pindex) 2568 break; 2569 if (pi_adv > m->pindex - pindex) { 2570 pi_adv = m->pindex - pindex; 2571 m_adv = m; 2572 } 2573 } 2574 if (tobj->backing_object == NULL) 2575 goto next; 2576 pindex += OFF_TO_IDX(tobj-> 2577 backing_object_offset); 2578 } 2579 } 2580 m_adv = NULL; 2581 if (m->psind != 0 && addr + pagesizes[1] <= entry->end && 2582 (addr & (pagesizes[1] - 1)) == 0 && 2583 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) { 2584 *super = true; 2585 pi_adv = atop(pagesizes[1]); 2586 } else { 2587 /* 2588 * We do not test the found page on validity. 2589 * Either the page is busy and being paged in, 2590 * or it was invalidated. The first case 2591 * should be counted as resident, the second 2592 * is not so clear; we do account both. 2593 */ 2594 pi_adv = 1; 2595 } 2596 *resident_count += pi_adv; 2597 next:; 2598 } 2599 } 2600 2601 /* 2602 * Must be called with the process locked and will return unlocked. 2603 */ 2604 int 2605 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags) 2606 { 2607 vm_map_entry_t entry, tmp_entry; 2608 struct vattr va; 2609 vm_map_t map; 2610 vm_object_t lobj, nobj, obj, tobj; 2611 char *fullpath, *freepath; 2612 struct kinfo_vmentry *kve; 2613 struct ucred *cred; 2614 struct vnode *vp; 2615 struct vmspace *vm; 2616 vm_offset_t addr; 2617 unsigned int last_timestamp; 2618 int error; 2619 bool guard, super; 2620 2621 PROC_LOCK_ASSERT(p, MA_OWNED); 2622 2623 _PHOLD(p); 2624 PROC_UNLOCK(p); 2625 vm = vmspace_acquire_ref(p); 2626 if (vm == NULL) { 2627 PRELE(p); 2628 return (ESRCH); 2629 } 2630 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO); 2631 2632 error = 0; 2633 map = &vm->vm_map; 2634 vm_map_lock_read(map); 2635 VM_MAP_ENTRY_FOREACH(entry, map) { 2636 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2637 continue; 2638 2639 addr = entry->end; 2640 bzero(kve, sizeof(*kve)); 2641 obj = entry->object.vm_object; 2642 if (obj != NULL) { 2643 if ((obj->flags & OBJ_ANON) != 0) 2644 kve->kve_obj = (uintptr_t)obj; 2645 2646 for (tobj = obj; tobj != NULL; 2647 tobj = tobj->backing_object) { 2648 VM_OBJECT_RLOCK(tobj); 2649 kve->kve_offset += tobj->backing_object_offset; 2650 lobj = tobj; 2651 } 2652 if (obj->backing_object == NULL) 2653 kve->kve_private_resident = 2654 obj->resident_page_count; 2655 kern_proc_vmmap_resident(map, entry, 2656 &kve->kve_resident, &super); 2657 if (super) 2658 kve->kve_flags |= KVME_FLAG_SUPER; 2659 for (tobj = obj; tobj != NULL; tobj = nobj) { 2660 nobj = tobj->backing_object; 2661 if (tobj != obj && tobj != lobj) 2662 VM_OBJECT_RUNLOCK(tobj); 2663 } 2664 } else { 2665 lobj = NULL; 2666 } 2667 2668 kve->kve_start = entry->start; 2669 kve->kve_end = entry->end; 2670 kve->kve_offset += entry->offset; 2671 2672 if (entry->protection & VM_PROT_READ) 2673 kve->kve_protection |= KVME_PROT_READ; 2674 if (entry->protection & VM_PROT_WRITE) 2675 kve->kve_protection |= KVME_PROT_WRITE; 2676 if (entry->protection & VM_PROT_EXECUTE) 2677 kve->kve_protection |= KVME_PROT_EXEC; 2678 2679 if (entry->eflags & MAP_ENTRY_COW) 2680 kve->kve_flags |= KVME_FLAG_COW; 2681 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2682 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2683 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2684 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2685 if (entry->eflags & MAP_ENTRY_GROWS_UP) 2686 kve->kve_flags |= KVME_FLAG_GROWS_UP; 2687 if (entry->eflags & MAP_ENTRY_GROWS_DOWN) 2688 kve->kve_flags |= KVME_FLAG_GROWS_DOWN; 2689 if (entry->eflags & MAP_ENTRY_USER_WIRED) 2690 kve->kve_flags |= KVME_FLAG_USER_WIRED; 2691 2692 guard = (entry->eflags & MAP_ENTRY_GUARD) != 0; 2693 2694 last_timestamp = map->timestamp; 2695 vm_map_unlock_read(map); 2696 2697 freepath = NULL; 2698 fullpath = ""; 2699 if (lobj != NULL) { 2700 kve->kve_type = vm_object_kvme_type(lobj, &vp); 2701 if (vp != NULL) 2702 vref(vp); 2703 if (lobj != obj) 2704 VM_OBJECT_RUNLOCK(lobj); 2705 2706 kve->kve_ref_count = obj->ref_count; 2707 kve->kve_shadow_count = obj->shadow_count; 2708 VM_OBJECT_RUNLOCK(obj); 2709 if (vp != NULL) { 2710 vn_fullpath(vp, &fullpath, &freepath); 2711 kve->kve_vn_type = vntype_to_kinfo(vp->v_type); 2712 cred = curthread->td_ucred; 2713 vn_lock(vp, LK_SHARED | LK_RETRY); 2714 if (VOP_GETATTR(vp, &va, cred) == 0) { 2715 kve->kve_vn_fileid = va.va_fileid; 2716 kve->kve_vn_fsid = va.va_fsid; 2717 kve->kve_vn_fsid_freebsd11 = 2718 kve->kve_vn_fsid; /* truncate */ 2719 kve->kve_vn_mode = 2720 MAKEIMODE(va.va_type, va.va_mode); 2721 kve->kve_vn_size = va.va_size; 2722 kve->kve_vn_rdev = va.va_rdev; 2723 kve->kve_vn_rdev_freebsd11 = 2724 kve->kve_vn_rdev; /* truncate */ 2725 kve->kve_status = KF_ATTR_VALID; 2726 } 2727 vput(vp); 2728 } 2729 } else { 2730 kve->kve_type = guard ? KVME_TYPE_GUARD : 2731 KVME_TYPE_NONE; 2732 kve->kve_ref_count = 0; 2733 kve->kve_shadow_count = 0; 2734 } 2735 2736 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2737 if (freepath != NULL) 2738 free(freepath, M_TEMP); 2739 2740 /* Pack record size down */ 2741 if ((flags & KERN_VMMAP_PACK_KINFO) != 0) 2742 kve->kve_structsize = 2743 offsetof(struct kinfo_vmentry, kve_path) + 2744 strlen(kve->kve_path) + 1; 2745 else 2746 kve->kve_structsize = sizeof(*kve); 2747 kve->kve_structsize = roundup(kve->kve_structsize, 2748 sizeof(uint64_t)); 2749 2750 /* Halt filling and truncate rather than exceeding maxlen */ 2751 if (maxlen != -1 && maxlen < kve->kve_structsize) { 2752 error = 0; 2753 vm_map_lock_read(map); 2754 break; 2755 } else if (maxlen != -1) 2756 maxlen -= kve->kve_structsize; 2757 2758 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0) 2759 error = ENOMEM; 2760 vm_map_lock_read(map); 2761 if (error != 0) 2762 break; 2763 if (last_timestamp != map->timestamp) { 2764 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2765 entry = tmp_entry; 2766 } 2767 } 2768 vm_map_unlock_read(map); 2769 vmspace_free(vm); 2770 PRELE(p); 2771 free(kve, M_TEMP); 2772 return (error); 2773 } 2774 2775 static int 2776 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 2777 { 2778 struct proc *p; 2779 struct sbuf sb; 2780 u_int namelen; 2781 int error, error2, *name; 2782 2783 namelen = arg2; 2784 if (namelen != 1) 2785 return (EINVAL); 2786 2787 name = (int *)arg1; 2788 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req); 2789 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 2790 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p); 2791 if (error != 0) { 2792 sbuf_delete(&sb); 2793 return (error); 2794 } 2795 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO); 2796 error2 = sbuf_finish(&sb); 2797 sbuf_delete(&sb); 2798 return (error != 0 ? error : error2); 2799 } 2800 2801 #if defined(STACK) || defined(DDB) 2802 static int 2803 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 2804 { 2805 struct kinfo_kstack *kkstp; 2806 int error, i, *name, numthreads; 2807 lwpid_t *lwpidarray; 2808 struct thread *td; 2809 struct stack *st; 2810 struct sbuf sb; 2811 struct proc *p; 2812 u_int namelen; 2813 2814 namelen = arg2; 2815 if (namelen != 1) 2816 return (EINVAL); 2817 2818 name = (int *)arg1; 2819 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p); 2820 if (error != 0) 2821 return (error); 2822 2823 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 2824 st = stack_create(M_WAITOK); 2825 2826 lwpidarray = NULL; 2827 PROC_LOCK(p); 2828 do { 2829 if (lwpidarray != NULL) { 2830 free(lwpidarray, M_TEMP); 2831 lwpidarray = NULL; 2832 } 2833 numthreads = p->p_numthreads; 2834 PROC_UNLOCK(p); 2835 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 2836 M_WAITOK | M_ZERO); 2837 PROC_LOCK(p); 2838 } while (numthreads < p->p_numthreads); 2839 2840 /* 2841 * XXXRW: During the below loop, execve(2) and countless other sorts 2842 * of changes could have taken place. Should we check to see if the 2843 * vmspace has been replaced, or the like, in order to prevent 2844 * giving a snapshot that spans, say, execve(2), with some threads 2845 * before and some after? Among other things, the credentials could 2846 * have changed, in which case the right to extract debug info might 2847 * no longer be assured. 2848 */ 2849 i = 0; 2850 FOREACH_THREAD_IN_PROC(p, td) { 2851 KASSERT(i < numthreads, 2852 ("sysctl_kern_proc_kstack: numthreads")); 2853 lwpidarray[i] = td->td_tid; 2854 i++; 2855 } 2856 PROC_UNLOCK(p); 2857 numthreads = i; 2858 for (i = 0; i < numthreads; i++) { 2859 td = tdfind(lwpidarray[i], p->p_pid); 2860 if (td == NULL) { 2861 continue; 2862 } 2863 bzero(kkstp, sizeof(*kkstp)); 2864 (void)sbuf_new(&sb, kkstp->kkst_trace, 2865 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 2866 thread_lock(td); 2867 kkstp->kkst_tid = td->td_tid; 2868 if (TD_IS_SWAPPED(td)) 2869 kkstp->kkst_state = KKST_STATE_SWAPPED; 2870 else if (stack_save_td(st, td) == 0) 2871 kkstp->kkst_state = KKST_STATE_STACKOK; 2872 else 2873 kkstp->kkst_state = KKST_STATE_RUNNING; 2874 thread_unlock(td); 2875 PROC_UNLOCK(p); 2876 stack_sbuf_print(&sb, st); 2877 sbuf_finish(&sb); 2878 sbuf_delete(&sb); 2879 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 2880 if (error) 2881 break; 2882 } 2883 PRELE(p); 2884 if (lwpidarray != NULL) 2885 free(lwpidarray, M_TEMP); 2886 stack_destroy(st); 2887 free(kkstp, M_TEMP); 2888 return (error); 2889 } 2890 #endif 2891 2892 /* 2893 * This sysctl allows a process to retrieve the full list of groups from 2894 * itself or another process. 2895 */ 2896 static int 2897 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 2898 { 2899 pid_t *pidp = (pid_t *)arg1; 2900 unsigned int arglen = arg2; 2901 struct proc *p; 2902 struct ucred *cred; 2903 int error; 2904 2905 if (arglen != 1) 2906 return (EINVAL); 2907 if (*pidp == -1) { /* -1 means this process */ 2908 p = req->td->td_proc; 2909 PROC_LOCK(p); 2910 } else { 2911 error = pget(*pidp, PGET_CANSEE, &p); 2912 if (error != 0) 2913 return (error); 2914 } 2915 2916 cred = crhold(p->p_ucred); 2917 PROC_UNLOCK(p); 2918 2919 error = SYSCTL_OUT(req, cred->cr_groups, 2920 cred->cr_ngroups * sizeof(gid_t)); 2921 crfree(cred); 2922 return (error); 2923 } 2924 2925 /* 2926 * This sysctl allows a process to retrieve or/and set the resource limit for 2927 * another process. 2928 */ 2929 static int 2930 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS) 2931 { 2932 int *name = (int *)arg1; 2933 u_int namelen = arg2; 2934 struct rlimit rlim; 2935 struct proc *p; 2936 u_int which; 2937 int flags, error; 2938 2939 if (namelen != 2) 2940 return (EINVAL); 2941 2942 which = (u_int)name[1]; 2943 if (which >= RLIM_NLIMITS) 2944 return (EINVAL); 2945 2946 if (req->newptr != NULL && req->newlen != sizeof(rlim)) 2947 return (EINVAL); 2948 2949 flags = PGET_HOLD | PGET_NOTWEXIT; 2950 if (req->newptr != NULL) 2951 flags |= PGET_CANDEBUG; 2952 else 2953 flags |= PGET_CANSEE; 2954 error = pget((pid_t)name[0], flags, &p); 2955 if (error != 0) 2956 return (error); 2957 2958 /* 2959 * Retrieve limit. 2960 */ 2961 if (req->oldptr != NULL) { 2962 PROC_LOCK(p); 2963 lim_rlimit_proc(p, which, &rlim); 2964 PROC_UNLOCK(p); 2965 } 2966 error = SYSCTL_OUT(req, &rlim, sizeof(rlim)); 2967 if (error != 0) 2968 goto errout; 2969 2970 /* 2971 * Set limit. 2972 */ 2973 if (req->newptr != NULL) { 2974 error = SYSCTL_IN(req, &rlim, sizeof(rlim)); 2975 if (error == 0) 2976 error = kern_proc_setrlimit(curthread, p, which, &rlim); 2977 } 2978 2979 errout: 2980 PRELE(p); 2981 return (error); 2982 } 2983 2984 /* 2985 * This sysctl allows a process to retrieve ps_strings structure location of 2986 * another process. 2987 */ 2988 static int 2989 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS) 2990 { 2991 int *name = (int *)arg1; 2992 u_int namelen = arg2; 2993 struct proc *p; 2994 vm_offset_t ps_strings; 2995 int error; 2996 #ifdef COMPAT_FREEBSD32 2997 uint32_t ps_strings32; 2998 #endif 2999 3000 if (namelen != 1) 3001 return (EINVAL); 3002 3003 error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 3004 if (error != 0) 3005 return (error); 3006 #ifdef COMPAT_FREEBSD32 3007 if ((req->flags & SCTL_MASK32) != 0) { 3008 /* 3009 * We return 0 if the 32 bit emulation request is for a 64 bit 3010 * process. 3011 */ 3012 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ? 3013 PTROUT(PROC_PS_STRINGS(p)) : 0; 3014 PROC_UNLOCK(p); 3015 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32)); 3016 return (error); 3017 } 3018 #endif 3019 ps_strings = PROC_PS_STRINGS(p); 3020 PROC_UNLOCK(p); 3021 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings)); 3022 return (error); 3023 } 3024 3025 /* 3026 * This sysctl allows a process to retrieve umask of another process. 3027 */ 3028 static int 3029 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS) 3030 { 3031 int *name = (int *)arg1; 3032 u_int namelen = arg2; 3033 struct proc *p; 3034 int error; 3035 u_short cmask; 3036 pid_t pid; 3037 3038 if (namelen != 1) 3039 return (EINVAL); 3040 3041 pid = (pid_t)name[0]; 3042 p = curproc; 3043 if (pid == p->p_pid || pid == 0) { 3044 cmask = p->p_pd->pd_cmask; 3045 goto out; 3046 } 3047 3048 error = pget(pid, PGET_WANTREAD, &p); 3049 if (error != 0) 3050 return (error); 3051 3052 cmask = p->p_pd->pd_cmask; 3053 PRELE(p); 3054 out: 3055 error = SYSCTL_OUT(req, &cmask, sizeof(cmask)); 3056 return (error); 3057 } 3058 3059 /* 3060 * This sysctl allows a process to set and retrieve binary osreldate of 3061 * another process. 3062 */ 3063 static int 3064 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS) 3065 { 3066 int *name = (int *)arg1; 3067 u_int namelen = arg2; 3068 struct proc *p; 3069 int flags, error, osrel; 3070 3071 if (namelen != 1) 3072 return (EINVAL); 3073 3074 if (req->newptr != NULL && req->newlen != sizeof(osrel)) 3075 return (EINVAL); 3076 3077 flags = PGET_HOLD | PGET_NOTWEXIT; 3078 if (req->newptr != NULL) 3079 flags |= PGET_CANDEBUG; 3080 else 3081 flags |= PGET_CANSEE; 3082 error = pget((pid_t)name[0], flags, &p); 3083 if (error != 0) 3084 return (error); 3085 3086 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel)); 3087 if (error != 0) 3088 goto errout; 3089 3090 if (req->newptr != NULL) { 3091 error = SYSCTL_IN(req, &osrel, sizeof(osrel)); 3092 if (error != 0) 3093 goto errout; 3094 if (osrel < 0) { 3095 error = EINVAL; 3096 goto errout; 3097 } 3098 p->p_osrel = osrel; 3099 } 3100 errout: 3101 PRELE(p); 3102 return (error); 3103 } 3104 3105 static int 3106 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS) 3107 { 3108 int *name = (int *)arg1; 3109 u_int namelen = arg2; 3110 struct proc *p; 3111 struct kinfo_sigtramp kst; 3112 const struct sysentvec *sv; 3113 int error; 3114 #ifdef COMPAT_FREEBSD32 3115 struct kinfo_sigtramp32 kst32; 3116 #endif 3117 3118 if (namelen != 1) 3119 return (EINVAL); 3120 3121 error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 3122 if (error != 0) 3123 return (error); 3124 sv = p->p_sysent; 3125 #ifdef COMPAT_FREEBSD32 3126 if ((req->flags & SCTL_MASK32) != 0) { 3127 bzero(&kst32, sizeof(kst32)); 3128 if (SV_PROC_FLAG(p, SV_ILP32)) { 3129 if (PROC_HAS_SHP(p)) { 3130 kst32.ksigtramp_start = PROC_SIGCODE(p); 3131 kst32.ksigtramp_end = kst32.ksigtramp_start + 3132 ((sv->sv_flags & SV_DSO_SIG) == 0 ? 3133 *sv->sv_szsigcode : 3134 (uintptr_t)sv->sv_szsigcode); 3135 } else { 3136 kst32.ksigtramp_start = PROC_PS_STRINGS(p) - 3137 *sv->sv_szsigcode; 3138 kst32.ksigtramp_end = PROC_PS_STRINGS(p); 3139 } 3140 } 3141 PROC_UNLOCK(p); 3142 error = SYSCTL_OUT(req, &kst32, sizeof(kst32)); 3143 return (error); 3144 } 3145 #endif 3146 bzero(&kst, sizeof(kst)); 3147 if (PROC_HAS_SHP(p)) { 3148 kst.ksigtramp_start = (char *)PROC_SIGCODE(p); 3149 kst.ksigtramp_end = (char *)kst.ksigtramp_start + 3150 ((sv->sv_flags & SV_DSO_SIG) == 0 ? *sv->sv_szsigcode : 3151 (uintptr_t)sv->sv_szsigcode); 3152 } else { 3153 kst.ksigtramp_start = (char *)PROC_PS_STRINGS(p) - 3154 *sv->sv_szsigcode; 3155 kst.ksigtramp_end = (char *)PROC_PS_STRINGS(p); 3156 } 3157 PROC_UNLOCK(p); 3158 error = SYSCTL_OUT(req, &kst, sizeof(kst)); 3159 return (error); 3160 } 3161 3162 static int 3163 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS) 3164 { 3165 int *name = (int *)arg1; 3166 u_int namelen = arg2; 3167 pid_t pid; 3168 struct proc *p; 3169 struct thread *td1; 3170 uintptr_t addr; 3171 #ifdef COMPAT_FREEBSD32 3172 uint32_t addr32; 3173 #endif 3174 int error; 3175 3176 if (namelen != 1 || req->newptr != NULL) 3177 return (EINVAL); 3178 3179 pid = (pid_t)name[0]; 3180 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p); 3181 if (error != 0) 3182 return (error); 3183 3184 PROC_LOCK(p); 3185 #ifdef COMPAT_FREEBSD32 3186 if (SV_CURPROC_FLAG(SV_ILP32)) { 3187 if (!SV_PROC_FLAG(p, SV_ILP32)) { 3188 error = EINVAL; 3189 goto errlocked; 3190 } 3191 } 3192 #endif 3193 if (pid <= PID_MAX) { 3194 td1 = FIRST_THREAD_IN_PROC(p); 3195 } else { 3196 FOREACH_THREAD_IN_PROC(p, td1) { 3197 if (td1->td_tid == pid) 3198 break; 3199 } 3200 } 3201 if (td1 == NULL) { 3202 error = ESRCH; 3203 goto errlocked; 3204 } 3205 /* 3206 * The access to the private thread flags. It is fine as far 3207 * as no out-of-thin-air values are read from td_pflags, and 3208 * usermode read of the td_sigblock_ptr is racy inherently, 3209 * since target process might have already changed it 3210 * meantime. 3211 */ 3212 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0) 3213 addr = (uintptr_t)td1->td_sigblock_ptr; 3214 else 3215 error = ENOTTY; 3216 3217 errlocked: 3218 _PRELE(p); 3219 PROC_UNLOCK(p); 3220 if (error != 0) 3221 return (error); 3222 3223 #ifdef COMPAT_FREEBSD32 3224 if (SV_CURPROC_FLAG(SV_ILP32)) { 3225 addr32 = addr; 3226 error = SYSCTL_OUT(req, &addr32, sizeof(addr32)); 3227 } else 3228 #endif 3229 error = SYSCTL_OUT(req, &addr, sizeof(addr)); 3230 return (error); 3231 } 3232 3233 static int 3234 sysctl_kern_proc_vm_layout(SYSCTL_HANDLER_ARGS) 3235 { 3236 struct kinfo_vm_layout kvm; 3237 struct proc *p; 3238 struct vmspace *vmspace; 3239 int error, *name; 3240 3241 name = (int *)arg1; 3242 if ((u_int)arg2 != 1) 3243 return (EINVAL); 3244 3245 error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 3246 if (error != 0) 3247 return (error); 3248 #ifdef COMPAT_FREEBSD32 3249 if (SV_CURPROC_FLAG(SV_ILP32)) { 3250 if (!SV_PROC_FLAG(p, SV_ILP32)) { 3251 PROC_UNLOCK(p); 3252 return (EINVAL); 3253 } 3254 } 3255 #endif 3256 vmspace = vmspace_acquire_ref(p); 3257 PROC_UNLOCK(p); 3258 3259 memset(&kvm, 0, sizeof(kvm)); 3260 kvm.kvm_min_user_addr = vm_map_min(&vmspace->vm_map); 3261 kvm.kvm_max_user_addr = vm_map_max(&vmspace->vm_map); 3262 kvm.kvm_text_addr = (uintptr_t)vmspace->vm_taddr; 3263 kvm.kvm_text_size = vmspace->vm_tsize; 3264 kvm.kvm_data_addr = (uintptr_t)vmspace->vm_daddr; 3265 kvm.kvm_data_size = vmspace->vm_dsize; 3266 kvm.kvm_stack_addr = (uintptr_t)vmspace->vm_maxsaddr; 3267 kvm.kvm_stack_size = vmspace->vm_ssize; 3268 kvm.kvm_shp_addr = vmspace->vm_shp_base; 3269 kvm.kvm_shp_size = p->p_sysent->sv_shared_page_len; 3270 if ((vmspace->vm_map.flags & MAP_WIREFUTURE) != 0) 3271 kvm.kvm_map_flags |= KMAP_FLAG_WIREFUTURE; 3272 if ((vmspace->vm_map.flags & MAP_ASLR) != 0) 3273 kvm.kvm_map_flags |= KMAP_FLAG_ASLR; 3274 if ((vmspace->vm_map.flags & MAP_ASLR_IGNSTART) != 0) 3275 kvm.kvm_map_flags |= KMAP_FLAG_ASLR_IGNSTART; 3276 if ((vmspace->vm_map.flags & MAP_WXORX) != 0) 3277 kvm.kvm_map_flags |= KMAP_FLAG_WXORX; 3278 if ((vmspace->vm_map.flags & MAP_ASLR_STACK) != 0) 3279 kvm.kvm_map_flags |= KMAP_FLAG_ASLR_STACK; 3280 if (vmspace->vm_shp_base != p->p_sysent->sv_shared_page_base && 3281 PROC_HAS_SHP(p)) 3282 kvm.kvm_map_flags |= KMAP_FLAG_ASLR_SHARED_PAGE; 3283 3284 #ifdef COMPAT_FREEBSD32 3285 if (SV_CURPROC_FLAG(SV_ILP32)) { 3286 struct kinfo_vm_layout32 kvm32; 3287 3288 memset(&kvm32, 0, sizeof(kvm32)); 3289 kvm32.kvm_min_user_addr = (uint32_t)kvm.kvm_min_user_addr; 3290 kvm32.kvm_max_user_addr = (uint32_t)kvm.kvm_max_user_addr; 3291 kvm32.kvm_text_addr = (uint32_t)kvm.kvm_text_addr; 3292 kvm32.kvm_text_size = (uint32_t)kvm.kvm_text_size; 3293 kvm32.kvm_data_addr = (uint32_t)kvm.kvm_data_addr; 3294 kvm32.kvm_data_size = (uint32_t)kvm.kvm_data_size; 3295 kvm32.kvm_stack_addr = (uint32_t)kvm.kvm_stack_addr; 3296 kvm32.kvm_stack_size = (uint32_t)kvm.kvm_stack_size; 3297 kvm32.kvm_shp_addr = (uint32_t)kvm.kvm_shp_addr; 3298 kvm32.kvm_shp_size = (uint32_t)kvm.kvm_shp_size; 3299 kvm32.kvm_map_flags = kvm.kvm_map_flags; 3300 error = SYSCTL_OUT(req, &kvm32, sizeof(kvm32)); 3301 goto out; 3302 } 3303 #endif 3304 3305 error = SYSCTL_OUT(req, &kvm, sizeof(kvm)); 3306 #ifdef COMPAT_FREEBSD32 3307 out: 3308 #endif 3309 vmspace_free(vmspace); 3310 return (error); 3311 } 3312 3313 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 3314 "Process table"); 3315 3316 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 3317 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 3318 "Return entire process table"); 3319 3320 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 3321 sysctl_kern_proc, "Process table"); 3322 3323 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 3324 sysctl_kern_proc, "Process table"); 3325 3326 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 3327 sysctl_kern_proc, "Process table"); 3328 3329 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 3330 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3331 3332 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 3333 sysctl_kern_proc, "Process table"); 3334 3335 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 3336 sysctl_kern_proc, "Process table"); 3337 3338 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 3339 sysctl_kern_proc, "Process table"); 3340 3341 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 3342 sysctl_kern_proc, "Process table"); 3343 3344 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 3345 sysctl_kern_proc, "Return process table, no threads"); 3346 3347 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 3348 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 3349 sysctl_kern_proc_args, "Process argument list"); 3350 3351 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE, 3352 sysctl_kern_proc_env, "Process environment"); 3353 3354 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD | 3355 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector"); 3356 3357 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 3358 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 3359 3360 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 3361 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 3362 "Process syscall vector name (ABI type)"); 3363 3364 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 3365 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3366 3367 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 3368 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3369 3370 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 3371 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3372 3373 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 3374 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3375 3376 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 3377 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3378 3379 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 3380 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3381 3382 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 3383 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3384 3385 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 3386 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 3387 3388 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 3389 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 3390 "Return process table, including threads"); 3391 3392 #ifdef COMPAT_FREEBSD7 3393 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 3394 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 3395 #endif 3396 3397 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 3398 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 3399 3400 #if defined(STACK) || defined(DDB) 3401 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 3402 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 3403 #endif 3404 3405 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 3406 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 3407 3408 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW | 3409 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, 3410 "Process resource limits"); 3411 3412 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD | 3413 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings, 3414 "Process ps_strings location"); 3415 3416 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD | 3417 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask"); 3418 3419 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW | 3420 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel, 3421 "Process binary osreldate"); 3422 3423 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD | 3424 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp, 3425 "Process signal trampoline location"); 3426 3427 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD | 3428 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk, 3429 "Thread sigfastblock address"); 3430 3431 static SYSCTL_NODE(_kern_proc, KERN_PROC_VM_LAYOUT, vm_layout, CTLFLAG_RD | 3432 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_vm_layout, 3433 "Process virtual address space layout info"); 3434 3435 static struct sx stop_all_proc_blocker; 3436 SX_SYSINIT(stop_all_proc_blocker, &stop_all_proc_blocker, "sapblk"); 3437 3438 bool 3439 stop_all_proc_block(void) 3440 { 3441 return (sx_xlock_sig(&stop_all_proc_blocker) == 0); 3442 } 3443 3444 void 3445 stop_all_proc_unblock(void) 3446 { 3447 sx_xunlock(&stop_all_proc_blocker); 3448 } 3449 3450 int allproc_gen; 3451 3452 /* 3453 * stop_all_proc() purpose is to stop all process which have usermode, 3454 * except current process for obvious reasons. This makes it somewhat 3455 * unreliable when invoked from multithreaded process. The service 3456 * must not be user-callable anyway. 3457 */ 3458 void 3459 stop_all_proc(void) 3460 { 3461 struct proc *cp, *p; 3462 int r, gen; 3463 bool restart, seen_stopped, seen_exiting, stopped_some; 3464 3465 if (!stop_all_proc_block()) 3466 return; 3467 3468 cp = curproc; 3469 allproc_loop: 3470 sx_xlock(&allproc_lock); 3471 gen = allproc_gen; 3472 seen_exiting = seen_stopped = stopped_some = restart = false; 3473 LIST_REMOVE(cp, p_list); 3474 LIST_INSERT_HEAD(&allproc, cp, p_list); 3475 for (;;) { 3476 p = LIST_NEXT(cp, p_list); 3477 if (p == NULL) 3478 break; 3479 LIST_REMOVE(cp, p_list); 3480 LIST_INSERT_AFTER(p, cp, p_list); 3481 PROC_LOCK(p); 3482 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) { 3483 PROC_UNLOCK(p); 3484 continue; 3485 } 3486 if ((p->p_flag2 & P2_WEXIT) != 0) { 3487 seen_exiting = true; 3488 PROC_UNLOCK(p); 3489 continue; 3490 } 3491 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 3492 /* 3493 * Stopped processes are tolerated when there 3494 * are no other processes which might continue 3495 * them. P_STOPPED_SINGLE but not 3496 * P_TOTAL_STOP process still has at least one 3497 * thread running. 3498 */ 3499 seen_stopped = true; 3500 PROC_UNLOCK(p); 3501 continue; 3502 } 3503 sx_xunlock(&allproc_lock); 3504 _PHOLD(p); 3505 r = thread_single(p, SINGLE_ALLPROC); 3506 if (r != 0) 3507 restart = true; 3508 else 3509 stopped_some = true; 3510 _PRELE(p); 3511 PROC_UNLOCK(p); 3512 sx_xlock(&allproc_lock); 3513 } 3514 /* Catch forked children we did not see in iteration. */ 3515 if (gen != allproc_gen) 3516 restart = true; 3517 sx_xunlock(&allproc_lock); 3518 if (restart || stopped_some || seen_exiting || seen_stopped) { 3519 kern_yield(PRI_USER); 3520 goto allproc_loop; 3521 } 3522 } 3523 3524 void 3525 resume_all_proc(void) 3526 { 3527 struct proc *cp, *p; 3528 3529 cp = curproc; 3530 sx_xlock(&allproc_lock); 3531 again: 3532 LIST_REMOVE(cp, p_list); 3533 LIST_INSERT_HEAD(&allproc, cp, p_list); 3534 for (;;) { 3535 p = LIST_NEXT(cp, p_list); 3536 if (p == NULL) 3537 break; 3538 LIST_REMOVE(cp, p_list); 3539 LIST_INSERT_AFTER(p, cp, p_list); 3540 PROC_LOCK(p); 3541 if ((p->p_flag & P_TOTAL_STOP) != 0) { 3542 sx_xunlock(&allproc_lock); 3543 _PHOLD(p); 3544 thread_single_end(p, SINGLE_ALLPROC); 3545 _PRELE(p); 3546 PROC_UNLOCK(p); 3547 sx_xlock(&allproc_lock); 3548 } else { 3549 PROC_UNLOCK(p); 3550 } 3551 } 3552 /* Did the loop above missed any stopped process ? */ 3553 FOREACH_PROC_IN_SYSTEM(p) { 3554 /* No need for proc lock. */ 3555 if ((p->p_flag & P_TOTAL_STOP) != 0) 3556 goto again; 3557 } 3558 sx_xunlock(&allproc_lock); 3559 3560 stop_all_proc_unblock(); 3561 } 3562 3563 /* #define TOTAL_STOP_DEBUG 1 */ 3564 #ifdef TOTAL_STOP_DEBUG 3565 volatile static int ap_resume; 3566 #include <sys/mount.h> 3567 3568 static int 3569 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS) 3570 { 3571 int error, val; 3572 3573 val = 0; 3574 ap_resume = 0; 3575 error = sysctl_handle_int(oidp, &val, 0, req); 3576 if (error != 0 || req->newptr == NULL) 3577 return (error); 3578 if (val != 0) { 3579 stop_all_proc(); 3580 syncer_suspend(); 3581 while (ap_resume == 0) 3582 ; 3583 syncer_resume(); 3584 resume_all_proc(); 3585 } 3586 return (0); 3587 } 3588 3589 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW | 3590 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0, 3591 sysctl_debug_stop_all_proc, "I", 3592 ""); 3593 #endif 3594