kern_shutdown.c revision 113633
1/*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 39 * $FreeBSD: head/sys/kern/kern_shutdown.c 113633 2003-04-17 22:29:23Z jhb $ 40 */ 41 42#include "opt_ddb.h" 43#include "opt_ddb_trace.h" 44#include "opt_ddb_unattended.h" 45#include "opt_hw_wdog.h" 46#include "opt_mac.h" 47#include "opt_panic.h" 48#include "opt_show_busybufs.h" 49 50#include <sys/param.h> 51#include <sys/systm.h> 52#include <sys/bio.h> 53#include <sys/buf.h> 54#include <sys/conf.h> 55#include <sys/cons.h> 56#include <sys/eventhandler.h> 57#include <sys/kernel.h> 58#include <sys/kthread.h> 59#include <sys/mac.h> 60#include <sys/malloc.h> 61#include <sys/mount.h> 62#include <sys/proc.h> 63#include <sys/reboot.h> 64#include <sys/resourcevar.h> 65#include <sys/smp.h> /* smp_active */ 66#include <sys/sysctl.h> 67#include <sys/sysproto.h> 68#include <sys/vnode.h> 69 70#include <machine/pcb.h> 71#include <machine/md_var.h> 72#include <machine/smp.h> 73 74#include <sys/signalvar.h> 75#ifdef DDB 76#include <ddb/ddb.h> 77#endif 78 79#ifndef PANIC_REBOOT_WAIT_TIME 80#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 81#endif 82 83/* 84 * Note that stdarg.h and the ANSI style va_start macro is used for both 85 * ANSI and traditional C compilers. 86 */ 87#include <machine/stdarg.h> 88 89#ifdef DDB 90#ifdef DDB_UNATTENDED 91int debugger_on_panic = 0; 92#else 93int debugger_on_panic = 1; 94#endif 95SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 96 &debugger_on_panic, 0, "Run debugger on kernel panic"); 97 98#ifdef DDB_TRACE 99int trace_on_panic = 1; 100#else 101int trace_on_panic = 0; 102#endif 103SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW, 104 &trace_on_panic, 0, "Print stack trace on kernel panic"); 105#endif 106 107int sync_on_panic = 1; 108SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 109 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 110 111SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 112 113#ifdef HW_WDOG 114/* 115 * If there is a hardware watchdog, point this at the function needed to 116 * hold it off. 117 * It's needed when the kernel needs to do some lengthy operations. 118 * e.g. in wd.c when dumping core.. It's most annoying to have 119 * your precious core-dump only half written because the wdog kicked in. 120 */ 121watchdog_tickle_fn wdog_tickler = NULL; 122#endif /* HW_WDOG */ 123 124/* 125 * Variable panicstr contains argument to first call to panic; used as flag 126 * to indicate that the kernel has already called panic. 127 */ 128const char *panicstr; 129 130int dumping; /* system is dumping */ 131static struct dumperinfo dumper; /* our selected dumper */ 132static struct pcb dumppcb; /* "You Are Here" sign for dump-debuggers */ 133 134static void boot(int) __dead2; 135static void poweroff_wait(void *, int); 136static void shutdown_halt(void *junk, int howto); 137static void shutdown_panic(void *junk, int howto); 138static void shutdown_reset(void *junk, int howto); 139 140/* register various local shutdown events */ 141static void 142shutdown_conf(void *unused) 143{ 144 145 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 146 SHUTDOWN_PRI_FIRST); 147 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 148 SHUTDOWN_PRI_LAST + 100); 149 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 150 SHUTDOWN_PRI_LAST + 100); 151 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 152 SHUTDOWN_PRI_LAST + 200); 153} 154 155SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 156 157/* 158 * The system call that results in a reboot 159 * 160 * MPSAFE 161 */ 162/* ARGSUSED */ 163int 164reboot(struct thread *td, struct reboot_args *uap) 165{ 166 int error; 167 168 error = 0; 169#ifdef MAC 170 error = mac_check_system_reboot(td->td_ucred, uap->opt); 171#endif 172 if (error == 0) 173 error = suser(td); 174 if (error == 0) { 175 mtx_lock(&Giant); 176 boot(uap->opt); 177 mtx_unlock(&Giant); 178 } 179 return (error); 180} 181 182/* 183 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 184 */ 185static int shutdown_howto = 0; 186 187void 188shutdown_nice(int howto) 189{ 190 191 shutdown_howto = howto; 192 193 /* Send a signal to init(8) and have it shutdown the world */ 194 if (initproc != NULL) { 195 PROC_LOCK(initproc); 196 psignal(initproc, SIGINT); 197 PROC_UNLOCK(initproc); 198 } else { 199 /* No init(8) running, so simply reboot */ 200 boot(RB_NOSYNC); 201 } 202 return; 203} 204static int waittime = -1; 205 206static void 207print_uptime(void) 208{ 209 int f; 210 struct timespec ts; 211 212 getnanouptime(&ts); 213 printf("Uptime: "); 214 f = 0; 215 if (ts.tv_sec >= 86400) { 216 printf("%ldd", (long)ts.tv_sec / 86400); 217 ts.tv_sec %= 86400; 218 f = 1; 219 } 220 if (f || ts.tv_sec >= 3600) { 221 printf("%ldh", (long)ts.tv_sec / 3600); 222 ts.tv_sec %= 3600; 223 f = 1; 224 } 225 if (f || ts.tv_sec >= 60) { 226 printf("%ldm", (long)ts.tv_sec / 60); 227 ts.tv_sec %= 60; 228 f = 1; 229 } 230 printf("%lds\n", (long)ts.tv_sec); 231} 232 233static void 234doadump(void) 235{ 236 237 savectx(&dumppcb); 238 dumping++; 239 dumpsys(&dumper); 240} 241 242/* 243 * Go through the rigmarole of shutting down.. 244 * this used to be in machdep.c but I'll be dammned if I could see 245 * anything machine dependant in it. 246 */ 247static void 248boot(int howto) 249{ 250 251 /* collect extra flags that shutdown_nice might have set */ 252 howto |= shutdown_howto; 253 254#ifdef DDB 255 /* We are out of the debugger now. */ 256 db_active = 0; 257#endif 258 259#ifdef SMP 260 if (smp_active) 261 printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 262#endif 263 /* 264 * Do any callouts that should be done BEFORE syncing the filesystems. 265 */ 266 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 267 268 /* 269 * Now sync filesystems 270 */ 271 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 272 register struct buf *bp; 273 int iter, nbusy, pbusy; 274 int subiter; 275 276 waittime = 0; 277 printf("\nsyncing disks, buffers remaining... "); 278 279 sync(&thread0, NULL); 280 281 /* 282 * With soft updates, some buffers that are 283 * written will be remarked as dirty until other 284 * buffers are written. 285 */ 286 for (iter = pbusy = 0; iter < 20; iter++) { 287 nbusy = 0; 288 for (bp = &buf[nbuf]; --bp >= buf; ) { 289 if ((bp->b_flags & B_INVAL) == 0 && 290 BUF_REFCNT(bp) > 0) { 291 nbusy++; 292 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 293 == B_DELWRI) { 294 /* bawrite(bp);*/ 295 nbusy++; 296 } 297 } 298 if (nbusy == 0) 299 break; 300 printf("%d ", nbusy); 301 if (nbusy < pbusy) 302 iter = 0; 303 pbusy = nbusy; 304 sync(&thread0, NULL); 305 if (curthread != NULL) { 306 DROP_GIANT(); 307 for (subiter = 0; subiter < 50 * iter; subiter++) { 308 mtx_lock_spin(&sched_lock); 309 curthread->td_proc->p_stats->p_ru.ru_nvcsw++; 310 mi_switch(); /* Allow interrupt threads to run */ 311 mtx_unlock_spin(&sched_lock); 312 DELAY(1000); 313 } 314 PICKUP_GIANT(); 315 } else 316 DELAY(50000 * iter); 317 } 318 printf("\n"); 319 /* 320 * Count only busy local buffers to prevent forcing 321 * a fsck if we're just a client of a wedged NFS server 322 */ 323 nbusy = 0; 324 for (bp = &buf[nbuf]; --bp >= buf; ) { 325 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 326 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 327 if (bp->b_dev == NODEV) { 328 TAILQ_REMOVE(&mountlist, 329 bp->b_vp->v_mount, mnt_list); 330 continue; 331 } 332 nbusy++; 333#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 334 printf( 335 "%d: dev:%s, flags:%0x, blkno:%ld, lblkno:%ld\n", 336 nbusy, devtoname(bp->b_dev), 337 bp->b_flags, (long)bp->b_blkno, 338 (long)bp->b_lblkno); 339#endif 340 } 341 } 342 if (nbusy) { 343 /* 344 * Failed to sync all blocks. Indicate this and don't 345 * unmount filesystems (thus forcing an fsck on reboot). 346 */ 347 printf("giving up on %d buffers\n", nbusy); 348 DELAY(5000000); /* 5 seconds */ 349 } else { 350 printf("done\n"); 351 /* 352 * Unmount filesystems 353 */ 354 if (panicstr == 0) 355 vfs_unmountall(); 356 } 357 DELAY(100000); /* wait for console output to finish */ 358 } 359 360 print_uptime(); 361 362 /* 363 * Ok, now do things that assume all filesystem activity has 364 * been completed. 365 */ 366 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 367 splhigh(); 368 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && 369 !cold && dumper.dumper != NULL && !dumping) 370 doadump(); 371 372 /* Now that we're going to really halt the system... */ 373 EVENTHANDLER_INVOKE(shutdown_final, howto); 374 375 for(;;) ; /* safety against shutdown_reset not working */ 376 /* NOTREACHED */ 377} 378 379/* 380 * If the shutdown was a clean halt, behave accordingly. 381 */ 382static void 383shutdown_halt(void *junk, int howto) 384{ 385 386 if (howto & RB_HALT) { 387 printf("\n"); 388 printf("The operating system has halted.\n"); 389 printf("Please press any key to reboot.\n\n"); 390 switch (cngetc()) { 391 case -1: /* No console, just die */ 392 cpu_halt(); 393 /* NOTREACHED */ 394 default: 395 howto &= ~RB_HALT; 396 break; 397 } 398 } 399} 400 401/* 402 * Check to see if the system paniced, pause and then reboot 403 * according to the specified delay. 404 */ 405static void 406shutdown_panic(void *junk, int howto) 407{ 408 int loop; 409 410 if (howto & RB_DUMP) { 411 if (PANIC_REBOOT_WAIT_TIME != 0) { 412 if (PANIC_REBOOT_WAIT_TIME != -1) { 413 printf("Automatic reboot in %d seconds - " 414 "press a key on the console to abort\n", 415 PANIC_REBOOT_WAIT_TIME); 416 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 417 loop > 0; --loop) { 418 DELAY(1000 * 100); /* 1/10th second */ 419 /* Did user type a key? */ 420 if (cncheckc() != -1) 421 break; 422 } 423 if (!loop) 424 return; 425 } 426 } else { /* zero time specified - reboot NOW */ 427 return; 428 } 429 printf("--> Press a key on the console to reboot,\n"); 430 printf("--> or switch off the system now.\n"); 431 cngetc(); 432 } 433} 434 435/* 436 * Everything done, now reset 437 */ 438static void 439shutdown_reset(void *junk, int howto) 440{ 441 442 printf("Rebooting...\n"); 443 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 444 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 445 cpu_reset(); 446 /* NOTREACHED */ /* assuming reset worked */ 447} 448 449/* 450 * Print a backtrace if we can. 451 */ 452 453void 454backtrace(void) 455{ 456 457#ifdef DDB 458 printf("Stack backtrace:\n"); 459 db_print_backtrace(); 460#else 461 printf("Sorry, need DDB option to print backtrace"); 462#endif 463} 464 465#ifdef SMP 466static u_int panic_cpu = NOCPU; 467#endif 468 469/* 470 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 471 * and then reboots. If we are called twice, then we avoid trying to sync 472 * the disks as this often leads to recursive panics. 473 * 474 * MPSAFE 475 */ 476void 477panic(const char *fmt, ...) 478{ 479 struct thread *td = curthread; 480 int bootopt, newpanic; 481 va_list ap; 482 static char buf[256]; 483 484#ifdef SMP 485 /* 486 * We don't want multiple CPU's to panic at the same time, so we 487 * use panic_cpu as a simple spinlock. We have to keep checking 488 * panic_cpu if we are spinning in case the panic on the first 489 * CPU is canceled. 490 */ 491 if (panic_cpu != PCPU_GET(cpuid)) 492 while (atomic_cmpset_int(&panic_cpu, NOCPU, 493 PCPU_GET(cpuid)) == 0) 494 while (panic_cpu != NOCPU) 495 ; /* nothing */ 496#endif 497 498 bootopt = RB_AUTOBOOT | RB_DUMP; 499 newpanic = 0; 500 if (panicstr) 501 bootopt |= RB_NOSYNC; 502 else { 503 panicstr = fmt; 504 newpanic = 1; 505 } 506 507 va_start(ap, fmt); 508 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 509 if (panicstr == fmt) 510 panicstr = buf; 511 va_end(ap); 512 printf("panic: %s\n", buf); 513#ifdef SMP 514 /* two separate prints in case of an unmapped page and trap */ 515 printf("cpuid = %d; ", PCPU_GET(cpuid)); 516#ifdef APIC_IO 517 printf("lapic.id = %08x\n", lapic.id); 518#else 519 printf("\n"); 520#endif 521#endif 522 523#if defined(DDB) 524 if (newpanic && trace_on_panic) 525 backtrace(); 526 if (debugger_on_panic) 527 Debugger ("panic"); 528#ifdef RESTARTABLE_PANICS 529 /* See if the user aborted the panic, in which case we continue. */ 530 if (panicstr == NULL) { 531#ifdef SMP 532 atomic_store_rel_int(&panic_cpu, NOCPU); 533#endif 534 return; 535 } 536#endif 537#endif 538 mtx_lock_spin(&sched_lock); 539 td->td_flags |= TDF_INPANIC; 540 mtx_unlock_spin(&sched_lock); 541 if (!sync_on_panic) 542 bootopt |= RB_NOSYNC; 543 boot(bootopt); 544} 545 546/* 547 * Support for poweroff delay. 548 */ 549#ifndef POWEROFF_DELAY 550# define POWEROFF_DELAY 5000 551#endif 552static int poweroff_delay = POWEROFF_DELAY; 553 554SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 555 &poweroff_delay, 0, ""); 556 557static void 558poweroff_wait(void *junk, int howto) 559{ 560 561 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 562 return; 563 DELAY(poweroff_delay * 1000); 564} 565 566/* 567 * Some system processes (e.g. syncer) need to be stopped at appropriate 568 * points in their main loops prior to a system shutdown, so that they 569 * won't interfere with the shutdown process (e.g. by holding a disk buf 570 * to cause sync to fail). For each of these system processes, register 571 * shutdown_kproc() as a handler for one of shutdown events. 572 */ 573static int kproc_shutdown_wait = 60; 574SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 575 &kproc_shutdown_wait, 0, ""); 576 577void 578kproc_shutdown(void *arg, int howto) 579{ 580 struct proc *p; 581 int error; 582 583 if (panicstr) 584 return; 585 586 p = (struct proc *)arg; 587 printf("Waiting (max %d seconds) for system process `%s' to stop...", 588 kproc_shutdown_wait, p->p_comm); 589 error = kthread_suspend(p, kproc_shutdown_wait * hz); 590 591 if (error == EWOULDBLOCK) 592 printf("timed out\n"); 593 else 594 printf("stopped\n"); 595} 596 597/* Registration of dumpers */ 598int 599set_dumper(struct dumperinfo *di) 600{ 601 602 if (di == NULL) { 603 bzero(&dumper, sizeof dumper); 604 return (0); 605 } 606 if (dumper.dumper != NULL) 607 return (EBUSY); 608 dumper = *di; 609 return (0); 610} 611 612#if defined(__powerpc__) 613void 614dumpsys(struct dumperinfo *di __unused) 615{ 616 617 printf("Kernel dumps not implemented on this architecture\n"); 618} 619#endif 620