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