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