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