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