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 error = kern_proc_out(p, &sb, flags); 1277 error2 = sbuf_finish(&sb); 1278 sbuf_delete(&sb); 1279 if (error != 0) 1280 return (error); 1281 else if (error2 != 0) 1282 return (error2); 1283 if (doingzomb) 1284 np = zpfind(pid); 1285 else { 1286 if (pid == 0) 1287 return (0); 1288 np = pfind(pid); 1289 } 1290 if (np == NULL) 1291 return (ESRCH); 1292 if (np != p) { 1293 PROC_UNLOCK(np); 1294 return (ESRCH); 1295 } 1296 PROC_UNLOCK(np); 1297 return (0); 1298 } 1299 1300 static int 1301 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1302 { 1303 int *name = (int *)arg1; 1304 u_int namelen = arg2; 1305 struct proc *p; 1306 int flags, doingzomb, oid_number; 1307 int error = 0; 1308 1309 oid_number = oidp->oid_number; 1310 if (oid_number != KERN_PROC_ALL && 1311 (oid_number & KERN_PROC_INC_THREAD) == 0) 1312 flags = KERN_PROC_NOTHREADS; 1313 else { 1314 flags = 0; 1315 oid_number &= ~KERN_PROC_INC_THREAD; 1316 } 1317 #ifdef COMPAT_FREEBSD32 1318 if (req->flags & SCTL_MASK32) 1319 flags |= KERN_PROC_MASK32; 1320 #endif 1321 if (oid_number == KERN_PROC_PID) { 1322 if (namelen != 1) 1323 return (EINVAL); 1324 error = sysctl_wire_old_buffer(req, 0); 1325 if (error) 1326 return (error); 1327 sx_slock(&proctree_lock); 1328 error = pget((pid_t)name[0], PGET_CANSEE, &p); 1329 if (error == 0) 1330 error = sysctl_out_proc(p, req, flags, 0); 1331 sx_sunlock(&proctree_lock); 1332 return (error); 1333 } 1334 1335 switch (oid_number) { 1336 case KERN_PROC_ALL: 1337 if (namelen != 0) 1338 return (EINVAL); 1339 break; 1340 case KERN_PROC_PROC: 1341 if (namelen != 0 && namelen != 1) 1342 return (EINVAL); 1343 break; 1344 default: 1345 if (namelen != 1) 1346 return (EINVAL); 1347 break; 1348 } 1349 1350 if (!req->oldptr) { 1351 /* overestimate by 5 procs */ 1352 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1353 if (error) 1354 return (error); 1355 } 1356 error = sysctl_wire_old_buffer(req, 0); 1357 if (error != 0) 1358 return (error); 1359 sx_slock(&proctree_lock); 1360 sx_slock(&allproc_lock); 1361 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1362 if (!doingzomb) 1363 p = LIST_FIRST(&allproc); 1364 else 1365 p = LIST_FIRST(&zombproc); 1366 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1367 /* 1368 * Skip embryonic processes. 1369 */ 1370 PROC_LOCK(p); 1371 if (p->p_state == PRS_NEW) { 1372 PROC_UNLOCK(p); 1373 continue; 1374 } 1375 KASSERT(p->p_ucred != NULL, 1376 ("process credential is NULL for non-NEW proc")); 1377 /* 1378 * Show a user only appropriate processes. 1379 */ 1380 if (p_cansee(curthread, p)) { 1381 PROC_UNLOCK(p); 1382 continue; 1383 } 1384 /* 1385 * TODO - make more efficient (see notes below). 1386 * do by session. 1387 */ 1388 switch (oid_number) { 1389 1390 case KERN_PROC_GID: 1391 if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1392 PROC_UNLOCK(p); 1393 continue; 1394 } 1395 break; 1396 1397 case KERN_PROC_PGRP: 1398 /* could do this by traversing pgrp */ 1399 if (p->p_pgrp == NULL || 1400 p->p_pgrp->pg_id != (pid_t)name[0]) { 1401 PROC_UNLOCK(p); 1402 continue; 1403 } 1404 break; 1405 1406 case KERN_PROC_RGID: 1407 if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1408 PROC_UNLOCK(p); 1409 continue; 1410 } 1411 break; 1412 1413 case KERN_PROC_SESSION: 1414 if (p->p_session == NULL || 1415 p->p_session->s_sid != (pid_t)name[0]) { 1416 PROC_UNLOCK(p); 1417 continue; 1418 } 1419 break; 1420 1421 case KERN_PROC_TTY: 1422 if ((p->p_flag & P_CONTROLT) == 0 || 1423 p->p_session == NULL) { 1424 PROC_UNLOCK(p); 1425 continue; 1426 } 1427 /* XXX proctree_lock */ 1428 SESS_LOCK(p->p_session); 1429 if (p->p_session->s_ttyp == NULL || 1430 tty_udev(p->p_session->s_ttyp) != 1431 (dev_t)name[0]) { 1432 SESS_UNLOCK(p->p_session); 1433 PROC_UNLOCK(p); 1434 continue; 1435 } 1436 SESS_UNLOCK(p->p_session); 1437 break; 1438 1439 case KERN_PROC_UID: 1440 if (p->p_ucred->cr_uid != (uid_t)name[0]) { 1441 PROC_UNLOCK(p); 1442 continue; 1443 } 1444 break; 1445 1446 case KERN_PROC_RUID: 1447 if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 1448 PROC_UNLOCK(p); 1449 continue; 1450 } 1451 break; 1452 1453 case KERN_PROC_PROC: 1454 break; 1455 1456 default: 1457 break; 1458 1459 } 1460 1461 error = sysctl_out_proc(p, req, flags, doingzomb); 1462 if (error) { 1463 sx_sunlock(&allproc_lock); 1464 sx_sunlock(&proctree_lock); 1465 return (error); 1466 } 1467 } 1468 } 1469 sx_sunlock(&allproc_lock); 1470 sx_sunlock(&proctree_lock); 1471 return (0); 1472 } 1473 1474 struct pargs * 1475 pargs_alloc(int len) 1476 { 1477 struct pargs *pa; 1478 1479 pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1480 M_WAITOK); 1481 refcount_init(&pa->ar_ref, 1); 1482 pa->ar_length = len; 1483 return (pa); 1484 } 1485 1486 static void 1487 pargs_free(struct pargs *pa) 1488 { 1489 1490 free(pa, M_PARGS); 1491 } 1492 1493 void 1494 pargs_hold(struct pargs *pa) 1495 { 1496 1497 if (pa == NULL) 1498 return; 1499 refcount_acquire(&pa->ar_ref); 1500 } 1501 1502 void 1503 pargs_drop(struct pargs *pa) 1504 { 1505 1506 if (pa == NULL) 1507 return; 1508 if (refcount_release(&pa->ar_ref)) 1509 pargs_free(pa); 1510 } 1511 1512 static int 1513 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf, 1514 size_t len) 1515 { 1516 struct iovec iov; 1517 struct uio uio; 1518 1519 iov.iov_base = (caddr_t)buf; 1520 iov.iov_len = len; 1521 uio.uio_iov = &iov; 1522 uio.uio_iovcnt = 1; 1523 uio.uio_offset = offset; 1524 uio.uio_resid = (ssize_t)len; 1525 uio.uio_segflg = UIO_SYSSPACE; 1526 uio.uio_rw = UIO_READ; 1527 uio.uio_td = td; 1528 1529 return (proc_rwmem(p, &uio)); 1530 } 1531 1532 static int 1533 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf, 1534 size_t len) 1535 { 1536 size_t i; 1537 int error; 1538 1539 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len); 1540 /* 1541 * Reading the chunk may validly return EFAULT if the string is shorter 1542 * than the chunk and is aligned at the end of the page, assuming the 1543 * next page is not mapped. So if EFAULT is returned do a fallback to 1544 * one byte read loop. 1545 */ 1546 if (error == EFAULT) { 1547 for (i = 0; i < len; i++, buf++, sptr++) { 1548 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1); 1549 if (error != 0) 1550 return (error); 1551 if (*buf == '\0') 1552 break; 1553 } 1554 error = 0; 1555 } 1556 return (error); 1557 } 1558 1559 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */ 1560 1561 enum proc_vector_type { 1562 PROC_ARG, 1563 PROC_ENV, 1564 PROC_AUX, 1565 }; 1566 1567 #ifdef COMPAT_FREEBSD32 1568 static int 1569 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp, 1570 size_t *vsizep, enum proc_vector_type type) 1571 { 1572 struct freebsd32_ps_strings pss; 1573 Elf32_Auxinfo aux; 1574 vm_offset_t vptr, ptr; 1575 uint32_t *proc_vector32; 1576 char **proc_vector; 1577 size_t vsize, size; 1578 int i, error; 1579 1580 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1581 &pss, sizeof(pss)); 1582 if (error != 0) 1583 return (error); 1584 switch (type) { 1585 case PROC_ARG: 1586 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr); 1587 vsize = pss.ps_nargvstr; 1588 if (vsize > ARG_MAX) 1589 return (ENOEXEC); 1590 size = vsize * sizeof(int32_t); 1591 break; 1592 case PROC_ENV: 1593 vptr = (vm_offset_t)PTRIN(pss.ps_envstr); 1594 vsize = pss.ps_nenvstr; 1595 if (vsize > ARG_MAX) 1596 return (ENOEXEC); 1597 size = vsize * sizeof(int32_t); 1598 break; 1599 case PROC_AUX: 1600 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) + 1601 (pss.ps_nenvstr + 1) * sizeof(int32_t); 1602 if (vptr % 4 != 0) 1603 return (ENOEXEC); 1604 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1605 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1606 if (error != 0) 1607 return (error); 1608 if (aux.a_type == AT_NULL) 1609 break; 1610 ptr += sizeof(aux); 1611 } 1612 if (aux.a_type != AT_NULL) 1613 return (ENOEXEC); 1614 vsize = i + 1; 1615 size = vsize * sizeof(aux); 1616 break; 1617 default: 1618 KASSERT(0, ("Wrong proc vector type: %d", type)); 1619 return (EINVAL); 1620 } 1621 proc_vector32 = malloc(size, M_TEMP, M_WAITOK); 1622 error = proc_read_mem(td, p, vptr, proc_vector32, size); 1623 if (error != 0) 1624 goto done; 1625 if (type == PROC_AUX) { 1626 *proc_vectorp = (char **)proc_vector32; 1627 *vsizep = vsize; 1628 return (0); 1629 } 1630 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK); 1631 for (i = 0; i < (int)vsize; i++) 1632 proc_vector[i] = PTRIN(proc_vector32[i]); 1633 *proc_vectorp = proc_vector; 1634 *vsizep = vsize; 1635 done: 1636 free(proc_vector32, M_TEMP); 1637 return (error); 1638 } 1639 #endif 1640 1641 static int 1642 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp, 1643 size_t *vsizep, enum proc_vector_type type) 1644 { 1645 struct ps_strings pss; 1646 Elf_Auxinfo aux; 1647 vm_offset_t vptr, ptr; 1648 char **proc_vector; 1649 size_t vsize, size; 1650 int error, i; 1651 1652 #ifdef COMPAT_FREEBSD32 1653 if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1654 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type)); 1655 #endif 1656 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1657 &pss, sizeof(pss)); 1658 if (error != 0) 1659 return (error); 1660 switch (type) { 1661 case PROC_ARG: 1662 vptr = (vm_offset_t)pss.ps_argvstr; 1663 vsize = pss.ps_nargvstr; 1664 if (vsize > ARG_MAX) 1665 return (ENOEXEC); 1666 size = vsize * sizeof(char *); 1667 break; 1668 case PROC_ENV: 1669 vptr = (vm_offset_t)pss.ps_envstr; 1670 vsize = pss.ps_nenvstr; 1671 if (vsize > ARG_MAX) 1672 return (ENOEXEC); 1673 size = vsize * sizeof(char *); 1674 break; 1675 case PROC_AUX: 1676 /* 1677 * The aux array is just above env array on the stack. Check 1678 * that the address is naturally aligned. 1679 */ 1680 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1) 1681 * sizeof(char *); 1682 #if __ELF_WORD_SIZE == 64 1683 if (vptr % sizeof(uint64_t) != 0) 1684 #else 1685 if (vptr % sizeof(uint32_t) != 0) 1686 #endif 1687 return (ENOEXEC); 1688 /* 1689 * We count the array size reading the aux vectors from the 1690 * stack until AT_NULL vector is returned. So (to keep the code 1691 * simple) we read the process stack twice: the first time here 1692 * to find the size and the second time when copying the vectors 1693 * to the allocated proc_vector. 1694 */ 1695 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1696 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1697 if (error != 0) 1698 return (error); 1699 if (aux.a_type == AT_NULL) 1700 break; 1701 ptr += sizeof(aux); 1702 } 1703 /* 1704 * If the PROC_AUXV_MAX entries are iterated over, and we have 1705 * not reached AT_NULL, it is most likely we are reading wrong 1706 * data: either the process doesn't have auxv array or data has 1707 * been modified. Return the error in this case. 1708 */ 1709 if (aux.a_type != AT_NULL) 1710 return (ENOEXEC); 1711 vsize = i + 1; 1712 size = vsize * sizeof(aux); 1713 break; 1714 default: 1715 KASSERT(0, ("Wrong proc vector type: %d", type)); 1716 return (EINVAL); /* In case we are built without INVARIANTS. */ 1717 } 1718 proc_vector = malloc(size, M_TEMP, M_WAITOK); 1719 if (proc_vector == NULL) 1720 return (ENOMEM); 1721 error = proc_read_mem(td, p, vptr, proc_vector, size); 1722 if (error != 0) { 1723 free(proc_vector, M_TEMP); 1724 return (error); 1725 } 1726 *proc_vectorp = proc_vector; 1727 *vsizep = vsize; 1728 1729 return (0); 1730 } 1731 1732 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */ 1733 1734 static int 1735 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb, 1736 enum proc_vector_type type) 1737 { 1738 size_t done, len, nchr, vsize; 1739 int error, i; 1740 char **proc_vector, *sptr; 1741 char pss_string[GET_PS_STRINGS_CHUNK_SZ]; 1742 1743 PROC_ASSERT_HELD(p); 1744 1745 /* 1746 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes. 1747 */ 1748 nchr = 2 * (PATH_MAX + ARG_MAX); 1749 1750 error = get_proc_vector(td, p, &proc_vector, &vsize, type); 1751 if (error != 0) 1752 return (error); 1753 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) { 1754 /* 1755 * The program may have scribbled into its argv array, e.g. to 1756 * remove some arguments. If that has happened, break out 1757 * before trying to read from NULL. 1758 */ 1759 if (proc_vector[i] == NULL) 1760 break; 1761 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) { 1762 error = proc_read_string(td, p, sptr, pss_string, 1763 sizeof(pss_string)); 1764 if (error != 0) 1765 goto done; 1766 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ); 1767 if (done + len >= nchr) 1768 len = nchr - done - 1; 1769 sbuf_bcat(sb, pss_string, len); 1770 if (len != GET_PS_STRINGS_CHUNK_SZ) 1771 break; 1772 done += GET_PS_STRINGS_CHUNK_SZ; 1773 } 1774 sbuf_bcat(sb, "", 1); 1775 done += len + 1; 1776 } 1777 done: 1778 free(proc_vector, M_TEMP); 1779 return (error); 1780 } 1781 1782 int 1783 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb) 1784 { 1785 1786 return (get_ps_strings(curthread, p, sb, PROC_ARG)); 1787 } 1788 1789 int 1790 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb) 1791 { 1792 1793 return (get_ps_strings(curthread, p, sb, PROC_ENV)); 1794 } 1795 1796 int 1797 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb) 1798 { 1799 size_t vsize, size; 1800 char **auxv; 1801 int error; 1802 1803 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX); 1804 if (error == 0) { 1805 #ifdef COMPAT_FREEBSD32 1806 if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1807 size = vsize * sizeof(Elf32_Auxinfo); 1808 else 1809 #endif 1810 size = vsize * sizeof(Elf_Auxinfo); 1811 if (sbuf_bcat(sb, auxv, size) != 0) 1812 error = ENOMEM; 1813 free(auxv, M_TEMP); 1814 } 1815 return (error); 1816 } 1817 1818 /* 1819 * This sysctl allows a process to retrieve the argument list or process 1820 * title for another process without groping around in the address space 1821 * of the other process. It also allow a process to set its own "process 1822 * title to a string of its own choice. 1823 */ 1824 static int 1825 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1826 { 1827 int *name = (int *)arg1; 1828 u_int namelen = arg2; 1829 struct pargs *newpa, *pa; 1830 struct proc *p; 1831 struct sbuf sb; 1832 int flags, error = 0, error2; 1833 1834 if (namelen != 1) 1835 return (EINVAL); 1836 1837 flags = PGET_CANSEE; 1838 if (req->newptr != NULL) 1839 flags |= PGET_ISCURRENT; 1840 error = pget((pid_t)name[0], flags, &p); 1841 if (error) 1842 return (error); 1843 1844 pa = p->p_args; 1845 if (pa != NULL) { 1846 pargs_hold(pa); 1847 PROC_UNLOCK(p); 1848 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1849 pargs_drop(pa); 1850 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) { 1851 _PHOLD(p); 1852 PROC_UNLOCK(p); 1853 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1854 error = proc_getargv(curthread, p, &sb); 1855 error2 = sbuf_finish(&sb); 1856 PRELE(p); 1857 sbuf_delete(&sb); 1858 if (error == 0 && error2 != 0) 1859 error = error2; 1860 } else { 1861 PROC_UNLOCK(p); 1862 } 1863 if (error != 0 || req->newptr == NULL) 1864 return (error); 1865 1866 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1867 return (ENOMEM); 1868 newpa = pargs_alloc(req->newlen); 1869 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1870 if (error != 0) { 1871 pargs_free(newpa); 1872 return (error); 1873 } 1874 PROC_LOCK(p); 1875 pa = p->p_args; 1876 p->p_args = newpa; 1877 PROC_UNLOCK(p); 1878 pargs_drop(pa); 1879 return (0); 1880 } 1881 1882 /* 1883 * This sysctl allows a process to retrieve environment of another process. 1884 */ 1885 static int 1886 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS) 1887 { 1888 int *name = (int *)arg1; 1889 u_int namelen = arg2; 1890 struct proc *p; 1891 struct sbuf sb; 1892 int error, error2; 1893 1894 if (namelen != 1) 1895 return (EINVAL); 1896 1897 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1898 if (error != 0) 1899 return (error); 1900 if ((p->p_flag & P_SYSTEM) != 0) { 1901 PRELE(p); 1902 return (0); 1903 } 1904 1905 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1906 error = proc_getenvv(curthread, p, &sb); 1907 error2 = sbuf_finish(&sb); 1908 PRELE(p); 1909 sbuf_delete(&sb); 1910 return (error != 0 ? error : error2); 1911 } 1912 1913 /* 1914 * This sysctl allows a process to retrieve ELF auxiliary vector of 1915 * another process. 1916 */ 1917 static int 1918 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS) 1919 { 1920 int *name = (int *)arg1; 1921 u_int namelen = arg2; 1922 struct proc *p; 1923 struct sbuf sb; 1924 int error, error2; 1925 1926 if (namelen != 1) 1927 return (EINVAL); 1928 1929 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1930 if (error != 0) 1931 return (error); 1932 if ((p->p_flag & P_SYSTEM) != 0) { 1933 PRELE(p); 1934 return (0); 1935 } 1936 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1937 error = proc_getauxv(curthread, p, &sb); 1938 error2 = sbuf_finish(&sb); 1939 PRELE(p); 1940 sbuf_delete(&sb); 1941 return (error != 0 ? error : error2); 1942 } 1943 1944 /* 1945 * This sysctl allows a process to retrieve the path of the executable for 1946 * itself or another process. 1947 */ 1948 static int 1949 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1950 { 1951 pid_t *pidp = (pid_t *)arg1; 1952 unsigned int arglen = arg2; 1953 struct proc *p; 1954 struct vnode *vp; 1955 char *retbuf, *freebuf; 1956 int error; 1957 1958 if (arglen != 1) 1959 return (EINVAL); 1960 if (*pidp == -1) { /* -1 means this process */ 1961 p = req->td->td_proc; 1962 } else { 1963 error = pget(*pidp, PGET_CANSEE, &p); 1964 if (error != 0) 1965 return (error); 1966 } 1967 1968 vp = p->p_textvp; 1969 if (vp == NULL) { 1970 if (*pidp != -1) 1971 PROC_UNLOCK(p); 1972 return (0); 1973 } 1974 vref(vp); 1975 if (*pidp != -1) 1976 PROC_UNLOCK(p); 1977 error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1978 vrele(vp); 1979 if (error) 1980 return (error); 1981 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1982 free(freebuf, M_TEMP); 1983 return (error); 1984 } 1985 1986 static int 1987 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1988 { 1989 struct proc *p; 1990 char *sv_name; 1991 int *name; 1992 int namelen; 1993 int error; 1994 1995 namelen = arg2; 1996 if (namelen != 1) 1997 return (EINVAL); 1998 1999 name = (int *)arg1; 2000 error = pget((pid_t)name[0], PGET_CANSEE, &p); 2001 if (error != 0) 2002 return (error); 2003 sv_name = p->p_sysent->sv_name; 2004 PROC_UNLOCK(p); 2005 return (sysctl_handle_string(oidp, sv_name, 0, req)); 2006 } 2007 2008 #ifdef KINFO_OVMENTRY_SIZE 2009 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 2010 #endif 2011 2012 #ifdef COMPAT_FREEBSD7 2013 static int 2014 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 2015 { 2016 vm_map_entry_t entry, tmp_entry; 2017 unsigned int last_timestamp; 2018 char *fullpath, *freepath; 2019 struct kinfo_ovmentry *kve; 2020 struct vattr va; 2021 struct ucred *cred; 2022 int error, *name; 2023 struct vnode *vp; 2024 struct proc *p; 2025 vm_map_t map; 2026 struct vmspace *vm; 2027 2028 name = (int *)arg1; 2029 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2030 if (error != 0) 2031 return (error); 2032 vm = vmspace_acquire_ref(p); 2033 if (vm == NULL) { 2034 PRELE(p); 2035 return (ESRCH); 2036 } 2037 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 2038 2039 map = &vm->vm_map; 2040 vm_map_lock_read(map); 2041 for (entry = map->header.next; entry != &map->header; 2042 entry = entry->next) { 2043 vm_object_t obj, tobj, lobj; 2044 vm_offset_t addr; 2045 2046 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2047 continue; 2048 2049 bzero(kve, sizeof(*kve)); 2050 kve->kve_structsize = sizeof(*kve); 2051 2052 kve->kve_private_resident = 0; 2053 obj = entry->object.vm_object; 2054 if (obj != NULL) { 2055 VM_OBJECT_RLOCK(obj); 2056 if (obj->shadow_count == 1) 2057 kve->kve_private_resident = 2058 obj->resident_page_count; 2059 } 2060 kve->kve_resident = 0; 2061 addr = entry->start; 2062 while (addr < entry->end) { 2063 if (pmap_extract(map->pmap, addr)) 2064 kve->kve_resident++; 2065 addr += PAGE_SIZE; 2066 } 2067 2068 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 2069 if (tobj != obj) 2070 VM_OBJECT_RLOCK(tobj); 2071 if (lobj != obj) 2072 VM_OBJECT_RUNLOCK(lobj); 2073 lobj = tobj; 2074 } 2075 2076 kve->kve_start = (void*)entry->start; 2077 kve->kve_end = (void*)entry->end; 2078 kve->kve_offset = (off_t)entry->offset; 2079 2080 if (entry->protection & VM_PROT_READ) 2081 kve->kve_protection |= KVME_PROT_READ; 2082 if (entry->protection & VM_PROT_WRITE) 2083 kve->kve_protection |= KVME_PROT_WRITE; 2084 if (entry->protection & VM_PROT_EXECUTE) 2085 kve->kve_protection |= KVME_PROT_EXEC; 2086 2087 if (entry->eflags & MAP_ENTRY_COW) 2088 kve->kve_flags |= KVME_FLAG_COW; 2089 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2090 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2091 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2092 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2093 2094 last_timestamp = map->timestamp; 2095 vm_map_unlock_read(map); 2096 2097 kve->kve_fileid = 0; 2098 kve->kve_fsid = 0; 2099 freepath = NULL; 2100 fullpath = ""; 2101 if (lobj) { 2102 vp = NULL; 2103 switch (lobj->type) { 2104 case OBJT_DEFAULT: 2105 kve->kve_type = KVME_TYPE_DEFAULT; 2106 break; 2107 case OBJT_VNODE: 2108 kve->kve_type = KVME_TYPE_VNODE; 2109 vp = lobj->handle; 2110 vref(vp); 2111 break; 2112 case OBJT_SWAP: 2113 kve->kve_type = KVME_TYPE_SWAP; 2114 break; 2115 case OBJT_DEVICE: 2116 kve->kve_type = KVME_TYPE_DEVICE; 2117 break; 2118 case OBJT_PHYS: 2119 kve->kve_type = KVME_TYPE_PHYS; 2120 break; 2121 case OBJT_DEAD: 2122 kve->kve_type = KVME_TYPE_DEAD; 2123 break; 2124 case OBJT_SG: 2125 kve->kve_type = KVME_TYPE_SG; 2126 break; 2127 default: 2128 kve->kve_type = KVME_TYPE_UNKNOWN; 2129 break; 2130 } 2131 if (lobj != obj) 2132 VM_OBJECT_RUNLOCK(lobj); 2133 2134 kve->kve_ref_count = obj->ref_count; 2135 kve->kve_shadow_count = obj->shadow_count; 2136 VM_OBJECT_RUNLOCK(obj); 2137 if (vp != NULL) { 2138 vn_fullpath(curthread, vp, &fullpath, 2139 &freepath); 2140 cred = curthread->td_ucred; 2141 vn_lock(vp, LK_SHARED | LK_RETRY); 2142 if (VOP_GETATTR(vp, &va, cred) == 0) { 2143 kve->kve_fileid = va.va_fileid; 2144 kve->kve_fsid = va.va_fsid; 2145 } 2146 vput(vp); 2147 } 2148 } else { 2149 kve->kve_type = KVME_TYPE_NONE; 2150 kve->kve_ref_count = 0; 2151 kve->kve_shadow_count = 0; 2152 } 2153 2154 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2155 if (freepath != NULL) 2156 free(freepath, M_TEMP); 2157 2158 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 2159 vm_map_lock_read(map); 2160 if (error) 2161 break; 2162 if (last_timestamp != map->timestamp) { 2163 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2164 entry = tmp_entry; 2165 } 2166 } 2167 vm_map_unlock_read(map); 2168 vmspace_free(vm); 2169 PRELE(p); 2170 free(kve, M_TEMP); 2171 return (error); 2172 } 2173 #endif /* COMPAT_FREEBSD7 */ 2174 2175 #ifdef KINFO_VMENTRY_SIZE 2176 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2177 #endif 2178 2179 static void 2180 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry, 2181 struct kinfo_vmentry *kve) 2182 { 2183 vm_object_t obj, tobj; 2184 vm_page_t m, m_adv; 2185 vm_offset_t addr; 2186 vm_paddr_t locked_pa; 2187 vm_pindex_t pi, pi_adv, pindex; 2188 2189 locked_pa = 0; 2190 obj = entry->object.vm_object; 2191 addr = entry->start; 2192 m_adv = NULL; 2193 pi = OFF_TO_IDX(entry->offset); 2194 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) { 2195 if (m_adv != NULL) { 2196 m = m_adv; 2197 } else { 2198 pi_adv = OFF_TO_IDX(entry->end - addr); 2199 pindex = pi; 2200 for (tobj = obj;; tobj = tobj->backing_object) { 2201 m = vm_page_find_least(tobj, pindex); 2202 if (m != NULL) { 2203 if (m->pindex == pindex) 2204 break; 2205 if (pi_adv > m->pindex - pindex) { 2206 pi_adv = m->pindex - pindex; 2207 m_adv = m; 2208 } 2209 } 2210 if (tobj->backing_object == NULL) 2211 goto next; 2212 pindex += OFF_TO_IDX(tobj-> 2213 backing_object_offset); 2214 } 2215 } 2216 m_adv = NULL; 2217 if (m->psind != 0 && addr + pagesizes[1] <= entry->end && 2218 (addr & (pagesizes[1] - 1)) == 0 && 2219 (pmap_mincore(map->pmap, addr, &locked_pa) & 2220 MINCORE_SUPER) != 0) { 2221 kve->kve_flags |= KVME_FLAG_SUPER; 2222 pi_adv = OFF_TO_IDX(pagesizes[1]); 2223 } else { 2224 /* 2225 * We do not test the found page on validity. 2226 * Either the page is busy and being paged in, 2227 * or it was invalidated. The first case 2228 * should be counted as resident, the second 2229 * is not so clear; we do account both. 2230 */ 2231 pi_adv = 1; 2232 } 2233 kve->kve_resident += pi_adv; 2234 next:; 2235 } 2236 PA_UNLOCK_COND(locked_pa); 2237 } 2238 2239 /* 2240 * Must be called with the process locked and will return unlocked. 2241 */ 2242 int 2243 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb) 2244 { 2245 vm_map_entry_t entry, tmp_entry; 2246 struct vattr va; 2247 vm_map_t map; 2248 vm_object_t obj, tobj, lobj; 2249 char *fullpath, *freepath; 2250 struct kinfo_vmentry *kve; 2251 struct ucred *cred; 2252 struct vnode *vp; 2253 struct vmspace *vm; 2254 vm_offset_t addr; 2255 unsigned int last_timestamp; 2256 int error; 2257 2258 PROC_LOCK_ASSERT(p, MA_OWNED); 2259 2260 _PHOLD(p); 2261 PROC_UNLOCK(p); 2262 vm = vmspace_acquire_ref(p); 2263 if (vm == NULL) { 2264 PRELE(p); 2265 return (ESRCH); 2266 } 2267 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 2268 2269 error = 0; 2270 map = &vm->vm_map; 2271 vm_map_lock_read(map); 2272 for (entry = map->header.next; entry != &map->header; 2273 entry = entry->next) { 2274 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2275 continue; 2276 2277 addr = entry->end; 2278 bzero(kve, sizeof(*kve)); 2279 obj = entry->object.vm_object; 2280 if (obj != NULL) { 2281 for (tobj = obj; tobj != NULL; 2282 tobj = tobj->backing_object) { 2283 VM_OBJECT_RLOCK(tobj); 2284 lobj = tobj; 2285 } 2286 if (obj->backing_object == NULL) 2287 kve->kve_private_resident = 2288 obj->resident_page_count; 2289 if (!vmmap_skip_res_cnt) 2290 kern_proc_vmmap_resident(map, entry, kve); 2291 for (tobj = obj; tobj != NULL; 2292 tobj = tobj->backing_object) { 2293 if (tobj != obj && tobj != lobj) 2294 VM_OBJECT_RUNLOCK(tobj); 2295 } 2296 } else { 2297 lobj = NULL; 2298 } 2299 2300 kve->kve_start = entry->start; 2301 kve->kve_end = entry->end; 2302 kve->kve_offset = entry->offset; 2303 2304 if (entry->protection & VM_PROT_READ) 2305 kve->kve_protection |= KVME_PROT_READ; 2306 if (entry->protection & VM_PROT_WRITE) 2307 kve->kve_protection |= KVME_PROT_WRITE; 2308 if (entry->protection & VM_PROT_EXECUTE) 2309 kve->kve_protection |= KVME_PROT_EXEC; 2310 2311 if (entry->eflags & MAP_ENTRY_COW) 2312 kve->kve_flags |= KVME_FLAG_COW; 2313 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2314 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2315 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2316 kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2317 if (entry->eflags & MAP_ENTRY_GROWS_UP) 2318 kve->kve_flags |= KVME_FLAG_GROWS_UP; 2319 if (entry->eflags & MAP_ENTRY_GROWS_DOWN) 2320 kve->kve_flags |= KVME_FLAG_GROWS_DOWN; 2321 2322 last_timestamp = map->timestamp; 2323 vm_map_unlock_read(map); 2324 2325 freepath = NULL; 2326 fullpath = ""; 2327 if (lobj != NULL) { 2328 vp = NULL; 2329 switch (lobj->type) { 2330 case OBJT_DEFAULT: 2331 kve->kve_type = KVME_TYPE_DEFAULT; 2332 break; 2333 case OBJT_VNODE: 2334 kve->kve_type = KVME_TYPE_VNODE; 2335 vp = lobj->handle; 2336 vref(vp); 2337 break; 2338 case OBJT_SWAP: 2339 kve->kve_type = KVME_TYPE_SWAP; 2340 break; 2341 case OBJT_DEVICE: 2342 kve->kve_type = KVME_TYPE_DEVICE; 2343 break; 2344 case OBJT_PHYS: 2345 kve->kve_type = KVME_TYPE_PHYS; 2346 break; 2347 case OBJT_DEAD: 2348 kve->kve_type = KVME_TYPE_DEAD; 2349 break; 2350 case OBJT_SG: 2351 kve->kve_type = KVME_TYPE_SG; 2352 break; 2353 case OBJT_MGTDEVICE: 2354 kve->kve_type = KVME_TYPE_MGTDEVICE; 2355 break; 2356 default: 2357 kve->kve_type = KVME_TYPE_UNKNOWN; 2358 break; 2359 } 2360 if (lobj != obj) 2361 VM_OBJECT_RUNLOCK(lobj); 2362 2363 kve->kve_ref_count = obj->ref_count; 2364 kve->kve_shadow_count = obj->shadow_count; 2365 VM_OBJECT_RUNLOCK(obj); 2366 if (vp != NULL) { 2367 vn_fullpath(curthread, vp, &fullpath, 2368 &freepath); 2369 kve->kve_vn_type = vntype_to_kinfo(vp->v_type); 2370 cred = curthread->td_ucred; 2371 vn_lock(vp, LK_SHARED | LK_RETRY); 2372 if (VOP_GETATTR(vp, &va, cred) == 0) { 2373 kve->kve_vn_fileid = va.va_fileid; 2374 kve->kve_vn_fsid = va.va_fsid; 2375 kve->kve_vn_mode = 2376 MAKEIMODE(va.va_type, va.va_mode); 2377 kve->kve_vn_size = va.va_size; 2378 kve->kve_vn_rdev = va.va_rdev; 2379 kve->kve_status = KF_ATTR_VALID; 2380 } 2381 vput(vp); 2382 } 2383 } else { 2384 kve->kve_type = KVME_TYPE_NONE; 2385 kve->kve_ref_count = 0; 2386 kve->kve_shadow_count = 0; 2387 } 2388 2389 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2390 if (freepath != NULL) 2391 free(freepath, M_TEMP); 2392 2393 /* Pack record size down */ 2394 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 2395 strlen(kve->kve_path) + 1; 2396 kve->kve_structsize = roundup(kve->kve_structsize, 2397 sizeof(uint64_t)); 2398 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0) 2399 error = ENOMEM; 2400 vm_map_lock_read(map); 2401 if (error != 0) 2402 break; 2403 if (last_timestamp != map->timestamp) { 2404 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2405 entry = tmp_entry; 2406 } 2407 } 2408 vm_map_unlock_read(map); 2409 vmspace_free(vm); 2410 PRELE(p); 2411 free(kve, M_TEMP); 2412 return (error); 2413 } 2414 2415 static int 2416 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 2417 { 2418 struct proc *p; 2419 struct sbuf sb; 2420 int error, error2, *name; 2421 2422 name = (int *)arg1; 2423 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req); 2424 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p); 2425 if (error != 0) { 2426 sbuf_delete(&sb); 2427 return (error); 2428 } 2429 error = kern_proc_vmmap_out(p, &sb); 2430 error2 = sbuf_finish(&sb); 2431 sbuf_delete(&sb); 2432 return (error != 0 ? error : error2); 2433 } 2434 2435 #if defined(STACK) || defined(DDB) 2436 static int 2437 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 2438 { 2439 struct kinfo_kstack *kkstp; 2440 int error, i, *name, numthreads; 2441 lwpid_t *lwpidarray; 2442 struct thread *td; 2443 struct stack *st; 2444 struct sbuf sb; 2445 struct proc *p; 2446 2447 name = (int *)arg1; 2448 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p); 2449 if (error != 0) 2450 return (error); 2451 2452 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 2453 st = stack_create(); 2454 2455 lwpidarray = NULL; 2456 numthreads = 0; 2457 PROC_LOCK(p); 2458 repeat: 2459 if (numthreads < p->p_numthreads) { 2460 if (lwpidarray != NULL) { 2461 free(lwpidarray, M_TEMP); 2462 lwpidarray = NULL; 2463 } 2464 numthreads = p->p_numthreads; 2465 PROC_UNLOCK(p); 2466 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 2467 M_WAITOK | M_ZERO); 2468 PROC_LOCK(p); 2469 goto repeat; 2470 } 2471 i = 0; 2472 2473 /* 2474 * XXXRW: During the below loop, execve(2) and countless other sorts 2475 * of changes could have taken place. Should we check to see if the 2476 * vmspace has been replaced, or the like, in order to prevent 2477 * giving a snapshot that spans, say, execve(2), with some threads 2478 * before and some after? Among other things, the credentials could 2479 * have changed, in which case the right to extract debug info might 2480 * no longer be assured. 2481 */ 2482 FOREACH_THREAD_IN_PROC(p, td) { 2483 KASSERT(i < numthreads, 2484 ("sysctl_kern_proc_kstack: numthreads")); 2485 lwpidarray[i] = td->td_tid; 2486 i++; 2487 } 2488 numthreads = i; 2489 for (i = 0; i < numthreads; i++) { 2490 td = thread_find(p, lwpidarray[i]); 2491 if (td == NULL) { 2492 continue; 2493 } 2494 bzero(kkstp, sizeof(*kkstp)); 2495 (void)sbuf_new(&sb, kkstp->kkst_trace, 2496 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 2497 thread_lock(td); 2498 kkstp->kkst_tid = td->td_tid; 2499 if (TD_IS_SWAPPED(td)) 2500 kkstp->kkst_state = KKST_STATE_SWAPPED; 2501 else if (TD_IS_RUNNING(td)) 2502 kkstp->kkst_state = KKST_STATE_RUNNING; 2503 else { 2504 kkstp->kkst_state = KKST_STATE_STACKOK; 2505 stack_save_td(st, td); 2506 } 2507 thread_unlock(td); 2508 PROC_UNLOCK(p); 2509 stack_sbuf_print(&sb, st); 2510 sbuf_finish(&sb); 2511 sbuf_delete(&sb); 2512 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 2513 PROC_LOCK(p); 2514 if (error) 2515 break; 2516 } 2517 _PRELE(p); 2518 PROC_UNLOCK(p); 2519 if (lwpidarray != NULL) 2520 free(lwpidarray, M_TEMP); 2521 stack_destroy(st); 2522 free(kkstp, M_TEMP); 2523 return (error); 2524 } 2525 #endif 2526 2527 /* 2528 * This sysctl allows a process to retrieve the full list of groups from 2529 * itself or another process. 2530 */ 2531 static int 2532 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 2533 { 2534 pid_t *pidp = (pid_t *)arg1; 2535 unsigned int arglen = arg2; 2536 struct proc *p; 2537 struct ucred *cred; 2538 int error; 2539 2540 if (arglen != 1) 2541 return (EINVAL); 2542 if (*pidp == -1) { /* -1 means this process */ 2543 p = req->td->td_proc; 2544 PROC_LOCK(p); 2545 } else { 2546 error = pget(*pidp, PGET_CANSEE, &p); 2547 if (error != 0) 2548 return (error); 2549 } 2550 2551 cred = crhold(p->p_ucred); 2552 PROC_UNLOCK(p); 2553 2554 error = SYSCTL_OUT(req, cred->cr_groups, 2555 cred->cr_ngroups * sizeof(gid_t)); 2556 crfree(cred); 2557 return (error); 2558 } 2559 2560 /* 2561 * This sysctl allows a process to retrieve or/and set the resource limit for 2562 * another process. 2563 */ 2564 static int 2565 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS) 2566 { 2567 int *name = (int *)arg1; 2568 u_int namelen = arg2; 2569 struct rlimit rlim; 2570 struct proc *p; 2571 u_int which; 2572 int flags, error; 2573 2574 if (namelen != 2) 2575 return (EINVAL); 2576 2577 which = (u_int)name[1]; 2578 if (which >= RLIM_NLIMITS) 2579 return (EINVAL); 2580 2581 if (req->newptr != NULL && req->newlen != sizeof(rlim)) 2582 return (EINVAL); 2583 2584 flags = PGET_HOLD | PGET_NOTWEXIT; 2585 if (req->newptr != NULL) 2586 flags |= PGET_CANDEBUG; 2587 else 2588 flags |= PGET_CANSEE; 2589 error = pget((pid_t)name[0], flags, &p); 2590 if (error != 0) 2591 return (error); 2592 2593 /* 2594 * Retrieve limit. 2595 */ 2596 if (req->oldptr != NULL) { 2597 PROC_LOCK(p); 2598 lim_rlimit(p, which, &rlim); 2599 PROC_UNLOCK(p); 2600 } 2601 error = SYSCTL_OUT(req, &rlim, sizeof(rlim)); 2602 if (error != 0) 2603 goto errout; 2604 2605 /* 2606 * Set limit. 2607 */ 2608 if (req->newptr != NULL) { 2609 error = SYSCTL_IN(req, &rlim, sizeof(rlim)); 2610 if (error == 0) 2611 error = kern_proc_setrlimit(curthread, p, which, &rlim); 2612 } 2613 2614 errout: 2615 PRELE(p); 2616 return (error); 2617 } 2618 2619 /* 2620 * This sysctl allows a process to retrieve ps_strings structure location of 2621 * another process. 2622 */ 2623 static int 2624 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS) 2625 { 2626 int *name = (int *)arg1; 2627 u_int namelen = arg2; 2628 struct proc *p; 2629 vm_offset_t ps_strings; 2630 int error; 2631 #ifdef COMPAT_FREEBSD32 2632 uint32_t ps_strings32; 2633 #endif 2634 2635 if (namelen != 1) 2636 return (EINVAL); 2637 2638 error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 2639 if (error != 0) 2640 return (error); 2641 #ifdef COMPAT_FREEBSD32 2642 if ((req->flags & SCTL_MASK32) != 0) { 2643 /* 2644 * We return 0 if the 32 bit emulation request is for a 64 bit 2645 * process. 2646 */ 2647 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ? 2648 PTROUT(p->p_sysent->sv_psstrings) : 0; 2649 PROC_UNLOCK(p); 2650 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32)); 2651 return (error); 2652 } 2653 #endif 2654 ps_strings = p->p_sysent->sv_psstrings; 2655 PROC_UNLOCK(p); 2656 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings)); 2657 return (error); 2658 } 2659 2660 /* 2661 * This sysctl allows a process to retrieve umask of another process. 2662 */ 2663 static int 2664 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS) 2665 { 2666 int *name = (int *)arg1; 2667 u_int namelen = arg2; 2668 struct proc *p; 2669 int error; 2670 u_short fd_cmask; 2671 2672 if (namelen != 1) 2673 return (EINVAL); 2674 2675 error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2676 if (error != 0) 2677 return (error); 2678 2679 FILEDESC_SLOCK(p->p_fd); 2680 fd_cmask = p->p_fd->fd_cmask; 2681 FILEDESC_SUNLOCK(p->p_fd); 2682 PRELE(p); 2683 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask)); 2684 return (error); 2685 } 2686 2687 /* 2688 * This sysctl allows a process to set and retrieve binary osreldate of 2689 * another process. 2690 */ 2691 static int 2692 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS) 2693 { 2694 int *name = (int *)arg1; 2695 u_int namelen = arg2; 2696 struct proc *p; 2697 int flags, error, osrel; 2698 2699 if (namelen != 1) 2700 return (EINVAL); 2701 2702 if (req->newptr != NULL && req->newlen != sizeof(osrel)) 2703 return (EINVAL); 2704 2705 flags = PGET_HOLD | PGET_NOTWEXIT; 2706 if (req->newptr != NULL) 2707 flags |= PGET_CANDEBUG; 2708 else 2709 flags |= PGET_CANSEE; 2710 error = pget((pid_t)name[0], flags, &p); 2711 if (error != 0) 2712 return (error); 2713 2714 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel)); 2715 if (error != 0) 2716 goto errout; 2717 2718 if (req->newptr != NULL) { 2719 error = SYSCTL_IN(req, &osrel, sizeof(osrel)); 2720 if (error != 0) 2721 goto errout; 2722 if (osrel < 0) { 2723 error = EINVAL; 2724 goto errout; 2725 } 2726 p->p_osrel = osrel; 2727 } 2728 errout: 2729 PRELE(p); 2730 return (error); 2731 } 2732 2733 static int 2734 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS) 2735 { 2736 int *name = (int *)arg1; 2737 u_int namelen = arg2; 2738 struct proc *p; 2739 struct kinfo_sigtramp kst; 2740 const struct sysentvec *sv; 2741 int error; 2742 #ifdef COMPAT_FREEBSD32 2743 struct kinfo_sigtramp32 kst32; 2744 #endif 2745 2746 if (namelen != 1) 2747 return (EINVAL); 2748 2749 error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 2750 if (error != 0) 2751 return (error); 2752 sv = p->p_sysent; 2753 #ifdef COMPAT_FREEBSD32 2754 if ((req->flags & SCTL_MASK32) != 0) { 2755 bzero(&kst32, sizeof(kst32)); 2756 if (SV_PROC_FLAG(p, SV_ILP32)) { 2757 if (sv->sv_sigcode_base != 0) { 2758 kst32.ksigtramp_start = sv->sv_sigcode_base; 2759 kst32.ksigtramp_end = sv->sv_sigcode_base + 2760 *sv->sv_szsigcode; 2761 } else { 2762 kst32.ksigtramp_start = sv->sv_psstrings - 2763 *sv->sv_szsigcode; 2764 kst32.ksigtramp_end = sv->sv_psstrings; 2765 } 2766 } 2767 PROC_UNLOCK(p); 2768 error = SYSCTL_OUT(req, &kst32, sizeof(kst32)); 2769 return (error); 2770 } 2771 #endif 2772 bzero(&kst, sizeof(kst)); 2773 if (sv->sv_sigcode_base != 0) { 2774 kst.ksigtramp_start = (char *)sv->sv_sigcode_base; 2775 kst.ksigtramp_end = (char *)sv->sv_sigcode_base + 2776 *sv->sv_szsigcode; 2777 } else { 2778 kst.ksigtramp_start = (char *)sv->sv_psstrings - 2779 *sv->sv_szsigcode; 2780 kst.ksigtramp_end = (char *)sv->sv_psstrings; 2781 } 2782 PROC_UNLOCK(p); 2783 error = SYSCTL_OUT(req, &kst, sizeof(kst)); 2784 return (error); 2785 } 2786 2787 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 2788 2789 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 2790 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 2791 "Return entire process table"); 2792 2793 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2794 sysctl_kern_proc, "Process table"); 2795 2796 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 2797 sysctl_kern_proc, "Process table"); 2798 2799 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2800 sysctl_kern_proc, "Process table"); 2801 2802 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 2803 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2804 2805 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2806 sysctl_kern_proc, "Process table"); 2807 2808 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2809 sysctl_kern_proc, "Process table"); 2810 2811 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2812 sysctl_kern_proc, "Process table"); 2813 2814 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2815 sysctl_kern_proc, "Process table"); 2816 2817 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 2818 sysctl_kern_proc, "Return process table, no threads"); 2819 2820 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 2821 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2822 sysctl_kern_proc_args, "Process argument list"); 2823 2824 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE, 2825 sysctl_kern_proc_env, "Process environment"); 2826 2827 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD | 2828 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector"); 2829 2830 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 2831 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2832 2833 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 2834 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 2835 "Process syscall vector name (ABI type)"); 2836 2837 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 2838 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2839 2840 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 2841 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2842 2843 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 2844 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2845 2846 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 2847 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2848 2849 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 2850 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2851 2852 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 2853 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2854 2855 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 2856 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2857 2858 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 2859 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2860 2861 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 2862 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 2863 "Return process table, no threads"); 2864 2865 #ifdef COMPAT_FREEBSD7 2866 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 2867 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 2868 #endif 2869 2870 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 2871 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 2872 2873 #if defined(STACK) || defined(DDB) 2874 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 2875 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 2876 #endif 2877 2878 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2879 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 2880 2881 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW | 2882 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, 2883 "Process resource limits"); 2884 2885 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD | 2886 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings, 2887 "Process ps_strings location"); 2888 2889 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD | 2890 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask"); 2891 2892 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW | 2893 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel, 2894 "Process binary osreldate"); 2895 2896 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD | 2897 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp, 2898 "Process signal trampoline location"); 2899 2900 int allproc_gen; 2901 2902 void 2903 stop_all_proc(void) 2904 { 2905 struct proc *cp, *p; 2906 int r, gen; 2907 bool restart, seen_stopped, seen_exiting, stopped_some; 2908 2909 cp = curproc; 2910 /* 2911 * stop_all_proc() assumes that all process which have 2912 * usermode must be stopped, except current process, for 2913 * obvious reasons. Since other threads in the process 2914 * establishing global stop could unstop something, disable 2915 * calls from multithreaded processes as precaution. The 2916 * service must not be user-callable anyway. 2917 */ 2918 KASSERT((cp->p_flag & P_HADTHREADS) == 0 || 2919 (cp->p_flag & P_KTHREAD) != 0, ("mt stop_all_proc")); 2920 2921 allproc_loop: 2922 sx_xlock(&allproc_lock); 2923 gen = allproc_gen; 2924 seen_exiting = seen_stopped = stopped_some = restart = false; 2925 LIST_REMOVE(cp, p_list); 2926 LIST_INSERT_HEAD(&allproc, cp, p_list); 2927 for (;;) { 2928 p = LIST_NEXT(cp, p_list); 2929 if (p == NULL) 2930 break; 2931 LIST_REMOVE(cp, p_list); 2932 LIST_INSERT_AFTER(p, cp, p_list); 2933 PROC_LOCK(p); 2934 if ((p->p_flag & (P_KTHREAD | P_SYSTEM | 2935 P_TOTAL_STOP)) != 0) { 2936 PROC_UNLOCK(p); 2937 continue; 2938 } 2939 if ((p->p_flag & P_WEXIT) != 0) { 2940 seen_exiting = true; 2941 PROC_UNLOCK(p); 2942 continue; 2943 } 2944 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 2945 /* 2946 * Stopped processes are tolerated when there 2947 * are no other processes which might continue 2948 * them. P_STOPPED_SINGLE but not 2949 * P_TOTAL_STOP process still has at least one 2950 * thread running. 2951 */ 2952 seen_stopped = true; 2953 PROC_UNLOCK(p); 2954 continue; 2955 } 2956 _PHOLD(p); 2957 sx_xunlock(&allproc_lock); 2958 r = thread_single(p, SINGLE_ALLPROC); 2959 if (r != 0) 2960 restart = true; 2961 else 2962 stopped_some = true; 2963 _PRELE(p); 2964 PROC_UNLOCK(p); 2965 sx_xlock(&allproc_lock); 2966 } 2967 /* Catch forked children we did not see in iteration. */ 2968 if (gen != allproc_gen) 2969 restart = true; 2970 sx_xunlock(&allproc_lock); 2971 if (restart || stopped_some || seen_exiting || seen_stopped) { 2972 kern_yield(PRI_USER); 2973 goto allproc_loop; 2974 } 2975 } 2976 2977 void 2978 resume_all_proc(void) 2979 { 2980 struct proc *cp, *p; 2981 2982 cp = curproc; 2983 sx_xlock(&allproc_lock); 2984 LIST_REMOVE(cp, p_list); 2985 LIST_INSERT_HEAD(&allproc, cp, p_list); 2986 for (;;) { 2987 p = LIST_NEXT(cp, p_list); 2988 if (p == NULL) 2989 break; 2990 LIST_REMOVE(cp, p_list); 2991 LIST_INSERT_AFTER(p, cp, p_list); 2992 PROC_LOCK(p); 2993 if ((p->p_flag & P_TOTAL_STOP) != 0) { 2994 sx_xunlock(&allproc_lock); 2995 _PHOLD(p); 2996 thread_single_end(p, SINGLE_ALLPROC); 2997 _PRELE(p); 2998 PROC_UNLOCK(p); 2999 sx_xlock(&allproc_lock); 3000 } else { 3001 PROC_UNLOCK(p); 3002 } 3003 } 3004 sx_xunlock(&allproc_lock); 3005 } 3006 3007 #define TOTAL_STOP_DEBUG 1 3008 #ifdef TOTAL_STOP_DEBUG 3009 volatile static int ap_resume; 3010 #include <sys/mount.h> 3011 3012 static int 3013 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS) 3014 { 3015 int error, val; 3016 3017 val = 0; 3018 ap_resume = 0; 3019 error = sysctl_handle_int(oidp, &val, 0, req); 3020 if (error != 0 || req->newptr == NULL) 3021 return (error); 3022 if (val != 0) { 3023 stop_all_proc(); 3024 syncer_suspend(); 3025 while (ap_resume == 0) 3026 ; 3027 syncer_resume(); 3028 resume_all_proc(); 3029 } 3030 return (0); 3031 } 3032 3033 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW | 3034 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0, 3035 sysctl_debug_stop_all_proc, "I", 3036 ""); 3037 #endif 3038