1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)kern_exit.c 7.38 (Berkeley) 09/07/91 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "map.h" 13 #include "ioctl.h" 14 #include "tty.h" 15 #include "time.h" 16 #include "resource.h" 17 #include "kernel.h" 18 #include "proc.h" 19 #include "buf.h" 20 #include "wait.h" 21 #include "file.h" 22 #include "vnode.h" 23 #include "syslog.h" 24 #include "malloc.h" 25 #include "resourcevar.h" 26 27 #include "machine/cpu.h" 28 #ifdef COMPAT_43 29 #include "machine/reg.h" 30 #include "machine/psl.h" 31 #endif 32 33 #include "vm/vm.h" 34 #include "vm/vm_kern.h" 35 36 /* 37 * Exit system call: pass back caller's arg 38 */ 39 /* ARGSUSED */ 40 rexit(p, uap, retval) 41 struct proc *p; 42 struct args { 43 int rval; 44 } *uap; 45 int *retval; 46 { 47 48 exit(p, W_EXITCODE(uap->rval, 0)); 49 /* NOTREACHED */ 50 } 51 52 /* 53 * Exit: deallocate address space and other resources, 54 * change proc state to zombie, and unlink proc from allproc 55 * and parent's lists. Save exit status and rusage for wait(). 56 * Check for child processes and orphan them. 57 */ 58 exit(p, rv) 59 register struct proc *p; 60 int rv; 61 { 62 register struct proc *q, *nq; 63 register struct proc **pp; 64 int s; 65 66 #ifdef PGINPROF 67 vmsizmon(); 68 #endif 69 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 70 M_ZOMBIE, M_WAITOK); 71 /* 72 * If parent is waiting for us to exit or exec, 73 * SPPWAIT is set; we will wakeup the parent below. 74 */ 75 p->p_flag &= ~(STRC|SPPWAIT); 76 p->p_flag |= SWEXIT; 77 p->p_sigignore = ~0; 78 p->p_sig = 0; 79 untimeout(realitexpire, (caddr_t)p); 80 81 /* 82 * Close open files and release open-file table. 83 * This may block! 84 */ 85 fdfree(p); 86 87 /* The next two chunks should probably be moved to vmspace_exit. */ 88 #ifdef SYSVSHM 89 if (p->p_vmspace->vm_shm) 90 shmexit(p); 91 #endif 92 /* 93 * Release user portion of address space. 94 * This releases references to vnodes, 95 * which could cause I/O if the file has been unlinked. 96 * Need to do this early enough that we can still sleep. 97 * Can't free the entire vmspace as the kernel stack 98 * may be mapped within that space also. 99 */ 100 if (p->p_vmspace->vm_refcnt == 1) 101 (void) vm_map_remove(&p->p_vmspace->vm_map, VM_MIN_ADDRESS, 102 VM_MAXUSER_ADDRESS); 103 104 if (p->p_pid == 1) 105 panic("init died"); 106 if (SESS_LEADER(p)) { 107 register struct session *sp = p->p_session; 108 109 if (sp->s_ttyvp) { 110 /* 111 * Controlling process. 112 * Signal foreground pgrp, 113 * drain controlling terminal 114 * and revoke access to controlling terminal. 115 */ 116 if (sp->s_ttyp->t_session == sp) { 117 if (sp->s_ttyp->t_pgrp) 118 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 119 (void) ttywait(sp->s_ttyp); 120 vgoneall(sp->s_ttyvp); 121 } 122 vrele(sp->s_ttyvp); 123 sp->s_ttyvp = NULL; 124 /* 125 * s_ttyp is not zero'd; we use this to indicate 126 * that the session once had a controlling terminal. 127 * (for logging and informational purposes) 128 */ 129 } 130 sp->s_leader = NULL; 131 } 132 fixjobc(p, p->p_pgrp, 0); 133 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 134 (void) acct(p); 135 #ifdef KTRACE 136 /* 137 * release trace file 138 */ 139 if (p->p_tracep) 140 vrele(p->p_tracep); 141 #endif 142 /* 143 * Clear curproc after we've done all operations 144 * that could block, and before tearing down 145 * the rest of the process state. 146 */ 147 curproc = NULL; 148 if (--p->p_limit->p_refcnt == 0) 149 FREE(p->p_limit, M_SUBPROC); 150 151 /* 152 * Remove proc from allproc queue and pidhash chain. 153 * Place onto zombproc. Unlink from parent's child list. 154 */ 155 if (*p->p_prev = p->p_nxt) 156 p->p_nxt->p_prev = p->p_prev; 157 if (p->p_nxt = zombproc) 158 p->p_nxt->p_prev = &p->p_nxt; 159 p->p_prev = &zombproc; 160 zombproc = p; 161 p->p_stat = SZOMB; 162 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) 163 if (*pp == p) { 164 *pp = p->p_hash; 165 goto done; 166 } 167 panic("exit"); 168 done: 169 170 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 171 wakeup((caddr_t) initproc); 172 for (q = p->p_cptr; q != NULL; q = nq) { 173 nq = q->p_osptr; 174 if (nq != NULL) 175 nq->p_ysptr = NULL; 176 if (initproc->p_cptr) 177 initproc->p_cptr->p_ysptr = q; 178 q->p_osptr = initproc->p_cptr; 179 q->p_ysptr = NULL; 180 initproc->p_cptr = q; 181 182 q->p_pptr = initproc; 183 /* 184 * Traced processes are killed 185 * since their existence means someone is screwing up. 186 */ 187 if (q->p_flag&STRC) { 188 q->p_flag &= ~STRC; 189 psignal(q, SIGKILL); 190 } 191 } 192 p->p_cptr = NULL; 193 194 /* 195 * Save exit status and final rusage info, 196 * adding in child rusage info and self times. 197 */ 198 p->p_xstat = rv; 199 *p->p_ru = p->p_stats->p_ru; 200 p->p_ru->ru_stime = p->p_stime; 201 p->p_ru->ru_utime = p->p_utime; 202 ruadd(p->p_ru, &p->p_stats->p_cru); 203 204 /* 205 * Notify parent that we're gone. 206 */ 207 psignal(p->p_pptr, SIGCHLD); 208 wakeup((caddr_t)p->p_pptr); 209 #if defined(tahoe) 210 /* move this to cpu_exit */ 211 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 212 #endif 213 /* 214 * Finally, call machine-dependent code to release the remaining 215 * resources including address space, the kernel stack and pcb. 216 * The address space is released by "vmspace_free(p->p_vmspace)"; 217 * This is machine-dependent, as we may have to change stacks 218 * or ensure that the current one isn't reallocated before we 219 * finish. cpu_exit will end with a call to swtch(), finishing 220 * our execution (pun intended). 221 */ 222 cpu_exit(p); 223 /* NOTREACHED */ 224 } 225 226 #ifdef COMPAT_43 227 owait(p, uap, retval) 228 struct proc *p; 229 register struct args { 230 int pid; 231 int *status; 232 int options; 233 struct rusage *rusage; 234 int compat; 235 } *uap; 236 int *retval; 237 { 238 239 #ifdef PSL_ALLCC 240 if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) { 241 uap->options = 0; 242 uap->rusage = 0; 243 } else { 244 uap->options = p->p_regs[R0]; 245 uap->rusage = (struct rusage *)p->p_regs[R1]; 246 } 247 #else 248 uap->options = 0; 249 uap->rusage = 0; 250 #endif 251 uap->pid = WAIT_ANY; 252 uap->status = 0; 253 uap->compat = 1; 254 return (wait1(p, uap, retval)); 255 } 256 257 wait4(p, uap, retval) 258 struct proc *p; 259 struct args { 260 int pid; 261 int *status; 262 int options; 263 struct rusage *rusage; 264 int compat; 265 } *uap; 266 int *retval; 267 { 268 269 uap->compat = 0; 270 return (wait1(p, uap, retval)); 271 } 272 #else 273 #define wait1 wait4 274 #endif 275 276 /* 277 * Wait: check child processes to see if any have exited, 278 * stopped under trace, or (optionally) stopped by a signal. 279 * Pass back status and deallocate exited child's proc structure. 280 */ 281 wait1(q, uap, retval) 282 register struct proc *q; 283 register struct args { 284 int pid; 285 int *status; 286 int options; 287 struct rusage *rusage; 288 #ifdef COMPAT_43 289 int compat; 290 #endif 291 } *uap; 292 int retval[]; 293 { 294 register int nfound; 295 register struct proc *p; 296 int status, error; 297 298 if (uap->pid == 0) 299 uap->pid = -q->p_pgid; 300 #ifdef notyet 301 if (uap->options &~ (WUNTRACED|WNOHANG)) 302 return (EINVAL); 303 #endif 304 loop: 305 nfound = 0; 306 for (p = q->p_cptr; p; p = p->p_osptr) { 307 if (uap->pid != WAIT_ANY && 308 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 309 continue; 310 nfound++; 311 if (p->p_stat == SZOMB) { 312 retval[0] = p->p_pid; 313 #ifdef COMPAT_43 314 if (uap->compat) 315 retval[1] = p->p_xstat; 316 else 317 #endif 318 if (uap->status) { 319 status = p->p_xstat; /* convert to int */ 320 if (error = copyout((caddr_t)&status, 321 (caddr_t)uap->status, sizeof(status))) 322 return (error); 323 } 324 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 325 (caddr_t)uap->rusage, sizeof (struct rusage)))) 326 return (error); 327 p->p_xstat = 0; 328 ruadd(&q->p_stats->p_cru, p->p_ru); 329 FREE(p->p_ru, M_ZOMBIE); 330 if (--p->p_cred->p_refcnt == 0) { 331 crfree(p->p_cred->pc_ucred); 332 FREE(p->p_cred, M_SUBPROC); 333 } 334 335 /* 336 * Finally finished with old proc entry. 337 * Unlink it from its process group and free it. 338 */ 339 leavepgrp(p); 340 if (*p->p_prev = p->p_nxt) /* off zombproc */ 341 p->p_nxt->p_prev = p->p_prev; 342 if (q = p->p_ysptr) 343 q->p_osptr = p->p_osptr; 344 if (q = p->p_osptr) 345 q->p_ysptr = p->p_ysptr; 346 if ((q = p->p_pptr)->p_cptr == p) 347 q->p_cptr = p->p_osptr; 348 349 /* 350 * Give machine-dependent layer a chance 351 * to free anything that cpu_exit couldn't 352 * release while still running in process context. 353 */ 354 cpu_wait(p); 355 FREE(p, M_PROC); 356 nprocs--; 357 return (0); 358 } 359 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 360 (p->p_flag & STRC || uap->options & WUNTRACED)) { 361 p->p_flag |= SWTED; 362 retval[0] = p->p_pid; 363 #ifdef COMPAT_43 364 if (uap->compat) { 365 retval[1] = W_STOPCODE(p->p_xstat); 366 error = 0; 367 } else 368 #endif 369 if (uap->status) { 370 status = W_STOPCODE(p->p_xstat); 371 error = copyout((caddr_t)&status, 372 (caddr_t)uap->status, sizeof(status)); 373 } else 374 error = 0; 375 return (error); 376 } 377 } 378 if (nfound == 0) 379 return (ECHILD); 380 if (uap->options & WNOHANG) { 381 retval[0] = 0; 382 return (0); 383 } 384 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) 385 return (error); 386 goto loop; 387 } 388