]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_shutdown.c
xnu-792.6.76.tar.gz
[apple/xnu.git] / bsd / kern / kern_shutdown.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
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.
1c79356b
A
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>
91447636 33#include <sys/proc_internal.h>
1c79356b 34#include <sys/user.h>
1c79356b
A
35#include <sys/reboot.h>
36#include <sys/conf.h>
91447636
A
37#include <sys/vnode_internal.h>
38#include <sys/file_internal.h>
1c79356b
A
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>
9bccf70c 47#include <sys/quota.h>
1c79356b
A
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>
91447636 57#include <mach/host_priv.h>
1c79356b 58#include <mach/host_reboot.h>
91447636 59
e5568f75 60#include <bsm/audit_kernel.h>
1c79356b
A
61
62int waittime = -1;
ff6e181a 63static void proc_shutdown();
1c79356b
A
64
65void
66boot(paniced, howto, command)
67 int paniced, howto;
68 char *command;
69{
70 register int i;
71 int s;
72 struct proc *p = current_proc(); /* XXX */
73 int hostboot_option=0;
74 int funnel_state;
91447636 75 struct proc *launchd_proc;
1c79356b 76
0b4e3aa0 77 extern void md_prepare_for_shutdown(int paniced, int howto, char * command);
1c79356b
A
78
79 funnel_state = thread_funnel_set(kernel_flock, TRUE);
80
0b4e3aa0 81 md_prepare_for_shutdown(paniced, howto, command);
1c79356b
A
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
55e303ae
A
97 audit_shutdown();
98
1c79356b
A
99 sync(p, (void *)NULL, (int *)NULL);
100
91447636
A
101 /*
102 * Now that all processes have been termianted and system is sync'ed up,
103 * suspend launchd
104 */
1c79356b 105
91447636
A
106 launchd_proc = pfind(1);
107 if (launchd_proc && p != launchd_proc) {
108 task_suspend(launchd_proc->task);
109 }
1c79356b
A
110
111 /*
112 * Unmount filesystems
113 */
91447636 114 vfs_unmountall();
1c79356b
A
115
116 /* Wait for the buffer cache to clean remaining dirty buffers */
91447636 117 for (iter = 0; iter < 100; iter++) {
1c79356b
A
118 nbusy = count_busy_buffers();
119 if (nbusy == 0)
120 break;
121 printf("%d ", nbusy);
91447636 122 IOSleep( 1 * nbusy );
1c79356b
A
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
91447636
A
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 */
1c79356b
A
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
174static void
175proc_shutdown()
176{
177 struct proc *p, *self;
178 struct vnode **cdirp, **rdirp, *vp;
179 int restart, i, TERM_catch;
91447636 180 int delayterm = 0;
1c79356b
A
181
182 /*
183 * Kill as many procs as we can. (Except ourself...)
184 */
0b4e3aa0 185 self = (struct proc *)current_proc();
1c79356b
A
186
187 /*
91447636
A
188 * Signal the init with SIGTERM so that he does not launch
189 * new processes
1c79356b
A
190 */
191 p = pfind(1);
91447636
A
192 if (p && p != self) {
193 psignal(p, SIGTERM);
194 }
1c79356b 195
1c79356b
A
196 printf("Killing all processes ");
197
198 /*
199 * send SIGTERM to those procs interested in catching one
200 */
4a249263 201sigterm_loop:
1c79356b 202 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
91447636
A
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 }
4a249263 208 if (p->p_sigcatch & sigmask(SIGTERM)) {
91447636 209 p->p_shutdownstate = 1;
ff6e181a
A
210 if (proc_refinternal(p, 1) == p) {
211 psignal(p, SIGTERM);
212 proc_dropinternal(p, 1);
213 }
4a249263 214 goto sigterm_loop;
1c79356b
A
215 }
216 }
91447636 217 }
1c79356b
A
218 /*
219 * now wait for up to 30 seconds to allow those procs catching SIGTERM
220 * to digest it
221 * as soon as these procs have exited, we'll continue on to the next step
222 */
223 for (i = 0; i < 300; i++) {
224 /*
225 * sleep for a tenth of a second
226 * and then check to see if the tasks that were sent a
227 * SIGTERM have exited
228 */
91447636 229 IOSleep(100);
1c79356b
A
230 TERM_catch = 0;
231
91447636
A
232 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
233 if (p->p_shutdownstate == 1) {
234 TERM_catch++;
235 }
1c79356b
A
236 }
237 if (TERM_catch == 0)
238 break;
239 }
55e303ae 240 if (TERM_catch) {
91447636 241 /*
55e303ae
A
242 * log the names of the unresponsive tasks
243 */
91447636 244
55e303ae 245 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
91447636 246 if (p->p_shutdownstate == 1) {
55e303ae 247 printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid);
91447636 248 }
55e303ae
A
249 }
250 IOSleep(1000 * 5);
251 }
1c79356b
A
252
253 /*
254 * send a SIGKILL to all the procs still hanging around
255 */
4a249263 256sigkill_loop:
1c79356b 257 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
91447636
A
258 if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_stat != SZOMB) && (p->p_shutdownstate != 2)) {
259
260 if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
261 continue;
262 }
ff6e181a
A
263 if (proc_refinternal(p, 1) == p) {
264 psignal(p, SIGKILL);
265 proc_dropinternal(p, 1);
266 }
4a249263 267 p->p_shutdownstate = 2;
4a249263
A
268 goto sigkill_loop;
269 }
1c79356b
A
270 }
271 /*
272 * wait for up to 60 seconds to allow these procs to exit normally
273 */
274 for (i = 0; i < 300; i++) {
275 IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */
276
277 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
91447636 278 if (p->p_shutdownstate == 2)
1c79356b
A
279 break;
280 }
281 if (!p)
282 break;
283 }
284
285 /*
286 * if we still have procs that haven't exited, then brute force 'em
287 */
288 p = allproc.lh_first;
289 while (p) {
ff6e181a 290 if ((p->p_shutdownstate == 3) || (p->p_flag&P_SYSTEM) || (!delayterm && ((p->p_lflag& P_LDELAYTERM)))
91447636 291 || (p->p_pptr->p_pid == 0) || (p == self)) {
1c79356b
A
292 p = p->p_list.le_next;
293 }
294 else {
ff6e181a 295 p->p_shutdownstate = 3;
1c79356b
A
296 /*
297 * NOTE: following code ignores sig_lock and plays
298 * with exit_thread correctly. This is OK unless we
299 * are a multiprocessor, in which case I do not
300 * understand the sig_lock. This needs to be fixed.
301 * XXX
302 */
91447636
A
303 if (p->exit_thread) { /* someone already doing it */
304 /* give him a chance */
305 thread_block(THREAD_CONTINUE_NULL);
306 } else {
307 p->exit_thread = current_thread();
1c79356b 308 printf(".");
ff6e181a
A
309 if (proc_refinternal(p, 1) == p) {
310 exit1(p, 1, (int *)NULL);
311 proc_dropinternal(p, 1);
312 }
1c79356b
A
313 }
314 p = allproc.lh_first;
315 }
316 }
317 printf("\n");
4a249263 318
91447636
A
319
320 /* Now start the termination of processes that are marked for delayed termn */
321 if (delayterm == 0) {
322 delayterm = 1;
323 goto sigterm_loop;
1c79356b 324 }
1c79356b
A
325 printf("continuing\n");
326}
327