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