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