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