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.45 1997/02/22 09:39:04 peter 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/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/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/signalvar.h> 61 #include <sys/ptrace.h> 62 #include <sys/acct.h> /* for acct_process() function prototype */ 63 #include <sys/filedesc.h> 64 #include <sys/shm.h> 65 #include <sys/sem.h> 66 67 #ifdef COMPAT_43 68 #include <machine/reg.h> 69 #include <machine/psl.h> 70 #endif 71 72 #include <vm/vm.h> 73 #include <vm/vm_param.h> 74 #include <vm/vm_prot.h> 75 #include <sys/lock.h> 76 #include <vm/pmap.h> 77 #include <vm/vm_map.h> 78 #include <vm/vm_kern.h> 79 80 static int wait1 __P((struct proc *, struct wait_args *, int [], int)); 81 82 /* 83 * callout list for things to do at exit time 84 */ 85 typedef struct exit_list_element { 86 struct exit_list_element *next; 87 exitlist_fn function; 88 } *ele_p; 89 90 static ele_p exit_list; 91 92 /* 93 * exit -- 94 * Death of process. 95 */ 96 void 97 exit(p, uap, retval) 98 struct proc *p; 99 struct rexit_args /* { 100 int rval; 101 } */ *uap; 102 int *retval; 103 { 104 105 exit1(p, W_EXITCODE(uap->rval, 0)); 106 /* NOTREACHED */ 107 } 108 109 /* 110 * Exit: deallocate address space and other resources, change proc state 111 * to zombie, and unlink proc from allproc and parent's lists. Save exit 112 * status and rusage for wait(). Check for child processes and orphan them. 113 */ 114 void 115 exit1(p, rv) 116 register struct proc *p; 117 int rv; 118 { 119 register struct proc *q, *nq; 120 register struct vmspace *vm; 121 ele_p ep = exit_list; 122 123 if (p->p_pid == 1) { 124 printf("init died (signal %d, exit %d)\n", 125 WTERMSIG(rv), WEXITSTATUS(rv)); 126 panic("Going nowhere without my init!"); 127 } 128 #ifdef PGINPROF 129 vmsizmon(); 130 #endif 131 /* 132 * Check if any LKMs need anything done at process exit. 133 * e.g. SYSV IPC stuff 134 * XXX what if one of these generates an error? 135 */ 136 while (ep) { 137 (*ep->function)(p); 138 ep = ep->next; 139 } 140 141 if (p->p_flag & P_PROFIL) 142 stopprofclock(p); 143 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 144 M_ZOMBIE, M_WAITOK); 145 /* 146 * If parent is waiting for us to exit or exec, 147 * P_PPWAIT is set; we will wakeup the parent below. 148 */ 149 p->p_flag &= ~(P_TRACED | P_PPWAIT); 150 p->p_flag |= P_WEXIT; 151 p->p_sigignore = ~0; 152 p->p_siglist = 0; 153 untimeout(realitexpire, (caddr_t)p); 154 155 /* 156 * Close open files and release open-file table. 157 * This may block! 158 */ 159 fdfree(p); 160 161 /* 162 * Delete select() buffers 163 */ 164 if (p->p_selbits) 165 free (p->p_selbits, M_SELECT); 166 167 /* 168 * XXX Shutdown SYSV semaphores 169 */ 170 semexit(p); 171 172 /* The next two chunks should probably be moved to vmspace_exit. */ 173 vm = p->p_vmspace; 174 if (vm->vm_shm) 175 shmexit(p); 176 /* 177 * Release user portion of address space. 178 * This releases references to vnodes, 179 * which could cause I/O if the file has been unlinked. 180 * Need to do this early enough that we can still sleep. 181 * Can't free the entire vmspace as the kernel stack 182 * may be mapped within that space also. 183 */ 184 if (vm->vm_refcnt == 1) { 185 pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS, 186 VM_MAXUSER_ADDRESS); 187 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 188 VM_MAXUSER_ADDRESS); 189 } 190 191 if (SESS_LEADER(p)) { 192 register struct session *sp = p->p_session; 193 194 if (sp->s_ttyvp) { 195 /* 196 * Controlling process. 197 * Signal foreground pgrp, 198 * drain controlling terminal 199 * and revoke access to controlling terminal. 200 */ 201 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 202 if (sp->s_ttyp->t_pgrp) 203 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 204 (void) ttywait(sp->s_ttyp); 205 /* 206 * The tty could have been revoked 207 * if we blocked. 208 */ 209 if (sp->s_ttyvp) 210 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 211 } 212 if (sp->s_ttyvp) 213 vrele(sp->s_ttyvp); 214 sp->s_ttyvp = NULL; 215 /* 216 * s_ttyp is not zero'd; we use this to indicate 217 * that the session once had a controlling terminal. 218 * (for logging and informational purposes) 219 */ 220 } 221 sp->s_leader = NULL; 222 } 223 fixjobc(p, p->p_pgrp, 0); 224 if (p->p_limit->p_refcnt > 1 && 225 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 226 p->p_limit->p_refcnt--; 227 p->p_limit = limcopy(p->p_limit); 228 } 229 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 230 (void)acct_process(p); 231 #ifdef KTRACE 232 /* 233 * release trace file 234 */ 235 p->p_traceflag = 0; /* don't trace the vrele() */ 236 if (p->p_tracep) 237 vrele(p->p_tracep); 238 #endif 239 /* 240 * Remove proc from allproc queue and pidhash chain. 241 * Place onto zombproc. Unlink from parent's child list. 242 */ 243 LIST_REMOVE(p, p_list); 244 LIST_INSERT_HEAD(&zombproc, p, p_list); 245 p->p_stat = SZOMB; 246 247 LIST_REMOVE(p, p_hash); 248 249 q = p->p_children.lh_first; 250 if (q) /* only need this if any child is S_ZOMB */ 251 wakeup((caddr_t) initproc); 252 for (; q != 0; q = nq) { 253 nq = q->p_sibling.le_next; 254 LIST_REMOVE(q, p_sibling); 255 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling); 256 q->p_pptr = initproc; 257 /* 258 * Traced processes are killed 259 * since their existence means someone is screwing up. 260 */ 261 if (q->p_flag & P_TRACED) { 262 q->p_flag &= ~P_TRACED; 263 psignal(q, SIGKILL); 264 } 265 } 266 267 /* 268 * Save exit status and final rusage info, adding in child rusage 269 * info and self times. 270 */ 271 p->p_xstat = rv; 272 *p->p_ru = p->p_stats->p_ru; 273 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 274 ruadd(p->p_ru, &p->p_stats->p_cru); 275 276 /* 277 * Notify parent that we're gone. 278 */ 279 psignal(p->p_pptr, SIGCHLD); 280 wakeup((caddr_t)p->p_pptr); 281 #if defined(tahoe) 282 /* move this to cpu_exit */ 283 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 284 #endif 285 /* 286 * Clear curproc after we've done all operations 287 * that could block, and before tearing down the rest 288 * of the process state that might be used from clock, etc. 289 * Also, can't clear curproc while we're still runnable, 290 * as we're not on a run queue (we are current, just not 291 * a proper proc any longer!). 292 * 293 * Other substructures are freed from wait(). 294 */ 295 curproc = NULL; 296 if (--p->p_limit->p_refcnt == 0) { 297 FREE(p->p_limit, M_SUBPROC); 298 p->p_limit = NULL; 299 } 300 301 /* 302 * Finally, call machine-dependent code to release the remaining 303 * resources including address space, the kernel stack and pcb. 304 * The address space is released by "vmspace_free(p->p_vmspace)"; 305 * This is machine-dependent, as we may have to change stacks 306 * or ensure that the current one isn't reallocated before we 307 * finish. cpu_exit will end with a call to cpu_switch(), finishing 308 * our execution (pun intended). 309 */ 310 cpu_exit(p); 311 } 312 313 #ifdef COMPAT_43 314 #if defined(hp300) || defined(luna68k) 315 #include <machine/frame.h> 316 #define GETPS(rp) ((struct frame *)(rp))->f_sr 317 #else 318 #define GETPS(rp) (rp)[PS] 319 #endif 320 321 int 322 owait(p, uap, retval) 323 struct proc *p; 324 register struct owait_args /* { 325 int dummy; 326 } */ *uap; 327 int *retval; 328 { 329 struct wait_args w; 330 331 #ifdef PSL_ALLCC 332 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) { 333 w.options = 0; 334 w.rusage = NULL; 335 } else { 336 w.options = p->p_md.md_regs[R0]; 337 w.rusage = (struct rusage *)p->p_md.md_regs[R1]; 338 } 339 #else 340 w.options = 0; 341 w.rusage = NULL; 342 #endif 343 w.pid = WAIT_ANY; 344 w.status = NULL; 345 return (wait1(p, &w, retval, 1)); 346 } 347 #endif /* COMPAT_43 */ 348 349 int 350 wait4(p, uap, retval) 351 struct proc *p; 352 struct wait_args *uap; 353 int *retval; 354 { 355 356 return (wait1(p, uap, retval, 0)); 357 } 358 359 static int 360 wait1(q, uap, retval, compat) 361 register struct proc *q; 362 register struct wait_args /* { 363 int pid; 364 int *status; 365 int options; 366 struct rusage *rusage; 367 } */ *uap; 368 int retval[]; 369 int compat; 370 { 371 register int nfound; 372 register struct proc *p, *t; 373 int status, error; 374 375 if (uap->pid == 0) 376 uap->pid = -q->p_pgid; 377 #ifdef notyet 378 if (uap->options &~ (WUNTRACED|WNOHANG)) 379 return (EINVAL); 380 #endif 381 loop: 382 nfound = 0; 383 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 384 if (uap->pid != WAIT_ANY && 385 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 386 continue; 387 nfound++; 388 if (p->p_stat == SZOMB) { 389 /* charge childs scheduling cpu usage to parent */ 390 if (curproc->p_pid != 1) { 391 curproc->p_estcpu = min(curproc->p_estcpu + 392 p->p_estcpu, UCHAR_MAX); 393 } 394 395 retval[0] = p->p_pid; 396 #ifdef COMPAT_43 397 if (compat) 398 retval[1] = p->p_xstat; 399 else 400 #endif 401 if (uap->status) { 402 status = p->p_xstat; /* convert to int */ 403 if ((error = copyout((caddr_t)&status, 404 (caddr_t)uap->status, sizeof(status)))) 405 return (error); 406 } 407 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 408 (caddr_t)uap->rusage, sizeof (struct rusage)))) 409 return (error); 410 /* 411 * If we got the child via a ptrace 'attach', 412 * we need to give it back to the old parent. 413 */ 414 if (p->p_oppid && (t = pfind(p->p_oppid))) { 415 p->p_oppid = 0; 416 proc_reparent(p, t); 417 psignal(t, SIGCHLD); 418 wakeup((caddr_t)t); 419 return (0); 420 } 421 p->p_xstat = 0; 422 ruadd(&q->p_stats->p_cru, p->p_ru); 423 FREE(p->p_ru, M_ZOMBIE); 424 p->p_ru = NULL; 425 426 /* 427 * Decrement the count of procs running with this uid. 428 */ 429 (void)chgproccnt(p->p_cred->p_ruid, -1); 430 431 /* 432 * Release reference to text vnode 433 */ 434 if (p->p_textvp) 435 vrele(p->p_textvp); 436 437 /* 438 * Free up credentials. 439 */ 440 if (--p->p_cred->p_refcnt == 0) { 441 crfree(p->p_cred->pc_ucred); 442 FREE(p->p_cred, M_SUBPROC); 443 p->p_cred = NULL; 444 } 445 446 /* 447 * Finally finished with old proc entry. 448 * Unlink it from its process group and free it. 449 */ 450 leavepgrp(p); 451 LIST_REMOVE(p, p_list); /* off zombproc */ 452 LIST_REMOVE(p, p_sibling); 453 454 /* 455 * Give machine-dependent layer a chance 456 * to free anything that cpu_exit couldn't 457 * release while still running in process context. 458 */ 459 cpu_wait(p); 460 FREE(p, M_PROC); 461 nprocs--; 462 return (0); 463 } 464 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 465 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 466 p->p_flag |= P_WAITED; 467 retval[0] = p->p_pid; 468 #ifdef COMPAT_43 469 if (compat) { 470 retval[1] = W_STOPCODE(p->p_xstat); 471 error = 0; 472 } else 473 #endif 474 if (uap->status) { 475 status = W_STOPCODE(p->p_xstat); 476 error = copyout((caddr_t)&status, 477 (caddr_t)uap->status, sizeof(status)); 478 } else 479 error = 0; 480 return (error); 481 } 482 } 483 if (nfound == 0) 484 return (ECHILD); 485 if (uap->options & WNOHANG) { 486 retval[0] = 0; 487 return (0); 488 } 489 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))) 490 return (error); 491 goto loop; 492 } 493 494 /* 495 * make process 'parent' the new parent of process 'child'. 496 */ 497 void 498 proc_reparent(child, parent) 499 register struct proc *child; 500 register struct proc *parent; 501 { 502 503 if (child->p_pptr == parent) 504 return; 505 506 LIST_REMOVE(child, p_sibling); 507 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 508 child->p_pptr = parent; 509 } 510 511 /* 512 * The next two functions are to handle adding/deleting items on the 513 * exit callout list 514 * 515 * at_exit(): 516 * Take the arguments given and put them onto the exit callout list, 517 * However first make sure that it's not already there. 518 * returns 0 on success. 519 */ 520 int 521 at_exit(exitlist_fn function) 522 { 523 ele_p ep; 524 525 /* Be noisy if the programmer has lost track of things */ 526 if (rm_at_exit(function)) 527 printf("exit callout entry already present\n"); 528 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 529 if (ep == NULL) 530 return (ENOMEM); 531 ep->next = exit_list; 532 ep->function = function; 533 exit_list = ep; 534 return (0); 535 } 536 /* 537 * Scan the exit callout list for the given items and remove them. 538 * Returns the number of items removed. 539 * Logically this can only be 0 or 1. 540 */ 541 int 542 rm_at_exit(exitlist_fn function) 543 { 544 ele_p *epp, ep; 545 int count; 546 547 count = 0; 548 epp = &exit_list; 549 ep = *epp; 550 while (ep) { 551 if (ep->function == function) { 552 *epp = ep->next; 553 free(ep, M_TEMP); 554 count++; 555 } else { 556 epp = &ep->next; 557 } 558 ep = *epp; 559 } 560 return (count); 561 } 562 563 564