]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/kern_shutdown.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / kern / kern_shutdown.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * File: bsd/kern/kern_shutdown.c
30 *
31 * Copyright (C) 1989, NeXT, Inc.
32 *
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/vm.h>
39#include <sys/proc_internal.h>
40#include <sys/user.h>
41#include <sys/reboot.h>
42#include <sys/conf.h>
43#include <sys/vnode_internal.h>
44#include <sys/file_internal.h>
45#include <sys/clist.h>
46#include <sys/callout.h>
47#include <sys/mbuf.h>
48#include <sys/msgbuf.h>
49#include <sys/ioctl.h>
50#include <sys/signal.h>
51#include <sys/tty.h>
52#include <kern/task.h>
53#include <sys/quota.h>
54#include <ufs/ufs/inode.h>
55#if NCPUS > 1
56#include <kern/processor.h>
57#include <kern/thread.h>
58#include <sys/lock.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>
65
66#include <bsm/audit_kernel.h>
67
68int waittime = -1;
69static void proc_shutdown();
70
71void
72boot(paniced, howto, command)
73 int paniced, howto;
74 char *command;
75{
76 register int i;
77 int s;
78 struct proc *p = current_proc(); /* XXX */
79 int hostboot_option=0;
80 int funnel_state;
81 struct proc *launchd_proc;
82
83 extern void md_prepare_for_shutdown(int paniced, int howto, char * command);
84
85 funnel_state = thread_funnel_set(kernel_flock, TRUE);
86
87 md_prepare_for_shutdown(paniced, howto, command);
88
89 if ((howto&RB_NOSYNC)==0 && waittime < 0) {
90 int iter, nbusy;
91
92 waittime = 0;
93
94 printf("syncing disks... ");
95
96 /*
97 * Release vnodes held by texts before sync.
98 */
99
100 /* handle live procs (deallocate their root and current directories). */
101 proc_shutdown();
102
103 audit_shutdown();
104
105 sync(p, (void *)NULL, (int *)NULL);
106
107 /*
108 * Now that all processes have been termianted and system is sync'ed up,
109 * suspend launchd
110 */
111
112 launchd_proc = pfind(1);
113 if (launchd_proc && p != launchd_proc) {
114 task_suspend(launchd_proc->task);
115 }
116
117 /*
118 * Unmount filesystems
119 */
120 vfs_unmountall();
121
122 /* Wait for the buffer cache to clean remaining dirty buffers */
123 for (iter = 0; iter < 100; iter++) {
124 nbusy = count_busy_buffers();
125 if (nbusy == 0)
126 break;
127 printf("%d ", nbusy);
128 IOSleep( 1 * nbusy );
129 }
130 if (nbusy)
131 printf("giving up\n");
132 else
133 printf("done\n");
134 }
135
136 /*
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.
140 */
141 if_down_all();
142
143 if (howto & RB_POWERDOWN)
144 hostboot_option = HOST_REBOOT_HALT;
145 if (howto & RB_HALT)
146 hostboot_option = HOST_REBOOT_HALT;
147 if (paniced == RB_PANIC)
148 hostboot_option = HOST_REBOOT_HALT;
149
150 /*
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
158 * committed.
159 */
160 if (hostboot_option == HOST_REBOOT_HALT)
161 IOSleep( 1 * 1000 );
162
163 host_reboot(host_priv_self(), hostboot_option);
164
165 thread_funnel_set(kernel_flock, FALSE);
166}
167
168/*
169 * proc_shutdown()
170 *
171 * Shutdown down proc system (release references to current and root
172 * dirs for each process).
173 *
174 * POSIX modifications:
175 *
176 * For POSIX fcntl() file locking call vno_lockrelease() on
177 * the file to release all of its record locks, if any.
178 */
179
180static void
181proc_shutdown()
182{
183 struct proc *p, *self;
184 struct vnode **cdirp, **rdirp, *vp;
185 int restart, i, TERM_catch;
186 int delayterm = 0;
187
188 /*
189 * Kill as many procs as we can. (Except ourself...)
190 */
191 self = (struct proc *)current_proc();
192
193 /*
194 * Signal the init with SIGTERM so that he does not launch
195 * new processes
196 */
197 p = pfind(1);
198 if (p && p != self) {
199 psignal(p, SIGTERM);
200 }
201
202 printf("Killing all processes ");
203
204 /*
205 * send SIGTERM to those procs interested in catching one
206 */
207sigterm_loop:
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)) {
210
211 if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
212 continue;
213 }
214 if (p->p_sigcatch & sigmask(SIGTERM)) {
215 p->p_shutdownstate = 1;
216 if (proc_refinternal(p, 1) == p) {
217 psignal(p, SIGTERM);
218 proc_dropinternal(p, 1);
219 }
220 goto sigterm_loop;
221 }
222 }
223 }
224 /*
225 * now wait for up to 30 seconds to allow those procs catching SIGTERM
226 * to digest it
227 * as soon as these procs have exited, we'll continue on to the next step
228 */
229 for (i = 0; i < 300; i++) {
230 /*
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
234 */
235 IOSleep(100);
236 TERM_catch = 0;
237
238 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
239 if (p->p_shutdownstate == 1) {
240 TERM_catch++;
241 }
242 }
243 if (TERM_catch == 0)
244 break;
245 }
246 if (TERM_catch) {
247 /*
248 * log the names of the unresponsive tasks
249 */
250
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);
254 }
255 }
256 IOSleep(1000 * 5);
257 }
258
259 /*
260 * send a SIGKILL to all the procs still hanging around
261 */
262sigkill_loop:
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)) {
265
266 if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
267 continue;
268 }
269 if (proc_refinternal(p, 1) == p) {
270 psignal(p, SIGKILL);
271 proc_dropinternal(p, 1);
272 }
273 p->p_shutdownstate = 2;
274 goto sigkill_loop;
275 }
276 }
277 /*
278 * wait for up to 60 seconds to allow these procs to exit normally
279 */
280 for (i = 0; i < 300; i++) {
281 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
282
283 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
284 if (p->p_shutdownstate == 2)
285 break;
286 }
287 if (!p)
288 break;
289 }
290
291 /*
292 * if we still have procs that haven't exited, then brute force 'em
293 */
294 p = allproc.lh_first;
295 while (p) {
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;
299 }
300 else {
301 p->p_shutdownstate = 3;
302 /*
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.
307 * XXX
308 */
309 if (p->exit_thread) { /* someone already doing it */
310 /* give him a chance */
311 thread_block(THREAD_CONTINUE_NULL);
312 } else {
313 p->exit_thread = current_thread();
314 printf(".");
315 if (proc_refinternal(p, 1) == p) {
316 exit1(p, 1, (int *)NULL);
317 proc_dropinternal(p, 1);
318 }
319 }
320 p = allproc.lh_first;
321 }
322 }
323 printf("\n");
324
325
326 /* Now start the termination of processes that are marked for delayed termn */
327 if (delayterm == 0) {
328 delayterm = 1;
329 goto sigterm_loop;
330 }
331 printf("continuing\n");
332}
333