]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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> | |
91447636 | 39 | #include <sys/proc_internal.h> |
1c79356b | 40 | #include <sys/user.h> |
1c79356b A |
41 | #include <sys/reboot.h> |
42 | #include <sys/conf.h> | |
91447636 A |
43 | #include <sys/vnode_internal.h> |
44 | #include <sys/file_internal.h> | |
1c79356b A |
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> | |
9bccf70c | 53 | #include <sys/quota.h> |
1c79356b A |
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 | |
b0d623f7 | 60 | #include <security/audit/audit.h> |
1c79356b | 61 | |
0a7de745 A |
62 | #include <kern/sched_prim.h> /* for thread_block() */ |
63 | #include <kern/host.h> /* for host_priv_self() */ | |
64 | #include <net/if_var.h> /* for if_down_all() */ | |
65 | #include <sys/buf_internal.h> /* for count_busy_buffers() */ | |
66 | #include <sys/mount_internal.h> /* for vfs_unmountall() */ | |
67 | #include <mach/task.h> /* for task_suspend() */ | |
68 | #include <sys/sysproto.h> /* abused for sync() */ | |
69 | #include <kern/clock.h> /* for delay_for_interval() */ | |
6d2010ae | 70 | #include <libkern/OSAtomic.h> |
a39ff7e2 | 71 | #include <IOKit/IOPlatformExpert.h> |
2d21ac55 | 72 | |
b0d623f7 A |
73 | #include <sys/kdebug.h> |
74 | ||
6d2010ae | 75 | uint32_t system_inshutdown = 0; |
b0d623f7 | 76 | |
2d21ac55 | 77 | /* XXX should be in a header file somewhere, but isn't */ |
b0d623f7 | 78 | extern void (*unmountroot_pre_hook)(void); |
2d21ac55 | 79 | |
b0d623f7 | 80 | unsigned int proc_shutdown_exitcount = 0; |
2d21ac55 | 81 | |
b0d623f7 A |
82 | static int sd_openlog(vfs_context_t); |
83 | static int sd_closelog(vfs_context_t); | |
84 | static void sd_log(vfs_context_t, const char *, ...); | |
2d21ac55 | 85 | static void proc_shutdown(void); |
d26ffc64 | 86 | static void zprint_panic_info(void); |
5ba3f43e A |
87 | extern void halt_log_enter(const char * what, const void * pc, uint64_t time); |
88 | ||
490019cf A |
89 | #if DEVELOPMENT || DEBUG |
90 | extern boolean_t kdp_has_polled_corefile(void); | |
91 | #endif /* DEVELOPMENT || DEBUG */ | |
2d21ac55 | 92 | |
0a7de745 | 93 | struct sd_filterargs { |
2d21ac55 A |
94 | int delayterm; |
95 | int shutdownstate; | |
96 | }; | |
97 | ||
98 | ||
99 | struct sd_iterargs { | |
0a7de745 A |
100 | int signo; /* the signal to be posted */ |
101 | int setsdstate; /* shutdown state to be set */ | |
102 | int countproc; /* count processes on action */ | |
103 | int activecount; /* number of processes on which action was done */ | |
2d21ac55 A |
104 | }; |
105 | ||
b0d623f7 A |
106 | static vnode_t sd_logvp = NULLVP; |
107 | static off_t sd_log_offset = 0; | |
108 | ||
109 | ||
2d21ac55 A |
110 | static int sd_filt1(proc_t, void *); |
111 | static int sd_filt2(proc_t, void *); | |
39037602 A |
112 | static int sd_callback1(proc_t p, void * arg); |
113 | static int sd_callback2(proc_t p, void * arg); | |
114 | static int sd_callback3(proc_t p, void * arg); | |
1c79356b | 115 | |
3e170ce0 | 116 | extern boolean_t panic_include_zprint; |
5ba3f43e A |
117 | extern mach_memory_info_t *panic_kext_memory_info; |
118 | extern vm_size_t panic_kext_memory_size; | |
3e170ce0 A |
119 | |
120 | static void | |
d26ffc64 | 121 | zprint_panic_info(void) |
3e170ce0 | 122 | { |
5ba3f43e A |
123 | unsigned int num_sites; |
124 | kern_return_t kr; | |
3e170ce0 A |
125 | |
126 | panic_include_zprint = TRUE; | |
5ba3f43e | 127 | panic_kext_memory_info = NULL; |
3e170ce0 A |
128 | panic_kext_memory_size = 0; |
129 | ||
5ba3f43e A |
130 | num_sites = vm_page_diagnose_estimate(); |
131 | panic_kext_memory_size = num_sites * sizeof(panic_kext_memory_info[0]); | |
132 | ||
133 | kr = kmem_alloc(kernel_map, (vm_offset_t *)&panic_kext_memory_info, round_page(panic_kext_memory_size), VM_KERN_MEMORY_OSFMK); | |
3e170ce0 | 134 | if (kr != KERN_SUCCESS) { |
5ba3f43e | 135 | panic_kext_memory_info = NULL; |
3e170ce0 A |
136 | return; |
137 | } | |
5ba3f43e A |
138 | |
139 | vm_page_diagnose(panic_kext_memory_info, num_sites, 0); | |
3e170ce0 A |
140 | } |
141 | ||
813fb2f6 A |
142 | int |
143 | get_system_inshutdown() | |
144 | { | |
0a7de745 | 145 | return system_inshutdown; |
813fb2f6 A |
146 | } |
147 | ||
cb323159 | 148 | __abortlike |
d26ffc64 A |
149 | static void |
150 | panic_kernel(int howto, char *message) | |
151 | { | |
152 | if ((howto & RB_PANIC_ZPRINT) == RB_PANIC_ZPRINT) { | |
153 | zprint_panic_info(); | |
154 | } | |
155 | panic("userspace panic: %s", message); | |
156 | } | |
157 | ||
cb323159 A |
158 | extern boolean_t compressor_store_stop_compaction; |
159 | extern lck_mtx_t vm_swap_data_lock; | |
160 | extern int vm_swapfile_create_thread_running; | |
161 | extern int vm_swapfile_gc_thread_running; | |
162 | ||
6d2010ae | 163 | int |
3e170ce0 | 164 | reboot_kernel(int howto, char *message) |
1c79356b | 165 | { |
0a7de745 | 166 | int hostboot_option = 0; |
5ba3f43e | 167 | uint64_t startTime; |
1c79356b | 168 | |
d26ffc64 A |
169 | if ((howto & (RB_PANIC | RB_QUICK)) == (RB_PANIC | RB_QUICK)) { |
170 | panic_kernel(howto, message); | |
171 | } | |
172 | ||
6d2010ae | 173 | if (!OSCompareAndSwap(0, 1, &system_inshutdown)) { |
0a7de745 | 174 | if ((howto & RB_QUICK) == RB_QUICK) { |
6d2010ae | 175 | goto force_reboot; |
0a7de745 A |
176 | } |
177 | return EBUSY; | |
6d2010ae | 178 | } |
cb323159 A |
179 | |
180 | lck_mtx_lock(&vm_swap_data_lock); | |
181 | ||
182 | /* Turn OFF future swapfile reclaimation / compaction etc.*/ | |
183 | compressor_store_stop_compaction = TRUE; | |
184 | ||
185 | /* wait for any current swapfile work to end */ | |
186 | while (vm_swapfile_create_thread_running || vm_swapfile_gc_thread_running) { | |
187 | assert_wait((event_t)&compressor_store_stop_compaction, THREAD_UNINT); | |
188 | ||
189 | lck_mtx_unlock(&vm_swap_data_lock); | |
190 | ||
191 | thread_block(THREAD_CONTINUE_NULL); | |
192 | ||
193 | lck_mtx_lock(&vm_swap_data_lock); | |
194 | } | |
195 | ||
196 | lck_mtx_unlock(&vm_swap_data_lock); | |
197 | ||
6d2010ae | 198 | /* |
a39ff7e2 | 199 | * Notify the power management root domain that the system will shut down. |
6d2010ae | 200 | */ |
a39ff7e2 | 201 | IOSystemShutdownNotification(kIOSystemShutdownNotificationStageProcessExit); |
2d21ac55 | 202 | |
0a7de745 | 203 | if ((howto & RB_QUICK) == RB_QUICK) { |
b0d623f7 | 204 | printf("Quick reboot...\n"); |
0a7de745 | 205 | if ((howto & RB_NOSYNC) == 0) { |
3e170ce0 | 206 | sync((proc_t)NULL, (void *)NULL, (int *)NULL); |
b0d623f7 | 207 | } |
0a7de745 | 208 | } else if ((howto & RB_NOSYNC) == 0) { |
1c79356b A |
209 | int iter, nbusy; |
210 | ||
1c79356b A |
211 | printf("syncing disks... "); |
212 | ||
213 | /* | |
214 | * Release vnodes held by texts before sync. | |
215 | */ | |
216 | ||
3e170ce0 | 217 | /* handle live procs (deallocate their root and current directories), suspend initproc */ |
5ba3f43e A |
218 | |
219 | startTime = mach_absolute_time(); | |
1c79356b | 220 | proc_shutdown(); |
5ba3f43e | 221 | halt_log_enter("proc_shutdown", 0, mach_absolute_time() - startTime); |
1c79356b | 222 | |
b0d623f7 | 223 | #if CONFIG_AUDIT |
5ba3f43e | 224 | startTime = mach_absolute_time(); |
6d2010ae | 225 | audit_shutdown(); |
5ba3f43e | 226 | halt_log_enter("audit_shutdown", 0, mach_absolute_time() - startTime); |
2d21ac55 | 227 | #endif |
55e303ae | 228 | |
0a7de745 | 229 | if (unmountroot_pre_hook != NULL) { |
b0d623f7 | 230 | unmountroot_pre_hook(); |
0a7de745 | 231 | } |
b0d623f7 | 232 | |
5ba3f43e | 233 | startTime = mach_absolute_time(); |
3e170ce0 | 234 | sync((proc_t)NULL, (void *)NULL, (int *)NULL); |
1c79356b | 235 | |
5ba3f43e A |
236 | if (kdebug_enable) { |
237 | startTime = mach_absolute_time(); | |
b0d623f7 | 238 | kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace"); |
5ba3f43e A |
239 | halt_log_enter("shutdown.trace", 0, mach_absolute_time() - startTime); |
240 | } | |
b0d623f7 | 241 | |
a39ff7e2 A |
242 | IOSystemShutdownNotification(kIOSystemShutdownNotificationStageRootUnmount); |
243 | ||
1c79356b A |
244 | /* |
245 | * Unmount filesystems | |
246 | */ | |
490019cf A |
247 | |
248 | #if DEVELOPMENT || DEBUG | |
249 | if (!(howto & RB_PANIC) || !kdp_has_polled_corefile()) | |
250 | #endif /* DEVELOPMENT || DEBUG */ | |
251 | { | |
5ba3f43e | 252 | startTime = mach_absolute_time(); |
490019cf | 253 | vfs_unmountall(); |
5ba3f43e | 254 | halt_log_enter("vfs_unmountall", 0, mach_absolute_time() - startTime); |
490019cf | 255 | } |
1c79356b A |
256 | |
257 | /* Wait for the buffer cache to clean remaining dirty buffers */ | |
5ba3f43e | 258 | startTime = mach_absolute_time(); |
91447636 | 259 | for (iter = 0; iter < 100; iter++) { |
1c79356b | 260 | nbusy = count_busy_buffers(); |
0a7de745 | 261 | if (nbusy == 0) { |
1c79356b | 262 | break; |
0a7de745 | 263 | } |
1c79356b | 264 | printf("%d ", nbusy); |
2d21ac55 | 265 | delay_for_interval( 1 * nbusy, 1000 * 1000); |
1c79356b | 266 | } |
0a7de745 | 267 | if (nbusy) { |
1c79356b | 268 | printf("giving up\n"); |
0a7de745 | 269 | } else { |
1c79356b | 270 | printf("done\n"); |
0a7de745 | 271 | } |
5ba3f43e | 272 | halt_log_enter("bufferclean", 0, mach_absolute_time() - startTime); |
1c79356b | 273 | } |
2d21ac55 | 274 | #if NETWORKING |
1c79356b A |
275 | /* |
276 | * Can't just use an splnet() here to disable the network | |
277 | * because that will lock out softints which the disk | |
278 | * drivers depend on to finish DMAs. | |
279 | */ | |
5ba3f43e | 280 | startTime = mach_absolute_time(); |
1c79356b | 281 | if_down_all(); |
5ba3f43e | 282 | halt_log_enter("if_down_all", 0, mach_absolute_time() - startTime); |
2d21ac55 | 283 | #endif /* NETWORKING */ |
1c79356b | 284 | |
6d2010ae | 285 | force_reboot: |
3e170ce0 A |
286 | |
287 | if (howto & RB_PANIC) { | |
d26ffc64 | 288 | panic_kernel(howto, message); |
3e170ce0 A |
289 | } |
290 | ||
0a7de745 | 291 | if (howto & RB_HALT) { |
1c79356b | 292 | hostboot_option = HOST_REBOOT_HALT; |
0a7de745 | 293 | } |
1c79356b | 294 | |
6d2010ae A |
295 | if (howto & RB_UPSDELAY) { |
296 | hostboot_option = HOST_REBOOT_UPSDELAY; | |
297 | } | |
0c530ab8 | 298 | |
1c79356b | 299 | host_reboot(host_priv_self(), hostboot_option); |
6d2010ae A |
300 | /* |
301 | * should not be reached | |
302 | */ | |
0a7de745 | 303 | return 0; |
1c79356b A |
304 | } |
305 | ||
b0d623f7 A |
306 | static int |
307 | sd_openlog(vfs_context_t ctx) | |
308 | { | |
309 | int error = 0; | |
310 | struct timeval tv; | |
0a7de745 | 311 | |
b0d623f7 A |
312 | /* Open shutdown log */ |
313 | if ((error = vnode_open(PROC_SHUTDOWN_LOG, (O_CREAT | FWRITE | O_NOFOLLOW), 0644, 0, &sd_logvp, ctx))) { | |
314 | printf("Failed to open %s: error %d\n", PROC_SHUTDOWN_LOG, error); | |
315 | sd_logvp = NULLVP; | |
316 | return error; | |
317 | } | |
318 | ||
319 | vnode_setsize(sd_logvp, (off_t)0, 0, ctx); | |
320 | ||
321 | /* Write a little header */ | |
322 | microtime(&tv); | |
323 | sd_log(ctx, "Process shutdown log. Current time is %lu (in seconds).\n\n", tv.tv_sec); | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
328 | static int | |
329 | sd_closelog(vfs_context_t ctx) | |
330 | { | |
331 | int error = 0; | |
332 | if (sd_logvp != NULLVP) { | |
333 | VNOP_FSYNC(sd_logvp, MNT_WAIT, ctx); | |
334 | error = vnode_close(sd_logvp, FWRITE, ctx); | |
335 | } | |
336 | ||
337 | return error; | |
338 | } | |
339 | ||
340 | static void | |
0a7de745 | 341 | sd_log(vfs_context_t ctx, const char *fmt, ...) |
b0d623f7 A |
342 | { |
343 | int resid, log_error, len; | |
344 | char logbuf[100]; | |
345 | va_list arglist; | |
346 | ||
347 | /* If the log isn't open yet, open it */ | |
348 | if (sd_logvp == NULLVP) { | |
349 | if (sd_openlog(ctx) != 0) { | |
350 | /* Couldn't open, we fail out */ | |
351 | return; | |
352 | } | |
353 | } | |
354 | ||
355 | va_start(arglist, fmt); | |
356 | len = vsnprintf(logbuf, sizeof(logbuf), fmt, arglist); | |
357 | log_error = vn_rdwr(UIO_WRITE, sd_logvp, (caddr_t)logbuf, len, sd_log_offset, | |
0a7de745 | 358 | UIO_SYSSPACE, IO_UNIT | IO_NOAUTH, vfs_context_ucred(ctx), &resid, vfs_context_proc(ctx)); |
b0d623f7 A |
359 | if (log_error == EIO || log_error == 0) { |
360 | sd_log_offset += (len - resid); | |
361 | } | |
362 | ||
363 | va_end(arglist); | |
b0d623f7 A |
364 | } |
365 | ||
2d21ac55 A |
366 | static int |
367 | sd_filt1(proc_t p, void * args) | |
368 | { | |
369 | proc_t self = current_proc(); | |
370 | struct sd_filterargs * sf = (struct sd_filterargs *)args; | |
0a7de745 | 371 | int delayterm = sf->delayterm; |
2d21ac55 A |
372 | int shutdownstate = sf->shutdownstate; |
373 | ||
0a7de745 A |
374 | if (((p->p_flag & P_SYSTEM) != 0) || (p->p_ppid == 0) |
375 | || (p == self) || (p->p_stat == SZOMB) | |
376 | || (p->p_shutdownstate != shutdownstate) | |
377 | || ((delayterm == 0) && ((p->p_lflag & P_LDELAYTERM) == P_LDELAYTERM)) | |
378 | || ((p->p_sigcatch & sigmask(SIGTERM)) == 0)) { | |
379 | return 0; | |
380 | } else { | |
381 | return 1; | |
382 | } | |
2d21ac55 A |
383 | } |
384 | ||
385 | ||
39037602 | 386 | static int |
2d21ac55 A |
387 | sd_callback1(proc_t p, void * args) |
388 | { | |
389 | struct sd_iterargs * sd = (struct sd_iterargs *)args; | |
390 | int signo = sd->signo; | |
391 | int setsdstate = sd->setsdstate; | |
b0d623f7 | 392 | int countproc = sd->countproc; |
2d21ac55 A |
393 | |
394 | proc_lock(p); | |
395 | p->p_shutdownstate = setsdstate; | |
396 | if (p->p_stat != SZOMB) { | |
397 | proc_unlock(p); | |
b0d623f7 A |
398 | if (countproc != 0) { |
399 | proc_list_lock(); | |
400 | p->p_listflag |= P_LIST_EXITCOUNT; | |
401 | proc_shutdown_exitcount++; | |
402 | proc_list_unlock(); | |
403 | } | |
404 | ||
2d21ac55 | 405 | psignal(p, signo); |
0a7de745 | 406 | if (countproc != 0) { |
b0d623f7 | 407 | sd->activecount++; |
0a7de745 | 408 | } |
39037602 | 409 | } else { |
2d21ac55 | 410 | proc_unlock(p); |
39037602 A |
411 | } |
412 | ||
413 | return PROC_RETURNED; | |
2d21ac55 A |
414 | } |
415 | ||
416 | static int | |
417 | sd_filt2(proc_t p, void * args) | |
418 | { | |
419 | proc_t self = current_proc(); | |
420 | struct sd_filterargs * sf = (struct sd_filterargs *)args; | |
0a7de745 | 421 | int delayterm = sf->delayterm; |
2d21ac55 A |
422 | int shutdownstate = sf->shutdownstate; |
423 | ||
0a7de745 A |
424 | if (((p->p_flag & P_SYSTEM) != 0) || (p->p_ppid == 0) |
425 | || (p == self) || (p->p_stat == SZOMB) | |
426 | || (p->p_shutdownstate == shutdownstate) | |
427 | || ((delayterm == 0) && ((p->p_lflag & P_LDELAYTERM) == P_LDELAYTERM))) { | |
428 | return 0; | |
429 | } else { | |
430 | return 1; | |
431 | } | |
2d21ac55 A |
432 | } |
433 | ||
39037602 | 434 | static int |
2d21ac55 A |
435 | sd_callback2(proc_t p, void * args) |
436 | { | |
437 | struct sd_iterargs * sd = (struct sd_iterargs *)args; | |
438 | int signo = sd->signo; | |
439 | int setsdstate = sd->setsdstate; | |
b0d623f7 | 440 | int countproc = sd->countproc; |
2d21ac55 A |
441 | |
442 | proc_lock(p); | |
443 | p->p_shutdownstate = setsdstate; | |
444 | if (p->p_stat != SZOMB) { | |
445 | proc_unlock(p); | |
0a7de745 | 446 | if (countproc != 0) { |
b0d623f7 A |
447 | proc_list_lock(); |
448 | p->p_listflag |= P_LIST_EXITCOUNT; | |
449 | proc_shutdown_exitcount++; | |
450 | proc_list_unlock(); | |
451 | } | |
2d21ac55 | 452 | psignal(p, signo); |
0a7de745 | 453 | if (countproc != 0) { |
b0d623f7 | 454 | sd->activecount++; |
0a7de745 | 455 | } |
39037602 | 456 | } else { |
2d21ac55 | 457 | proc_unlock(p); |
39037602 | 458 | } |
2d21ac55 | 459 | |
39037602 | 460 | return PROC_RETURNED; |
2d21ac55 A |
461 | } |
462 | ||
39037602 | 463 | static int |
2d21ac55 A |
464 | sd_callback3(proc_t p, void * args) |
465 | { | |
466 | struct sd_iterargs * sd = (struct sd_iterargs *)args; | |
b0d623f7 A |
467 | vfs_context_t ctx = vfs_context_current(); |
468 | ||
2d21ac55 A |
469 | int setsdstate = sd->setsdstate; |
470 | ||
471 | proc_lock(p); | |
472 | p->p_shutdownstate = setsdstate; | |
473 | if (p->p_stat != SZOMB) { | |
0a7de745 A |
474 | /* |
475 | * NOTE: following code ignores sig_lock and plays | |
476 | * with exit_thread correctly. This is OK unless we | |
477 | * are a multiprocessor, in which case I do not | |
478 | * understand the sig_lock. This needs to be fixed. | |
479 | * XXX | |
480 | */ | |
481 | if (p->exit_thread) { /* someone already doing it */ | |
2d21ac55 A |
482 | proc_unlock(p); |
483 | /* give him a chance */ | |
484 | thread_block(THREAD_CONTINUE_NULL); | |
485 | } else { | |
486 | p->exit_thread = current_thread(); | |
487 | printf("."); | |
b0d623f7 A |
488 | |
489 | sd_log(ctx, "%s[%d] had to be forced closed with exit1().\n", p->p_comm, p->p_pid); | |
490 | ||
2d21ac55 | 491 | proc_unlock(p); |
b0d623f7 | 492 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE, |
0a7de745 | 493 | p->p_pid, 0, 1, 0, 0); |
b0d623f7 | 494 | sd->activecount++; |
2d21ac55 A |
495 | exit1(p, 1, (int *)NULL); |
496 | } | |
39037602 | 497 | } else { |
2d21ac55 | 498 | proc_unlock(p); |
39037602 | 499 | } |
2d21ac55 | 500 | |
39037602 | 501 | return PROC_RETURNED; |
2d21ac55 A |
502 | } |
503 | ||
504 | ||
1c79356b A |
505 | /* |
506 | * proc_shutdown() | |
507 | * | |
508 | * Shutdown down proc system (release references to current and root | |
509 | * dirs for each process). | |
510 | * | |
511 | * POSIX modifications: | |
512 | * | |
5ba3f43e | 513 | * For POSIX fcntl() file locking call vno_lockrelease() on |
1c79356b A |
514 | * the file to release all of its record locks, if any. |
515 | */ | |
516 | ||
517 | static void | |
2d21ac55 | 518 | proc_shutdown(void) |
1c79356b | 519 | { |
b0d623f7 A |
520 | vfs_context_t ctx = vfs_context_current(); |
521 | struct proc *p, *self; | |
91447636 | 522 | int delayterm = 0; |
2d21ac55 A |
523 | struct sd_filterargs sfargs; |
524 | struct sd_iterargs sdargs; | |
b0d623f7 A |
525 | int error = 0; |
526 | struct timespec ts; | |
1c79356b A |
527 | |
528 | /* | |
529 | * Kill as many procs as we can. (Except ourself...) | |
530 | */ | |
0b4e3aa0 | 531 | self = (struct proc *)current_proc(); |
5ba3f43e | 532 | |
1c79356b | 533 | /* |
91447636 | 534 | * Signal the init with SIGTERM so that he does not launch |
5ba3f43e | 535 | * new processes |
1c79356b | 536 | */ |
2d21ac55 | 537 | p = proc_find(1); |
91447636 A |
538 | if (p && p != self) { |
539 | psignal(p, SIGTERM); | |
540 | } | |
2d21ac55 | 541 | proc_rele(p); |
1c79356b | 542 | |
1c79356b A |
543 | printf("Killing all processes "); |
544 | ||
2d21ac55 | 545 | sigterm_loop: |
1c79356b A |
546 | /* |
547 | * send SIGTERM to those procs interested in catching one | |
548 | */ | |
2d21ac55 A |
549 | sfargs.delayterm = delayterm; |
550 | sfargs.shutdownstate = 0; | |
551 | sdargs.signo = SIGTERM; | |
552 | sdargs.setsdstate = 1; | |
b0d623f7 A |
553 | sdargs.countproc = 1; |
554 | sdargs.activecount = 0; | |
2d21ac55 | 555 | |
b0d623f7 | 556 | error = 0; |
2d21ac55 A |
557 | /* post a SIGTERM to all that catch SIGTERM and not marked for delay */ |
558 | proc_rebootscan(sd_callback1, (void *)&sdargs, sd_filt1, (void *)&sfargs); | |
91447636 | 559 | |
0a7de745 | 560 | if (sdargs.activecount != 0 && proc_shutdown_exitcount != 0) { |
2d21ac55 | 561 | proc_list_lock(); |
b0d623f7 A |
562 | if (proc_shutdown_exitcount != 0) { |
563 | /* | |
5ba3f43e A |
564 | * now wait for up to 3 seconds to allow those procs catching SIGTERM |
565 | * to digest it | |
566 | * as soon as these procs have exited, we'll continue on to the next step | |
567 | */ | |
568 | ts.tv_sec = 3; | |
b0d623f7 A |
569 | ts.tv_nsec = 0; |
570 | error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts); | |
571 | if (error != 0) { | |
572 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
0a7de745 | 573 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) { |
b0d623f7 | 574 | p->p_listflag &= ~P_LIST_EXITCOUNT; |
0a7de745 | 575 | } |
b0d623f7 A |
576 | } |
577 | for (p = zombproc.lh_first; p; p = p->p_list.le_next) { | |
0a7de745 | 578 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) { |
b0d623f7 | 579 | p->p_listflag &= ~P_LIST_EXITCOUNT; |
0a7de745 | 580 | } |
b0d623f7 | 581 | } |
91447636 | 582 | } |
1c79356b | 583 | } |
2d21ac55 | 584 | proc_list_unlock(); |
1c79356b | 585 | } |
b0d623f7 | 586 | if (error == ETIMEDOUT) { |
91447636 | 587 | /* |
55e303ae A |
588 | * log the names of the unresponsive tasks |
589 | */ | |
91447636 | 590 | |
2d21ac55 A |
591 | proc_list_lock(); |
592 | ||
b0d623f7 | 593 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { |
91447636 | 594 | if (p->p_shutdownstate == 1) { |
b0d623f7 A |
595 | printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid); |
596 | sd_log(ctx, "%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid); | |
91447636 | 597 | } |
55e303ae | 598 | } |
2d21ac55 A |
599 | |
600 | proc_list_unlock(); | |
55e303ae | 601 | } |
1c79356b A |
602 | |
603 | /* | |
604 | * send a SIGKILL to all the procs still hanging around | |
605 | */ | |
2d21ac55 A |
606 | sfargs.delayterm = delayterm; |
607 | sfargs.shutdownstate = 2; | |
608 | sdargs.signo = SIGKILL; | |
609 | sdargs.setsdstate = 2; | |
b0d623f7 A |
610 | sdargs.countproc = 1; |
611 | sdargs.activecount = 0; | |
2d21ac55 | 612 | |
b0d623f7 | 613 | /* post a SIGKILL to all that catch SIGTERM and not marked for delay */ |
2d21ac55 | 614 | proc_rebootscan(sd_callback2, (void *)&sdargs, sd_filt2, (void *)&sfargs); |
91447636 | 615 | |
5ba3f43e A |
616 | error = 0; |
617 | ||
0a7de745 | 618 | if (sdargs.activecount != 0 && proc_shutdown_exitcount != 0) { |
2d21ac55 | 619 | proc_list_lock(); |
b0d623f7 A |
620 | if (proc_shutdown_exitcount != 0) { |
621 | /* | |
0a7de745 A |
622 | * wait for up to 60 seconds to allow these procs to exit normally |
623 | * | |
624 | * History: The delay interval was changed from 100 to 200 | |
625 | * for NFS requests in particular. | |
626 | */ | |
5ba3f43e | 627 | ts.tv_sec = 10; |
b0d623f7 A |
628 | ts.tv_nsec = 0; |
629 | error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts); | |
630 | if (error != 0) { | |
631 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
0a7de745 | 632 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) { |
b0d623f7 | 633 | p->p_listflag &= ~P_LIST_EXITCOUNT; |
0a7de745 | 634 | } |
b0d623f7 A |
635 | } |
636 | for (p = zombproc.lh_first; p; p = p->p_list.le_next) { | |
0a7de745 | 637 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) { |
b0d623f7 | 638 | p->p_listflag &= ~P_LIST_EXITCOUNT; |
0a7de745 | 639 | } |
b0d623f7 A |
640 | } |
641 | } | |
1c79356b | 642 | } |
2d21ac55 | 643 | proc_list_unlock(); |
1c79356b A |
644 | } |
645 | ||
5ba3f43e A |
646 | if (error == ETIMEDOUT) { |
647 | /* | |
648 | * log the names of the unresponsive tasks | |
649 | */ | |
650 | ||
651 | proc_list_lock(); | |
652 | ||
653 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
654 | if (p->p_shutdownstate == 2) { | |
655 | printf("%s[%d]: didn't act on SIGKILL\n", p->p_comm, p->p_pid); | |
656 | sd_log(ctx, "%s[%d]: didn't act on SIGKILL\n", p->p_comm, p->p_pid); | |
657 | } | |
658 | } | |
659 | ||
660 | proc_list_unlock(); | |
661 | } | |
662 | ||
1c79356b A |
663 | /* |
664 | * if we still have procs that haven't exited, then brute force 'em | |
665 | */ | |
2d21ac55 A |
666 | sfargs.delayterm = delayterm; |
667 | sfargs.shutdownstate = 3; | |
668 | sdargs.signo = 0; | |
669 | sdargs.setsdstate = 3; | |
b0d623f7 A |
670 | sdargs.countproc = 0; |
671 | sdargs.activecount = 0; | |
4a249263 | 672 | |
5ba3f43e A |
673 | |
674 | ||
2d21ac55 A |
675 | /* post a SIGTERM to all that catch SIGTERM and not marked for delay */ |
676 | proc_rebootscan(sd_callback3, (void *)&sdargs, sd_filt2, (void *)&sfargs); | |
677 | printf("\n"); | |
91447636 A |
678 | |
679 | /* Now start the termination of processes that are marked for delayed termn */ | |
680 | if (delayterm == 0) { | |
681 | delayterm = 1; | |
682 | goto sigterm_loop; | |
1c79356b | 683 | } |
b0d623f7 A |
684 | |
685 | sd_closelog(ctx); | |
686 | ||
3e170ce0 A |
687 | /* |
688 | * Now that all other processes have been terminated, suspend init | |
689 | */ | |
690 | task_suspend_internal(initproc->task); | |
691 | ||
2d21ac55 A |
692 | /* drop the ref on initproc */ |
693 | proc_rele(initproc); | |
1c79356b A |
694 | printf("continuing\n"); |
695 | } |