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