kern_shutdown.c revision 21673
117658Sjulian/*- 217658Sjulian * Copyright (c) 1986, 1988, 1991, 1993 317658Sjulian * The Regents of the University of California. All rights reserved. 417658Sjulian * (c) UNIX System Laboratories, Inc. 517658Sjulian * All or some portions of this file are derived from material licensed 617658Sjulian * to the University of California by American Telephone and Telegraph 717658Sjulian * Co. or Unix System Laboratories, Inc. and are reproduced herein with 817658Sjulian * the permission of UNIX System Laboratories, Inc. 917658Sjulian * 1017658Sjulian * Redistribution and use in source and binary forms, with or without 1117658Sjulian * modification, are permitted provided that the following conditions 1217658Sjulian * are met: 1317658Sjulian * 1. Redistributions of source code must retain the above copyright 1417658Sjulian * notice, this list of conditions and the following disclaimer. 1517658Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1617658Sjulian * notice, this list of conditions and the following disclaimer in the 1717658Sjulian * documentation and/or other materials provided with the distribution. 1817658Sjulian * 3. All advertising materials mentioning features or use of this software 1917658Sjulian * must display the following acknowledgement: 2017658Sjulian * This product includes software developed by the University of 2117658Sjulian * California, Berkeley and its contributors. 2217658Sjulian * 4. Neither the name of the University nor the names of its contributors 2317658Sjulian * may be used to endorse or promote products derived from this software 2417658Sjulian * without specific prior written permission. 2517658Sjulian * 2617658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2717658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2817658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2917658Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3017658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3117658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3217658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3317658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3417658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3517658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3617658Sjulian * SUCH DAMAGE. 3717658Sjulian * 3817658Sjulian * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 3921673Sjkh * $FreeBSD: head/sys/kern/kern_shutdown.c 21673 1997-01-14 07:20:47Z jkh $ 4017658Sjulian */ 4117658Sjulian 4217658Sjulian#include "opt_ddb.h" 4317658Sjulian 4417658Sjulian#include <sys/param.h> 4517658Sjulian#include <sys/systm.h> 4617658Sjulian#include <sys/reboot.h> 4717658Sjulian#include <sys/msgbuf.h> 4817658Sjulian#include <sys/proc.h> 4917658Sjulian#include <sys/vnode.h> 5017658Sjulian#include <sys/tty.h> 5117658Sjulian#include <sys/tprintf.h> 5217658Sjulian#include <sys/syslog.h> 5317658Sjulian#include <sys/malloc.h> 5417658Sjulian#include <sys/kernel.h> 5517658Sjulian#include <sys/sysctl.h> 5617658Sjulian#include <sys/conf.h> 5717658Sjulian#include <sys/sysproto.h> 5817658Sjulian 5917658Sjulian#include <machine/pcb.h> 6017658Sjulian#include <machine/clock.h> 6117658Sjulian#include <machine/cons.h> 6217658Sjulian#include <machine/md_var.h> 6317658Sjulian 6417658Sjulian#include <sys/utsname.h> 6517658Sjulian#include <sys/signalvar.h> 6617658Sjulian 6717658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 6817658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 6917658Sjulian#endif 7017658Sjulian 7117658Sjulian/* 7217658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7317658Sjulian * ANSI and traditional C compilers. 7417658Sjulian */ 7517658Sjulian#include <machine/stdarg.h> 7617658Sjulian 7717658Sjulian#if defined(DDB) 7817658Sjulian#ifdef DDB_UNATTENDED 7917658Sjulian static int debugger_on_panic = 0; 8017658Sjulian#else 8117658Sjulian static int debugger_on_panic = 1; 8217658Sjulian#endif 8317658Sjulian 8417658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 8517658Sjulian &debugger_on_panic, 0, ""); 8617658Sjulian#endif 8717658Sjulian 8817658Sjulian 8917658Sjulian/* 9017658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 9117658Sjulian * to indicate that the kernel has already called panic. 9217658Sjulian */ 9317658Sjulianconst char *panicstr; 9417658Sjulian 9517658Sjulian/* 9617658Sjulian * callout list for things to do a shutdown 9717658Sjulian */ 9817658Sjuliantypedef struct shutdown_list_element { 9917658Sjulian struct shutdown_list_element *next; 10017658Sjulian bootlist_fn function; 10117658Sjulian void *arg; 10217658Sjulian} *sle_p; 10317658Sjulian 10417768Sjulian/* 10517768Sjulian * there are two shutdown lists. Some things need to be shut down 10617768Sjulian * Earlier than others. 10717768Sjulian */ 10817768Sjulianstatic sle_p shutdown_list1; 10917768Sjulianstatic sle_p shutdown_list2; 11017658Sjulian 11117658Sjulian 11217658Sjulianstatic void dumpsys(void); 11317658Sjulian 11417658Sjulian#ifndef _SYS_SYSPROTO_H_ 11517658Sjulianstruct reboot_args { 11617658Sjulian int opt; 11717658Sjulian}; 11817658Sjulian#endif 11917658Sjulian/* ARGSUSED */ 12017658Sjulian 12117658Sjulian/* 12217658Sjulian * The system call that results in a reboot 12317658Sjulian */ 12417658Sjulianint 12517658Sjulianreboot(p, uap, retval) 12617658Sjulian struct proc *p; 12717658Sjulian struct reboot_args *uap; 12817658Sjulian int *retval; 12917658Sjulian{ 13017658Sjulian int error; 13117658Sjulian 13217658Sjulian if ((error = suser(p->p_ucred, &p->p_acflag))) 13317658Sjulian return (error); 13417658Sjulian 13517658Sjulian boot(uap->opt); 13617658Sjulian return (0); 13717658Sjulian} 13817658Sjulian 13917658Sjulian/* 14017658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 14117658Sjulian */ 14217658Sjulianvoid 14317658Sjulianshutdown_nice(void) 14417658Sjulian{ 14517658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 14617658Sjulian if (initproc != NULL) { 14717658Sjulian psignal(initproc, SIGINT); 14817658Sjulian } else { 14917658Sjulian /* No init(8) running, so simply reboot */ 15017658Sjulian boot(RB_NOSYNC); 15117658Sjulian } 15217658Sjulian return; 15317658Sjulian} 15417658Sjulianstatic int waittime = -1; 15517658Sjulianstatic struct pcb dumppcb; 15617658Sjulian 15717658Sjulian/* 15817658Sjulian * Go through the rigmarole of shutting down.. 15917658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 16017658Sjulian * anything machine dependant in it. 16117658Sjulian */ 16218277Sbdevoid 16317658Sjulianboot(howto) 16417658Sjulian int howto; 16517658Sjulian{ 16617768Sjulian sle_p ep; 16717658Sjulian 16817768Sjulian ep = shutdown_list1; 16917768Sjulian while (ep) { 17017768Sjulian shutdown_list1 = ep->next; 17117658Sjulian (*ep->function)(howto, ep->arg); 17217658Sjulian ep = ep->next; 17317658Sjulian } 17417658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 17517658Sjulian register struct buf *bp; 17617658Sjulian int iter, nbusy; 17717658Sjulian 17817658Sjulian waittime = 0; 17917658Sjulian printf("\nsyncing disks... "); 18017658Sjulian 18117658Sjulian sync(&proc0, NULL, NULL); 18217658Sjulian 18317658Sjulian for (iter = 0; iter < 20; iter++) { 18417658Sjulian nbusy = 0; 18517658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 18617658Sjulian if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 18717658Sjulian nbusy++; 18817658Sjulian } 18917658Sjulian } 19017658Sjulian if (nbusy == 0) 19117658Sjulian break; 19217658Sjulian printf("%d ", nbusy); 19317658Sjulian DELAY(40000 * iter); 19417658Sjulian } 19517658Sjulian if (nbusy) { 19617658Sjulian /* 19717658Sjulian * Failed to sync all blocks. Indicate this and don't 19817658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 19917658Sjulian */ 20017658Sjulian printf("giving up\n"); 20117658Sjulian#ifdef SHOW_BUSYBUFS 20217658Sjulian nbusy = 0; 20317658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 20417658Sjulian if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 20517658Sjulian nbusy++; 20617658Sjulian printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno); 20717658Sjulian } 20817658Sjulian } 20917658Sjulian DELAY(5000000); /* 5 seconds */ 21017658Sjulian#endif 21117658Sjulian } else { 21217658Sjulian printf("done\n"); 21317658Sjulian /* 21417658Sjulian * Unmount filesystems 21517658Sjulian */ 21617658Sjulian if (panicstr == 0) 21717658Sjulian vfs_unmountall(); 21817658Sjulian } 21917658Sjulian DELAY(100000); /* wait for console output to finish */ 22017658Sjulian } 22117768Sjulian ep = shutdown_list2; 22217768Sjulian while (ep) { 22317768Sjulian shutdown_list2 = ep->next; 22417768Sjulian (*ep->function)(howto, ep->arg); 22517768Sjulian ep = ep->next; 22617768Sjulian } 22717658Sjulian splhigh(); 22817658Sjulian if (howto & RB_HALT) { 22917658Sjulian printf("\n"); 23017658Sjulian printf("The operating system has halted.\n"); 23117658Sjulian printf("Please press any key to reboot.\n\n"); 23219274Sjulian switch (cngetc()) { 23319274Sjulian case -1: /* No console, just die */ 23419274Sjulian cpu_halt(); 23519274Sjulian /* NOTREACHED */ 23619274Sjulian default: 23719274Sjulian break; 23819274Sjulian } 23917658Sjulian } else { 24017658Sjulian if (howto & RB_DUMP) { 24117658Sjulian if (!cold) { 24217658Sjulian savectx(&dumppcb); 24317658Sjulian dumppcb.pcb_cr3 = rcr3(); 24417658Sjulian dumpsys(); 24517658Sjulian } 24617658Sjulian 24717658Sjulian if (PANIC_REBOOT_WAIT_TIME != 0) { 24817658Sjulian if (PANIC_REBOOT_WAIT_TIME != -1) { 24917658Sjulian int loop; 25017658Sjulian printf("Automatic reboot in %d seconds - press a key on the console to abort\n", 25117658Sjulian PANIC_REBOOT_WAIT_TIME); 25217658Sjulian for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) { 25317658Sjulian DELAY(1000 * 100); /* 1/10th second */ 25418290Sbde /* Did user type a key? */ 25518290Sbde if (cncheckc() != -1) 25617658Sjulian break; 25717658Sjulian } 25817658Sjulian if (!loop) 25917658Sjulian goto die; 26017658Sjulian } 26117658Sjulian } else { /* zero time specified - reboot NOW */ 26217658Sjulian goto die; 26317658Sjulian } 26417658Sjulian printf("--> Press a key on the console to reboot <--\n"); 26517658Sjulian cngetc(); 26617658Sjulian } 26717658Sjulian } 26817658Sjuliandie: 26917658Sjulian printf("Rebooting...\n"); 27017658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 27117677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 27217658Sjulian cpu_reset(); 27317658Sjulian for(;;) ; 27417658Sjulian /* NOTREACHED */ 27517658Sjulian} 27617658Sjulian 27717658Sjulian/* 27817658Sjulian * Magic number for savecore 27917658Sjulian * 28017658Sjulian * exported (symorder) and used at least by savecore(8) 28117658Sjulian * 28217658Sjulian */ 28317658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 28417658Sjulian 28517658Sjulianstatic int dumpsize = 0; /* also for savecore */ 28617658Sjulian 28717658Sjulianstatic int dodump = 1; 28817658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, ""); 28917658Sjulian 29017658Sjulian/* 29117658Sjulian * Doadump comes here after turning off memory management and 29217658Sjulian * getting on the dump stack, either when called above, or by 29317658Sjulian * the auto-restart code. 29417658Sjulian */ 29517658Sjulianstatic void 29617658Sjuliandumpsys(void) 29717658Sjulian{ 29817658Sjulian 29917658Sjulian if (!dodump) 30017658Sjulian return; 30117658Sjulian if (dumpdev == NODEV) 30217658Sjulian return; 30317658Sjulian if ((minor(dumpdev)&07) != 1) 30417658Sjulian return; 30517658Sjulian if (!(bdevsw[major(dumpdev)])) 30617658Sjulian return; 30717658Sjulian if (!(bdevsw[major(dumpdev)]->d_dump)) 30817658Sjulian return; 30917658Sjulian dumpsize = Maxmem; 31017658Sjulian printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo); 31117658Sjulian printf("dump "); 31217658Sjulian switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) { 31317658Sjulian 31417658Sjulian case ENXIO: 31517658Sjulian printf("device bad\n"); 31617658Sjulian break; 31717658Sjulian 31817658Sjulian case EFAULT: 31917658Sjulian printf("device not ready\n"); 32017658Sjulian break; 32117658Sjulian 32217658Sjulian case EINVAL: 32317658Sjulian printf("area improper\n"); 32417658Sjulian break; 32517658Sjulian 32617658Sjulian case EIO: 32717658Sjulian printf("i/o error\n"); 32817658Sjulian break; 32917658Sjulian 33017658Sjulian case EINTR: 33117658Sjulian printf("aborted from console\n"); 33217658Sjulian break; 33317658Sjulian 33417658Sjulian default: 33517658Sjulian printf("succeeded\n"); 33617658Sjulian break; 33717658Sjulian } 33817658Sjulian} 33917658Sjulian 34017658Sjulian/* 34117658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 34217658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 34317658Sjulian * the disks as this often leads to recursive panics. 34417658Sjulian */ 34517658Sjulianvoid 34617658Sjulianpanic(const char *fmt, ...) 34717658Sjulian{ 34817658Sjulian int bootopt; 34917658Sjulian va_list ap; 35017658Sjulian 35117658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 35217658Sjulian if (panicstr) 35317658Sjulian bootopt |= RB_NOSYNC; 35417658Sjulian else 35517658Sjulian panicstr = fmt; 35617658Sjulian 35717658Sjulian printf("panic: "); 35817658Sjulian va_start(ap, fmt); 35917658Sjulian vprintf(fmt, ap); 36017658Sjulian va_end(ap); 36117658Sjulian printf("\n"); 36217658Sjulian 36317658Sjulian#if defined(DDB) 36417658Sjulian if (debugger_on_panic) 36517658Sjulian Debugger ("panic"); 36617658Sjulian#endif 36717658Sjulian boot(bootopt); 36817658Sjulian} 36917658Sjulian 37017768Sjulian/* 37117768Sjulian * Two routines to handle adding/deleting items on the 37217768Sjulian * shutdown callout lists 37317768Sjulian * 37417768Sjulian * at_shutdown(): 37517658Sjulian * Take the arguments given and put them onto the shutdown callout list. 37617658Sjulian * However first make sure that it's not already there. 37717658Sjulian * returns 0 on success. 37817658Sjulian */ 37917658Sjulianint 38017768Sjulianat_shutdown(bootlist_fn function, void *arg, int position) 38117658Sjulian{ 38217768Sjulian sle_p ep, *epp; 38317768Sjulian 38417768Sjulian switch(position) { 38517768Sjulian case SHUTDOWN_PRE_SYNC: 38617768Sjulian epp = &shutdown_list1; 38717768Sjulian break; 38817768Sjulian case SHUTDOWN_POST_SYNC: 38917768Sjulian epp = &shutdown_list2; 39017768Sjulian break; 39117768Sjulian default: 39217768Sjulian printf("bad exit callout list specified\n"); 39317768Sjulian return (EINVAL); 39417768Sjulian } 39517768Sjulian if (rm_at_shutdown(function, arg)) 39617658Sjulian printf("exit callout entry already present\n"); 39717768Sjulian ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 39817768Sjulian if (ep == NULL) 39917768Sjulian return (ENOMEM); 40017768Sjulian ep->next = *epp; 40117658Sjulian ep->function = function; 40217658Sjulian ep->arg = arg; 40317768Sjulian *epp = ep; 40417768Sjulian return (0); 40517658Sjulian} 40617768Sjulian 40717658Sjulian/* 40817768Sjulian * Scan the exit callout lists for the given items and remove them. 40917658Sjulian * Returns the number of items removed. 41017658Sjulian */ 41117658Sjulianint 41217658Sjulianrm_at_shutdown(bootlist_fn function, void *arg) 41317658Sjulian{ 41417768Sjulian sle_p *epp, ep; 41517768Sjulian int count; 41617658Sjulian 41717768Sjulian count = 0; 41817768Sjulian epp = &shutdown_list1; 41917658Sjulian ep = *epp; 42017768Sjulian while (ep) { 42117834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 42217658Sjulian *epp = ep->next; 42317768Sjulian free(ep, M_TEMP); 42417658Sjulian count++; 42517658Sjulian } else { 42617658Sjulian epp = &ep->next; 42717658Sjulian } 42817658Sjulian ep = *epp; 42917658Sjulian } 43017768Sjulian epp = &shutdown_list2; 43117768Sjulian ep = *epp; 43217768Sjulian while (ep) { 43317834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 43417768Sjulian *epp = ep->next; 43517768Sjulian free(ep, M_TEMP); 43617768Sjulian count++; 43717768Sjulian } else { 43817768Sjulian epp = &ep->next; 43917768Sjulian } 44017768Sjulian ep = *epp; 44117768Sjulian } 44217768Sjulian return (count); 44317658Sjulian} 44417658Sjulian 445