kern_shutdown.c revision 48431
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 * $Id: kern_shutdown.c,v 1.53 1999/06/26 02:46:02 mckusick Exp $ 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/buf.h> 50#include <sys/reboot.h> 51#include <sys/proc.h> 52#include <sys/vnode.h> 53#include <sys/malloc.h> 54#include <sys/kernel.h> 55#include <sys/mount.h> 56#include <sys/queue.h> 57#include <sys/sysctl.h> 58#include <sys/conf.h> 59#include <sys/sysproto.h> 60 61#include <machine/pcb.h> 62#include <machine/clock.h> 63#include <machine/cons.h> 64#include <machine/md_var.h> 65#ifdef SMP 66#include <machine/smp.h> /* smp_active, cpuid */ 67#endif 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 110/* 111 * callout list for things to do a shutdown 112 */ 113typedef struct shutdown_list_element { 114 LIST_ENTRY(shutdown_list_element) links; 115 bootlist_fn function; 116 void *arg; 117 int priority; 118} *sle_p; 119 120/* 121 * There are three shutdown lists. Some things need to be shut down 122 * earlier than others. 123 */ 124LIST_HEAD(shutdown_list, shutdown_list_element); 125 126static struct shutdown_list shutdown_lists[SHUTDOWN_FINAL + 1]; 127 128static void boot __P((int)) __dead2; 129static void dumpsys __P((void)); 130 131#ifndef _SYS_SYSPROTO_H_ 132struct reboot_args { 133 int opt; 134}; 135#endif 136/* ARGSUSED */ 137 138/* 139 * The system call that results in a reboot 140 */ 141int 142reboot(p, uap) 143 struct proc *p; 144 struct reboot_args *uap; 145{ 146 int error; 147 148 if ((error = suser(p))) 149 return (error); 150 151 boot(uap->opt); 152 return (0); 153} 154 155/* 156 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 157 */ 158void 159shutdown_nice() 160{ 161 /* Send a signal to init(8) and have it shutdown the world */ 162 if (initproc != NULL) { 163 psignal(initproc, SIGINT); 164 } else { 165 /* No init(8) running, so simply reboot */ 166 boot(RB_NOSYNC); 167 } 168 return; 169} 170static int waittime = -1; 171static struct pcb dumppcb; 172 173/* 174 * Go through the rigmarole of shutting down.. 175 * this used to be in machdep.c but I'll be dammned if I could see 176 * anything machine dependant in it. 177 */ 178static void 179boot(howto) 180 int howto; 181{ 182 sle_p ep; 183 184#ifdef SMP 185 if (smp_active) { 186 printf("boot() called on cpu#%d\n", cpuid); 187 } 188#endif 189 /* 190 * Do any callouts that should be done BEFORE syncing the filesystems. 191 */ 192 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_PRE_SYNC], links) 193 (*ep->function)(howto, ep->arg); 194 195 /* 196 * Now sync filesystems 197 */ 198 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 199 register struct buf *bp; 200 int iter, nbusy; 201 202 waittime = 0; 203 printf("\nsyncing disks... "); 204 205 sync(&proc0, NULL); 206 207 /* 208 * With soft updates, some buffers that are 209 * written will be remarked as dirty until other 210 * buffers are written. 211 */ 212 for (iter = 0; iter < 20; iter++) { 213 nbusy = 0; 214 for (bp = &buf[nbuf]; --bp >= buf; ) { 215 if ((bp->b_flags & B_INVAL) == 0 && 216 BUF_REFCNT(bp) > 0) { 217 nbusy++; 218 } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 219 == B_DELWRI) { 220 /* bawrite(bp);*/ 221 nbusy++; 222 } 223 } 224 if (nbusy == 0) 225 break; 226 printf("%d ", nbusy); 227 sync(&proc0, NULL); 228 DELAY(50000 * iter); 229 } 230 /* 231 * Count only busy local buffers to prevent forcing 232 * a fsck if we're just a client of a wedged NFS server 233 */ 234 nbusy = 0; 235 for (bp = &buf[nbuf]; --bp >= buf; ) { 236 if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 237 ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 238 if (bp->b_dev == NODEV) 239 CIRCLEQ_REMOVE(&mountlist, 240 bp->b_vp->v_mount, mnt_list); 241 else 242 nbusy++; 243 } 244 245 246 } 247 if (nbusy) { 248 /* 249 * Failed to sync all blocks. Indicate this and don't 250 * unmount filesystems (thus forcing an fsck on reboot). 251 */ 252 printf("giving up\n"); 253#ifdef SHOW_BUSYBUFS 254 nbusy = 0; 255 for (bp = &buf[nbuf]; --bp >= buf; ) { 256 if ((bp->b_flags & B_INVAL) == 0 && 257 BUF_REFCNT(bp) > 0) { 258 nbusy++; 259 printf( 260 "%d: dev:%08lx, flags:%08lx, blkno:%ld, lblkno:%ld\n", 261 nbusy, (u_long)bp->b_dev, 262 bp->b_flags, (long)bp->b_blkno, 263 (long)bp->b_lblkno); 264 } 265 } 266 DELAY(5000000); /* 5 seconds */ 267#endif 268 } else { 269 printf("done\n"); 270 /* 271 * Unmount filesystems 272 */ 273 if (panicstr == 0) 274 vfs_unmountall(); 275 } 276 DELAY(100000); /* wait for console output to finish */ 277 } 278 279 /* 280 * Ok, now do things that assume all filesystem activity has 281 * been completed. 282 */ 283 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_POST_SYNC], links) 284 (*ep->function)(howto, ep->arg); 285 splhigh(); 286 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) { 287 savectx(&dumppcb); 288#ifdef __i386__ 289 dumppcb.pcb_cr3 = rcr3(); 290#endif 291 dumpsys(); 292 } 293 294 /* Now that we're going to really halt the system... */ 295 LIST_FOREACH(ep, &shutdown_lists[SHUTDOWN_FINAL], links) 296 (*ep->function)(howto, ep->arg); 297 298 if (howto & RB_HALT) { 299 printf("\n"); 300 printf("The operating system has halted.\n"); 301 printf("Please press any key to reboot.\n\n"); 302 switch (cngetc()) { 303 case -1: /* No console, just die */ 304 cpu_halt(); 305 /* NOTREACHED */ 306 default: 307 howto &= ~RB_HALT; 308 break; 309 } 310 } else if (howto & RB_DUMP) { 311 /* System Paniced */ 312 313 if (PANIC_REBOOT_WAIT_TIME != 0) { 314 if (PANIC_REBOOT_WAIT_TIME != -1) { 315 int loop; 316 printf("Automatic reboot in %d seconds - " 317 "press a key on the console to abort\n", 318 PANIC_REBOOT_WAIT_TIME); 319 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 320 loop > 0; --loop) { 321 DELAY(1000 * 100); /* 1/10th second */ 322 /* Did user type a key? */ 323 if (cncheckc() != -1) 324 break; 325 } 326 if (!loop) 327 goto die; 328 } 329 } else { /* zero time specified - reboot NOW */ 330 goto die; 331 } 332 printf("--> Press a key on the console to reboot <--\n"); 333 cngetc(); 334 } 335die: 336 printf("Rebooting...\n"); 337 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 338 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 339 cpu_reset(); 340 for(;;) ; 341 /* NOTREACHED */ 342} 343 344/* 345 * Magic number for savecore 346 * 347 * exported (symorder) and used at least by savecore(8) 348 * 349 */ 350static u_long const dumpmag = 0x8fca0101UL; 351 352static int dumpsize = 0; /* also for savecore */ 353 354static int dodump = 1; 355SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, 356 &dodump, 0, "Do coredump on kernel panic"); 357 358/* ARGSUSED */ 359static void dump_conf __P((void *dummy)); 360static void 361dump_conf(dummy) 362 void *dummy; 363{ 364 cpu_dumpconf(); 365} 366SYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 367 368/* 369 * Doadump comes here after turning off memory management and 370 * getting on the dump stack, either when called above, or by 371 * the auto-restart code. 372 */ 373static void 374dumpsys(void) 375{ 376 377 if (!dodump) 378 return; 379 if (dumpdev == NODEV) 380 return; 381 if (!(bdevsw(dumpdev))) 382 return; 383 if (!(bdevsw(dumpdev)->d_dump)) 384 return; 385 dumpsize = Maxmem; 386 printf("\ndumping to dev (%d,%d), offset %ld\n", 387 major(dumpdev), minor(dumpdev), dumplo); 388 printf("dump "); 389 switch ((*bdevsw(dumpdev)->d_dump)(dumpdev)) { 390 391 case ENXIO: 392 printf("device bad\n"); 393 break; 394 395 case EFAULT: 396 printf("device not ready\n"); 397 break; 398 399 case EINVAL: 400 printf("area improper\n"); 401 break; 402 403 case EIO: 404 printf("i/o error\n"); 405 break; 406 407 case EINTR: 408 printf("aborted from console\n"); 409 break; 410 411 default: 412 printf("succeeded\n"); 413 break; 414 } 415} 416 417/* 418 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 419 * and then reboots. If we are called twice, then we avoid trying to sync 420 * the disks as this often leads to recursive panics. 421 */ 422void 423panic(const char *fmt, ...) 424{ 425 int bootopt; 426 va_list ap; 427 static char buf[256]; 428 429 bootopt = RB_AUTOBOOT | RB_DUMP; 430 if (panicstr) 431 bootopt |= RB_NOSYNC; 432 else 433 panicstr = fmt; 434 435 va_start(ap, fmt); 436 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 437 if (panicstr == fmt) 438 panicstr = buf; 439 va_end(ap); 440 printf("panic: %s\n", buf); 441#ifdef SMP 442 /* three seperate prints in case of an unmapped page and trap */ 443 printf("mp_lock = %08x; ", mp_lock); 444 printf("cpuid = %d; ", cpuid); 445 printf("lapic.id = %08x\n", lapic.id); 446#endif 447 448#if defined(DDB) 449 if (debugger_on_panic) 450 Debugger ("panic"); 451#endif 452 boot(bootopt); 453} 454 455/* 456 * Three routines to handle adding/deleting items on the 457 * shutdown callout lists 458 * 459 * at_shutdown(): 460 * Take the arguments given and put them onto the shutdown callout list. 461 * However first make sure that it's not already there. 462 * returns 0 on success. 463 */ 464int 465at_shutdown(bootlist_fn function, void *arg, int queue) 466{ 467 return(at_shutdown_pri(function, arg, queue, SHUTDOWN_PRI_DEFAULT)); 468} 469 470/* 471 * at_shutdown_pri(): 472 * Take the arguments given and put them onto the shutdown callout list 473 * with the given execution priority. 474 * returns 0 on success. 475 */ 476int 477at_shutdown_pri(bootlist_fn function, void *arg, int queue, int pri) 478{ 479 sle_p op, ep, ip; 480 481 op = NULL; /* shut up gcc */ 482 if (queue < SHUTDOWN_PRE_SYNC 483 || queue > SHUTDOWN_FINAL) { 484 printf("at_shutdown: bad exit callout queue %d specified\n", 485 queue); 486 return (EINVAL); 487 } 488 if (rm_at_shutdown(function, arg)) 489 printf("at_shutdown: exit callout entry was already present\n"); 490 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 491 if (ep == NULL) 492 return (ENOMEM); 493 ep->function = function; 494 ep->arg = arg; 495 ep->priority = pri; 496 497 /* Sort into list of items on this queue */ 498 ip = LIST_FIRST(&shutdown_lists[queue]); 499 if (ip == NULL) { 500 LIST_INSERT_HEAD(&shutdown_lists[queue], ep, links); 501 } else { 502 for (; ip != NULL; op = ip, ip = LIST_NEXT(ip, links)) { 503 if (ep->priority < ip->priority) { 504 LIST_INSERT_BEFORE(ip, ep, links); 505 ep = NULL; 506 break; 507 } 508 } 509 if (ep != NULL) 510 LIST_INSERT_AFTER(op, ep, links); 511 } 512 return (0); 513} 514 515/* 516 * Scan the exit callout lists for the given items and remove them. 517 * Returns the number of items removed. 518 */ 519int 520rm_at_shutdown(bootlist_fn function, void *arg) 521{ 522 sle_p ep; 523 int count; 524 int queue; 525 526 count = 0; 527 for (queue = SHUTDOWN_PRE_SYNC; queue < SHUTDOWN_FINAL; queue++) { 528 LIST_FOREACH(ep, &shutdown_lists[queue], links) { 529 if ((ep->function == function) && (ep->arg == arg)) { 530 LIST_REMOVE(ep, links); 531 free(ep, M_TEMP); 532 count++; 533 } 534 } 535 } 536 return (count); 537} 538 539/* 540 * Support for poweroff delay. 541 */ 542static int poweroff_delay = 0; 543SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 544 &poweroff_delay, 0, ""); 545 546static void poweroff_wait(int howto, void *unused) 547{ 548 if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 549 return; 550 DELAY(poweroff_delay * 1000); 551} 552 553/* 554 * XXX OK? This implies I know SHUTDOWN_PRI_LAST > SHUTDOWN_PRI_FIRST 555 */ 556static void poweroff_conf(void *unused) 557{ 558 at_shutdown_pri(poweroff_wait, NULL, SHUTDOWN_FINAL, SHUTDOWN_PRI_FIRST); 559} 560 561SYSINIT(poweroff_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, poweroff_conf, NULL) 562