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