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