kern_shutdown.c revision 65268
1238901Sandrew/*- 2238901Sandrew * Copyright (c) 1986, 1988, 1991, 1993 3238901Sandrew * The Regents of the University of California. All rights reserved. 4238901Sandrew * (c) UNIX System Laboratories, Inc. 5238901Sandrew * All or some portions of this file are derived from material licensed 6238901Sandrew * to the University of California by American Telephone and Telegraph 7238901Sandrew * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8238901Sandrew * the permission of UNIX System Laboratories, Inc. 9238901Sandrew * 10238901Sandrew * Redistribution and use in source and binary forms, with or without 11238901Sandrew * modification, are permitted provided that the following conditions 12238901Sandrew * are met: 13238901Sandrew * 1. Redistributions of source code must retain the above copyright 14238901Sandrew * notice, this list of conditions and the following disclaimer. 15238901Sandrew * 2. Redistributions in binary form must reproduce the above copyright 16238901Sandrew * notice, this list of conditions and the following disclaimer in the 17238901Sandrew * documentation and/or other materials provided with the distribution. 18238901Sandrew * 3. All advertising materials mentioning features or use of this software 19238901Sandrew * must display the following acknowledgement: 20238901Sandrew * This product includes software developed by the University of 21238901Sandrew * California, Berkeley and its contributors. 22238901Sandrew * 4. Neither the name of the University nor the names of its contributors 23238901Sandrew * may be used to endorse or promote products derived from this software 24238901Sandrew * without specific prior written permission. 25238901Sandrew * 26238901Sandrew * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27238901Sandrew * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28238901Sandrew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29238901Sandrew * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30245641Sandrew * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31245641Sandrew * 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 65268 2000-08-31 00:08:50Z msmith $ 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/md_var.h> 67#include <machine/smp.h> /* smp_active, cpuid */ 68 69#include <sys/signalvar.h> 70 71#ifndef PANIC_REBOOT_WAIT_TIME 72#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 73#endif 74 75/* 76 * Note that stdarg.h and the ANSI style va_start macro is used for both 77 * ANSI and traditional C compilers. 78 */ 79#include <machine/stdarg.h> 80 81#ifdef DDB 82#ifdef DDB_UNATTENDED 83int debugger_on_panic = 0; 84#else 85int debugger_on_panic = 1; 86#endif 87SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 88 &debugger_on_panic, 0, "Run debugger on kernel panic"); 89#endif 90 91SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 92 93#ifdef HW_WDOG 94/* 95 * If there is a hardware watchdog, point this at the function needed to 96 * hold it off. 97 * It's needed when the kernel needs to do some lengthy operations. 98 * e.g. in wd.c when dumping core.. It's most annoying to have 99 * your precious core-dump only half written because the wdog kicked in. 100 */ 101watchdog_tickle_fn wdog_tickler = NULL; 102#endif /* HW_WDOG */ 103 104/* 105 * Variable panicstr contains argument to first call to panic; used as flag 106 * to indicate that the kernel has already called panic. 107 */ 108const char *panicstr; 109 110static void boot __P((int)) __dead2; 111static void dumpsys __P((void)); 112static int setdumpdev __P((dev_t dev)); 113static void poweroff_wait __P((void *, int)); 114static void print_uptime __P((void)); 115static void shutdown_halt __P((void *junk, int howto)); 116static void shutdown_panic __P((void *junk, int howto)); 117static void shutdown_reset __P((void *junk, int howto)); 118 119/* register various local shutdown events */ 120static void 121shutdown_conf(void *unused) 122{ 123 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 124 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 125 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 126 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 127} 128 129SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 130 131/* ARGSUSED */ 132 133/* 134 * The system call that results in a reboot 135 */ 136int 137reboot(p, uap) 138 struct proc *p; 139 struct reboot_args *uap; 140{ 141 int error; 142 143 if ((error = suser(p))) 144 return (error); 145 146 boot(uap->opt); 147 return (0); 148} 149 150/* 151 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 152 */ 153static int shutdown_howto = 0; 154 155void 156shutdown_nice(int howto) 157{ 158 shutdown_howto = howto; 159 160 /* Send a signal to init(8) and have it shutdown the world */ 161 if (initproc != NULL) { 162 psignal(initproc, SIGINT); 163 } else { 164 /* No init(8) running, so simply reboot */ 165 boot(RB_NOSYNC); 166 } 167 return; 168} 169static int waittime = -1; 170static struct pcb dumppcb; 171 172static void 173print_uptime() 174{ 175 int f; 176 struct timespec ts; 177 178 getnanouptime(&ts); 179 printf("Uptime: "); 180 f = 0; 181 if (ts.tv_sec >= 86400) { 182 printf("%ldd", ts.tv_sec / 86400); 183 ts.tv_sec %= 86400; 184 f = 1; 185 } 186 if (f || ts.tv_sec >= 3600) { 187 printf("%ldh", ts.tv_sec / 3600); 188 ts.tv_sec %= 3600; 189 f = 1; 190 } 191 if (f || ts.tv_sec >= 60) { 192 printf("%ldm", ts.tv_sec / 60); 193 ts.tv_sec %= 60; 194 f = 1; 195 } 196 printf("%lds\n", ts.tv_sec); 197} 198 199/* 200 * Go through the rigmarole of shutting down.. 201 * this used to be in machdep.c but I'll be dammned if I could see 202 * anything machine dependant in it. 203 */ 204static void 205boot(howto) 206 int howto; 207{ 208 209 /* collect extra flags that shutdown_nice might have set */ 210 howto |= shutdown_howto; 211 212#ifdef SMP 213 if (smp_active) { 214 printf("boot() called on cpu#%d\n", cpuid); 215 } 216#endif 217 /* 218 * Do any callouts that should be done BEFORE syncing the filesystems. 219 */ 220 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 221 222 /* 223 * Now sync filesystems 224 */ 225 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 226 register struct buf *bp; 227 int iter, nbusy; 228 229 waittime = 0; 230 printf("\nsyncing disks... "); 231 232 sync(&proc0, NULL); 233 234 /* 235 * With soft updates, some buffers that are 236 * written will be remarked as dirty until other 237 * buffers are written. 238 */ 239 for (iter = 0; iter < 20; iter++) { 240 nbusy = 0; 241 for (bp = &buf[nbuf]; --bp >= buf; ) { 242 if ((bp->b_flags & B_INVAL) == 0 && 243 BUF_REFCNT(bp) > 0) { 244 nbusy++; 245 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 246 == B_DELWRI) { 247 /* bawrite(bp);*/ 248 nbusy++; 249 } 250 } 251 if (nbusy == 0) 252 break; 253 printf("%d ", nbusy); 254 sync(&proc0, NULL); 255 DELAY(50000 * iter); 256 } 257 printf("\n"); 258 /* 259 * Count only busy local buffers to prevent forcing 260 * a fsck if we're just a client of a wedged NFS server 261 */ 262 nbusy = 0; 263 for (bp = &buf[nbuf]; --bp >= buf; ) { 264 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 265 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 266 if (bp->b_dev == NODEV) { 267 TAILQ_REMOVE(&mountlist, 268 bp->b_vp->v_mount, mnt_list); 269 continue; 270 } 271 nbusy++; 272#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 273 printf( 274 "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 275 nbusy, devtoname(bp->b_dev), 276 bp->b_flags, (long)bp->b_blkno, 277 (long)bp->b_lblkno); 278#endif 279 } 280 } 281 if (nbusy) { 282 /* 283 * Failed to sync all blocks. Indicate this and don't 284 * unmount filesystems (thus forcing an fsck on reboot). 285 */ 286 printf("giving up on %d buffers\n", nbusy); 287 DELAY(5000000); /* 5 seconds */ 288 } else { 289 printf("done\n"); 290 /* 291 * Unmount filesystems 292 */ 293 if (panicstr == 0) 294 vfs_unmountall(); 295 } 296 DELAY(100000); /* wait for console output to finish */ 297 } 298 299 print_uptime(); 300 301 /* 302 * Ok, now do things that assume all filesystem activity has 303 * been completed. 304 */ 305 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 306 splhigh(); 307 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) { 308 savectx(&dumppcb); 309#ifdef __i386__ 310 dumppcb.pcb_cr3 = rcr3(); 311#endif 312 dumpsys(); 313 } 314 315 /* Now that we're going to really halt the system... */ 316 EVENTHANDLER_INVOKE(shutdown_final, howto); 317 318 for(;;) ; /* safety against shutdown_reset not working */ 319 /* NOTREACHED */ 320} 321 322/* 323 * If the shutdown was a clean halt, behave accordingly. 324 */ 325static void 326shutdown_halt(void *junk, int howto) 327{ 328 if (howto & RB_HALT) { 329 printf("\n"); 330 printf("The operating system has halted.\n"); 331 printf("Please press any key to reboot.\n\n"); 332 switch (cngetc()) { 333 case -1: /* No console, just die */ 334 cpu_halt(); 335 /* NOTREACHED */ 336 default: 337 howto &= ~RB_HALT; 338 break; 339 } 340 } 341} 342 343/* 344 * Check to see if the system paniced, pause and then reboot 345 * according to the specified delay. 346 */ 347static void 348shutdown_panic(void *junk, int howto) 349{ 350 int loop; 351 352 if (howto & RB_DUMP) { 353 if (PANIC_REBOOT_WAIT_TIME != 0) { 354 if (PANIC_REBOOT_WAIT_TIME != -1) { 355 printf("Automatic reboot in %d seconds - " 356 "press a key on the console to abort\n", 357 PANIC_REBOOT_WAIT_TIME); 358 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 359 loop > 0; --loop) { 360 DELAY(1000 * 100); /* 1/10th second */ 361 /* Did user type a key? */ 362 if (cncheckc() != -1) 363 break; 364 } 365 if (!loop) 366 return; 367 } 368 } else { /* zero time specified - reboot NOW */ 369 return; 370 } 371 printf("--> Press a key on the console to reboot <--\n"); 372 cngetc(); 373 } 374} 375 376/* 377 * Everything done, now reset 378 */ 379static void 380shutdown_reset(void *junk, int howto) 381{ 382 printf("Rebooting...\n"); 383 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 384 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 385 cpu_reset(); 386 /* NOTREACHED */ /* assuming reset worked */ 387} 388 389/* 390 * Magic number for savecore 391 * 392 * exported (symorder) and used at least by savecore(8) 393 * 394 */ 395static u_long const dumpmag = 0x8fca0101UL; 396 397static int dumpsize = 0; /* also for savecore */ 398 399static int dodump = 1; 400 401SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 402 "Try to perform coredump on kernel panic"); 403 404static int 405setdumpdev(dev) 406 dev_t dev; 407{ 408 int psize; 409 long newdumplo; 410 411 if (dev == NODEV) { 412 dumpdev = dev; 413 return (0); 414 } 415 if (devsw(dev) == NULL) 416 return (ENXIO); /* XXX is this right? */ 417 if (devsw(dev)->d_psize == NULL) 418 return (ENXIO); /* XXX should be ENODEV ? */ 419 psize = devsw(dev)->d_psize(dev); 420 if (psize == -1) 421 return (ENXIO); /* XXX should be ENODEV ? */ 422 /* 423 * XXX should clean up checking in dumpsys() to be more like this. 424 */ 425 newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE; 426 if (newdumplo < 0) 427 return (ENOSPC); 428 dumpdev = dev; 429 dumplo = newdumplo; 430 return (0); 431} 432 433 434/* ARGSUSED */ 435static void dump_conf __P((void *dummy)); 436static void 437dump_conf(dummy) 438 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 if (dumping++) { 474 printf("Dump already in progress, bailing...\n"); 475 return; 476 } 477 if (!dodump) 478 return; 479 if (dumpdev == NODEV) 480 return; 481 if (!(devsw(dumpdev))) 482 return; 483 if (!(devsw(dumpdev)->d_dump)) 484 return; 485 dumpsize = Maxmem; 486 printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 487 printf("dump "); 488 error = (*devsw(dumpdev)->d_dump)(dumpdev); 489 if (error == 0) { 490 printf("succeeded\n"); 491 return; 492 } 493 printf("failed, reason: "); 494 switch (error) { 495 case ENODEV: 496 printf("device doesn't support a dump routine\n"); 497 break; 498 499 case ENXIO: 500 printf("device bad\n"); 501 break; 502 503 case EFAULT: 504 printf("device not ready\n"); 505 break; 506 507 case EINVAL: 508 printf("area improper\n"); 509 break; 510 511 case EIO: 512 printf("i/o error\n"); 513 break; 514 515 case EINTR: 516 printf("aborted from console\n"); 517 break; 518 519 default: 520 printf("unknown, error = %d\n", error); 521 break; 522 } 523} 524 525/* 526 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 527 * and then reboots. If we are called twice, then we avoid trying to sync 528 * the disks as this often leads to recursive panics. 529 */ 530void 531panic(const char *fmt, ...) 532{ 533 int bootopt; 534 va_list ap; 535 static char buf[256]; 536 537 bootopt = RB_AUTOBOOT | RB_DUMP; 538 if (panicstr) 539 bootopt |= RB_NOSYNC; 540 else 541 panicstr = fmt; 542 543 va_start(ap, fmt); 544 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 545 if (panicstr == fmt) 546 panicstr = buf; 547 va_end(ap); 548 printf("panic: %s\n", buf); 549#ifdef SMP 550 /* three seperate prints in case of an unmapped page and trap */ 551 printf("mp_lock = %08x; ", mp_lock); 552 printf("cpuid = %d; ", cpuid); 553 printf("lapic.id = %08x\n", lapic.id); 554#endif 555 556#if defined(DDB) 557 if (debugger_on_panic) 558 Debugger ("panic"); 559#endif 560 boot(bootopt); 561} 562 563/* 564 * Support for poweroff delay. 565 */ 566#ifndef POWEROFF_DELAY 567# define POWEROFF_DELAY 5000 568#endif 569static int poweroff_delay = POWEROFF_DELAY; 570 571SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 572 &poweroff_delay, 0, ""); 573 574static void 575poweroff_wait(void *junk, int howto) 576{ 577 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 578 return; 579 DELAY(poweroff_delay * 1000); 580} 581 582/* 583 * Some system processes (e.g. syncer) need to be stopped at appropriate 584 * points in their main loops prior to a system shutdown, so that they 585 * won't interfere with the shutdown process (e.g. by holding a disk buf 586 * to cause sync to fail). For each of these system processes, register 587 * shutdown_kproc() as a handler for one of shutdown events. 588 */ 589static int kproc_shutdown_wait = 60; 590SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 591 &kproc_shutdown_wait, 0, ""); 592 593void 594shutdown_kproc(void *arg, int howto) 595{ 596 struct proc *p; 597 int error; 598 599 if (panicstr) 600 return; 601 602 p = (struct proc *)arg; 603 printf("Waiting (max %d seconds) for system process `%s' to stop...", 604 kproc_shutdown_wait, p->p_comm); 605 error = suspend_kproc(p, kproc_shutdown_wait * hz); 606 607 if (error == EWOULDBLOCK) 608 printf("timed out\n"); 609 else 610 printf("stopped\n"); 611} 612