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