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