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/sysctl.h> 41 #include <sys/malloc.h> 42 #include <sys/proc.h> 43 #include <sys/filedesc.h> 44 #include <sys/tty.h> 45 #include <sys/signalvar.h> 46 #include <vm/vm.h> 47 #include <sys/lock.h> 48 #include <vm/pmap.h> 49 #include <vm/vm_map.h> 50 #include <sys/user.h> 51 #include <vm/vm_zone.h> 52 53 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 54 MALLOC_DEFINE(M_SESSION, "session", "session header"); 55 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 56 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 57 58 static void pgdelete __P((struct pgrp *)); 59 60 /* 61 * Structure associated with user cacheing. 62 */ 63 struct uidinfo { 64 LIST_ENTRY(uidinfo) ui_hash; 65 uid_t ui_uid; 66 long ui_proccnt; 67 rlim_t ui_sbsize; 68 }; 69 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 70 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 71 static u_long uihash; /* size of hash table - 1 */ 72 73 static void orphanpg __P((struct pgrp *pg)); 74 75 /* 76 * Other process lists 77 */ 78 struct pidhashhead *pidhashtbl; 79 u_long pidhash; 80 struct pgrphashhead *pgrphashtbl; 81 u_long pgrphash; 82 struct proclist allproc; 83 struct proclist zombproc; 84 vm_zone_t proc_zone; 85 86 /* 87 * Initialize global process hashing structures. 88 */ 89 void 90 procinit() 91 { 92 93 LIST_INIT(&allproc); 94 LIST_INIT(&zombproc); 95 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 96 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 97 uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash); 98 proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5); 99 } 100 101 /* 102 * Change the count associated with number of processes 103 * a given user is using. 104 */ 105 int 106 chgproccnt(uid, diff) 107 uid_t uid; 108 int diff; 109 { 110 register struct uidinfo *uip; 111 register struct uihashhead *uipp; 112 113 uipp = UIHASH(uid); 114 LIST_FOREACH(uip, uipp, ui_hash) 115 if (uip->ui_uid == uid) 116 break; 117 if (uip) { 118 uip->ui_proccnt += diff; 119 if (uip->ui_proccnt < 0) 120 panic("chgproccnt: procs < 0"); 121 if (uip->ui_proccnt > 0 || uip->ui_sbsize > 0) 122 return (uip->ui_proccnt); 123 LIST_REMOVE(uip, ui_hash); 124 FREE(uip, M_PROC); 125 return (0); 126 } 127 if (diff <= 0) { 128 if (diff == 0) 129 return(0); 130 panic("chgproccnt: lost user"); 131 } 132 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 133 LIST_INSERT_HEAD(uipp, uip, ui_hash); 134 uip->ui_uid = uid; 135 uip->ui_proccnt = diff; 136 uip->ui_sbsize = 0; 137 return (diff); 138 } 139 140 /* 141 * Change the total socket buffer size a user has used. 142 */ 143 rlim_t 144 chgsbsize(uid, diff) 145 uid_t uid; 146 rlim_t diff; 147 { 148 register struct uidinfo *uip; 149 register struct uihashhead *uipp; 150 151 uipp = UIHASH(uid); 152 LIST_FOREACH(uip, uipp, ui_hash) 153 if (uip->ui_uid == uid) 154 break; 155 if (diff <= 0) { 156 if (diff == 0) 157 return (uip ? uip->ui_sbsize : 0); 158 KASSERT(uip != NULL, ("uidinfo (%d) gone", uid)); 159 } 160 if (uip) { 161 uip->ui_sbsize += diff; 162 if (uip->ui_sbsize == 0 && uip->ui_proccnt == 0) { 163 LIST_REMOVE(uip, ui_hash); 164 FREE(uip, M_PROC); 165 return (0); 166 } 167 return (uip->ui_sbsize); 168 } 169 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 170 LIST_INSERT_HEAD(uipp, uip, ui_hash); 171 uip->ui_uid = uid; 172 uip->ui_proccnt = 0; 173 uip->ui_sbsize = diff; 174 return (diff); 175 } 176 177 /* 178 * Is p an inferior of the current process? 179 */ 180 int 181 inferior(p) 182 register struct proc *p; 183 { 184 185 for (; p != curproc; p = p->p_pptr) 186 if (p->p_pid == 0) 187 return (0); 188 return (1); 189 } 190 191 /* 192 * Locate a process by number 193 */ 194 struct proc * 195 pfind(pid) 196 register pid_t pid; 197 { 198 register struct proc *p; 199 200 LIST_FOREACH(p, PIDHASH(pid), p_hash) 201 if (p->p_pid == pid) 202 return (p); 203 return (NULL); 204 } 205 206 /* 207 * Locate a process group by number 208 */ 209 struct pgrp * 210 pgfind(pgid) 211 register pid_t pgid; 212 { 213 register struct pgrp *pgrp; 214 215 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) 216 if (pgrp->pg_id == pgid) 217 return (pgrp); 218 return (NULL); 219 } 220 221 /* 222 * Move p to a new or existing process group (and session) 223 */ 224 int 225 enterpgrp(p, pgid, mksess) 226 register struct proc *p; 227 pid_t pgid; 228 int mksess; 229 { 230 register struct pgrp *pgrp = pgfind(pgid); 231 232 KASSERT(pgrp == NULL || !mksess, 233 ("enterpgrp: setsid into non-empty pgrp")); 234 KASSERT(!SESS_LEADER(p), 235 ("enterpgrp: session leader attempted setpgrp")); 236 237 if (pgrp == NULL) { 238 pid_t savepid = p->p_pid; 239 struct proc *np; 240 /* 241 * new process group 242 */ 243 KASSERT(p->p_pid == pgid, 244 ("enterpgrp: new pgrp and pid != pgid")); 245 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 246 M_WAITOK); 247 if ((np = pfind(savepid)) == NULL || np != p) 248 return (ESRCH); 249 if (mksess) { 250 register struct session *sess; 251 252 /* 253 * new session 254 */ 255 MALLOC(sess, struct session *, sizeof(struct session), 256 M_SESSION, M_WAITOK); 257 sess->s_leader = p; 258 sess->s_sid = p->p_pid; 259 sess->s_count = 1; 260 sess->s_ttyvp = NULL; 261 sess->s_ttyp = NULL; 262 bcopy(p->p_session->s_login, sess->s_login, 263 sizeof(sess->s_login)); 264 p->p_flag &= ~P_CONTROLT; 265 pgrp->pg_session = sess; 266 KASSERT(p == curproc, 267 ("enterpgrp: mksession and p != curproc")); 268 } else { 269 pgrp->pg_session = p->p_session; 270 pgrp->pg_session->s_count++; 271 } 272 pgrp->pg_id = pgid; 273 LIST_INIT(&pgrp->pg_members); 274 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 275 pgrp->pg_jobc = 0; 276 SLIST_INIT(&pgrp->pg_sigiolst); 277 } else if (pgrp == p->p_pgrp) 278 return (0); 279 280 /* 281 * Adjust eligibility of affected pgrps to participate in job control. 282 * Increment eligibility counts before decrementing, otherwise we 283 * could reach 0 spuriously during the first call. 284 */ 285 fixjobc(p, pgrp, 1); 286 fixjobc(p, p->p_pgrp, 0); 287 288 LIST_REMOVE(p, p_pglist); 289 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 290 pgdelete(p->p_pgrp); 291 p->p_pgrp = pgrp; 292 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 293 return (0); 294 } 295 296 /* 297 * remove process from process group 298 */ 299 int 300 leavepgrp(p) 301 register struct proc *p; 302 { 303 304 LIST_REMOVE(p, p_pglist); 305 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 306 pgdelete(p->p_pgrp); 307 p->p_pgrp = 0; 308 return (0); 309 } 310 311 /* 312 * delete a process group 313 */ 314 static void 315 pgdelete(pgrp) 316 register struct pgrp *pgrp; 317 { 318 319 /* 320 * Reset any sigio structures pointing to us as a result of 321 * F_SETOWN with our pgid. 322 */ 323 funsetownlst(&pgrp->pg_sigiolst); 324 325 if (pgrp->pg_session->s_ttyp != NULL && 326 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 327 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 328 LIST_REMOVE(pgrp, pg_hash); 329 if (--pgrp->pg_session->s_count == 0) 330 FREE(pgrp->pg_session, M_SESSION); 331 FREE(pgrp, M_PGRP); 332 } 333 334 /* 335 * Adjust pgrp jobc counters when specified process changes process group. 336 * We count the number of processes in each process group that "qualify" 337 * the group for terminal job control (those with a parent in a different 338 * process group of the same session). If that count reaches zero, the 339 * process group becomes orphaned. Check both the specified process' 340 * process group and that of its children. 341 * entering == 0 => p is leaving specified group. 342 * entering == 1 => p is entering specified group. 343 */ 344 void 345 fixjobc(p, pgrp, entering) 346 register struct proc *p; 347 register struct pgrp *pgrp; 348 int entering; 349 { 350 register struct pgrp *hispgrp; 351 register struct session *mysession = pgrp->pg_session; 352 353 /* 354 * Check p's parent to see whether p qualifies its own process 355 * group; if so, adjust count for p's process group. 356 */ 357 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 358 hispgrp->pg_session == mysession) { 359 if (entering) 360 pgrp->pg_jobc++; 361 else if (--pgrp->pg_jobc == 0) 362 orphanpg(pgrp); 363 } 364 365 /* 366 * Check this process' children to see whether they qualify 367 * their process groups; if so, adjust counts for children's 368 * process groups. 369 */ 370 LIST_FOREACH(p, &p->p_children, p_sibling) 371 if ((hispgrp = p->p_pgrp) != pgrp && 372 hispgrp->pg_session == mysession && 373 p->p_stat != SZOMB) { 374 if (entering) 375 hispgrp->pg_jobc++; 376 else if (--hispgrp->pg_jobc == 0) 377 orphanpg(hispgrp); 378 } 379 } 380 381 /* 382 * A process group has become orphaned; 383 * if there are any stopped processes in the group, 384 * hang-up all process in that group. 385 */ 386 static void 387 orphanpg(pg) 388 struct pgrp *pg; 389 { 390 register struct proc *p; 391 392 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 393 if (p->p_stat == SSTOP) { 394 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 395 psignal(p, SIGHUP); 396 psignal(p, SIGCONT); 397 } 398 return; 399 } 400 } 401 } 402 403 #include "opt_ddb.h" 404 #ifdef DDB 405 #include <ddb/ddb.h> 406 407 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 408 { 409 register struct pgrp *pgrp; 410 register struct proc *p; 411 register int i; 412 413 for (i = 0; i <= pgrphash; i++) { 414 if (!LIST_EMPTY(&pgrphashtbl[i])) { 415 printf("\tindx %d\n", i); 416 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 417 printf( 418 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 419 (void *)pgrp, (long)pgrp->pg_id, 420 (void *)pgrp->pg_session, 421 pgrp->pg_session->s_count, 422 (void *)LIST_FIRST(&pgrp->pg_members)); 423 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 424 printf("\t\tpid %ld addr %p pgrp %p\n", 425 (long)p->p_pid, (void *)p, 426 (void *)p->p_pgrp); 427 } 428 } 429 } 430 } 431 } 432 #endif /* DDB */ 433 434 /* 435 * Fill in an eproc structure for the specified process. 436 */ 437 void 438 fill_eproc(p, ep) 439 register struct proc *p; 440 register struct eproc *ep; 441 { 442 register struct tty *tp; 443 444 bzero(ep, sizeof(*ep)); 445 446 ep->e_paddr = p; 447 if (p->p_cred) { 448 ep->e_pcred = *p->p_cred; 449 if (p->p_ucred) 450 ep->e_ucred = *p->p_ucred; 451 } 452 if (p->p_procsig) { 453 ep->e_procsig = *p->p_procsig; 454 } 455 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 456 register struct vmspace *vm = p->p_vmspace; 457 ep->e_vm = *vm; 458 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/ 459 } 460 if ((p->p_flag & P_INMEM) && p->p_stats) 461 ep->e_stats = *p->p_stats; 462 if (p->p_pptr) 463 ep->e_ppid = p->p_pptr->p_pid; 464 if (p->p_pgrp) { 465 ep->e_pgid = p->p_pgrp->pg_id; 466 ep->e_jobc = p->p_pgrp->pg_jobc; 467 ep->e_sess = p->p_pgrp->pg_session; 468 469 if (ep->e_sess) { 470 bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login)); 471 if (ep->e_sess->s_ttyvp) 472 ep->e_flag = EPROC_CTTY; 473 if (p->p_session && SESS_LEADER(p)) 474 ep->e_flag |= EPROC_SLEADER; 475 } 476 } 477 if ((p->p_flag & P_CONTROLT) && 478 (ep->e_sess != NULL) && 479 ((tp = ep->e_sess->s_ttyp) != NULL)) { 480 ep->e_tdev = dev2udev(tp->t_dev); 481 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 482 ep->e_tsess = tp->t_session; 483 } else 484 ep->e_tdev = NOUDEV; 485 if (p->p_wmesg) { 486 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 487 ep->e_wmesg[WMESGLEN] = 0; 488 } 489 } 490 491 static struct proc * 492 zpfind(pid_t pid) 493 { 494 struct proc *p; 495 496 LIST_FOREACH(p, &zombproc, p_list) 497 if (p->p_pid == pid) 498 return (p); 499 return (NULL); 500 } 501 502 503 static int 504 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 505 { 506 struct eproc eproc; 507 int error; 508 pid_t pid = p->p_pid; 509 510 fill_eproc(p, &eproc); 511 error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc)); 512 if (error) 513 return (error); 514 error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); 515 if (error) 516 return (error); 517 if (!doingzomb && pid && (pfind(pid) != p)) 518 return EAGAIN; 519 if (doingzomb && zpfind(pid) != p) 520 return EAGAIN; 521 return (0); 522 } 523 524 static int 525 sysctl_kern_proc SYSCTL_HANDLER_ARGS 526 { 527 int *name = (int*) arg1; 528 u_int namelen = arg2; 529 struct proc *p; 530 int doingzomb; 531 int error = 0; 532 533 if (oidp->oid_number == KERN_PROC_PID) { 534 if (namelen != 1) 535 return (EINVAL); 536 p = pfind((pid_t)name[0]); 537 if (!p) 538 return (0); 539 if (!PRISON_CHECK(curproc, p)) 540 return (0); 541 error = sysctl_out_proc(p, req, 0); 542 return (error); 543 } 544 if (oidp->oid_number == KERN_PROC_ALL && !namelen) 545 ; 546 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 547 ; 548 else 549 return (EINVAL); 550 551 if (!req->oldptr) { 552 /* overestimate by 5 procs */ 553 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 554 if (error) 555 return (error); 556 } 557 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 558 if (!doingzomb) 559 p = LIST_FIRST(&allproc); 560 else 561 p = LIST_FIRST(&zombproc); 562 for (; p != 0; p = LIST_NEXT(p, p_list)) { 563 /* 564 * Skip embryonic processes. 565 */ 566 if (p->p_stat == SIDL) 567 continue; 568 /* 569 * TODO - make more efficient (see notes below). 570 * do by session. 571 */ 572 switch (oidp->oid_number) { 573 574 case KERN_PROC_PGRP: 575 /* could do this by traversing pgrp */ 576 if (p->p_pgrp == NULL || 577 p->p_pgrp->pg_id != (pid_t)name[0]) 578 continue; 579 break; 580 581 case KERN_PROC_TTY: 582 if ((p->p_flag & P_CONTROLT) == 0 || 583 p->p_session == NULL || 584 p->p_session->s_ttyp == NULL || 585 dev2udev(p->p_session->s_ttyp->t_dev) != 586 (udev_t)name[0]) 587 continue; 588 break; 589 590 case KERN_PROC_UID: 591 if (p->p_ucred == NULL || 592 p->p_ucred->cr_uid != (uid_t)name[0]) 593 continue; 594 break; 595 596 case KERN_PROC_RUID: 597 if (p->p_ucred == NULL || 598 p->p_cred->p_ruid != (uid_t)name[0]) 599 continue; 600 break; 601 } 602 603 if (!PRISON_CHECK(curproc, p)) 604 continue; 605 606 error = sysctl_out_proc(p, req, doingzomb); 607 if (error) 608 return (error); 609 } 610 } 611 return (0); 612 } 613 614 /* 615 * This sysctl allows a process to retrieve the argument list or process 616 * title for another process without groping around in the address space 617 * of the other process. It also allow a process to set its own "process 618 * title to a string of its own choice. 619 */ 620 static int 621 sysctl_kern_proc_args SYSCTL_HANDLER_ARGS 622 { 623 int *name = (int*) arg1; 624 u_int namelen = arg2; 625 struct proc *p; 626 struct pargs *pa; 627 int error = 0; 628 629 if (namelen != 1) 630 return (EINVAL); 631 632 p = pfind((pid_t)name[0]); 633 if (!p) 634 return (0); 635 636 if ((!ps_argsopen) && p_trespass(curproc, p)) 637 return (0); 638 639 if (req->newptr && curproc != p) 640 return (EPERM); 641 642 if (req->oldptr && p->p_args != NULL) 643 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length); 644 if (req->newptr == NULL) 645 return (error); 646 647 if (p->p_args && --p->p_args->ar_ref == 0) 648 FREE(p->p_args, M_PARGS); 649 p->p_args = NULL; 650 651 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 652 return (error); 653 654 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, 655 M_PARGS, M_WAITOK); 656 pa->ar_ref = 1; 657 pa->ar_length = req->newlen; 658 error = SYSCTL_IN(req, pa->ar_args, req->newlen); 659 if (!error) 660 p->p_args = pa; 661 else 662 FREE(pa, M_PARGS); 663 return (error); 664 } 665 666 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 667 668 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 669 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 670 671 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 672 sysctl_kern_proc, "Process table"); 673 674 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 675 sysctl_kern_proc, "Process table"); 676 677 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 678 sysctl_kern_proc, "Process table"); 679 680 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 681 sysctl_kern_proc, "Process table"); 682 683 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 684 sysctl_kern_proc, "Process table"); 685 686 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW, 687 sysctl_kern_proc_args, "Return process argument"); 688