]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
2 * Copyright (c) 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>
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>
59 #include <bsm/audit_kernel.h>
64 boot(paniced
, howto
, command
)
70 struct proc
*p
= current_proc(); /* XXX */
71 int hostboot_option
=0;
74 static void proc_shutdown();
75 extern void md_prepare_for_shutdown(int paniced
, int howto
, char * command
);
77 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
79 md_prepare_for_shutdown(paniced
, howto
, command
);
81 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
86 printf("syncing disks... ");
89 * Release vnodes held by texts before sync.
92 /* handle live procs (deallocate their root and current directories). */
97 sync(p
, (void *)NULL
, (int *)NULL
);
99 /* Release vnodes from the VM object cache */
105 * Unmount filesystems
110 /* Wait for the buffer cache to clean remaining dirty buffers */
111 for (iter
= 0; iter
< 20; iter
++) {
112 nbusy
= count_busy_buffers();
115 printf("%d ", nbusy
);
116 IOSleep( 4 * nbusy
);
119 printf("giving up\n");
125 * Can't just use an splnet() here to disable the network
126 * because that will lock out softints which the disk
127 * drivers depend on to finish DMAs.
131 if (howto
& RB_POWERDOWN
)
132 hostboot_option
= HOST_REBOOT_HALT
;
134 hostboot_option
= HOST_REBOOT_HALT
;
135 if (paniced
== RB_PANIC
)
136 hostboot_option
= HOST_REBOOT_HALT
;
138 if (hostboot_option
== HOST_REBOOT_HALT
)
141 host_reboot(host_priv_self(), hostboot_option
);
143 thread_funnel_set(kernel_flock
, FALSE
);
149 * Shutdown down proc system (release references to current and root
150 * dirs for each process).
152 * POSIX modifications:
154 * For POSIX fcntl() file locking call vno_lockrelease() on
155 * the file to release all of its record locks, if any.
161 struct proc
*p
, *self
;
162 struct vnode
**cdirp
, **rdirp
, *vp
;
163 int restart
, i
, TERM_catch
;
166 * Kill as many procs as we can. (Except ourself...)
168 self
= (struct proc
*)current_proc();
175 task_suspend(p
->task
); /* stop init */
177 printf("Killing all processes ");
180 * send SIGTERM to those procs interested in catching one
183 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
184 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_shutdownstate
== 0)) {
185 if (p
->p_sigcatch
& sigmask(SIGTERM
)) {
186 p
->p_shutdownstate
= 1;
194 * now wait for up to 30 seconds to allow those procs catching SIGTERM
196 * as soon as these procs have exited, we'll continue on to the next step
198 for (i
= 0; i
< 300; i
++) {
200 * sleep for a tenth of a second
201 * and then check to see if the tasks that were sent a
202 * SIGTERM have exited
207 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
208 if (p
->p_shutdownstate
== 1)
216 * log the names of the unresponsive tasks
218 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
219 if (p
->p_shutdownstate
== 1)
220 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
226 * send a SIGKILL to all the procs still hanging around
229 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
230 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_shutdownstate
!= 2)) {
232 p
->p_shutdownstate
= 2;
238 * wait for up to 60 seconds to allow these procs to exit normally
240 for (i
= 0; i
< 300; i
++) {
241 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
243 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
244 if (p
->p_shutdownstate
== 2)
252 * if we still have procs that haven't exited, then brute force 'em
254 p
= allproc
.lh_first
;
256 if ((p
->p_flag
&P_SYSTEM
) || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
257 p
= p
->p_list
.le_next
;
261 * NOTE: following code ignores sig_lock and plays
262 * with exit_thread correctly. This is OK unless we
263 * are a multiprocessor, in which case I do not
264 * understand the sig_lock. This needs to be fixed.
267 if (p
->exit_thread
) { /* someone already doing it */
268 /* give him a chance */
269 thread_block(THREAD_CONTINUE_NULL
);
272 p
->exit_thread
= current_act();
274 exit1(p
, 1, (int *)NULL
);
276 p
= allproc
.lh_first
;
281 * Forcibly free resources of what's left.
284 p
= allproc
.lh_first
;
287 * Close open files and release open-file table.
291 /* panics on reboot due to "zfree: non-allocated memory in collectable zone" message */
293 p
= p
->p_list
.le_next
;
296 /* Wait for the reaper thread to run, and clean up what we have done
297 * before we proceed with the hardcore shutdown. This reduces the race
298 * between kill_tasks and the reaper thread.
300 /* thread_wakeup(&reaper_queue); */
301 /* IOSleep( 1 * 1000); */
302 printf("continuing\n");