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