]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * File: bsd/kern/kern_shutdown.c
26 * Copyright (C) 1989, NeXT, Inc.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/proc_internal.h>
36 #include <sys/reboot.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/file_internal.h>
40 #include <sys/clist.h>
41 #include <sys/callout.h>
43 #include <sys/msgbuf.h>
44 #include <sys/ioctl.h>
45 #include <sys/signal.h>
47 #include <kern/task.h>
48 #include <sys/quota.h>
49 #include <ufs/ufs/inode.h>
51 #include <kern/processor.h>
52 #include <kern/thread.h>
54 #endif /* NCPUS > 1 */
55 #include <vm/vm_kern.h>
56 #include <mach/vm_param.h>
57 #include <sys/filedesc.h>
58 #include <mach/host_priv.h>
59 #include <mach/host_reboot.h>
61 #include <bsm/audit_kernel.h>
64 static void proc_shutdown();
67 boot(paniced
, howto
, command
)
73 struct proc
*p
= current_proc(); /* XXX */
74 int hostboot_option
=0;
76 struct proc
*launchd_proc
;
78 extern void md_prepare_for_shutdown(int paniced
, int howto
, char * command
);
80 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
82 md_prepare_for_shutdown(paniced
, howto
, command
);
84 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
89 printf("syncing disks... ");
92 * Release vnodes held by texts before sync.
95 /* handle live procs (deallocate their root and current directories). */
100 sync(p
, (void *)NULL
, (int *)NULL
);
103 * Now that all processes have been termianted and system is sync'ed up,
107 launchd_proc
= pfind(1);
108 if (launchd_proc
&& p
!= launchd_proc
) {
109 task_suspend(launchd_proc
->task
);
113 * Unmount filesystems
117 /* Wait for the buffer cache to clean remaining dirty buffers */
118 for (iter
= 0; iter
< 100; iter
++) {
119 nbusy
= count_busy_buffers();
122 printf("%d ", nbusy
);
123 IOSleep( 1 * nbusy
);
126 printf("giving up\n");
132 * Can't just use an splnet() here to disable the network
133 * because that will lock out softints which the disk
134 * drivers depend on to finish DMAs.
138 if (howto
& RB_POWERDOWN
)
139 hostboot_option
= HOST_REBOOT_HALT
;
141 hostboot_option
= HOST_REBOOT_HALT
;
142 if (paniced
== RB_PANIC
)
143 hostboot_option
= HOST_REBOOT_HALT
;
146 * if we're going to power down due to a halt,
147 * give the disks a chance to finish getting
148 * the track cache flushed to the media...
149 * unfortunately, some of our earlier drives
150 * don't properly hold off on returning
151 * from the track flush command (issued by
152 * the unmounts) until it's actully fully
155 if (hostboot_option
== HOST_REBOOT_HALT
)
158 host_reboot(host_priv_self(), hostboot_option
);
160 thread_funnel_set(kernel_flock
, FALSE
);
166 * Shutdown down proc system (release references to current and root
167 * dirs for each process).
169 * POSIX modifications:
171 * For POSIX fcntl() file locking call vno_lockrelease() on
172 * the file to release all of its record locks, if any.
178 struct proc
*p
, *self
;
179 struct vnode
**cdirp
, **rdirp
, *vp
;
180 int restart
, i
, TERM_catch
;
184 * Kill as many procs as we can. (Except ourself...)
186 self
= (struct proc
*)current_proc();
189 * Signal the init with SIGTERM so that he does not launch
193 if (p
&& p
!= self
) {
197 printf("Killing all processes ");
200 * send SIGTERM to those procs interested in catching one
203 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
204 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
== 0)) {
206 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
209 if (p
->p_sigcatch
& sigmask(SIGTERM
)) {
210 p
->p_shutdownstate
= 1;
211 if (proc_refinternal(p
, 1) == p
) {
213 proc_dropinternal(p
, 1);
220 * now wait for up to 30 seconds to allow those procs catching SIGTERM
222 * as soon as these procs have exited, we'll continue on to the next step
224 for (i
= 0; i
< 300; i
++) {
226 * sleep for a tenth of a second
227 * and then check to see if the tasks that were sent a
228 * SIGTERM have exited
233 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
234 if (p
->p_shutdownstate
== 1) {
243 * log the names of the unresponsive tasks
246 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
247 if (p
->p_shutdownstate
== 1) {
248 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
255 * send a SIGKILL to all the procs still hanging around
258 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
259 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
!= 2)) {
261 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
264 if (proc_refinternal(p
, 1) == p
) {
266 proc_dropinternal(p
, 1);
268 p
->p_shutdownstate
= 2;
273 * wait for up to 60 seconds to allow these procs to exit normally
275 for (i
= 0; i
< 300; i
++) {
276 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
278 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
279 if (p
->p_shutdownstate
== 2)
287 * if we still have procs that haven't exited, then brute force 'em
289 p
= allproc
.lh_first
;
291 if ((p
->p_shutdownstate
== 3) || (p
->p_flag
&P_SYSTEM
) || (!delayterm
&& ((p
->p_lflag
& P_LDELAYTERM
)))
292 || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
293 p
= p
->p_list
.le_next
;
296 p
->p_shutdownstate
= 3;
298 * NOTE: following code ignores sig_lock and plays
299 * with exit_thread correctly. This is OK unless we
300 * are a multiprocessor, in which case I do not
301 * understand the sig_lock. This needs to be fixed.
304 if (p
->exit_thread
) { /* someone already doing it */
305 /* give him a chance */
306 thread_block(THREAD_CONTINUE_NULL
);
308 p
->exit_thread
= current_thread();
310 if (proc_refinternal(p
, 1) == p
) {
311 exit1(p
, 1, (int *)NULL
);
312 proc_dropinternal(p
, 1);
315 p
= allproc
.lh_first
;
321 /* Now start the termination of processes that are marked for delayed termn */
322 if (delayterm
== 0) {
326 printf("continuing\n");