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