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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_ktrace.h" 38 #include "opt_kstack_pages.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/mutex.h> 46 #include <sys/proc.h> 47 #include <sys/kse.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 struct proc *dopfind(register pid_t); 74 75 static void doenterpgrp(struct proc *, struct pgrp *); 76 77 static void pgdelete(struct pgrp *); 78 79 static void orphanpg(struct pgrp *pg); 80 81 static void proc_ctor(void *mem, int size, void *arg); 82 static void proc_dtor(void *mem, int size, void *arg); 83 static void proc_init(void *mem, int size); 84 static void proc_fini(void *mem, int size); 85 86 /* 87 * Other process lists 88 */ 89 struct pidhashhead *pidhashtbl; 90 u_long pidhash; 91 struct pgrphashhead *pgrphashtbl; 92 u_long pgrphash; 93 struct proclist allproc; 94 struct proclist zombproc; 95 struct sx allproc_lock; 96 struct sx proctree_lock; 97 struct mtx pargs_ref_lock; 98 struct mtx ppeers_lock; 99 uma_zone_t proc_zone; 100 uma_zone_t ithread_zone; 101 102 int kstack_pages = KSTACK_PAGES; 103 int uarea_pages = UAREA_PAGES; 104 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 105 SYSCTL_INT(_kern, OID_AUTO, uarea_pages, CTLFLAG_RD, &uarea_pages, 0, ""); 106 107 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 108 109 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 110 111 /* 112 * Initialize global process hashing structures. 113 */ 114 void 115 procinit() 116 { 117 118 sx_init(&allproc_lock, "allproc"); 119 sx_init(&proctree_lock, "proctree"); 120 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF); 121 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 122 LIST_INIT(&allproc); 123 LIST_INIT(&zombproc); 124 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 125 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 126 proc_zone = uma_zcreate("PROC", sizeof (struct proc), 127 proc_ctor, proc_dtor, proc_init, proc_fini, 128 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 129 uihashinit(); 130 } 131 132 /* 133 * Prepare a proc for use. 134 */ 135 static void 136 proc_ctor(void *mem, int size, void *arg) 137 { 138 struct proc *p; 139 140 KASSERT((size == sizeof(struct proc)), 141 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 142 p = (struct proc *)mem; 143 } 144 145 /* 146 * Reclaim a proc after use. 147 */ 148 static void 149 proc_dtor(void *mem, int size, void *arg) 150 { 151 struct proc *p; 152 struct thread *td; 153 struct ksegrp *kg; 154 struct kse *ke; 155 156 /* INVARIANTS checks go here */ 157 KASSERT((size == sizeof(struct proc)), 158 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 159 p = (struct proc *)mem; 160 KASSERT((p->p_numthreads == 1), 161 ("bad number of threads in exiting process")); 162 td = FIRST_THREAD_IN_PROC(p); 163 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 164 kg = FIRST_KSEGRP_IN_PROC(p); 165 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 166 ke = FIRST_KSE_IN_KSEGRP(kg); 167 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 168 169 /* Dispose of an alternate kstack, if it exists. 170 * XXX What if there are more than one thread in the proc? 171 * The first thread in the proc is special and not 172 * freed, so you gotta do this here. 173 */ 174 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0)) 175 pmap_dispose_altkstack(td); 176 177 /* 178 * We want to make sure we know the initial linkages. 179 * so for now tear them down and remake them. 180 * This is probably un-needed as we can probably rely 181 * on the state coming in here from wait4(). 182 */ 183 proc_linkup(p, kg, ke, td); 184 } 185 186 /* 187 * Initialize type-stable parts of a proc (when newly created). 188 */ 189 static void 190 proc_init(void *mem, int size) 191 { 192 struct proc *p; 193 struct thread *td; 194 struct ksegrp *kg; 195 struct kse *ke; 196 197 KASSERT((size == sizeof(struct proc)), 198 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 199 p = (struct proc *)mem; 200 vm_proc_new(p); 201 td = thread_alloc(); 202 ke = kse_alloc(); 203 kg = ksegrp_alloc(); 204 proc_linkup(p, kg, ke, td); 205 } 206 207 /* 208 * Tear down type-stable parts of a proc (just before being discarded) 209 */ 210 static void 211 proc_fini(void *mem, int size) 212 { 213 struct proc *p; 214 struct thread *td; 215 struct ksegrp *kg; 216 struct kse *ke; 217 218 KASSERT((size == sizeof(struct proc)), 219 ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc))); 220 p = (struct proc *)mem; 221 KASSERT((p->p_numthreads == 1), 222 ("bad number of threads in freeing process")); 223 td = FIRST_THREAD_IN_PROC(p); 224 KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 225 kg = FIRST_KSEGRP_IN_PROC(p); 226 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer")); 227 ke = FIRST_KSE_IN_KSEGRP(kg); 228 KASSERT((ke != NULL), ("proc_dtor: bad ke pointer")); 229 vm_proc_dispose(p); 230 thread_free(td); 231 ksegrp_free(kg); 232 kse_free(ke); 233 } 234 235 /* 236 * Is p an inferior of the current process? 237 */ 238 int 239 inferior(p) 240 register struct proc *p; 241 { 242 243 sx_assert(&proctree_lock, SX_LOCKED); 244 for (; p != curproc; p = p->p_pptr) 245 if (p->p_pid == 0) 246 return (0); 247 return (1); 248 } 249 250 /* 251 * Locate a process by number 252 */ 253 struct proc * 254 pfind(pid) 255 register pid_t pid; 256 { 257 register struct proc *p; 258 259 sx_slock(&allproc_lock); 260 p = dopfind(pid); 261 sx_sunlock(&allproc_lock); 262 return (p); 263 } 264 265 static struct proc * 266 dopfind(pid) 267 register pid_t pid; 268 { 269 register struct proc *p; 270 271 sx_assert(&allproc_lock, SX_LOCKED); 272 273 LIST_FOREACH(p, PIDHASH(pid), p_hash) 274 if (p->p_pid == pid) { 275 PROC_LOCK(p); 276 break; 277 } 278 return (p); 279 } 280 281 /* 282 * Locate a process group by number. 283 * The caller must hold proctree_lock. 284 */ 285 struct pgrp * 286 pgfind(pgid) 287 register pid_t pgid; 288 { 289 register struct pgrp *pgrp; 290 291 sx_assert(&proctree_lock, SX_LOCKED); 292 293 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 294 if (pgrp->pg_id == pgid) { 295 PGRP_LOCK(pgrp); 296 return (pgrp); 297 } 298 } 299 return (NULL); 300 } 301 302 /* 303 * Create a new process group. 304 * pgid must be equal to the pid of p. 305 * Begin a new session if required. 306 */ 307 int 308 enterpgrp(p, pgid, pgrp, sess) 309 register struct proc *p; 310 pid_t pgid; 311 struct pgrp *pgrp; 312 struct session *sess; 313 { 314 struct pgrp *pgrp2; 315 316 sx_assert(&proctree_lock, SX_XLOCKED); 317 318 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 319 KASSERT(p->p_pid == pgid, 320 ("enterpgrp: new pgrp and pid != pgid")); 321 322 pgrp2 = pgfind(pgid); 323 324 KASSERT(pgrp2 == NULL, 325 ("enterpgrp: pgrp with pgid exists")); 326 KASSERT(!SESS_LEADER(p), 327 ("enterpgrp: session leader attempted setpgrp")); 328 329 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 330 331 if (sess != NULL) { 332 /* 333 * new session 334 */ 335 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 336 PROC_LOCK(p); 337 p->p_flag &= ~P_CONTROLT; 338 PROC_UNLOCK(p); 339 PGRP_LOCK(pgrp); 340 sess->s_leader = p; 341 sess->s_sid = p->p_pid; 342 sess->s_count = 1; 343 sess->s_ttyvp = NULL; 344 sess->s_ttyp = NULL; 345 bcopy(p->p_session->s_login, sess->s_login, 346 sizeof(sess->s_login)); 347 pgrp->pg_session = sess; 348 KASSERT(p == curproc, 349 ("enterpgrp: mksession and p != curproc")); 350 } else { 351 pgrp->pg_session = p->p_session; 352 SESS_LOCK(pgrp->pg_session); 353 pgrp->pg_session->s_count++; 354 SESS_UNLOCK(pgrp->pg_session); 355 PGRP_LOCK(pgrp); 356 } 357 pgrp->pg_id = pgid; 358 LIST_INIT(&pgrp->pg_members); 359 360 /* 361 * As we have an exclusive lock of proctree_lock, 362 * this should not deadlock. 363 */ 364 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 365 pgrp->pg_jobc = 0; 366 SLIST_INIT(&pgrp->pg_sigiolst); 367 PGRP_UNLOCK(pgrp); 368 369 doenterpgrp(p, pgrp); 370 371 return (0); 372 } 373 374 /* 375 * Move p to an existing process group 376 */ 377 int 378 enterthispgrp(p, pgrp) 379 register struct proc *p; 380 struct pgrp *pgrp; 381 { 382 383 sx_assert(&proctree_lock, SX_XLOCKED); 384 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 385 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 386 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 387 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 388 KASSERT(pgrp->pg_session == p->p_session, 389 ("%s: pgrp's session %p, p->p_session %p.\n", 390 __func__, 391 pgrp->pg_session, 392 p->p_session)); 393 KASSERT(pgrp != p->p_pgrp, 394 ("%s: p belongs to pgrp.", __func__)); 395 396 doenterpgrp(p, pgrp); 397 398 return (0); 399 } 400 401 /* 402 * Move p to a process group 403 */ 404 static void 405 doenterpgrp(p, pgrp) 406 struct proc *p; 407 struct pgrp *pgrp; 408 { 409 struct pgrp *savepgrp; 410 411 sx_assert(&proctree_lock, SX_XLOCKED); 412 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 413 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 414 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 415 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 416 417 savepgrp = p->p_pgrp; 418 419 /* 420 * Adjust eligibility of affected pgrps to participate in job control. 421 * Increment eligibility counts before decrementing, otherwise we 422 * could reach 0 spuriously during the first call. 423 */ 424 fixjobc(p, pgrp, 1); 425 fixjobc(p, p->p_pgrp, 0); 426 427 PGRP_LOCK(pgrp); 428 PGRP_LOCK(savepgrp); 429 PROC_LOCK(p); 430 LIST_REMOVE(p, p_pglist); 431 p->p_pgrp = pgrp; 432 PROC_UNLOCK(p); 433 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 434 PGRP_UNLOCK(savepgrp); 435 PGRP_UNLOCK(pgrp); 436 if (LIST_EMPTY(&savepgrp->pg_members)) 437 pgdelete(savepgrp); 438 } 439 440 /* 441 * remove process from process group 442 */ 443 int 444 leavepgrp(p) 445 register struct proc *p; 446 { 447 struct pgrp *savepgrp; 448 449 sx_assert(&proctree_lock, SX_XLOCKED); 450 savepgrp = p->p_pgrp; 451 PGRP_LOCK(savepgrp); 452 PROC_LOCK(p); 453 LIST_REMOVE(p, p_pglist); 454 p->p_pgrp = NULL; 455 PROC_UNLOCK(p); 456 PGRP_UNLOCK(savepgrp); 457 if (LIST_EMPTY(&savepgrp->pg_members)) 458 pgdelete(savepgrp); 459 return (0); 460 } 461 462 /* 463 * delete a process group 464 */ 465 static void 466 pgdelete(pgrp) 467 register struct pgrp *pgrp; 468 { 469 struct session *savesess; 470 471 sx_assert(&proctree_lock, SX_XLOCKED); 472 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 473 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 474 475 /* 476 * Reset any sigio structures pointing to us as a result of 477 * F_SETOWN with our pgid. 478 */ 479 funsetownlst(&pgrp->pg_sigiolst); 480 481 PGRP_LOCK(pgrp); 482 if (pgrp->pg_session->s_ttyp != NULL && 483 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 484 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 485 LIST_REMOVE(pgrp, pg_hash); 486 savesess = pgrp->pg_session; 487 SESS_LOCK(savesess); 488 savesess->s_count--; 489 SESS_UNLOCK(savesess); 490 PGRP_UNLOCK(pgrp); 491 if (savesess->s_count == 0) { 492 mtx_destroy(&savesess->s_mtx); 493 FREE(pgrp->pg_session, M_SESSION); 494 } 495 mtx_destroy(&pgrp->pg_mtx); 496 FREE(pgrp, M_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 PGRP_LOCK(pgrp); 531 if (entering) 532 pgrp->pg_jobc++; 533 else { 534 --pgrp->pg_jobc; 535 if (pgrp->pg_jobc == 0) 536 orphanpg(pgrp); 537 } 538 PGRP_UNLOCK(pgrp); 539 } 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 if ((hispgrp = p->p_pgrp) != pgrp && 548 hispgrp->pg_session == mysession && 549 p->p_state != PRS_ZOMBIE) { 550 PGRP_LOCK(hispgrp); 551 if (entering) 552 hispgrp->pg_jobc++; 553 else { 554 --hispgrp->pg_jobc; 555 if (hispgrp->pg_jobc == 0) 556 orphanpg(hispgrp); 557 } 558 PGRP_UNLOCK(hispgrp); 559 } 560 } 561 } 562 563 /* 564 * A process group has become orphaned; 565 * if there are any stopped processes in the group, 566 * hang-up all process in that group. 567 */ 568 static void 569 orphanpg(pg) 570 struct pgrp *pg; 571 { 572 register struct proc *p; 573 574 PGRP_LOCK_ASSERT(pg, MA_OWNED); 575 576 mtx_lock_spin(&sched_lock); 577 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 578 if (P_SHOULDSTOP(p)) { 579 mtx_unlock_spin(&sched_lock); 580 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 581 PROC_LOCK(p); 582 psignal(p, SIGHUP); 583 psignal(p, SIGCONT); 584 PROC_UNLOCK(p); 585 } 586 return; 587 } 588 } 589 mtx_unlock_spin(&sched_lock); 590 } 591 592 #include "opt_ddb.h" 593 #ifdef DDB 594 #include <ddb/ddb.h> 595 596 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 597 { 598 register struct pgrp *pgrp; 599 register struct proc *p; 600 register int i; 601 602 for (i = 0; i <= pgrphash; i++) { 603 if (!LIST_EMPTY(&pgrphashtbl[i])) { 604 printf("\tindx %d\n", i); 605 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 606 printf( 607 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 608 (void *)pgrp, (long)pgrp->pg_id, 609 (void *)pgrp->pg_session, 610 pgrp->pg_session->s_count, 611 (void *)LIST_FIRST(&pgrp->pg_members)); 612 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 613 printf("\t\tpid %ld addr %p pgrp %p\n", 614 (long)p->p_pid, (void *)p, 615 (void *)p->p_pgrp); 616 } 617 } 618 } 619 } 620 } 621 #endif /* DDB */ 622 623 /* 624 * Fill in an kinfo_proc structure for the specified process. 625 * Must be called with the target process locked. 626 */ 627 void 628 fill_kinfo_proc(p, kp) 629 struct proc *p; 630 struct kinfo_proc *kp; 631 { 632 struct thread *td; 633 struct kse *ke; 634 struct ksegrp *kg; 635 struct tty *tp; 636 struct session *sp; 637 struct timeval tv; 638 639 bzero(kp, sizeof(*kp)); 640 641 kp->ki_structsize = sizeof(*kp); 642 kp->ki_paddr = p; 643 PROC_LOCK_ASSERT(p, MA_OWNED); 644 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 645 kp->ki_args = p->p_args; 646 kp->ki_textvp = p->p_textvp; 647 #ifdef KTRACE 648 kp->ki_tracep = p->p_tracep; 649 mtx_lock(&ktrace_mtx); 650 kp->ki_traceflag = p->p_traceflag; 651 mtx_unlock(&ktrace_mtx); 652 #endif 653 kp->ki_fd = p->p_fd; 654 kp->ki_vmspace = p->p_vmspace; 655 if (p->p_ucred) { 656 kp->ki_uid = p->p_ucred->cr_uid; 657 kp->ki_ruid = p->p_ucred->cr_ruid; 658 kp->ki_svuid = p->p_ucred->cr_svuid; 659 /* XXX bde doesn't like KI_NGROUPS */ 660 kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS); 661 bcopy(p->p_ucred->cr_groups, kp->ki_groups, 662 kp->ki_ngroups * sizeof(gid_t)); 663 kp->ki_rgid = p->p_ucred->cr_rgid; 664 kp->ki_svgid = p->p_ucred->cr_svgid; 665 } 666 if (p->p_procsig) { 667 kp->ki_sigignore = p->p_procsig->ps_sigignore; 668 kp->ki_sigcatch = p->p_procsig->ps_sigcatch; 669 } 670 mtx_lock_spin(&sched_lock); 671 if (p->p_state != PRS_NEW && 672 p->p_state != PRS_ZOMBIE && 673 p->p_vmspace != NULL) { 674 struct vmspace *vm = p->p_vmspace; 675 676 kp->ki_size = vm->vm_map.size; 677 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 678 if (p->p_sflag & PS_INMEM) 679 kp->ki_rssize += UAREA_PAGES; 680 FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */ 681 kp->ki_rssize += KSTACK_PAGES; 682 kp->ki_swrss = vm->vm_swrss; 683 kp->ki_tsize = vm->vm_tsize; 684 kp->ki_dsize = vm->vm_dsize; 685 kp->ki_ssize = vm->vm_ssize; 686 } 687 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 688 kp->ki_start = p->p_stats->p_start; 689 kp->ki_rusage = p->p_stats->p_ru; 690 kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec + 691 p->p_stats->p_cru.ru_stime.tv_sec; 692 kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec + 693 p->p_stats->p_cru.ru_stime.tv_usec; 694 } 695 if (p->p_state != PRS_ZOMBIE) { 696 td = FIRST_THREAD_IN_PROC(p); 697 if (td == NULL) { 698 /* XXXKSE: This should never happen. */ 699 printf("fill_kinfo_proc(): pid %d has no threads!\n", 700 p->p_pid); 701 mtx_unlock_spin(&sched_lock); 702 return; 703 } 704 if (!(p->p_flag & P_KSES)) { 705 if (td->td_wmesg != NULL) { 706 strlcpy(kp->ki_wmesg, td->td_wmesg, 707 sizeof(kp->ki_wmesg)); 708 } 709 if (TD_ON_LOCK(td)) { 710 kp->ki_kiflag |= KI_LOCKBLOCK; 711 strlcpy(kp->ki_lockname, td->td_lockname, 712 sizeof(kp->ki_lockname)); 713 } 714 } 715 716 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 717 if (TD_ON_RUNQ(td) || 718 TD_CAN_RUN(td) || 719 TD_IS_RUNNING(td)) { 720 kp->ki_stat = SRUN; 721 } else if (P_SHOULDSTOP(p)) { 722 kp->ki_stat = SSTOP; 723 } else if (TD_IS_SLEEPING(td)) { 724 kp->ki_stat = SSLEEP; 725 } else if (TD_ON_LOCK(td)) { 726 kp->ki_stat = SLOCK; 727 } else { 728 kp->ki_stat = SWAIT; 729 } 730 } else { 731 kp->ki_stat = SIDL; 732 } 733 734 kp->ki_sflag = p->p_sflag; 735 kp->ki_swtime = p->p_swtime; 736 kp->ki_pid = p->p_pid; 737 /* vvv XXXKSE */ 738 if (!(p->p_flag & P_KSES)) { 739 kg = td->td_ksegrp; 740 ke = td->td_kse; 741 KASSERT((ke != NULL), ("fill_kinfo_proc: Null KSE")); 742 bintime2timeval(&p->p_runtime, &tv); 743 kp->ki_runtime = 744 tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec; 745 746 /* things in the KSE GROUP */ 747 kp->ki_estcpu = kg->kg_estcpu; 748 kp->ki_slptime = kg->kg_slptime; 749 kp->ki_pri.pri_user = kg->kg_user_pri; 750 kp->ki_pri.pri_class = kg->kg_pri_class; 751 kp->ki_nice = kg->kg_nice; 752 753 /* Things in the thread */ 754 kp->ki_wchan = td->td_wchan; 755 kp->ki_pri.pri_level = td->td_priority; 756 kp->ki_pri.pri_native = td->td_base_pri; 757 kp->ki_lastcpu = td->td_lastcpu; 758 kp->ki_tdflags = td->td_flags; 759 kp->ki_pcb = td->td_pcb; 760 kp->ki_kstack = (void *)td->td_kstack; 761 762 /* Things in the kse */ 763 kp->ki_rqindex = ke->ke_rqindex; 764 kp->ki_oncpu = ke->ke_oncpu; 765 kp->ki_pctcpu = ke->ke_pctcpu; 766 } else { 767 kp->ki_oncpu = -1; 768 kp->ki_lastcpu = -1; 769 kp->ki_tdflags = -1; 770 /* All the rest are 0 for now */ 771 } 772 /* ^^^ XXXKSE */ 773 } else { 774 kp->ki_stat = SZOMB; 775 } 776 mtx_unlock_spin(&sched_lock); 777 sp = NULL; 778 tp = NULL; 779 if (p->p_pgrp) { 780 kp->ki_pgid = p->p_pgrp->pg_id; 781 kp->ki_jobc = p->p_pgrp->pg_jobc; 782 sp = p->p_pgrp->pg_session; 783 784 if (sp != NULL) { 785 kp->ki_sid = sp->s_sid; 786 SESS_LOCK(sp); 787 strlcpy(kp->ki_login, sp->s_login, 788 sizeof(kp->ki_login)); 789 if (sp->s_ttyvp) 790 kp->ki_kiflag |= KI_CTTY; 791 if (SESS_LEADER(p)) 792 kp->ki_kiflag |= KI_SLEADER; 793 tp = sp->s_ttyp; 794 SESS_UNLOCK(sp); 795 } 796 } 797 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 798 kp->ki_tdev = dev2udev(tp->t_dev); 799 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 800 if (tp->t_session) 801 kp->ki_tsid = tp->t_session->s_sid; 802 } else 803 kp->ki_tdev = NOUDEV; 804 if (p->p_comm[0] != '\0') { 805 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 806 strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm)); 807 } 808 kp->ki_siglist = p->p_siglist; 809 kp->ki_sigmask = p->p_sigmask; 810 kp->ki_xstat = p->p_xstat; 811 kp->ki_acflag = p->p_acflag; 812 kp->ki_flag = p->p_flag; 813 /* If jailed(p->p_ucred), emulate the old P_JAILED flag. */ 814 if (jailed(p->p_ucred)) 815 kp->ki_flag |= P_JAILED; 816 kp->ki_lock = p->p_lock; 817 if (p->p_pptr) 818 kp->ki_ppid = p->p_pptr->p_pid; 819 } 820 821 /* 822 * Locate a zombie process by number 823 */ 824 struct proc * 825 zpfind(pid_t pid) 826 { 827 struct proc *p; 828 829 sx_slock(&allproc_lock); 830 LIST_FOREACH(p, &zombproc, p_list) 831 if (p->p_pid == pid) { 832 PROC_LOCK(p); 833 break; 834 } 835 sx_sunlock(&allproc_lock); 836 return (p); 837 } 838 839 840 /* 841 * Must be called with the process locked and will return with it unlocked. 842 */ 843 static int 844 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 845 { 846 struct kinfo_proc kinfo_proc; 847 int error; 848 struct proc *np; 849 pid_t pid = p->p_pid; 850 851 PROC_LOCK_ASSERT(p, MA_OWNED); 852 fill_kinfo_proc(p, &kinfo_proc); 853 PROC_UNLOCK(p); 854 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc)); 855 if (error) 856 return (error); 857 if (doingzomb) 858 np = zpfind(pid); 859 else { 860 if (pid == 0) 861 return (0); 862 np = pfind(pid); 863 } 864 if (np == NULL) 865 return EAGAIN; 866 if (np != p) { 867 PROC_UNLOCK(np); 868 return EAGAIN; 869 } 870 PROC_UNLOCK(np); 871 return (0); 872 } 873 874 static int 875 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 876 { 877 int *name = (int*) arg1; 878 u_int namelen = arg2; 879 struct proc *p; 880 int doingzomb; 881 int error = 0; 882 883 if (oidp->oid_number == KERN_PROC_PID) { 884 if (namelen != 1) 885 return (EINVAL); 886 p = pfind((pid_t)name[0]); 887 if (!p) 888 return (0); 889 if (p_cansee(curthread, p)) { 890 PROC_UNLOCK(p); 891 return (0); 892 } 893 error = sysctl_out_proc(p, req, 0); 894 return (error); 895 } 896 if (oidp->oid_number == KERN_PROC_ALL && !namelen) 897 ; 898 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 899 ; 900 else 901 return (EINVAL); 902 903 if (!req->oldptr) { 904 /* overestimate by 5 procs */ 905 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 906 if (error) 907 return (error); 908 } 909 sysctl_wire_old_buffer(req, 0); 910 sx_slock(&allproc_lock); 911 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 912 if (!doingzomb) 913 p = LIST_FIRST(&allproc); 914 else 915 p = LIST_FIRST(&zombproc); 916 for (; p != 0; p = LIST_NEXT(p, p_list)) { 917 PROC_LOCK(p); 918 /* 919 * Show a user only appropriate processes. 920 */ 921 if (p_cansee(curthread, p)) { 922 PROC_UNLOCK(p); 923 continue; 924 } 925 /* 926 * Skip embryonic processes. 927 */ 928 if (p->p_state == PRS_NEW) { 929 PROC_UNLOCK(p); 930 continue; 931 } 932 /* 933 * TODO - make more efficient (see notes below). 934 * do by session. 935 */ 936 switch (oidp->oid_number) { 937 938 case KERN_PROC_PGRP: 939 /* could do this by traversing pgrp */ 940 if (p->p_pgrp == NULL || 941 p->p_pgrp->pg_id != (pid_t)name[0]) { 942 PROC_UNLOCK(p); 943 continue; 944 } 945 break; 946 947 case KERN_PROC_TTY: 948 if ((p->p_flag & P_CONTROLT) == 0 || 949 p->p_session == NULL) { 950 PROC_UNLOCK(p); 951 continue; 952 } 953 SESS_LOCK(p->p_session); 954 if (p->p_session->s_ttyp == NULL || 955 dev2udev(p->p_session->s_ttyp->t_dev) != 956 (udev_t)name[0]) { 957 SESS_UNLOCK(p->p_session); 958 PROC_UNLOCK(p); 959 continue; 960 } 961 SESS_UNLOCK(p->p_session); 962 break; 963 964 case KERN_PROC_UID: 965 if (p->p_ucred == NULL || 966 p->p_ucred->cr_uid != (uid_t)name[0]) { 967 PROC_UNLOCK(p); 968 continue; 969 } 970 break; 971 972 case KERN_PROC_RUID: 973 if (p->p_ucred == NULL || 974 p->p_ucred->cr_ruid != (uid_t)name[0]) { 975 PROC_UNLOCK(p); 976 continue; 977 } 978 break; 979 } 980 981 error = sysctl_out_proc(p, req, doingzomb); 982 if (error) { 983 sx_sunlock(&allproc_lock); 984 return (error); 985 } 986 } 987 } 988 sx_sunlock(&allproc_lock); 989 return (0); 990 } 991 992 struct pargs * 993 pargs_alloc(int len) 994 { 995 struct pargs *pa; 996 997 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 998 M_WAITOK); 999 pa->ar_ref = 1; 1000 pa->ar_length = len; 1001 return (pa); 1002 } 1003 1004 void 1005 pargs_free(struct pargs *pa) 1006 { 1007 1008 FREE(pa, M_PARGS); 1009 } 1010 1011 void 1012 pargs_hold(struct pargs *pa) 1013 { 1014 1015 if (pa == NULL) 1016 return; 1017 PARGS_LOCK(pa); 1018 pa->ar_ref++; 1019 PARGS_UNLOCK(pa); 1020 } 1021 1022 void 1023 pargs_drop(struct pargs *pa) 1024 { 1025 1026 if (pa == NULL) 1027 return; 1028 PARGS_LOCK(pa); 1029 if (--pa->ar_ref == 0) { 1030 PARGS_UNLOCK(pa); 1031 pargs_free(pa); 1032 } else 1033 PARGS_UNLOCK(pa); 1034 } 1035 1036 /* 1037 * This sysctl allows a process to retrieve the argument list or process 1038 * title for another process without groping around in the address space 1039 * of the other process. It also allow a process to set its own "process 1040 * title to a string of its own choice. 1041 */ 1042 static int 1043 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1044 { 1045 int *name = (int*) arg1; 1046 u_int namelen = arg2; 1047 struct proc *p; 1048 struct pargs *pa; 1049 int error = 0; 1050 1051 if (namelen != 1) 1052 return (EINVAL); 1053 1054 p = pfind((pid_t)name[0]); 1055 if (!p) 1056 return (0); 1057 1058 if ((!ps_argsopen) && p_cansee(curthread, p)) { 1059 PROC_UNLOCK(p); 1060 return (0); 1061 } 1062 PROC_UNLOCK(p); 1063 1064 if (req->newptr && curproc != p) 1065 return (EPERM); 1066 1067 PROC_LOCK(p); 1068 pa = p->p_args; 1069 pargs_hold(pa); 1070 PROC_UNLOCK(p); 1071 if (req->oldptr && pa != NULL) { 1072 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1073 } 1074 pargs_drop(pa); 1075 if (req->newptr == NULL) 1076 return (error); 1077 1078 PROC_LOCK(p); 1079 pa = p->p_args; 1080 p->p_args = NULL; 1081 PROC_UNLOCK(p); 1082 pargs_drop(pa); 1083 1084 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1085 return (error); 1086 1087 pa = pargs_alloc(req->newlen); 1088 error = SYSCTL_IN(req, pa->ar_args, req->newlen); 1089 if (!error) { 1090 PROC_LOCK(p); 1091 p->p_args = pa; 1092 PROC_UNLOCK(p); 1093 } else 1094 pargs_free(pa); 1095 return (error); 1096 } 1097 1098 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1099 1100 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1101 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1102 1103 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1104 sysctl_kern_proc, "Process table"); 1105 1106 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1107 sysctl_kern_proc, "Process table"); 1108 1109 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1110 sysctl_kern_proc, "Process table"); 1111 1112 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1113 sysctl_kern_proc, "Process table"); 1114 1115 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1116 sysctl_kern_proc, "Process table"); 1117 1118 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1119 sysctl_kern_proc_args, "Process argument list"); 1120 1121