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