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/proc.h> 42 #include <sys/malloc.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 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next) 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 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next) 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 KASSERT(uip->ui_sbsize >= 0, ("ui_sbsize (%d) < 0", uid)); 163 if (uip->ui_sbsize == 0 && uip->ui_proccnt == 0) { 164 LIST_REMOVE(uip, ui_hash); 165 FREE(uip, M_PROC); 166 return (0); 167 } 168 return (uip->ui_sbsize); 169 } 170 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 171 LIST_INSERT_HEAD(uipp, uip, ui_hash); 172 uip->ui_uid = uid; 173 uip->ui_proccnt = 0; 174 uip->ui_sbsize = diff; 175 return (diff); 176 } 177 178 /* 179 * Is p an inferior of the current process? 180 */ 181 int 182 inferior(p) 183 register struct proc *p; 184 { 185 186 for (; p != curproc; p = p->p_pptr) 187 if (p->p_pid == 0) 188 return (0); 189 return (1); 190 } 191 192 /* 193 * Locate a process by number 194 */ 195 struct proc * 196 pfind(pid) 197 register pid_t pid; 198 { 199 register struct proc *p; 200 201 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next) 202 if (p->p_pid == pid) 203 return (p); 204 return (NULL); 205 } 206 207 /* 208 * Locate a process group by number 209 */ 210 struct pgrp * 211 pgfind(pgid) 212 register pid_t pgid; 213 { 214 register struct pgrp *pgrp; 215 216 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; 217 pgrp = pgrp->pg_hash.le_next) 218 if (pgrp->pg_id == pgid) 219 return (pgrp); 220 return (NULL); 221 } 222 223 /* 224 * Move p to a new or existing process group (and session) 225 */ 226 int 227 enterpgrp(p, pgid, mksess) 228 register struct proc *p; 229 pid_t pgid; 230 int mksess; 231 { 232 register struct pgrp *pgrp = pgfind(pgid); 233 234 KASSERT(pgrp == NULL || !mksess, 235 ("enterpgrp: setsid into non-empty pgrp")); 236 KASSERT(!SESS_LEADER(p), 237 ("enterpgrp: session leader attempted setpgrp")); 238 239 if (pgrp == NULL) { 240 pid_t savepid = p->p_pid; 241 struct proc *np; 242 /* 243 * new process group 244 */ 245 KASSERT(p->p_pid == pgid, 246 ("enterpgrp: new pgrp and pid != pgid")); 247 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 248 M_WAITOK); 249 if ((np = pfind(savepid)) == NULL || np != p) 250 return (ESRCH); 251 if (mksess) { 252 register struct session *sess; 253 254 /* 255 * new session 256 */ 257 MALLOC(sess, struct session *, sizeof(struct session), 258 M_SESSION, M_WAITOK); 259 sess->s_leader = p; 260 sess->s_sid = p->p_pid; 261 sess->s_count = 1; 262 sess->s_ttyvp = NULL; 263 sess->s_ttyp = NULL; 264 bcopy(p->p_session->s_login, sess->s_login, 265 sizeof(sess->s_login)); 266 p->p_flag &= ~P_CONTROLT; 267 pgrp->pg_session = sess; 268 KASSERT(p == curproc, 269 ("enterpgrp: mksession and p != curproc")); 270 } else { 271 pgrp->pg_session = p->p_session; 272 pgrp->pg_session->s_count++; 273 } 274 pgrp->pg_id = pgid; 275 LIST_INIT(&pgrp->pg_members); 276 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 277 pgrp->pg_jobc = 0; 278 SLIST_INIT(&pgrp->pg_sigiolst); 279 } else if (pgrp == p->p_pgrp) 280 return (0); 281 282 /* 283 * Adjust eligibility of affected pgrps to participate in job control. 284 * Increment eligibility counts before decrementing, otherwise we 285 * could reach 0 spuriously during the first call. 286 */ 287 fixjobc(p, pgrp, 1); 288 fixjobc(p, p->p_pgrp, 0); 289 290 LIST_REMOVE(p, p_pglist); 291 if (p->p_pgrp->pg_members.lh_first == 0) 292 pgdelete(p->p_pgrp); 293 p->p_pgrp = pgrp; 294 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 295 return (0); 296 } 297 298 /* 299 * remove process from process group 300 */ 301 int 302 leavepgrp(p) 303 register struct proc *p; 304 { 305 306 LIST_REMOVE(p, p_pglist); 307 if (p->p_pgrp->pg_members.lh_first == 0) 308 pgdelete(p->p_pgrp); 309 p->p_pgrp = 0; 310 return (0); 311 } 312 313 /* 314 * delete a process group 315 */ 316 static void 317 pgdelete(pgrp) 318 register struct pgrp *pgrp; 319 { 320 321 /* 322 * Reset any sigio structures pointing to us as a result of 323 * F_SETOWN with our pgid. 324 */ 325 funsetownlst(&pgrp->pg_sigiolst); 326 327 if (pgrp->pg_session->s_ttyp != NULL && 328 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 329 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 330 LIST_REMOVE(pgrp, pg_hash); 331 if (--pgrp->pg_session->s_count == 0) 332 FREE(pgrp->pg_session, M_SESSION); 333 FREE(pgrp, M_PGRP); 334 } 335 336 /* 337 * Adjust pgrp jobc counters when specified process changes process group. 338 * We count the number of processes in each process group that "qualify" 339 * the group for terminal job control (those with a parent in a different 340 * process group of the same session). If that count reaches zero, the 341 * process group becomes orphaned. Check both the specified process' 342 * process group and that of its children. 343 * entering == 0 => p is leaving specified group. 344 * entering == 1 => p is entering specified group. 345 */ 346 void 347 fixjobc(p, pgrp, entering) 348 register struct proc *p; 349 register struct pgrp *pgrp; 350 int entering; 351 { 352 register struct pgrp *hispgrp; 353 register struct session *mysession = pgrp->pg_session; 354 355 /* 356 * Check p's parent to see whether p qualifies its own process 357 * group; if so, adjust count for p's process group. 358 */ 359 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 360 hispgrp->pg_session == mysession) { 361 if (entering) 362 pgrp->pg_jobc++; 363 else if (--pgrp->pg_jobc == 0) 364 orphanpg(pgrp); 365 } 366 367 /* 368 * Check this process' children to see whether they qualify 369 * their process groups; if so, adjust counts for children's 370 * process groups. 371 */ 372 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) 373 if ((hispgrp = p->p_pgrp) != pgrp && 374 hispgrp->pg_session == mysession && 375 p->p_stat != SZOMB) { 376 if (entering) 377 hispgrp->pg_jobc++; 378 else if (--hispgrp->pg_jobc == 0) 379 orphanpg(hispgrp); 380 } 381 } 382 383 /* 384 * A process group has become orphaned; 385 * if there are any stopped processes in the group, 386 * hang-up all process in that group. 387 */ 388 static void 389 orphanpg(pg) 390 struct pgrp *pg; 391 { 392 register struct proc *p; 393 394 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 395 if (p->p_stat == SSTOP) { 396 for (p = pg->pg_members.lh_first; p != 0; 397 p = p->p_pglist.le_next) { 398 psignal(p, SIGHUP); 399 psignal(p, SIGCONT); 400 } 401 return; 402 } 403 } 404 } 405 406 #include "opt_ddb.h" 407 #ifdef DDB 408 #include <ddb/ddb.h> 409 410 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 411 { 412 register struct pgrp *pgrp; 413 register struct proc *p; 414 register int i; 415 416 for (i = 0; i <= pgrphash; i++) { 417 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) { 418 printf("\tindx %d\n", i); 419 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) { 420 printf( 421 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 422 (void *)pgrp, (long)pgrp->pg_id, 423 (void *)pgrp->pg_session, 424 pgrp->pg_session->s_count, 425 (void *)pgrp->pg_members.lh_first); 426 for (p = pgrp->pg_members.lh_first; p != 0; 427 p = p->p_pglist.le_next) { 428 printf("\t\tpid %ld addr %p pgrp %p\n", 429 (long)p->p_pid, (void *)p, 430 (void *)p->p_pgrp); 431 } 432 } 433 } 434 } 435 } 436 #endif /* DDB */ 437 438 /* 439 * Fill in an eproc structure for the specified process. 440 */ 441 void 442 fill_eproc(p, ep) 443 register struct proc *p; 444 register struct eproc *ep; 445 { 446 register struct tty *tp; 447 448 bzero(ep, sizeof(*ep)); 449 450 ep->e_paddr = p; 451 if (p->p_cred) { 452 ep->e_pcred = *p->p_cred; 453 if (p->p_ucred) 454 ep->e_ucred = *p->p_ucred; 455 } 456 if (p->p_procsig){ 457 ep->e_procsig = *p->p_procsig; 458 } 459 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 460 register struct vmspace *vm = p->p_vmspace; 461 ep->e_vm = *vm; 462 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/ 463 } 464 if (p->p_pptr) 465 ep->e_ppid = p->p_pptr->p_pid; 466 if (p->p_pgrp) { 467 ep->e_pgid = p->p_pgrp->pg_id; 468 ep->e_jobc = p->p_pgrp->pg_jobc; 469 ep->e_sess = p->p_pgrp->pg_session; 470 471 if (ep->e_sess) { 472 bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login)); 473 if (ep->e_sess->s_ttyvp) 474 ep->e_flag = EPROC_CTTY; 475 if (p->p_session && SESS_LEADER(p)) 476 ep->e_flag |= EPROC_SLEADER; 477 } 478 } 479 if ((p->p_flag & P_CONTROLT) && 480 (ep->e_sess != NULL) && 481 ((tp = ep->e_sess->s_ttyp) != NULL)) { 482 ep->e_tdev = dev2udev(tp->t_dev); 483 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 484 ep->e_tsess = tp->t_session; 485 } else 486 ep->e_tdev = NOUDEV; 487 if (p->p_wmesg) { 488 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 489 ep->e_wmesg[WMESGLEN] = 0; 490 } 491 } 492 493 static struct proc * 494 zpfind(pid_t pid) 495 { 496 struct proc *p; 497 498 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next) 499 if (p->p_pid == pid) 500 return (p); 501 return (NULL); 502 } 503 504 505 static int 506 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 507 { 508 struct eproc eproc; 509 int error; 510 pid_t pid = p->p_pid; 511 512 fill_eproc(p, &eproc); 513 error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc)); 514 if (error) 515 return (error); 516 error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); 517 if (error) 518 return (error); 519 if (!doingzomb && pid && (pfind(pid) != p)) 520 return EAGAIN; 521 if (doingzomb && zpfind(pid) != p) 522 return EAGAIN; 523 return (0); 524 } 525 526 static int 527 sysctl_kern_proc SYSCTL_HANDLER_ARGS 528 { 529 int *name = (int*) arg1; 530 u_int namelen = arg2; 531 struct proc *p; 532 int doingzomb; 533 int error = 0; 534 535 if (oidp->oid_number == KERN_PROC_PID) { 536 if (namelen != 1) 537 return (EINVAL); 538 p = pfind((pid_t)name[0]); 539 if (!p) 540 return (0); 541 if (!PRISON_CHECK(curproc, p)) 542 return (0); 543 error = sysctl_out_proc(p, req, 0); 544 return (error); 545 } 546 if (oidp->oid_number == KERN_PROC_ALL && !namelen) 547 ; 548 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 549 ; 550 else 551 return (EINVAL); 552 553 if (!req->oldptr) { 554 /* overestimate by 5 procs */ 555 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 556 if (error) 557 return (error); 558 } 559 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 560 if (!doingzomb) 561 p = allproc.lh_first; 562 else 563 p = zombproc.lh_first; 564 for (; p != 0; p = p->p_list.le_next) { 565 /* 566 * Skip embryonic processes. 567 */ 568 if (p->p_stat == SIDL) 569 continue; 570 /* 571 * TODO - make more efficient (see notes below). 572 * do by session. 573 */ 574 switch (oidp->oid_number) { 575 576 case KERN_PROC_PGRP: 577 /* could do this by traversing pgrp */ 578 if (p->p_pgrp == NULL || 579 p->p_pgrp->pg_id != (pid_t)name[0]) 580 continue; 581 break; 582 583 case KERN_PROC_TTY: 584 if ((p->p_flag & P_CONTROLT) == 0 || 585 p->p_session == NULL || 586 p->p_session->s_ttyp == NULL || 587 dev2udev(p->p_session->s_ttyp->t_dev) != 588 (udev_t)name[0]) 589 continue; 590 break; 591 592 case KERN_PROC_UID: 593 if (p->p_ucred == NULL || 594 p->p_ucred->cr_uid != (uid_t)name[0]) 595 continue; 596 break; 597 598 case KERN_PROC_RUID: 599 if (p->p_ucred == NULL || 600 p->p_cred->p_ruid != (uid_t)name[0]) 601 continue; 602 break; 603 } 604 605 if (!PRISON_CHECK(curproc, p)) 606 continue; 607 608 error = sysctl_out_proc(p, req, doingzomb); 609 if (error) 610 return (error); 611 } 612 } 613 return (0); 614 } 615 616 617 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 618 619 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 620 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 621 622 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 623 sysctl_kern_proc, "Process table"); 624 625 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 626 sysctl_kern_proc, "Process table"); 627 628 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 629 sysctl_kern_proc, "Process table"); 630 631 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 632 sysctl_kern_proc, "Process table"); 633 634 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 635 sysctl_kern_proc, "Process table"); 636