]>
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 <ufs/ufs/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();
75 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
77 /* md_prepare_for_shutdown(paniced, howto, command); */
79 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
84 printf("syncing disks... ");
87 * Release vnodes held by texts before sync.
90 /* handle live procs (deallocate their root and current directories). */
93 sync(p
, (void *)NULL
, (int *)NULL
);
95 /* Release vnodes from the VM object cache */
101 * Unmount filesystems
106 /* Wait for the buffer cache to clean remaining dirty buffers */
107 for (iter
= 0; iter
< 20; iter
++) {
108 nbusy
= count_busy_buffers();
111 printf("%d ", nbusy
);
112 IOSleep( 4 * nbusy
);
115 printf("giving up\n");
121 * Can't just use an splnet() here to disable the network
122 * because that will lock out softints which the disk
123 * drivers depend on to finish DMAs.
127 if (howto
& RB_POWERDOWN
)
128 hostboot_option
= HOST_REBOOT_HALT
;
130 hostboot_option
= HOST_REBOOT_HALT
;
131 if (paniced
== RB_PANIC
)
132 hostboot_option
= HOST_REBOOT_HALT
;
134 if (hostboot_option
== HOST_REBOOT_HALT
)
137 host_reboot(host_priv_self(), hostboot_option
);
139 thread_funnel_set(kernel_flock
, FALSE
);
145 * Shutdown down proc system (release references to current and root
146 * dirs for each process).
148 * POSIX modifications:
150 * For POSIX fcntl() file locking call vno_lockrelease() on
151 * the file to release all of its record locks, if any.
157 struct proc
*p
, *self
;
158 struct vnode
**cdirp
, **rdirp
, *vp
;
159 int restart
, i
, TERM_catch
;
162 * Kill as many procs as we can. (Except ourself...)
164 self
= (struct proc
*)(get_bsdtask_info(current_task()));
171 task_suspend(p
->task
); /* stop init */
178 task_suspend(p
->task
); /* stop mach_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 * send a SIGKILL to all the procs still hanging around
218 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
219 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
))
223 * wait for up to 60 seconds to allow these procs to exit normally
225 for (i
= 0; i
< 300; i
++) {
226 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
228 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
229 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
))
237 * if we still have procs that haven't exited, then brute force 'em
239 p
= allproc
.lh_first
;
241 if ((p
->p_flag
&P_SYSTEM
) || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
242 p
= p
->p_list
.le_next
;
246 * NOTE: following code ignores sig_lock and plays
247 * with exit_thread correctly. This is OK unless we
248 * are a multiprocessor, in which case I do not
249 * understand the sig_lock. This needs to be fixed.
252 if (p
->exit_thread
) { /* someone already doing it */
253 thread_block(0);/* give him a chance */
256 p
->exit_thread
= current_thread();
260 p
= allproc
.lh_first
;
265 * Forcibly free resources of what's left.
267 p
= allproc
.lh_first
;
270 * Close open files and release open-file table.
274 /* panics on reboot due to "zfree: non-allocated memory in collectable zone" message */
277 p
= p
->p_list
.le_next
;
279 /* Wait for the reaper thread to run, and clean up what we have done
280 * before we proceed with the hardcore shutdown. This reduces the race
281 * between kill_tasks and the reaper thread.
283 /* thread_wakeup(&reaper_queue); */
284 /* IOSleep( 1 * 1000); */
285 printf("continuing\n");