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