]>
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@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
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 | |
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. | |
8f6c56a5 | 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 | |
2d21ac55 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> |
2d21ac55 | 71 | |
b0d623f7 A |
72 | #include <sys/kdebug.h> |
73 | ||
6d2010ae | 74 | uint32_t system_inshutdown = 0; |
b0d623f7 | 75 | |
2d21ac55 | 76 | /* XXX should be in a header file somewhere, but isn't */ |
b0d623f7 | 77 | extern void (*unmountroot_pre_hook)(void); |
2d21ac55 | 78 | |
b0d623f7 | 79 | unsigned int proc_shutdown_exitcount = 0; |
2d21ac55 | 80 | |
b0d623f7 A |
81 | static int sd_openlog(vfs_context_t); |
82 | static int sd_closelog(vfs_context_t); | |
83 | static void sd_log(vfs_context_t, const char *, ...); | |
2d21ac55 | 84 | static void proc_shutdown(void); |
3e170ce0 | 85 | static void kernel_hwm_panic_info(void); |
2d21ac55 | 86 | extern void IOSystemShutdownNotification(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 A |
92 | |
93 | struct sd_filterargs{ | |
94 | int delayterm; | |
95 | int shutdownstate; | |
96 | }; | |
97 | ||
98 | ||
99 | struct sd_iterargs { | |
b0d623f7 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 | |
121 | kernel_hwm_panic_info(void) | |
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 | { | |
145 | return (system_inshutdown); | |
146 | } | |
147 | ||
6d2010ae | 148 | int |
3e170ce0 | 149 | reboot_kernel(int howto, char *message) |
1c79356b | 150 | { |
1c79356b | 151 | int hostboot_option=0; |
5ba3f43e | 152 | uint64_t startTime; |
1c79356b | 153 | |
6d2010ae A |
154 | if (!OSCompareAndSwap(0, 1, &system_inshutdown)) { |
155 | if ( (howto&RB_QUICK) == RB_QUICK) | |
156 | goto force_reboot; | |
157 | return (EBUSY); | |
158 | } | |
159 | /* | |
160 | * Temporary hack to notify the power management root domain | |
161 | * that the system will shut down. | |
162 | */ | |
2d21ac55 A |
163 | IOSystemShutdownNotification(); |
164 | ||
6d2010ae | 165 | if ((howto&RB_QUICK)==RB_QUICK) { |
b0d623f7 A |
166 | printf("Quick reboot...\n"); |
167 | if ((howto&RB_NOSYNC)==0) { | |
3e170ce0 | 168 | sync((proc_t)NULL, (void *)NULL, (int *)NULL); |
b0d623f7 A |
169 | } |
170 | } | |
6d2010ae | 171 | else if ((howto&RB_NOSYNC)==0) { |
1c79356b A |
172 | int iter, nbusy; |
173 | ||
1c79356b A |
174 | printf("syncing disks... "); |
175 | ||
176 | /* | |
177 | * Release vnodes held by texts before sync. | |
178 | */ | |
179 | ||
3e170ce0 | 180 | /* handle live procs (deallocate their root and current directories), suspend initproc */ |
5ba3f43e A |
181 | |
182 | startTime = mach_absolute_time(); | |
1c79356b | 183 | proc_shutdown(); |
5ba3f43e | 184 | halt_log_enter("proc_shutdown", 0, mach_absolute_time() - startTime); |
1c79356b | 185 | |
b0d623f7 | 186 | #if CONFIG_AUDIT |
5ba3f43e | 187 | startTime = mach_absolute_time(); |
6d2010ae | 188 | audit_shutdown(); |
5ba3f43e | 189 | halt_log_enter("audit_shutdown", 0, mach_absolute_time() - startTime); |
2d21ac55 | 190 | #endif |
55e303ae | 191 | |
b0d623f7 A |
192 | if (unmountroot_pre_hook != NULL) |
193 | unmountroot_pre_hook(); | |
194 | ||
5ba3f43e | 195 | startTime = mach_absolute_time(); |
3e170ce0 | 196 | sync((proc_t)NULL, (void *)NULL, (int *)NULL); |
1c79356b | 197 | |
5ba3f43e A |
198 | if (kdebug_enable) { |
199 | startTime = mach_absolute_time(); | |
b0d623f7 | 200 | kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace"); |
5ba3f43e A |
201 | halt_log_enter("shutdown.trace", 0, mach_absolute_time() - startTime); |
202 | } | |
b0d623f7 | 203 | |
1c79356b A |
204 | /* |
205 | * Unmount filesystems | |
206 | */ | |
490019cf A |
207 | |
208 | #if DEVELOPMENT || DEBUG | |
209 | if (!(howto & RB_PANIC) || !kdp_has_polled_corefile()) | |
210 | #endif /* DEVELOPMENT || DEBUG */ | |
211 | { | |
5ba3f43e | 212 | startTime = mach_absolute_time(); |
490019cf | 213 | vfs_unmountall(); |
5ba3f43e | 214 | halt_log_enter("vfs_unmountall", 0, mach_absolute_time() - startTime); |
490019cf | 215 | } |
1c79356b A |
216 | |
217 | /* Wait for the buffer cache to clean remaining dirty buffers */ | |
5ba3f43e | 218 | startTime = mach_absolute_time(); |
91447636 | 219 | for (iter = 0; iter < 100; iter++) { |
1c79356b A |
220 | nbusy = count_busy_buffers(); |
221 | if (nbusy == 0) | |
222 | break; | |
223 | printf("%d ", nbusy); | |
2d21ac55 | 224 | delay_for_interval( 1 * nbusy, 1000 * 1000); |
1c79356b A |
225 | } |
226 | if (nbusy) | |
227 | printf("giving up\n"); | |
228 | else | |
229 | printf("done\n"); | |
5ba3f43e | 230 | halt_log_enter("bufferclean", 0, mach_absolute_time() - startTime); |
1c79356b | 231 | } |
2d21ac55 | 232 | #if NETWORKING |
1c79356b A |
233 | /* |
234 | * Can't just use an splnet() here to disable the network | |
235 | * because that will lock out softints which the disk | |
236 | * drivers depend on to finish DMAs. | |
237 | */ | |
5ba3f43e | 238 | startTime = mach_absolute_time(); |
1c79356b | 239 | if_down_all(); |
5ba3f43e | 240 | halt_log_enter("if_down_all", 0, mach_absolute_time() - startTime); |
2d21ac55 | 241 | #endif /* NETWORKING */ |
1c79356b | 242 | |
6d2010ae | 243 | force_reboot: |
3e170ce0 A |
244 | |
245 | if (howto & RB_PANIC) { | |
246 | if (strncmp(message, "Kernel memory has exceeded limits", 33) == 0) { | |
247 | kernel_hwm_panic_info(); | |
248 | } | |
249 | panic ("userspace panic: %s", message); | |
250 | } | |
251 | ||
1c79356b A |
252 | if (howto & RB_POWERDOWN) |
253 | hostboot_option = HOST_REBOOT_HALT; | |
254 | if (howto & RB_HALT) | |
255 | hostboot_option = HOST_REBOOT_HALT; | |
1c79356b | 256 | |
6d2010ae A |
257 | if (howto & RB_UPSDELAY) { |
258 | hostboot_option = HOST_REBOOT_UPSDELAY; | |
259 | } | |
0c530ab8 | 260 | |
1c79356b | 261 | host_reboot(host_priv_self(), hostboot_option); |
6d2010ae A |
262 | /* |
263 | * should not be reached | |
264 | */ | |
265 | return (0); | |
1c79356b A |
266 | } |
267 | ||
b0d623f7 A |
268 | static int |
269 | sd_openlog(vfs_context_t ctx) | |
270 | { | |
271 | int error = 0; | |
272 | struct timeval tv; | |
273 | ||
274 | /* Open shutdown log */ | |
275 | if ((error = vnode_open(PROC_SHUTDOWN_LOG, (O_CREAT | FWRITE | O_NOFOLLOW), 0644, 0, &sd_logvp, ctx))) { | |
276 | printf("Failed to open %s: error %d\n", PROC_SHUTDOWN_LOG, error); | |
277 | sd_logvp = NULLVP; | |
278 | return error; | |
279 | } | |
280 | ||
281 | vnode_setsize(sd_logvp, (off_t)0, 0, ctx); | |
282 | ||
283 | /* Write a little header */ | |
284 | microtime(&tv); | |
285 | sd_log(ctx, "Process shutdown log. Current time is %lu (in seconds).\n\n", tv.tv_sec); | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | static int | |
291 | sd_closelog(vfs_context_t ctx) | |
292 | { | |
293 | int error = 0; | |
294 | if (sd_logvp != NULLVP) { | |
295 | VNOP_FSYNC(sd_logvp, MNT_WAIT, ctx); | |
296 | error = vnode_close(sd_logvp, FWRITE, ctx); | |
297 | } | |
298 | ||
299 | return error; | |
300 | } | |
301 | ||
302 | static void | |
303 | sd_log(vfs_context_t ctx, const char *fmt, ...) | |
304 | { | |
305 | int resid, log_error, len; | |
306 | char logbuf[100]; | |
307 | va_list arglist; | |
308 | ||
309 | /* If the log isn't open yet, open it */ | |
310 | if (sd_logvp == NULLVP) { | |
311 | if (sd_openlog(ctx) != 0) { | |
312 | /* Couldn't open, we fail out */ | |
313 | return; | |
314 | } | |
315 | } | |
316 | ||
317 | va_start(arglist, fmt); | |
318 | len = vsnprintf(logbuf, sizeof(logbuf), fmt, arglist); | |
319 | log_error = vn_rdwr(UIO_WRITE, sd_logvp, (caddr_t)logbuf, len, sd_log_offset, | |
320 | UIO_SYSSPACE, IO_UNIT | IO_NOAUTH, vfs_context_ucred(ctx), &resid, vfs_context_proc(ctx)); | |
321 | if (log_error == EIO || log_error == 0) { | |
322 | sd_log_offset += (len - resid); | |
323 | } | |
324 | ||
325 | va_end(arglist); | |
326 | ||
327 | } | |
328 | ||
2d21ac55 A |
329 | static int |
330 | sd_filt1(proc_t p, void * args) | |
331 | { | |
332 | proc_t self = current_proc(); | |
333 | struct sd_filterargs * sf = (struct sd_filterargs *)args; | |
334 | int delayterm = sf-> delayterm; | |
335 | int shutdownstate = sf->shutdownstate; | |
336 | ||
337 | if (((p->p_flag&P_SYSTEM) != 0) || (p->p_ppid == 0) | |
338 | ||(p == self) || (p->p_stat == SZOMB) | |
339 | || (p->p_shutdownstate != shutdownstate) | |
340 | ||((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) | |
341 | || ((p->p_sigcatch & sigmask(SIGTERM))== 0)) { | |
342 | return(0); | |
343 | } | |
344 | else | |
345 | return(1); | |
346 | } | |
347 | ||
348 | ||
39037602 | 349 | static int |
2d21ac55 A |
350 | sd_callback1(proc_t p, void * args) |
351 | { | |
352 | struct sd_iterargs * sd = (struct sd_iterargs *)args; | |
353 | int signo = sd->signo; | |
354 | int setsdstate = sd->setsdstate; | |
b0d623f7 | 355 | int countproc = sd->countproc; |
2d21ac55 A |
356 | |
357 | proc_lock(p); | |
358 | p->p_shutdownstate = setsdstate; | |
359 | if (p->p_stat != SZOMB) { | |
360 | proc_unlock(p); | |
b0d623f7 A |
361 | if (countproc != 0) { |
362 | proc_list_lock(); | |
363 | p->p_listflag |= P_LIST_EXITCOUNT; | |
364 | proc_shutdown_exitcount++; | |
365 | proc_list_unlock(); | |
366 | } | |
367 | ||
2d21ac55 | 368 | psignal(p, signo); |
b0d623f7 A |
369 | if (countproc != 0) |
370 | sd->activecount++; | |
39037602 | 371 | } else { |
2d21ac55 | 372 | proc_unlock(p); |
39037602 A |
373 | } |
374 | ||
375 | return PROC_RETURNED; | |
2d21ac55 A |
376 | } |
377 | ||
378 | static int | |
379 | sd_filt2(proc_t p, void * args) | |
380 | { | |
381 | proc_t self = current_proc(); | |
382 | struct sd_filterargs * sf = (struct sd_filterargs *)args; | |
383 | int delayterm = sf-> delayterm; | |
384 | int shutdownstate = sf->shutdownstate; | |
385 | ||
386 | if (((p->p_flag&P_SYSTEM) != 0) || (p->p_ppid == 0) | |
387 | ||(p == self) || (p->p_stat == SZOMB) | |
388 | || (p->p_shutdownstate == shutdownstate) | |
389 | ||((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM))) { | |
390 | return(0); | |
391 | } | |
392 | else | |
393 | return(1); | |
394 | } | |
395 | ||
39037602 | 396 | static int |
2d21ac55 A |
397 | sd_callback2(proc_t p, void * args) |
398 | { | |
399 | struct sd_iterargs * sd = (struct sd_iterargs *)args; | |
400 | int signo = sd->signo; | |
401 | int setsdstate = sd->setsdstate; | |
b0d623f7 | 402 | int countproc = sd->countproc; |
2d21ac55 A |
403 | |
404 | proc_lock(p); | |
405 | p->p_shutdownstate = setsdstate; | |
406 | if (p->p_stat != SZOMB) { | |
407 | proc_unlock(p); | |
b0d623f7 A |
408 | if (countproc != 0) { |
409 | proc_list_lock(); | |
410 | p->p_listflag |= P_LIST_EXITCOUNT; | |
411 | proc_shutdown_exitcount++; | |
412 | proc_list_unlock(); | |
413 | } | |
2d21ac55 | 414 | psignal(p, signo); |
b0d623f7 A |
415 | if (countproc != 0) |
416 | sd->activecount++; | |
39037602 | 417 | } else { |
2d21ac55 | 418 | proc_unlock(p); |
39037602 | 419 | } |
2d21ac55 | 420 | |
39037602 | 421 | return PROC_RETURNED; |
2d21ac55 A |
422 | } |
423 | ||
39037602 | 424 | static int |
2d21ac55 A |
425 | sd_callback3(proc_t p, void * args) |
426 | { | |
427 | struct sd_iterargs * sd = (struct sd_iterargs *)args; | |
b0d623f7 A |
428 | vfs_context_t ctx = vfs_context_current(); |
429 | ||
2d21ac55 A |
430 | int setsdstate = sd->setsdstate; |
431 | ||
432 | proc_lock(p); | |
433 | p->p_shutdownstate = setsdstate; | |
434 | if (p->p_stat != SZOMB) { | |
435 | /* | |
436 | * NOTE: following code ignores sig_lock and plays | |
437 | * with exit_thread correctly. This is OK unless we | |
438 | * are a multiprocessor, in which case I do not | |
439 | * understand the sig_lock. This needs to be fixed. | |
440 | * XXX | |
441 | */ | |
442 | if (p->exit_thread) { /* someone already doing it */ | |
443 | proc_unlock(p); | |
444 | /* give him a chance */ | |
445 | thread_block(THREAD_CONTINUE_NULL); | |
446 | } else { | |
447 | p->exit_thread = current_thread(); | |
448 | printf("."); | |
b0d623f7 A |
449 | |
450 | sd_log(ctx, "%s[%d] had to be forced closed with exit1().\n", p->p_comm, p->p_pid); | |
451 | ||
2d21ac55 | 452 | proc_unlock(p); |
b0d623f7 A |
453 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE, |
454 | p->p_pid, 0, 1, 0, 0); | |
455 | sd->activecount++; | |
2d21ac55 A |
456 | exit1(p, 1, (int *)NULL); |
457 | } | |
39037602 | 458 | } else { |
2d21ac55 | 459 | proc_unlock(p); |
39037602 | 460 | } |
2d21ac55 | 461 | |
39037602 | 462 | return PROC_RETURNED; |
2d21ac55 A |
463 | } |
464 | ||
465 | ||
1c79356b A |
466 | /* |
467 | * proc_shutdown() | |
468 | * | |
469 | * Shutdown down proc system (release references to current and root | |
470 | * dirs for each process). | |
471 | * | |
472 | * POSIX modifications: | |
473 | * | |
5ba3f43e | 474 | * For POSIX fcntl() file locking call vno_lockrelease() on |
1c79356b A |
475 | * the file to release all of its record locks, if any. |
476 | */ | |
477 | ||
478 | static void | |
2d21ac55 | 479 | proc_shutdown(void) |
1c79356b | 480 | { |
b0d623f7 A |
481 | vfs_context_t ctx = vfs_context_current(); |
482 | struct proc *p, *self; | |
91447636 | 483 | int delayterm = 0; |
2d21ac55 A |
484 | struct sd_filterargs sfargs; |
485 | struct sd_iterargs sdargs; | |
b0d623f7 A |
486 | int error = 0; |
487 | struct timespec ts; | |
1c79356b A |
488 | |
489 | /* | |
490 | * Kill as many procs as we can. (Except ourself...) | |
491 | */ | |
0b4e3aa0 | 492 | self = (struct proc *)current_proc(); |
5ba3f43e | 493 | |
1c79356b | 494 | /* |
91447636 | 495 | * Signal the init with SIGTERM so that he does not launch |
5ba3f43e | 496 | * new processes |
1c79356b | 497 | */ |
2d21ac55 | 498 | p = proc_find(1); |
91447636 A |
499 | if (p && p != self) { |
500 | psignal(p, SIGTERM); | |
501 | } | |
2d21ac55 | 502 | proc_rele(p); |
1c79356b | 503 | |
1c79356b A |
504 | printf("Killing all processes "); |
505 | ||
2d21ac55 | 506 | sigterm_loop: |
1c79356b A |
507 | /* |
508 | * send SIGTERM to those procs interested in catching one | |
509 | */ | |
2d21ac55 A |
510 | sfargs.delayterm = delayterm; |
511 | sfargs.shutdownstate = 0; | |
512 | sdargs.signo = SIGTERM; | |
513 | sdargs.setsdstate = 1; | |
b0d623f7 A |
514 | sdargs.countproc = 1; |
515 | sdargs.activecount = 0; | |
2d21ac55 | 516 | |
b0d623f7 | 517 | error = 0; |
2d21ac55 A |
518 | /* post a SIGTERM to all that catch SIGTERM and not marked for delay */ |
519 | proc_rebootscan(sd_callback1, (void *)&sdargs, sd_filt1, (void *)&sfargs); | |
91447636 | 520 | |
b0d623f7 | 521 | if (sdargs.activecount != 0 && proc_shutdown_exitcount!= 0) { |
2d21ac55 | 522 | proc_list_lock(); |
b0d623f7 A |
523 | if (proc_shutdown_exitcount != 0) { |
524 | /* | |
5ba3f43e A |
525 | * now wait for up to 3 seconds to allow those procs catching SIGTERM |
526 | * to digest it | |
527 | * as soon as these procs have exited, we'll continue on to the next step | |
528 | */ | |
529 | ts.tv_sec = 3; | |
b0d623f7 A |
530 | ts.tv_nsec = 0; |
531 | error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts); | |
532 | if (error != 0) { | |
533 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
534 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) | |
535 | p->p_listflag &= ~P_LIST_EXITCOUNT; | |
536 | } | |
537 | for (p = zombproc.lh_first; p; p = p->p_list.le_next) { | |
538 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) | |
539 | p->p_listflag &= ~P_LIST_EXITCOUNT; | |
540 | } | |
91447636 | 541 | } |
1c79356b | 542 | } |
2d21ac55 | 543 | proc_list_unlock(); |
1c79356b | 544 | } |
b0d623f7 | 545 | if (error == ETIMEDOUT) { |
91447636 | 546 | /* |
55e303ae A |
547 | * log the names of the unresponsive tasks |
548 | */ | |
91447636 | 549 | |
2d21ac55 A |
550 | proc_list_lock(); |
551 | ||
b0d623f7 | 552 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { |
91447636 | 553 | if (p->p_shutdownstate == 1) { |
b0d623f7 A |
554 | printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid); |
555 | sd_log(ctx, "%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid); | |
91447636 | 556 | } |
55e303ae | 557 | } |
2d21ac55 A |
558 | |
559 | proc_list_unlock(); | |
55e303ae | 560 | } |
1c79356b A |
561 | |
562 | /* | |
563 | * send a SIGKILL to all the procs still hanging around | |
564 | */ | |
2d21ac55 A |
565 | sfargs.delayterm = delayterm; |
566 | sfargs.shutdownstate = 2; | |
567 | sdargs.signo = SIGKILL; | |
568 | sdargs.setsdstate = 2; | |
b0d623f7 A |
569 | sdargs.countproc = 1; |
570 | sdargs.activecount = 0; | |
2d21ac55 | 571 | |
b0d623f7 | 572 | /* post a SIGKILL to all that catch SIGTERM and not marked for delay */ |
2d21ac55 | 573 | proc_rebootscan(sd_callback2, (void *)&sdargs, sd_filt2, (void *)&sfargs); |
91447636 | 574 | |
5ba3f43e A |
575 | error = 0; |
576 | ||
b0d623f7 | 577 | if (sdargs.activecount != 0 && proc_shutdown_exitcount!= 0) { |
2d21ac55 | 578 | proc_list_lock(); |
b0d623f7 A |
579 | if (proc_shutdown_exitcount != 0) { |
580 | /* | |
5ba3f43e A |
581 | * wait for up to 60 seconds to allow these procs to exit normally |
582 | * | |
583 | * History: The delay interval was changed from 100 to 200 | |
584 | * for NFS requests in particular. | |
585 | */ | |
586 | ts.tv_sec = 10; | |
b0d623f7 A |
587 | ts.tv_nsec = 0; |
588 | error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts); | |
589 | if (error != 0) { | |
590 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
591 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) | |
592 | p->p_listflag &= ~P_LIST_EXITCOUNT; | |
593 | } | |
594 | for (p = zombproc.lh_first; p; p = p->p_list.le_next) { | |
595 | if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) | |
596 | p->p_listflag &= ~P_LIST_EXITCOUNT; | |
597 | } | |
598 | } | |
1c79356b | 599 | } |
2d21ac55 | 600 | proc_list_unlock(); |
1c79356b A |
601 | } |
602 | ||
5ba3f43e A |
603 | if (error == ETIMEDOUT) { |
604 | /* | |
605 | * log the names of the unresponsive tasks | |
606 | */ | |
607 | ||
608 | proc_list_lock(); | |
609 | ||
610 | for (p = allproc.lh_first; p; p = p->p_list.le_next) { | |
611 | if (p->p_shutdownstate == 2) { | |
612 | printf("%s[%d]: didn't act on SIGKILL\n", p->p_comm, p->p_pid); | |
613 | sd_log(ctx, "%s[%d]: didn't act on SIGKILL\n", p->p_comm, p->p_pid); | |
614 | } | |
615 | } | |
616 | ||
617 | proc_list_unlock(); | |
618 | } | |
619 | ||
1c79356b A |
620 | /* |
621 | * if we still have procs that haven't exited, then brute force 'em | |
622 | */ | |
2d21ac55 A |
623 | sfargs.delayterm = delayterm; |
624 | sfargs.shutdownstate = 3; | |
625 | sdargs.signo = 0; | |
626 | sdargs.setsdstate = 3; | |
b0d623f7 A |
627 | sdargs.countproc = 0; |
628 | sdargs.activecount = 0; | |
4a249263 | 629 | |
5ba3f43e A |
630 | |
631 | ||
2d21ac55 A |
632 | /* post a SIGTERM to all that catch SIGTERM and not marked for delay */ |
633 | proc_rebootscan(sd_callback3, (void *)&sdargs, sd_filt2, (void *)&sfargs); | |
634 | printf("\n"); | |
91447636 A |
635 | |
636 | /* Now start the termination of processes that are marked for delayed termn */ | |
637 | if (delayterm == 0) { | |
638 | delayterm = 1; | |
639 | goto sigterm_loop; | |
1c79356b | 640 | } |
b0d623f7 A |
641 | |
642 | sd_closelog(ctx); | |
643 | ||
3e170ce0 A |
644 | /* |
645 | * Now that all other processes have been terminated, suspend init | |
646 | */ | |
647 | task_suspend_internal(initproc->task); | |
648 | ||
2d21ac55 A |
649 | /* drop the ref on initproc */ |
650 | proc_rele(initproc); | |
1c79356b A |
651 | printf("continuing\n"); |
652 | } | |
653 |