1 /* $OpenBSD: kern_proc.c,v 1.21 2004/07/25 20:50:51 tedu Exp $ */ 2 /* $NetBSD: kern_proc.c,v 1.14 1996/02/09 18:59:41 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/proc.h> 39 #include <sys/buf.h> 40 #include <sys/acct.h> 41 #include <sys/wait.h> 42 #include <sys/file.h> 43 #include <ufs/ufs/quota.h> 44 #include <sys/uio.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/ioctl.h> 48 #include <sys/tty.h> 49 #include <sys/signalvar.h> 50 #include <sys/pool.h> 51 52 /* 53 * Structure associated with user cacheing. 54 */ 55 struct uidinfo { 56 LIST_ENTRY(uidinfo) ui_hash; 57 uid_t ui_uid; 58 long ui_proccnt; 59 }; 60 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 61 LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 62 u_long uihash; /* size of hash table - 1 */ 63 64 /* 65 * Other process lists 66 */ 67 struct pidhashhead *pidhashtbl; 68 u_long pidhash; 69 struct pgrphashhead *pgrphashtbl; 70 u_long pgrphash; 71 struct proclist allproc; 72 struct proclist zombproc; 73 74 struct pool proc_pool; 75 struct pool rusage_pool; 76 struct pool ucred_pool; 77 struct pool pgrp_pool; 78 struct pool session_pool; 79 struct pool pcred_pool; 80 81 static void orphanpg(struct pgrp *); 82 #ifdef DEBUG 83 void pgrpdump(void); 84 #endif 85 86 /* 87 * Initialize global process hashing structures. 88 */ 89 void 90 procinit() 91 { 92 93 LIST_INIT(&allproc); 94 LIST_INIT(&zombproc); 95 96 pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash); 97 pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash); 98 uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash); 99 100 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl", 101 &pool_allocator_nointr); 102 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl", 103 &pool_allocator_nointr); 104 pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl", 105 &pool_allocator_nointr); 106 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl", 107 &pool_allocator_nointr); 108 pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl", 109 &pool_allocator_nointr); 110 pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl", 111 &pool_allocator_nointr); 112 } 113 114 /* 115 * Change the count associated with number of processes 116 * a given user is using. 117 */ 118 int 119 chgproccnt(uid, diff) 120 uid_t uid; 121 int diff; 122 { 123 register struct uidinfo *uip; 124 register struct uihashhead *uipp; 125 126 uipp = UIHASH(uid); 127 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next) 128 if (uip->ui_uid == uid) 129 break; 130 if (uip) { 131 uip->ui_proccnt += diff; 132 if (uip->ui_proccnt > 0) 133 return (uip->ui_proccnt); 134 if (uip->ui_proccnt < 0) 135 panic("chgproccnt: procs < 0"); 136 LIST_REMOVE(uip, ui_hash); 137 FREE(uip, M_PROC); 138 return (0); 139 } 140 if (diff <= 0) { 141 if (diff == 0) 142 return(0); 143 panic("chgproccnt: lost user"); 144 } 145 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 146 LIST_INSERT_HEAD(uipp, uip, ui_hash); 147 uip->ui_uid = uid; 148 uip->ui_proccnt = diff; 149 return (diff); 150 } 151 152 /* 153 * Is p an inferior of the current process? 154 */ 155 int 156 inferior(p) 157 register struct proc *p; 158 { 159 160 for (; p != curproc; p = p->p_pptr) 161 if (p->p_pid == 0) 162 return (0); 163 return (1); 164 } 165 166 /* 167 * Locate a process by number 168 */ 169 struct proc * 170 pfind(pid) 171 register pid_t pid; 172 { 173 register struct proc *p; 174 175 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next) 176 if (p->p_pid == pid) 177 return (p); 178 return (NULL); 179 } 180 181 /* 182 * Locate a process group by number 183 */ 184 struct pgrp * 185 pgfind(pgid) 186 register pid_t pgid; 187 { 188 register struct pgrp *pgrp; 189 190 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next) 191 if (pgrp->pg_id == pgid) 192 return (pgrp); 193 return (NULL); 194 } 195 196 /* 197 * Move p to a new or existing process group (and session) 198 */ 199 int 200 enterpgrp(p, pgid, mksess) 201 register struct proc *p; 202 pid_t pgid; 203 int mksess; 204 { 205 register struct pgrp *pgrp = pgfind(pgid); 206 207 #ifdef DIAGNOSTIC 208 if (pgrp != NULL && mksess) /* firewalls */ 209 panic("enterpgrp: setsid into non-empty pgrp"); 210 if (SESS_LEADER(p)) 211 panic("enterpgrp: session leader attempted setpgrp"); 212 #endif 213 if (pgrp == NULL) { 214 pid_t savepid = p->p_pid; 215 struct proc *np; 216 /* 217 * new process group 218 */ 219 #ifdef DIAGNOSTIC 220 if (p->p_pid != pgid) 221 panic("enterpgrp: new pgrp and pid != pgid"); 222 #endif 223 if ((np = pfind(savepid)) == NULL || np != p) 224 return (ESRCH); 225 pgrp = pool_get(&pgrp_pool, PR_WAITOK); 226 if (mksess) { 227 register struct session *sess; 228 229 /* 230 * new session 231 */ 232 sess = pool_get(&session_pool, PR_WAITOK); 233 sess->s_leader = p; 234 sess->s_count = 1; 235 sess->s_ttyvp = NULL; 236 sess->s_ttyp = NULL; 237 bcopy(p->p_session->s_login, sess->s_login, 238 sizeof(sess->s_login)); 239 p->p_flag &= ~P_CONTROLT; 240 pgrp->pg_session = sess; 241 #ifdef DIAGNOSTIC 242 if (p != curproc) 243 panic("enterpgrp: mksession and p != curproc"); 244 #endif 245 } else { 246 pgrp->pg_session = p->p_session; 247 pgrp->pg_session->s_count++; 248 } 249 pgrp->pg_id = pgid; 250 LIST_INIT(&pgrp->pg_members); 251 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 252 pgrp->pg_jobc = 0; 253 } else if (pgrp == p->p_pgrp) 254 return (0); 255 256 /* 257 * Adjust eligibility of affected pgrps to participate in job control. 258 * Increment eligibility counts before decrementing, otherwise we 259 * could reach 0 spuriously during the first call. 260 */ 261 fixjobc(p, pgrp, 1); 262 fixjobc(p, p->p_pgrp, 0); 263 264 LIST_REMOVE(p, p_pglist); 265 if (p->p_pgrp->pg_members.lh_first == 0) 266 pgdelete(p->p_pgrp); 267 p->p_pgrp = pgrp; 268 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 269 return (0); 270 } 271 272 /* 273 * remove process from process group 274 */ 275 int 276 leavepgrp(p) 277 register struct proc *p; 278 { 279 280 LIST_REMOVE(p, p_pglist); 281 if (p->p_pgrp->pg_members.lh_first == 0) 282 pgdelete(p->p_pgrp); 283 p->p_pgrp = 0; 284 return (0); 285 } 286 287 /* 288 * delete a process group 289 */ 290 void 291 pgdelete(pgrp) 292 register struct pgrp *pgrp; 293 { 294 295 if (pgrp->pg_session->s_ttyp != NULL && 296 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 297 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 298 LIST_REMOVE(pgrp, pg_hash); 299 SESSRELE(pgrp->pg_session); 300 pool_put(&pgrp_pool, pgrp); 301 } 302 303 /* 304 * Adjust pgrp jobc counters when specified process changes process group. 305 * We count the number of processes in each process group that "qualify" 306 * the group for terminal job control (those with a parent in a different 307 * process group of the same session). If that count reaches zero, the 308 * process group becomes orphaned. Check both the specified process' 309 * process group and that of its children. 310 * entering == 0 => p is leaving specified group. 311 * entering == 1 => p is entering specified group. 312 */ 313 void 314 fixjobc(p, pgrp, entering) 315 register struct proc *p; 316 register struct pgrp *pgrp; 317 int entering; 318 { 319 register struct pgrp *hispgrp; 320 register struct session *mysession = pgrp->pg_session; 321 322 /* 323 * Check p's parent to see whether p qualifies its own process 324 * group; if so, adjust count for p's process group. 325 */ 326 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 327 hispgrp->pg_session == mysession) { 328 if (entering) 329 pgrp->pg_jobc++; 330 else if (--pgrp->pg_jobc == 0) 331 orphanpg(pgrp); 332 } 333 334 /* 335 * Check this process' children to see whether they qualify 336 * their process groups; if so, adjust counts for children's 337 * process groups. 338 */ 339 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) 340 if ((hispgrp = p->p_pgrp) != pgrp && 341 hispgrp->pg_session == mysession && 342 P_ZOMBIE(p) == 0) { 343 if (entering) 344 hispgrp->pg_jobc++; 345 else if (--hispgrp->pg_jobc == 0) 346 orphanpg(hispgrp); 347 } 348 } 349 350 /* 351 * A process group has become orphaned; 352 * if there are any stopped processes in the group, 353 * hang-up all process in that group. 354 */ 355 static void 356 orphanpg(pg) 357 struct pgrp *pg; 358 { 359 register struct proc *p; 360 361 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 362 if (p->p_stat == SSTOP) { 363 for (p = pg->pg_members.lh_first; p != 0; 364 p = p->p_pglist.le_next) { 365 psignal(p, SIGHUP); 366 psignal(p, SIGCONT); 367 } 368 return; 369 } 370 } 371 } 372 373 #ifdef DDB 374 void 375 proc_printit(struct proc *p, const char *modif, int (*pr)(const char *, ...)) 376 { 377 static const char *const pstat[] = { 378 "idle", "run", "sleep", "stop", "zombie", "dead" 379 }; 380 char pstbuf[5]; 381 const char *pst = pstbuf; 382 383 if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0])) 384 snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat); 385 else 386 pst = pstat[(int)p->p_stat - 1]; 387 388 (*pr)("PROC (%s) pid=%d stat=%s flags=%b\n", 389 p->p_comm, p->p_pid, pst, p->p_flag, P_BITS); 390 (*pr)(" pri=%u, usrpri=%u, nice=%d\n", 391 p->p_priority, p->p_usrpri, p->p_nice); 392 (*pr)(" forw=%p, back=%p, list=%p,%p\n", 393 p->p_forw, p->p_back, p->p_list.le_next, p->p_list.le_prev); 394 (*pr)(" user=%p, vmspace=%p\n", 395 p->p_addr, p->p_vmspace); 396 (*pr)(" estcpu=%u, cpticks=%d, pctcpu=%u.%u%, swtime=%u\n", 397 p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100, 398 p->p_swtime); 399 (*pr)(" user=%llu, sys=%llu, intr=%llu\n", 400 p->p_uticks, p->p_sticks, p->p_iticks); 401 } 402 #include <machine/db_machdep.h> 403 404 #include <ddb/db_interface.h> 405 #include <ddb/db_output.h> 406 407 void 408 db_show_all_procs(addr, haddr, count, modif) 409 db_expr_t addr; 410 int haddr; 411 db_expr_t count; 412 char *modif; 413 { 414 char *mode; 415 int doingzomb = 0; 416 struct proc *p, *pp; 417 418 if (modif[0] == 0) 419 modif[0] = 'n'; /* default == normal mode */ 420 421 mode = "mawn"; 422 while (*mode && *mode != modif[0]) 423 mode++; 424 if (*mode == 0 || *mode == 'm') { 425 db_printf("usage: show all procs [/a] [/n] [/w]\n"); 426 db_printf("\t/a == show process address info\n"); 427 db_printf("\t/n == show normal process info [default]\n"); 428 db_printf("\t/w == show process wait/emul info\n"); 429 return; 430 } 431 432 p = LIST_FIRST(&allproc); 433 434 switch (*mode) { 435 436 case 'a': 437 db_printf(" PID %-10s %18s %18s %18s\n", 438 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP"); 439 break; 440 case 'n': 441 db_printf(" PID %5s %5s %5s S %10s %-9s %-16s\n", 442 "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND"); 443 break; 444 case 'w': 445 db_printf(" PID %-16s %-8s %18s %s\n", 446 "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG"); 447 break; 448 } 449 450 while (p != 0) { 451 pp = p->p_pptr; 452 if (p->p_stat) { 453 454 db_printf("%c%5d ", p == curproc ? '*' : ' ', 455 p->p_pid); 456 457 switch (*mode) { 458 459 case 'a': 460 db_printf("%-10.10s %18p %18p %18p\n", 461 p->p_comm, p, p->p_addr, p->p_vmspace); 462 break; 463 464 case 'n': 465 db_printf("%5d %5d %5d %d %#10x " 466 "%-9.9s %-16s\n", 467 pp ? pp->p_pid : -1, p->p_pgrp->pg_id, 468 p->p_cred->p_ruid, p->p_stat, p->p_flag, 469 (p->p_wchan && p->p_wmesg) ? 470 p->p_wmesg : "", p->p_comm); 471 break; 472 473 case 'w': 474 db_printf("%-16s %-8s %18p %s\n", p->p_comm, 475 p->p_emul->e_name, p->p_wchan, 476 (p->p_wchan && p->p_wmesg) ? 477 p->p_wmesg : ""); 478 break; 479 480 } 481 } 482 p = LIST_NEXT(p, p_list); 483 if (p == 0 && doingzomb == 0) { 484 doingzomb = 1; 485 p = LIST_FIRST(&zombproc); 486 } 487 } 488 } 489 #endif 490 491 #ifdef DEBUG 492 void 493 pgrpdump() 494 { 495 register struct pgrp *pgrp; 496 register struct proc *p; 497 register int i; 498 499 for (i = 0; i <= pgrphash; i++) { 500 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) { 501 printf("\tindx %d\n", i); 502 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) { 503 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 504 pgrp, pgrp->pg_id, pgrp->pg_session, 505 pgrp->pg_session->s_count, 506 pgrp->pg_members.lh_first); 507 for (p = pgrp->pg_members.lh_first; p != 0; 508 p = p->p_pglist.le_next) { 509 printf("\t\tpid %d addr %p pgrp %p\n", 510 p->p_pid, p, p->p_pgrp); 511 } 512 } 513 } 514 } 515 } 516 #endif /* DEBUG */ 517