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.74 2007/02/21 15:46:48 corecode 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/ktrace.h>
53 #include <sys/pioctl.h>
56 #include <sys/vnode.h>
57 #include <sys/resourcevar.h>
58 #include <sys/signalvar.h>
59 #include <sys/ptrace.h>
60 #include <sys/acct.h> /* for acct_process() function prototype */
61 #include <sys/filedesc.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/upcall.h>
71 #include <vm/vm_param.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_zone.h>
76 #include <vm/vm_extern.h>
79 #include <sys/thread2.h>
81 static MALLOC_DEFINE(M_ATEXIT
, "atexit", "atexit callback");
82 static MALLOC_DEFINE(M_ZOMBIE
, "zombie", "zombie proc status");
85 * callout list for things to do at exit time
89 TAILQ_ENTRY(exitlist
) next
;
92 TAILQ_HEAD(exit_list_head
, exitlist
);
93 static struct exit_list_head exit_list
= TAILQ_HEAD_INITIALIZER(exit_list
);
99 * SYS_EXIT_ARGS(int rval)
102 sys_exit(struct exit_args
*uap
)
104 exit1(W_EXITCODE(uap
->rval
, 0));
109 * Exit: deallocate address space and other resources, change proc state
110 * to zombie, and unlink proc from allproc and parent's lists. Save exit
111 * status and rusage for wait(). Check for child processes and orphan them.
116 struct thread
*td
= curthread
;
117 struct proc
*p
= td
->td_proc
;
118 struct lwp
*lp
= td
->td_lwp
;
125 kprintf("init died (signal %d, exit %d)\n",
126 WTERMSIG(rv
), WEXITSTATUS(rv
));
127 panic("Going nowhere without my init!");
130 /* XXX lwp kill other threads */
132 caps_exit(lp
->lwp_thread
);
135 /* are we a task leader? */
136 if(p
== p
->p_leader
) {
137 struct kill_args killArgs
;
138 killArgs
.signum
= SIGKILL
;
141 killArgs
.pid
= q
->p_pid
;
143 * The interface for kill is better
144 * than the internal signal
151 tsleep((caddr_t
)p
, 0, "exit1", 0);
157 STOPEVENT(p
, S_EXIT
, rv
);
158 wakeup(&p
->p_stype
); /* Wakeup anyone in procfs' PIOCWAIT */
161 * Check if any loadable modules need anything done at process exit.
162 * e.g. SYSV IPC stuff
163 * XXX what if one of these generates an error?
165 TAILQ_FOREACH(ep
, &exit_list
, next
)
168 if (p
->p_flag
& P_PROFIL
)
171 * If parent is waiting for us to exit or exec,
172 * P_PPWAIT is set; we will wakeup the parent below.
174 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
175 p
->p_flag
|= P_WEXIT
;
176 SIGEMPTYSET(p
->p_siglist
);
177 SIGEMPTYSET(lp
->lwp_siglist
);
178 if (timevalisset(&p
->p_realtimer
.it_value
))
179 callout_stop(&p
->p_ithandle
);
182 * Reset any sigio structures pointing to us as a result of
183 * F_SETOWN with our pid.
185 funsetownlst(&p
->p_sigiolst
);
188 * Close open files and release open-file table.
194 if(p
->p_leader
->p_peers
) {
196 while(q
->p_peers
!= p
)
198 q
->p_peers
= p
->p_peers
;
199 wakeup((caddr_t
)p
->p_leader
);
203 * XXX Shutdown SYSV semaphores
207 KKASSERT(p
->p_numposixlocks
== 0);
209 /* The next two chunks should probably be moved to vmspace_exit. */
213 * Release upcalls associated with this process
218 /* clean up data related to virtual kernel operation */
223 * Release user portion of address space.
224 * This releases references to vnodes,
225 * which could cause I/O if the file has been unlinked.
226 * Need to do this early enough that we can still sleep.
227 * Can't free the entire vmspace as the kernel stack
228 * may be mapped within that space also.
230 * Processes sharing the same vmspace may exit in one order, and
231 * get cleaned up by vmspace_exit() in a different order. The
232 * last exiting process to reach this point releases as much of
233 * the environment as it can, and the last process cleaned up
234 * by vmspace_exit() (which decrements exitingcnt) cleans up the
238 if (--vm
->vm_refcnt
== 0) {
240 pmap_remove_pages(vmspace_pmap(vm
), VM_MIN_USER_ADDRESS
,
241 VM_MAX_USER_ADDRESS
);
242 vm_map_remove(&vm
->vm_map
, VM_MIN_USER_ADDRESS
,
243 VM_MAX_USER_ADDRESS
);
246 if (SESS_LEADER(p
)) {
247 struct session
*sp
= p
->p_session
;
252 * We are the controlling process. Signal the
253 * foreground process group, drain the controlling
254 * terminal, and revoke access to the controlling
257 * NOTE: while waiting for the process group to exit
258 * it is possible that one of the processes in the
259 * group will revoke the tty, so we have to recheck.
261 if (sp
->s_ttyp
&& (sp
->s_ttyp
->t_session
== sp
)) {
262 if (sp
->s_ttyp
->t_pgrp
)
263 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
264 (void) ttywait(sp
->s_ttyp
);
266 * The tty could have been revoked
269 if ((vp
= sp
->s_ttyvp
) != NULL
) {
270 ttyclosesession(sp
, 0);
272 VOP_REVOKE(vp
, REVOKEALL
);
274 vrele(vp
); /* s_ttyvp ref */
278 * Release the tty. If someone has it open via
279 * /dev/tty then close it (since they no longer can
280 * once we've NULL'd it out).
283 ttyclosesession(sp
, 1);
285 * s_ttyp is not zero'd; we use this to indicate
286 * that the session once had a controlling terminal.
287 * (for logging and informational purposes)
292 fixjobc(p
, p
->p_pgrp
, 0);
293 (void)acct_process(p
);
299 ktrdestroy(&p
->p_tracenode
);
303 * Release reference to text vnode
305 if ((vtmp
= p
->p_textvp
) != NULL
) {
311 * Move the process to the zombie list. This will block
312 * until the process p_lock count reaches 0. The process will
313 * not be reaped until TDF_EXITING is set by cpu_thread_exit(),
314 * which is called from cpu_proc_exit().
316 proc_move_allproc_zombie(p
);
318 q
= LIST_FIRST(&p
->p_children
);
319 if (q
) /* only need this if any child is S_ZOMB */
320 wakeup((caddr_t
) initproc
);
321 for (; q
!= 0; q
= nq
) {
322 nq
= LIST_NEXT(q
, p_sibling
);
323 LIST_REMOVE(q
, p_sibling
);
324 LIST_INSERT_HEAD(&initproc
->p_children
, q
, p_sibling
);
325 q
->p_pptr
= initproc
;
326 q
->p_sigparent
= SIGCHLD
;
328 * Traced processes are killed
329 * since their existence means someone is screwing up.
331 if (q
->p_flag
& P_TRACED
) {
332 q
->p_flag
&= ~P_TRACED
;
338 * Save exit status and final rusage info, adding in child rusage
339 * info and self times.
342 calcru_proc(p
, &p
->p_ru
);
343 ruadd(&p
->p_ru
, &p
->p_cru
);
346 * notify interested parties of our demise.
348 KNOTE(&p
->p_klist
, NOTE_EXIT
);
351 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT
352 * flag set, notify process 1 instead (and hope it will handle
355 if (p
->p_pptr
->p_procsig
->ps_flag
& PS_NOCLDWAIT
) {
356 struct proc
*pp
= p
->p_pptr
;
357 proc_reparent(p
, initproc
);
359 * If this was the last child of our parent, notify
360 * parent, so in case he was wait(2)ing, he will
363 if (LIST_EMPTY(&pp
->p_children
))
367 if (p
->p_sigparent
&& p
->p_pptr
!= initproc
) {
368 ksignal(p
->p_pptr
, p
->p_sigparent
);
370 ksignal(p
->p_pptr
, SIGCHLD
);
373 wakeup((caddr_t
)p
->p_pptr
);
375 * cpu_exit is responsible for clearing curproc, since
376 * it is heavily integrated with the thread/switching sequence.
378 * Other substructures are freed from wait().
380 plimit_free(&p
->p_limit
);
383 * Release the current user process designation on the process so
384 * the userland scheduler can work in someone else.
386 p
->p_usched
->release_curproc(lp
);
389 * Finally, call machine-dependent code to release the remaining
390 * resources including address space, the kernel stack and pcb.
391 * The address space is released by "vmspace_free(p->p_vmspace)";
392 * This is machine-dependent, as we may have to change stacks
393 * or ensure that the current one isn't reallocated before we
394 * finish. cpu_exit will end with a call to cpu_switch(), finishing
395 * our execution (pun intended).
401 sys_wait4(struct wait_args
*uap
)
403 struct rusage rusage
;
406 error
= kern_wait(uap
->pid
, uap
->status
? &status
: NULL
,
407 uap
->options
, uap
->rusage
? &rusage
: NULL
, &uap
->sysmsg_fds
[0]);
409 if (error
== 0 && uap
->status
)
410 error
= copyout(&status
, uap
->status
, sizeof(*uap
->status
));
411 if (error
== 0 && uap
->rusage
)
412 error
= copyout(&rusage
, uap
->rusage
, sizeof(*uap
->rusage
));
419 * wait_args(int pid, int *status, int options, struct rusage *rusage)
422 kern_wait(pid_t pid
, int *status
, int options
, struct rusage
*rusage
, int *res
)
424 struct thread
*td
= curthread
;
426 struct proc
*q
= td
->td_proc
;
432 if (options
&~ (WUNTRACED
|WNOHANG
|WLINUXCLONE
))
436 * Hack for backwards compatibility with badly written user code.
437 * Or perhaps we have to do this anyway, it is unclear. XXX
439 * The problem is that if a process group is stopped and the parent
440 * is doing a wait*(..., WUNTRACED, ...), it will see the STOP
441 * of the child and then stop itself when it tries to return from the
442 * system call. When the process group is resumed the parent will
443 * then get the STOP status even though the child has now resumed
444 * (a followup wait*() will get the CONT status).
446 * Previously the CONT would overwrite the STOP because the tstop
447 * was handled within tsleep(), and the parent would only see
448 * the CONT when both are stopped and continued together. This litte
449 * two-line hack restores this effect.
451 while (q
->p_stat
== SSTOP
)
455 LIST_FOREACH(p
, &q
->p_children
, p_sibling
) {
456 if (pid
!= WAIT_ANY
&&
457 p
->p_pid
!= pid
&& p
->p_pgid
!= -pid
)
460 /* This special case handles a kthread spawned by linux_clone
461 * (see linux_misc.c). The linux_wait4 and linux_waitpid
462 * functions need to be able to distinguish between waiting
463 * on a process and waiting on a thread. It is a thread if
464 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
465 * signifies we want to wait for threads and not processes.
467 if ((p
->p_sigparent
!= SIGCHLD
) ^
468 ((options
& WLINUXCLONE
) != 0)) {
473 if (p
->p_stat
== SZOMB
) {
474 deadlp
= ONLY_LWP_IN_PROC(p
);
477 * Other kernel threads may be in the middle of
478 * accessing the proc. For example, kern/kern_proc.c
479 * could be blocked writing proc data to a sysctl.
480 * At the moment, if this occurs, we are not woken
481 * up and rely on a one-second retry.
483 while (p
->p_lock
|| deadlp
->lwp_lock
) {
484 while (deadlp
->lwp_lock
)
485 tsleep(deadlp
, 0, "reap3l", hz
);
487 tsleep(p
, 0, "reap3", hz
);
489 lwkt_wait_free(deadlp
->lwp_thread
);
492 * The process's thread may still be in the middle
493 * of switching away, we can't rip its stack out from
494 * under it until TDF_EXITING is set and both
495 * TDF_RUNNING and TDF_PREEMPT_LOCK are clear.
496 * TDF_PREEMPT_LOCK must be checked because TDF_RUNNING
497 * will be cleared temporarily if a thread gets
500 * YYY no wakeup occurs so we depend on the timeout.
502 if ((deadlp
->lwp_thread
->td_flags
& (TDF_RUNNING
|TDF_PREEMPT_LOCK
|TDF_EXITING
)) != TDF_EXITING
) {
503 tsleep(deadlp
->lwp_thread
, 0, "reap2", 1);
507 /* scheduling hook for heuristic */
508 p
->p_usched
->heuristic_exiting(td
->td_lwp
, deadlp
);
510 /* Take care of our return values. */
513 *status
= p
->p_xstat
;
517 * If we got the child via a ptrace 'attach',
518 * we need to give it back to the old parent.
520 if (p
->p_oppid
&& (t
= pfind(p
->p_oppid
))) {
528 ruadd(&q
->p_cru
, &p
->p_ru
);
531 * Decrement the count of procs running with this uid.
533 chgproccnt(p
->p_ucred
->cr_ruidinfo
, -1, 0);
536 * Free up credentials.
542 * Remove unused arguments
544 if (p
->p_args
&& --p
->p_args
->ar_ref
== 0)
545 FREE(p
->p_args
, M_PARGS
);
548 * Finally finished with old proc entry.
549 * Unlink it from its process group and free it.
551 proc_remove_zombie(p
);
555 * Drain all references to the last lwp.
556 * Not sure if this is needed, but better safe than sorry.
558 while (deadlp
->lwp_lock
)
559 tsleep(deadlp
, 0, "reapl", hz
);
561 if (--p
->p_procsig
->ps_refcnt
== 0) {
562 if (p
->p_sigacts
!= &p
->p_addr
->u_sigacts
)
563 FREE(p
->p_sigacts
, M_SUBPROC
);
564 FREE(p
->p_procsig
, M_SUBPROC
);
569 zfree(lwp_zone
, deadlp
);
574 if (p
->p_stat
== SSTOP
&& (p
->p_flag
& P_WAITED
) == 0 &&
575 (p
->p_flag
& P_TRACED
|| options
& WUNTRACED
)) {
576 p
->p_flag
|= P_WAITED
;
580 *status
= W_STOPCODE(p
->p_xstat
);
581 /* Zero rusage so we get something consistent. */
583 bzero(rusage
, sizeof(rusage
));
589 if (options
& WNOHANG
) {
593 error
= tsleep((caddr_t
)q
, PCATCH
, "wait", 0);
600 * make process 'parent' the new parent of process 'child'.
603 proc_reparent(struct proc
*child
, struct proc
*parent
)
606 if (child
->p_pptr
== parent
)
609 LIST_REMOVE(child
, p_sibling
);
610 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
611 child
->p_pptr
= parent
;
615 * The next two functions are to handle adding/deleting items on the
619 * Take the arguments given and put them onto the exit callout list,
620 * However first make sure that it's not already there.
621 * returns 0 on success.
625 at_exit(exitlist_fn function
)
630 /* Be noisy if the programmer has lost track of things */
631 if (rm_at_exit(function
))
632 kprintf("WARNING: exit callout entry (%p) already present\n",
635 ep
= kmalloc(sizeof(*ep
), M_ATEXIT
, M_NOWAIT
);
638 ep
->function
= function
;
639 TAILQ_INSERT_TAIL(&exit_list
, ep
, next
);
644 * Scan the exit callout list for the given item and remove it.
645 * Returns the number of items removed (0 or 1)
648 rm_at_exit(exitlist_fn function
)
652 TAILQ_FOREACH(ep
, &exit_list
, next
) {
653 if (ep
->function
== function
) {
654 TAILQ_REMOVE(&exit_list
, ep
, next
);
665 struct proc
*p
= curproc
;
668 if (p
->p_procsig
->ps_refcnt
== 1 &&
669 p
->p_sigacts
!= &p
->p_addr
->u_sigacts
) {
672 p
->p_addr
->u_sigacts
= *pss
;
673 p
->p_sigacts
= &p
->p_addr
->u_sigacts
;
675 FREE(pss
, M_SUBPROC
);