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 <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mutex.h> 43 #include <sys/proc.h> 44 #include <sys/sysproto.h> 45 #include <sys/sysctl.h> 46 #include <sys/filedesc.h> 47 #include <sys/tty.h> 48 #include <sys/signalvar.h> 49 #include <sys/sx.h> 50 #include <sys/user.h> 51 #include <sys/jail.h> 52 53 #include <vm/vm.h> 54 #include <vm/pmap.h> 55 #include <vm/vm_map.h> 56 #include <vm/uma.h> 57 #include <machine/critical.h> 58 59 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 60 MALLOC_DEFINE(M_SESSION, "session", "session header"); 61 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 62 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 63 64 static struct proc *dopfind(register pid_t); 65 66 static void doenterpgrp(struct proc *, struct pgrp *); 67 68 static void pgdelete(struct pgrp *); 69 70 static void orphanpg(struct pgrp *pg); 71 72 /* 73 * Other process lists 74 */ 75 struct pidhashhead *pidhashtbl; 76 u_long pidhash; 77 struct pgrphashhead *pgrphashtbl; 78 u_long pgrphash; 79 struct proclist allproc; 80 struct proclist zombproc; 81 struct sx allproc_lock; 82 struct sx proctree_lock; 83 struct mtx pargs_ref_lock; 84 uma_zone_t proc_zone; 85 uma_zone_t ithread_zone; 86 87 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 88 89 /* 90 * Initialize global process hashing structures. 91 */ 92 void 93 procinit() 94 { 95 96 sx_init(&allproc_lock, "allproc"); 97 sx_init(&proctree_lock, "proctree"); 98 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF); 99 LIST_INIT(&allproc); 100 LIST_INIT(&zombproc); 101 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 102 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 103 proc_zone = uma_zcreate("PROC", sizeof (struct proc), NULL, NULL, 104 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 105 uihashinit(); 106 } 107 108 /* 109 * Note that we do not link to the proc's ucred here 110 * The thread is linked as if running but no KSE assigned 111 */ 112 static void 113 thread_link(struct thread *td, struct ksegrp *kg) 114 { 115 struct proc *p = kg->kg_proc; 116 117 td->td_proc = p; 118 td->td_ksegrp = kg; 119 td->td_last_kse = &p->p_kse; 120 121 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 122 TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist); 123 td->td_critnest = 0; 124 td->td_kse = NULL; 125 cpu_thread_link(td); 126 } 127 128 /* 129 * KSE is linked onto the idle queue. 130 */ 131 static void 132 kse_link(struct kse *ke, struct ksegrp *kg) 133 { 134 struct proc *p = kg->kg_proc; 135 136 TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist); 137 kg->kg_kses++; 138 TAILQ_INSERT_HEAD(&kg->kg_iq, ke, ke_kgrlist); 139 ke->ke_proc = p; 140 ke->ke_ksegrp = kg; 141 ke->ke_thread = NULL; 142 ke->ke_oncpu = NOCPU; 143 } 144 145 static void 146 ksegrp_link(struct ksegrp *kg, struct proc *p) 147 { 148 149 TAILQ_INIT(&kg->kg_threads); 150 TAILQ_INIT(&kg->kg_runq); /* links with td_runq */ 151 TAILQ_INIT(&kg->kg_slpq); /* links with td_runq */ 152 TAILQ_INIT(&kg->kg_kseq); /* all kses in ksegrp */ 153 TAILQ_INIT(&kg->kg_iq); /* all kses in ksegrp */ 154 kg->kg_proc = p; 155 /* the following counters are in the -zero- section and may not need clearing */ 156 kg->kg_runnable = 0; 157 kg->kg_kses = 0; 158 kg->kg_runq_kses = 0; /* XXXKSE change name */ 159 /* link it in now that it's consitant */ 160 TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp); 161 } 162 163 /* 164 * for a newly created process, 165 * link up a the structure and its initial threads etc. 166 */ 167 void 168 proc_linkup(struct proc *p, struct ksegrp *kg, 169 struct kse *ke, struct thread *td) 170 { 171 172 TAILQ_INIT(&p->p_ksegrps); /* all ksegrps in proc */ 173 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 174 175 ksegrp_link(kg, p); 176 kse_link(ke, kg); 177 thread_link(td, kg); 178 /* link them together for 1:1 */ 179 td->td_kse = ke; 180 ke->ke_thread = td; 181 } 182 183 /* temporary version is ultra simple while we are in 1:1 mode */ 184 struct thread * 185 thread_get(struct proc *p) 186 { 187 struct thread *td = &p->p_xxthread; 188 189 return (td); 190 } 191 192 193 /********************* 194 * STUB KSE syscalls 195 *********************/ 196 197 /* struct thread_wakeup_args { struct thread_mailbox *tmbx; }; */ 198 int 199 thread_wakeup(struct thread *td, struct thread_wakeup_args *uap) 200 { 201 202 return(ENOSYS); 203 } 204 205 int 206 kse_exit(struct thread *td, struct kse_exit_args *uap) 207 { 208 209 return(ENOSYS); 210 } 211 212 int 213 kse_yield(struct thread *td, struct kse_yield_args *uap) 214 { 215 216 return(ENOSYS); 217 } 218 219 int kse_wakeup(struct thread *td, struct kse_wakeup_args *uap) 220 { 221 222 return(ENOSYS); 223 } 224 225 226 int 227 kse_new(struct thread *td, struct kse_new_args *uap) 228 /* struct kse_new_args { 229 struct kse_mailbox *mbx; 230 int new_grp_flag; 231 }; */ 232 { 233 234 return (ENOSYS); 235 } 236 237 /* 238 * Is p an inferior of the current process? 239 */ 240 int 241 inferior(p) 242 register struct proc *p; 243 { 244 245 sx_assert(&proctree_lock, SX_LOCKED); 246 for (; p != curproc; p = p->p_pptr) 247 if (p->p_pid == 0) 248 return (0); 249 return (1); 250 } 251 252 /* 253 * Locate a process by number 254 */ 255 struct proc * 256 pfind(pid) 257 register pid_t pid; 258 { 259 register struct proc *p; 260 261 sx_slock(&allproc_lock); 262 p = dopfind(pid); 263 sx_sunlock(&allproc_lock); 264 return (p); 265 } 266 267 static struct proc * 268 dopfind(pid) 269 register pid_t pid; 270 { 271 register struct proc *p; 272 273 sx_assert(&allproc_lock, SX_LOCKED); 274 275 LIST_FOREACH(p, PIDHASH(pid), p_hash) 276 if (p->p_pid == pid) { 277 PROC_LOCK(p); 278 break; 279 } 280 return (p); 281 } 282 283 /* 284 * Locate a process group by number. 285 * The caller must hold proctree_lock. 286 */ 287 struct pgrp * 288 pgfind(pgid) 289 register pid_t pgid; 290 { 291 register struct pgrp *pgrp; 292 293 sx_assert(&proctree_lock, SX_LOCKED); 294 295 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 296 if (pgrp->pg_id == pgid) { 297 PGRP_LOCK(pgrp); 298 return (pgrp); 299 } 300 } 301 return (NULL); 302 } 303 304 /* 305 * Create a new process group. 306 * pgid must be equal to the pid of p. 307 * Begin a new session if required. 308 */ 309 int 310 enterpgrp(p, pgid, pgrp, sess) 311 register struct proc *p; 312 pid_t pgid; 313 struct pgrp *pgrp; 314 struct session *sess; 315 { 316 struct pgrp *pgrp2; 317 318 sx_assert(&proctree_lock, SX_XLOCKED); 319 320 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 321 KASSERT(p->p_pid == pgid, 322 ("enterpgrp: new pgrp and pid != pgid")); 323 324 pgrp2 = pgfind(pgid); 325 326 KASSERT(pgrp2 == NULL, 327 ("enterpgrp: pgrp with pgid exists")); 328 KASSERT(!SESS_LEADER(p), 329 ("enterpgrp: session leader attempted setpgrp")); 330 331 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 332 333 if (sess != NULL) { 334 /* 335 * new session 336 */ 337 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 338 PROC_LOCK(p); 339 p->p_flag &= ~P_CONTROLT; 340 PROC_UNLOCK(p); 341 PGRP_LOCK(pgrp); 342 sess->s_leader = p; 343 sess->s_sid = p->p_pid; 344 sess->s_count = 1; 345 sess->s_ttyvp = NULL; 346 sess->s_ttyp = NULL; 347 bcopy(p->p_session->s_login, sess->s_login, 348 sizeof(sess->s_login)); 349 pgrp->pg_session = sess; 350 KASSERT(p == curproc, 351 ("enterpgrp: mksession and p != curproc")); 352 } else { 353 pgrp->pg_session = p->p_session; 354 SESS_LOCK(pgrp->pg_session); 355 pgrp->pg_session->s_count++; 356 SESS_UNLOCK(pgrp->pg_session); 357 PGRP_LOCK(pgrp); 358 } 359 pgrp->pg_id = pgid; 360 LIST_INIT(&pgrp->pg_members); 361 362 /* 363 * As we have an exclusive lock of proctree_lock, 364 * this should not deadlock. 365 */ 366 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 367 pgrp->pg_jobc = 0; 368 SLIST_INIT(&pgrp->pg_sigiolst); 369 PGRP_UNLOCK(pgrp); 370 371 doenterpgrp(p, pgrp); 372 373 return (0); 374 } 375 376 /* 377 * Move p to an existing process group 378 */ 379 int 380 enterthispgrp(p, pgrp) 381 register struct proc *p; 382 struct pgrp *pgrp; 383 { 384 385 sx_assert(&proctree_lock, SX_XLOCKED); 386 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 387 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 388 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 389 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 390 KASSERT(pgrp->pg_session == p->p_session, 391 ("%s: pgrp's session %p, p->p_session %p.\n", 392 __func__, 393 pgrp->pg_session, 394 p->p_session)); 395 KASSERT(pgrp != p->p_pgrp, 396 ("%s: p belongs to pgrp.", __func__)); 397 398 doenterpgrp(p, pgrp); 399 400 return (0); 401 } 402 403 /* 404 * Move p to a process group 405 */ 406 static void 407 doenterpgrp(p, pgrp) 408 struct proc *p; 409 struct pgrp *pgrp; 410 { 411 struct pgrp *savepgrp; 412 413 sx_assert(&proctree_lock, SX_XLOCKED); 414 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 415 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 416 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 417 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 418 419 savepgrp = p->p_pgrp; 420 421 /* 422 * Adjust eligibility of affected pgrps to participate in job control. 423 * Increment eligibility counts before decrementing, otherwise we 424 * could reach 0 spuriously during the first call. 425 */ 426 fixjobc(p, pgrp, 1); 427 fixjobc(p, p->p_pgrp, 0); 428 429 PGRP_LOCK(pgrp); 430 PGRP_LOCK(savepgrp); 431 PROC_LOCK(p); 432 LIST_REMOVE(p, p_pglist); 433 p->p_pgrp = pgrp; 434 PROC_UNLOCK(p); 435 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 436 PGRP_UNLOCK(savepgrp); 437 PGRP_UNLOCK(pgrp); 438 if (LIST_EMPTY(&savepgrp->pg_members)) 439 pgdelete(savepgrp); 440 } 441 442 /* 443 * remove process from process group 444 */ 445 int 446 leavepgrp(p) 447 register struct proc *p; 448 { 449 struct pgrp *savepgrp; 450 451 sx_assert(&proctree_lock, SX_XLOCKED); 452 savepgrp = p->p_pgrp; 453 PGRP_LOCK(savepgrp); 454 PROC_LOCK(p); 455 LIST_REMOVE(p, p_pglist); 456 p->p_pgrp = NULL; 457 PROC_UNLOCK(p); 458 PGRP_UNLOCK(savepgrp); 459 if (LIST_EMPTY(&savepgrp->pg_members)) 460 pgdelete(savepgrp); 461 return (0); 462 } 463 464 /* 465 * delete a process group 466 */ 467 static void 468 pgdelete(pgrp) 469 register struct pgrp *pgrp; 470 { 471 struct session *savesess; 472 473 sx_assert(&proctree_lock, SX_XLOCKED); 474 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 475 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 476 477 SIGIO_LOCK(); 478 PGRP_LOCK(pgrp); 479 480 /* 481 * Reset any sigio structures pointing to us as a result of 482 * F_SETOWN with our pgid. 483 */ 484 funsetownlst(&pgrp->pg_sigiolst); 485 SIGIO_UNLOCK(); 486 487 if (pgrp->pg_session->s_ttyp != NULL && 488 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 489 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 490 LIST_REMOVE(pgrp, pg_hash); 491 savesess = pgrp->pg_session; 492 SESS_LOCK(savesess); 493 savesess->s_count--; 494 SESS_UNLOCK(savesess); 495 PGRP_UNLOCK(pgrp); 496 if (savesess->s_count == 0) { 497 mtx_destroy(&savesess->s_mtx); 498 FREE(pgrp->pg_session, M_SESSION); 499 } 500 mtx_destroy(&pgrp->pg_mtx); 501 FREE(pgrp, M_PGRP); 502 } 503 504 /* 505 * Adjust pgrp jobc counters when specified process changes process group. 506 * We count the number of processes in each process group that "qualify" 507 * the group for terminal job control (those with a parent in a different 508 * process group of the same session). If that count reaches zero, the 509 * process group becomes orphaned. Check both the specified process' 510 * process group and that of its children. 511 * entering == 0 => p is leaving specified group. 512 * entering == 1 => p is entering specified group. 513 */ 514 void 515 fixjobc(p, pgrp, entering) 516 register struct proc *p; 517 register struct pgrp *pgrp; 518 int entering; 519 { 520 register struct pgrp *hispgrp; 521 register struct session *mysession; 522 523 sx_assert(&proctree_lock, SX_LOCKED); 524 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 525 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 526 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 527 528 /* 529 * Check p's parent to see whether p qualifies its own process 530 * group; if so, adjust count for p's process group. 531 */ 532 mysession = pgrp->pg_session; 533 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 534 hispgrp->pg_session == mysession) { 535 PGRP_LOCK(pgrp); 536 if (entering) 537 pgrp->pg_jobc++; 538 else { 539 --pgrp->pg_jobc; 540 if (pgrp->pg_jobc == 0) 541 orphanpg(pgrp); 542 } 543 PGRP_UNLOCK(pgrp); 544 } 545 546 /* 547 * Check this process' children to see whether they qualify 548 * their process groups; if so, adjust counts for children's 549 * process groups. 550 */ 551 LIST_FOREACH(p, &p->p_children, p_sibling) { 552 if ((hispgrp = p->p_pgrp) != pgrp && 553 hispgrp->pg_session == mysession && 554 p->p_stat != SZOMB) { 555 PGRP_LOCK(hispgrp); 556 if (entering) 557 hispgrp->pg_jobc++; 558 else { 559 --hispgrp->pg_jobc; 560 if (hispgrp->pg_jobc == 0) 561 orphanpg(hispgrp); 562 } 563 PGRP_UNLOCK(hispgrp); 564 } 565 } 566 } 567 568 /* 569 * A process group has become orphaned; 570 * if there are any stopped processes in the group, 571 * hang-up all process in that group. 572 */ 573 static void 574 orphanpg(pg) 575 struct pgrp *pg; 576 { 577 register struct proc *p; 578 579 PGRP_LOCK_ASSERT(pg, MA_OWNED); 580 581 mtx_lock_spin(&sched_lock); 582 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 583 if (p->p_stat == SSTOP) { 584 mtx_unlock_spin(&sched_lock); 585 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 586 PROC_LOCK(p); 587 psignal(p, SIGHUP); 588 psignal(p, SIGCONT); 589 PROC_UNLOCK(p); 590 } 591 return; 592 } 593 } 594 mtx_unlock_spin(&sched_lock); 595 } 596 597 #include "opt_ddb.h" 598 #ifdef DDB 599 #include <ddb/ddb.h> 600 601 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 602 { 603 register struct pgrp *pgrp; 604 register struct proc *p; 605 register int i; 606 607 for (i = 0; i <= pgrphash; i++) { 608 if (!LIST_EMPTY(&pgrphashtbl[i])) { 609 printf("\tindx %d\n", i); 610 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 611 printf( 612 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 613 (void *)pgrp, (long)pgrp->pg_id, 614 (void *)pgrp->pg_session, 615 pgrp->pg_session->s_count, 616 (void *)LIST_FIRST(&pgrp->pg_members)); 617 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 618 printf("\t\tpid %ld addr %p pgrp %p\n", 619 (long)p->p_pid, (void *)p, 620 (void *)p->p_pgrp); 621 } 622 } 623 } 624 } 625 } 626 #endif /* DDB */ 627 628 /* 629 * Fill in an kinfo_proc structure for the specified process. 630 * Must be called with the target process locked. 631 */ 632 void 633 fill_kinfo_proc(p, kp) 634 struct proc *p; 635 struct kinfo_proc *kp; 636 { 637 struct thread *td; 638 struct tty *tp; 639 struct session *sp; 640 struct timeval tv; 641 642 bzero(kp, sizeof(*kp)); 643 644 kp->ki_structsize = sizeof(*kp); 645 kp->ki_paddr = p; 646 PROC_LOCK_ASSERT(p, MA_OWNED); 647 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 648 kp->ki_args = p->p_args; 649 kp->ki_tracep = p->p_tracep; 650 kp->ki_textvp = p->p_textvp; 651 kp->ki_fd = p->p_fd; 652 kp->ki_vmspace = p->p_vmspace; 653 if (p->p_ucred) { 654 kp->ki_uid = p->p_ucred->cr_uid; 655 kp->ki_ruid = p->p_ucred->cr_ruid; 656 kp->ki_svuid = p->p_ucred->cr_svuid; 657 /* XXX bde doesn't like KI_NGROUPS */ 658 kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS); 659 bcopy(p->p_ucred->cr_groups, kp->ki_groups, 660 kp->ki_ngroups * sizeof(gid_t)); 661 kp->ki_rgid = p->p_ucred->cr_rgid; 662 kp->ki_svgid = p->p_ucred->cr_svgid; 663 } 664 if (p->p_procsig) { 665 kp->ki_sigignore = p->p_procsig->ps_sigignore; 666 kp->ki_sigcatch = p->p_procsig->ps_sigcatch; 667 } 668 mtx_lock_spin(&sched_lock); 669 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 670 struct vmspace *vm = p->p_vmspace; 671 672 kp->ki_size = vm->vm_map.size; 673 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 674 if (p->p_sflag & PS_INMEM) 675 kp->ki_rssize += UAREA_PAGES; 676 FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */ 677 kp->ki_rssize += KSTACK_PAGES; 678 kp->ki_swrss = vm->vm_swrss; 679 kp->ki_tsize = vm->vm_tsize; 680 kp->ki_dsize = vm->vm_dsize; 681 kp->ki_ssize = vm->vm_ssize; 682 } 683 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 684 kp->ki_start = p->p_stats->p_start; 685 kp->ki_rusage = p->p_stats->p_ru; 686 kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec + 687 p->p_stats->p_cru.ru_stime.tv_sec; 688 kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec + 689 p->p_stats->p_cru.ru_stime.tv_usec; 690 } 691 td = FIRST_THREAD_IN_PROC(p); 692 if (td->td_wmesg != NULL) 693 strncpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg) - 1); 694 if (p->p_stat == SMTX) { 695 kp->ki_kiflag |= KI_MTXBLOCK; 696 strncpy(kp->ki_mtxname, td->td_mtxname, 697 sizeof(kp->ki_mtxname) - 1); 698 } 699 kp->ki_stat = p->p_stat; 700 kp->ki_sflag = p->p_sflag; 701 kp->ki_swtime = p->p_swtime; 702 kp->ki_traceflag = p->p_traceflag; 703 kp->ki_pid = p->p_pid; 704 /* vvv XXXKSE */ 705 bintime2timeval(&p->p_runtime, &tv); 706 kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec; 707 kp->ki_pctcpu = p->p_kse.ke_pctcpu; 708 kp->ki_estcpu = td->td_ksegrp->kg_estcpu; 709 kp->ki_slptime = td->td_ksegrp->kg_slptime; 710 kp->ki_wchan = td->td_wchan; 711 kp->ki_pri.pri_level = td->td_priority; 712 kp->ki_pri.pri_user = td->td_ksegrp->kg_user_pri; 713 kp->ki_pri.pri_class = td->td_ksegrp->kg_pri_class; 714 kp->ki_pri.pri_native = td->td_base_pri; 715 kp->ki_nice = td->td_ksegrp->kg_nice; 716 kp->ki_rqindex = p->p_kse.ke_rqindex; 717 kp->ki_oncpu = p->p_kse.ke_oncpu; 718 kp->ki_lastcpu = td->td_lastcpu; 719 kp->ki_tdflags = td->td_flags; 720 kp->ki_pcb = td->td_pcb; 721 kp->ki_kstack = (void *)td->td_kstack; 722 /* ^^^ XXXKSE */ 723 mtx_unlock_spin(&sched_lock); 724 sp = NULL; 725 tp = NULL; 726 if (p->p_pgrp) { 727 kp->ki_pgid = p->p_pgrp->pg_id; 728 kp->ki_jobc = p->p_pgrp->pg_jobc; 729 sp = p->p_pgrp->pg_session; 730 731 if (sp != NULL) { 732 kp->ki_sid = sp->s_sid; 733 SESS_LOCK(sp); 734 strncpy(kp->ki_login, sp->s_login, 735 sizeof(kp->ki_login) - 1); 736 if (sp->s_ttyvp) 737 kp->ki_kiflag |= KI_CTTY; 738 if (SESS_LEADER(p)) 739 kp->ki_kiflag |= KI_SLEADER; 740 tp = sp->s_ttyp; 741 SESS_UNLOCK(sp); 742 } 743 } 744 if ((p->p_flag & P_CONTROLT) && tp != NULL) { 745 kp->ki_tdev = dev2udev(tp->t_dev); 746 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 747 if (tp->t_session) 748 kp->ki_tsid = tp->t_session->s_sid; 749 } else 750 kp->ki_tdev = NOUDEV; 751 if (p->p_comm[0] != '\0') { 752 strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1); 753 strncpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm) - 1); 754 } 755 kp->ki_siglist = p->p_siglist; 756 kp->ki_sigmask = p->p_sigmask; 757 kp->ki_xstat = p->p_xstat; 758 kp->ki_acflag = p->p_acflag; 759 kp->ki_flag = p->p_flag; 760 /* If jailed(p->p_ucred), emulate the old P_JAILED flag. */ 761 if (jailed(p->p_ucred)) 762 kp->ki_flag |= P_JAILED; 763 kp->ki_lock = p->p_lock; 764 if (p->p_pptr) 765 kp->ki_ppid = p->p_pptr->p_pid; 766 } 767 768 /* 769 * Locate a zombie process by number 770 */ 771 struct proc * 772 zpfind(pid_t pid) 773 { 774 struct proc *p; 775 776 sx_slock(&allproc_lock); 777 LIST_FOREACH(p, &zombproc, p_list) 778 if (p->p_pid == pid) { 779 PROC_LOCK(p); 780 break; 781 } 782 sx_sunlock(&allproc_lock); 783 return (p); 784 } 785 786 787 /* 788 * Must be called with the process locked and will return with it unlocked. 789 */ 790 static int 791 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 792 { 793 struct kinfo_proc kinfo_proc; 794 int error; 795 struct proc *np; 796 pid_t pid = p->p_pid; 797 798 PROC_LOCK_ASSERT(p, MA_OWNED); 799 fill_kinfo_proc(p, &kinfo_proc); 800 PROC_UNLOCK(p); 801 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc)); 802 if (error) 803 return (error); 804 if (doingzomb) 805 np = zpfind(pid); 806 else { 807 if (pid == 0) 808 return (0); 809 np = pfind(pid); 810 } 811 if (np == NULL) 812 return EAGAIN; 813 if (np != p) { 814 PROC_UNLOCK(np); 815 return EAGAIN; 816 } 817 PROC_UNLOCK(np); 818 return (0); 819 } 820 821 static int 822 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 823 { 824 int *name = (int*) arg1; 825 u_int namelen = arg2; 826 struct proc *p; 827 int doingzomb; 828 int error = 0; 829 830 if (oidp->oid_number == KERN_PROC_PID) { 831 if (namelen != 1) 832 return (EINVAL); 833 p = pfind((pid_t)name[0]); 834 if (!p) 835 return (0); 836 if (p_cansee(curproc, p)) { 837 PROC_UNLOCK(p); 838 return (0); 839 } 840 error = sysctl_out_proc(p, req, 0); 841 return (error); 842 } 843 if (oidp->oid_number == KERN_PROC_ALL && !namelen) 844 ; 845 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 846 ; 847 else 848 return (EINVAL); 849 850 if (!req->oldptr) { 851 /* overestimate by 5 procs */ 852 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 853 if (error) 854 return (error); 855 } 856 sx_slock(&allproc_lock); 857 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 858 if (!doingzomb) 859 p = LIST_FIRST(&allproc); 860 else 861 p = LIST_FIRST(&zombproc); 862 for (; p != 0; p = LIST_NEXT(p, p_list)) { 863 PROC_LOCK(p); 864 /* 865 * Show a user only appropriate processes. 866 */ 867 if (p_cansee(curproc, p)) { 868 PROC_UNLOCK(p); 869 continue; 870 } 871 /* 872 * Skip embryonic processes. 873 */ 874 if (p->p_stat == SIDL) { 875 PROC_UNLOCK(p); 876 continue; 877 } 878 /* 879 * TODO - make more efficient (see notes below). 880 * do by session. 881 */ 882 switch (oidp->oid_number) { 883 884 case KERN_PROC_PGRP: 885 /* could do this by traversing pgrp */ 886 if (p->p_pgrp == NULL || 887 p->p_pgrp->pg_id != (pid_t)name[0]) { 888 PROC_UNLOCK(p); 889 continue; 890 } 891 break; 892 893 case KERN_PROC_TTY: 894 if ((p->p_flag & P_CONTROLT) == 0 || 895 p->p_session == NULL) { 896 PROC_UNLOCK(p); 897 continue; 898 } 899 SESS_LOCK(p->p_session); 900 if (p->p_session->s_ttyp == NULL || 901 dev2udev(p->p_session->s_ttyp->t_dev) != 902 (udev_t)name[0]) { 903 SESS_UNLOCK(p->p_session); 904 PROC_UNLOCK(p); 905 continue; 906 } 907 SESS_UNLOCK(p->p_session); 908 break; 909 910 case KERN_PROC_UID: 911 if (p->p_ucred == NULL || 912 p->p_ucred->cr_uid != (uid_t)name[0]) { 913 PROC_UNLOCK(p); 914 continue; 915 } 916 break; 917 918 case KERN_PROC_RUID: 919 if (p->p_ucred == NULL || 920 p->p_ucred->cr_ruid != (uid_t)name[0]) { 921 PROC_UNLOCK(p); 922 continue; 923 } 924 break; 925 } 926 927 error = sysctl_out_proc(p, req, doingzomb); 928 if (error) { 929 sx_sunlock(&allproc_lock); 930 return (error); 931 } 932 } 933 } 934 sx_sunlock(&allproc_lock); 935 return (0); 936 } 937 938 struct pargs * 939 pargs_alloc(int len) 940 { 941 struct pargs *pa; 942 943 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 944 M_WAITOK); 945 pa->ar_ref = 1; 946 pa->ar_length = len; 947 return (pa); 948 } 949 950 void 951 pargs_free(struct pargs *pa) 952 { 953 954 FREE(pa, M_PARGS); 955 } 956 957 void 958 pargs_hold(struct pargs *pa) 959 { 960 961 if (pa == NULL) 962 return; 963 PARGS_LOCK(pa); 964 pa->ar_ref++; 965 PARGS_UNLOCK(pa); 966 } 967 968 void 969 pargs_drop(struct pargs *pa) 970 { 971 972 if (pa == NULL) 973 return; 974 PARGS_LOCK(pa); 975 if (--pa->ar_ref == 0) { 976 PARGS_UNLOCK(pa); 977 pargs_free(pa); 978 } else 979 PARGS_UNLOCK(pa); 980 } 981 982 /* 983 * This sysctl allows a process to retrieve the argument list or process 984 * title for another process without groping around in the address space 985 * of the other process. It also allow a process to set its own "process 986 * title to a string of its own choice. 987 */ 988 static int 989 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 990 { 991 int *name = (int*) arg1; 992 u_int namelen = arg2; 993 struct proc *p; 994 struct pargs *pa; 995 int error = 0; 996 997 if (namelen != 1) 998 return (EINVAL); 999 1000 p = pfind((pid_t)name[0]); 1001 if (!p) 1002 return (0); 1003 1004 if ((!ps_argsopen) && p_cansee(curproc, p)) { 1005 PROC_UNLOCK(p); 1006 return (0); 1007 } 1008 PROC_UNLOCK(p); 1009 1010 if (req->newptr && curproc != p) 1011 return (EPERM); 1012 1013 PROC_LOCK(p); 1014 pa = p->p_args; 1015 pargs_hold(pa); 1016 PROC_UNLOCK(p); 1017 if (req->oldptr && pa != NULL) { 1018 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 1019 } 1020 if (req->newptr == NULL) { 1021 pargs_drop(pa); 1022 return (error); 1023 } 1024 1025 PROC_LOCK(p); 1026 pa = p->p_args; 1027 p->p_args = NULL; 1028 PROC_UNLOCK(p); 1029 pargs_drop(pa); 1030 1031 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 1032 return (error); 1033 1034 pa = pargs_alloc(req->newlen); 1035 error = SYSCTL_IN(req, pa->ar_args, req->newlen); 1036 if (!error) { 1037 PROC_LOCK(p); 1038 p->p_args = pa; 1039 PROC_UNLOCK(p); 1040 } else 1041 pargs_free(pa); 1042 return (error); 1043 } 1044 1045 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1046 1047 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1048 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1049 1050 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1051 sysctl_kern_proc, "Process table"); 1052 1053 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1054 sysctl_kern_proc, "Process table"); 1055 1056 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1057 sysctl_kern_proc, "Process table"); 1058 1059 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1060 sysctl_kern_proc, "Process table"); 1061 1062 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1063 sysctl_kern_proc, "Process table"); 1064 1065 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1066 sysctl_kern_proc_args, "Process argument list"); 1067