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