1 /* $OpenBSD: kern_proc.c,v 1.61 2014/11/03 03:08:00 deraadt 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, 0, 0, "procpl", 97 &pool_allocator_nointr); 98 pool_init(&process_pool, sizeof(struct process), 0, 0, 0, "processpl", 99 &pool_allocator_nointr); 100 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl", 101 &pool_allocator_nointr); 102 pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl", 103 &pool_allocator_nointr); 104 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl", 105 &pool_allocator_nointr); 106 pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl", 107 &pool_allocator_nointr); 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 p 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 * Move p to a new or existing process group (and session) 209 * Caller provides a pre-allocated pgrp and session that should 210 * be freed if they are not used. 211 * XXX need proctree lock 212 */ 213 int 214 enterpgrp(struct process *pr, pid_t pgid, struct pgrp *newpgrp, 215 struct session *newsess) 216 { 217 struct pgrp *pgrp = pgfind(pgid); 218 219 #ifdef DIAGNOSTIC 220 if (pgrp != NULL && newsess) /* firewalls */ 221 panic("enterpgrp: setsid into non-empty pgrp"); 222 if (SESS_LEADER(pr)) 223 panic("enterpgrp: session leader attempted setpgrp"); 224 #endif 225 if (pgrp == NULL) { 226 /* 227 * new process group 228 */ 229 #ifdef DIAGNOSTIC 230 if (pr->ps_pid != pgid) 231 panic("enterpgrp: new pgrp and pid != pgid"); 232 #endif 233 234 pgrp = newpgrp; 235 if (newsess) { 236 /* 237 * new session 238 */ 239 newsess->s_leader = pr; 240 newsess->s_count = 1; 241 newsess->s_ttyvp = NULL; 242 newsess->s_ttyp = NULL; 243 bcopy(pr->ps_session->s_login, newsess->s_login, 244 sizeof(newsess->s_login)); 245 atomic_clearbits_int(&pr->ps_flags, PS_CONTROLT); 246 pgrp->pg_session = newsess; 247 #ifdef DIAGNOSTIC 248 if (pr != curproc->p_p) 249 panic("enterpgrp: mksession but not curproc"); 250 #endif 251 } else { 252 pgrp->pg_session = pr->ps_session; 253 pgrp->pg_session->s_count++; 254 } 255 pgrp->pg_id = pgid; 256 LIST_INIT(&pgrp->pg_members); 257 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 258 pgrp->pg_jobc = 0; 259 } else if (pgrp == pr->ps_pgrp) { 260 if (newsess) 261 pool_put(&session_pool, newsess); 262 pool_put(&pgrp_pool, newpgrp); 263 return (0); 264 } else { 265 if (newsess) 266 pool_put(&session_pool, newsess); 267 pool_put(&pgrp_pool, newpgrp); 268 } 269 270 /* 271 * Adjust eligibility of affected pgrps to participate in job control. 272 * Increment eligibility counts before decrementing, otherwise we 273 * could reach 0 spuriously during the first call. 274 */ 275 fixjobc(pr, pgrp, 1); 276 fixjobc(pr, pr->ps_pgrp, 0); 277 278 LIST_REMOVE(pr, ps_pglist); 279 if (LIST_EMPTY(&pr->ps_pgrp->pg_members)) 280 pgdelete(pr->ps_pgrp); 281 pr->ps_pgrp = pgrp; 282 LIST_INSERT_HEAD(&pgrp->pg_members, pr, ps_pglist); 283 return (0); 284 } 285 286 /* 287 * remove process from process group 288 */ 289 void 290 leavepgrp(struct process *pr) 291 { 292 293 LIST_REMOVE(pr, ps_pglist); 294 if (LIST_EMPTY(&pr->ps_pgrp->pg_members)) 295 pgdelete(pr->ps_pgrp); 296 pr->ps_pgrp = 0; 297 } 298 299 /* 300 * delete a process group 301 */ 302 void 303 pgdelete(struct pgrp *pgrp) 304 { 305 306 if (pgrp->pg_session->s_ttyp != NULL && 307 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 308 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 309 LIST_REMOVE(pgrp, pg_hash); 310 SESSRELE(pgrp->pg_session); 311 pool_put(&pgrp_pool, pgrp); 312 } 313 314 /* 315 * Adjust pgrp jobc counters when specified process changes process group. 316 * We count the number of processes in each process group that "qualify" 317 * the group for terminal job control (those with a parent in a different 318 * process group of the same session). If that count reaches zero, the 319 * process group becomes orphaned. Check both the specified process' 320 * process group and that of its children. 321 * entering == 0 => pr is leaving specified group. 322 * entering == 1 => pr is entering specified group. 323 * XXX need proctree lock 324 */ 325 void 326 fixjobc(struct process *pr, struct pgrp *pgrp, int entering) 327 { 328 struct pgrp *hispgrp; 329 struct session *mysession = pgrp->pg_session; 330 331 /* 332 * Check pr's parent to see whether pr qualifies its own process 333 * group; if so, adjust count for pr's process group. 334 */ 335 if ((hispgrp = pr->ps_pptr->ps_pgrp) != pgrp && 336 hispgrp->pg_session == mysession) { 337 if (entering) 338 pgrp->pg_jobc++; 339 else if (--pgrp->pg_jobc == 0) 340 orphanpg(pgrp); 341 } 342 343 /* 344 * Check this process' children to see whether they qualify 345 * their process groups; if so, adjust counts for children's 346 * process groups. 347 */ 348 LIST_FOREACH(pr, &pr->ps_children, ps_sibling) 349 if ((hispgrp = pr->ps_pgrp) != pgrp && 350 hispgrp->pg_session == mysession && 351 (pr->ps_flags & PS_ZOMBIE) == 0) { 352 if (entering) 353 hispgrp->pg_jobc++; 354 else if (--hispgrp->pg_jobc == 0) 355 orphanpg(hispgrp); 356 } 357 } 358 359 /* 360 * A process group has become orphaned; 361 * if there are any stopped processes in the group, 362 * hang-up all process in that group. 363 */ 364 static void 365 orphanpg(struct pgrp *pg) 366 { 367 struct process *pr; 368 369 LIST_FOREACH(pr, &pg->pg_members, ps_pglist) { 370 if (pr->ps_mainproc->p_stat == SSTOP) { 371 LIST_FOREACH(pr, &pg->pg_members, ps_pglist) { 372 prsignal(pr, SIGHUP); 373 prsignal(pr, SIGCONT); 374 } 375 return; 376 } 377 } 378 } 379 380 #ifdef DDB 381 void 382 proc_printit(struct proc *p, const char *modif, 383 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 384 { 385 static const char *const pstat[] = { 386 "idle", "run", "sleep", "stop", "zombie", "dead", "onproc" 387 }; 388 char pstbuf[5]; 389 const char *pst = pstbuf; 390 391 392 if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0])) 393 snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat); 394 else 395 pst = pstat[(int)p->p_stat - 1]; 396 397 (*pr)("PROC (%s) pid=%d stat=%s\n", p->p_comm, p->p_pid, pst); 398 (*pr)(" flags process=%b proc=%b\n", 399 p->p_p->ps_flags, PS_BITS, p->p_flag, P_BITS); 400 (*pr)(" pri=%u, usrpri=%u, nice=%d\n", 401 p->p_priority, p->p_usrpri, p->p_p->ps_nice); 402 (*pr)(" forw=%p, list=%p,%p\n", 403 TAILQ_NEXT(p, p_runq), p->p_list.le_next, p->p_list.le_prev); 404 (*pr)(" process=%p user=%p, vmspace=%p\n", 405 p->p_p, p->p_addr, p->p_vmspace); 406 (*pr)(" estcpu=%u, cpticks=%d, pctcpu=%u.%u\n", 407 p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100); 408 (*pr)(" user=%u, sys=%u, intr=%u\n", 409 p->p_uticks, p->p_sticks, p->p_iticks); 410 } 411 #include <machine/db_machdep.h> 412 413 #include <ddb/db_interface.h> 414 #include <ddb/db_output.h> 415 416 void 417 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) 418 { 419 char *mode; 420 int doingzomb = 0; 421 struct proc *p; 422 struct process *pr, *ppr; 423 424 if (modif[0] == 0) 425 modif[0] = 'n'; /* default == normal mode */ 426 427 mode = "mawn"; 428 while (*mode && *mode != modif[0]) 429 mode++; 430 if (*mode == 0 || *mode == 'm') { 431 db_printf("usage: show all procs [/a] [/n] [/w]\n"); 432 db_printf("\t/a == show process address info\n"); 433 db_printf("\t/n == show normal process info [default]\n"); 434 db_printf("\t/w == show process wait/emul info\n"); 435 return; 436 } 437 438 pr = LIST_FIRST(&allprocess); 439 440 switch (*mode) { 441 442 case 'a': 443 db_printf(" PID %-10s %18s %18s %18s\n", 444 "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP"); 445 break; 446 case 'n': 447 db_printf(" PID %5s %5s %5s S %10s %-12s %-16s\n", 448 "PPID", "PGRP", "UID", "FLAGS", "WAIT", "COMMAND"); 449 break; 450 case 'w': 451 db_printf(" PID %-16s %-8s %18s %s\n", 452 "COMMAND", "EMUL", "WAIT-CHANNEL", "WAIT-MSG"); 453 break; 454 } 455 456 while (pr != NULL) { 457 ppr = pr->ps_pptr; 458 459 TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { 460 if (p->p_stat) { 461 db_printf("%c%5d ", p == curproc ? '*' : ' ', 462 p->p_pid); 463 464 switch (*mode) { 465 466 case 'a': 467 db_printf("%-10.10s %18p %18p %18p\n", 468 p->p_comm, p, p->p_addr, p->p_vmspace); 469 break; 470 471 case 'n': 472 db_printf("%5d %5d %5d %d %#10x " 473 "%-12.12s %-16s\n", 474 ppr ? ppr->ps_pid : -1, 475 pr->ps_pgrp ? pr->ps_pgrp->pg_id : -1, 476 pr->ps_ucred->cr_ruid, p->p_stat, 477 p->p_flag | pr->ps_flags, 478 (p->p_wchan && p->p_wmesg) ? 479 p->p_wmesg : "", p->p_comm); 480 break; 481 482 case 'w': 483 db_printf("%-16s %-8s %18p %s\n", p->p_comm, 484 pr->ps_emul->e_name, p->p_wchan, 485 (p->p_wchan && p->p_wmesg) ? 486 p->p_wmesg : ""); 487 break; 488 489 } 490 } 491 } 492 pr = LIST_NEXT(pr, ps_list); 493 if (pr == NULL && doingzomb == 0) { 494 doingzomb = 1; 495 pr = LIST_FIRST(&zombprocess); 496 } 497 } 498 } 499 #endif 500 501 #ifdef DEBUG 502 void 503 pgrpdump(void) 504 { 505 struct pgrp *pgrp; 506 struct process *pr; 507 int i; 508 509 for (i = 0; i <= pgrphash; i++) { 510 if (!LIST_EMPTY(&pgrphashtbl[i])) { 511 printf("\tindx %d\n", i); 512 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 513 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 514 pgrp, pgrp->pg_id, pgrp->pg_session, 515 pgrp->pg_session->s_count, 516 LIST_FIRST(&pgrp->pg_members)); 517 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) { 518 printf("\t\tpid %d addr %p pgrp %p\n", 519 pr->ps_pid, pr, pr->ps_pgrp); 520 } 521 } 522 } 523 } 524 } 525 #endif /* DEBUG */ 526