]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * File: bsd/kern/kern_shutdown.c
28 * Copyright (C) 1989, NeXT, Inc.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
39 #include <sys/reboot.h>
41 #include <sys/vnode.h>
43 #include <sys/clist.h>
44 #include <sys/callout.h>
46 #include <sys/msgbuf.h>
47 #include <sys/ioctl.h>
48 #include <sys/signal.h>
50 #include <kern/task.h>
51 #include <sys/quota.h>
52 #include <ufs/ufs/inode.h>
54 #include <kern/processor.h>
55 #include <kern/thread.h>
57 #endif /* NCPUS > 1 */
58 #include <vm/vm_kern.h>
59 #include <mach/vm_param.h>
60 #include <sys/filedesc.h>
61 #include <mach/host_reboot.h>
62 #include <sys/kern_audit.h>
67 boot(paniced
, howto
, command
)
73 struct proc
*p
= current_proc(); /* XXX */
74 int hostboot_option
=0;
77 static void proc_shutdown();
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
);
102 /* Release vnodes from the VM object cache */
108 * Unmount filesystems
113 /* Wait for the buffer cache to clean remaining dirty buffers */
114 for (iter
= 0; iter
< 20; iter
++) {
115 nbusy
= count_busy_buffers();
118 printf("%d ", nbusy
);
119 IOSleep( 4 * nbusy
);
122 printf("giving up\n");
128 * Can't just use an splnet() here to disable the network
129 * because that will lock out softints which the disk
130 * drivers depend on to finish DMAs.
134 if (howto
& RB_POWERDOWN
)
135 hostboot_option
= HOST_REBOOT_HALT
;
137 hostboot_option
= HOST_REBOOT_HALT
;
138 if (paniced
== RB_PANIC
)
139 hostboot_option
= HOST_REBOOT_HALT
;
141 if (hostboot_option
== HOST_REBOOT_HALT
)
144 host_reboot(host_priv_self(), hostboot_option
);
146 thread_funnel_set(kernel_flock
, FALSE
);
152 * Shutdown down proc system (release references to current and root
153 * dirs for each process).
155 * POSIX modifications:
157 * For POSIX fcntl() file locking call vno_lockrelease() on
158 * the file to release all of its record locks, if any.
164 struct proc
*p
, *self
;
165 struct vnode
**cdirp
, **rdirp
, *vp
;
166 int restart
, i
, TERM_catch
;
169 * Kill as many procs as we can. (Except ourself...)
171 self
= (struct proc
*)current_proc();
178 task_suspend(p
->task
); /* stop init */
180 printf("Killing all processes ");
183 * send SIGTERM to those procs interested in catching one
185 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
186 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
)) {
187 if (p
->p_sigcatch
& sigmask(SIGTERM
))
192 * now wait for up to 30 seconds to allow those procs catching SIGTERM
194 * as soon as these procs have exited, we'll continue on to the next step
196 for (i
= 0; i
< 300; i
++) {
198 * sleep for a tenth of a second
199 * and then check to see if the tasks that were sent a
200 * SIGTERM have exited
205 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
206 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
)) {
207 if (p
->p_sigcatch
& sigmask(SIGTERM
))
216 * log the names of the unresponsive tasks
219 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
220 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
)) {
221 if (p
->p_sigcatch
& sigmask(SIGTERM
))
222 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
229 * send a SIGKILL to all the procs still hanging around
231 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
232 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
))
236 * wait for up to 60 seconds to allow these procs to exit normally
238 for (i
= 0; i
< 300; i
++) {
239 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
241 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
242 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
))
250 * if we still have procs that haven't exited, then brute force 'em
252 p
= allproc
.lh_first
;
254 if ((p
->p_flag
&P_SYSTEM
) || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
255 p
= p
->p_list
.le_next
;
259 * NOTE: following code ignores sig_lock and plays
260 * with exit_thread correctly. This is OK unless we
261 * are a multiprocessor, in which case I do not
262 * understand the sig_lock. This needs to be fixed.
265 if (p
->exit_thread
) { /* someone already doing it */
266 /* give him a chance */
267 thread_block(THREAD_CONTINUE_NULL
);
270 p
->exit_thread
= current_act();
272 exit1(p
, 1, (int *)NULL
);
274 p
= allproc
.lh_first
;
279 * Forcibly free resources of what's left.
281 p
= allproc
.lh_first
;
284 * Close open files and release open-file table.
288 /* panics on reboot due to "zfree: non-allocated memory in collectable zone" message */
291 p
= p
->p_list
.le_next
;
293 /* Wait for the reaper thread to run, and clean up what we have done
294 * before we proceed with the hardcore shutdown. This reduces the race
295 * between kill_tasks and the reaper thread.
297 /* thread_wakeup(&reaper_queue); */
298 /* IOSleep( 1 * 1000); */
299 printf("continuing\n");