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