kern_shutdown.c revision 90361
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 90361 2002-02-07 20:58:47Z julian $ 40 */ 41 42#include "opt_ddb.h" 43#include "opt_hw_wdog.h" 44#include "opt_panic.h" 45#include "opt_show_busybufs.h" 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/bio.h> 50#include <sys/buf.h> 51#include <sys/conf.h> 52#include <sys/cons.h> 53#include <sys/disklabel.h> 54#include <sys/eventhandler.h> 55#include <sys/kernel.h> 56#include <sys/kthread.h> 57#include <sys/malloc.h> 58#include <sys/mount.h> 59#include <sys/proc.h> 60#include <sys/reboot.h> 61#include <sys/resourcevar.h> 62#include <sys/smp.h> /* smp_active */ 63#include <sys/sysctl.h> 64#include <sys/sysproto.h> 65#include <sys/vnode.h> 66 67#include <machine/pcb.h> 68#include <machine/md_var.h> 69 70#include <sys/signalvar.h> 71#ifdef DDB 72#include <ddb/ddb.h> 73#endif 74 75#ifndef PANIC_REBOOT_WAIT_TIME 76#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 77#endif 78 79/* 80 * Note that stdarg.h and the ANSI style va_start macro is used for both 81 * ANSI and traditional C compilers. 82 */ 83#include <machine/stdarg.h> 84 85#ifdef DDB 86#ifdef DDB_UNATTENDED 87int debugger_on_panic = 0; 88#else 89int debugger_on_panic = 1; 90#endif 91SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 92 &debugger_on_panic, 0, "Run debugger on kernel panic"); 93#endif 94 95int sync_on_panic = 1; 96SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 97 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 98 99SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 100 101#ifdef HW_WDOG 102/* 103 * If there is a hardware watchdog, point this at the function needed to 104 * hold it off. 105 * It's needed when the kernel needs to do some lengthy operations. 106 * e.g. in wd.c when dumping core.. It's most annoying to have 107 * your precious core-dump only half written because the wdog kicked in. 108 */ 109watchdog_tickle_fn wdog_tickler = NULL; 110#endif /* HW_WDOG */ 111 112/* 113 * Variable panicstr contains argument to first call to panic; used as flag 114 * to indicate that the kernel has already called panic. 115 */ 116const char *panicstr; 117 118int dumping; /* system is dumping */ 119 120static void boot(int) __dead2; 121static void dumpsys(void); 122static void poweroff_wait(void *, int); 123static void shutdown_halt(void *junk, int howto); 124static void shutdown_panic(void *junk, int howto); 125static void shutdown_reset(void *junk, int howto); 126 127/* register various local shutdown events */ 128static void 129shutdown_conf(void *unused) 130{ 131 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 132 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 133 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 134 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 135} 136 137SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 138 139/* 140 * The system call that results in a reboot 141 * 142 * MPSAFE 143 */ 144/* ARGSUSED */ 145int 146reboot(struct thread *td, struct reboot_args *uap) 147{ 148 int error; 149 150 mtx_lock(&Giant); 151 if ((error = suser_td(td)) == 0) 152 boot(uap->opt); 153 mtx_unlock(&Giant); 154 return (error); 155} 156 157/* 158 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 159 */ 160static int shutdown_howto = 0; 161 162void 163shutdown_nice(int howto) 164{ 165 shutdown_howto = howto; 166 167 /* Send a signal to init(8) and have it shutdown the world */ 168 if (initproc != NULL) { 169 PROC_LOCK(initproc); 170 psignal(initproc, SIGINT); 171 PROC_UNLOCK(initproc); 172 } else { 173 /* No init(8) running, so simply reboot */ 174 boot(RB_NOSYNC); 175 } 176 return; 177} 178static int waittime = -1; 179static struct pcb dumppcb; 180 181static void 182print_uptime(void) 183{ 184 int f; 185 struct timespec ts; 186 187 getnanouptime(&ts); 188 printf("Uptime: "); 189 f = 0; 190 if (ts.tv_sec >= 86400) { 191 printf("%ldd", (long)ts.tv_sec / 86400); 192 ts.tv_sec %= 86400; 193 f = 1; 194 } 195 if (f || ts.tv_sec >= 3600) { 196 printf("%ldh", (long)ts.tv_sec / 3600); 197 ts.tv_sec %= 3600; 198 f = 1; 199 } 200 if (f || ts.tv_sec >= 60) { 201 printf("%ldm", (long)ts.tv_sec / 60); 202 ts.tv_sec %= 60; 203 f = 1; 204 } 205 printf("%lds\n", (long)ts.tv_sec); 206} 207 208/* 209 * Go through the rigmarole of shutting down.. 210 * this used to be in machdep.c but I'll be dammned if I could see 211 * anything machine dependant in it. 212 */ 213static void 214boot(int howto) 215{ 216 217 /* collect extra flags that shutdown_nice might have set */ 218 howto |= shutdown_howto; 219 220#ifdef DDB 221 /* We are out of the debugger now. */ 222 db_active = 0; 223#endif 224 225#ifdef SMP 226 if (smp_active) 227 printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 228#endif 229 /* 230 * Do any callouts that should be done BEFORE syncing the filesystems. 231 */ 232 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 233 234 /* 235 * Now sync filesystems 236 */ 237 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 238 register struct buf *bp; 239 int iter, nbusy, pbusy; 240 int subiter; 241 242 waittime = 0; 243 printf("\nsyncing disks... "); 244 245 sync(&thread0, NULL); 246 247 /* 248 * With soft updates, some buffers that are 249 * written will be remarked as dirty until other 250 * buffers are written. 251 */ 252 for (iter = pbusy = 0; iter < 20; iter++) { 253 nbusy = 0; 254 for (bp = &buf[nbuf]; --bp >= buf; ) { 255 if ((bp->b_flags & B_INVAL) == 0 && 256 BUF_REFCNT(bp) > 0) { 257 nbusy++; 258 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 259 == B_DELWRI) { 260 /* bawrite(bp);*/ 261 nbusy++; 262 } 263 } 264 if (nbusy == 0) 265 break; 266 printf("%d ", nbusy); 267 if (nbusy < pbusy) 268 iter = 0; 269 pbusy = nbusy; 270 sync(&thread0, NULL); 271 if (curthread != NULL) { 272 DROP_GIANT(); 273 for (subiter = 0; subiter < 50 * iter; subiter++) { 274 mtx_lock_spin(&sched_lock); 275 curthread->td_proc->p_stats->p_ru.ru_nvcsw++; 276 mi_switch(); /* Allow interrupt threads to run */ 277 mtx_unlock_spin(&sched_lock); 278 DELAY(1000); 279 } 280 PICKUP_GIANT(); 281 } else 282 DELAY(50000 * iter); 283 } 284 printf("\n"); 285 /* 286 * Count only busy local buffers to prevent forcing 287 * a fsck if we're just a client of a wedged NFS server 288 */ 289 nbusy = 0; 290 for (bp = &buf[nbuf]; --bp >= buf; ) { 291 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 292 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 293 if (bp->b_dev == NODEV) { 294 TAILQ_REMOVE(&mountlist, 295 bp->b_vp->v_mount, mnt_list); 296 continue; 297 } 298 nbusy++; 299#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 300 printf( 301 "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 302 nbusy, devtoname(bp->b_dev), 303 bp->b_flags, (long)bp->b_blkno, 304 (long)bp->b_lblkno); 305#endif 306 } 307 } 308 if (nbusy) { 309 /* 310 * Failed to sync all blocks. Indicate this and don't 311 * unmount filesystems (thus forcing an fsck on reboot). 312 */ 313 printf("giving up on %d buffers\n", nbusy); 314 DELAY(5000000); /* 5 seconds */ 315 } else { 316 printf("done\n"); 317 /* 318 * Unmount filesystems 319 */ 320 if (panicstr == 0) 321 vfs_unmountall(); 322 } 323 DELAY(100000); /* wait for console output to finish */ 324 } 325 326 print_uptime(); 327 328 /* 329 * Ok, now do things that assume all filesystem activity has 330 * been completed. 331 */ 332 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 333 splhigh(); 334 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 335 dumpsys(); 336 337 /* Now that we're going to really halt the system... */ 338 EVENTHANDLER_INVOKE(shutdown_final, howto); 339 340 for(;;) ; /* safety against shutdown_reset not working */ 341 /* NOTREACHED */ 342} 343 344/* 345 * If the shutdown was a clean halt, behave accordingly. 346 */ 347static void 348shutdown_halt(void *junk, int howto) 349{ 350 if (howto & RB_HALT) { 351 printf("\n"); 352 printf("The operating system has halted.\n"); 353 printf("Please press any key to reboot.\n\n"); 354 switch (cngetc()) { 355 case -1: /* No console, just die */ 356 cpu_halt(); 357 /* NOTREACHED */ 358 default: 359 howto &= ~RB_HALT; 360 break; 361 } 362 } 363} 364 365/* 366 * Check to see if the system paniced, pause and then reboot 367 * according to the specified delay. 368 */ 369static void 370shutdown_panic(void *junk, int howto) 371{ 372 int loop; 373 374 if (howto & RB_DUMP) { 375 if (PANIC_REBOOT_WAIT_TIME != 0) { 376 if (PANIC_REBOOT_WAIT_TIME != -1) { 377 printf("Automatic reboot in %d seconds - " 378 "press a key on the console to abort\n", 379 PANIC_REBOOT_WAIT_TIME); 380 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 381 loop > 0; --loop) { 382 DELAY(1000 * 100); /* 1/10th second */ 383 /* Did user type a key? */ 384 if (cncheckc() != -1) 385 break; 386 } 387 if (!loop) 388 return; 389 } 390 } else { /* zero time specified - reboot NOW */ 391 return; 392 } 393 printf("--> Press a key on the console to reboot,\n"); 394 printf("--> or switch off the system now.\n"); 395 cngetc(); 396 } 397} 398 399/* 400 * Everything done, now reset 401 */ 402static void 403shutdown_reset(void *junk, int howto) 404{ 405 printf("Rebooting...\n"); 406 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 407 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 408 cpu_reset(); 409 /* NOTREACHED */ /* assuming reset worked */ 410} 411 412/* 413 * Magic number for savecore 414 * 415 * exported (symorder) and used at least by savecore(8) 416 * 417 */ 418static u_long const dumpmag = 0x8fca0101UL; 419 420static int dumpsize = 0; /* also for savecore */ 421 422static int dodump = 1; 423 424SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 425 "Try to perform coredump on kernel panic"); 426 427static int 428setdumpdev(dev_t dev) 429{ 430 int psize; 431 long newdumplo; 432 433 if (dev == NODEV) { 434 dumpdev = dev; 435 return (0); 436 } 437 if (devsw(dev) == NULL) 438 return (ENXIO); /* XXX is this right? */ 439 if (devsw(dev)->d_psize == NULL) 440 return (ENXIO); /* XXX should be ENODEV ? */ 441 psize = devsw(dev)->d_psize(dev); 442 if (psize == -1) 443 return (ENXIO); /* XXX should be ENODEV ? */ 444 /* 445 * XXX should clean up checking in dumpsys() to be more like this. 446 */ 447 newdumplo = psize - Maxmem * (PAGE_SIZE / DEV_BSIZE); 448 if (newdumplo <= LABELSECTOR) 449 return (ENOSPC); 450 dumpdev = dev; 451 dumplo = newdumplo; 452 return (0); 453} 454 455 456/* ARGSUSED */ 457static void 458dump_conf(void *dummy) 459{ 460 char *path; 461 dev_t dev; 462 463 path = malloc(MNAMELEN, M_TEMP, M_WAITOK); 464 if (TUNABLE_STR_FETCH("dumpdev", path, MNAMELEN) != 0) { 465 dev = getdiskbyname(path); 466 if (dev != NODEV) 467 dumpdev = dev; 468 } 469 free(path, M_TEMP); 470 if (setdumpdev(dumpdev) != 0) 471 dumpdev = NODEV; 472} 473 474SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 475 476static int 477sysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 478{ 479 int error; 480 udev_t ndumpdev; 481 482 ndumpdev = dev2udev(dumpdev); 483 error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 484 if (error == 0 && req->newptr != NULL) 485 error = setdumpdev(udev2dev(ndumpdev, 0)); 486 return (error); 487} 488 489SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 490 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 491 492/* 493 * Doadump comes here after turning off memory management and 494 * getting on the dump stack, either when called above, or by 495 * the auto-restart code. 496 */ 497static void 498dumpsys(void) 499{ 500 int error; 501 502 savectx(&dumppcb); 503 if (!dodump) 504 return; 505 if (dumpdev == NODEV) 506 return; 507 if (!(devsw(dumpdev))) 508 return; 509 if (!(devsw(dumpdev)->d_dump)) 510 return; 511 if (dumping++) { 512 dumping--; 513 printf("Dump already in progress, bailing...\n"); 514 return; 515 } 516 dumpsize = Maxmem; 517 printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 518 printf("dump "); 519 error = (*devsw(dumpdev)->d_dump)(dumpdev); 520 dumping--; 521 if (error == 0) { 522 printf("succeeded\n"); 523 return; 524 } 525 printf("failed, reason: "); 526 switch (error) { 527 case ENODEV: 528 printf("device doesn't support a dump routine\n"); 529 break; 530 531 case ENXIO: 532 printf("device bad\n"); 533 break; 534 535 case EFAULT: 536 printf("device not ready\n"); 537 break; 538 539 case EINVAL: 540 printf("area improper\n"); 541 break; 542 543 case EIO: 544 printf("i/o error\n"); 545 break; 546 547 case EINTR: 548 printf("aborted from console\n"); 549 break; 550 551 default: 552 printf("unknown, error = %d\n", error); 553 break; 554 } 555} 556 557int 558dumpstatus(vm_offset_t addr, off_t count) 559{ 560 int c; 561 562 if (addr % (1024 * 1024) == 0) { 563#ifdef HW_WDOG 564 if (wdog_tickler) 565 (*wdog_tickler)(); 566#endif 567 printf("%ld ", (long)(count / (1024 * 1024))); 568 } 569 570 if ((c = cncheckc()) == 0x03) 571 return -1; 572 else if (c != -1) 573 printf("[CTRL-C to abort] "); 574 575 return 0; 576} 577 578#ifdef SMP 579static u_int panic_cpu = NOCPU; 580#endif 581 582/* 583 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 584 * and then reboots. If we are called twice, then we avoid trying to sync 585 * the disks as this often leads to recursive panics. 586 * 587 * MPSAFE 588 */ 589void 590panic(const char *fmt, ...) 591{ 592 int bootopt; 593 va_list ap; 594 static char buf[256]; 595 596#ifdef SMP 597 /* 598 * We don't want multiple CPU's to panic at the same time, so we 599 * use panic_cpu as a simple spinlock. We have to keep checking 600 * panic_cpu if we are spinning in case the panic on the first 601 * CPU is canceled. 602 */ 603 if (panic_cpu != PCPU_GET(cpuid)) 604 while (atomic_cmpset_int(&panic_cpu, NOCPU, 605 PCPU_GET(cpuid)) == 0) 606 while (panic_cpu != NOCPU) 607 ; /* nothing */ 608#endif 609 610 bootopt = RB_AUTOBOOT | RB_DUMP; 611 if (panicstr) 612 bootopt |= RB_NOSYNC; 613 else 614 panicstr = fmt; 615 616 va_start(ap, fmt); 617 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 618 if (panicstr == fmt) 619 panicstr = buf; 620 va_end(ap); 621 printf("panic: %s\n", buf); 622#ifdef SMP 623 /* two separate prints in case of an unmapped page and trap */ 624 printf("cpuid = %d; ", PCPU_GET(cpuid)); 625#ifdef APIC_IO 626 printf("lapic.id = %08x\n", lapic.id); 627#endif 628#endif 629 630#if defined(DDB) 631 if (debugger_on_panic) 632 Debugger ("panic"); 633#ifdef RESTARTABLE_PANICS 634 /* See if the user aborted the panic, in which case we continue. */ 635 if (panicstr == NULL) { 636#ifdef SMP 637 atomic_store_rel_int(&panic_cpu, NOCPU); 638#endif 639 return; 640 } 641#endif 642#endif 643 if (!sync_on_panic) 644 bootopt |= RB_NOSYNC; 645 boot(bootopt); 646} 647 648/* 649 * Support for poweroff delay. 650 */ 651#ifndef POWEROFF_DELAY 652# define POWEROFF_DELAY 5000 653#endif 654static int poweroff_delay = POWEROFF_DELAY; 655 656SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 657 &poweroff_delay, 0, ""); 658 659static void 660poweroff_wait(void *junk, int howto) 661{ 662 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 663 return; 664 DELAY(poweroff_delay * 1000); 665} 666 667/* 668 * Some system processes (e.g. syncer) need to be stopped at appropriate 669 * points in their main loops prior to a system shutdown, so that they 670 * won't interfere with the shutdown process (e.g. by holding a disk buf 671 * to cause sync to fail). For each of these system processes, register 672 * shutdown_kproc() as a handler for one of shutdown events. 673 */ 674static int kproc_shutdown_wait = 60; 675SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 676 &kproc_shutdown_wait, 0, ""); 677 678void 679kproc_shutdown(void *arg, int howto) 680{ 681 struct proc *p; 682 int error; 683 684 if (panicstr) 685 return; 686 687 p = (struct proc *)arg; 688 printf("Waiting (max %d seconds) for system process `%s' to stop...", 689 kproc_shutdown_wait, p->p_comm); 690 error = kthread_suspend(p, kproc_shutdown_wait * hz); 691 692 if (error == EWOULDBLOCK) 693 printf("timed out\n"); 694 else 695 printf("stopped\n"); 696} 697