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_kdtrace.h" 38 #include "opt_ktrace.h" 39 #include "opt_kstack_pages.h" 40 #include "opt_stack.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/limits.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/refcount.h> 52 #include <sys/sbuf.h> 53 #include <sys/sysent.h> 54 #include <sys/sched.h> 55 #include <sys/smp.h> 56 #include <sys/stack.h> 57 #include <sys/sysctl.h> 58 #include <sys/filedesc.h> 59 #include <sys/tty.h> 60 #include <sys/signalvar.h> 61 #include <sys/sdt.h> 62 #include <sys/sx.h> 63 #include <sys/user.h> 64 #include <sys/jail.h> 65 #include <sys/vnode.h> 66 #include <sys/eventhandler.h> 67 #ifdef KTRACE 68 #include <sys/uio.h> 69 #include <sys/ktrace.h> 70 #endif 71 72 #ifdef DDB 73 #include <ddb/ddb.h> 74 #endif 75 76 #include <vm/vm.h> 77 #include <vm/vm_extern.h> 78 #include <vm/pmap.h> 79 #include <vm/vm_map.h> 80 #include <vm/vm_object.h> 81 #include <vm/uma.h> 82 83 #ifdef COMPAT_FREEBSD32 84 #include <compat/freebsd32/freebsd32.h> 85 #include <compat/freebsd32/freebsd32_util.h> 86 #endif 87 88 SDT_PROVIDER_DEFINE(proc); 89 SDT_PROBE_DEFINE(proc, kernel, ctor, entry); 90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *"); 91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int"); 92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *"); 93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int"); 94 SDT_PROBE_DEFINE(proc, kernel, ctor, return); 95 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *"); 96 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int"); 97 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *"); 98 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int"); 99 SDT_PROBE_DEFINE(proc, kernel, dtor, entry); 100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *"); 101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int"); 102 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *"); 103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *"); 104 SDT_PROBE_DEFINE(proc, kernel, dtor, return); 105 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *"); 106 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int"); 107 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *"); 108 SDT_PROBE_DEFINE(proc, kernel, init, entry); 109 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *"); 110 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int"); 111 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int"); 112 SDT_PROBE_DEFINE(proc, kernel, init, return); 113 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *"); 114 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int"); 115 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int"); 116 117 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 118 MALLOC_DEFINE(M_SESSION, "session", "session header"); 119 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 120 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 121 122 static void doenterpgrp(struct proc *, struct pgrp *); 123 static void orphanpg(struct pgrp *pg); 124 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); 125 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 126 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 127 int preferthread); 128 static void pgadjustjobc(struct pgrp *pgrp, int entering); 129 static void pgdelete(struct pgrp *); 130 static int proc_ctor(void *mem, int size, void *arg, int flags); 131 static void proc_dtor(void *mem, int size, void *arg); 132 static int proc_init(void *mem, int size, int flags); 133 static void proc_fini(void *mem, int size); 134 static void pargs_free(struct pargs *pa); 135 136 /* 137 * Other process lists 138 */ 139 struct pidhashhead *pidhashtbl; 140 u_long pidhash; 141 struct pgrphashhead *pgrphashtbl; 142 u_long pgrphash; 143 struct proclist allproc; 144 struct proclist zombproc; 145 struct sx allproc_lock; 146 struct sx proctree_lock; 147 struct mtx ppeers_lock; 148 uma_zone_t proc_zone; 149 150 int kstack_pages = KSTACK_PAGES; 151 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 152 153 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 154 #ifdef COMPAT_FREEBSD32 155 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); 156 #endif 157 158 /* 159 * Initialize global process hashing structures. 160 */ 161 void 162 procinit() 163 { 164 165 sx_init(&allproc_lock, "allproc"); 166 sx_init(&proctree_lock, "proctree"); 167 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 168 LIST_INIT(&allproc); 169 LIST_INIT(&zombproc); 170 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 171 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 172 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 173 proc_ctor, proc_dtor, proc_init, proc_fini, 174 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 175 uihashinit(); 176 } 177 178 /* 179 * Prepare a proc for use. 180 */ 181 static int 182 proc_ctor(void *mem, int size, void *arg, int flags) 183 { 184 struct proc *p; 185 186 p = (struct proc *)mem; 187 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0); 188 EVENTHANDLER_INVOKE(process_ctor, p); 189 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0); 190 return (0); 191 } 192 193 /* 194 * Reclaim a proc after use. 195 */ 196 static void 197 proc_dtor(void *mem, int size, void *arg) 198 { 199 struct proc *p; 200 struct thread *td; 201 202 /* INVARIANTS checks go here */ 203 p = (struct proc *)mem; 204 td = FIRST_THREAD_IN_PROC(p); 205 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0); 206 if (td != NULL) { 207 #ifdef INVARIANTS 208 KASSERT((p->p_numthreads == 1), 209 ("bad number of threads in exiting process")); 210 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 211 #endif 212 /* Free all OSD associated to this thread. */ 213 osd_thread_exit(td); 214 } 215 EVENTHANDLER_INVOKE(process_dtor, p); 216 if (p->p_ksi != NULL) 217 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 218 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0); 219 } 220 221 /* 222 * Initialize type-stable parts of a proc (when newly created). 223 */ 224 static int 225 proc_init(void *mem, int size, int flags) 226 { 227 struct proc *p; 228 229 p = (struct proc *)mem; 230 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0); 231 p->p_sched = (struct p_sched *)&p[1]; 232 bzero(&p->p_mtx, sizeof(struct mtx)); 233 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 234 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 235 cv_init(&p->p_pwait, "ppwait"); 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(p) 270 register struct proc *p; 271 { 272 273 sx_assert(&proctree_lock, SX_LOCKED); 274 for (; p != curproc; p = p->p_pptr) 275 if (p->p_pid == 0) 276 return (0); 277 return (1); 278 } 279 280 /* 281 * Locate a process by number; return only "live" processes -- i.e., neither 282 * zombies nor newly born but incompletely initialized processes. By not 283 * returning processes in the PRS_NEW state, we allow callers to avoid 284 * testing for that condition to avoid dereferencing p_ucred, et al. 285 */ 286 struct proc * 287 pfind(pid) 288 register pid_t pid; 289 { 290 register struct proc *p; 291 292 sx_slock(&allproc_lock); 293 LIST_FOREACH(p, PIDHASH(pid), p_hash) 294 if (p->p_pid == pid) { 295 if (p->p_state == PRS_NEW) { 296 p = NULL; 297 break; 298 } 299 PROC_LOCK(p); 300 break; 301 } 302 sx_sunlock(&allproc_lock); 303 return (p); 304 } 305 306 /* 307 * Locate a process group by number. 308 * The caller must hold proctree_lock. 309 */ 310 struct pgrp * 311 pgfind(pgid) 312 register pid_t pgid; 313 { 314 register struct pgrp *pgrp; 315 316 sx_assert(&proctree_lock, SX_LOCKED); 317 318 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 319 if (pgrp->pg_id == pgid) { 320 PGRP_LOCK(pgrp); 321 return (pgrp); 322 } 323 } 324 return (NULL); 325 } 326 327 /* 328 * Create a new process group. 329 * pgid must be equal to the pid of p. 330 * Begin a new session if required. 331 */ 332 int 333 enterpgrp(p, pgid, pgrp, sess) 334 register struct proc *p; 335 pid_t pgid; 336 struct pgrp *pgrp; 337 struct session *sess; 338 { 339 struct pgrp *pgrp2; 340 341 sx_assert(&proctree_lock, SX_XLOCKED); 342 343 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 344 KASSERT(p->p_pid == pgid, 345 ("enterpgrp: new pgrp and pid != pgid")); 346 347 pgrp2 = pgfind(pgid); 348 349 KASSERT(pgrp2 == NULL, 350 ("enterpgrp: pgrp with pgid exists")); 351 KASSERT(!SESS_LEADER(p), 352 ("enterpgrp: session leader attempted setpgrp")); 353 354 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 355 356 if (sess != NULL) { 357 /* 358 * new session 359 */ 360 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 361 PROC_LOCK(p); 362 p->p_flag &= ~P_CONTROLT; 363 PROC_UNLOCK(p); 364 PGRP_LOCK(pgrp); 365 sess->s_leader = p; 366 sess->s_sid = p->p_pid; 367 refcount_init(&sess->s_count, 1); 368 sess->s_ttyvp = NULL; 369 sess->s_ttydp = NULL; 370 sess->s_ttyp = NULL; 371 bcopy(p->p_session->s_login, sess->s_login, 372 sizeof(sess->s_login)); 373 pgrp->pg_session = sess; 374 KASSERT(p == curproc, 375 ("enterpgrp: mksession and p != curproc")); 376 } else { 377 pgrp->pg_session = p->p_session; 378 sess_hold(pgrp->pg_session); 379 PGRP_LOCK(pgrp); 380 } 381 pgrp->pg_id = pgid; 382 LIST_INIT(&pgrp->pg_members); 383 384 /* 385 * As we have an exclusive lock of proctree_lock, 386 * this should not deadlock. 387 */ 388 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 389 pgrp->pg_jobc = 0; 390 SLIST_INIT(&pgrp->pg_sigiolst); 391 PGRP_UNLOCK(pgrp); 392 393 doenterpgrp(p, pgrp); 394 395 return (0); 396 } 397 398 /* 399 * Move p to an existing process group 400 */ 401 int 402 enterthispgrp(p, pgrp) 403 register struct proc *p; 404 struct pgrp *pgrp; 405 { 406 407 sx_assert(&proctree_lock, SX_XLOCKED); 408 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 409 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 410 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 411 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 412 KASSERT(pgrp->pg_session == p->p_session, 413 ("%s: pgrp's session %p, p->p_session %p.\n", 414 __func__, 415 pgrp->pg_session, 416 p->p_session)); 417 KASSERT(pgrp != p->p_pgrp, 418 ("%s: p belongs to pgrp.", __func__)); 419 420 doenterpgrp(p, pgrp); 421 422 return (0); 423 } 424 425 /* 426 * Move p to a process group 427 */ 428 static void 429 doenterpgrp(p, pgrp) 430 struct proc *p; 431 struct pgrp *pgrp; 432 { 433 struct pgrp *savepgrp; 434 435 sx_assert(&proctree_lock, SX_XLOCKED); 436 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 437 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 438 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 439 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 440 441 savepgrp = p->p_pgrp; 442 443 /* 444 * Adjust eligibility of affected pgrps to participate in job control. 445 * Increment eligibility counts before decrementing, otherwise we 446 * could reach 0 spuriously during the first call. 447 */ 448 fixjobc(p, pgrp, 1); 449 fixjobc(p, p->p_pgrp, 0); 450 451 PGRP_LOCK(pgrp); 452 PGRP_LOCK(savepgrp); 453 PROC_LOCK(p); 454 LIST_REMOVE(p, p_pglist); 455 p->p_pgrp = pgrp; 456 PROC_UNLOCK(p); 457 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 458 PGRP_UNLOCK(savepgrp); 459 PGRP_UNLOCK(pgrp); 460 if (LIST_EMPTY(&savepgrp->pg_members)) 461 pgdelete(savepgrp); 462 } 463 464 /* 465 * remove process from process group 466 */ 467 int 468 leavepgrp(p) 469 register struct proc *p; 470 { 471 struct pgrp *savepgrp; 472 473 sx_assert(&proctree_lock, SX_XLOCKED); 474 savepgrp = p->p_pgrp; 475 PGRP_LOCK(savepgrp); 476 PROC_LOCK(p); 477 LIST_REMOVE(p, p_pglist); 478 p->p_pgrp = NULL; 479 PROC_UNLOCK(p); 480 PGRP_UNLOCK(savepgrp); 481 if (LIST_EMPTY(&savepgrp->pg_members)) 482 pgdelete(savepgrp); 483 return (0); 484 } 485 486 /* 487 * delete a process group 488 */ 489 static void 490 pgdelete(pgrp) 491 register struct pgrp *pgrp; 492 { 493 struct session *savesess; 494 struct tty *tp; 495 496 sx_assert(&proctree_lock, SX_XLOCKED); 497 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 498 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 499 500 /* 501 * Reset any sigio structures pointing to us as a result of 502 * F_SETOWN with our pgid. 503 */ 504 funsetownlst(&pgrp->pg_sigiolst); 505 506 PGRP_LOCK(pgrp); 507 tp = pgrp->pg_session->s_ttyp; 508 LIST_REMOVE(pgrp, pg_hash); 509 savesess = pgrp->pg_session; 510 PGRP_UNLOCK(pgrp); 511 512 /* Remove the reference to the pgrp before deallocating it. */ 513 if (tp != NULL) { 514 tty_lock(tp); 515 tty_rel_pgrp(tp, pgrp); 516 } 517 518 mtx_destroy(&pgrp->pg_mtx); 519 free(pgrp, M_PGRP); 520 sess_release(savesess); 521 } 522 523 static void 524 pgadjustjobc(pgrp, entering) 525 struct pgrp *pgrp; 526 int entering; 527 { 528 529 PGRP_LOCK(pgrp); 530 if (entering) 531 pgrp->pg_jobc++; 532 else { 533 --pgrp->pg_jobc; 534 if (pgrp->pg_jobc == 0) 535 orphanpg(pgrp); 536 } 537 PGRP_UNLOCK(pgrp); 538 } 539 540 /* 541 * Adjust pgrp jobc counters when specified process changes process group. 542 * We count the number of processes in each process group that "qualify" 543 * the group for terminal job control (those with a parent in a different 544 * process group of the same session). If that count reaches zero, the 545 * process group becomes orphaned. Check both the specified process' 546 * process group and that of its children. 547 * entering == 0 => p is leaving specified group. 548 * entering == 1 => p is entering specified group. 549 */ 550 void 551 fixjobc(p, pgrp, entering) 552 register struct proc *p; 553 register struct pgrp *pgrp; 554 int entering; 555 { 556 register struct pgrp *hispgrp; 557 register struct session *mysession; 558 559 sx_assert(&proctree_lock, SX_LOCKED); 560 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 561 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 562 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 563 564 /* 565 * Check p's parent to see whether p qualifies its own process 566 * group; if so, adjust count for p's process group. 567 */ 568 mysession = pgrp->pg_session; 569 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 570 hispgrp->pg_session == mysession) 571 pgadjustjobc(pgrp, entering); 572 573 /* 574 * Check this process' children to see whether they qualify 575 * their process groups; if so, adjust counts for children's 576 * process groups. 577 */ 578 LIST_FOREACH(p, &p->p_children, p_sibling) { 579 hispgrp = p->p_pgrp; 580 if (hispgrp == pgrp || 581 hispgrp->pg_session != mysession) 582 continue; 583 PROC_LOCK(p); 584 if (p->p_state == PRS_ZOMBIE) { 585 PROC_UNLOCK(p); 586 continue; 587 } 588 PROC_UNLOCK(p); 589 pgadjustjobc(hispgrp, entering); 590 } 591 } 592 593 /* 594 * A process group has become orphaned; 595 * if there are any stopped processes in the group, 596 * hang-up all process in that group. 597 */ 598 static void 599 orphanpg(pg) 600 struct pgrp *pg; 601 { 602 register struct proc *p; 603 604 PGRP_LOCK_ASSERT(pg, MA_OWNED); 605 606 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 607 PROC_LOCK(p); 608 if (P_SHOULDSTOP(p)) { 609 PROC_UNLOCK(p); 610 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 611 PROC_LOCK(p); 612 psignal(p, SIGHUP); 613 psignal(p, SIGCONT); 614 PROC_UNLOCK(p); 615 } 616 return; 617 } 618 PROC_UNLOCK(p); 619 } 620 } 621 622 void 623 sess_hold(struct session *s) 624 { 625 626 refcount_acquire(&s->s_count); 627 } 628 629 void 630 sess_release(struct session *s) 631 { 632 633 if (refcount_release(&s->s_count)) { 634 if (s->s_ttyp != NULL) { 635 tty_lock(s->s_ttyp); 636 tty_rel_sess(s->s_ttyp, s); 637 } 638 mtx_destroy(&s->s_mtx); 639 free(s, M_SESSION); 640 } 641 } 642 643 #include "opt_ddb.h" 644 #ifdef DDB 645 #include <ddb/ddb.h> 646 647 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 648 { 649 register struct pgrp *pgrp; 650 register struct proc *p; 651 register int i; 652 653 for (i = 0; i <= pgrphash; i++) { 654 if (!LIST_EMPTY(&pgrphashtbl[i])) { 655 printf("\tindx %d\n", i); 656 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 657 printf( 658 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 659 (void *)pgrp, (long)pgrp->pg_id, 660 (void *)pgrp->pg_session, 661 pgrp->pg_session->s_count, 662 (void *)LIST_FIRST(&pgrp->pg_members)); 663 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 664 printf("\t\tpid %ld addr %p pgrp %p\n", 665 (long)p->p_pid, (void *)p, 666 (void *)p->p_pgrp); 667 } 668 } 669 } 670 } 671 } 672 #endif /* DDB */ 673 674 /* 675 * Calculate the kinfo_proc members which contain process-wide 676 * informations. 677 * Must be called with the target process locked. 678 */ 679 static void 680 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) 681 { 682 struct thread *td; 683 684 PROC_LOCK_ASSERT(p, MA_OWNED); 685 686 kp->ki_estcpu = 0; 687 kp->ki_pctcpu = 0; 688 FOREACH_THREAD_IN_PROC(p, td) { 689 thread_lock(td); 690 kp->ki_pctcpu += sched_pctcpu(td); 691 kp->ki_estcpu += td->td_estcpu; 692 thread_unlock(td); 693 } 694 } 695 696 /* 697 * Clear kinfo_proc and fill in any information that is common 698 * to all threads in the process. 699 * Must be called with the target process locked. 700 */ 701 static void 702 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 703 { 704 struct thread *td0; 705 struct tty *tp; 706 struct session *sp; 707 struct ucred *cred; 708 struct sigacts *ps; 709 710 PROC_LOCK_ASSERT(p, MA_OWNED); 711 bzero(kp, sizeof(*kp)); 712 713 kp->ki_structsize = sizeof(*kp); 714 kp->ki_paddr = p; 715 kp->ki_addr =/* p->p_addr; */0; /* XXX */ 716 kp->ki_args = p->p_args; 717 kp->ki_textvp = p->p_textvp; 718 #ifdef KTRACE 719 kp->ki_tracep = p->p_tracevp; 720 mtx_lock(&ktrace_mtx); 721 kp->ki_traceflag = p->p_traceflag; 722 mtx_unlock(&ktrace_mtx); 723 #endif 724 kp->ki_fd = p->p_fd; 725 kp->ki_vmspace = p->p_vmspace; 726 kp->ki_flag = p->p_flag; 727 cred = p->p_ucred; 728 if (cred) { 729 kp->ki_uid = cred->cr_uid; 730 kp->ki_ruid = cred->cr_ruid; 731 kp->ki_svuid = cred->cr_svuid; 732 kp->ki_cr_flags = cred->cr_flags; 733 /* XXX bde doesn't like KI_NGROUPS */ 734 if (cred->cr_ngroups > KI_NGROUPS) { 735 kp->ki_ngroups = KI_NGROUPS; 736 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 737 } else 738 kp->ki_ngroups = cred->cr_ngroups; 739 bcopy(cred->cr_groups, kp->ki_groups, 740 kp->ki_ngroups * sizeof(gid_t)); 741 kp->ki_rgid = cred->cr_rgid; 742 kp->ki_svgid = cred->cr_svgid; 743 /* If jailed(cred), emulate the old P_JAILED flag. */ 744 if (jailed(cred)) { 745 kp->ki_flag |= P_JAILED; 746 /* If inside the jail, use 0 as a jail ID. */ 747 if (cred->cr_prison != curthread->td_ucred->cr_prison) 748 kp->ki_jid = cred->cr_prison->pr_id; 749 } 750 } 751 ps = p->p_sigacts; 752 if (ps) { 753 mtx_lock(&ps->ps_mtx); 754 kp->ki_sigignore = ps->ps_sigignore; 755 kp->ki_sigcatch = ps->ps_sigcatch; 756 mtx_unlock(&ps->ps_mtx); 757 } 758 PROC_SLOCK(p); 759 if (p->p_state != PRS_NEW && 760 p->p_state != PRS_ZOMBIE && 761 p->p_vmspace != NULL) { 762 struct vmspace *vm = p->p_vmspace; 763 764 kp->ki_size = vm->vm_map.size; 765 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 766 FOREACH_THREAD_IN_PROC(p, td0) { 767 if (!TD_IS_SWAPPED(td0)) 768 kp->ki_rssize += td0->td_kstack_pages; 769 } 770 kp->ki_swrss = vm->vm_swrss; 771 kp->ki_tsize = vm->vm_tsize; 772 kp->ki_dsize = vm->vm_dsize; 773 kp->ki_ssize = vm->vm_ssize; 774 } else if (p->p_state == PRS_ZOMBIE) 775 kp->ki_stat = SZOMB; 776 if (kp->ki_flag & P_INMEM) 777 kp->ki_sflag = PS_INMEM; 778 else 779 kp->ki_sflag = 0; 780 /* Calculate legacy swtime as seconds since 'swtick'. */ 781 kp->ki_swtime = (ticks - p->p_swtick) / hz; 782 kp->ki_pid = p->p_pid; 783 kp->ki_nice = p->p_nice; 784 rufetch(p, &kp->ki_rusage); 785 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 786 PROC_SUNLOCK(p); 787 if ((p->p_flag & P_INMEM) && p->p_stats != NULL) { 788 kp->ki_start = p->p_stats->p_start; 789 timevaladd(&kp->ki_start, &boottime); 790 PROC_SLOCK(p); 791 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 792 PROC_SUNLOCK(p); 793 calccru(p, &kp->ki_childutime, &kp->ki_childstime); 794 795 /* Some callers want child-times in a single value */ 796 kp->ki_childtime = kp->ki_childstime; 797 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 798 } 799 tp = NULL; 800 if (p->p_pgrp) { 801 kp->ki_pgid = p->p_pgrp->pg_id; 802 kp->ki_jobc = p->p_pgrp->pg_jobc; 803 sp = p->p_pgrp->pg_session; 804 805 if (sp != NULL) { 806 kp->ki_sid = sp->s_sid; 807 SESS_LOCK(sp); 808 strlcpy(kp->ki_login, sp->s_login, 809 sizeof(kp->ki_login)); 810 if (sp->s_ttyvp) 811 kp->ki_kiflag |= KI_CTTY; 812 if (SESS_LEADER(p)) 813 kp->ki_kiflag |= KI_SLEADER; 814 /* XXX proctree_lock */ 815 tp = sp->s_ttyp; 816 SESS_UNLOCK(sp); 817 } 818 } 819 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 820 kp->ki_tdev = tty_udev(tp); 821 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 822 if (tp->t_session) 823 kp->ki_tsid = tp->t_session->s_sid; 824 } else 825 kp->ki_tdev = NODEV; 826 if (p->p_comm[0] != '\0') 827 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 828 if (p->p_sysent && p->p_sysent->sv_name != NULL && 829 p->p_sysent->sv_name[0] != '\0') 830 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 831 kp->ki_siglist = p->p_siglist; 832 kp->ki_xstat = p->p_xstat; 833 kp->ki_acflag = p->p_acflag; 834 kp->ki_lock = p->p_lock; 835 if (p->p_pptr) 836 kp->ki_ppid = p->p_pptr->p_pid; 837 } 838 839 /* 840 * Fill in information that is thread specific. Must be called with 841 * target process locked. If 'preferthread' is set, overwrite certain 842 * process-related fields that are maintained for both threads and 843 * processes. 844 */ 845 static void 846 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 847 { 848 struct proc *p; 849 850 p = td->td_proc; 851 PROC_LOCK_ASSERT(p, MA_OWNED); 852 853 thread_lock(td); 854 if (td->td_wmesg != NULL) 855 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 856 else 857 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 858 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 859 if (TD_ON_LOCK(td)) { 860 kp->ki_kiflag |= KI_LOCKBLOCK; 861 strlcpy(kp->ki_lockname, td->td_lockname, 862 sizeof(kp->ki_lockname)); 863 } else { 864 kp->ki_kiflag &= ~KI_LOCKBLOCK; 865 bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 866 } 867 868 if (p->p_state == PRS_NORMAL) { /* approximate. */ 869 if (TD_ON_RUNQ(td) || 870 TD_CAN_RUN(td) || 871 TD_IS_RUNNING(td)) { 872 kp->ki_stat = SRUN; 873 } else if (P_SHOULDSTOP(p)) { 874 kp->ki_stat = SSTOP; 875 } else if (TD_IS_SLEEPING(td)) { 876 kp->ki_stat = SSLEEP; 877 } else if (TD_ON_LOCK(td)) { 878 kp->ki_stat = SLOCK; 879 } else { 880 kp->ki_stat = SWAIT; 881 } 882 } else if (p->p_state == PRS_ZOMBIE) { 883 kp->ki_stat = SZOMB; 884 } else { 885 kp->ki_stat = SIDL; 886 } 887 888 /* Things in the thread */ 889 kp->ki_wchan = td->td_wchan; 890 kp->ki_pri.pri_level = td->td_priority; 891 kp->ki_pri.pri_native = td->td_base_pri; 892 kp->ki_lastcpu = td->td_lastcpu; 893 kp->ki_oncpu = td->td_oncpu; 894 kp->ki_tdflags = td->td_flags; 895 kp->ki_tid = td->td_tid; 896 kp->ki_numthreads = p->p_numthreads; 897 kp->ki_pcb = td->td_pcb; 898 kp->ki_kstack = (void *)td->td_kstack; 899 kp->ki_slptime = (ticks - td->td_slptick) / hz; 900 kp->ki_pri.pri_class = td->td_pri_class; 901 kp->ki_pri.pri_user = td->td_user_pri; 902 903 if (preferthread) { 904 kp->ki_runtime = cputick2usec(td->td_runtime); 905 kp->ki_pctcpu = sched_pctcpu(td); 906 kp->ki_estcpu = td->td_estcpu; 907 } 908 909 /* We can't get this anymore but ps etc never used it anyway. */ 910 kp->ki_rqindex = 0; 911 912 if (preferthread) 913 kp->ki_siglist = td->td_siglist; 914 kp->ki_sigmask = td->td_sigmask; 915 thread_unlock(td); 916 } 917 918 /* 919 * Fill in a kinfo_proc structure for the specified process. 920 * Must be called with the target process locked. 921 */ 922 void 923 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 924 { 925 926 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 927 928 fill_kinfo_proc_only(p, kp); 929 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 930 fill_kinfo_aggregate(p, kp); 931 } 932 933 struct pstats * 934 pstats_alloc(void) 935 { 936 937 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 938 } 939 940 /* 941 * Copy parts of p_stats; zero the rest of p_stats (statistics). 942 */ 943 void 944 pstats_fork(struct pstats *src, struct pstats *dst) 945 { 946 947 bzero(&dst->pstat_startzero, 948 __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 949 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 950 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 951 } 952 953 void 954 pstats_free(struct pstats *ps) 955 { 956 957 free(ps, M_SUBPROC); 958 } 959 960 /* 961 * Locate a zombie process by number 962 */ 963 struct proc * 964 zpfind(pid_t pid) 965 { 966 struct proc *p; 967 968 sx_slock(&allproc_lock); 969 LIST_FOREACH(p, &zombproc, p_list) 970 if (p->p_pid == pid) { 971 PROC_LOCK(p); 972 break; 973 } 974 sx_sunlock(&allproc_lock); 975 return (p); 976 } 977 978 #define KERN_PROC_ZOMBMASK 0x3 979 #define KERN_PROC_NOTHREADS 0x4 980 981 #ifdef COMPAT_FREEBSD32 982 983 /* 984 * This function is typically used to copy out the kernel address, so 985 * it can be replaced by assignment of zero. 986 */ 987 static inline uint32_t 988 ptr32_trim(void *ptr) 989 { 990 uintptr_t uptr; 991 992 uptr = (uintptr_t)ptr; 993 return ((uptr > UINT_MAX) ? 0 : uptr); 994 } 995 996 #define PTRTRIM_CP(src,dst,fld) \ 997 do { (dst).fld = ptr32_trim((src).fld); } while (0) 998 999 static void 1000 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 1001 { 1002 int i; 1003 1004 bzero(ki32, sizeof(struct kinfo_proc32)); 1005 ki32->ki_structsize = sizeof(struct kinfo_proc32); 1006 CP(*ki, *ki32, ki_layout); 1007 PTRTRIM_CP(*ki, *ki32, ki_args); 1008 PTRTRIM_CP(*ki, *ki32, ki_paddr); 1009 PTRTRIM_CP(*ki, *ki32, ki_addr); 1010 PTRTRIM_CP(*ki, *ki32, ki_tracep); 1011 PTRTRIM_CP(*ki, *ki32, ki_textvp); 1012 PTRTRIM_CP(*ki, *ki32, ki_fd); 1013 PTRTRIM_CP(*ki, *ki32, ki_vmspace); 1014 PTRTRIM_CP(*ki, *ki32, ki_wchan); 1015 CP(*ki, *ki32, ki_pid); 1016 CP(*ki, *ki32, ki_ppid); 1017 CP(*ki, *ki32, ki_pgid); 1018 CP(*ki, *ki32, ki_tpgid); 1019 CP(*ki, *ki32, ki_sid); 1020 CP(*ki, *ki32, ki_tsid); 1021 CP(*ki, *ki32, ki_jobc); 1022 CP(*ki, *ki32, ki_tdev); 1023 CP(*ki, *ki32, ki_siglist); 1024 CP(*ki, *ki32, ki_sigmask); 1025 CP(*ki, *ki32, ki_sigignore); 1026 CP(*ki, *ki32, ki_sigcatch); 1027 CP(*ki, *ki32, ki_uid); 1028 CP(*ki, *ki32, ki_ruid); 1029 CP(*ki, *ki32, ki_svuid); 1030 CP(*ki, *ki32, ki_rgid); 1031 CP(*ki, *ki32, ki_svgid); 1032 CP(*ki, *ki32, ki_ngroups); 1033 for (i = 0; i < KI_NGROUPS; i++) 1034 CP(*ki, *ki32, ki_groups[i]); 1035 CP(*ki, *ki32, ki_size); 1036 CP(*ki, *ki32, ki_rssize); 1037 CP(*ki, *ki32, ki_swrss); 1038 CP(*ki, *ki32, ki_tsize); 1039 CP(*ki, *ki32, ki_dsize); 1040 CP(*ki, *ki32, ki_ssize); 1041 CP(*ki, *ki32, ki_xstat); 1042 CP(*ki, *ki32, ki_acflag); 1043 CP(*ki, *ki32, ki_pctcpu); 1044 CP(*ki, *ki32, ki_estcpu); 1045 CP(*ki, *ki32, ki_slptime); 1046 CP(*ki, *ki32, ki_swtime); 1047 CP(*ki, *ki32, ki_runtime); 1048 TV_CP(*ki, *ki32, ki_start); 1049 TV_CP(*ki, *ki32, ki_childtime); 1050 CP(*ki, *ki32, ki_flag); 1051 CP(*ki, *ki32, ki_kiflag); 1052 CP(*ki, *ki32, ki_traceflag); 1053 CP(*ki, *ki32, ki_stat); 1054 CP(*ki, *ki32, ki_nice); 1055 CP(*ki, *ki32, ki_lock); 1056 CP(*ki, *ki32, ki_rqindex); 1057 CP(*ki, *ki32, ki_oncpu); 1058 CP(*ki, *ki32, ki_lastcpu); 1059 bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1); 1060 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 1061 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 1062 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 1063 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 1064 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 1065 CP(*ki, *ki32, ki_cr_flags); 1066 CP(*ki, *ki32, ki_jid); 1067 CP(*ki, *ki32, ki_numthreads); 1068 CP(*ki, *ki32, ki_tid); 1069 CP(*ki, *ki32, ki_pri); 1070 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 1071 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 1072 PTRTRIM_CP(*ki, *ki32, ki_pcb); 1073 PTRTRIM_CP(*ki, *ki32, ki_kstack); 1074 PTRTRIM_CP(*ki, *ki32, ki_udata); 1075 CP(*ki, *ki32, ki_sflag); 1076 CP(*ki, *ki32, ki_tdflags); 1077 } 1078 1079 static int 1080 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1081 { 1082 struct kinfo_proc32 ki32; 1083 int error; 1084 1085 if (req->flags & SCTL_MASK32) { 1086 freebsd32_kinfo_proc_out(ki, &ki32); 1087 error = SYSCTL_OUT(req, (caddr_t)&ki32, 1088 sizeof(struct kinfo_proc32)); 1089 } else 1090 error = SYSCTL_OUT(req, (caddr_t)ki, 1091 sizeof(struct kinfo_proc)); 1092 return (error); 1093 } 1094 #else 1095 static int 1096 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 1097 { 1098 1099 return (SYSCTL_OUT(req, (caddr_t)ki, sizeof(struct kinfo_proc))); 1100 } 1101 #endif 1102 1103 /* 1104 * Must be called with the process locked and will return with it unlocked. 1105 */ 1106 static int 1107 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 1108 { 1109 struct thread *td; 1110 struct kinfo_proc kinfo_proc; 1111 int error = 0; 1112 struct proc *np; 1113 pid_t pid = p->p_pid; 1114 1115 PROC_LOCK_ASSERT(p, MA_OWNED); 1116 MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 1117 1118 fill_kinfo_proc(p, &kinfo_proc); 1119 if (flags & KERN_PROC_NOTHREADS) 1120 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1121 else { 1122 FOREACH_THREAD_IN_PROC(p, td) { 1123 fill_kinfo_thread(td, &kinfo_proc, 1); 1124 error = sysctl_out_proc_copyout(&kinfo_proc, req); 1125 if (error) 1126 break; 1127 } 1128 } 1129 PROC_UNLOCK(p); 1130 if (error) 1131 return (error); 1132 if (flags & KERN_PROC_ZOMBMASK) 1133 np = zpfind(pid); 1134 else { 1135 if (pid == 0) 1136 return (0); 1137 np = pfind(pid); 1138 } 1139 if (np == NULL) 1140 return (ESRCH); 1141 if (np != p) { 1142 PROC_UNLOCK(np); 1143 return (ESRCH); 1144 } 1145 PROC_UNLOCK(np); 1146 return (0); 1147 } 1148 1149 static int 1150 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1151 { 1152 int *name = (int*) arg1; 1153 u_int namelen = arg2; 1154 struct proc *p; 1155 int flags, doingzomb, oid_number; 1156 int error = 0; 1157 1158 oid_number = oidp->oid_number; 1159 if (oid_number != KERN_PROC_ALL && 1160 (oid_number & KERN_PROC_INC_THREAD) == 0) 1161 flags = KERN_PROC_NOTHREADS; 1162 else { 1163 flags = 0; 1164 oid_number &= ~KERN_PROC_INC_THREAD; 1165 } 1166 if (oid_number == KERN_PROC_PID) { 1167 if (namelen != 1) 1168 return (EINVAL); 1169 error = sysctl_wire_old_buffer(req, 0); 1170 if (error) 1171 return (error); 1172 p = pfind((pid_t)name[0]); 1173 if (!p) 1174 return (ESRCH); 1175 if ((error = p_cansee(curthread, p))) { 1176 PROC_UNLOCK(p); 1177 return (error); 1178 } 1179 error = sysctl_out_proc(p, req, flags); 1180 return (error); 1181 } 1182 1183 switch (oid_number) { 1184 case KERN_PROC_ALL: 1185 if (namelen != 0) 1186 return (EINVAL); 1187 break; 1188 case KERN_PROC_PROC: 1189 if (namelen != 0 && namelen != 1) 1190 return (EINVAL); 1191 break; 1192 default: 1193 if (namelen != 1) 1194 return (EINVAL); 1195 break; 1196 } 1197 1198 if (!req->oldptr) { 1199 /* overestimate by 5 procs */ 1200 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1201 if (error) 1202 return (error); 1203 } 1204 error = sysctl_wire_old_buffer(req, 0); 1205 if (error != 0) 1206 return (error); 1207 sx_slock(&allproc_lock); 1208 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 1209 if (!doingzomb) 1210 p = LIST_FIRST(&allproc); 1211 else 1212 p = LIST_FIRST(&zombproc); 1213 for (; p != 0; p = LIST_NEXT(p, p_list)) { 1214 /* 1215 * Skip embryonic processes. 1216 */ 1217 PROC_SLOCK(p); 1218 if (p->p_state == PRS_NEW) { 1219 PROC_SUNLOCK(p); 1220 continue; 1221 } 1222 PROC_SUNLOCK(p); 1223 PROC_LOCK(p); 1224 KASSERT(p->p_ucred != NULL, 1225 ("process credential is NULL for non-NEW proc")); 1226 /* 1227 * Show a user only appropriate processes. 1228 */ 1229 if (p_cansee(curthread, p)) { 1230 PROC_UNLOCK(p); 1231 continue; 1232 } 1233 /* 1234 * TODO - make more efficient (see notes below). 1235 * do by session. 1236 */ 1237 switch (oid_number) { 1238 1239 case KERN_PROC_GID: 1240 if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1241 PROC_UNLOCK(p); 1242 continue; 1243 } 1244 break; 1245 1246 case KERN_PROC_PGRP: 1247 /* could do this by traversing pgrp */ 1248 if (p->p_pgrp == NULL || 1249 p->p_pgrp->pg_id != (pid_t)name[0]) { 1250 PROC_UNLOCK(p); 1251 continue; 1252 } 1253 break; 1254 1255 case KERN_PROC_RGID: 1256 if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1257 PROC_UNLOCK(p); 1258 continue; 1259 } 1260 break; 1261 1262 case KERN_PROC_SESSION: 1263 if (p->p_session == NULL || 1264 p->p_session->s_sid != (pid_t)name[0]) { 1265 PROC_UNLOCK(p); 1266 continue; 1267 } 1268 break; 1269 1270 case KERN_PROC_TTY: 1271 if ((p->p_flag & P_CONTROLT) == 0 || 1272 p->p_session == NULL) { 1273 PROC_UNLOCK(p); 1274 continue; 1275 } 1276 /* XXX proctree_lock */ 1277 SESS_LOCK(p->p_session); 1278 if (p->p_session->s_ttyp == NULL || 1279 tty_udev(p->p_session->s_ttyp) != 1280 (dev_t)name[0]) { 1281 SESS_UNLOCK(p->p_session); 1282 PROC_UNLOCK(p); 1283 continue; 1284 } 1285 SESS_UNLOCK(p->p_session); 1286 break; 1287 1288 case KERN_PROC_UID: 1289 if (p->p_ucred->cr_uid != (uid_t)name[0]) { 1290 PROC_UNLOCK(p); 1291 continue; 1292 } 1293 break; 1294 1295 case KERN_PROC_RUID: 1296 if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 1297 PROC_UNLOCK(p); 1298 continue; 1299 } 1300 break; 1301 1302 case KERN_PROC_PROC: 1303 break; 1304 1305 default: 1306 break; 1307 1308 } 1309 1310 error = sysctl_out_proc(p, req, flags | doingzomb); 1311 if (error) { 1312 sx_sunlock(&allproc_lock); 1313 return (error); 1314 } 1315 } 1316 } 1317 sx_sunlock(&allproc_lock); 1318 return (0); 1319 } 1320 1321 struct pargs * 1322 pargs_alloc(int len) 1323 { 1324 struct pargs *pa; 1325 1326 pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1327 M_WAITOK); 1328 refcount_init(&pa->ar_ref, 1); 1329 pa->ar_length = len; 1330 return (pa); 1331 } 1332 1333 static void 1334 pargs_free(struct pargs *pa) 1335 { 1336 1337 free(pa, M_PARGS); 1338 } 1339 1340 void 1341 pargs_hold(struct pargs *pa) 1342 { 1343 1344 if (pa == NULL) 1345 return; 1346 refcount_acquire(&pa->ar_ref); 1347 } 1348 1349 void 1350 pargs_drop(struct pargs *pa) 1351 { 1352 1353 if (pa == NULL) 1354 return; 1355 if (refcount_release(&pa->ar_ref)) 1356 pargs_free(pa); 1357 } 1358 1359 /* 1360 * This sysctl allows a process to retrieve the argument list or process 1361 * title for another process without groping around in the address space 1362 * of the other process. It also allow a process to set its own "process 1363 * title to a string of its own choice. 1364 */ 1365 static int 1366 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1367 { 1368 int *name = (int*) arg1; 1369 u_int namelen = arg2; 1370 struct pargs *newpa, *pa; 1371 struct proc *p; 1372 int error = 0; 1373 1374 if (namelen != 1) 1375 return (EINVAL); 1376 1377 p = pfind((pid_t)name[0]); 1378 if (!p) 1379 return (ESRCH); 1380 1381 if ((error = p_cansee(curthread, p)) != 0) { 1382 PROC_UNLOCK(p); 1383 return (error); 1384 } 1385 1386 if (req->newptr && curproc != p) { 1387 PROC_UNLOCK(p); 1388 return (EPERM); 1389 } 1390 1391 pa = p->p_args; 1392 pargs_hold(pa); 1393 PROC_UNLOCK(p); 1394 if (req->oldptr != NULL && pa != NULL) 1395 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1396 pargs_drop(pa); 1397 if (error != 0 || req->newptr == NULL) 1398 return (error); 1399 1400 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1401 return (ENOMEM); 1402 newpa = pargs_alloc(req->newlen); 1403 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1404 if (error != 0) { 1405 pargs_free(newpa); 1406 return (error); 1407 } 1408 PROC_LOCK(p); 1409 pa = p->p_args; 1410 p->p_args = newpa; 1411 PROC_UNLOCK(p); 1412 pargs_drop(pa); 1413 return (0); 1414 } 1415 1416 /* 1417 * This sysctl allows a process to retrieve the path of the executable for 1418 * itself or another process. 1419 */ 1420 static int 1421 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1422 { 1423 pid_t *pidp = (pid_t *)arg1; 1424 unsigned int arglen = arg2; 1425 struct proc *p; 1426 struct vnode *vp; 1427 char *retbuf, *freebuf; 1428 int error, vfslocked; 1429 1430 if (arglen != 1) 1431 return (EINVAL); 1432 if (*pidp == -1) { /* -1 means this process */ 1433 p = req->td->td_proc; 1434 } else { 1435 p = pfind(*pidp); 1436 if (p == NULL) 1437 return (ESRCH); 1438 if ((error = p_cansee(curthread, p)) != 0) { 1439 PROC_UNLOCK(p); 1440 return (error); 1441 } 1442 } 1443 1444 vp = p->p_textvp; 1445 if (vp == NULL) { 1446 if (*pidp != -1) 1447 PROC_UNLOCK(p); 1448 return (0); 1449 } 1450 vref(vp); 1451 if (*pidp != -1) 1452 PROC_UNLOCK(p); 1453 error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1454 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1455 vrele(vp); 1456 VFS_UNLOCK_GIANT(vfslocked); 1457 if (error) 1458 return (error); 1459 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1460 free(freebuf, M_TEMP); 1461 return (error); 1462 } 1463 1464 static int 1465 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1466 { 1467 struct proc *p; 1468 char *sv_name; 1469 int *name; 1470 int namelen; 1471 int error; 1472 1473 namelen = arg2; 1474 if (namelen != 1) 1475 return (EINVAL); 1476 1477 name = (int *)arg1; 1478 if ((p = pfind((pid_t)name[0])) == NULL) 1479 return (ESRCH); 1480 if ((error = p_cansee(curthread, p))) { 1481 PROC_UNLOCK(p); 1482 return (error); 1483 } 1484 sv_name = p->p_sysent->sv_name; 1485 PROC_UNLOCK(p); 1486 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1487 } 1488 1489 #ifdef KINFO_OVMENTRY_SIZE 1490 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 1491 #endif 1492 1493 #ifdef COMPAT_FREEBSD7 1494 static int 1495 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 1496 { 1497 vm_map_entry_t entry, tmp_entry; 1498 unsigned int last_timestamp; 1499 char *fullpath, *freepath; 1500 struct kinfo_ovmentry *kve; 1501 struct vattr va; 1502 struct ucred *cred; 1503 int error, *name; 1504 struct vnode *vp; 1505 struct proc *p; 1506 vm_map_t map; 1507 struct vmspace *vm; 1508 1509 name = (int *)arg1; 1510 if ((p = pfind((pid_t)name[0])) == NULL) 1511 return (ESRCH); 1512 if (p->p_flag & P_WEXIT) { 1513 PROC_UNLOCK(p); 1514 return (ESRCH); 1515 } 1516 if ((error = p_candebug(curthread, p))) { 1517 PROC_UNLOCK(p); 1518 return (error); 1519 } 1520 _PHOLD(p); 1521 PROC_UNLOCK(p); 1522 vm = vmspace_acquire_ref(p); 1523 if (vm == NULL) { 1524 PRELE(p); 1525 return (ESRCH); 1526 } 1527 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1528 1529 map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */ 1530 vm_map_lock_read(map); 1531 for (entry = map->header.next; entry != &map->header; 1532 entry = entry->next) { 1533 vm_object_t obj, tobj, lobj; 1534 vm_offset_t addr; 1535 int vfslocked; 1536 1537 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1538 continue; 1539 1540 bzero(kve, sizeof(*kve)); 1541 kve->kve_structsize = sizeof(*kve); 1542 1543 kve->kve_private_resident = 0; 1544 obj = entry->object.vm_object; 1545 if (obj != NULL) { 1546 VM_OBJECT_LOCK(obj); 1547 if (obj->shadow_count == 1) 1548 kve->kve_private_resident = 1549 obj->resident_page_count; 1550 } 1551 kve->kve_resident = 0; 1552 addr = entry->start; 1553 while (addr < entry->end) { 1554 if (pmap_extract(map->pmap, addr)) 1555 kve->kve_resident++; 1556 addr += PAGE_SIZE; 1557 } 1558 1559 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1560 if (tobj != obj) 1561 VM_OBJECT_LOCK(tobj); 1562 if (lobj != obj) 1563 VM_OBJECT_UNLOCK(lobj); 1564 lobj = tobj; 1565 } 1566 1567 kve->kve_start = (void*)entry->start; 1568 kve->kve_end = (void*)entry->end; 1569 kve->kve_offset = (off_t)entry->offset; 1570 1571 if (entry->protection & VM_PROT_READ) 1572 kve->kve_protection |= KVME_PROT_READ; 1573 if (entry->protection & VM_PROT_WRITE) 1574 kve->kve_protection |= KVME_PROT_WRITE; 1575 if (entry->protection & VM_PROT_EXECUTE) 1576 kve->kve_protection |= KVME_PROT_EXEC; 1577 1578 if (entry->eflags & MAP_ENTRY_COW) 1579 kve->kve_flags |= KVME_FLAG_COW; 1580 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1581 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1582 1583 last_timestamp = map->timestamp; 1584 vm_map_unlock_read(map); 1585 1586 kve->kve_fileid = 0; 1587 kve->kve_fsid = 0; 1588 freepath = NULL; 1589 fullpath = ""; 1590 if (lobj) { 1591 vp = NULL; 1592 switch (lobj->type) { 1593 case OBJT_DEFAULT: 1594 kve->kve_type = KVME_TYPE_DEFAULT; 1595 break; 1596 case OBJT_VNODE: 1597 kve->kve_type = KVME_TYPE_VNODE; 1598 vp = lobj->handle; 1599 vref(vp); 1600 break; 1601 case OBJT_SWAP: 1602 kve->kve_type = KVME_TYPE_SWAP; 1603 break; 1604 case OBJT_DEVICE: 1605 kve->kve_type = KVME_TYPE_DEVICE; 1606 break; 1607 case OBJT_PHYS: 1608 kve->kve_type = KVME_TYPE_PHYS; 1609 break; 1610 case OBJT_DEAD: 1611 kve->kve_type = KVME_TYPE_DEAD; 1612 break; 1613 case OBJT_SG: 1614 kve->kve_type = KVME_TYPE_SG; 1615 break; 1616 default: 1617 kve->kve_type = KVME_TYPE_UNKNOWN; 1618 break; 1619 } 1620 if (lobj != obj) 1621 VM_OBJECT_UNLOCK(lobj); 1622 1623 kve->kve_ref_count = obj->ref_count; 1624 kve->kve_shadow_count = obj->shadow_count; 1625 VM_OBJECT_UNLOCK(obj); 1626 if (vp != NULL) { 1627 vn_fullpath(curthread, vp, &fullpath, 1628 &freepath); 1629 cred = curthread->td_ucred; 1630 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1631 vn_lock(vp, LK_SHARED | LK_RETRY); 1632 if (VOP_GETATTR(vp, &va, cred) == 0) { 1633 kve->kve_fileid = va.va_fileid; 1634 kve->kve_fsid = va.va_fsid; 1635 } 1636 vput(vp); 1637 VFS_UNLOCK_GIANT(vfslocked); 1638 } 1639 } else { 1640 kve->kve_type = KVME_TYPE_NONE; 1641 kve->kve_ref_count = 0; 1642 kve->kve_shadow_count = 0; 1643 } 1644 1645 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1646 if (freepath != NULL) 1647 free(freepath, M_TEMP); 1648 1649 error = SYSCTL_OUT(req, kve, sizeof(*kve)); 1650 vm_map_lock_read(map); 1651 if (error) 1652 break; 1653 if (last_timestamp != map->timestamp) { 1654 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1655 entry = tmp_entry; 1656 } 1657 } 1658 vm_map_unlock_read(map); 1659 vmspace_free(vm); 1660 PRELE(p); 1661 free(kve, M_TEMP); 1662 return (error); 1663 } 1664 #endif /* COMPAT_FREEBSD7 */ 1665 1666 #ifdef KINFO_VMENTRY_SIZE 1667 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 1668 #endif 1669 1670 static int 1671 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 1672 { 1673 vm_map_entry_t entry, tmp_entry; 1674 unsigned int last_timestamp; 1675 char *fullpath, *freepath; 1676 struct kinfo_vmentry *kve; 1677 struct vattr va; 1678 struct ucred *cred; 1679 int error, *name; 1680 struct vnode *vp; 1681 struct proc *p; 1682 struct vmspace *vm; 1683 vm_map_t map; 1684 1685 name = (int *)arg1; 1686 if ((p = pfind((pid_t)name[0])) == NULL) 1687 return (ESRCH); 1688 if (p->p_flag & P_WEXIT) { 1689 PROC_UNLOCK(p); 1690 return (ESRCH); 1691 } 1692 if ((error = p_candebug(curthread, p))) { 1693 PROC_UNLOCK(p); 1694 return (error); 1695 } 1696 _PHOLD(p); 1697 PROC_UNLOCK(p); 1698 vm = vmspace_acquire_ref(p); 1699 if (vm == NULL) { 1700 PRELE(p); 1701 return (ESRCH); 1702 } 1703 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1704 1705 map = &vm->vm_map; /* XXXRW: More locking required? */ 1706 vm_map_lock_read(map); 1707 for (entry = map->header.next; entry != &map->header; 1708 entry = entry->next) { 1709 vm_object_t obj, tobj, lobj; 1710 vm_offset_t addr; 1711 int vfslocked; 1712 1713 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1714 continue; 1715 1716 bzero(kve, sizeof(*kve)); 1717 1718 kve->kve_private_resident = 0; 1719 obj = entry->object.vm_object; 1720 if (obj != NULL) { 1721 VM_OBJECT_LOCK(obj); 1722 if (obj->shadow_count == 1) 1723 kve->kve_private_resident = 1724 obj->resident_page_count; 1725 } 1726 kve->kve_resident = 0; 1727 addr = entry->start; 1728 while (addr < entry->end) { 1729 if (pmap_extract(map->pmap, addr)) 1730 kve->kve_resident++; 1731 addr += PAGE_SIZE; 1732 } 1733 1734 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1735 if (tobj != obj) 1736 VM_OBJECT_LOCK(tobj); 1737 if (lobj != obj) 1738 VM_OBJECT_UNLOCK(lobj); 1739 lobj = tobj; 1740 } 1741 1742 kve->kve_start = entry->start; 1743 kve->kve_end = entry->end; 1744 kve->kve_offset = entry->offset; 1745 1746 if (entry->protection & VM_PROT_READ) 1747 kve->kve_protection |= KVME_PROT_READ; 1748 if (entry->protection & VM_PROT_WRITE) 1749 kve->kve_protection |= KVME_PROT_WRITE; 1750 if (entry->protection & VM_PROT_EXECUTE) 1751 kve->kve_protection |= KVME_PROT_EXEC; 1752 1753 if (entry->eflags & MAP_ENTRY_COW) 1754 kve->kve_flags |= KVME_FLAG_COW; 1755 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1756 kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1757 1758 last_timestamp = map->timestamp; 1759 vm_map_unlock_read(map); 1760 1761 kve->kve_fileid = 0; 1762 kve->kve_fsid = 0; 1763 freepath = NULL; 1764 fullpath = ""; 1765 if (lobj) { 1766 vp = NULL; 1767 switch (lobj->type) { 1768 case OBJT_DEFAULT: 1769 kve->kve_type = KVME_TYPE_DEFAULT; 1770 break; 1771 case OBJT_VNODE: 1772 kve->kve_type = KVME_TYPE_VNODE; 1773 vp = lobj->handle; 1774 vref(vp); 1775 break; 1776 case OBJT_SWAP: 1777 kve->kve_type = KVME_TYPE_SWAP; 1778 break; 1779 case OBJT_DEVICE: 1780 kve->kve_type = KVME_TYPE_DEVICE; 1781 break; 1782 case OBJT_PHYS: 1783 kve->kve_type = KVME_TYPE_PHYS; 1784 break; 1785 case OBJT_DEAD: 1786 kve->kve_type = KVME_TYPE_DEAD; 1787 break; 1788 case OBJT_SG: 1789 kve->kve_type = KVME_TYPE_SG; 1790 break; 1791 default: 1792 kve->kve_type = KVME_TYPE_UNKNOWN; 1793 break; 1794 } 1795 if (lobj != obj) 1796 VM_OBJECT_UNLOCK(lobj); 1797 1798 kve->kve_ref_count = obj->ref_count; 1799 kve->kve_shadow_count = obj->shadow_count; 1800 VM_OBJECT_UNLOCK(obj); 1801 if (vp != NULL) { 1802 vn_fullpath(curthread, vp, &fullpath, 1803 &freepath); 1804 cred = curthread->td_ucred; 1805 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1806 vn_lock(vp, LK_SHARED | LK_RETRY); 1807 if (VOP_GETATTR(vp, &va, cred) == 0) { 1808 kve->kve_fileid = va.va_fileid; 1809 kve->kve_fsid = va.va_fsid; 1810 } 1811 vput(vp); 1812 VFS_UNLOCK_GIANT(vfslocked); 1813 } 1814 } else { 1815 kve->kve_type = KVME_TYPE_NONE; 1816 kve->kve_ref_count = 0; 1817 kve->kve_shadow_count = 0; 1818 } 1819 1820 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 1821 if (freepath != NULL) 1822 free(freepath, M_TEMP); 1823 1824 /* Pack record size down */ 1825 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 1826 strlen(kve->kve_path) + 1; 1827 kve->kve_structsize = roundup(kve->kve_structsize, 1828 sizeof(uint64_t)); 1829 error = SYSCTL_OUT(req, kve, kve->kve_structsize); 1830 vm_map_lock_read(map); 1831 if (error) 1832 break; 1833 if (last_timestamp != map->timestamp) { 1834 vm_map_lookup_entry(map, addr - 1, &tmp_entry); 1835 entry = tmp_entry; 1836 } 1837 } 1838 vm_map_unlock_read(map); 1839 vmspace_free(vm); 1840 PRELE(p); 1841 free(kve, M_TEMP); 1842 return (error); 1843 } 1844 1845 #if defined(STACK) || defined(DDB) 1846 static int 1847 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 1848 { 1849 struct kinfo_kstack *kkstp; 1850 int error, i, *name, numthreads; 1851 lwpid_t *lwpidarray; 1852 struct thread *td; 1853 struct stack *st; 1854 struct sbuf sb; 1855 struct proc *p; 1856 1857 name = (int *)arg1; 1858 if ((p = pfind((pid_t)name[0])) == NULL) 1859 return (ESRCH); 1860 /* XXXRW: Not clear ESRCH is the right error during proc execve(). */ 1861 if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) { 1862 PROC_UNLOCK(p); 1863 return (ESRCH); 1864 } 1865 if ((error = p_candebug(curthread, p))) { 1866 PROC_UNLOCK(p); 1867 return (error); 1868 } 1869 _PHOLD(p); 1870 PROC_UNLOCK(p); 1871 1872 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 1873 st = stack_create(); 1874 1875 lwpidarray = NULL; 1876 numthreads = 0; 1877 PROC_LOCK(p); 1878 repeat: 1879 if (numthreads < p->p_numthreads) { 1880 if (lwpidarray != NULL) { 1881 free(lwpidarray, M_TEMP); 1882 lwpidarray = NULL; 1883 } 1884 numthreads = p->p_numthreads; 1885 PROC_UNLOCK(p); 1886 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 1887 M_WAITOK | M_ZERO); 1888 PROC_LOCK(p); 1889 goto repeat; 1890 } 1891 i = 0; 1892 1893 /* 1894 * XXXRW: During the below loop, execve(2) and countless other sorts 1895 * of changes could have taken place. Should we check to see if the 1896 * vmspace has been replaced, or the like, in order to prevent 1897 * giving a snapshot that spans, say, execve(2), with some threads 1898 * before and some after? Among other things, the credentials could 1899 * have changed, in which case the right to extract debug info might 1900 * no longer be assured. 1901 */ 1902 FOREACH_THREAD_IN_PROC(p, td) { 1903 KASSERT(i < numthreads, 1904 ("sysctl_kern_proc_kstack: numthreads")); 1905 lwpidarray[i] = td->td_tid; 1906 i++; 1907 } 1908 numthreads = i; 1909 for (i = 0; i < numthreads; i++) { 1910 td = thread_find(p, lwpidarray[i]); 1911 if (td == NULL) { 1912 continue; 1913 } 1914 bzero(kkstp, sizeof(*kkstp)); 1915 (void)sbuf_new(&sb, kkstp->kkst_trace, 1916 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 1917 thread_lock(td); 1918 kkstp->kkst_tid = td->td_tid; 1919 if (TD_IS_SWAPPED(td)) 1920 kkstp->kkst_state = KKST_STATE_SWAPPED; 1921 else if (TD_IS_RUNNING(td)) 1922 kkstp->kkst_state = KKST_STATE_RUNNING; 1923 else { 1924 kkstp->kkst_state = KKST_STATE_STACKOK; 1925 stack_save_td(st, td); 1926 } 1927 thread_unlock(td); 1928 PROC_UNLOCK(p); 1929 stack_sbuf_print(&sb, st); 1930 sbuf_finish(&sb); 1931 sbuf_delete(&sb); 1932 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 1933 PROC_LOCK(p); 1934 if (error) 1935 break; 1936 } 1937 _PRELE(p); 1938 PROC_UNLOCK(p); 1939 if (lwpidarray != NULL) 1940 free(lwpidarray, M_TEMP); 1941 stack_destroy(st); 1942 free(kkstp, M_TEMP); 1943 return (error); 1944 } 1945 #endif 1946 1947 /* 1948 * This sysctl allows a process to retrieve the full list of groups from 1949 * itself or another process. 1950 */ 1951 static int 1952 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 1953 { 1954 pid_t *pidp = (pid_t *)arg1; 1955 unsigned int arglen = arg2; 1956 struct proc *p; 1957 struct ucred *cred; 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 p = pfind(*pidp); 1966 if (p == NULL) 1967 return (ESRCH); 1968 if ((error = p_cansee(curthread, p)) != 0) { 1969 PROC_UNLOCK(p); 1970 return (error); 1971 } 1972 } 1973 1974 cred = crhold(p->p_ucred); 1975 if (*pidp != -1) 1976 PROC_UNLOCK(p); 1977 1978 error = SYSCTL_OUT(req, cred->cr_groups, 1979 cred->cr_ngroups * sizeof(gid_t)); 1980 crfree(cred); 1981 return (error); 1982 } 1983 1984 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1985 1986 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 1987 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 1988 "Return entire process table"); 1989 1990 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 1991 sysctl_kern_proc, "Process table"); 1992 1993 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 1994 sysctl_kern_proc, "Process table"); 1995 1996 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 1997 sysctl_kern_proc, "Process table"); 1998 1999 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 2000 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2001 2002 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2003 sysctl_kern_proc, "Process table"); 2004 2005 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2006 sysctl_kern_proc, "Process table"); 2007 2008 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2009 sysctl_kern_proc, "Process table"); 2010 2011 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2012 sysctl_kern_proc, "Process table"); 2013 2014 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 2015 sysctl_kern_proc, "Return process table, no threads"); 2016 2017 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 2018 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2019 sysctl_kern_proc_args, "Process argument list"); 2020 2021 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 2022 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2023 2024 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 2025 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 2026 "Process syscall vector name (ABI type)"); 2027 2028 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 2029 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2030 2031 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 2032 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2033 2034 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 2035 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2036 2037 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 2038 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2039 2040 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 2041 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2042 2043 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 2044 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2045 2046 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 2047 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2048 2049 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 2050 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2051 2052 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 2053 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 2054 "Return process table, no threads"); 2055 2056 #ifdef COMPAT_FREEBSD7 2057 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 2058 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 2059 #endif 2060 2061 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 2062 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 2063 2064 #if defined(STACK) || defined(DDB) 2065 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 2066 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 2067 #endif 2068 2069 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2070 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 2071