]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_shutdown.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / kern / kern_shutdown.c
CommitLineData
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/mbuf.h>
46#include <sys/msgbuf.h>
47#include <sys/ioctl.h>
48#include <sys/signal.h>
49#include <sys/tty.h>
50#include <kern/task.h>
9bccf70c 51#include <sys/quota.h>
1c79356b
A
52#include <vm/vm_kern.h>
53#include <mach/vm_param.h>
54#include <sys/filedesc.h>
91447636 55#include <mach/host_priv.h>
1c79356b 56#include <mach/host_reboot.h>
91447636 57
b0d623f7 58#include <security/audit/audit.h>
1c79356b 59
0a7de745
A
60#include <kern/sched_prim.h> /* for thread_block() */
61#include <kern/host.h> /* for host_priv_self() */
62#include <net/if_var.h> /* for if_down_all() */
63#include <sys/buf_internal.h> /* for count_busy_buffers() */
64#include <sys/mount_internal.h> /* for vfs_unmountall() */
65#include <mach/task.h> /* for task_suspend() */
66#include <sys/sysproto.h> /* abused for sync() */
67#include <kern/clock.h> /* for delay_for_interval() */
6d2010ae 68#include <libkern/OSAtomic.h>
a39ff7e2 69#include <IOKit/IOPlatformExpert.h>
2d21ac55 70
b0d623f7
A
71#include <sys/kdebug.h>
72
6d2010ae 73uint32_t system_inshutdown = 0;
b0d623f7 74
f427ee49 75#if XNU_TARGET_OS_OSX
2d21ac55 76/* XXX should be in a header file somewhere, but isn't */
b0d623f7 77extern void (*unmountroot_pre_hook)(void);
f427ee49 78#endif
2d21ac55 79
b0d623f7 80unsigned int proc_shutdown_exitcount = 0;
2d21ac55 81
b0d623f7
A
82static int sd_openlog(vfs_context_t);
83static int sd_closelog(vfs_context_t);
84static void sd_log(vfs_context_t, const char *, ...);
2d21ac55 85static void proc_shutdown(void);
d26ffc64 86static void zprint_panic_info(void);
5ba3f43e
A
87extern void halt_log_enter(const char * what, const void * pc, uint64_t time);
88
490019cf
A
89#if DEVELOPMENT || DEBUG
90extern boolean_t kdp_has_polled_corefile(void);
91#endif /* DEVELOPMENT || DEBUG */
2d21ac55 92
0a7de745 93struct sd_filterargs {
2d21ac55
A
94 int delayterm;
95 int shutdownstate;
96};
97
98
99struct 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
106static vnode_t sd_logvp = NULLVP;
107static off_t sd_log_offset = 0;
108
109
2d21ac55
A
110static int sd_filt1(proc_t, void *);
111static int sd_filt2(proc_t, void *);
39037602
A
112static int sd_callback1(proc_t p, void * arg);
113static int sd_callback2(proc_t p, void * arg);
114static int sd_callback3(proc_t p, void * arg);
1c79356b 115
3e170ce0 116extern boolean_t panic_include_zprint;
5ba3f43e
A
117extern mach_memory_info_t *panic_kext_memory_info;
118extern vm_size_t panic_kext_memory_size;
3e170ce0
A
119
120static void
d26ffc64 121zprint_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
142int
143get_system_inshutdown()
144{
0a7de745 145 return system_inshutdown;
813fb2f6
A
146}
147
cb323159 148__abortlike
d26ffc64
A
149static void
150panic_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
158extern boolean_t compressor_store_stop_compaction;
159extern lck_mtx_t vm_swap_data_lock;
160extern int vm_swapfile_create_thread_running;
161extern int vm_swapfile_gc_thread_running;
162
6d2010ae 163int
3e170ce0 164reboot_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
f427ee49 229#if XNU_TARGET_OS_OSX
0a7de745 230 if (unmountroot_pre_hook != NULL) {
b0d623f7 231 unmountroot_pre_hook();
0a7de745 232 }
f427ee49 233#endif
b0d623f7 234
5ba3f43e 235 startTime = mach_absolute_time();
3e170ce0 236 sync((proc_t)NULL, (void *)NULL, (int *)NULL);
1c79356b 237
5ba3f43e
A
238 if (kdebug_enable) {
239 startTime = mach_absolute_time();
b0d623f7 240 kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace");
5ba3f43e
A
241 halt_log_enter("shutdown.trace", 0, mach_absolute_time() - startTime);
242 }
b0d623f7 243
a39ff7e2
A
244 IOSystemShutdownNotification(kIOSystemShutdownNotificationStageRootUnmount);
245
1c79356b
A
246 /*
247 * Unmount filesystems
248 */
490019cf
A
249
250#if DEVELOPMENT || DEBUG
251 if (!(howto & RB_PANIC) || !kdp_has_polled_corefile())
252#endif /* DEVELOPMENT || DEBUG */
253 {
5ba3f43e 254 startTime = mach_absolute_time();
490019cf 255 vfs_unmountall();
5ba3f43e 256 halt_log_enter("vfs_unmountall", 0, mach_absolute_time() - startTime);
490019cf 257 }
1c79356b
A
258
259 /* Wait for the buffer cache to clean remaining dirty buffers */
5ba3f43e 260 startTime = mach_absolute_time();
91447636 261 for (iter = 0; iter < 100; iter++) {
1c79356b 262 nbusy = count_busy_buffers();
0a7de745 263 if (nbusy == 0) {
1c79356b 264 break;
0a7de745 265 }
1c79356b 266 printf("%d ", nbusy);
2d21ac55 267 delay_for_interval( 1 * nbusy, 1000 * 1000);
1c79356b 268 }
0a7de745 269 if (nbusy) {
1c79356b 270 printf("giving up\n");
0a7de745 271 } else {
1c79356b 272 printf("done\n");
0a7de745 273 }
5ba3f43e 274 halt_log_enter("bufferclean", 0, mach_absolute_time() - startTime);
1c79356b 275 }
2d21ac55 276#if NETWORKING
1c79356b
A
277 /*
278 * Can't just use an splnet() here to disable the network
279 * because that will lock out softints which the disk
280 * drivers depend on to finish DMAs.
281 */
5ba3f43e 282 startTime = mach_absolute_time();
1c79356b 283 if_down_all();
5ba3f43e 284 halt_log_enter("if_down_all", 0, mach_absolute_time() - startTime);
2d21ac55 285#endif /* NETWORKING */
1c79356b 286
6d2010ae 287force_reboot:
3e170ce0
A
288
289 if (howto & RB_PANIC) {
d26ffc64 290 panic_kernel(howto, message);
3e170ce0
A
291 }
292
0a7de745 293 if (howto & RB_HALT) {
1c79356b 294 hostboot_option = HOST_REBOOT_HALT;
0a7de745 295 }
1c79356b 296
6d2010ae
A
297 if (howto & RB_UPSDELAY) {
298 hostboot_option = HOST_REBOOT_UPSDELAY;
299 }
0c530ab8 300
1c79356b 301 host_reboot(host_priv_self(), hostboot_option);
6d2010ae
A
302 /*
303 * should not be reached
304 */
0a7de745 305 return 0;
1c79356b
A
306}
307
b0d623f7
A
308static int
309sd_openlog(vfs_context_t ctx)
310{
311 int error = 0;
312 struct timeval tv;
0a7de745 313
b0d623f7
A
314 /* Open shutdown log */
315 if ((error = vnode_open(PROC_SHUTDOWN_LOG, (O_CREAT | FWRITE | O_NOFOLLOW), 0644, 0, &sd_logvp, ctx))) {
316 printf("Failed to open %s: error %d\n", PROC_SHUTDOWN_LOG, error);
317 sd_logvp = NULLVP;
318 return error;
319 }
320
321 vnode_setsize(sd_logvp, (off_t)0, 0, ctx);
322
323 /* Write a little header */
324 microtime(&tv);
325 sd_log(ctx, "Process shutdown log. Current time is %lu (in seconds).\n\n", tv.tv_sec);
326
327 return 0;
328}
329
330static int
331sd_closelog(vfs_context_t ctx)
332{
333 int error = 0;
334 if (sd_logvp != NULLVP) {
335 VNOP_FSYNC(sd_logvp, MNT_WAIT, ctx);
336 error = vnode_close(sd_logvp, FWRITE, ctx);
337 }
338
339 return error;
340}
341
342static void
0a7de745 343sd_log(vfs_context_t ctx, const char *fmt, ...)
b0d623f7
A
344{
345 int resid, log_error, len;
346 char logbuf[100];
347 va_list arglist;
348
349 /* If the log isn't open yet, open it */
350 if (sd_logvp == NULLVP) {
351 if (sd_openlog(ctx) != 0) {
352 /* Couldn't open, we fail out */
353 return;
354 }
355 }
356
357 va_start(arglist, fmt);
358 len = vsnprintf(logbuf, sizeof(logbuf), fmt, arglist);
359 log_error = vn_rdwr(UIO_WRITE, sd_logvp, (caddr_t)logbuf, len, sd_log_offset,
0a7de745 360 UIO_SYSSPACE, IO_UNIT | IO_NOAUTH, vfs_context_ucred(ctx), &resid, vfs_context_proc(ctx));
b0d623f7
A
361 if (log_error == EIO || log_error == 0) {
362 sd_log_offset += (len - resid);
363 }
364
365 va_end(arglist);
b0d623f7
A
366}
367
2d21ac55
A
368static int
369sd_filt1(proc_t p, void * args)
370{
371 proc_t self = current_proc();
372 struct sd_filterargs * sf = (struct sd_filterargs *)args;
0a7de745 373 int delayterm = sf->delayterm;
2d21ac55
A
374 int shutdownstate = sf->shutdownstate;
375
0a7de745
A
376 if (((p->p_flag & P_SYSTEM) != 0) || (p->p_ppid == 0)
377 || (p == self) || (p->p_stat == SZOMB)
378 || (p->p_shutdownstate != shutdownstate)
379 || ((delayterm == 0) && ((p->p_lflag & P_LDELAYTERM) == P_LDELAYTERM))
380 || ((p->p_sigcatch & sigmask(SIGTERM)) == 0)) {
381 return 0;
382 } else {
383 return 1;
384 }
2d21ac55
A
385}
386
387
39037602 388static int
2d21ac55
A
389sd_callback1(proc_t p, void * args)
390{
391 struct sd_iterargs * sd = (struct sd_iterargs *)args;
392 int signo = sd->signo;
393 int setsdstate = sd->setsdstate;
b0d623f7 394 int countproc = sd->countproc;
2d21ac55
A
395
396 proc_lock(p);
f427ee49 397 p->p_shutdownstate = (char)setsdstate;
2d21ac55
A
398 if (p->p_stat != SZOMB) {
399 proc_unlock(p);
b0d623f7
A
400 if (countproc != 0) {
401 proc_list_lock();
402 p->p_listflag |= P_LIST_EXITCOUNT;
403 proc_shutdown_exitcount++;
404 proc_list_unlock();
405 }
406
2d21ac55 407 psignal(p, signo);
0a7de745 408 if (countproc != 0) {
b0d623f7 409 sd->activecount++;
0a7de745 410 }
39037602 411 } else {
2d21ac55 412 proc_unlock(p);
39037602
A
413 }
414
415 return PROC_RETURNED;
2d21ac55
A
416}
417
418static int
419sd_filt2(proc_t p, void * args)
420{
421 proc_t self = current_proc();
422 struct sd_filterargs * sf = (struct sd_filterargs *)args;
0a7de745 423 int delayterm = sf->delayterm;
2d21ac55
A
424 int shutdownstate = sf->shutdownstate;
425
0a7de745
A
426 if (((p->p_flag & P_SYSTEM) != 0) || (p->p_ppid == 0)
427 || (p == self) || (p->p_stat == SZOMB)
428 || (p->p_shutdownstate == shutdownstate)
429 || ((delayterm == 0) && ((p->p_lflag & P_LDELAYTERM) == P_LDELAYTERM))) {
430 return 0;
431 } else {
432 return 1;
433 }
2d21ac55
A
434}
435
39037602 436static int
2d21ac55
A
437sd_callback2(proc_t p, void * args)
438{
439 struct sd_iterargs * sd = (struct sd_iterargs *)args;
440 int signo = sd->signo;
441 int setsdstate = sd->setsdstate;
b0d623f7 442 int countproc = sd->countproc;
2d21ac55
A
443
444 proc_lock(p);
f427ee49 445 p->p_shutdownstate = (char)setsdstate;
2d21ac55
A
446 if (p->p_stat != SZOMB) {
447 proc_unlock(p);
0a7de745 448 if (countproc != 0) {
b0d623f7
A
449 proc_list_lock();
450 p->p_listflag |= P_LIST_EXITCOUNT;
451 proc_shutdown_exitcount++;
452 proc_list_unlock();
453 }
2d21ac55 454 psignal(p, signo);
0a7de745 455 if (countproc != 0) {
b0d623f7 456 sd->activecount++;
0a7de745 457 }
39037602 458 } else {
2d21ac55 459 proc_unlock(p);
39037602 460 }
2d21ac55 461
39037602 462 return PROC_RETURNED;
2d21ac55
A
463}
464
39037602 465static int
2d21ac55
A
466sd_callback3(proc_t p, void * args)
467{
468 struct sd_iterargs * sd = (struct sd_iterargs *)args;
b0d623f7
A
469 vfs_context_t ctx = vfs_context_current();
470
2d21ac55
A
471 int setsdstate = sd->setsdstate;
472
473 proc_lock(p);
f427ee49 474 p->p_shutdownstate = (char)setsdstate;
2d21ac55 475 if (p->p_stat != SZOMB) {
0a7de745
A
476 /*
477 * NOTE: following code ignores sig_lock and plays
478 * with exit_thread correctly. This is OK unless we
479 * are a multiprocessor, in which case I do not
480 * understand the sig_lock. This needs to be fixed.
481 * XXX
482 */
483 if (p->exit_thread) { /* someone already doing it */
2d21ac55
A
484 proc_unlock(p);
485 /* give him a chance */
486 thread_block(THREAD_CONTINUE_NULL);
487 } else {
488 p->exit_thread = current_thread();
489 printf(".");
b0d623f7
A
490
491 sd_log(ctx, "%s[%d] had to be forced closed with exit1().\n", p->p_comm, p->p_pid);
492
2d21ac55 493 proc_unlock(p);
b0d623f7 494 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
0a7de745 495 p->p_pid, 0, 1, 0, 0);
b0d623f7 496 sd->activecount++;
2d21ac55
A
497 exit1(p, 1, (int *)NULL);
498 }
39037602 499 } else {
2d21ac55 500 proc_unlock(p);
39037602 501 }
2d21ac55 502
39037602 503 return PROC_RETURNED;
2d21ac55
A
504}
505
506
1c79356b
A
507/*
508 * proc_shutdown()
509 *
510 * Shutdown down proc system (release references to current and root
511 * dirs for each process).
512 *
513 * POSIX modifications:
514 *
5ba3f43e 515 * For POSIX fcntl() file locking call vno_lockrelease() on
1c79356b
A
516 * the file to release all of its record locks, if any.
517 */
518
519static void
2d21ac55 520proc_shutdown(void)
1c79356b 521{
b0d623f7
A
522 vfs_context_t ctx = vfs_context_current();
523 struct proc *p, *self;
91447636 524 int delayterm = 0;
2d21ac55
A
525 struct sd_filterargs sfargs;
526 struct sd_iterargs sdargs;
b0d623f7
A
527 int error = 0;
528 struct timespec ts;
1c79356b
A
529
530 /*
531 * Kill as many procs as we can. (Except ourself...)
532 */
0b4e3aa0 533 self = (struct proc *)current_proc();
5ba3f43e 534
1c79356b 535 /*
91447636 536 * Signal the init with SIGTERM so that he does not launch
5ba3f43e 537 * new processes
1c79356b 538 */
2d21ac55 539 p = proc_find(1);
91447636
A
540 if (p && p != self) {
541 psignal(p, SIGTERM);
542 }
2d21ac55 543 proc_rele(p);
1c79356b 544
1c79356b
A
545 printf("Killing all processes ");
546
2d21ac55 547sigterm_loop:
1c79356b
A
548 /*
549 * send SIGTERM to those procs interested in catching one
550 */
2d21ac55
A
551 sfargs.delayterm = delayterm;
552 sfargs.shutdownstate = 0;
553 sdargs.signo = SIGTERM;
554 sdargs.setsdstate = 1;
b0d623f7
A
555 sdargs.countproc = 1;
556 sdargs.activecount = 0;
2d21ac55 557
b0d623f7 558 error = 0;
2d21ac55
A
559 /* post a SIGTERM to all that catch SIGTERM and not marked for delay */
560 proc_rebootscan(sd_callback1, (void *)&sdargs, sd_filt1, (void *)&sfargs);
91447636 561
0a7de745 562 if (sdargs.activecount != 0 && proc_shutdown_exitcount != 0) {
2d21ac55 563 proc_list_lock();
b0d623f7
A
564 if (proc_shutdown_exitcount != 0) {
565 /*
5ba3f43e
A
566 * now wait for up to 3 seconds to allow those procs catching SIGTERM
567 * to digest it
568 * as soon as these procs have exited, we'll continue on to the next step
569 */
570 ts.tv_sec = 3;
b0d623f7
A
571 ts.tv_nsec = 0;
572 error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts);
573 if (error != 0) {
574 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
0a7de745 575 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
b0d623f7 576 p->p_listflag &= ~P_LIST_EXITCOUNT;
0a7de745 577 }
b0d623f7
A
578 }
579 for (p = zombproc.lh_first; p; p = p->p_list.le_next) {
0a7de745 580 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
b0d623f7 581 p->p_listflag &= ~P_LIST_EXITCOUNT;
0a7de745 582 }
b0d623f7 583 }
91447636 584 }
1c79356b 585 }
2d21ac55 586 proc_list_unlock();
1c79356b 587 }
b0d623f7 588 if (error == ETIMEDOUT) {
91447636 589 /*
55e303ae
A
590 * log the names of the unresponsive tasks
591 */
91447636 592
2d21ac55
A
593 proc_list_lock();
594
b0d623f7 595 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
91447636 596 if (p->p_shutdownstate == 1) {
b0d623f7
A
597 printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid);
598 sd_log(ctx, "%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid);
91447636 599 }
55e303ae 600 }
2d21ac55
A
601
602 proc_list_unlock();
55e303ae 603 }
1c79356b
A
604
605 /*
606 * send a SIGKILL to all the procs still hanging around
607 */
2d21ac55
A
608 sfargs.delayterm = delayterm;
609 sfargs.shutdownstate = 2;
610 sdargs.signo = SIGKILL;
611 sdargs.setsdstate = 2;
b0d623f7
A
612 sdargs.countproc = 1;
613 sdargs.activecount = 0;
2d21ac55 614
b0d623f7 615 /* post a SIGKILL to all that catch SIGTERM and not marked for delay */
2d21ac55 616 proc_rebootscan(sd_callback2, (void *)&sdargs, sd_filt2, (void *)&sfargs);
91447636 617
5ba3f43e
A
618 error = 0;
619
0a7de745 620 if (sdargs.activecount != 0 && proc_shutdown_exitcount != 0) {
2d21ac55 621 proc_list_lock();
b0d623f7
A
622 if (proc_shutdown_exitcount != 0) {
623 /*
0a7de745
A
624 * wait for up to 60 seconds to allow these procs to exit normally
625 *
626 * History: The delay interval was changed from 100 to 200
627 * for NFS requests in particular.
628 */
5ba3f43e 629 ts.tv_sec = 10;
b0d623f7
A
630 ts.tv_nsec = 0;
631 error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts);
632 if (error != 0) {
633 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
0a7de745 634 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
b0d623f7 635 p->p_listflag &= ~P_LIST_EXITCOUNT;
0a7de745 636 }
b0d623f7
A
637 }
638 for (p = zombproc.lh_first; p; p = p->p_list.le_next) {
0a7de745 639 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
b0d623f7 640 p->p_listflag &= ~P_LIST_EXITCOUNT;
0a7de745 641 }
b0d623f7
A
642 }
643 }
1c79356b 644 }
2d21ac55 645 proc_list_unlock();
1c79356b
A
646 }
647
5ba3f43e
A
648 if (error == ETIMEDOUT) {
649 /*
650 * log the names of the unresponsive tasks
651 */
652
653 proc_list_lock();
654
655 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
656 if (p->p_shutdownstate == 2) {
657 printf("%s[%d]: didn't act on SIGKILL\n", p->p_comm, p->p_pid);
658 sd_log(ctx, "%s[%d]: didn't act on SIGKILL\n", p->p_comm, p->p_pid);
659 }
660 }
661
662 proc_list_unlock();
663 }
664
1c79356b
A
665 /*
666 * if we still have procs that haven't exited, then brute force 'em
667 */
2d21ac55
A
668 sfargs.delayterm = delayterm;
669 sfargs.shutdownstate = 3;
670 sdargs.signo = 0;
671 sdargs.setsdstate = 3;
b0d623f7
A
672 sdargs.countproc = 0;
673 sdargs.activecount = 0;
4a249263 674
5ba3f43e
A
675
676
2d21ac55
A
677 /* post a SIGTERM to all that catch SIGTERM and not marked for delay */
678 proc_rebootscan(sd_callback3, (void *)&sdargs, sd_filt2, (void *)&sfargs);
679 printf("\n");
91447636
A
680
681 /* Now start the termination of processes that are marked for delayed termn */
682 if (delayterm == 0) {
683 delayterm = 1;
684 goto sigterm_loop;
1c79356b 685 }
b0d623f7
A
686
687 sd_closelog(ctx);
688
3e170ce0
A
689 /*
690 * Now that all other processes have been terminated, suspend init
691 */
692 task_suspend_internal(initproc->task);
693
2d21ac55
A
694 /* drop the ref on initproc */
695 proc_rele(initproc);
1c79356b
A
696 printf("continuing\n");
697}