1 /* $OpenBSD: kern_proc.c,v 1.13 2002/03/14 01:27:04 millert 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/buf.h> 44 #include <sys/acct.h> 45 #include <sys/wait.h> 46 #include <sys/file.h> 47 #include <ufs/ufs/quota.h> 48 #include <sys/uio.h> 49 #include <sys/malloc.h> 50 #include <sys/mbuf.h> 51 #include <sys/ioctl.h> 52 #include <sys/tty.h> 53 #include <sys/signalvar.h> 54 #include <sys/pool.h> 55 56 /* 57 * Structure associated with user cacheing. 58 */ 59 struct uidinfo { 60 LIST_ENTRY(uidinfo) ui_hash; 61 uid_t ui_uid; 62 long ui_proccnt; 63 }; 64 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 65 LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 66 u_long uihash; /* size of hash table - 1 */ 67 68 /* 69 * Other process lists 70 */ 71 struct pidhashhead *pidhashtbl; 72 u_long pidhash; 73 struct pgrphashhead *pgrphashtbl; 74 u_long pgrphash; 75 struct proclist allproc; 76 struct proclist zombproc; 77 78 struct pool proc_pool; 79 struct pool rusage_pool; 80 struct pool ucred_pool; 81 struct pool pgrp_pool; 82 struct pool session_pool; 83 struct pool pcred_pool; 84 85 /* 86 * Locking of this proclist is special; it's accessed in a 87 * critical section of process exit, and thus locking it can't 88 * modify interrupt state. We use a simple spin lock for this 89 * proclist. Processes on this proclist are also on zombproc; 90 * we use the p_hash member to linkup to deadproc. 91 */ 92 struct simplelock deadproc_slock; 93 struct proclist deadproc; /* dead, but not yet undead */ 94 95 static void orphanpg(struct pgrp *); 96 #ifdef DEBUG 97 void pgrpdump(void); 98 #endif 99 100 /* 101 * Initialize global process hashing structures. 102 */ 103 void 104 procinit() 105 { 106 107 LIST_INIT(&allproc); 108 LIST_INIT(&zombproc); 109 110 LIST_INIT(&deadproc); 111 simple_lock_init(&deadproc_slock); 112 113 pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash); 114 pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash); 115 uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash); 116 117 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl", 118 &pool_allocator_nointr); 119 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl", 120 &pool_allocator_nointr); 121 pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl", 122 &pool_allocator_nointr); 123 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl", 124 &pool_allocator_nointr); 125 pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl", 126 &pool_allocator_nointr); 127 pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl", 128 &pool_allocator_nointr); 129 } 130 131 /* 132 * Change the count associated with number of processes 133 * a given user is using. 134 */ 135 int 136 chgproccnt(uid, diff) 137 uid_t uid; 138 int diff; 139 { 140 register struct uidinfo *uip; 141 register struct uihashhead *uipp; 142 143 uipp = UIHASH(uid); 144 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next) 145 if (uip->ui_uid == uid) 146 break; 147 if (uip) { 148 uip->ui_proccnt += diff; 149 if (uip->ui_proccnt > 0) 150 return (uip->ui_proccnt); 151 if (uip->ui_proccnt < 0) 152 panic("chgproccnt: procs < 0"); 153 LIST_REMOVE(uip, ui_hash); 154 FREE(uip, M_PROC); 155 return (0); 156 } 157 if (diff <= 0) { 158 if (diff == 0) 159 return(0); 160 panic("chgproccnt: lost user"); 161 } 162 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 163 LIST_INSERT_HEAD(uipp, uip, ui_hash); 164 uip->ui_uid = uid; 165 uip->ui_proccnt = diff; 166 return (diff); 167 } 168 169 /* 170 * Is p an inferior of the current process? 171 */ 172 int 173 inferior(p) 174 register struct proc *p; 175 { 176 177 for (; p != curproc; p = p->p_pptr) 178 if (p->p_pid == 0) 179 return (0); 180 return (1); 181 } 182 183 /* 184 * Locate a process by number 185 */ 186 struct proc * 187 pfind(pid) 188 register pid_t pid; 189 { 190 register struct proc *p; 191 192 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next) 193 if (p->p_pid == pid) 194 return (p); 195 return (NULL); 196 } 197 198 /* 199 * Locate a process group by number 200 */ 201 struct pgrp * 202 pgfind(pgid) 203 register pid_t pgid; 204 { 205 register struct pgrp *pgrp; 206 207 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next) 208 if (pgrp->pg_id == pgid) 209 return (pgrp); 210 return (NULL); 211 } 212 213 /* 214 * Move p to a new or existing process group (and session) 215 */ 216 int 217 enterpgrp(p, pgid, mksess) 218 register struct proc *p; 219 pid_t pgid; 220 int mksess; 221 { 222 register struct pgrp *pgrp = pgfind(pgid); 223 224 #ifdef DIAGNOSTIC 225 if (pgrp != NULL && mksess) /* firewalls */ 226 panic("enterpgrp: setsid into non-empty pgrp"); 227 if (SESS_LEADER(p)) 228 panic("enterpgrp: session leader attempted setpgrp"); 229 #endif 230 if (pgrp == NULL) { 231 pid_t savepid = p->p_pid; 232 struct proc *np; 233 /* 234 * new process group 235 */ 236 #ifdef DIAGNOSTIC 237 if (p->p_pid != pgid) 238 panic("enterpgrp: new pgrp and pid != pgid"); 239 #endif 240 if ((np = pfind(savepid)) == NULL || np != p) 241 return (ESRCH); 242 pgrp = pool_get(&pgrp_pool, PR_WAITOK); 243 if (mksess) { 244 register struct session *sess; 245 246 /* 247 * new session 248 */ 249 sess = pool_get(&session_pool, PR_WAITOK); 250 sess->s_leader = p; 251 sess->s_count = 1; 252 sess->s_ttyvp = NULL; 253 sess->s_ttyp = NULL; 254 bcopy(p->p_session->s_login, sess->s_login, 255 sizeof(sess->s_login)); 256 p->p_flag &= ~P_CONTROLT; 257 pgrp->pg_session = sess; 258 #ifdef DIAGNOSTIC 259 if (p != curproc) 260 panic("enterpgrp: mksession and p != curproc"); 261 #endif 262 } else { 263 pgrp->pg_session = p->p_session; 264 pgrp->pg_session->s_count++; 265 } 266 pgrp->pg_id = pgid; 267 LIST_INIT(&pgrp->pg_members); 268 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 269 pgrp->pg_jobc = 0; 270 } else if (pgrp == p->p_pgrp) 271 return (0); 272 273 /* 274 * Adjust eligibility of affected pgrps to participate in job control. 275 * Increment eligibility counts before decrementing, otherwise we 276 * could reach 0 spuriously during the first call. 277 */ 278 fixjobc(p, pgrp, 1); 279 fixjobc(p, p->p_pgrp, 0); 280 281 LIST_REMOVE(p, p_pglist); 282 if (p->p_pgrp->pg_members.lh_first == 0) 283 pgdelete(p->p_pgrp); 284 p->p_pgrp = pgrp; 285 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 286 return (0); 287 } 288 289 /* 290 * remove process from process group 291 */ 292 int 293 leavepgrp(p) 294 register struct proc *p; 295 { 296 297 LIST_REMOVE(p, p_pglist); 298 if (p->p_pgrp->pg_members.lh_first == 0) 299 pgdelete(p->p_pgrp); 300 p->p_pgrp = 0; 301 return (0); 302 } 303 304 /* 305 * delete a process group 306 */ 307 void 308 pgdelete(pgrp) 309 register struct pgrp *pgrp; 310 { 311 312 if (pgrp->pg_session->s_ttyp != NULL && 313 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 314 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 315 LIST_REMOVE(pgrp, pg_hash); 316 SESSRELE(pgrp->pg_session); 317 pool_put(&pgrp_pool, pgrp); 318 } 319 320 /* 321 * Adjust pgrp jobc counters when specified process changes process group. 322 * We count the number of processes in each process group that "qualify" 323 * the group for terminal job control (those with a parent in a different 324 * process group of the same session). If that count reaches zero, the 325 * process group becomes orphaned. Check both the specified process' 326 * process group and that of its children. 327 * entering == 0 => p is leaving specified group. 328 * entering == 1 => p is entering specified group. 329 */ 330 void 331 fixjobc(p, pgrp, entering) 332 register struct proc *p; 333 register struct pgrp *pgrp; 334 int entering; 335 { 336 register struct pgrp *hispgrp; 337 register struct session *mysession = pgrp->pg_session; 338 339 /* 340 * Check p's parent to see whether p qualifies its own process 341 * group; if so, adjust count for p's process group. 342 */ 343 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 344 hispgrp->pg_session == mysession) { 345 if (entering) 346 pgrp->pg_jobc++; 347 else if (--pgrp->pg_jobc == 0) 348 orphanpg(pgrp); 349 } 350 351 /* 352 * Check this process' children to see whether they qualify 353 * their process groups; if so, adjust counts for children's 354 * process groups. 355 */ 356 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) 357 if ((hispgrp = p->p_pgrp) != pgrp && 358 hispgrp->pg_session == mysession && 359 P_ZOMBIE(p) == 0) { 360 if (entering) 361 hispgrp->pg_jobc++; 362 else if (--hispgrp->pg_jobc == 0) 363 orphanpg(hispgrp); 364 } 365 } 366 367 /* 368 * A process group has become orphaned; 369 * if there are any stopped processes in the group, 370 * hang-up all process in that group. 371 */ 372 static void 373 orphanpg(pg) 374 struct pgrp *pg; 375 { 376 register struct proc *p; 377 378 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 379 if (p->p_stat == SSTOP) { 380 for (p = pg->pg_members.lh_first; p != 0; 381 p = p->p_pglist.le_next) { 382 psignal(p, SIGHUP); 383 psignal(p, SIGCONT); 384 } 385 return; 386 } 387 } 388 } 389 390 #ifdef DEBUG 391 void 392 pgrpdump() 393 { 394 register struct pgrp *pgrp; 395 register struct proc *p; 396 register int i; 397 398 for (i = 0; i <= pgrphash; i++) { 399 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) { 400 printf("\tindx %d\n", i); 401 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) { 402 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 403 pgrp, pgrp->pg_id, pgrp->pg_session, 404 pgrp->pg_session->s_count, 405 pgrp->pg_members.lh_first); 406 for (p = pgrp->pg_members.lh_first; p != 0; 407 p = p->p_pglist.le_next) { 408 printf("\t\tpid %d addr %p pgrp %p\n", 409 p->p_pid, p, p->p_pgrp); 410 } 411 } 412 } 413 } 414 } 415 #endif /* DEBUG */ 416