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