]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
e5568f75 | 2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 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 | * |
e5568f75 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, | |
e5568f75 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> | |
33 | #include <sys/proc.h> | |
34 | #include <sys/user.h> | |
35 | #include <sys/buf.h> | |
36 | #include <sys/reboot.h> | |
37 | #include <sys/conf.h> | |
38 | #include <sys/vnode.h> | |
39 | #include <sys/file.h> | |
40 | #include <sys/clist.h> | |
41 | #include <sys/callout.h> | |
42 | #include <sys/mbuf.h> | |
43 | #include <sys/msgbuf.h> | |
44 | #include <sys/ioctl.h> | |
45 | #include <sys/signal.h> | |
46 | #include <sys/tty.h> | |
47 | #include <kern/task.h> | |
9bccf70c | 48 | #include <sys/quota.h> |
1c79356b A |
49 | #include <ufs/ufs/inode.h> |
50 | #if NCPUS > 1 | |
51 | #include <kern/processor.h> | |
52 | #include <kern/thread.h> | |
53 | #include <sys/lock.h> | |
54 | #endif /* NCPUS > 1 */ | |
55 | #include <vm/vm_kern.h> | |
56 | #include <mach/vm_param.h> | |
57 | #include <sys/filedesc.h> | |
58 | #include <mach/host_reboot.h> | |
e5568f75 | 59 | #include <bsm/audit_kernel.h> |
1c79356b A |
60 | |
61 | int waittime = -1; | |
62 | ||
63 | void | |
64 | boot(paniced, howto, command) | |
65 | int paniced, howto; | |
66 | char *command; | |
67 | { | |
68 | register int i; | |
69 | int s; | |
70 | struct proc *p = current_proc(); /* XXX */ | |
71 | int hostboot_option=0; | |
72 | int funnel_state; | |
73 | ||
0b4e3aa0 A |
74 | static void proc_shutdown(); |
75 | extern void md_prepare_for_shutdown(int paniced, int howto, char * command); | |
1c79356b A |
76 | |
77 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
78 | ||
0b4e3aa0 | 79 | md_prepare_for_shutdown(paniced, howto, command); |
1c79356b A |
80 | |
81 | if ((howto&RB_NOSYNC)==0 && waittime < 0) { | |
82 | int iter, nbusy; | |
83 | ||
84 | waittime = 0; | |
85 | ||
86 | printf("syncing disks... "); | |
87 | ||
88 | /* | |
89 | * Release vnodes held by texts before sync. | |
90 | */ | |
91 | ||
92 | /* handle live procs (deallocate their root and current directories). */ | |
93 | proc_shutdown(); | |
94 | ||
55e303ae A |
95 | audit_shutdown(); |
96 | ||
1c79356b A |
97 | sync(p, (void *)NULL, (int *)NULL); |
98 | ||
99 | /* Release vnodes from the VM object cache */ | |
100 | ubc_unmountall(); | |
101 | ||
102 | IOSleep( 1 * 1000 ); | |
103 | ||
104 | /* | |
105 | * Unmount filesystems | |
106 | */ | |
107 | if (panicstr == 0) | |
108 | vfs_unmountall(); | |
109 | ||
110 | /* Wait for the buffer cache to clean remaining dirty buffers */ | |
111 | for (iter = 0; iter < 20; iter++) { | |
112 | nbusy = count_busy_buffers(); | |
113 | if (nbusy == 0) | |
114 | break; | |
115 | printf("%d ", nbusy); | |
116 | IOSleep( 4 * nbusy ); | |
117 | } | |
118 | if (nbusy) | |
119 | printf("giving up\n"); | |
120 | else | |
121 | printf("done\n"); | |
122 | } | |
123 | ||
124 | /* | |
125 | * Can't just use an splnet() here to disable the network | |
126 | * because that will lock out softints which the disk | |
127 | * drivers depend on to finish DMAs. | |
128 | */ | |
129 | if_down_all(); | |
130 | ||
131 | if (howto & RB_POWERDOWN) | |
132 | hostboot_option = HOST_REBOOT_HALT; | |
133 | if (howto & RB_HALT) | |
134 | hostboot_option = HOST_REBOOT_HALT; | |
135 | if (paniced == RB_PANIC) | |
136 | hostboot_option = HOST_REBOOT_HALT; | |
137 | ||
138 | if (hostboot_option == HOST_REBOOT_HALT) | |
139 | IOSleep( 1 * 1000 ); | |
140 | ||
141 | host_reboot(host_priv_self(), hostboot_option); | |
142 | ||
143 | thread_funnel_set(kernel_flock, FALSE); | |
144 | } | |
145 | ||
146 | /* | |
147 | * proc_shutdown() | |
148 | * | |
149 | * Shutdown down proc system (release references to current and root | |
150 | * dirs for each process). | |
151 | * | |
152 | * POSIX modifications: | |
153 | * | |
154 | * For POSIX fcntl() file locking call vno_lockrelease() on | |
155 | * the file to release all of its record locks, if any. | |
156 | */ | |
157 | ||
158 | static void | |
159 | proc_shutdown() | |
160 | { | |
161 | struct proc *p, *self; | |
162 | struct vnode **cdirp, **rdirp, *vp; | |
163 | int restart, i, TERM_catch; | |
164 | ||
165 | /* | |
166 | * Kill as many procs as we can. (Except ourself...) | |
167 | */ | |
0b4e3aa0 | 168 | self = (struct proc *)current_proc(); |
1c79356b A |
169 | |
170 | /* | |
171 | * Suspend /etc/init | |
172 | */ | |
173 | p = pfind(1); | |
174 | if (p && p != self) | |
175 | task_suspend(p->task); /* stop init */ | |
176 | ||
1c79356b A |
177 | printf("Killing all processes "); |
178 | ||
179 | /* | |
180 | * send SIGTERM to those procs interested in catching one | |
181 | */ | |
4a249263 | 182 | sigterm_loop: |
1c79356b | 183 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { |
4a249263 A |
184 | if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_shutdownstate == 0)) { |
185 | if (p->p_sigcatch & sigmask(SIGTERM)) { | |
186 | p->p_shutdownstate = 1; | |
1c79356b | 187 | psignal(p, SIGTERM); |
4a249263 A |
188 | |
189 | goto sigterm_loop; | |
190 | } | |
1c79356b A |
191 | } |
192 | } | |
193 | /* | |
194 | * now wait for up to 30 seconds to allow those procs catching SIGTERM | |
195 | * to digest it | |
196 | * as soon as these procs have exited, we'll continue on to the next step | |
197 | */ | |
198 | for (i = 0; i < 300; i++) { | |
199 | /* | |
200 | * sleep for a tenth of a second | |
201 | * and then check to see if the tasks that were sent a | |
202 | * SIGTERM have exited | |
203 | */ | |
204 | IOSleep(100); | |
205 | TERM_catch = 0; | |
206 | ||
207 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
4a249263 A |
208 | if (p->p_shutdownstate == 1) |
209 | TERM_catch++; | |
1c79356b A |
210 | } |
211 | if (TERM_catch == 0) | |
212 | break; | |
213 | } | |
55e303ae A |
214 | if (TERM_catch) { |
215 | /* | |
216 | * log the names of the unresponsive tasks | |
217 | */ | |
55e303ae | 218 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { |
4a249263 | 219 | if (p->p_shutdownstate == 1) |
55e303ae | 220 | printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid); |
55e303ae A |
221 | } |
222 | IOSleep(1000 * 5); | |
223 | } | |
1c79356b A |
224 | |
225 | /* | |
226 | * send a SIGKILL to all the procs still hanging around | |
227 | */ | |
4a249263 | 228 | sigkill_loop: |
1c79356b | 229 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { |
4a249263 | 230 | if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_shutdownstate != 2)) { |
1c79356b | 231 | psignal(p, SIGKILL); |
4a249263 A |
232 | p->p_shutdownstate = 2; |
233 | ||
234 | goto sigkill_loop; | |
235 | } | |
1c79356b A |
236 | } |
237 | /* | |
238 | * wait for up to 60 seconds to allow these procs to exit normally | |
239 | */ | |
240 | for (i = 0; i < 300; i++) { | |
241 | IOSleep(200); /* double the time from 100 to 200 for NFS requests in particular */ | |
242 | ||
243 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
4a249263 | 244 | if (p->p_shutdownstate == 2) |
1c79356b A |
245 | break; |
246 | } | |
247 | if (!p) | |
248 | break; | |
249 | } | |
250 | ||
251 | /* | |
252 | * if we still have procs that haven't exited, then brute force 'em | |
253 | */ | |
254 | p = allproc.lh_first; | |
255 | while (p) { | |
256 | if ((p->p_flag&P_SYSTEM) || (p->p_pptr->p_pid == 0) || (p == self)) { | |
257 | p = p->p_list.le_next; | |
258 | } | |
259 | else { | |
260 | /* | |
261 | * NOTE: following code ignores sig_lock and plays | |
262 | * with exit_thread correctly. This is OK unless we | |
263 | * are a multiprocessor, in which case I do not | |
264 | * understand the sig_lock. This needs to be fixed. | |
265 | * XXX | |
266 | */ | |
267 | if (p->exit_thread) { /* someone already doing it */ | |
9bccf70c A |
268 | /* give him a chance */ |
269 | thread_block(THREAD_CONTINUE_NULL); | |
1c79356b A |
270 | } |
271 | else { | |
55e303ae | 272 | p->exit_thread = current_act(); |
1c79356b | 273 | printf("."); |
0b4e3aa0 | 274 | exit1(p, 1, (int *)NULL); |
1c79356b A |
275 | } |
276 | p = allproc.lh_first; | |
277 | } | |
278 | } | |
279 | printf("\n"); | |
280 | /* | |
281 | * Forcibly free resources of what's left. | |
282 | */ | |
4a249263 | 283 | #ifdef notyet |
1c79356b A |
284 | p = allproc.lh_first; |
285 | while (p) { | |
286 | /* | |
287 | * Close open files and release open-file table. | |
288 | * This may block! | |
289 | */ | |
4a249263 | 290 | |
1c79356b A |
291 | /* panics on reboot due to "zfree: non-allocated memory in collectable zone" message */ |
292 | fdfree(p); | |
1c79356b A |
293 | p = p->p_list.le_next; |
294 | } | |
4a249263 | 295 | #endif /* notyet */ |
1c79356b A |
296 | /* Wait for the reaper thread to run, and clean up what we have done |
297 | * before we proceed with the hardcore shutdown. This reduces the race | |
298 | * between kill_tasks and the reaper thread. | |
299 | */ | |
300 | /* thread_wakeup(&reaper_queue); */ | |
301 | /* IOSleep( 1 * 1000); */ | |
302 | printf("continuing\n"); | |
303 | } | |
304 |