1 /* $NetBSD: kern_exit.c,v 1.91 2001/07/18 19:10:27 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1989, 1991, 1993 42 * The Regents of the University of California. All rights reserved. 43 * (c) UNIX System Laboratories, Inc. 44 * All or some portions of this file are derived from material licensed 45 * to the University of California by American Telephone and Telegraph 46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 47 * the permission of UNIX System Laboratories, Inc. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 * 77 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95 78 */ 79 80 #include "opt_ktrace.h" 81 #include "opt_sysv.h" 82 83 #include <sys/param.h> 84 #include <sys/systm.h> 85 #include <sys/map.h> 86 #include <sys/ioctl.h> 87 #include <sys/proc.h> 88 #include <sys/tty.h> 89 #include <sys/time.h> 90 #include <sys/resource.h> 91 #include <sys/kernel.h> 92 #include <sys/ktrace.h> 93 #include <sys/proc.h> 94 #include <sys/buf.h> 95 #include <sys/wait.h> 96 #include <sys/file.h> 97 #include <sys/vnode.h> 98 #include <sys/syslog.h> 99 #include <sys/malloc.h> 100 #include <sys/pool.h> 101 #include <sys/resourcevar.h> 102 #include <sys/ptrace.h> 103 #include <sys/acct.h> 104 #include <sys/filedesc.h> 105 #include <sys/signalvar.h> 106 #include <sys/sched.h> 107 #ifdef SYSVSHM 108 #include <sys/shm.h> 109 #endif 110 #ifdef SYSVSEM 111 #include <sys/sem.h> 112 #endif 113 114 #include <sys/mount.h> 115 #include <sys/syscallargs.h> 116 117 #include <machine/cpu.h> 118 119 #include <uvm/uvm_extern.h> 120 121 /* 122 * exit -- 123 * Death of process. 124 */ 125 int 126 sys_exit(struct proc *p, void *v, register_t *retval) 127 { 128 struct sys_exit_args /* { 129 syscallarg(int) rval; 130 } */ *uap = v; 131 132 exit1(p, W_EXITCODE(SCARG(uap, rval), 0)); 133 /* NOTREACHED */ 134 return (0); 135 } 136 137 /* 138 * Exit: deallocate address space and other resources, change proc state 139 * to zombie, and unlink proc from allproc and parent's lists. Save exit 140 * status and rusage for wait(). Check for child processes and orphan them. 141 */ 142 void 143 exit1(struct proc *p, int rv) 144 { 145 struct proc *q, *nq; 146 int s; 147 148 if (__predict_false(p == initproc)) 149 panic("init died (signal %d, exit %d)", 150 WTERMSIG(rv), WEXITSTATUS(rv)); 151 152 #ifdef PGINPROF 153 vmsizmon(); 154 #endif 155 if (p->p_flag & P_PROFIL) 156 stopprofclock(p); 157 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 158 /* 159 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 160 * wake up the parent early to avoid deadlock. 161 */ 162 p->p_flag |= P_WEXIT; 163 if (p->p_flag & P_PPWAIT) { 164 p->p_flag &= ~P_PPWAIT; 165 wakeup((caddr_t)p->p_pptr); 166 } 167 sigfillset(&p->p_sigctx.ps_sigignore); 168 sigemptyset(&p->p_sigctx.ps_siglist); 169 p->p_sigctx.ps_sigcheck = 0; 170 callout_stop(&p->p_realit_ch); 171 172 /* 173 * Close open files and release open-file table. 174 * This may block! 175 */ 176 fdfree(p); 177 cwdfree(p); 178 179 #ifdef SYSVSEM 180 semexit(p); 181 #endif 182 if (SESS_LEADER(p)) { 183 struct session *sp = p->p_session; 184 185 if (sp->s_ttyvp) { 186 /* 187 * Controlling process. 188 * Signal foreground pgrp, 189 * drain controlling terminal 190 * and revoke access to controlling terminal. 191 */ 192 if (sp->s_ttyp->t_session == sp) { 193 if (sp->s_ttyp->t_pgrp) 194 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 195 (void) ttywait(sp->s_ttyp); 196 /* 197 * The tty could have been revoked 198 * if we blocked. 199 */ 200 if (sp->s_ttyvp) 201 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 202 } 203 if (sp->s_ttyvp) 204 vrele(sp->s_ttyvp); 205 sp->s_ttyvp = NULL; 206 /* 207 * s_ttyp is not zero'd; we use this to indicate 208 * that the session once had a controlling terminal. 209 * (for logging and informational purposes) 210 */ 211 } 212 sp->s_leader = NULL; 213 } 214 fixjobc(p, p->p_pgrp, 0); 215 (void)acct_process(p); 216 #ifdef KTRACE 217 /* 218 * release trace file 219 */ 220 ktrderef(p); 221 #endif 222 /* 223 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 224 */ 225 p->p_stat = SDEAD; 226 227 /* 228 * Remove proc from pidhash chain so looking it up won't 229 * work. Move it from allproc to zombproc, but do not yet 230 * wake up the reaper. We will put the proc on the 231 * deadproc list later (using the p_hash member), and 232 * wake up the reaper when we do. 233 */ 234 s = proclist_lock_write(); 235 LIST_REMOVE(p, p_hash); 236 LIST_REMOVE(p, p_list); 237 LIST_INSERT_HEAD(&zombproc, p, p_list); 238 proclist_unlock_write(s); 239 240 /* 241 * Give orphaned children to init(8). 242 */ 243 q = p->p_children.lh_first; 244 if (q) /* only need this if any child is S_ZOMB */ 245 wakeup((caddr_t)initproc); 246 for (; q != 0; q = nq) { 247 nq = q->p_sibling.le_next; 248 proc_reparent(q, initproc); 249 /* 250 * Traced processes are killed 251 * since their existence means someone is screwing up. 252 */ 253 if (q->p_flag & P_TRACED) { 254 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 255 psignal(q, SIGKILL); 256 } 257 } 258 259 /* 260 * Save exit status and final rusage info, adding in child rusage 261 * info and self times. 262 */ 263 p->p_xstat = rv; 264 *p->p_ru = p->p_stats->p_ru; 265 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 266 ruadd(p->p_ru, &p->p_stats->p_cru); 267 268 /* 269 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 270 * flag set, notify init instead (and hope it will handle 271 * this situation). 272 */ 273 if (p->p_pptr->p_flag & P_NOCLDWAIT) { 274 struct proc *pp = p->p_pptr; 275 proc_reparent(p, initproc); 276 /* 277 * If this was the last child of our parent, notify 278 * parent, so in case he was wait(2)ing, he will 279 * continue. 280 */ 281 if (pp->p_children.lh_first == NULL) 282 wakeup((caddr_t)pp); 283 } 284 285 /* 286 * Release the process's signal state. 287 */ 288 sigactsfree(p); 289 290 /* 291 * Clear curproc after we've done all operations 292 * that could block, and before tearing down the rest 293 * of the process state that might be used from clock, etc. 294 * Also, can't clear curproc while we're still runnable, 295 * as we're not on a run queue (we are current, just not 296 * a proper proc any longer!). 297 * 298 * Other substructures are freed from wait(). 299 */ 300 curproc = NULL; 301 limfree(p->p_limit); 302 p->p_limit = NULL; 303 304 /* 305 * If emulation has process exit hook, call it now. 306 */ 307 if (p->p_emul->e_proc_exit) 308 (*p->p_emul->e_proc_exit)(p); 309 310 /* This process no longer needs to hold the kernel lock. */ 311 KERNEL_PROC_UNLOCK(p); 312 313 /* 314 * Finally, call machine-dependent code to switch to a new 315 * context (possibly the idle context). Once we are no longer 316 * using the dead process's vmspace and stack, exit2() will be 317 * called to schedule those resources to be released by the 318 * reaper thread. 319 * 320 * Note that cpu_exit() will end with a call equivalent to 321 * cpu_switch(), finishing our execution (pun intended). 322 */ 323 cpu_exit(p); 324 } 325 326 /* 327 * We are called from cpu_exit() once it is safe to schedule the 328 * dead process's resources to be freed (i.e., once we've switched to 329 * the idle PCB for the current CPU). 330 * 331 * NOTE: One must be careful with locking in this routine. It's 332 * called from a critical section in machine-dependent code, so 333 * we should refrain from changing any interrupt state. 334 * 335 * We lock the deadproc list (a spin lock), place the proc on that 336 * list (using the p_hash member), and wake up the reaper. 337 */ 338 void 339 exit2(struct proc *p) 340 { 341 342 simple_lock(&deadproc_slock); 343 LIST_INSERT_HEAD(&deadproc, p, p_hash); 344 simple_unlock(&deadproc_slock); 345 346 wakeup(&deadproc); 347 } 348 349 /* 350 * Process reaper. This is run by a kernel thread to free the resources 351 * of a dead process. Once the resources are free, the process becomes 352 * a zombie, and the parent is allowed to read the undead's status. 353 */ 354 void 355 reaper(void *arg) 356 { 357 struct proc *p; 358 359 KERNEL_PROC_UNLOCK(curproc); 360 361 for (;;) { 362 simple_lock(&deadproc_slock); 363 p = LIST_FIRST(&deadproc); 364 if (p == NULL) { 365 /* No work for us; go to sleep until someone exits. */ 366 (void) ltsleep(&deadproc, PVM|PNORELOCK, 367 "reaper", 0, &deadproc_slock); 368 continue; 369 } 370 371 /* Remove us from the deadproc list. */ 372 LIST_REMOVE(p, p_hash); 373 simple_unlock(&deadproc_slock); 374 KERNEL_PROC_LOCK(curproc); 375 376 /* 377 * Give machine-dependent code a chance to free any 378 * resources it couldn't free while still running on 379 * that process's context. This must be done before 380 * uvm_exit(), in case these resources are in the PCB. 381 */ 382 cpu_wait(p); 383 384 /* 385 * Free the VM resources we're still holding on to. 386 * We must do this from a valid thread because doing 387 * so may block. 388 */ 389 uvm_exit(p); 390 391 /* Process is now a true zombie. */ 392 p->p_stat = SZOMB; 393 394 /* Wake up the parent so it can get exit status. */ 395 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 396 psignal(p->p_pptr, P_EXITSIG(p)); 397 KERNEL_PROC_UNLOCK(curproc); 398 wakeup((caddr_t)p->p_pptr); 399 } 400 } 401 402 int 403 sys_wait4(struct proc *q, void *v, register_t *retval) 404 { 405 struct sys_wait4_args /* { 406 syscallarg(int) pid; 407 syscallarg(int *) status; 408 syscallarg(int) options; 409 syscallarg(struct rusage *) rusage; 410 } */ *uap = v; 411 struct proc *p, *t; 412 int nfound, status, error, s; 413 414 if (SCARG(uap, pid) == 0) 415 SCARG(uap, pid) = -q->p_pgid; 416 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG)) 417 return (EINVAL); 418 419 loop: 420 nfound = 0; 421 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 422 if (SCARG(uap, pid) != WAIT_ANY && 423 p->p_pid != SCARG(uap, pid) && 424 p->p_pgid != -SCARG(uap, pid)) 425 continue; 426 /* 427 * Wait for processes with p_exitsig != SIGCHLD processes only 428 * if WALTSIG is set; wait for processes with p_exitsig == 429 * SIGCHLD only if WALTSIG is clear. 430 */ 431 if (((SCARG(uap, options) & WALLSIG) == 0) && 432 ((SCARG(uap, options) & WALTSIG) ? 433 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD))) 434 continue; 435 436 nfound++; 437 if (p->p_stat == SZOMB) { 438 retval[0] = p->p_pid; 439 440 if (SCARG(uap, status)) { 441 status = p->p_xstat; /* convert to int */ 442 error = copyout((caddr_t)&status, 443 (caddr_t)SCARG(uap, status), 444 sizeof(status)); 445 if (error) 446 return (error); 447 } 448 if (SCARG(uap, rusage) && 449 (error = copyout((caddr_t)p->p_ru, 450 (caddr_t)SCARG(uap, rusage), 451 sizeof(struct rusage)))) 452 return (error); 453 /* 454 * If we got the child via ptrace(2) or procfs, and 455 * the parent is different (meaning the process was 456 * attached, rather than run as a child), then we need 457 * to give it back to the old parent, and send the 458 * parent the exit signal. The rest of the cleanup 459 * will be done when the old parent waits on the child. 460 */ 461 if ((p->p_flag & P_TRACED) && 462 p->p_oppid != p->p_pptr->p_pid) { 463 t = pfind(p->p_oppid); 464 proc_reparent(p, t ? t : initproc); 465 p->p_oppid = 0; 466 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 467 if (p->p_exitsig != 0) 468 psignal(p->p_pptr, P_EXITSIG(p)); 469 wakeup((caddr_t)p->p_pptr); 470 return (0); 471 } 472 scheduler_wait_hook(q, p); 473 p->p_xstat = 0; 474 ruadd(&q->p_stats->p_cru, p->p_ru); 475 pool_put(&rusage_pool, p->p_ru); 476 477 /* 478 * Finally finished with old proc entry. 479 * Unlink it from its process group and free it. 480 */ 481 leavepgrp(p); 482 483 s = proclist_lock_write(); 484 LIST_REMOVE(p, p_list); /* off zombproc */ 485 proclist_unlock_write(s); 486 487 LIST_REMOVE(p, p_sibling); 488 489 /* 490 * Decrement the count of procs running with this uid. 491 */ 492 (void)chgproccnt(p->p_cred->p_ruid, -1); 493 494 /* 495 * Free up credentials. 496 */ 497 if (--p->p_cred->p_refcnt == 0) { 498 crfree(p->p_cred->pc_ucred); 499 pool_put(&pcred_pool, p->p_cred); 500 } 501 502 /* 503 * Release reference to text vnode 504 */ 505 if (p->p_textvp) 506 vrele(p->p_textvp); 507 508 pool_put(&proc_pool, p); 509 nprocs--; 510 return (0); 511 } 512 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 513 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 514 p->p_flag |= P_WAITED; 515 retval[0] = p->p_pid; 516 517 if (SCARG(uap, status)) { 518 status = W_STOPCODE(p->p_xstat); 519 error = copyout((caddr_t)&status, 520 (caddr_t)SCARG(uap, status), 521 sizeof(status)); 522 } else 523 error = 0; 524 return (error); 525 } 526 } 527 if (nfound == 0) 528 return (ECHILD); 529 if (SCARG(uap, options) & WNOHANG) { 530 retval[0] = 0; 531 return (0); 532 } 533 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 534 return (error); 535 goto loop; 536 } 537 538 /* 539 * make process 'parent' the new parent of process 'child'. 540 */ 541 void 542 proc_reparent(struct proc *child, struct proc *parent) 543 { 544 545 if (child->p_pptr == parent) 546 return; 547 548 if (parent == initproc) 549 child->p_exitsig = SIGCHLD; 550 551 LIST_REMOVE(child, p_sibling); 552 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 553 child->p_pptr = parent; 554 } 555