]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
xnu-792.tar.gz
[apple/xnu.git] / bsd / kern / kern_shutdown.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * File: bsd/kern/kern_shutdown.c
24 *
25 * Copyright (C) 1989, NeXT, Inc.
26 *
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/vm.h>
33 #include <sys/proc_internal.h>
34 #include <sys/user.h>
35 #include <sys/reboot.h>
36 #include <sys/conf.h>
37 #include <sys/vnode_internal.h>
38 #include <sys/file_internal.h>
39 #include <sys/clist.h>
40 #include <sys/callout.h>
41 #include <sys/mbuf.h>
42 #include <sys/msgbuf.h>
43 #include <sys/ioctl.h>
44 #include <sys/signal.h>
45 #include <sys/tty.h>
46 #include <kern/task.h>
47 #include <sys/quota.h>
48 #include <ufs/ufs/inode.h>
49 #if NCPUS > 1
50 #include <kern/processor.h>
51 #include <kern/thread.h>
52 #include <sys/lock.h>
53 #endif /* NCPUS > 1 */
54 #include <vm/vm_kern.h>
55 #include <mach/vm_param.h>
56 #include <sys/filedesc.h>
57 #include <mach/host_priv.h>
58 #include <mach/host_reboot.h>
59
60 #include <bsm/audit_kernel.h>
61
62 int waittime = -1;
63
64 void
65 boot(paniced, howto, command)
66 int paniced, howto;
67 char *command;
68 {
69 register int i;
70 int s;
71 struct proc *p = current_proc(); /* XXX */
72 int hostboot_option=0;
73 int funnel_state;
74 struct proc *launchd_proc;
75
76 static void proc_shutdown();
77 extern void md_prepare_for_shutdown(int paniced, int howto, char * command);
78
79 funnel_state = thread_funnel_set(kernel_flock, TRUE);
80
81 md_prepare_for_shutdown(paniced, howto, command);
82
83 if ((howto&RB_NOSYNC)==0 && waittime < 0) {
84 int iter, nbusy;
85
86 waittime = 0;
87
88 printf("syncing disks... ");
89
90 /*
91 * Release vnodes held by texts before sync.
92 */
93
94 /* handle live procs (deallocate their root and current directories). */
95 proc_shutdown();
96
97 audit_shutdown();
98
99 sync(p, (void *)NULL, (int *)NULL);
100
101 /*
102 * Now that all processes have been termianted and system is sync'ed up,
103 * suspend launchd
104 */
105
106 launchd_proc = pfind(1);
107 if (launchd_proc && p != launchd_proc) {
108 task_suspend(launchd_proc->task);
109 }
110
111 /*
112 * Unmount filesystems
113 */
114 vfs_unmountall();
115
116 /* Wait for the buffer cache to clean remaining dirty buffers */
117 for (iter = 0; iter < 100; iter++) {
118 nbusy = count_busy_buffers();
119 if (nbusy == 0)
120 break;
121 printf("%d ", nbusy);
122 IOSleep( 1 * nbusy );
123 }
124 if (nbusy)
125 printf("giving up\n");
126 else
127 printf("done\n");
128 }
129
130 /*
131 * Can't just use an splnet() here to disable the network
132 * because that will lock out softints which the disk
133 * drivers depend on to finish DMAs.
134 */
135 if_down_all();
136
137 if (howto & RB_POWERDOWN)
138 hostboot_option = HOST_REBOOT_HALT;
139 if (howto & RB_HALT)
140 hostboot_option = HOST_REBOOT_HALT;
141 if (paniced == RB_PANIC)
142 hostboot_option = HOST_REBOOT_HALT;
143
144 /*
145 * if we're going to power down due to a halt,
146 * give the disks a chance to finish getting
147 * the track cache flushed to the media...
148 * unfortunately, some of our earlier drives
149 * don't properly hold off on returning
150 * from the track flush command (issued by
151 * the unmounts) until it's actully fully
152 * committed.
153 */
154 if (hostboot_option == HOST_REBOOT_HALT)
155 IOSleep( 1 * 1000 );
156
157 host_reboot(host_priv_self(), hostboot_option);
158
159 thread_funnel_set(kernel_flock, FALSE);
160 }
161
162 /*
163 * proc_shutdown()
164 *
165 * Shutdown down proc system (release references to current and root
166 * dirs for each process).
167 *
168 * POSIX modifications:
169 *
170 * For POSIX fcntl() file locking call vno_lockrelease() on
171 * the file to release all of its record locks, if any.
172 */
173
174 static void
175 proc_shutdown()
176 {
177 struct proc *p, *self;
178 struct vnode **cdirp, **rdirp, *vp;
179 int restart, i, TERM_catch;
180 int delayterm = 0;
181
182 /*
183 * Kill as many procs as we can. (Except ourself...)
184 */
185 self = (struct proc *)current_proc();
186
187 /*
188 * Signal the init with SIGTERM so that he does not launch
189 * new processes
190 */
191 p = pfind(1);
192 if (p && p != self) {
193 psignal(p, SIGTERM);
194 }
195
196 printf("Killing all processes ");
197
198 /*
199 * send SIGTERM to those procs interested in catching one
200 */
201 sigterm_loop:
202 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
203 if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_stat != SZOMB) && (p->p_shutdownstate == 0)) {
204
205 if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
206 continue;
207 }
208 if (p->p_sigcatch & sigmask(SIGTERM)) {
209 p->p_shutdownstate = 1;
210 psignal(p, SIGTERM);
211
212 goto sigterm_loop;
213 }
214 }
215 }
216 /*
217 * now wait for up to 30 seconds to allow those procs catching SIGTERM
218 * to digest it
219 * as soon as these procs have exited, we'll continue on to the next step
220 */
221 for (i = 0; i < 300; i++) {
222 /*
223 * sleep for a tenth of a second
224 * and then check to see if the tasks that were sent a
225 * SIGTERM have exited
226 */
227 IOSleep(100);
228 TERM_catch = 0;
229
230 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
231 if (p->p_shutdownstate == 1) {
232 TERM_catch++;
233 }
234 }
235 if (TERM_catch == 0)
236 break;
237 }
238 if (TERM_catch) {
239 /*
240 * log the names of the unresponsive tasks
241 */
242
243 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
244 if (p->p_shutdownstate == 1) {
245 printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid);
246 }
247 }
248 IOSleep(1000 * 5);
249 }
250
251 /*
252 * send a SIGKILL to all the procs still hanging around
253 */
254 sigkill_loop:
255 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
256 if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_stat != SZOMB) && (p->p_shutdownstate != 2)) {
257
258 if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
259 continue;
260 }
261 psignal(p, SIGKILL);
262 p->p_shutdownstate = 2;
263 goto sigkill_loop;
264 }
265 }
266 /*
267 * wait for up to 60 seconds to allow these procs to exit normally
268 */
269 for (i = 0; i < 300; i++) {
270 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
271
272 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
273 if (p->p_shutdownstate == 2)
274 break;
275 }
276 if (!p)
277 break;
278 }
279
280 /*
281 * if we still have procs that haven't exited, then brute force 'em
282 */
283 p = allproc.lh_first;
284 while (p) {
285 if ((p->p_flag&P_SYSTEM) || (!delayterm && ((p->p_lflag& P_LDELAYTERM)))
286 || (p->p_pptr->p_pid == 0) || (p == self)) {
287 p = p->p_list.le_next;
288 }
289 else {
290 /*
291 * NOTE: following code ignores sig_lock and plays
292 * with exit_thread correctly. This is OK unless we
293 * are a multiprocessor, in which case I do not
294 * understand the sig_lock. This needs to be fixed.
295 * XXX
296 */
297 if (p->exit_thread) { /* someone already doing it */
298 /* give him a chance */
299 thread_block(THREAD_CONTINUE_NULL);
300 } else {
301 p->exit_thread = current_thread();
302 printf(".");
303 exit1(p, 1, (int *)NULL);
304 }
305 p = allproc.lh_first;
306 }
307 }
308 printf("\n");
309
310
311 /* Now start the termination of processes that are marked for delayed termn */
312 if (delayterm == 0) {
313 delayterm = 1;
314 goto sigterm_loop;
315 }
316 printf("continuing\n");
317 }
318