1 /* $OpenBSD: kern_proc.c,v 1.25 2005/03/10 17:26:10 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 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 53 LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 54 u_long uihash; /* size of hash table - 1 */ 55 56 /* 57 * Other process lists 58 */ 59 struct pidhashhead *pidhashtbl; 60 u_long pidhash; 61 struct pgrphashhead *pgrphashtbl; 62 u_long pgrphash; 63 struct proclist allproc; 64 struct proclist zombproc; 65 66 struct pool proc_pool; 67 struct pool rusage_pool; 68 struct pool ucred_pool; 69 struct pool pgrp_pool; 70 struct pool session_pool; 71 struct pool pcred_pool; 72 73 static void orphanpg(struct pgrp *); 74 #ifdef DEBUG 75 void pgrpdump(void); 76 #endif 77 78 /* 79 * Initialize global process hashing structures. 80 */ 81 void 82 procinit() 83 { 84 85 LIST_INIT(&allproc); 86 LIST_INIT(&zombproc); 87 88 pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash); 89 pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash); 90 uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash); 91 92 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl", 93 &pool_allocator_nointr); 94 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl", 95 &pool_allocator_nointr); 96 pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl", 97 &pool_allocator_nointr); 98 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl", 99 &pool_allocator_nointr); 100 pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl", 101 &pool_allocator_nointr); 102 pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl", 103 &pool_allocator_nointr); 104 } 105 106 /* 107 * Change the count associated with number of processes 108 * a given user is using. 109 */ 110 struct uidinfo * 111 uid_find(uid_t uid) 112 { 113 struct uidinfo *uip; 114 struct uihashhead *uipp; 115 116 uipp = UIHASH(uid); 117 LIST_FOREACH(uip, uipp, ui_hash) 118 if (uip->ui_uid == uid) 119 break; 120 if (uip) 121 return (uip); 122 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 123 bzero(uip, sizeof(*uip)); 124 LIST_INSERT_HEAD(uipp, uip, ui_hash); 125 uip->ui_uid = uid; 126 127 return (uip); 128 } 129 130 int 131 chgproccnt(uid_t uid, int diff) 132 { 133 struct uidinfo *uip; 134 135 uip = uid_find(uid); 136 uip->ui_proccnt += diff; 137 if (uip->ui_proccnt < 0) 138 panic("chgproccnt: procs < 0"); 139 return (uip->ui_proccnt); 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 LIST_FOREACH(p, PIDHASH(pid), p_hash) 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 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) 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 if ((np = pfind(savepid)) == NULL || np != p) 214 return (ESRCH); 215 pgrp = pool_get(&pgrp_pool, PR_WAITOK); 216 if (mksess) { 217 register struct session *sess; 218 219 /* 220 * new session 221 */ 222 sess = pool_get(&session_pool, PR_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 LIST_INIT(&pgrp->pg_members); 241 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 242 pgrp->pg_jobc = 0; 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 (LIST_EMPTY(&p->p_pgrp->pg_members)) 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 */ 265 int 266 leavepgrp(p) 267 register struct proc *p; 268 { 269 270 LIST_REMOVE(p, p_pglist); 271 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 272 pgdelete(p->p_pgrp); 273 p->p_pgrp = 0; 274 return (0); 275 } 276 277 /* 278 * delete a process group 279 */ 280 void 281 pgdelete(pgrp) 282 register struct pgrp *pgrp; 283 { 284 285 if (pgrp->pg_session->s_ttyp != NULL && 286 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 287 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 288 LIST_REMOVE(pgrp, pg_hash); 289 SESSRELE(pgrp->pg_session); 290 pool_put(&pgrp_pool, pgrp); 291 } 292 293 /* 294 * Adjust pgrp jobc counters when specified process changes process group. 295 * We count the number of processes in each process group that "qualify" 296 * the group for terminal job control (those with a parent in a different 297 * process group of the same session). If that count reaches zero, the 298 * process group becomes orphaned. Check both the specified process' 299 * process group and that of its children. 300 * entering == 0 => p is leaving specified group. 301 * entering == 1 => p is entering specified group. 302 */ 303 void 304 fixjobc(p, pgrp, entering) 305 register struct proc *p; 306 register struct pgrp *pgrp; 307 int entering; 308 { 309 register struct pgrp *hispgrp; 310 register struct session *mysession = pgrp->pg_session; 311 312 /* 313 * Check p's parent to see whether p qualifies its own process 314 * group; if so, adjust count for p's process group. 315 */ 316 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 317 hispgrp->pg_session == mysession) { 318 if (entering) 319 pgrp->pg_jobc++; 320 else if (--pgrp->pg_jobc == 0) 321 orphanpg(pgrp); 322 } 323 324 /* 325 * Check this process' children to see whether they qualify 326 * their process groups; if so, adjust counts for children's 327 * process groups. 328 */ 329 LIST_FOREACH(p, &p->p_children, p_sibling) 330 if ((hispgrp = p->p_pgrp) != pgrp && 331 hispgrp->pg_session == mysession && 332 P_ZOMBIE(p) == 0) { 333 if (entering) 334 hispgrp->pg_jobc++; 335 else if (--hispgrp->pg_jobc == 0) 336 orphanpg(hispgrp); 337 } 338 } 339 340 /* 341 * A process group has become orphaned; 342 * if there are any stopped processes in the group, 343 * hang-up all process in that group. 344 */ 345 static void 346 orphanpg(pg) 347 struct pgrp *pg; 348 { 349 register struct proc *p; 350 351 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 352 if (p->p_stat == SSTOP) { 353 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 354 psignal(p, SIGHUP); 355 psignal(p, SIGCONT); 356 } 357 return; 358 } 359 } 360 } 361 362 #ifdef DDB 363 void 364 proc_printit(struct proc *p, const char *modif, int (*pr)(const char *, ...)) 365 { 366 static const char *const pstat[] = { 367 "idle", "run", "sleep", "stop", "zombie", "dead", "onproc" 368 }; 369 char pstbuf[5]; 370 const char *pst = pstbuf; 371 372 if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0])) 373 snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat); 374 else 375 pst = pstat[(int)p->p_stat - 1]; 376 377 (*pr)("PROC (%s) pid=%d stat=%s flags=%b\n", 378 p->p_comm, p->p_pid, pst, p->p_flag, P_BITS); 379 (*pr)(" pri=%u, usrpri=%u, nice=%d\n", 380 p->p_priority, p->p_usrpri, p->p_nice); 381 (*pr)(" forw=%p, back=%p, list=%p,%p\n", 382 p->p_forw, p->p_back, p->p_list.le_next, p->p_list.le_prev); 383 (*pr)(" user=%p, vmspace=%p\n", 384 p->p_addr, p->p_vmspace); 385 (*pr)(" estcpu=%u, cpticks=%d, pctcpu=%u.%u%, swtime=%u\n", 386 p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100, 387 p->p_swtime); 388 (*pr)(" user=%llu, sys=%llu, intr=%llu\n", 389 p->p_uticks, p->p_sticks, p->p_iticks); 390 } 391 #include <machine/db_machdep.h> 392 393 #include <ddb/db_interface.h> 394 #include <ddb/db_output.h> 395 396 void 397 db_show_all_procs(addr, haddr, count, modif) 398 db_expr_t addr; 399 int haddr; 400 db_expr_t count; 401 char *modif; 402 { 403 char *mode; 404 int doingzomb = 0; 405 struct proc *p, *pp; 406 407 if (modif[0] == 0) 408 modif[0] = 'n'; /* default == normal mode */ 409 410 mode = "mawn"; 411 while (*mode && *mode != modif[0]) 412 mode++; 413 if (*mode == 0 || *mode == 'm') { 414 db_printf("usage: show all procs [/a] [/n] [/w]\n"); 415 db_printf("\t/a == show process address info\n"); 416 db_printf("\t/n == show normal process info [default]\n"); 417 db_printf("\t/w == show process wait/emul info\n"); 418 return; 419 } 420 421 p = LIST_FIRST(&allproc); 422 423 switch (*mode) { 424 425 case 'a': 426 db_printf(" PID %-10s %18s %18s %18s\n", 427 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP"); 428 break; 429 case 'n': 430 db_printf(" PID %5s %5s %5s S %10s %-9s %-16s\n", 431 "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND"); 432 break; 433 case 'w': 434 db_printf(" PID %-16s %-8s %18s %s\n", 435 "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG"); 436 break; 437 } 438 439 while (p != 0) { 440 pp = p->p_pptr; 441 if (p->p_stat) { 442 443 db_printf("%c%5d ", p == curproc ? '*' : ' ', 444 p->p_pid); 445 446 switch (*mode) { 447 448 case 'a': 449 db_printf("%-10.10s %18p %18p %18p\n", 450 p->p_comm, p, p->p_addr, p->p_vmspace); 451 break; 452 453 case 'n': 454 db_printf("%5d %5d %5d %d %#10x " 455 "%-9.9s %-16s\n", 456 pp ? pp->p_pid : -1, p->p_pgrp->pg_id, 457 p->p_cred->p_ruid, p->p_stat, p->p_flag, 458 (p->p_wchan && p->p_wmesg) ? 459 p->p_wmesg : "", p->p_comm); 460 break; 461 462 case 'w': 463 db_printf("%-16s %-8s %18p %s\n", p->p_comm, 464 p->p_emul->e_name, p->p_wchan, 465 (p->p_wchan && p->p_wmesg) ? 466 p->p_wmesg : ""); 467 break; 468 469 } 470 } 471 p = LIST_NEXT(p, p_list); 472 if (p == 0 && doingzomb == 0) { 473 doingzomb = 1; 474 p = LIST_FIRST(&zombproc); 475 } 476 } 477 } 478 #endif 479 480 #ifdef DEBUG 481 void 482 pgrpdump() 483 { 484 register struct pgrp *pgrp; 485 register struct proc *p; 486 register int i; 487 488 for (i = 0; i <= pgrphash; i++) { 489 if (!LIST_EMPTY(&pgrphashtbl[i])) { 490 printf("\tindx %d\n", i); 491 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 492 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 493 pgrp, pgrp->pg_id, pgrp->pg_session, 494 pgrp->pg_session->s_count, 495 LIST_FIRST(&pgrp->pg_members)); 496 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 497 printf("\t\tpid %d addr %p pgrp %p\n", 498 p->p_pid, p, p->p_pgrp); 499 } 500 } 501 } 502 } 503 } 504 #endif /* DEBUG */ 505