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.37 (Berkeley) 09/06/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 if (--p->p_limit->p_refcnt == 0) 136 FREE(p->p_limit, M_SUBPROC); 137 if (--p->p_cred->p_refcnt == 0) { 138 crfree(p->p_cred->pc_ucred); 139 FREE(p->p_cred, M_SUBPROC); 140 } 141 #ifdef KTRACE 142 /* 143 * release trace file 144 */ 145 if (p->p_tracep) 146 vrele(p->p_tracep); 147 #endif 148 /* 149 * Clear curproc after we've done all operations 150 * that could block, and before tearing down 151 * the rest of the process state. 152 */ 153 curproc = NULL; 154 if (--p->p_limit->p_refcnt == 0) 155 FREE(p->p_limit, M_SUBPROC); 156 157 /* 158 * Remove proc from allproc queue and pidhash chain. 159 * Place onto zombproc. Unlink from parent's child list. 160 */ 161 if (*p->p_prev = p->p_nxt) 162 p->p_nxt->p_prev = p->p_prev; 163 if (p->p_nxt = zombproc) 164 p->p_nxt->p_prev = &p->p_nxt; 165 p->p_prev = &zombproc; 166 zombproc = p; 167 p->p_stat = SZOMB; 168 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) 169 if (*pp == p) { 170 *pp = p->p_hash; 171 goto done; 172 } 173 panic("exit"); 174 done: 175 176 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 177 wakeup((caddr_t) initproc); 178 for (q = p->p_cptr; q != NULL; q = nq) { 179 nq = q->p_osptr; 180 if (nq != NULL) 181 nq->p_ysptr = NULL; 182 if (initproc->p_cptr) 183 initproc->p_cptr->p_ysptr = q; 184 q->p_osptr = initproc->p_cptr; 185 q->p_ysptr = NULL; 186 initproc->p_cptr = q; 187 188 q->p_pptr = initproc; 189 /* 190 * Traced processes are killed 191 * since their existence means someone is screwing up. 192 */ 193 if (q->p_flag&STRC) { 194 q->p_flag &= ~STRC; 195 psignal(q, SIGKILL); 196 } 197 } 198 p->p_cptr = NULL; 199 200 /* 201 * Save exit status and final rusage info, 202 * adding in child rusage info and self times. 203 */ 204 p->p_xstat = rv; 205 *p->p_ru = p->p_stats->p_ru; 206 p->p_ru->ru_stime = p->p_stime; 207 p->p_ru->ru_utime = p->p_utime; 208 ruadd(p->p_ru, &p->p_stats->p_cru); 209 210 /* 211 * Notify parent that we're gone. 212 */ 213 psignal(p->p_pptr, SIGCHLD); 214 wakeup((caddr_t)p->p_pptr); 215 #if defined(tahoe) 216 /* move this to cpu_exit */ 217 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 218 #endif 219 /* 220 * Finally, call machine-dependent code to release the remaining 221 * resources including address space, the kernel stack and pcb. 222 * The address space is released by "vmspace_free(p->p_vmspace)"; 223 * This is machine-dependent, as we may have to change stacks 224 * or ensure that the current one isn't reallocated before we 225 * finish. cpu_exit will end with a call to swtch(), finishing 226 * our execution (pun intended). 227 */ 228 cpu_exit(p); 229 /* NOTREACHED */ 230 } 231 232 #ifdef COMPAT_43 233 owait(p, uap, retval) 234 struct proc *p; 235 register struct args { 236 int pid; 237 int *status; 238 int options; 239 struct rusage *rusage; 240 int compat; 241 } *uap; 242 int *retval; 243 { 244 245 #ifdef PSL_ALLCC 246 if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) { 247 uap->options = 0; 248 uap->rusage = 0; 249 } else { 250 uap->options = p->p_regs[R0]; 251 uap->rusage = (struct rusage *)p->p_regs[R1]; 252 } 253 #else 254 uap->options = 0; 255 uap->rusage = 0; 256 #endif 257 uap->pid = WAIT_ANY; 258 uap->status = 0; 259 uap->compat = 1; 260 return (wait1(p, uap, retval)); 261 } 262 263 wait4(p, uap, retval) 264 struct proc *p; 265 struct args { 266 int pid; 267 int *status; 268 int options; 269 struct rusage *rusage; 270 int compat; 271 } *uap; 272 int *retval; 273 { 274 275 uap->compat = 0; 276 return (wait1(p, uap, retval)); 277 } 278 #else 279 #define wait1 wait4 280 #endif 281 282 /* 283 * Wait: check child processes to see if any have exited, 284 * stopped under trace, or (optionally) stopped by a signal. 285 * Pass back status and deallocate exited child's proc structure. 286 */ 287 wait1(q, uap, retval) 288 register struct proc *q; 289 register struct args { 290 int pid; 291 int *status; 292 int options; 293 struct rusage *rusage; 294 #ifdef COMPAT_43 295 int compat; 296 #endif 297 } *uap; 298 int retval[]; 299 { 300 register int nfound; 301 register struct proc *p; 302 int status, error; 303 304 if (uap->pid == 0) 305 uap->pid = -q->p_pgid; 306 #ifdef notyet 307 if (uap->options &~ (WUNTRACED|WNOHANG)) 308 return (EINVAL); 309 #endif 310 loop: 311 nfound = 0; 312 for (p = q->p_cptr; p; p = p->p_osptr) { 313 if (uap->pid != WAIT_ANY && 314 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 315 continue; 316 nfound++; 317 if (p->p_stat == SZOMB) { 318 retval[0] = p->p_pid; 319 #ifdef COMPAT_43 320 if (uap->compat) 321 retval[1] = p->p_xstat; 322 else 323 #endif 324 if (uap->status) { 325 status = p->p_xstat; /* convert to int */ 326 if (error = copyout((caddr_t)&status, 327 (caddr_t)uap->status, sizeof(status))) 328 return (error); 329 } 330 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 331 (caddr_t)uap->rusage, sizeof (struct rusage)))) 332 return (error); 333 p->p_xstat = 0; 334 ruadd(&q->p_stats->p_cru, p->p_ru); 335 FREE(p->p_ru, M_ZOMBIE); 336 if (--p->p_cred->p_refcnt == 0) { 337 crfree(p->p_cred->pc_ucred); 338 FREE(p->p_cred, M_SUBPROC); 339 } 340 341 /* 342 * Finally finished with old proc entry. 343 * Unlink it from its process group and free it. 344 */ 345 leavepgrp(p); 346 if (*p->p_prev = p->p_nxt) /* off zombproc */ 347 p->p_nxt->p_prev = p->p_prev; 348 if (q = p->p_ysptr) 349 q->p_osptr = p->p_osptr; 350 if (q = p->p_osptr) 351 q->p_ysptr = p->p_ysptr; 352 if ((q = p->p_pptr)->p_cptr == p) 353 q->p_cptr = p->p_osptr; 354 355 /* 356 * Give machine-dependent layer a chance 357 * to free anything that cpu_exit couldn't 358 * release while still running in process context. 359 */ 360 cpu_wait(p); 361 FREE(p, M_PROC); 362 nprocs--; 363 return (0); 364 } 365 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 366 (p->p_flag & STRC || uap->options & WUNTRACED)) { 367 p->p_flag |= SWTED; 368 retval[0] = p->p_pid; 369 #ifdef COMPAT_43 370 if (uap->compat) { 371 retval[1] = W_STOPCODE(p->p_xstat); 372 error = 0; 373 } else 374 #endif 375 if (uap->status) { 376 status = W_STOPCODE(p->p_xstat); 377 error = copyout((caddr_t)&status, 378 (caddr_t)uap->status, sizeof(status)); 379 } else 380 error = 0; 381 return (error); 382 } 383 } 384 if (nfound == 0) 385 return (ECHILD); 386 if (uap->options & WNOHANG) { 387 retval[0] = 0; 388 return (0); 389 } 390 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) 391 return (error); 392 goto loop; 393 } 394