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