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.4 (Berkeley) 1/4/94 34 * $Id: kern_proc.c,v 1.14 1995/12/14 08:31:30 phk 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/ioctl.h> 51 #include <sys/tty.h> 52 #include <sys/signalvar.h> 53 #include <vm/vm.h> 54 #include <vm/vm_param.h> 55 #include <vm/vm_prot.h> 56 #include <vm/lock.h> 57 #include <vm/pmap.h> 58 #include <vm/vm_map.h> 59 #include <sys/user.h> 60 61 struct prochd qs[NQS]; /* as good a place as any... */ 62 struct prochd rtqs[NQS]; /* Space for REALTIME queues too */ 63 struct prochd idqs[NQS]; /* Space for IDLE queues too */ 64 65 volatile struct proc *allproc; /* all processes */ 66 struct proc *zombproc; /* just zombies */ 67 68 static void pgdelete __P((struct pgrp *)); 69 70 /* 71 * Structure associated with user cacheing. 72 */ 73 static struct uidinfo { 74 struct uidinfo *ui_next; 75 struct uidinfo **ui_prev; 76 uid_t ui_uid; 77 long ui_proccnt; 78 } **uihashtbl; 79 static u_long uihash; /* size of hash table - 1 */ 80 #define UIHASH(uid) ((uid) & uihash) 81 82 static void orphanpg __P((struct pgrp *pg)); 83 84 /* 85 * Allocate a hash table. 86 */ 87 void 88 usrinfoinit() 89 { 90 91 uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash); 92 } 93 94 /* 95 * Change the count associated with number of processes 96 * a given user is using. 97 */ 98 int 99 chgproccnt(uid, diff) 100 uid_t uid; 101 int diff; 102 { 103 register struct uidinfo **uipp, *uip, *uiq; 104 105 uipp = &uihashtbl[UIHASH(uid)]; 106 for (uip = *uipp; uip; uip = uip->ui_next) 107 if (uip->ui_uid == uid) 108 break; 109 if (uip) { 110 uip->ui_proccnt += diff; 111 if (uip->ui_proccnt > 0) 112 return (uip->ui_proccnt); 113 if (uip->ui_proccnt < 0) 114 panic("chgproccnt: procs < 0"); 115 if ((uiq = uip->ui_next)) 116 uiq->ui_prev = uip->ui_prev; 117 *uip->ui_prev = uiq; 118 FREE(uip, M_PROC); 119 return (0); 120 } 121 if (diff <= 0) { 122 if (diff == 0) 123 return(0); 124 panic("chgproccnt: lost user"); 125 } 126 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 127 if ((uiq = *uipp)) 128 uiq->ui_prev = &uip->ui_next; 129 uip->ui_next = uiq; 130 uip->ui_prev = uipp; 131 *uipp = uip; 132 uip->ui_uid = uid; 133 uip->ui_proccnt = diff; 134 return (diff); 135 } 136 137 /* 138 * Is p an inferior of the current process? 139 */ 140 int 141 inferior(p) 142 register struct proc *p; 143 { 144 145 for (; p != curproc; p = p->p_pptr) 146 if (p->p_pid == 0) 147 return (0); 148 return (1); 149 } 150 151 /* 152 * Locate a process by number 153 */ 154 struct proc * 155 pfind(pid) 156 register pid_t pid; 157 { 158 register struct proc *p; 159 160 for (p = pidhash[PIDHASH(pid)]; p != NULL; p = p->p_hash) 161 if (p->p_pid == pid) 162 return (p); 163 return (NULL); 164 } 165 166 /* 167 * Locate a process group by number 168 */ 169 struct pgrp * 170 pgfind(pgid) 171 register pid_t pgid; 172 { 173 register struct pgrp *pgrp; 174 175 for (pgrp = pgrphash[PIDHASH(pgid)]; 176 pgrp != NULL; pgrp = pgrp->pg_hforw) 177 if (pgrp->pg_id == pgid) 178 return (pgrp); 179 return (NULL); 180 } 181 182 /* 183 * Move p to a new or existing process group (and session) 184 */ 185 int 186 enterpgrp(p, pgid, mksess) 187 register struct proc *p; 188 pid_t pgid; 189 int mksess; 190 { 191 register struct pgrp *pgrp = pgfind(pgid); 192 register struct proc **pp; 193 int n; 194 195 #ifdef DIAGNOSTIC 196 if (pgrp != NULL && mksess) /* firewalls */ 197 panic("enterpgrp: setsid into non-empty pgrp"); 198 if (SESS_LEADER(p)) 199 panic("enterpgrp: session leader attempted setpgrp"); 200 #endif 201 if (pgrp == NULL) { 202 pid_t savepid = p->p_pid; 203 struct proc *np; 204 /* 205 * new process group 206 */ 207 #ifdef DIAGNOSTIC 208 if (p->p_pid != pgid) 209 panic("enterpgrp: new pgrp and pid != pgid"); 210 #endif 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_count = 1; 225 sess->s_ttyvp = NULL; 226 sess->s_ttyp = NULL; 227 bcopy(p->p_session->s_login, sess->s_login, 228 sizeof(sess->s_login)); 229 p->p_flag &= ~P_CONTROLT; 230 pgrp->pg_session = sess; 231 #ifdef DIAGNOSTIC 232 if (p != curproc) 233 panic("enterpgrp: mksession and p != curproc"); 234 #endif 235 } else { 236 pgrp->pg_session = p->p_session; 237 pgrp->pg_session->s_count++; 238 } 239 pgrp->pg_id = pgid; 240 pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)]; 241 pgrphash[n] = pgrp; 242 pgrp->pg_jobc = 0; 243 pgrp->pg_mem = NULL; 244 } else if (pgrp == p->p_pgrp) 245 return (0); 246 247 /* 248 * Adjust eligibility of affected pgrps to participate in job control. 249 * Increment eligibility counts before decrementing, otherwise we 250 * could reach 0 spuriously during the first call. 251 */ 252 fixjobc(p, pgrp, 1); 253 fixjobc(p, p->p_pgrp, 0); 254 255 /* 256 * unlink p from old process group 257 */ 258 for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt) { 259 if (*pp == p) { 260 *pp = p->p_pgrpnxt; 261 break; 262 } 263 } 264 #ifdef DIAGNOSTIC 265 if (pp == NULL) 266 panic("enterpgrp: can't find p on old pgrp"); 267 #endif 268 /* 269 * delete old if empty 270 */ 271 if (p->p_pgrp->pg_mem == 0) 272 pgdelete(p->p_pgrp); 273 /* 274 * link into new one 275 */ 276 p->p_pgrp = pgrp; 277 p->p_pgrpnxt = pgrp->pg_mem; 278 pgrp->pg_mem = p; 279 return (0); 280 } 281 282 /* 283 * remove process from process group 284 */ 285 int 286 leavepgrp(p) 287 register struct proc *p; 288 { 289 register struct proc **pp = &p->p_pgrp->pg_mem; 290 291 for (; *pp; pp = &(*pp)->p_pgrpnxt) { 292 if (*pp == p) { 293 *pp = p->p_pgrpnxt; 294 break; 295 } 296 } 297 #ifdef DIAGNOSTIC 298 if (pp == NULL) 299 panic("leavepgrp: can't find p in pgrp"); 300 #endif 301 if (!p->p_pgrp->pg_mem) 302 pgdelete(p->p_pgrp); 303 p->p_pgrp = 0; 304 return (0); 305 } 306 307 /* 308 * delete a process group 309 */ 310 static void 311 pgdelete(pgrp) 312 register struct pgrp *pgrp; 313 { 314 register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)]; 315 316 if (pgrp->pg_session->s_ttyp != NULL && 317 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 318 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 319 for (; *pgp; pgp = &(*pgp)->pg_hforw) { 320 if (*pgp == pgrp) { 321 *pgp = pgrp->pg_hforw; 322 break; 323 } 324 } 325 #ifdef DIAGNOSTIC 326 if (pgp == NULL) 327 panic("pgdelete: can't find pgrp on hash chain"); 328 #endif 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 * Check this process' children to see whether they qualify 366 * their process groups; if so, adjust counts for children's 367 * process groups. 368 */ 369 for (p = p->p_cptr; p; p = p->p_osptr) 370 if ((hispgrp = p->p_pgrp) != pgrp && 371 hispgrp->pg_session == mysession && 372 p->p_stat != SZOMB) 373 if (entering) 374 hispgrp->pg_jobc++; 375 else if (--hispgrp->pg_jobc == 0) 376 orphanpg(hispgrp); 377 } 378 379 /* 380 * A process group has become orphaned; 381 * if there are any stopped processes in the group, 382 * hang-up all process in that group. 383 */ 384 static void 385 orphanpg(pg) 386 struct pgrp *pg; 387 { 388 register struct proc *p; 389 390 for (p = pg->pg_mem; p; p = p->p_pgrpnxt) { 391 if (p->p_stat == SSTOP) { 392 for (p = pg->pg_mem; p; p = p->p_pgrpnxt) { 393 psignal(p, SIGHUP); 394 psignal(p, SIGCONT); 395 } 396 return; 397 } 398 } 399 } 400 401 #ifdef debug 402 /* DEBUG */ 403 pgrpdump() 404 { 405 register struct pgrp *pgrp; 406 register struct proc *p; 407 register i; 408 409 for (i=0; i<PIDHSZ; i++) { 410 if (pgrphash[i]) { 411 printf("\tindx %d\n", i); 412 for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) { 413 printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n", 414 pgrp, pgrp->pg_id, pgrp->pg_session, 415 pgrp->pg_session->s_count, pgrp->pg_mem); 416 for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) { 417 printf("\t\tpid %d addr %x pgrp %x\n", 418 p->p_pid, p, p->p_pgrp); 419 } 420 } 421 422 } 423 } 424 } 425 #endif /* debug */ 426 427 /* 428 * Fill in an eproc structure for the specified process. 429 */ 430 void 431 fill_eproc(p, ep) 432 register struct proc *p; 433 register struct eproc *ep; 434 { 435 register struct tty *tp; 436 437 bzero(ep, sizeof(*ep)); 438 439 ep->e_paddr = p; 440 if (p->p_cred) { 441 ep->e_pcred = *p->p_cred; 442 if (p->p_ucred) 443 ep->e_ucred = *p->p_ucred; 444 } 445 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 446 register struct vmspace *vm = p->p_vmspace; 447 448 #ifdef pmap_resident_count 449 ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/ 450 #else 451 ep->e_vm.vm_rssize = vm->vm_rssize; 452 #endif 453 ep->e_vm.vm_tsize = vm->vm_tsize; 454 ep->e_vm.vm_dsize = vm->vm_dsize; 455 ep->e_vm.vm_ssize = vm->vm_ssize; 456 #ifndef sparc 457 ep->e_vm.vm_pmap = vm->vm_pmap; 458 #endif 459 } 460 if (p->p_pptr) 461 ep->e_ppid = p->p_pptr->p_pid; 462 if (p->p_pgrp) { 463 ep->e_sess = p->p_pgrp->pg_session; 464 ep->e_pgid = p->p_pgrp->pg_id; 465 ep->e_jobc = p->p_pgrp->pg_jobc; 466 } 467 if (ep->e_sess) 468 bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login)); 469 if ((p->p_flag & P_CONTROLT) && 470 (ep->e_sess != NULL) && 471 ((tp = ep->e_sess->s_ttyp) != NULL)) { 472 ep->e_tdev = tp->t_dev; 473 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 474 ep->e_tsess = tp->t_session; 475 } else 476 ep->e_tdev = NODEV; 477 if (ep->e_sess && ep->e_sess->s_ttyvp) 478 ep->e_flag = EPROC_CTTY; 479 if (SESS_LEADER(p)) 480 ep->e_flag |= EPROC_SLEADER; 481 if (p->p_wmesg) { 482 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 483 ep->e_wmesg[WMESGLEN] = 0; 484 } 485 } 486 487 static int 488 sysctl_kern_proc SYSCTL_HANDLER_ARGS 489 { 490 int *name = (int*) arg1; 491 u_int namelen = arg2; 492 struct proc *p; 493 int doingzomb; 494 struct eproc eproc; 495 int error = 0; 496 497 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 498 return (EINVAL); 499 if (!req->oldptr) { 500 /* 501 * try over estimating by 5 procs 502 */ 503 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 504 if (error) 505 return (error); 506 } 507 p = (struct proc *)allproc; 508 doingzomb = 0; 509 again: 510 for (; p != NULL; p = p->p_next) { 511 /* 512 * Skip embryonic processes. 513 */ 514 if (p->p_stat == SIDL) 515 continue; 516 /* 517 * TODO - make more efficient (see notes below). 518 * do by session. 519 */ 520 switch (name[0]) { 521 522 case KERN_PROC_PID: 523 /* could do this with just a lookup */ 524 if (p->p_pid != (pid_t)name[1]) 525 continue; 526 break; 527 528 case KERN_PROC_PGRP: 529 /* could do this by traversing pgrp */ 530 if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[1]) 531 continue; 532 break; 533 534 case KERN_PROC_TTY: 535 if ((p->p_flag & P_CONTROLT) == 0 || 536 p->p_session == NULL || 537 p->p_session->s_ttyp == NULL || 538 p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 539 continue; 540 break; 541 542 case KERN_PROC_UID: 543 if (p->p_ucred == NULL || p->p_ucred->cr_uid != (uid_t)name[1]) 544 continue; 545 break; 546 547 case KERN_PROC_RUID: 548 if (p->p_ucred == NULL || p->p_cred->p_ruid != (uid_t)name[1]) 549 continue; 550 break; 551 } 552 553 fill_eproc(p, &eproc); 554 error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc)); 555 if (error) 556 return (error); 557 error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); 558 if (error) 559 return (error); 560 } 561 if (doingzomb == 0) { 562 p = zombproc; 563 doingzomb++; 564 goto again; 565 } 566 return (0); 567 } 568 569 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 570 sysctl_kern_proc, "Process table"); 571