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