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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
39 * $FreeBSD: src/sys/kern/kern_exit.c,v 1.92.2.11 2003/01/13 22:51:16 dillon Exp $
40 * $DragonFly: src/sys/kern/kern_exit.c,v 1.6 2003/06/20 02:09:56 dillon Exp $
43 #include "opt_compat.h"
44 #include "opt_ktrace.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/pioctl.h>
55 #include <sys/vnode.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/ptrace.h>
59 #include <sys/acct.h> /* for acct_process() function prototype */
60 #include <sys/filedesc.h>
67 #include <vm/vm_param.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_extern.h>
75 /* Required to be non-static for SysVR4 emulator */
76 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
78 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
80 static int wait1 __P((struct proc *, struct wait_args *, int));
83 * callout list for things to do at exit time
87 TAILQ_ENTRY(exitlist) next;
90 TAILQ_HEAD(exit_list_head, exitlist);
91 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
100 struct sys_exit_args /* {
105 exit1(p, W_EXITCODE(uap->rval, 0));
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.
116 register struct proc *p;
119 register struct proc *q, *nq;
120 register struct vmspace *vm;
125 printf("init died (signal %d, exit %d)\n",
126 WTERMSIG(rv), WEXITSTATUS(rv));
127 panic("Going nowhere without my init!");
132 /* are we a task leader? */
133 if(p == p->p_leader) {
134 struct kill_args killArgs;
135 killArgs.signum = SIGKILL;
138 killArgs.pid = q->p_pid;
140 * The interface for kill is better
141 * than the internal signal
148 tsleep((caddr_t)p, PWAIT, "exit1", 0);
154 STOPEVENT(p, S_EXIT, rv);
155 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */
158 * Check if any loadable modules need anything done at process exit.
159 * e.g. SYSV IPC stuff
160 * XXX what if one of these generates an error?
162 TAILQ_FOREACH(ep, &exit_list, next)
165 if (p->p_flag & P_PROFIL)
167 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
170 * If parent is waiting for us to exit or exec,
171 * P_PPWAIT is set; we will wakeup the parent below.
173 p->p_flag &= ~(P_TRACED | P_PPWAIT);
174 p->p_flag |= P_WEXIT;
175 SIGEMPTYSET(p->p_siglist);
176 if (timevalisset(&p->p_realtimer.it_value))
177 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
180 * Reset any sigio structures pointing to us as a result of
181 * F_SETOWN with our pid.
183 funsetownlst(&p->p_sigiolst);
186 * Close open files and release open-file table.
191 if(p->p_leader->p_peers) {
193 while(q->p_peers != p)
195 q->p_peers = p->p_peers;
196 wakeup((caddr_t)p->p_leader);
200 * XXX Shutdown SYSV semaphores
204 /* The next two chunks should probably be moved to vmspace_exit. */
207 * Release user portion of address space.
208 * This releases references to vnodes,
209 * which could cause I/O if the file has been unlinked.
210 * Need to do this early enough that we can still sleep.
211 * Can't free the entire vmspace as the kernel stack
212 * may be mapped within that space also.
214 * Processes sharing the same vmspace may exit in one order, and
215 * get cleaned up by vmspace_exit() in a different order. The
216 * last exiting process to reach this point releases as much of
217 * the environment as it can, and the last process cleaned up
218 * by vmspace_exit() (which decrements exitingcnt) cleans up the
222 if (--vm->vm_refcnt == 0) {
225 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
227 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
231 if (SESS_LEADER(p)) {
232 register struct session *sp = p->p_session;
236 * Controlling process.
237 * Signal foreground pgrp,
238 * drain controlling terminal
239 * and revoke access to controlling terminal.
241 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
242 if (sp->s_ttyp->t_pgrp)
243 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
244 (void) ttywait(sp->s_ttyp);
246 * The tty could have been revoked
250 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
256 * s_ttyp is not zero'd; we use this to indicate
257 * that the session once had a controlling terminal.
258 * (for logging and informational purposes)
263 fixjobc(p, p->p_pgrp, 0);
264 (void)acct_process(p);
269 p->p_traceflag = 0; /* don't trace the vrele() */
270 if ((vtmp = p->p_tracep) != NULL) {
276 * Release reference to text vnode
278 if ((vtmp = p->p_textvp) != NULL) {
284 * Remove proc from allproc queue and pidhash chain.
285 * Place onto zombproc. Unlink from parent's child list.
287 LIST_REMOVE(p, p_list);
288 LIST_INSERT_HEAD(&zombproc, p, p_list);
291 LIST_REMOVE(p, p_hash);
293 q = LIST_FIRST(&p->p_children);
294 if (q) /* only need this if any child is S_ZOMB */
295 wakeup((caddr_t) initproc);
296 for (; q != 0; q = nq) {
297 nq = LIST_NEXT(q, p_sibling);
298 LIST_REMOVE(q, p_sibling);
299 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
300 q->p_pptr = initproc;
301 q->p_sigparent = SIGCHLD;
303 * Traced processes are killed
304 * since their existence means someone is screwing up.
306 if (q->p_flag & P_TRACED) {
307 q->p_flag &= ~P_TRACED;
313 * Save exit status and final rusage info, adding in child rusage
314 * info and self times.
317 *p->p_ru = p->p_stats->p_ru;
318 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
319 ruadd(p->p_ru, &p->p_stats->p_cru);
322 * Pretend that an mi_switch() to the next process occurs now. We
323 * must set `switchtime' directly since we will call cpu_switch()
324 * directly. Set it now so that the rest of the exit time gets
325 * counted somewhere if possible.
327 microuptime(&mycpu->gd_switchtime);
328 mycpu->gd_switchticks = ticks;
331 * notify interested parties of our demise.
333 KNOTE(&p->p_klist, NOTE_EXIT);
336 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT
337 * flag set, notify process 1 instead (and hope it will handle
340 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
341 struct proc *pp = p->p_pptr;
342 proc_reparent(p, initproc);
344 * If this was the last child of our parent, notify
345 * parent, so in case he was wait(2)ing, he will
348 if (LIST_EMPTY(&pp->p_children))
352 if (p->p_sigparent && p->p_pptr != initproc) {
353 psignal(p->p_pptr, p->p_sigparent);
355 psignal(p->p_pptr, SIGCHLD);
358 wakeup((caddr_t)p->p_pptr);
360 /* move this to cpu_exit */
361 p->p_thread->td_pcb->pcb_saveacc.faddr = (float *)NULL;
364 * cpu_exit is responsible for clearing curproc, since
365 * it is heavily integrated with the thread/switching sequence.
367 * After this point we cannot block and we cannot become runnable
370 * Other substructures are freed from wait().
372 if (--p->p_limit->p_refcnt == 0) {
373 FREE(p->p_limit, M_SUBPROC);
378 * Finally, call machine-dependent code to release the remaining
379 * resources including address space, the kernel stack and pcb.
380 * The address space is released by "vmspace_free(p->p_vmspace)";
381 * This is machine-dependent, as we may have to change stacks
382 * or ensure that the current one isn't reallocated before we
383 * finish. cpu_exit will end with a call to cpu_switch(), finishing
384 * our execution (pun intended).
393 register struct owait_args /* {
403 return (wait1(p, &w, 1));
405 #endif /* COMPAT_43 */
410 struct wait_args *uap;
413 return (wait1(p, uap, 0));
417 wait1(q, uap, compat)
418 register struct proc *q;
419 register struct wait_args /* {
423 struct rusage *rusage;
428 register struct proc *p, *t;
432 uap->pid = -q->p_pgid;
433 if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
437 LIST_FOREACH(p, &q->p_children, p_sibling) {
438 if (uap->pid != WAIT_ANY &&
439 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
442 /* This special case handles a kthread spawned by linux_clone
443 * (see linux_misc.c). The linux_wait4 and linux_waitpid functions
444 * need to be able to distinguish between waiting on a process and
445 * waiting on a thread. It is a thread if p_sigparent is not SIGCHLD,
446 * and the WLINUXCLONE option signifies we want to wait for threads
449 if ((p->p_sigparent != SIGCHLD) ^ ((uap->options & WLINUXCLONE) != 0))
453 if (p->p_stat == SZOMB) {
454 /* charge childs scheduling cpu usage to parent */
455 if (curproc->p_pid != 1) {
457 ESTCPULIM(curproc->p_estcpu + p->p_estcpu);
460 q->p_retval[0] = p->p_pid;
463 q->p_retval[1] = p->p_xstat;
467 status = p->p_xstat; /* convert to int */
468 if ((error = copyout((caddr_t)&status,
469 (caddr_t)uap->status, sizeof(status))))
472 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
473 (caddr_t)uap->rusage, sizeof (struct rusage))))
476 * If we got the child via a ptrace 'attach',
477 * we need to give it back to the old parent.
479 if (p->p_oppid && (t = pfind(p->p_oppid))) {
487 ruadd(&q->p_stats->p_cru, p->p_ru);
488 FREE(p->p_ru, M_ZOMBIE);
492 * Decrement the count of procs running with this uid.
494 (void)chgproccnt(p->p_cred->p_uidinfo, -1, 0);
497 * Free up credentials.
499 if (--p->p_cred->p_refcnt == 0) {
501 uifree(p->p_cred->p_uidinfo);
502 FREE(p->p_cred, M_SUBPROC);
507 * Destroy empty prisons
509 if (p->p_prison && !--p->p_prison->pr_ref) {
510 if (p->p_prison->pr_linux != NULL)
511 FREE(p->p_prison->pr_linux, M_PRISON);
512 FREE(p->p_prison, M_PRISON);
516 * Remove unused arguments
518 if (p->p_args && --p->p_args->ar_ref == 0)
519 FREE(p->p_args, M_PARGS);
522 * Finally finished with old proc entry.
523 * Unlink it from its process group and free it.
526 LIST_REMOVE(p, p_list); /* off zombproc */
527 LIST_REMOVE(p, p_sibling);
529 if (--p->p_procsig->ps_refcnt == 0) {
530 if (p->p_sigacts != &p->p_addr->u_sigacts)
531 FREE(p->p_sigacts, M_SUBPROC);
532 FREE(p->p_procsig, M_SUBPROC);
537 * Give machine-dependent layer a chance
538 * to free anything that cpu_exit couldn't
539 * release while still running in process context.
546 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
547 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
548 p->p_flag |= P_WAITED;
549 q->p_retval[0] = p->p_pid;
552 q->p_retval[1] = W_STOPCODE(p->p_xstat);
557 status = W_STOPCODE(p->p_xstat);
558 error = copyout((caddr_t)&status,
559 (caddr_t)uap->status, sizeof(status));
567 if (uap->options & WNOHANG) {
571 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
577 * make process 'parent' the new parent of process 'child'.
580 proc_reparent(child, parent)
581 register struct proc *child;
582 register struct proc *parent;
585 if (child->p_pptr == parent)
588 LIST_REMOVE(child, p_sibling);
589 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
590 child->p_pptr = parent;
594 * The next two functions are to handle adding/deleting items on the
598 * Take the arguments given and put them onto the exit callout list,
599 * However first make sure that it's not already there.
600 * returns 0 on success.
605 exitlist_fn function;
610 /* Be noisy if the programmer has lost track of things */
611 if (rm_at_exit(function))
612 printf("WARNING: exit callout entry (%p) already present\n",
615 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
618 ep->function = function;
619 TAILQ_INSERT_TAIL(&exit_list, ep, next);
624 * Scan the exit callout list for the given item and remove it.
625 * Returns the number of items removed (0 or 1)
629 exitlist_fn function;
633 TAILQ_FOREACH(ep, &exit_list, next) {
634 if (ep->function == function) {
635 TAILQ_REMOVE(&exit_list, ep, next);
643 void check_sigacts (void)
645 struct proc *p = curproc;
649 if (p->p_procsig->ps_refcnt == 1 &&
650 p->p_sigacts != &p->p_addr->u_sigacts) {
653 p->p_addr->u_sigacts = *pss;
654 p->p_sigacts = &p->p_addr->u_sigacts;
656 FREE(pss, M_SUBPROC);