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 * $FreeBSD$ 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_ktrace.h" 37 #include "opt_kstack_pages.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mutex.h> 45 #include <sys/proc.h> 46 #include <sys/sysent.h> 47 #include <sys/sched.h> 48 #include <sys/smp.h> 49 #include <sys/sysctl.h> 50 #include <sys/filedesc.h> 51 #include <sys/tty.h> 52 #include <sys/signalvar.h> 53 #include <sys/sx.h> 54 #include <sys/user.h> 55 #include <sys/jail.h> 56 #ifdef KTRACE 57 #include <sys/uio.h> 58 #include <sys/ktrace.h> 59 #endif 60 61 #include <vm/vm.h> 62 #include <vm/vm_extern.h> 63 #include <vm/pmap.h> 64 #include <vm/vm_map.h> 65 #include <vm/uma.h> 66 #include <machine/critical.h> 67 68 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 69 MALLOC_DEFINE(M_SESSION, "session", "session header"); 70 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 71 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 72 73 static void doenterpgrp(struct proc *, struct pgrp *); 74 static void orphanpg(struct pgrp *pg); 75 static void pgadjustjobc(struct pgrp *pgrp, int entering); 76 static void pgdelete(struct pgrp *); 77 static int proc_ctor(void *mem, int size, void *arg, int flags); 78 static void proc_dtor(void *mem, int size, void *arg); 79 static int proc_init(void *mem, int size, int flags); 80 static void proc_fini(void *mem, int size); 81 82 /* 83 * Other process lists 84 */ 85 struct pidhashhead *pidhashtbl; 86 u_long pidhash; 87 struct pgrphashhead *pgrphashtbl; 88 u_long pgrphash; 89 struct proclist allproc; 90 struct proclist zombproc; 91 struct sx allproc_lock; 92 struct sx proctree_lock; 93 struct mtx pargs_ref_lock; 94 struct mtx ppeers_lock; 95 uma_zone_t proc_zone; 96 uma_zone_t ithread_zone; 97 98 int kstack_pages = KSTACK_PAGES; 99 int uarea_pages = UAREA_PAGES; 100 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 101 SYSCTL_INT(_kern, OID_AUTO, uarea_pages, CTLFLAG_RD, &uarea_pages, 0, ""); 102 103 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 104 105 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 106 107 /* 108 * Initialize global process hashing structures. 109 */ 110 void 111 procinit() 112 { 113 114 sx_init(&allproc_lock, "allproc"); 115 sx_init(&proctree_lock, "proctree"); 116 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF); 117 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 118 LIST_INIT(&allproc); 119 LIST_INIT(&zombproc); 120 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 121 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 122 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 123 proc_ctor, proc_dtor, proc_init, proc_fini, 124 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 125 uihashinit(); 126 } 127 128 /* 129 * Prepare a proc for use. 130 */ 131 static int 132 proc_ctor(void *mem, int size, void *arg, int flags) 133 { 134 struct proc *p; 135 136 p = (struct proc *)mem; 137 return (0); 138 } 139 140 /* 141 * Reclaim a proc after use. 142 */ 143 static void 144 proc_dtor(void *mem, int size, void *arg) 145 { 146 struct proc *p; 147 struct thread *td; 148 struct ksegrp *kg; 149 struct kse *ke; 150 151 /* INVARIANTS checks go here */ 152 p = (struct proc *)mem; 153 KASSERT((p->p_numthreads == 1), 154 ("bad number of threads in exiting process")); 155 td = FIRST_THREAD_IN_PROC(p); 156 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 157 kg = FIRST_KSEGRP_IN_PROC(p); 158 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 159 ke = FIRST_KSE_IN_KSEGRP(kg); 160 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 161 162 /* Dispose of an alternate kstack, if it exists. 163 * XXX What if there are more than one thread in the proc? 164 * The first thread in the proc is special and not 165 * freed, so you gotta do this here. 166 */ 167 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0)) 168 vm_thread_dispose_altkstack(td); 169 170 /* 171 * We want to make sure we know the initial linkages. 172 * so for now tear them down and remake them. 173 * This is probably un-needed as we can probably rely 174 * on the state coming in here from wait4(). 175 */ 176 proc_linkup(p, kg, ke, td); 177 } 178 179 /* 180 * Initialize type-stable parts of a proc (when newly created). 181 */ 182 static int 183 proc_init(void *mem, int size, int flags) 184 { 185 struct proc *p; 186 struct thread *td; 187 struct ksegrp *kg; 188 struct kse *ke; 189 190 p = (struct proc *)mem; 191 p->p_sched = (struct p_sched *)&p[1]; 192 vm_proc_new(p); 193 td = thread_alloc(); 194 ke = kse_alloc(); 195 kg = ksegrp_alloc(); 196 proc_linkup(p, kg, ke, td); 197 bzero(&p->p_mtx, sizeof(struct mtx)); 198 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 199 return (0); 200 } 201 202 /* 203 * Tear down type-stable parts of a proc (just before being discarded) 204 */ 205 static void 206 proc_fini(void *mem, int size) 207 { 208 struct proc *p; 209 struct thread *td; 210 struct ksegrp *kg; 211 struct kse *ke; 212 213 p = (struct proc *)mem; 214 KASSERT((p->p_numthreads == 1), 215 ("bad number of threads in freeing process")); 216 td = FIRST_THREAD_IN_PROC(p); 217 KASSERT((td != NULL), ("proc_fini: bad thread pointer")); 218 kg = FIRST_KSEGRP_IN_PROC(p); 219 KASSERT((kg != NULL), ("proc_fini: bad kg pointer")); 220 ke = FIRST_KSE_IN_KSEGRP(kg); 221 KASSERT((ke != NULL), ("proc_fini: bad ke pointer")); 222 vm_proc_dispose(p); 223 thread_free(td); 224 ksegrp_free(kg); 225 kse_free(ke); 226 mtx_destroy(&p->p_mtx); 227 } 228 229 /* 230 * Is p an inferior of the current process? 231 */ 232 int 233 inferior(p) 234 register struct proc *p; 235 { 236 237 sx_assert(&proctree_lock, SX_LOCKED); 238 for (; p != curproc; p = p->p_pptr) 239 if (p->p_pid == 0) 240 return (0); 241 return (1); 242 } 243 244 /* 245 * Locate a process by number; return only "live" processes -- i.e., neither 246 * zombies nor newly born but incompletely initialized processes. By not 247 * returning processes in the PRS_NEW state, we allow callers to avoid 248 * testing for that condition to avoid dereferencing p_ucred, et al. 249 */ 250 struct proc * 251 pfind(pid) 252 register pid_t pid; 253 { 254 register struct proc *p; 255 256 sx_slock(&allproc_lock); 257 LIST_FOREACH(p, PIDHASH(pid), p_hash) 258 if (p->p_pid == pid) { 259 if (p->p_state == PRS_NEW) { 260 p = NULL; 261 break; 262 } 263 PROC_LOCK(p); 264 break; 265 } 266 sx_sunlock(&allproc_lock); 267 return (p); 268 } 269 270 /* 271 * Locate a process group by number. 272 * The caller must hold proctree_lock. 273 */ 274 struct pgrp * 275 pgfind(pgid) 276 register pid_t pgid; 277 { 278 register struct pgrp *pgrp; 279 280 sx_assert(&proctree_lock, SX_LOCKED); 281 282 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 283 if (pgrp->pg_id == pgid) { 284 PGRP_LOCK(pgrp); 285 return (pgrp); 286 } 287 } 288 return (NULL); 289 } 290 291 /* 292 * Create a new process group. 293 * pgid must be equal to the pid of p. 294 * Begin a new session if required. 295 */ 296 int 297 enterpgrp(p, pgid, pgrp, sess) 298 register struct proc *p; 299 pid_t pgid; 300 struct pgrp *pgrp; 301 struct session *sess; 302 { 303 struct pgrp *pgrp2; 304 305 sx_assert(&proctree_lock, SX_XLOCKED); 306 307 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 308 KASSERT(p->p_pid == pgid, 309 ("enterpgrp: new pgrp and pid != pgid")); 310 311 pgrp2 = pgfind(pgid); 312 313 KASSERT(pgrp2 == NULL, 314 ("enterpgrp: pgrp with pgid exists")); 315 KASSERT(!SESS_LEADER(p), 316 ("enterpgrp: session leader attempted setpgrp")); 317 318 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 319 320 if (sess != NULL) { 321 /* 322 * new session 323 */ 324 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 325 PROC_LOCK(p); 326 p->p_flag &= ~P_CONTROLT; 327 PROC_UNLOCK(p); 328 PGRP_LOCK(pgrp); 329 sess->s_leader = p; 330 sess->s_sid = p->p_pid; 331 sess->s_count = 1; 332 sess->s_ttyvp = NULL; 333 sess->s_ttyp = NULL; 334 bcopy(p->p_session->s_login, sess->s_login, 335 sizeof(sess->s_login)); 336 pgrp->pg_session = sess; 337 KASSERT(p == curproc, 338 ("enterpgrp: mksession and p != curproc")); 339 } else { 340 pgrp->pg_session = p->p_session; 341 SESS_LOCK(pgrp->pg_session); 342 pgrp->pg_session->s_count++; 343 SESS_UNLOCK(pgrp->pg_session); 344 PGRP_LOCK(pgrp); 345 } 346 pgrp->pg_id = pgid; 347 LIST_INIT(&pgrp->pg_members); 348 349 /* 350 * As we have an exclusive lock of proctree_lock, 351 * this should not deadlock. 352 */ 353 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 354 pgrp->pg_jobc = 0; 355 SLIST_INIT(&pgrp->pg_sigiolst); 356 PGRP_UNLOCK(pgrp); 357 358 doenterpgrp(p, pgrp); 359 360 return (0); 361 } 362 363 /* 364 * Move p to an existing process group 365 */ 366 int 367 enterthispgrp(p, pgrp) 368 register struct proc *p; 369 struct pgrp *pgrp; 370 { 371 372 sx_assert(&proctree_lock, SX_XLOCKED); 373 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 374 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 375 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 376 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 377 KASSERT(pgrp->pg_session == p->p_session, 378 ("%s: pgrp's session %p, p->p_session %p.\n", 379 __func__, 380 pgrp->pg_session, 381 p->p_session)); 382 KASSERT(pgrp != p->p_pgrp, 383 ("%s: p belongs to pgrp.", __func__)); 384 385 doenterpgrp(p, pgrp); 386 387 return (0); 388 } 389 390 /* 391 * Move p to a process group 392 */ 393 static void 394 doenterpgrp(p, pgrp) 395 struct proc *p; 396 struct pgrp *pgrp; 397 { 398 struct pgrp *savepgrp; 399 400 sx_assert(&proctree_lock, SX_XLOCKED); 401 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 402 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 403 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 404 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 405 406 savepgrp = p->p_pgrp; 407 408 /* 409 * Adjust eligibility of affected pgrps to participate in job control. 410 * Increment eligibility counts before decrementing, otherwise we 411 * could reach 0 spuriously during the first call. 412 */ 413 fixjobc(p, pgrp, 1); 414 fixjobc(p, p->p_pgrp, 0); 415 416 PGRP_LOCK(pgrp); 417 PGRP_LOCK(savepgrp); 418 PROC_LOCK(p); 419 LIST_REMOVE(p, p_pglist); 420 p->p_pgrp = pgrp; 421 PROC_UNLOCK(p); 422 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 423 PGRP_UNLOCK(savepgrp); 424 PGRP_UNLOCK(pgrp); 425 if (LIST_EMPTY(&savepgrp->pg_members)) 426 pgdelete(savepgrp); 427 } 428 429 /* 430 * remove process from process group 431 */ 432 int 433 leavepgrp(p) 434 register struct proc *p; 435 { 436 struct pgrp *savepgrp; 437 438 sx_assert(&proctree_lock, SX_XLOCKED); 439 savepgrp = p->p_pgrp; 440 PGRP_LOCK(savepgrp); 441 PROC_LOCK(p); 442 LIST_REMOVE(p, p_pglist); 443 p->p_pgrp = NULL; 444 PROC_UNLOCK(p); 445 PGRP_UNLOCK(savepgrp); 446 if (LIST_EMPTY(&savepgrp->pg_members)) 447 pgdelete(savepgrp); 448 return (0); 449 } 450 451 /* 452 * delete a process group 453 */ 454 static void 455 pgdelete(pgrp) 456 register struct pgrp *pgrp; 457 { 458 struct session *savesess; 459 int i; 460 461 sx_assert(&proctree_lock, SX_XLOCKED); 462 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 463 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 464 465 /* 466 * Reset any sigio structures pointing to us as a result of 467 * F_SETOWN with our pgid. 468 */ 469 funsetownlst(&pgrp->pg_sigiolst); 470 471 PGRP_LOCK(pgrp); 472 if (pgrp->pg_session->s_ttyp != NULL && 473 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 474 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 475 LIST_REMOVE(pgrp, pg_hash); 476 savesess = pgrp->pg_session; 477 SESS_LOCK(savesess); 478 i = --savesess->s_count; 479 SESS_UNLOCK(savesess); 480 PGRP_UNLOCK(pgrp); 481 if (i == 0) { 482 if (savesess->s_ttyp != NULL) 483 ttyrel(savesess->s_ttyp); 484 mtx_destroy(&savesess->s_mtx); 485 FREE(savesess, M_SESSION); 486 } 487 mtx_destroy(&pgrp->pg_mtx); 488 FREE(pgrp, M_PGRP); 489 } 490 491 static void 492 pgadjustjobc(pgrp, entering) 493 struct pgrp *pgrp; 494 int entering; 495 { 496 497 PGRP_LOCK(pgrp); 498 if (entering) 499 pgrp->pg_jobc++; 500 else { 501 --pgrp->pg_jobc; 502 if (pgrp->pg_jobc == 0) 503 orphanpg(pgrp); 504 } 505 PGRP_UNLOCK(pgrp); 506 } 507 508 /* 509 * Adjust pgrp jobc counters when specified process changes process group. 510 * We count the number of processes in each process group that "qualify" 511 * the group for terminal job control (those with a parent in a different 512 * process group of the same session). If that count reaches zero, the 513 * process group becomes orphaned. Check both the specified process' 514 * process group and that of its children. 515 * entering == 0 => p is leaving specified group. 516 * entering == 1 => p is entering specified group. 517 */ 518 void 519 fixjobc(p, pgrp, entering) 520 register struct proc *p; 521 register struct pgrp *pgrp; 522 int entering; 523 { 524 register struct pgrp *hispgrp; 525 register struct session *mysession; 526 527 sx_assert(&proctree_lock, SX_LOCKED); 528 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 529 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 530 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 531 532 /* 533 * Check p's parent to see whether p qualifies its own process 534 * group; if so, adjust count for p's process group. 535 */ 536 mysession = pgrp->pg_session; 537 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 538 hispgrp->pg_session == mysession) 539 pgadjustjobc(pgrp, entering); 540 541 /* 542 * Check this process' children to see whether they qualify 543 * their process groups; if so, adjust counts for children's 544 * process groups. 545 */ 546 LIST_FOREACH(p, &p->p_children, p_sibling) { 547 hispgrp = p->p_pgrp; 548 if (hispgrp == pgrp || 549 hispgrp->pg_session != mysession) 550 continue; 551 PROC_LOCK(p); 552 if (p->p_state == PRS_ZOMBIE) { 553 PROC_UNLOCK(p); 554 continue; 555 } 556 PROC_UNLOCK(p); 557 pgadjustjobc(hispgrp, entering); 558 } 559 } 560 561 /* 562 * A process group has become orphaned; 563 * if there are any stopped processes in the group, 564 * hang-up all process in that group. 565 */ 566 static void 567 orphanpg(pg) 568 struct pgrp *pg; 569 { 570 register struct proc *p; 571 572 PGRP_LOCK_ASSERT(pg, MA_OWNED); 573 574 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 575 PROC_LOCK(p); 576 if (P_SHOULDSTOP(p)) { 577 PROC_UNLOCK(p); 578 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 579 PROC_LOCK(p); 580 psignal(p, SIGHUP); 581 psignal(p, SIGCONT); 582 PROC_UNLOCK(p); 583 } 584 return; 585 } 586 PROC_UNLOCK(p); 587 } 588 } 589 590 #include "opt_ddb.h" 591 #ifdef DDB 592 #include <ddb/ddb.h> 593 594 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 595 { 596 register struct pgrp *pgrp; 597 register struct proc *p; 598 register int i; 599 600 for (i = 0; i <= pgrphash; i++) { 601 if (!LIST_EMPTY(&pgrphashtbl[i])) { 602 printf("\tindx %d\n", i); 603 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 604 printf( 605 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 606 (void *)pgrp, (long)pgrp->pg_id, 607 (void *)pgrp->pg_session, 608 pgrp->pg_session->s_count, 609 (void *)LIST_FIRST(&pgrp->pg_members)); 610 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 611 printf("\t\tpid %ld addr %p pgrp %p\n", 612 (long)p->p_pid, (void *)p, 613 (void *)p->p_pgrp); 614 } 615 } 616 } 617 } 618 } 619 #endif /* DDB */ 620 void 621 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp); 622 623 /* 624 * Fill in a kinfo_proc structure for the specified process. 625 * Must be called with the target process locked. 626 */ 627 void 628 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 629 { 630 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp); 631 } 632 633 void 634 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp) 635 { 636 struct proc *p; 637 struct thread *td0; 638 struct kse *ke; 639 struct ksegrp *kg; 640 struct tty *tp; 641 struct session *sp; 642 struct timeval tv; 643 struct sigacts *ps; 644 645 p = td->td_proc; 646 647 bzero(kp, sizeof(*kp)); 648 649 kp->ki_structsize = sizeof(*kp); 650 kp->ki_paddr = p; 651 PROC_LOCK_ASSERT(p, MA_OWNED); 652 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 653 kp->ki_args = p->p_args; 654 kp->ki_textvp = p->p_textvp; 655 #ifdef KTRACE 656 kp->ki_tracep = p->p_tracevp; 657 mtx_lock(&ktrace_mtx); 658 kp->ki_traceflag = p->p_traceflag; 659 mtx_unlock(&ktrace_mtx); 660 #endif 661 kp->ki_fd = p->p_fd; 662 kp->ki_vmspace = p->p_vmspace; 663 if (p->p_ucred) { 664 kp->ki_uid = p->p_ucred->cr_uid; 665 kp->ki_ruid = p->p_ucred->cr_ruid; 666 kp->ki_svuid = p->p_ucred->cr_svuid; 667 /* XXX bde doesn't like KI_NGROUPS */ 668 kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS); 669 bcopy(p->p_ucred->cr_groups, kp->ki_groups, 670 kp->ki_ngroups * sizeof(gid_t)); 671 kp->ki_rgid = p->p_ucred->cr_rgid; 672 kp->ki_svgid = p->p_ucred->cr_svgid; 673 } 674 if (p->p_sigacts) { 675 ps = p->p_sigacts; 676 mtx_lock(&ps->ps_mtx); 677 kp->ki_sigignore = ps->ps_sigignore; 678 kp->ki_sigcatch = ps->ps_sigcatch; 679 mtx_unlock(&ps->ps_mtx); 680 } 681 mtx_lock_spin(&sched_lock); 682 if (p->p_state != PRS_NEW && 683 p->p_state != PRS_ZOMBIE && 684 p->p_vmspace != NULL) { 685 struct vmspace *vm = p->p_vmspace; 686 687 kp->ki_size = vm->vm_map.size; 688 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 689 if (p->p_sflag & PS_INMEM) 690 kp->ki_rssize += UAREA_PAGES; 691 FOREACH_THREAD_IN_PROC(p, td0) { 692 if (!TD_IS_SWAPPED(td0)) 693 kp->ki_rssize += td0->td_kstack_pages; 694 if (td0->td_altkstack_obj != NULL) 695 kp->ki_rssize += td0->td_altkstack_pages; 696 } 697 kp->ki_swrss = vm->vm_swrss; 698 kp->ki_tsize = vm->vm_tsize; 699 kp->ki_dsize = vm->vm_dsize; 700 kp->ki_ssize = vm->vm_ssize; 701 } 702 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 703 kp->ki_start = p->p_stats->p_start; 704 timevaladd(&kp->ki_start, &boottime); 705 kp->ki_rusage = p->p_stats->p_ru; 706 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime, 707 NULL); 708 kp->ki_childstime = p->p_stats->p_cru.ru_stime; 709 kp->ki_childutime = p->p_stats->p_cru.ru_utime; 710 /* Some callers want child-times in a single value */ 711 kp->ki_childtime = kp->ki_childstime; 712 timevaladd(&kp->ki_childtime, &kp->ki_childutime); 713 } 714 kp->ki_sflag = p->p_sflag; 715 kp->ki_swtime = p->p_swtime; 716 kp->ki_pid = p->p_pid; 717 kp->ki_nice = p->p_nice; 718 bintime2timeval(&p->p_runtime, &tv); 719 kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec; 720 if (p->p_state != PRS_ZOMBIE) { 721 #if 0 722 if (td == NULL) { 723 /* XXXKSE: This should never happen. */ 724 printf("fill_kinfo_proc(): pid %d has no threads!\n", 725 p->p_pid); 726 mtx_unlock_spin(&sched_lock); 727 return; 728 } 729 #endif 730 if (td->td_wmesg != NULL) { 731 strlcpy(kp->ki_wmesg, td->td_wmesg, 732 sizeof(kp->ki_wmesg)); 733 } 734 if (TD_ON_LOCK(td)) { 735 kp->ki_kiflag |= KI_LOCKBLOCK; 736 strlcpy(kp->ki_lockname, td->td_lockname, 737 sizeof(kp->ki_lockname)); 738 } 739 740 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 741 if (TD_ON_RUNQ(td) || 742 TD_CAN_RUN(td) || 743 TD_IS_RUNNING(td)) { 744 kp->ki_stat = SRUN; 745 } else if (P_SHOULDSTOP(p)) { 746 kp->ki_stat = SSTOP; 747 } else if (TD_IS_SLEEPING(td)) { 748 kp->ki_stat = SSLEEP; 749 } else if (TD_ON_LOCK(td)) { 750 kp->ki_stat = SLOCK; 751 } else { 752 kp->ki_stat = SWAIT; 753 } 754 } else { 755 kp->ki_stat = SIDL; 756 } 757 758 kg = td->td_ksegrp; 759 ke = td->td_kse; 760 761 /* things in the KSE GROUP */ 762 kp->ki_estcpu = kg->kg_estcpu; 763 kp->ki_slptime = kg->kg_slptime; 764 kp->ki_pri.pri_user = kg->kg_user_pri; 765 kp->ki_pri.pri_class = kg->kg_pri_class; 766 767 /* Things in the thread */ 768 kp->ki_wchan = td->td_wchan; 769 kp->ki_pri.pri_level = td->td_priority; 770 kp->ki_pri.pri_native = td->td_base_pri; 771 kp->ki_lastcpu = td->td_lastcpu; 772 kp->ki_oncpu = td->td_oncpu; 773 kp->ki_tdflags = td->td_flags; 774 kp->ki_tid = td->td_tid; 775 kp->ki_numthreads = p->p_numthreads; 776 kp->ki_pcb = td->td_pcb; 777 kp->ki_kstack = (void *)td->td_kstack; 778 kp->ki_pctcpu = sched_pctcpu(td); 779 780 /* Things in the kse */ 781 if (ke) 782 kp->ki_rqindex = ke->ke_rqindex; 783 else 784 kp->ki_rqindex = 0; 785 786 } else { 787 kp->ki_stat = SZOMB; 788 } 789 mtx_unlock_spin(&sched_lock); 790 sp = NULL; 791 tp = NULL; 792 if (p->p_pgrp) { 793 kp->ki_pgid = p->p_pgrp->pg_id; 794 kp->ki_jobc = p->p_pgrp->pg_jobc; 795 sp = p->p_pgrp->pg_session; 796 797 if (sp != NULL) { 798 kp->ki_sid = sp->s_sid; 799 SESS_LOCK(sp); 800 strlcpy(kp->ki_login, sp->s_login, 801 sizeof(kp->ki_login)); 802 if (sp->s_ttyvp) 803 kp->ki_kiflag |= KI_CTTY; 804 if (SESS_LEADER(p)) 805 kp->ki_kiflag |= KI_SLEADER; 806 tp = sp->s_ttyp; 807 SESS_UNLOCK(sp); 808 } 809 } 810 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 811 kp->ki_tdev = dev2udev(tp->t_dev); 812 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 813 if (tp->t_session) 814 kp->ki_tsid = tp->t_session->s_sid; 815 } else 816 kp->ki_tdev = NODEV; 817 if (p->p_comm[0] != '\0') { 818 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 819 strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm)); 820 } 821 if (p->p_sysent && p->p_sysent->sv_name != NULL && 822 p->p_sysent->sv_name[0] != '\0') 823 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 824 kp->ki_siglist = p->p_siglist; 825 SIGSETOR(kp->ki_siglist, td->td_siglist); 826 kp->ki_sigmask = td->td_sigmask; 827 kp->ki_xstat = p->p_xstat; 828 kp->ki_acflag = p->p_acflag; 829 kp->ki_flag = p->p_flag; 830 /* If jailed(p->p_ucred), emulate the old P_JAILED flag. */ 831 if (jailed(p->p_ucred)) 832 kp->ki_flag |= P_JAILED; 833 kp->ki_lock = p->p_lock; 834 if (p->p_pptr) 835 kp->ki_ppid = p->p_pptr->p_pid; 836 } 837 838 /* 839 * Locate a zombie process by number 840 */ 841 struct proc * 842 zpfind(pid_t pid) 843 { 844 struct proc *p; 845 846 sx_slock(&allproc_lock); 847 LIST_FOREACH(p, &zombproc, p_list) 848 if (p->p_pid == pid) { 849 PROC_LOCK(p); 850 break; 851 } 852 sx_sunlock(&allproc_lock); 853 return (p); 854 } 855 856 #define KERN_PROC_ZOMBMASK 0x3 857 #define KERN_PROC_NOTHREADS 0x4 858 859 /* 860 * Must be called with the process locked and will return with it unlocked. 861 */ 862 static int 863 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 864 { 865 struct thread *td; 866 struct kinfo_proc kinfo_proc; 867 int error = 0; 868 struct proc *np; 869 pid_t pid = p->p_pid; 870 871 PROC_LOCK_ASSERT(p, MA_OWNED); 872 873 if (flags & KERN_PROC_NOTHREADS) { 874 fill_kinfo_proc(p, &kinfo_proc); 875 PROC_UNLOCK(p); 876 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 877 sizeof(kinfo_proc)); 878 PROC_LOCK(p); 879 } else { 880 _PHOLD(p); 881 FOREACH_THREAD_IN_PROC(p, td) { 882 fill_kinfo_thread(td, &kinfo_proc); 883 PROC_UNLOCK(p); 884 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 885 sizeof(kinfo_proc)); 886 PROC_LOCK(p); 887 if (error) 888 break; 889 } 890 _PRELE(p); 891 } 892 PROC_UNLOCK(p); 893 if (error) 894 return (error); 895 if (flags & KERN_PROC_ZOMBMASK) 896 np = zpfind(pid); 897 else { 898 if (pid == 0) 899 return (0); 900 np = pfind(pid); 901 } 902 if (np == NULL) 903 return EAGAIN; 904 if (np != p) { 905 PROC_UNLOCK(np); 906 return EAGAIN; 907 } 908 PROC_UNLOCK(np); 909 return (0); 910 } 911 912 static int 913 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 914 { 915 int *name = (int*) arg1; 916 u_int namelen = arg2; 917 struct proc *p; 918 int flags, doingzomb, oid_number; 919 int error = 0; 920 921 oid_number = oidp->oid_number; 922 if (oid_number != KERN_PROC_ALL && 923 (oid_number & KERN_PROC_INC_THREAD) == 0) 924 flags = KERN_PROC_NOTHREADS; 925 else { 926 flags = 0; 927 oid_number &= ~KERN_PROC_INC_THREAD; 928 } 929 if (oid_number == KERN_PROC_PID) { 930 if (namelen != 1) 931 return (EINVAL); 932 p = pfind((pid_t)name[0]); 933 if (!p) 934 return (ESRCH); 935 if ((error = p_cansee(curthread, p))) { 936 PROC_UNLOCK(p); 937 return (error); 938 } 939 error = sysctl_out_proc(p, req, flags); 940 return (error); 941 } 942 943 switch (oid_number) { 944 case KERN_PROC_ALL: 945 if (namelen != 0) 946 return (EINVAL); 947 break; 948 case KERN_PROC_PROC: 949 if (namelen != 0 && namelen != 1) 950 return (EINVAL); 951 break; 952 default: 953 if (namelen != 1) 954 return (EINVAL); 955 break; 956 } 957 958 if (!req->oldptr) { 959 /* overestimate by 5 procs */ 960 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 961 if (error) 962 return (error); 963 } 964 error = sysctl_wire_old_buffer(req, 0); 965 if (error != 0) 966 return (error); 967 sx_slock(&allproc_lock); 968 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 969 if (!doingzomb) 970 p = LIST_FIRST(&allproc); 971 else 972 p = LIST_FIRST(&zombproc); 973 for (; p != 0; p = LIST_NEXT(p, p_list)) { 974 /* 975 * Skip embryonic processes. 976 */ 977 mtx_lock_spin(&sched_lock); 978 if (p->p_state == PRS_NEW) { 979 mtx_unlock_spin(&sched_lock); 980 continue; 981 } 982 mtx_unlock_spin(&sched_lock); 983 PROC_LOCK(p); 984 /* 985 * Show a user only appropriate processes. 986 */ 987 if (p_cansee(curthread, p)) { 988 PROC_UNLOCK(p); 989 continue; 990 } 991 /* 992 * TODO - make more efficient (see notes below). 993 * do by session. 994 */ 995 switch (oid_number) { 996 997 case KERN_PROC_GID: 998 if (p->p_ucred == NULL || 999 p->p_ucred->cr_gid != (gid_t)name[0]) { 1000 PROC_UNLOCK(p); 1001 continue; 1002 } 1003 break; 1004 1005 case KERN_PROC_PGRP: 1006 /* could do this by traversing pgrp */ 1007 if (p->p_pgrp == NULL || 1008 p->p_pgrp->pg_id != (pid_t)name[0]) { 1009 PROC_UNLOCK(p); 1010 continue; 1011 } 1012 break; 1013 1014 case KERN_PROC_RGID: 1015 if (p->p_ucred == NULL || 1016 p->p_ucred->cr_rgid != (gid_t)name[0]) { 1017 PROC_UNLOCK(p); 1018 continue; 1019 } 1020 break; 1021 1022 case KERN_PROC_SESSION: 1023 if (p->p_session == NULL || 1024 p->p_session->s_sid != (pid_t)name[0]) { 1025 PROC_UNLOCK(p); 1026 continue; 1027 } 1028 break; 1029 1030 case KERN_PROC_TTY: 1031 if ((p->p_flag & P_CONTROLT) == 0 || 1032 p->p_session == NULL) { 1033 PROC_UNLOCK(p); 1034 continue; 1035 } 1036 SESS_LOCK(p->p_session); 1037 if (p->p_session->s_ttyp == NULL || 1038 dev2udev(p->p_session->s_ttyp->t_dev) != 1039 (dev_t)name[0]) { 1040 SESS_UNLOCK(p->p_session); 1041 PROC_UNLOCK(p); 1042 continue; 1043 } 1044 SESS_UNLOCK(p->p_session); 1045 break; 1046 1047 case KERN_PROC_UID: 1048 if (p->p_ucred == NULL || 1049 p->p_ucred->cr_uid != (uid_t)name[0]) { 1050 PROC_UNLOCK(p); 1051 continue; 1052 } 1053 break; 1054 1055 case KERN_PROC_RUID: 1056 if (p->p_ucred == NULL || 1057 p->p_ucred->cr_ruid != (uid_t)name[0]) { 1058 PROC_UNLOCK(p); 1059 continue; 1060 } 1061 break; 1062 1063 case KERN_PROC_PROC: 1064 break; 1065 1066 default: 1067 break; 1068 1069 } 1070 1071 error = sysctl_out_proc(p, req, flags | doingzomb); 1072 if (error) { 1073 sx_sunlock(&allproc_lock); 1074 return (error); 1075 } 1076 } 1077 } 1078 sx_sunlock(&allproc_lock); 1079 return (0); 1080 } 1081 1082 struct pargs * 1083 pargs_alloc(int len) 1084 { 1085 struct pargs *pa; 1086 1087 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1088 M_WAITOK); 1089 pa->ar_ref = 1; 1090 pa->ar_length = len; 1091 return (pa); 1092 } 1093 1094 void 1095 pargs_free(struct pargs *pa) 1096 { 1097 1098 FREE(pa, M_PARGS); 1099 } 1100 1101 void 1102 pargs_hold(struct pargs *pa) 1103 { 1104 1105 if (pa == NULL) 1106 return; 1107 PARGS_LOCK(pa); 1108 pa->ar_ref++; 1109 PARGS_UNLOCK(pa); 1110 } 1111 1112 void 1113 pargs_drop(struct pargs *pa) 1114 { 1115 1116 if (pa == NULL) 1117 return; 1118 PARGS_LOCK(pa); 1119 if (--pa->ar_ref == 0) { 1120 PARGS_UNLOCK(pa); 1121 pargs_free(pa); 1122 } else 1123 PARGS_UNLOCK(pa); 1124 } 1125 1126 /* 1127 * This sysctl allows a process to retrieve the argument list or process 1128 * title for another process without groping around in the address space 1129 * of the other process. It also allow a process to set its own "process 1130 * title to a string of its own choice. 1131 */ 1132 static int 1133 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1134 { 1135 int *name = (int*) arg1; 1136 u_int namelen = arg2; 1137 struct pargs *newpa, *pa; 1138 struct proc *p; 1139 int error = 0; 1140 1141 if (namelen != 1) 1142 return (EINVAL); 1143 1144 p = pfind((pid_t)name[0]); 1145 if (!p) 1146 return (ESRCH); 1147 1148 if ((error = p_cansee(curthread, p)) != 0) { 1149 PROC_UNLOCK(p); 1150 return (error); 1151 } 1152 1153 if (req->newptr && curproc != p) { 1154 PROC_UNLOCK(p); 1155 return (EPERM); 1156 } 1157 1158 pa = p->p_args; 1159 pargs_hold(pa); 1160 PROC_UNLOCK(p); 1161 if (req->oldptr != NULL && pa != NULL) 1162 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1163 pargs_drop(pa); 1164 if (error != 0 || req->newptr == NULL) 1165 return (error); 1166 1167 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1168 return (ENOMEM); 1169 newpa = pargs_alloc(req->newlen); 1170 error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 1171 if (error != 0) { 1172 pargs_free(newpa); 1173 return (error); 1174 } 1175 PROC_LOCK(p); 1176 pa = p->p_args; 1177 p->p_args = newpa; 1178 PROC_UNLOCK(p); 1179 pargs_drop(pa); 1180 return (0); 1181 } 1182 1183 static int 1184 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1185 { 1186 struct proc *p; 1187 char *sv_name; 1188 int *name; 1189 int namelen; 1190 int error; 1191 1192 namelen = arg2; 1193 if (namelen != 1) 1194 return (EINVAL); 1195 1196 name = (int *)arg1; 1197 if ((p = pfind((pid_t)name[0])) == NULL) 1198 return (ESRCH); 1199 if ((error = p_cansee(curthread, p))) { 1200 PROC_UNLOCK(p); 1201 return (error); 1202 } 1203 sv_name = p->p_sysent->sv_name; 1204 PROC_UNLOCK(p); 1205 return (sysctl_handle_string(oidp, sv_name, 0, req)); 1206 } 1207 1208 1209 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1210 1211 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1212 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1213 1214 SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD, 1215 sysctl_kern_proc, "Process table"); 1216 1217 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1218 sysctl_kern_proc, "Process table"); 1219 1220 SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD, 1221 sysctl_kern_proc, "Process table"); 1222 1223 SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD, 1224 sysctl_kern_proc, "Process table"); 1225 1226 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1227 sysctl_kern_proc, "Process table"); 1228 1229 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1230 sysctl_kern_proc, "Process table"); 1231 1232 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1233 sysctl_kern_proc, "Process table"); 1234 1235 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1236 sysctl_kern_proc, "Process table"); 1237 1238 SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD, 1239 sysctl_kern_proc, "Return process table, no threads"); 1240 1241 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1242 sysctl_kern_proc_args, "Process argument list"); 1243 1244 SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD, 1245 sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); 1246 1247 SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 1248 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1249 1250 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 1251 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1252 1253 SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 1254 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1255 1256 SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), sid_td, 1257 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1258 1259 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 1260 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1261 1262 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 1263 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1264 1265 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 1266 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1267 1268 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 1269 CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1270 1271 SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 1272 CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads"); 1273