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