]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
d31699e5357647f2bf6987c1c235cbff782444fe
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * File: bsd/kern/kern_shutdown.c
33 * Copyright (C) 1989, NeXT, Inc.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/proc_internal.h>
43 #include <sys/reboot.h>
45 #include <sys/vnode_internal.h>
46 #include <sys/file_internal.h>
47 #include <sys/clist.h>
48 #include <sys/callout.h>
50 #include <sys/msgbuf.h>
51 #include <sys/ioctl.h>
52 #include <sys/signal.h>
54 #include <kern/task.h>
55 #include <sys/quota.h>
56 #include <ufs/ufs/inode.h>
58 #include <kern/processor.h>
59 #include <kern/thread.h>
61 #endif /* NCPUS > 1 */
62 #include <vm/vm_kern.h>
63 #include <mach/vm_param.h>
64 #include <sys/filedesc.h>
65 #include <mach/host_priv.h>
66 #include <mach/host_reboot.h>
68 #include <bsm/audit_kernel.h>
71 static void proc_shutdown();
74 boot(paniced
, howto
, command
)
80 struct proc
*p
= current_proc(); /* XXX */
81 int hostboot_option
=0;
83 struct proc
*launchd_proc
;
85 extern void md_prepare_for_shutdown(int paniced
, int howto
, char * command
);
87 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
89 md_prepare_for_shutdown(paniced
, howto
, command
);
91 if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
96 printf("syncing disks... ");
99 * Release vnodes held by texts before sync.
102 /* handle live procs (deallocate their root and current directories). */
107 sync(p
, (void *)NULL
, (int *)NULL
);
110 * Now that all processes have been termianted and system is sync'ed up,
114 launchd_proc
= pfind(1);
115 if (launchd_proc
&& p
!= launchd_proc
) {
116 task_suspend(launchd_proc
->task
);
120 * Unmount filesystems
124 /* Wait for the buffer cache to clean remaining dirty buffers */
125 for (iter
= 0; iter
< 100; iter
++) {
126 nbusy
= count_busy_buffers();
129 printf("%d ", nbusy
);
130 IOSleep( 1 * nbusy
);
133 printf("giving up\n");
139 * Can't just use an splnet() here to disable the network
140 * because that will lock out softints which the disk
141 * drivers depend on to finish DMAs.
145 if (howto
& RB_POWERDOWN
)
146 hostboot_option
= HOST_REBOOT_HALT
;
148 hostboot_option
= HOST_REBOOT_HALT
;
149 if (paniced
== RB_PANIC
)
150 hostboot_option
= HOST_REBOOT_HALT
;
153 * if we're going to power down due to a halt,
154 * give the disks a chance to finish getting
155 * the track cache flushed to the media...
156 * unfortunately, some of our earlier drives
157 * don't properly hold off on returning
158 * from the track flush command (issued by
159 * the unmounts) until it's actully fully
162 if (hostboot_option
== HOST_REBOOT_HALT
)
165 host_reboot(host_priv_self(), hostboot_option
);
167 thread_funnel_set(kernel_flock
, FALSE
);
173 * Shutdown down proc system (release references to current and root
174 * dirs for each process).
176 * POSIX modifications:
178 * For POSIX fcntl() file locking call vno_lockrelease() on
179 * the file to release all of its record locks, if any.
185 struct proc
*p
, *self
;
186 struct vnode
**cdirp
, **rdirp
, *vp
;
187 int restart
, i
, TERM_catch
;
191 * Kill as many procs as we can. (Except ourself...)
193 self
= (struct proc
*)current_proc();
196 * Signal the init with SIGTERM so that he does not launch
200 if (p
&& p
!= self
) {
204 printf("Killing all processes ");
207 * send SIGTERM to those procs interested in catching one
210 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
211 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
== 0)) {
213 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
216 if (p
->p_sigcatch
& sigmask(SIGTERM
)) {
217 p
->p_shutdownstate
= 1;
218 if (proc_refinternal(p
, 1) == p
) {
220 proc_dropinternal(p
, 1);
227 * now wait for up to 30 seconds to allow those procs catching SIGTERM
229 * as soon as these procs have exited, we'll continue on to the next step
231 for (i
= 0; i
< 300; i
++) {
233 * sleep for a tenth of a second
234 * and then check to see if the tasks that were sent a
235 * SIGTERM have exited
240 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
241 if (p
->p_shutdownstate
== 1) {
250 * log the names of the unresponsive tasks
253 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
254 if (p
->p_shutdownstate
== 1) {
255 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
262 * send a SIGKILL to all the procs still hanging around
265 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
266 if (((p
->p_flag
&P_SYSTEM
) == 0) && (p
->p_pptr
->p_pid
!= 0) && (p
!= self
) && (p
->p_stat
!= SZOMB
) && (p
->p_shutdownstate
!= 2)) {
268 if ((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
)) {
271 if (proc_refinternal(p
, 1) == p
) {
273 proc_dropinternal(p
, 1);
275 p
->p_shutdownstate
= 2;
280 * wait for up to 60 seconds to allow these procs to exit normally
282 for (i
= 0; i
< 300; i
++) {
283 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
285 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
286 if (p
->p_shutdownstate
== 2)
294 * if we still have procs that haven't exited, then brute force 'em
296 p
= allproc
.lh_first
;
298 if ((p
->p_shutdownstate
== 3) || (p
->p_flag
&P_SYSTEM
) || (!delayterm
&& ((p
->p_lflag
& P_LDELAYTERM
)))
299 || (p
->p_pptr
->p_pid
== 0) || (p
== self
)) {
300 p
= p
->p_list
.le_next
;
303 p
->p_shutdownstate
= 3;
305 * NOTE: following code ignores sig_lock and plays
306 * with exit_thread correctly. This is OK unless we
307 * are a multiprocessor, in which case I do not
308 * understand the sig_lock. This needs to be fixed.
311 if (p
->exit_thread
) { /* someone already doing it */
312 /* give him a chance */
313 thread_block(THREAD_CONTINUE_NULL
);
315 p
->exit_thread
= current_thread();
317 if (proc_refinternal(p
, 1) == p
) {
318 exit1(p
, 1, (int *)NULL
);
319 proc_dropinternal(p
, 1);
322 p
= allproc
.lh_first
;
328 /* Now start the termination of processes that are marked for delayed termn */
329 if (delayterm
== 0) {
333 printf("continuing\n");