1 /* $OpenBSD: kern_proc.c,v 1.11 2002/01/23 15:46:48 art 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 84 /* 85 * Locking of this proclist is special; it's accessed in a 86 * critical section of process exit, and thus locking it can't 87 * modify interrupt state. We use a simple spin lock for this 88 * proclist. Processes on this proclist are also on zombproc; 89 * we use the p_hash member to linkup to deadproc. 90 */ 91 struct simplelock deadproc_slock; 92 struct proclist deadproc; /* dead, but not yet undead */ 93 94 static void orphanpg __P((struct pgrp *)); 95 #ifdef DEBUG 96 void pgrpdump __P((void)); 97 #endif 98 99 /* 100 * Initialize global process hashing structures. 101 */ 102 void 103 procinit() 104 { 105 106 LIST_INIT(&allproc); 107 LIST_INIT(&zombproc); 108 109 LIST_INIT(&deadproc); 110 simple_lock_init(&deadproc_slock); 111 112 pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash); 113 pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash); 114 uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash); 115 116 pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl", 117 &pool_allocator_nointr); 118 pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "zombiepl", 119 &pool_allocator_nointr); 120 pool_init(&ucred_pool, sizeof(struct ucred), 0, 0, 0, "ucredpl", 121 &pool_allocator_nointr); 122 pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl", 123 &pool_allocator_nointr); 124 pool_init(&session_pool, sizeof(struct session), 0, 0, 0, "sessionpl", 125 &pool_allocator_nointr); 126 } 127 128 /* 129 * Change the count associated with number of processes 130 * a given user is using. 131 */ 132 int 133 chgproccnt(uid, diff) 134 uid_t uid; 135 int diff; 136 { 137 register struct uidinfo *uip; 138 register struct uihashhead *uipp; 139 140 uipp = UIHASH(uid); 141 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next) 142 if (uip->ui_uid == uid) 143 break; 144 if (uip) { 145 uip->ui_proccnt += diff; 146 if (uip->ui_proccnt > 0) 147 return (uip->ui_proccnt); 148 if (uip->ui_proccnt < 0) 149 panic("chgproccnt: procs < 0"); 150 LIST_REMOVE(uip, ui_hash); 151 FREE(uip, M_PROC); 152 return (0); 153 } 154 if (diff <= 0) { 155 if (diff == 0) 156 return(0); 157 panic("chgproccnt: lost user"); 158 } 159 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 160 LIST_INSERT_HEAD(uipp, uip, ui_hash); 161 uip->ui_uid = uid; 162 uip->ui_proccnt = diff; 163 return (diff); 164 } 165 166 /* 167 * Is p an inferior of the current process? 168 */ 169 int 170 inferior(p) 171 register struct proc *p; 172 { 173 174 for (; p != curproc; p = p->p_pptr) 175 if (p->p_pid == 0) 176 return (0); 177 return (1); 178 } 179 180 /* 181 * Locate a process by number 182 */ 183 struct proc * 184 pfind(pid) 185 register pid_t pid; 186 { 187 register struct proc *p; 188 189 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next) 190 if (p->p_pid == pid) 191 return (p); 192 return (NULL); 193 } 194 195 /* 196 * Locate a process group by number 197 */ 198 struct pgrp * 199 pgfind(pgid) 200 register pid_t pgid; 201 { 202 register struct pgrp *pgrp; 203 204 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next) 205 if (pgrp->pg_id == pgid) 206 return (pgrp); 207 return (NULL); 208 } 209 210 /* 211 * Move p to a new or existing process group (and session) 212 */ 213 int 214 enterpgrp(p, pgid, mksess) 215 register struct proc *p; 216 pid_t pgid; 217 int mksess; 218 { 219 register struct pgrp *pgrp = pgfind(pgid); 220 221 #ifdef DIAGNOSTIC 222 if (pgrp != NULL && mksess) /* firewalls */ 223 panic("enterpgrp: setsid into non-empty pgrp"); 224 if (SESS_LEADER(p)) 225 panic("enterpgrp: session leader attempted setpgrp"); 226 #endif 227 if (pgrp == NULL) { 228 pid_t savepid = p->p_pid; 229 struct proc *np; 230 /* 231 * new process group 232 */ 233 #ifdef DIAGNOSTIC 234 if (p->p_pid != pgid) 235 panic("enterpgrp: new pgrp and pid != pgid"); 236 #endif 237 if ((np = pfind(savepid)) == NULL || np != p) 238 return (ESRCH); 239 pgrp = pool_get(&pgrp_pool, PR_WAITOK); 240 if (mksess) { 241 register struct session *sess; 242 243 /* 244 * new session 245 */ 246 sess = pool_get(&session_pool, PR_WAITOK); 247 sess->s_leader = p; 248 sess->s_count = 1; 249 sess->s_ttyvp = NULL; 250 sess->s_ttyp = NULL; 251 bcopy(p->p_session->s_login, sess->s_login, 252 sizeof(sess->s_login)); 253 p->p_flag &= ~P_CONTROLT; 254 pgrp->pg_session = sess; 255 #ifdef DIAGNOSTIC 256 if (p != curproc) 257 panic("enterpgrp: mksession and p != curproc"); 258 #endif 259 } else { 260 pgrp->pg_session = p->p_session; 261 pgrp->pg_session->s_count++; 262 } 263 pgrp->pg_id = pgid; 264 LIST_INIT(&pgrp->pg_members); 265 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 266 pgrp->pg_jobc = 0; 267 } else if (pgrp == p->p_pgrp) 268 return (0); 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(p, pgrp, 1); 276 fixjobc(p, p->p_pgrp, 0); 277 278 LIST_REMOVE(p, p_pglist); 279 if (p->p_pgrp->pg_members.lh_first == 0) 280 pgdelete(p->p_pgrp); 281 p->p_pgrp = pgrp; 282 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 283 return (0); 284 } 285 286 /* 287 * remove process from process group 288 */ 289 int 290 leavepgrp(p) 291 register struct proc *p; 292 { 293 294 LIST_REMOVE(p, p_pglist); 295 if (p->p_pgrp->pg_members.lh_first == 0) 296 pgdelete(p->p_pgrp); 297 p->p_pgrp = 0; 298 return (0); 299 } 300 301 /* 302 * delete a process group 303 */ 304 void 305 pgdelete(pgrp) 306 register struct pgrp *pgrp; 307 { 308 309 if (pgrp->pg_session->s_ttyp != NULL && 310 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 311 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 312 LIST_REMOVE(pgrp, pg_hash); 313 SESSRELE(pgrp->pg_session); 314 pool_put(&pgrp_pool, pgrp); 315 } 316 317 /* 318 * Adjust pgrp jobc counters when specified process changes process group. 319 * We count the number of processes in each process group that "qualify" 320 * the group for terminal job control (those with a parent in a different 321 * process group of the same session). If that count reaches zero, the 322 * process group becomes orphaned. Check both the specified process' 323 * process group and that of its children. 324 * entering == 0 => p is leaving specified group. 325 * entering == 1 => p is entering specified group. 326 */ 327 void 328 fixjobc(p, pgrp, entering) 329 register struct proc *p; 330 register struct pgrp *pgrp; 331 int entering; 332 { 333 register struct pgrp *hispgrp; 334 register struct session *mysession = pgrp->pg_session; 335 336 /* 337 * Check p's parent to see whether p qualifies its own process 338 * group; if so, adjust count for p's process group. 339 */ 340 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 341 hispgrp->pg_session == mysession) { 342 if (entering) 343 pgrp->pg_jobc++; 344 else if (--pgrp->pg_jobc == 0) 345 orphanpg(pgrp); 346 } 347 348 /* 349 * Check this process' children to see whether they qualify 350 * their process groups; if so, adjust counts for children's 351 * process groups. 352 */ 353 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) 354 if ((hispgrp = p->p_pgrp) != pgrp && 355 hispgrp->pg_session == mysession && 356 P_ZOMBIE(p) == 0) { 357 if (entering) 358 hispgrp->pg_jobc++; 359 else if (--hispgrp->pg_jobc == 0) 360 orphanpg(hispgrp); 361 } 362 } 363 364 /* 365 * A process group has become orphaned; 366 * if there are any stopped processes in the group, 367 * hang-up all process in that group. 368 */ 369 static void 370 orphanpg(pg) 371 struct pgrp *pg; 372 { 373 register struct proc *p; 374 375 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 376 if (p->p_stat == SSTOP) { 377 for (p = pg->pg_members.lh_first; p != 0; 378 p = p->p_pglist.le_next) { 379 psignal(p, SIGHUP); 380 psignal(p, SIGCONT); 381 } 382 return; 383 } 384 } 385 } 386 387 #ifdef DEBUG 388 void 389 pgrpdump() 390 { 391 register struct pgrp *pgrp; 392 register struct proc *p; 393 register int i; 394 395 for (i = 0; i <= pgrphash; i++) { 396 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) { 397 printf("\tindx %d\n", i); 398 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) { 399 printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n", 400 pgrp, pgrp->pg_id, pgrp->pg_session, 401 pgrp->pg_session->s_count, 402 pgrp->pg_members.lh_first); 403 for (p = pgrp->pg_members.lh_first; p != 0; 404 p = p->p_pglist.le_next) { 405 printf("\t\tpid %d addr %p pgrp %p\n", 406 p->p_pid, p, p->p_pgrp); 407 } 408 } 409 } 410 } 411 } 412 #endif /* DEBUG */ 413