]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
1fcd585aee2def3085c0f5044b4f1e36f8e64a47
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * File: bsd/kern/kern_shutdown.c
31 * Copyright (C) 1989, NeXT, Inc.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/proc_internal.h>
41 #include <sys/reboot.h>
43 #include <sys/vnode_internal.h>
44 #include <sys/file_internal.h>
45 #include <sys/clist.h>
46 #include <sys/callout.h>
48 #include <sys/msgbuf.h>
49 #include <sys/ioctl.h>
50 #include <sys/signal.h>
52 #include <kern/task.h>
53 #include <sys/quota.h>
54 #include <ufs/ufs/inode.h>
56 #include <kern/processor.h>
57 #include <kern/thread.h>
59 #endif /* NCPUS > 1 */
60 #include <vm/vm_kern.h>
61 #include <mach/vm_param.h>
62 #include <sys/filedesc.h>
63 #include <mach/host_priv.h>
64 #include <mach/host_reboot.h>
66 #include <bsm/audit_kernel.h>
69 static void proc_shutdown();
72 boot(paniced
, howto
, command
)
78 struct proc
*p
= current_proc(); /* XXX */
79 int hostboot_option
=0;
81 struct proc
*launchd_proc
;
83 extern void md_prepare_for_shutdown(int paniced
, int howto
, char * command
);
85 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
87 md_prepare_for_shutdown(paniced
, howto
, command
);
89 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
94 printf("syncing disks... ");
97 * Release vnodes held by texts before sync.
100 /* handle live procs (deallocate their root and current directories). */
105 sync(p
, (void *)NULL
, (int *)NULL
);
108 * Now that all processes have been termianted and system is sync'ed up,
112 launchd_proc
= pfind(1);
113 if (launchd_proc
&& p
!= launchd_proc
) {
114 task_suspend(launchd_proc
->task
);
118 * Unmount filesystems
122 /* Wait for the buffer cache to clean remaining dirty buffers */
123 for (iter
= 0; iter
< 100; iter
++) {
124 nbusy
= count_busy_buffers();
127 printf("%d ", nbusy
);
128 IOSleep( 1 * nbusy
);
131 printf("giving up\n");
137 * Can't just use an splnet() here to disable the network
138 * because that will lock out softints which the disk
139 * drivers depend on to finish DMAs.
143 if (howto
& RB_POWERDOWN
)
144 hostboot_option
= HOST_REBOOT_HALT
;
146 hostboot_option
= HOST_REBOOT_HALT
;
147 if (paniced
== RB_PANIC
)
148 hostboot_option
= HOST_REBOOT_HALT
;
151 * if we're going to power down due to a halt,
152 * give the disks a chance to finish getting
153 * the track cache flushed to the media...
154 * unfortunately, some of our earlier drives
155 * don't properly hold off on returning
156 * from the track flush command (issued by
157 * the unmounts) until it's actully fully
160 if (hostboot_option
== HOST_REBOOT_HALT
)
163 host_reboot(host_priv_self(), hostboot_option
);
165 thread_funnel_set(kernel_flock
, FALSE
);
171 * Shutdown down proc system (release references to current and root
172 * dirs for each process).
174 * POSIX modifications:
176 * For POSIX fcntl() file locking call vno_lockrelease() on
177 * the file to release all of its record locks, if any.
183 struct proc
*p
, *self
;
184 struct vnode
**cdirp
, **rdirp
, *vp
;
185 int restart
, i
, TERM_catch
;
189 * Kill as many procs as we can. (Except ourself...)
191 self
= (struct proc
*)current_proc();
194 * Signal the init with SIGTERM so that he does not launch
198 if (p
&& p
!= self
) {
202 printf("Killing all processes ");
205 * send SIGTERM to those procs interested in catching one
208 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
209 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
== 0)) {
211 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
214 if (p
->p_sigcatch
& sigmask(SIGTERM
)) {
215 p
->p_shutdownstate
= 1;
216 if (proc_refinternal(p
, 1) == p
) {
218 proc_dropinternal(p
, 1);
225 * now wait for up to 30 seconds to allow those procs catching SIGTERM
227 * as soon as these procs have exited, we'll continue on to the next step
229 for (i
= 0; i
< 300; i
++) {
231 * sleep for a tenth of a second
232 * and then check to see if the tasks that were sent a
233 * SIGTERM have exited
238 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
239 if (p
->p_shutdownstate
== 1) {
248 * log the names of the unresponsive tasks
251 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
252 if (p
->p_shutdownstate
== 1) {
253 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
260 * send a SIGKILL to all the procs still hanging around
263 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
264 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
!= 2)) {
266 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
269 if (proc_refinternal(p
, 1) == p
) {
271 proc_dropinternal(p
, 1);
273 p
->p_shutdownstate
= 2;
278 * wait for up to 60 seconds to allow these procs to exit normally
280 for (i
= 0; i
< 300; i
++) {
281 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
283 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
284 if (p
->p_shutdownstate
== 2)
292 * if we still have procs that haven't exited, then brute force 'em
294 p
= allproc
.lh_first
;
296 if ((p
->p_shutdownstate
== 3) || (p
->p_flag
&P_SYSTEM
) || (!delayterm
&& ((p
->p_lflag
& P_LDELAYTERM
)))
297 || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
298 p
= p
->p_list
.le_next
;
301 p
->p_shutdownstate
= 3;
303 * NOTE: following code ignores sig_lock and plays
304 * with exit_thread correctly. This is OK unless we
305 * are a multiprocessor, in which case I do not
306 * understand the sig_lock. This needs to be fixed.
309 if (p
->exit_thread
) { /* someone already doing it */
310 /* give him a chance */
311 thread_block(THREAD_CONTINUE_NULL
);
313 p
->exit_thread
= current_thread();
315 if (proc_refinternal(p
, 1) == p
) {
316 exit1(p
, 1, (int *)NULL
);
317 proc_dropinternal(p
, 1);
320 p
= allproc
.lh_first
;
326 /* Now start the termination of processes that are marked for delayed termn */
327 if (delayterm
== 0) {
331 printf("continuing\n");