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