]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * File: bsd/kern/kern_shutdown.c
25 * Copyright (C) 1989, NeXT, Inc.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/proc_internal.h>
35 #include <sys/reboot.h>
37 #include <sys/vnode_internal.h>
38 #include <sys/file_internal.h>
39 #include <sys/clist.h>
40 #include <sys/callout.h>
42 #include <sys/msgbuf.h>
43 #include <sys/ioctl.h>
44 #include <sys/signal.h>
46 #include <kern/task.h>
47 #include <sys/quota.h>
48 #include <ufs/ufs/inode.h>
50 #include <kern/processor.h>
51 #include <kern/thread.h>
53 #endif /* NCPUS > 1 */
54 #include <vm/vm_kern.h>
55 #include <mach/vm_param.h>
56 #include <sys/filedesc.h>
57 #include <mach/host_priv.h>
58 #include <mach/host_reboot.h>
60 #include <bsm/audit_kernel.h>
65 boot(paniced
, howto
, command
)
71 struct proc
*p
= current_proc(); /* XXX */
72 int hostboot_option
=0;
74 struct proc
*launchd_proc
;
76 static void proc_shutdown();
77 extern void md_prepare_for_shutdown(int paniced
, int howto
, char * command
);
79 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
81 md_prepare_for_shutdown(paniced
, howto
, command
);
83 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
88 printf("syncing disks... ");
91 * Release vnodes held by texts before sync.
94 /* handle live procs (deallocate their root and current directories). */
99 sync(p
, (void *)NULL
, (int *)NULL
);
102 * Now that all processes have been termianted and system is sync'ed up,
106 launchd_proc
= pfind(1);
107 if (launchd_proc
&& p
!= launchd_proc
) {
108 task_suspend(launchd_proc
->task
);
112 * Unmount filesystems
116 /* Wait for the buffer cache to clean remaining dirty buffers */
117 for (iter
= 0; iter
< 100; iter
++) {
118 nbusy
= count_busy_buffers();
121 printf("%d ", nbusy
);
122 IOSleep( 1 * nbusy
);
125 printf("giving up\n");
131 * Can't just use an splnet() here to disable the network
132 * because that will lock out softints which the disk
133 * drivers depend on to finish DMAs.
137 if (howto
& RB_POWERDOWN
)
138 hostboot_option
= HOST_REBOOT_HALT
;
140 hostboot_option
= HOST_REBOOT_HALT
;
141 if (paniced
== RB_PANIC
)
142 hostboot_option
= HOST_REBOOT_HALT
;
145 * if we're going to power down due to a halt,
146 * give the disks a chance to finish getting
147 * the track cache flushed to the media...
148 * unfortunately, some of our earlier drives
149 * don't properly hold off on returning
150 * from the track flush command (issued by
151 * the unmounts) until it's actully fully
154 if (hostboot_option
== HOST_REBOOT_HALT
)
157 host_reboot(host_priv_self(), hostboot_option
);
159 thread_funnel_set(kernel_flock
, FALSE
);
165 * Shutdown down proc system (release references to current and root
166 * dirs for each process).
168 * POSIX modifications:
170 * For POSIX fcntl() file locking call vno_lockrelease() on
171 * the file to release all of its record locks, if any.
177 struct proc
*p
, *self
;
178 struct vnode
**cdirp
, **rdirp
, *vp
;
179 int restart
, i
, TERM_catch
;
183 * Kill as many procs as we can. (Except ourself...)
185 self
= (struct proc
*)current_proc();
188 * Signal the init with SIGTERM so that he does not launch
192 if (p
&& p
!= self
) {
196 printf("Killing all processes ");
199 * send SIGTERM to those procs interested in catching one
202 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
203 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
== 0)) {
205 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
208 if (p
->p_sigcatch
& sigmask(SIGTERM
)) {
209 p
->p_shutdownstate
= 1;
217 * now wait for up to 30 seconds to allow those procs catching SIGTERM
219 * as soon as these procs have exited, we'll continue on to the next step
221 for (i
= 0; i
< 300; i
++) {
223 * sleep for a tenth of a second
224 * and then check to see if the tasks that were sent a
225 * SIGTERM have exited
230 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
231 if (p
->p_shutdownstate
== 1) {
240 * log the names of the unresponsive tasks
243 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
244 if (p
->p_shutdownstate
== 1) {
245 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
252 * send a SIGKILL to all the procs still hanging around
255 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
256 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
!= 2)) {
258 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
262 p
->p_shutdownstate
= 2;
267 * wait for up to 60 seconds to allow these procs to exit normally
269 for (i
= 0; i
< 300; i
++) {
270 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
272 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
273 if (p
->p_shutdownstate
== 2)
281 * if we still have procs that haven't exited, then brute force 'em
283 p
= allproc
.lh_first
;
285 if ((p
->p_flag
&P_SYSTEM
) || (!delayterm
&& ((p
->p_lflag
& P_LDELAYTERM
)))
286 || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
287 p
= p
->p_list
.le_next
;
291 * NOTE: following code ignores sig_lock and plays
292 * with exit_thread correctly. This is OK unless we
293 * are a multiprocessor, in which case I do not
294 * understand the sig_lock. This needs to be fixed.
297 if (p
->exit_thread
) { /* someone already doing it */
298 /* give him a chance */
299 thread_block(THREAD_CONTINUE_NULL
);
301 p
->exit_thread
= current_thread();
303 exit1(p
, 1, (int *)NULL
);
305 p
= allproc
.lh_first
;
311 /* Now start the termination of processes that are marked for delayed termn */
312 if (delayterm
== 0) {
316 printf("continuing\n");