1 /* $OpenBSD: kern_proc.c,v 1.70 2016/09/15 02:00:16 dlg 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 processlist allprocess; 64 struct processlist zombprocess; 65 struct proclist allproc; 66 67 struct pool proc_pool; 68 struct pool process_pool; 69 struct pool rusage_pool; 70 struct pool ucred_pool; 71 struct pool pgrp_pool; 72 struct pool session_pool; 73 74 static void orphanpg(struct pgrp *); 75 #ifdef DEBUG 76 void pgrpdump(void); 77 #endif 78 79 /* 80 * Initialize global process hashing structures. 81 */ 82 void 83 procinit(void) 84 { 85 LIST_INIT(&allprocess); 86 LIST_INIT(&zombprocess); 87 LIST_INIT(&allproc); 88 89 90 pidhashtbl = hashinit(maxthread / 4, M_PROC, M_NOWAIT, &pidhash); 91 pgrphashtbl = hashinit(maxprocess / 4, M_PROC, M_NOWAIT, &pgrphash); 92 uihashtbl = hashinit(maxprocess / 16, M_PROC, M_NOWAIT, &uihash); 93 if (!pidhashtbl || !pgrphashtbl || !uihashtbl) 94 panic("procinit: malloc"); 95 96 pool_init(&proc_pool, sizeof(struct proc), 0, IPL_NONE, 97 PR_WAITOK, "procpl", NULL); 98 pool_init(&process_pool, sizeof(struct process), 0, IPL_NONE, 99 PR_WAITOK, "processpl", NULL); 100 pool_init(&rusage_pool, sizeof(struct rusage), 0, IPL_NONE, 101 PR_WAITOK, "zombiepl", NULL); 102 pool_init(&ucred_pool, sizeof(struct ucred), 0, IPL_NONE, 103 PR_WAITOK, "ucredpl", NULL); 104 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, IPL_NONE, 105 PR_WAITOK, "pgrppl", NULL); 106 pool_init(&session_pool, sizeof(struct session), 0, IPL_NONE, 107 PR_WAITOK, "sessionpl", NULL); 108 } 109 110 struct uidinfo * 111 uid_find(uid_t uid) 112 { 113 struct uidinfo *uip, *nuip; 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 nuip = malloc(sizeof(*nuip), M_PROC, M_WAITOK|M_ZERO); 123 LIST_FOREACH(uip, uipp, ui_hash) 124 if (uip->ui_uid == uid) 125 break; 126 if (uip) { 127 free(nuip, M_PROC, sizeof(*nuip)); 128 return (uip); 129 } 130 nuip->ui_uid = uid; 131 LIST_INSERT_HEAD(uipp, nuip, ui_hash); 132 133 return (nuip); 134 } 135 136 /* 137 * Change the count associated with number of threads 138 * a given user is using. 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 pr an inferior of parent? 154 */ 155 int 156 inferior(struct process *pr, struct process *parent) 157 { 158 159 for (; pr != parent; pr = pr->ps_pptr) 160 if (pr->ps_pid == 0 || pr->ps_pid == 1) 161 return (0); 162 return (1); 163 } 164 165 /* 166 * Locate a proc (thread) 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 by number 181 */ 182 struct process * 183 prfind(pid_t pid) 184 { 185 struct proc *p; 186 187 LIST_FOREACH(p, PIDHASH(pid), p_hash) 188 if (p->p_pid == pid) 189 return (p->p_flag & P_THREAD ? NULL : p->p_p); 190 return (NULL); 191 } 192 193 /* 194 * Locate a process group by number 195 */ 196 struct pgrp * 197 pgfind(pid_t pgid) 198 { 199 struct pgrp *pgrp; 200 201 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) 202 if (pgrp->pg_id == pgid) 203 return (pgrp); 204 return (NULL); 205 } 206 207 /* 208 * Locate a zombie process 209 */ 210 struct process * 211 zombiefind(pid_t pid) 212 { 213 struct process *pr; 214 215 LIST_FOREACH(pr, &zombprocess, ps_list) 216 if (pr->ps_mainproc->p_pid == pid) 217 return (pr); 218 return (NULL); 219 } 220 221 /* 222 * Move p to a new or existing process group (and session) 223 * Caller provides a pre-allocated pgrp and session that should 224 * be freed if they are not used. 225 * XXX need proctree lock 226 */ 227 int 228 enterpgrp(struct process *pr, pid_t pgid, struct pgrp *newpgrp, 229 struct session *newsess) 230 { 231 struct pgrp *pgrp = pgfind(pgid); 232 233 #ifdef DIAGNOSTIC 234 if (pgrp != NULL && newsess) /* firewalls */ 235 panic("enterpgrp: setsid into non-empty pgrp"); 236 if (SESS_LEADER(pr)) 237 panic("enterpgrp: session leader attempted setpgrp"); 238 #endif 239 if (pgrp == NULL) { 240 /* 241 * new process group 242 */ 243 #ifdef DIAGNOSTIC 244 if (pr->ps_pid != pgid) 245 panic("enterpgrp: new pgrp and pid != pgid"); 246 #endif 247 248 pgrp = newpgrp; 249 if (newsess) { 250 /* 251 * new session 252 */ 253 newsess->s_leader = pr; 254 newsess->s_count = 1; 255 newsess->s_ttyvp = NULL; 256 newsess->s_ttyp = NULL; 257 memcpy(newsess->s_login, pr->ps_session->s_login, 258 sizeof(newsess->s_login)); 259 atomic_clearbits_int(&pr->ps_flags, PS_CONTROLT); 260 pgrp->pg_session = newsess; 261 #ifdef DIAGNOSTIC 262 if (pr != curproc->p_p) 263 panic("enterpgrp: mksession but not curproc"); 264 #endif 265 } else { 266 pgrp->pg_session = pr->ps_session; 267 pgrp->pg_session->s_count++; 268 } 269 pgrp->pg_id = pgid; 270 LIST_INIT(&pgrp->pg_members); 271 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 272 pgrp->pg_jobc = 0; 273 } else if (pgrp == pr->ps_pgrp) { 274 if (newsess) 275 pool_put(&session_pool, newsess); 276 pool_put(&pgrp_pool, newpgrp); 277 return (0); 278 } else { 279 if (newsess) 280 pool_put(&session_pool, newsess); 281 pool_put(&pgrp_pool, newpgrp); 282 } 283 284 /* 285 * Adjust eligibility of affected pgrps to participate in job control. 286 * Increment eligibility counts before decrementing, otherwise we 287 * could reach 0 spuriously during the first call. 288 */ 289 fixjobc(pr, pgrp, 1); 290 fixjobc(pr, pr->ps_pgrp, 0); 291 292 LIST_REMOVE(pr, ps_pglist); 293 if (LIST_EMPTY(&pr->ps_pgrp->pg_members)) 294 pgdelete(pr->ps_pgrp); 295 pr->ps_pgrp = pgrp; 296 LIST_INSERT_HEAD(&pgrp->pg_members, pr, ps_pglist); 297 return (0); 298 } 299 300 /* 301 * remove process from process group 302 */ 303 void 304 leavepgrp(struct process *pr) 305 { 306 307 if (pr->ps_session->s_verauthppid == pr->ps_pid) 308 zapverauth(pr->ps_session); 309 LIST_REMOVE(pr, ps_pglist); 310 if (LIST_EMPTY(&pr->ps_pgrp->pg_members)) 311 pgdelete(pr->ps_pgrp); 312 pr->ps_pgrp = 0; 313 } 314 315 /* 316 * delete a process group 317 */ 318 void 319 pgdelete(struct pgrp *pgrp) 320 { 321 322 if (pgrp->pg_session->s_ttyp != NULL && 323 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 324 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 325 LIST_REMOVE(pgrp, pg_hash); 326 SESSRELE(pgrp->pg_session); 327 pool_put(&pgrp_pool, pgrp); 328 } 329 330 void 331 zapverauth(void *v) 332 { 333 struct session *sess = v; 334 sess->s_verauthuid = 0; 335 sess->s_verauthppid = 0; 336 } 337 338 /* 339 * Adjust pgrp jobc counters when specified process changes process group. 340 * We count the number of processes in each process group that "qualify" 341 * the group for terminal job control (those with a parent in a different 342 * process group of the same session). If that count reaches zero, the 343 * process group becomes orphaned. Check both the specified process' 344 * process group and that of its children. 345 * entering == 0 => pr is leaving specified group. 346 * entering == 1 => pr is entering specified group. 347 * XXX need proctree lock 348 */ 349 void 350 fixjobc(struct process *pr, struct pgrp *pgrp, int entering) 351 { 352 struct pgrp *hispgrp; 353 struct session *mysession = pgrp->pg_session; 354 355 /* 356 * Check pr's parent to see whether pr qualifies its own process 357 * group; if so, adjust count for pr's process group. 358 */ 359 if ((hispgrp = pr->ps_pptr->ps_pgrp) != pgrp && 360 hispgrp->pg_session == mysession) { 361 if (entering) 362 pgrp->pg_jobc++; 363 else if (--pgrp->pg_jobc == 0) 364 orphanpg(pgrp); 365 } 366 367 /* 368 * Check this process' children to see whether they qualify 369 * their process groups; if so, adjust counts for children's 370 * process groups. 371 */ 372 LIST_FOREACH(pr, &pr->ps_children, ps_sibling) 373 if ((hispgrp = pr->ps_pgrp) != pgrp && 374 hispgrp->pg_session == mysession && 375 (pr->ps_flags & PS_ZOMBIE) == 0) { 376 if (entering) 377 hispgrp->pg_jobc++; 378 else if (--hispgrp->pg_jobc == 0) 379 orphanpg(hispgrp); 380 } 381 } 382 383 /* 384 * A process group has become orphaned; 385 * if there are any stopped processes in the group, 386 * hang-up all process in that group. 387 */ 388 static void 389 orphanpg(struct pgrp *pg) 390 { 391 struct process *pr; 392 393 LIST_FOREACH(pr, &pg->pg_members, ps_pglist) { 394 if (pr->ps_mainproc->p_stat == SSTOP) { 395 LIST_FOREACH(pr, &pg->pg_members, ps_pglist) { 396 prsignal(pr, SIGHUP); 397 prsignal(pr, SIGCONT); 398 } 399 return; 400 } 401 } 402 } 403 404 #ifdef DDB 405 void 406 proc_printit(struct proc *p, const char *modif, 407 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 408 { 409 static const char *const pstat[] = { 410 "idle", "run", "sleep", "stop", "zombie", "dead", "onproc" 411 }; 412 char pstbuf[5]; 413 const char *pst = pstbuf; 414 415 416 if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0])) 417 snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat); 418 else 419 pst = pstat[(int)p->p_stat - 1]; 420 421 (*pr)("PROC (%s) pid=%d stat=%s\n", p->p_comm, p->p_pid, pst); 422 (*pr)(" flags process=%b proc=%b\n", 423 p->p_p->ps_flags, PS_BITS, p->p_flag, P_BITS); 424 (*pr)(" pri=%u, usrpri=%u, nice=%d\n", 425 p->p_priority, p->p_usrpri, p->p_p->ps_nice); 426 (*pr)(" forw=%p, list=%p,%p\n", 427 TAILQ_NEXT(p, p_runq), p->p_list.le_next, p->p_list.le_prev); 428 (*pr)(" process=%p user=%p, vmspace=%p\n", 429 p->p_p, p->p_addr, p->p_vmspace); 430 (*pr)(" estcpu=%u, cpticks=%d, pctcpu=%u.%u\n", 431 p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100); 432 (*pr)(" user=%u, sys=%u, intr=%u\n", 433 p->p_uticks, p->p_sticks, p->p_iticks); 434 } 435 #include <machine/db_machdep.h> 436 437 #include <ddb/db_output.h> 438 439 void 440 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) 441 { 442 char *mode; 443 int skipzomb = 0; 444 struct proc *p; 445 struct process *pr, *ppr; 446 447 if (modif[0] == 0) 448 modif[0] = 'n'; /* default == normal mode */ 449 450 mode = "mawno"; 451 while (*mode && *mode != modif[0]) 452 mode++; 453 if (*mode == 0 || *mode == 'm') { 454 db_printf("usage: show all procs [/a] [/n] [/w]\n"); 455 db_printf("\t/a == show process address info\n"); 456 db_printf("\t/n == show normal process info [default]\n"); 457 db_printf("\t/w == show process wait/emul info\n"); 458 db_printf("\t/o == show normal info for non-idle SONPROC\n"); 459 return; 460 } 461 462 pr = LIST_FIRST(&allprocess); 463 464 switch (*mode) { 465 466 case 'a': 467 db_printf(" TID %-10s %18s %18s %18s\n", 468 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP"); 469 break; 470 case 'n': 471 db_printf(" TID %5s %5s %5s S %10s %-12s %-16s\n", 472 "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND"); 473 break; 474 case 'w': 475 db_printf(" TID %-16s %-8s %18s %s\n", 476 "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG"); 477 break; 478 case 'o': 479 skipzomb = 1; 480 db_printf(" TID %5s %5s %10s %10s %3s %-31s\n", 481 "PID", "UID", "PRFLAGS", "PFLAGS", "CPU", "COMMAND"); 482 break; 483 } 484 485 while (pr != NULL) { 486 ppr = pr->ps_pptr; 487 488 TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { 489 if (p->p_stat) { 490 if (*mode == 'o') { 491 if (p->p_stat != SONPROC) 492 continue; 493 if (p->p_cpu != NULL && p->p_cpu-> 494 ci_schedstate.spc_idleproc == p) 495 continue; 496 } 497 db_printf("%c%5d ", p == curproc ? '*' : ' ', 498 p->p_pid); 499 500 switch (*mode) { 501 502 case 'a': 503 db_printf("%-10.10s %18p %18p %18p\n", 504 p->p_comm, p, p->p_addr, p->p_vmspace); 505 break; 506 507 case 'n': 508 db_printf("%5d %5d %5d %d %#10x " 509 "%-12.12s %-16s\n", 510 ppr ? ppr->ps_pid : -1, 511 pr->ps_pgrp ? pr->ps_pgrp->pg_id : -1, 512 pr->ps_ucred->cr_ruid, p->p_stat, 513 p->p_flag | pr->ps_flags, 514 (p->p_wchan && p->p_wmesg) ? 515 p->p_wmesg : "", p->p_comm); 516 break; 517 518 case 'w': 519 db_printf("%-16s %-8s %18p %s\n", p->p_comm, 520 pr->ps_emul->e_name, p->p_wchan, 521 (p->p_wchan && p->p_wmesg) ? 522 p->p_wmesg : ""); 523 break; 524 525 case 'o': 526 db_printf("%5d %5d %#10x %#10x %3d" 527 " %-31s\n", 528 pr->ps_pid, pr->ps_ucred->cr_ruid, 529 pr->ps_flags, p->p_flag, 530 CPU_INFO_UNIT(p->p_cpu), 531 p->p_comm); 532 break; 533 534 } 535 } 536 } 537 pr = LIST_NEXT(pr, ps_list); 538 if (pr == NULL && skipzomb == 0) { 539 skipzomb = 1; 540 pr = LIST_FIRST(&zombprocess); 541 } 542 } 543 } 544 #endif 545 546 #ifdef DEBUG 547 void 548 pgrpdump(void) 549 { 550 struct pgrp *pgrp; 551 struct process *pr; 552 int i; 553 554 for (i = 0; i <= pgrphash; i++) { 555 if (!LIST_EMPTY(&pgrphashtbl[i])) { 556 printf("\tindx %d\n", i); 557 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 558 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 559 pgrp, pgrp->pg_id, pgrp->pg_session, 560 pgrp->pg_session->s_count, 561 LIST_FIRST(&pgrp->pg_members)); 562 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 563 printf("\t\tpid %d addr %p pgrp %p\n", 564 pr->ps_pid, pr, pr->ps_pgrp); 565 } 566 } 567 } 568 } 569 } 570 #endif /* DEBUG */ 571