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