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