]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
2 * Copyright (c) 2000 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>
36 #include <sys/reboot.h>
38 #include <sys/vnode.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_reboot.h>
63 boot(paniced
, howto
, command
)
69 struct proc
*p
= current_proc(); /* XXX */
70 int hostboot_option
=0;
73 static void proc_shutdown();
74 extern void md_prepare_for_shutdown(int paniced
, int howto
, char * command
);
76 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
78 md_prepare_for_shutdown(paniced
, howto
, command
);
80 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
85 printf("syncing disks... ");
88 * Release vnodes held by texts before sync.
91 /* handle live procs (deallocate their root and current directories). */
94 sync(p
, (void *)NULL
, (int *)NULL
);
96 /* Release vnodes from the VM object cache */
102 * Unmount filesystems
107 /* Wait for the buffer cache to clean remaining dirty buffers */
108 for (iter
= 0; iter
< 20; iter
++) {
109 nbusy
= count_busy_buffers();
112 printf("%d ", nbusy
);
113 IOSleep( 4 * nbusy
);
116 printf("giving up\n");
122 * Can't just use an splnet() here to disable the network
123 * because that will lock out softints which the disk
124 * drivers depend on to finish DMAs.
128 if (howto
& RB_POWERDOWN
)
129 hostboot_option
= HOST_REBOOT_HALT
;
131 hostboot_option
= HOST_REBOOT_HALT
;
132 if (paniced
== RB_PANIC
)
133 hostboot_option
= HOST_REBOOT_HALT
;
135 if (hostboot_option
== HOST_REBOOT_HALT
)
138 host_reboot(host_priv_self(), hostboot_option
);
140 thread_funnel_set(kernel_flock
, FALSE
);
146 * Shutdown down proc system (release references to current and root
147 * dirs for each process).
149 * POSIX modifications:
151 * For POSIX fcntl() file locking call vno_lockrelease() on
152 * the file to release all of its record locks, if any.
158 struct proc
*p
, *self
;
159 struct vnode
**cdirp
, **rdirp
, *vp
;
160 int restart
, i
, TERM_catch
;
163 * Kill as many procs as we can. (Except ourself...)
165 self
= (struct proc
*)current_proc();
172 task_suspend(p
->task
); /* stop init */
174 printf("Killing all processes ");
177 * send SIGTERM to those procs interested in catching one
179 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
180 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
)) {
181 if (p
->p_sigcatch
& sigmask(SIGTERM
))
186 * now wait for up to 30 seconds to allow those procs catching SIGTERM
188 * as soon as these procs have exited, we'll continue on to the next step
190 for (i
= 0; i
< 300; i
++) {
192 * sleep for a tenth of a second
193 * and then check to see if the tasks that were sent a
194 * SIGTERM have exited
199 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
200 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
)) {
201 if (p
->p_sigcatch
& sigmask(SIGTERM
))
210 * send a SIGKILL to all the procs still hanging around
212 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
213 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
))
217 * wait for up to 60 seconds to allow these procs to exit normally
219 for (i
= 0; i
< 300; i
++) {
220 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
222 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
223 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
))
231 * if we still have procs that haven't exited, then brute force 'em
233 p
= allproc
.lh_first
;
235 if ((p
->p_flag
&P_SYSTEM
) || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
236 p
= p
->p_list
.le_next
;
240 * NOTE: following code ignores sig_lock and plays
241 * with exit_thread correctly. This is OK unless we
242 * are a multiprocessor, in which case I do not
243 * understand the sig_lock. This needs to be fixed.
246 if (p
->exit_thread
) { /* someone already doing it */
247 /* give him a chance */
248 thread_block(THREAD_CONTINUE_NULL
);
251 p
->exit_thread
= current_thread();
253 exit1(p
, 1, (int *)NULL
);
255 p
= allproc
.lh_first
;
260 * Forcibly free resources of what's left.
262 p
= allproc
.lh_first
;
265 * Close open files and release open-file table.
269 /* panics on reboot due to "zfree: non-allocated memory in collectable zone" message */
272 p
= p
->p_list
.le_next
;
274 /* Wait for the reaper thread to run, and clean up what we have done
275 * before we proceed with the hardcore shutdown. This reduces the race
276 * between kill_tasks and the reaper thread.
278 /* thread_wakeup(&reaper_queue); */
279 /* IOSleep( 1 * 1000); */
280 printf("continuing\n");