1 /* $OpenBSD: kern_proc.c,v 1.31 2005/12/22 06:55:03 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(void) 83 { 84 LIST_INIT(&allproc); 85 LIST_INIT(&zombproc); 86 87 88 pidhashtbl = hashinit(maxproc / 4, M_PROC, M_NOWAIT, &pidhash); 89 pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_NOWAIT, &pgrphash); 90 uihashtbl = hashinit(maxproc / 16, M_PROC, M_NOWAIT, &uihash); 91 if (!pidhashtbl || !pgrphashtbl || !uihashtbl) 92 panic("procinit: malloc"); 93 94 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl", 95 &pool_allocator_nointr); 96 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl", 97 &pool_allocator_nointr); 98 pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl", 99 &pool_allocator_nointr); 100 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl", 101 &pool_allocator_nointr); 102 pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl", 103 &pool_allocator_nointr); 104 pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl", 105 &pool_allocator_nointr); 106 } 107 108 /* 109 * Change the count associated with number of processes 110 * a given user is using. 111 */ 112 struct uidinfo * 113 uid_find(uid_t uid) 114 { 115 struct uidinfo *uip, *nuip; 116 struct uihashhead *uipp; 117 118 uipp = UIHASH(uid); 119 LIST_FOREACH(uip, uipp, ui_hash) 120 if (uip->ui_uid == uid) 121 break; 122 if (uip) 123 return (uip); 124 MALLOC(nuip, struct uidinfo *, sizeof(*nuip), M_PROC, M_WAITOK); 125 /* may have slept, have to check again */ 126 LIST_FOREACH(uip, uipp, ui_hash) 127 if (uip->ui_uid == uid) 128 break; 129 if (uip) { 130 free(nuip, M_PROC); 131 return (uip); 132 } 133 bzero(nuip, sizeof(*nuip)); 134 nuip->ui_uid = uid; 135 LIST_INSERT_HEAD(uipp, nuip, ui_hash); 136 137 return (nuip); 138 } 139 140 int 141 chgproccnt(uid_t uid, int diff) 142 { 143 struct uidinfo *uip; 144 145 uip = uid_find(uid); 146 uip->ui_proccnt += diff; 147 if (uip->ui_proccnt < 0) 148 panic("chgproccnt: procs < 0"); 149 return (uip->ui_proccnt); 150 } 151 152 /* 153 * Is p an inferior of the current process? 154 */ 155 int 156 inferior(struct proc *p) 157 { 158 159 for (; p != curproc; p = p->p_pptr) 160 if (p->p_pid == 0) 161 return (0); 162 return (1); 163 } 164 165 /* 166 * Locate a process by number 167 */ 168 struct proc * 169 pfind(pid_t pid) 170 { 171 struct proc *p; 172 173 LIST_FOREACH(p, PIDHASH(pid), p_hash) 174 if (p->p_pid == pid) 175 return (p); 176 return (NULL); 177 } 178 179 /* 180 * Locate a process group by number 181 */ 182 struct pgrp * 183 pgfind(pid_t pgid) 184 { 185 struct pgrp *pgrp; 186 187 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) 188 if (pgrp->pg_id == pgid) 189 return (pgrp); 190 return (NULL); 191 } 192 193 /* 194 * Move p to a new or existing process group (and session) 195 */ 196 int 197 enterpgrp(struct proc *p, pid_t pgid, int mksess) 198 { 199 struct pgrp *pgrp = pgfind(pgid); 200 201 #ifdef DIAGNOSTIC 202 if (pgrp != NULL && mksess) /* firewalls */ 203 panic("enterpgrp: setsid into non-empty pgrp"); 204 if (SESS_LEADER(p)) 205 panic("enterpgrp: session leader attempted setpgrp"); 206 #endif 207 if (pgrp == NULL) { 208 pid_t savepid = p->p_pid; 209 struct proc *np; 210 /* 211 * new process group 212 */ 213 #ifdef DIAGNOSTIC 214 if (p->p_pid != pgid) 215 panic("enterpgrp: new pgrp and pid != pgid"); 216 #endif 217 if ((np = pfind(savepid)) == NULL || np != p) 218 return (ESRCH); 219 pgrp = pool_get(&pgrp_pool, PR_WAITOK); 220 if (mksess) { 221 struct session *sess; 222 223 /* 224 * new session 225 */ 226 sess = pool_get(&session_pool, PR_WAITOK); 227 sess->s_leader = p; 228 sess->s_count = 1; 229 sess->s_ttyvp = NULL; 230 sess->s_ttyp = NULL; 231 bcopy(p->p_session->s_login, sess->s_login, 232 sizeof(sess->s_login)); 233 p->p_flag &= ~P_CONTROLT; 234 pgrp->pg_session = sess; 235 #ifdef DIAGNOSTIC 236 if (p != curproc) 237 panic("enterpgrp: mksession and p != curproc"); 238 #endif 239 } else { 240 pgrp->pg_session = p->p_session; 241 pgrp->pg_session->s_count++; 242 } 243 pgrp->pg_id = pgid; 244 LIST_INIT(&pgrp->pg_members); 245 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 246 pgrp->pg_jobc = 0; 247 } else if (pgrp == p->p_pgrp) 248 return (0); 249 250 /* 251 * Adjust eligibility of affected pgrps to participate in job control. 252 * Increment eligibility counts before decrementing, otherwise we 253 * could reach 0 spuriously during the first call. 254 */ 255 fixjobc(p, pgrp, 1); 256 fixjobc(p, p->p_pgrp, 0); 257 258 LIST_REMOVE(p, p_pglist); 259 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 260 pgdelete(p->p_pgrp); 261 p->p_pgrp = pgrp; 262 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 263 return (0); 264 } 265 266 /* 267 * remove process from process group 268 */ 269 int 270 leavepgrp(struct proc *p) 271 { 272 273 LIST_REMOVE(p, p_pglist); 274 if (LIST_EMPTY(&p->p_pgrp->pg_members)) 275 pgdelete(p->p_pgrp); 276 p->p_pgrp = 0; 277 return (0); 278 } 279 280 /* 281 * delete a process group 282 */ 283 void 284 pgdelete(struct pgrp *pgrp) 285 { 286 287 if (pgrp->pg_session->s_ttyp != NULL && 288 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 289 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 290 LIST_REMOVE(pgrp, pg_hash); 291 SESSRELE(pgrp->pg_session); 292 pool_put(&pgrp_pool, pgrp); 293 } 294 295 /* 296 * Adjust pgrp jobc counters when specified process changes process group. 297 * We count the number of processes in each process group that "qualify" 298 * the group for terminal job control (those with a parent in a different 299 * process group of the same session). If that count reaches zero, the 300 * process group becomes orphaned. Check both the specified process' 301 * process group and that of its children. 302 * entering == 0 => p is leaving specified group. 303 * entering == 1 => p is entering specified group. 304 */ 305 void 306 fixjobc(struct proc *p, struct pgrp *pgrp, int entering) 307 { 308 struct pgrp *hispgrp; 309 struct session *mysession = pgrp->pg_session; 310 311 /* 312 * Check p's parent to see whether p qualifies its own process 313 * group; if so, adjust count for p's process group. 314 */ 315 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 316 hispgrp->pg_session == mysession) { 317 if (entering) 318 pgrp->pg_jobc++; 319 else if (--pgrp->pg_jobc == 0) 320 orphanpg(pgrp); 321 } 322 323 /* 324 * Check this process' children to see whether they qualify 325 * their process groups; if so, adjust counts for children's 326 * process groups. 327 */ 328 LIST_FOREACH(p, &p->p_children, p_sibling) 329 if ((hispgrp = p->p_pgrp) != pgrp && 330 hispgrp->pg_session == mysession && 331 P_ZOMBIE(p) == 0) { 332 if (entering) 333 hispgrp->pg_jobc++; 334 else if (--hispgrp->pg_jobc == 0) 335 orphanpg(hispgrp); 336 } 337 } 338 339 /* 340 * A process group has become orphaned; 341 * if there are any stopped processes in the group, 342 * hang-up all process in that group. 343 */ 344 static void 345 orphanpg(struct pgrp *pg) 346 { 347 struct proc *p; 348 349 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 350 if (p->p_stat == SSTOP) { 351 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 352 psignal(p, SIGHUP); 353 psignal(p, SIGCONT); 354 } 355 return; 356 } 357 } 358 } 359 360 #ifdef DDB 361 void 362 proc_printit(struct proc *p, const char *modif, int (*pr)(const char *, ...)) 363 { 364 static const char *const pstat[] = { 365 "idle", "run", "sleep", "stop", "zombie", "dead", "onproc" 366 }; 367 char pstbuf[5]; 368 const char *pst = pstbuf; 369 370 if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0])) 371 snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat); 372 else 373 pst = pstat[(int)p->p_stat - 1]; 374 375 (*pr)("PROC (%s) pid=%d stat=%s flags=%b\n", 376 p->p_comm, p->p_pid, pst, p->p_flag, P_BITS); 377 (*pr)(" pri=%u, usrpri=%u, nice=%d\n", 378 p->p_priority, p->p_usrpri, p->p_nice); 379 (*pr)(" forw=%p, back=%p, list=%p,%p\n", 380 p->p_forw, p->p_back, p->p_list.le_next, p->p_list.le_prev); 381 (*pr)(" user=%p, vmspace=%p\n", 382 p->p_addr, p->p_vmspace); 383 (*pr)(" estcpu=%u, cpticks=%d, pctcpu=%u.%u%, swtime=%u\n", 384 p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100, 385 p->p_swtime); 386 (*pr)(" user=%llu, sys=%llu, intr=%llu\n", 387 p->p_uticks, p->p_sticks, p->p_iticks); 388 } 389 #include <machine/db_machdep.h> 390 391 #include <ddb/db_interface.h> 392 #include <ddb/db_output.h> 393 394 void 395 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) 396 { 397 char *mode; 398 int doingzomb = 0; 399 struct proc *p, *pp; 400 401 if (modif[0] == 0) 402 modif[0] = 'n'; /* default == normal mode */ 403 404 mode = "mawn"; 405 while (*mode && *mode != modif[0]) 406 mode++; 407 if (*mode == 0 || *mode == 'm') { 408 db_printf("usage: show all procs [/a] [/n] [/w]\n"); 409 db_printf("\t/a == show process address info\n"); 410 db_printf("\t/n == show normal process info [default]\n"); 411 db_printf("\t/w == show process wait/emul info\n"); 412 return; 413 } 414 415 p = LIST_FIRST(&allproc); 416 417 switch (*mode) { 418 419 case 'a': 420 db_printf(" PID %-10s %18s %18s %18s\n", 421 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP"); 422 break; 423 case 'n': 424 db_printf(" PID %5s %5s %5s S %10s %-9s %-16s\n", 425 "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND"); 426 break; 427 case 'w': 428 db_printf(" PID %-16s %-8s %18s %s\n", 429 "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG"); 430 break; 431 } 432 433 while (p != 0) { 434 pp = p->p_pptr; 435 if (p->p_stat) { 436 437 db_printf("%c%5d ", p == curproc ? '*' : ' ', 438 p->p_pid); 439 440 switch (*mode) { 441 442 case 'a': 443 db_printf("%-10.10s %18p %18p %18p\n", 444 p->p_comm, p, p->p_addr, p->p_vmspace); 445 break; 446 447 case 'n': 448 db_printf("%5d %5d %5d %d %#10x " 449 "%-9.9s %-16s\n", 450 pp ? pp->p_pid : -1, p->p_pgrp->pg_id, 451 p->p_cred->p_ruid, p->p_stat, p->p_flag, 452 (p->p_wchan && p->p_wmesg) ? 453 p->p_wmesg : "", p->p_comm); 454 break; 455 456 case 'w': 457 db_printf("%-16s %-8s %18p %s\n", p->p_comm, 458 p->p_emul->e_name, p->p_wchan, 459 (p->p_wchan && p->p_wmesg) ? 460 p->p_wmesg : ""); 461 break; 462 463 } 464 } 465 p = LIST_NEXT(p, p_list); 466 if (p == 0 && doingzomb == 0) { 467 doingzomb = 1; 468 p = LIST_FIRST(&zombproc); 469 } 470 } 471 } 472 #endif 473 474 #ifdef DEBUG 475 void 476 pgrpdump(void) 477 { 478 struct pgrp *pgrp; 479 struct proc *p; 480 int i; 481 482 for (i = 0; i <= pgrphash; i++) { 483 if (!LIST_EMPTY(&pgrphashtbl[i])) { 484 printf("\tindx %d\n", i); 485 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 486 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 487 pgrp, pgrp->pg_id, pgrp->pg_session, 488 pgrp->pg_session->s_count, 489 LIST_FIRST(&pgrp->pg_members)); 490 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 491 printf("\t\tpid %d addr %p pgrp %p\n", 492 p->p_pid, p, p->p_pgrp); 493 } 494 } 495 } 496 } 497 } 498 #endif /* DEBUG */ 499