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