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