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