1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 36 * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $ 37 * $DragonFly: src/sys/kern/kern_proc.c,v 1.45 2008/06/12 23:25:02 dillon Exp $ 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/sysctl.h> 44 #include <sys/malloc.h> 45 #include <sys/proc.h> 46 #include <sys/jail.h> 47 #include <sys/filedesc.h> 48 #include <sys/tty.h> 49 #include <sys/dsched.h> 50 #include <sys/signalvar.h> 51 #include <sys/spinlock.h> 52 #include <vm/vm.h> 53 #include <sys/lock.h> 54 #include <vm/pmap.h> 55 #include <vm/vm_map.h> 56 #include <sys/user.h> 57 #include <machine/smp.h> 58 59 #include <sys/spinlock2.h> 60 #include <sys/mplock2.h> 61 62 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 63 MALLOC_DEFINE(M_SESSION, "session", "session header"); 64 MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 65 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures"); 66 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 67 68 int ps_showallprocs = 1; 69 static int ps_showallthreads = 1; 70 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW, 71 &ps_showallprocs, 0, 72 "Unprivileged processes can see proccesses with different UID/GID"); 73 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW, 74 &ps_showallthreads, 0, 75 "Unprivileged processes can see kernel threads"); 76 77 static void pgdelete(struct pgrp *); 78 static void orphanpg(struct pgrp *pg); 79 static pid_t proc_getnewpid_locked(int random_offset); 80 81 /* 82 * Other process lists 83 */ 84 struct pidhashhead *pidhashtbl; 85 u_long pidhash; 86 struct pgrphashhead *pgrphashtbl; 87 u_long pgrphash; 88 struct proclist allproc; 89 struct proclist zombproc; 90 91 /* 92 * Random component to nextpid generation. We mix in a random factor to make 93 * it a little harder to predict. We sanity check the modulus value to avoid 94 * doing it in critical paths. Don't let it be too small or we pointlessly 95 * waste randomness entropy, and don't let it be impossibly large. Using a 96 * modulus that is too big causes a LOT more process table scans and slows 97 * down fork processing as the pidchecked caching is defeated. 98 */ 99 static int randompid = 0; 100 101 /* 102 * No requirements. 103 */ 104 static int 105 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) 106 { 107 int error, pid; 108 109 pid = randompid; 110 error = sysctl_handle_int(oidp, &pid, 0, req); 111 if (error || !req->newptr) 112 return (error); 113 if (pid < 0 || pid > PID_MAX - 100) /* out of range */ 114 pid = PID_MAX - 100; 115 else if (pid < 2) /* NOP */ 116 pid = 0; 117 else if (pid < 100) /* Make it reasonable */ 118 pid = 100; 119 randompid = pid; 120 return (error); 121 } 122 123 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, 124 0, 0, sysctl_kern_randompid, "I", "Random PID modulus"); 125 126 /* 127 * Initialize global process hashing structures. 128 * 129 * Called from the low level boot code only. 130 */ 131 void 132 procinit(void) 133 { 134 LIST_INIT(&allproc); 135 LIST_INIT(&zombproc); 136 lwkt_init(); 137 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 138 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 139 uihashinit(); 140 } 141 142 /* 143 * Is p an inferior of the current process? 144 * 145 * No requirements. 146 * The caller must hold proc_token if the caller wishes a stable result. 147 */ 148 int 149 inferior(struct proc *p) 150 { 151 lwkt_gettoken(&proc_token); 152 while (p != curproc) { 153 if (p->p_pid == 0) { 154 lwkt_reltoken(&proc_token); 155 return (0); 156 } 157 p = p->p_pptr; 158 } 159 lwkt_reltoken(&proc_token); 160 return (1); 161 } 162 163 /* 164 * Locate a process by number 165 * 166 * XXX TODO - change API to PHOLD() the returned process ? 167 * 168 * No requirements. 169 * The caller must hold proc_token if the caller wishes a stable result. 170 */ 171 struct proc * 172 pfind(pid_t pid) 173 { 174 struct proc *p; 175 176 lwkt_gettoken(&proc_token); 177 LIST_FOREACH(p, PIDHASH(pid), p_hash) { 178 if (p->p_pid == pid) { 179 lwkt_reltoken(&proc_token); 180 return (p); 181 } 182 } 183 lwkt_reltoken(&proc_token); 184 return (NULL); 185 } 186 187 /* 188 * Locate a process group by number 189 * 190 * No requirements. 191 * The caller must hold proc_token if the caller wishes a stable result. 192 */ 193 struct pgrp * 194 pgfind(pid_t pgid) 195 { 196 struct pgrp *pgrp; 197 198 lwkt_gettoken(&proc_token); 199 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 200 if (pgrp->pg_id == pgid) { 201 lwkt_reltoken(&proc_token); 202 return (pgrp); 203 } 204 } 205 lwkt_reltoken(&proc_token); 206 return (NULL); 207 } 208 209 /* 210 * Move p to a new or existing process group (and session) 211 * 212 * No requirements. 213 */ 214 int 215 enterpgrp(struct proc *p, pid_t pgid, int mksess) 216 { 217 struct pgrp *pgrp; 218 int error; 219 220 lwkt_gettoken(&proc_token); 221 pgrp = pgfind(pgid); 222 223 KASSERT(pgrp == NULL || !mksess, 224 ("enterpgrp: setsid into non-empty pgrp")); 225 KASSERT(!SESS_LEADER(p), 226 ("enterpgrp: session leader attempted setpgrp")); 227 228 if (pgrp == NULL) { 229 pid_t savepid = p->p_pid; 230 struct proc *np; 231 /* 232 * new process group 233 */ 234 KASSERT(p->p_pid == pgid, 235 ("enterpgrp: new pgrp and pid != pgid")); 236 if ((np = pfind(savepid)) == NULL || np != p) { 237 error = ESRCH; 238 goto fatal; 239 } 240 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), 241 M_PGRP, M_WAITOK); 242 if (mksess) { 243 struct session *sess; 244 245 /* 246 * new session 247 */ 248 MALLOC(sess, struct session *, sizeof(struct session), 249 M_SESSION, M_WAITOK); 250 sess->s_leader = p; 251 sess->s_sid = p->p_pid; 252 sess->s_count = 1; 253 sess->s_ttyvp = NULL; 254 sess->s_ttyp = NULL; 255 bcopy(p->p_session->s_login, sess->s_login, 256 sizeof(sess->s_login)); 257 p->p_flag &= ~P_CONTROLT; 258 pgrp->pg_session = sess; 259 KASSERT(p == curproc, 260 ("enterpgrp: mksession and p != curproc")); 261 } else { 262 pgrp->pg_session = p->p_session; 263 sess_hold(pgrp->pg_session); 264 } 265 pgrp->pg_id = pgid; 266 LIST_INIT(&pgrp->pg_members); 267 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 268 pgrp->pg_jobc = 0; 269 SLIST_INIT(&pgrp->pg_sigiolst); 270 lockinit(&pgrp->pg_lock, "pgwt", 0, 0); 271 } else if (pgrp == p->p_pgrp) { 272 goto done; 273 } 274 275 /* 276 * Adjust eligibility of affected pgrps to participate in job control. 277 * Increment eligibility counts before decrementing, otherwise we 278 * could reach 0 spuriously during the first call. 279 */ 280 fixjobc(p, pgrp, 1); 281 fixjobc(p, p->p_pgrp, 0); 282 283 LIST_REMOVE(p, p_pglist); 284 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 285 pgdelete(p->p_pgrp); 286 p->p_pgrp = pgrp; 287 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 288 done: 289 error = 0; 290 fatal: 291 lwkt_reltoken(&proc_token); 292 return (error); 293 } 294 295 /* 296 * Remove process from process group 297 * 298 * No requirements. 299 */ 300 int 301 leavepgrp(struct proc *p) 302 { 303 lwkt_gettoken(&proc_token); 304 LIST_REMOVE(p, p_pglist); 305 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 306 pgdelete(p->p_pgrp); 307 p->p_pgrp = NULL; 308 lwkt_reltoken(&proc_token); 309 return (0); 310 } 311 312 /* 313 * Delete a process group 314 * 315 * The caller must hold proc_token. 316 */ 317 static void 318 pgdelete(struct pgrp *pgrp) 319 { 320 /* 321 * Reset any sigio structures pointing to us as a result of 322 * F_SETOWN with our pgid. 323 */ 324 funsetownlst(&pgrp->pg_sigiolst); 325 326 if (pgrp->pg_session->s_ttyp != NULL && 327 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 328 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 329 LIST_REMOVE(pgrp, pg_hash); 330 sess_rele(pgrp->pg_session); 331 kfree(pgrp, M_PGRP); 332 } 333 334 /* 335 * Adjust the ref count on a session structure. When the ref count falls to 336 * zero the tty is disassociated from the session and the session structure 337 * is freed. Note that tty assocation is not itself ref-counted. 338 * 339 * No requirements. 340 */ 341 void 342 sess_hold(struct session *sp) 343 { 344 lwkt_gettoken(&tty_token); 345 ++sp->s_count; 346 lwkt_reltoken(&tty_token); 347 } 348 349 /* 350 * No requirements. 351 */ 352 void 353 sess_rele(struct session *sp) 354 { 355 struct tty *tp; 356 357 KKASSERT(sp->s_count > 0); 358 lwkt_gettoken(&tty_token); 359 if (--sp->s_count == 0) { 360 if (sp->s_ttyp && sp->s_ttyp->t_session) { 361 #ifdef TTY_DO_FULL_CLOSE 362 /* FULL CLOSE, see ttyclearsession() */ 363 KKASSERT(sp->s_ttyp->t_session == sp); 364 sp->s_ttyp->t_session = NULL; 365 #else 366 /* HALF CLOSE, see ttyclearsession() */ 367 if (sp->s_ttyp->t_session == sp) 368 sp->s_ttyp->t_session = NULL; 369 #endif 370 } 371 if ((tp = sp->s_ttyp) != NULL) { 372 sp->s_ttyp = NULL; 373 ttyunhold(tp); 374 } 375 kfree(sp, M_SESSION); 376 } 377 lwkt_reltoken(&tty_token); 378 } 379 380 /* 381 * Adjust pgrp jobc counters when specified process changes process group. 382 * We count the number of processes in each process group that "qualify" 383 * the group for terminal job control (those with a parent in a different 384 * process group of the same session). If that count reaches zero, the 385 * process group becomes orphaned. Check both the specified process' 386 * process group and that of its children. 387 * entering == 0 => p is leaving specified group. 388 * entering == 1 => p is entering specified group. 389 * 390 * No requirements. 391 */ 392 void 393 fixjobc(struct proc *p, struct pgrp *pgrp, int entering) 394 { 395 struct pgrp *hispgrp; 396 struct session *mysession; 397 398 /* 399 * Check p's parent to see whether p qualifies its own process 400 * group; if so, adjust count for p's process group. 401 */ 402 lwkt_gettoken(&proc_token); 403 mysession = pgrp->pg_session; 404 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 405 hispgrp->pg_session == mysession) { 406 if (entering) 407 pgrp->pg_jobc++; 408 else if (--pgrp->pg_jobc == 0) 409 orphanpg(pgrp); 410 } 411 412 /* 413 * Check this process' children to see whether they qualify 414 * their process groups; if so, adjust counts for children's 415 * process groups. 416 */ 417 LIST_FOREACH(p, &p->p_children, p_sibling) { 418 if ((hispgrp = p->p_pgrp) != pgrp && 419 hispgrp->pg_session == mysession && 420 p->p_stat != SZOMB) { 421 if (entering) 422 hispgrp->pg_jobc++; 423 else if (--hispgrp->pg_jobc == 0) 424 orphanpg(hispgrp); 425 } 426 } 427 lwkt_reltoken(&proc_token); 428 } 429 430 /* 431 * A process group has become orphaned; 432 * if there are any stopped processes in the group, 433 * hang-up all process in that group. 434 * 435 * The caller must hold proc_token. 436 */ 437 static void 438 orphanpg(struct pgrp *pg) 439 { 440 struct proc *p; 441 442 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 443 if (p->p_stat == SSTOP) { 444 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 445 ksignal(p, SIGHUP); 446 ksignal(p, SIGCONT); 447 } 448 return; 449 } 450 } 451 } 452 453 /* 454 * Add a new process to the allproc list and the PID hash. This 455 * also assigns a pid to the new process. 456 * 457 * No requirements. 458 */ 459 void 460 proc_add_allproc(struct proc *p) 461 { 462 int random_offset; 463 464 if ((random_offset = randompid) != 0) { 465 get_mplock(); 466 random_offset = karc4random() % random_offset; 467 rel_mplock(); 468 } 469 470 lwkt_gettoken(&proc_token); 471 p->p_pid = proc_getnewpid_locked(random_offset); 472 LIST_INSERT_HEAD(&allproc, p, p_list); 473 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash); 474 lwkt_reltoken(&proc_token); 475 } 476 477 /* 478 * Calculate a new process pid. This function is integrated into 479 * proc_add_allproc() to guarentee that the new pid is not reused before 480 * the new process can be added to the allproc list. 481 * 482 * The caller must hold proc_token. 483 */ 484 static 485 pid_t 486 proc_getnewpid_locked(int random_offset) 487 { 488 static pid_t nextpid; 489 static pid_t pidchecked; 490 struct proc *p; 491 492 /* 493 * Find an unused process ID. We remember a range of unused IDs 494 * ready to use (from nextpid+1 through pidchecked-1). 495 */ 496 nextpid = nextpid + 1 + random_offset; 497 retry: 498 /* 499 * If the process ID prototype has wrapped around, 500 * restart somewhat above 0, as the low-numbered procs 501 * tend to include daemons that don't exit. 502 */ 503 if (nextpid >= PID_MAX) { 504 nextpid = nextpid % PID_MAX; 505 if (nextpid < 100) 506 nextpid += 100; 507 pidchecked = 0; 508 } 509 if (nextpid >= pidchecked) { 510 int doingzomb = 0; 511 512 pidchecked = PID_MAX; 513 /* 514 * Scan the active and zombie procs to check whether this pid 515 * is in use. Remember the lowest pid that's greater 516 * than nextpid, so we can avoid checking for a while. 517 */ 518 p = LIST_FIRST(&allproc); 519 again: 520 for (; p != 0; p = LIST_NEXT(p, p_list)) { 521 while (p->p_pid == nextpid || 522 p->p_pgrp->pg_id == nextpid || 523 p->p_session->s_sid == nextpid) { 524 nextpid++; 525 if (nextpid >= pidchecked) 526 goto retry; 527 } 528 if (p->p_pid > nextpid && pidchecked > p->p_pid) 529 pidchecked = p->p_pid; 530 if (p->p_pgrp->pg_id > nextpid && 531 pidchecked > p->p_pgrp->pg_id) 532 pidchecked = p->p_pgrp->pg_id; 533 if (p->p_session->s_sid > nextpid && 534 pidchecked > p->p_session->s_sid) 535 pidchecked = p->p_session->s_sid; 536 } 537 if (!doingzomb) { 538 doingzomb = 1; 539 p = LIST_FIRST(&zombproc); 540 goto again; 541 } 542 } 543 return(nextpid); 544 } 545 546 /* 547 * Called from exit1 to remove a process from the allproc 548 * list and move it to the zombie list. 549 * 550 * No requirements. 551 */ 552 void 553 proc_move_allproc_zombie(struct proc *p) 554 { 555 lwkt_gettoken(&proc_token); 556 while (p->p_lock) { 557 tsleep(p, 0, "reap1", hz / 10); 558 } 559 LIST_REMOVE(p, p_list); 560 LIST_INSERT_HEAD(&zombproc, p, p_list); 561 LIST_REMOVE(p, p_hash); 562 p->p_stat = SZOMB; 563 lwkt_reltoken(&proc_token); 564 dsched_exit_proc(p); 565 } 566 567 /* 568 * This routine is called from kern_wait() and will remove the process 569 * from the zombie list and the sibling list. This routine will block 570 * if someone has a lock on the proces (p_lock). 571 * 572 * No requirements. 573 */ 574 void 575 proc_remove_zombie(struct proc *p) 576 { 577 lwkt_gettoken(&proc_token); 578 while (p->p_lock) { 579 tsleep(p, 0, "reap1", hz / 10); 580 } 581 LIST_REMOVE(p, p_list); /* off zombproc */ 582 LIST_REMOVE(p, p_sibling); 583 lwkt_reltoken(&proc_token); 584 } 585 586 /* 587 * Scan all processes on the allproc list. The process is automatically 588 * held for the callback. A return value of -1 terminates the loop. 589 * 590 * No requirements. 591 * The callback is made with the process held and proc_token held. 592 */ 593 void 594 allproc_scan(int (*callback)(struct proc *, void *), void *data) 595 { 596 struct proc *p; 597 int r; 598 599 lwkt_gettoken(&proc_token); 600 LIST_FOREACH(p, &allproc, p_list) { 601 PHOLD(p); 602 r = callback(p, data); 603 PRELE(p); 604 if (r < 0) 605 break; 606 } 607 lwkt_reltoken(&proc_token); 608 } 609 610 /* 611 * Scan all lwps of processes on the allproc list. The lwp is automatically 612 * held for the callback. A return value of -1 terminates the loop. 613 * 614 * No requirements. 615 * The callback is made with the proces and lwp both held, and proc_token held. 616 */ 617 void 618 alllwp_scan(int (*callback)(struct lwp *, void *), void *data) 619 { 620 struct proc *p; 621 struct lwp *lp; 622 int r = 0; 623 624 lwkt_gettoken(&proc_token); 625 LIST_FOREACH(p, &allproc, p_list) { 626 PHOLD(p); 627 FOREACH_LWP_IN_PROC(lp, p) { 628 LWPHOLD(lp); 629 r = callback(lp, data); 630 LWPRELE(lp); 631 } 632 PRELE(p); 633 if (r < 0) 634 break; 635 } 636 lwkt_reltoken(&proc_token); 637 } 638 639 /* 640 * Scan all processes on the zombproc list. The process is automatically 641 * held for the callback. A return value of -1 terminates the loop. 642 * 643 * No requirements. 644 * The callback is made with the proces held and proc_token held. 645 */ 646 void 647 zombproc_scan(int (*callback)(struct proc *, void *), void *data) 648 { 649 struct proc *p; 650 int r; 651 652 lwkt_gettoken(&proc_token); 653 LIST_FOREACH(p, &zombproc, p_list) { 654 PHOLD(p); 655 r = callback(p, data); 656 PRELE(p); 657 if (r < 0) 658 break; 659 } 660 lwkt_reltoken(&proc_token); 661 } 662 663 #include "opt_ddb.h" 664 #ifdef DDB 665 #include <ddb/ddb.h> 666 667 /* 668 * Debugging only 669 */ 670 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 671 { 672 struct pgrp *pgrp; 673 struct proc *p; 674 int i; 675 676 for (i = 0; i <= pgrphash; i++) { 677 if (!LIST_EMPTY(&pgrphashtbl[i])) { 678 kprintf("\tindx %d\n", i); 679 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 680 kprintf( 681 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 682 (void *)pgrp, (long)pgrp->pg_id, 683 (void *)pgrp->pg_session, 684 pgrp->pg_session->s_count, 685 (void *)LIST_FIRST(&pgrp->pg_members)); 686 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 687 kprintf("\t\tpid %ld addr %p pgrp %p\n", 688 (long)p->p_pid, (void *)p, 689 (void *)p->p_pgrp); 690 } 691 } 692 } 693 } 694 } 695 #endif /* DDB */ 696 697 /* 698 * Locate a process on the zombie list. Return a process or NULL. 699 * 700 * The caller must hold proc_token if a stable result is desired. 701 * No other requirements. 702 */ 703 struct proc * 704 zpfind(pid_t pid) 705 { 706 struct proc *p; 707 708 lwkt_gettoken(&proc_token); 709 LIST_FOREACH(p, &zombproc, p_list) { 710 if (p->p_pid == pid) { 711 lwkt_reltoken(&proc_token); 712 return (p); 713 } 714 } 715 lwkt_reltoken(&proc_token); 716 return (NULL); 717 } 718 719 /* 720 * The caller must hold proc_token. 721 */ 722 static int 723 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 724 { 725 struct kinfo_proc ki; 726 struct lwp *lp; 727 int skp = 0, had_output = 0; 728 int error; 729 730 bzero(&ki, sizeof(ki)); 731 fill_kinfo_proc(p, &ki); 732 if ((flags & KERN_PROC_FLAG_LWP) == 0) 733 skp = 1; 734 error = 0; 735 FOREACH_LWP_IN_PROC(lp, p) { 736 LWPHOLD(lp); 737 fill_kinfo_lwp(lp, &ki.kp_lwp); 738 had_output = 1; 739 error = SYSCTL_OUT(req, &ki, sizeof(ki)); 740 LWPRELE(lp); 741 if (error) 742 break; 743 if (skp) 744 break; 745 } 746 /* We need to output at least the proc, even if there is no lwp. */ 747 if (had_output == 0) { 748 error = SYSCTL_OUT(req, &ki, sizeof(ki)); 749 } 750 return (error); 751 } 752 753 /* 754 * The caller must hold proc_token. 755 */ 756 static int 757 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags) 758 { 759 struct kinfo_proc ki; 760 int error; 761 762 fill_kinfo_proc_kthread(td, &ki); 763 error = SYSCTL_OUT(req, &ki, sizeof(ki)); 764 if (error) 765 return error; 766 return(0); 767 } 768 769 /* 770 * No requirements. 771 */ 772 static int 773 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 774 { 775 int *name = (int*) arg1; 776 int oid = oidp->oid_number; 777 u_int namelen = arg2; 778 struct proc *p; 779 struct proclist *plist; 780 struct thread *td; 781 int doingzomb, flags = 0; 782 int error = 0; 783 int n; 784 int origcpu; 785 struct ucred *cr1 = curproc->p_ucred; 786 787 flags = oid & KERN_PROC_FLAGMASK; 788 oid &= ~KERN_PROC_FLAGMASK; 789 790 if ((oid == KERN_PROC_ALL && namelen != 0) || 791 (oid != KERN_PROC_ALL && namelen != 1)) 792 return (EINVAL); 793 794 lwkt_gettoken(&proc_token); 795 if (oid == KERN_PROC_PID) { 796 p = pfind((pid_t)name[0]); 797 if (p == NULL) 798 goto post_threads; 799 if (!PRISON_CHECK(cr1, p->p_ucred)) 800 goto post_threads; 801 PHOLD(p); 802 error = sysctl_out_proc(p, req, flags); 803 PRELE(p); 804 goto post_threads; 805 } 806 807 if (!req->oldptr) { 808 /* overestimate by 5 procs */ 809 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 810 if (error) 811 goto post_threads; 812 } 813 for (doingzomb = 0; doingzomb <= 1; doingzomb++) { 814 if (doingzomb) 815 plist = &zombproc; 816 else 817 plist = &allproc; 818 LIST_FOREACH(p, plist, p_list) { 819 /* 820 * Show a user only their processes. 821 */ 822 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred)) 823 continue; 824 /* 825 * Skip embryonic processes. 826 */ 827 if (p->p_stat == SIDL) 828 continue; 829 /* 830 * TODO - make more efficient (see notes below). 831 * do by session. 832 */ 833 switch (oid) { 834 case KERN_PROC_PGRP: 835 /* could do this by traversing pgrp */ 836 if (p->p_pgrp == NULL || 837 p->p_pgrp->pg_id != (pid_t)name[0]) 838 continue; 839 break; 840 841 case KERN_PROC_TTY: 842 if ((p->p_flag & P_CONTROLT) == 0 || 843 p->p_session == NULL || 844 p->p_session->s_ttyp == NULL || 845 dev2udev(p->p_session->s_ttyp->t_dev) != 846 (udev_t)name[0]) 847 continue; 848 break; 849 850 case KERN_PROC_UID: 851 if (p->p_ucred == NULL || 852 p->p_ucred->cr_uid != (uid_t)name[0]) 853 continue; 854 break; 855 856 case KERN_PROC_RUID: 857 if (p->p_ucred == NULL || 858 p->p_ucred->cr_ruid != (uid_t)name[0]) 859 continue; 860 break; 861 } 862 863 if (!PRISON_CHECK(cr1, p->p_ucred)) 864 continue; 865 PHOLD(p); 866 error = sysctl_out_proc(p, req, flags); 867 PRELE(p); 868 if (error) 869 goto post_threads; 870 } 871 } 872 873 /* 874 * Iterate over all active cpus and scan their thread list. Start 875 * with the next logical cpu and end with our original cpu. We 876 * migrate our own thread to each target cpu in order to safely scan 877 * its thread list. In the last loop we migrate back to our original 878 * cpu. 879 */ 880 origcpu = mycpu->gd_cpuid; 881 if (!ps_showallthreads || jailed(cr1)) 882 goto post_threads; 883 884 for (n = 1; n <= ncpus; ++n) { 885 globaldata_t rgd; 886 int nid; 887 888 nid = (origcpu + n) % ncpus; 889 if ((smp_active_mask & CPUMASK(nid)) == 0) 890 continue; 891 rgd = globaldata_find(nid); 892 lwkt_setcpu_self(rgd); 893 894 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) { 895 if (td->td_proc) 896 continue; 897 switch (oid) { 898 case KERN_PROC_PGRP: 899 case KERN_PROC_TTY: 900 case KERN_PROC_UID: 901 case KERN_PROC_RUID: 902 continue; 903 default: 904 break; 905 } 906 lwkt_hold(td); 907 error = sysctl_out_proc_kthread(td, req, doingzomb); 908 lwkt_rele(td); 909 if (error) 910 goto post_threads; 911 } 912 } 913 post_threads: 914 lwkt_reltoken(&proc_token); 915 return (error); 916 } 917 918 /* 919 * This sysctl allows a process to retrieve the argument list or process 920 * title for another process without groping around in the address space 921 * of the other process. It also allow a process to set its own "process 922 * title to a string of its own choice. 923 * 924 * No requirements. 925 */ 926 static int 927 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 928 { 929 int *name = (int*) arg1; 930 u_int namelen = arg2; 931 struct proc *p; 932 struct pargs *pa; 933 int error = 0; 934 struct ucred *cr1 = curproc->p_ucred; 935 936 if (namelen != 1) 937 return (EINVAL); 938 939 lwkt_gettoken(&proc_token); 940 p = pfind((pid_t)name[0]); 941 if (p == NULL) 942 goto done; 943 944 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred)) 945 goto done; 946 947 if (req->newptr && curproc != p) { 948 error = EPERM; 949 goto done; 950 } 951 952 PHOLD(p); 953 if (req->oldptr && p->p_args != NULL) { 954 error = SYSCTL_OUT(req, p->p_args->ar_args, 955 p->p_args->ar_length); 956 } 957 if (req->newptr == NULL) { 958 PRELE(p); 959 goto done; 960 } 961 962 if (p->p_args && --p->p_args->ar_ref == 0) 963 FREE(p->p_args, M_PARGS); 964 p->p_args = NULL; 965 966 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) { 967 PRELE(p); 968 goto done; 969 } 970 971 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, 972 M_PARGS, M_WAITOK); 973 pa->ar_ref = 1; 974 pa->ar_length = req->newlen; 975 error = SYSCTL_IN(req, pa->ar_args, req->newlen); 976 if (!error) 977 p->p_args = pa; 978 else 979 FREE(pa, M_PARGS); 980 PRELE(p); 981 done: 982 lwkt_reltoken(&proc_token); 983 return (error); 984 } 985 986 static int 987 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS) 988 { 989 int *name = (int*) arg1; 990 u_int namelen = arg2; 991 struct proc *p; 992 int error = 0; 993 char *fullpath, *freepath; 994 struct ucred *cr1 = curproc->p_ucred; 995 996 if (namelen != 1) 997 return (EINVAL); 998 999 lwkt_gettoken(&proc_token); 1000 p = pfind((pid_t)name[0]); 1001 if (p == NULL) 1002 goto done; 1003 1004 /* 1005 * If we are not allowed to see other args, we certainly shouldn't 1006 * get the cwd either. Also check the usual trespassing. 1007 */ 1008 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred)) 1009 goto done; 1010 1011 PHOLD(p); 1012 if (req->oldptr && p->p_fd != NULL) { 1013 error = cache_fullpath(p, &p->p_fd->fd_ncdir, 1014 &fullpath, &freepath, 0); 1015 if (error) 1016 goto done; 1017 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1); 1018 kfree(freepath, M_TEMP); 1019 } 1020 1021 PRELE(p); 1022 1023 done: 1024 lwkt_reltoken(&proc_token); 1025 return (error); 1026 } 1027 1028 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 1029 1030 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 1031 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 1032 1033 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 1034 sysctl_kern_proc, "Process table"); 1035 1036 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 1037 sysctl_kern_proc, "Process table"); 1038 1039 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 1040 sysctl_kern_proc, "Process table"); 1041 1042 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 1043 sysctl_kern_proc, "Process table"); 1044 1045 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1046 sysctl_kern_proc, "Process table"); 1047 1048 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD, 1049 sysctl_kern_proc, "Process table"); 1050 1051 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD, 1052 sysctl_kern_proc, "Process table"); 1053 1054 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD, 1055 sysctl_kern_proc, "Process table"); 1056 1057 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD, 1058 sysctl_kern_proc, "Process table"); 1059 1060 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD, 1061 sysctl_kern_proc, "Process table"); 1062 1063 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD, 1064 sysctl_kern_proc, "Process table"); 1065 1066 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 1067 sysctl_kern_proc_args, "Process argument list"); 1068 1069 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY, 1070 sysctl_kern_proc_cwd, "Process argument list"); 1071