kern_shutdown.c revision 28000
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 3928000Sjulian * $Id: kern_shutdown.c,v 1.18 1997/08/09 01:25:53 julian Exp $ 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> 5521776Sbde#include <sys/mount.h> 5617658Sjulian#include <sys/sysctl.h> 5717658Sjulian#include <sys/conf.h> 5817658Sjulian#include <sys/sysproto.h> 5917658Sjulian 6017658Sjulian#include <machine/pcb.h> 6117658Sjulian#include <machine/clock.h> 6217658Sjulian#include <machine/cons.h> 6317658Sjulian#include <machine/md_var.h> 6426812Speter#ifdef SMP 6526812Speter#include <machine/smp.h> /* smp_active, cpuid */ 6626812Speter#endif 6717658Sjulian 6817658Sjulian#include <sys/utsname.h> 6917658Sjulian#include <sys/signalvar.h> 7017658Sjulian 7117658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7217658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7317658Sjulian#endif 7417658Sjulian 7517658Sjulian/* 7617658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7717658Sjulian * ANSI and traditional C compilers. 7817658Sjulian */ 7917658Sjulian#include <machine/stdarg.h> 8017658Sjulian 8117658Sjulian#if defined(DDB) 8217658Sjulian#ifdef DDB_UNATTENDED 8317658Sjulian static int debugger_on_panic = 0; 8417658Sjulian#else 8517658Sjulian static int debugger_on_panic = 1; 8617658Sjulian#endif 8717658Sjulian 8817658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 8917658Sjulian &debugger_on_panic, 0, ""); 9017658Sjulian#endif 9117658Sjulian 9217658Sjulian 9328000Sjulian#ifdef HW_WDOG 9417658Sjulian/* 9527997Sjulian * If there is a hardware watchdog, point this at the function needed to 9627997Sjulian * hold it off. 9727997Sjulian * It's needed when the kernel needs to do some lengthy operations. 9827997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have 9927997Sjulian * your precious core-dump only half written because the wdog kicked in. 10027997Sjulian */ 10127997Sjulianwatchdog_tickle_fn wdog_tickler = NULL; 10228000Sjulian#endif /* HW_WDOG */ 10327997Sjulian 10427997Sjulian/* 10517658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 10617658Sjulian * to indicate that the kernel has already called panic. 10717658Sjulian */ 10817658Sjulianconst char *panicstr; 10917658Sjulian 11017658Sjulian/* 11117658Sjulian * callout list for things to do a shutdown 11217658Sjulian */ 11317658Sjuliantypedef struct shutdown_list_element { 11417658Sjulian struct shutdown_list_element *next; 11517658Sjulian bootlist_fn function; 11617658Sjulian void *arg; 11717658Sjulian} *sle_p; 11817658Sjulian 11917768Sjulian/* 12017768Sjulian * there are two shutdown lists. Some things need to be shut down 12117768Sjulian * Earlier than others. 12217768Sjulian */ 12317768Sjulianstatic sle_p shutdown_list1; 12417768Sjulianstatic sle_p shutdown_list2; 12517658Sjulian 12617658Sjulian 12717658Sjulianstatic void dumpsys(void); 12817658Sjulian 12917658Sjulian#ifndef _SYS_SYSPROTO_H_ 13017658Sjulianstruct reboot_args { 13117658Sjulian int opt; 13217658Sjulian}; 13317658Sjulian#endif 13417658Sjulian/* ARGSUSED */ 13517658Sjulian 13617658Sjulian/* 13717658Sjulian * The system call that results in a reboot 13817658Sjulian */ 13917658Sjulianint 14017658Sjulianreboot(p, uap, retval) 14117658Sjulian struct proc *p; 14217658Sjulian struct reboot_args *uap; 14317658Sjulian int *retval; 14417658Sjulian{ 14517658Sjulian int error; 14617658Sjulian 14717658Sjulian if ((error = suser(p->p_ucred, &p->p_acflag))) 14817658Sjulian return (error); 14917658Sjulian 15017658Sjulian boot(uap->opt); 15117658Sjulian return (0); 15217658Sjulian} 15317658Sjulian 15417658Sjulian/* 15517658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 15617658Sjulian */ 15717658Sjulianvoid 15817658Sjulianshutdown_nice(void) 15917658Sjulian{ 16017658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 16117658Sjulian if (initproc != NULL) { 16217658Sjulian psignal(initproc, SIGINT); 16317658Sjulian } else { 16417658Sjulian /* No init(8) running, so simply reboot */ 16517658Sjulian boot(RB_NOSYNC); 16617658Sjulian } 16717658Sjulian return; 16817658Sjulian} 16917658Sjulianstatic int waittime = -1; 17017658Sjulianstatic struct pcb dumppcb; 17117658Sjulian 17217658Sjulian/* 17317658Sjulian * Go through the rigmarole of shutting down.. 17417658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 17517658Sjulian * anything machine dependant in it. 17617658Sjulian */ 17718277Sbdevoid 17817658Sjulianboot(howto) 17917658Sjulian int howto; 18017658Sjulian{ 18117768Sjulian sle_p ep; 18217658Sjulian 18325164Speter#ifdef SMP 18425164Speter int c, spins; 18525164Speter 18625164Speter /* don't accidently start it */ 18725164Speter if (smp_active) { 18825164Speter smp_active = 1; 18925164Speter 19025164Speter spins = 100; 19125164Speter 19226812Speter printf("boot() called on cpu#%d\n", cpuid); 19326812Speter while ((c = cpuid) != 0) { 19425164Speter if (spins-- < 1) { 19525164Speter printf("timeout waiting for cpu #0!\n"); 19625164Speter break; 19725164Speter } 19825164Speter printf("oops, I'm on cpu#%d, I need to be on cpu#0!\n", 19925164Speter c); 20025164Speter tsleep((caddr_t)&smp_active, PZERO, "cpu0wt", 10); 20125164Speter } 20225164Speter } 20325164Speter#endif 20427997Sjulian /* 20527997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 20627997Sjulian */ 20717768Sjulian ep = shutdown_list1; 20817768Sjulian while (ep) { 20917768Sjulian shutdown_list1 = ep->next; 21017658Sjulian (*ep->function)(howto, ep->arg); 21117658Sjulian ep = ep->next; 21217658Sjulian } 21327997Sjulian 21427997Sjulian /* 21527997Sjulian * Now sync filesystems 21627997Sjulian */ 21717658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 21817658Sjulian register struct buf *bp; 21917658Sjulian int iter, nbusy; 22017658Sjulian 22117658Sjulian waittime = 0; 22217658Sjulian printf("\nsyncing disks... "); 22317658Sjulian 22417658Sjulian sync(&proc0, NULL, NULL); 22517658Sjulian 22617658Sjulian for (iter = 0; iter < 20; iter++) { 22717658Sjulian nbusy = 0; 22817658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 22917658Sjulian if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 23017658Sjulian nbusy++; 23117658Sjulian } 23217658Sjulian } 23317658Sjulian if (nbusy == 0) 23417658Sjulian break; 23517658Sjulian printf("%d ", nbusy); 23617658Sjulian DELAY(40000 * iter); 23717658Sjulian } 23817658Sjulian if (nbusy) { 23917658Sjulian /* 24017658Sjulian * Failed to sync all blocks. Indicate this and don't 24117658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 24217658Sjulian */ 24317658Sjulian printf("giving up\n"); 24417658Sjulian#ifdef SHOW_BUSYBUFS 24517658Sjulian nbusy = 0; 24617658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 24717658Sjulian if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 24817658Sjulian nbusy++; 24917658Sjulian printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno); 25017658Sjulian } 25117658Sjulian } 25217658Sjulian DELAY(5000000); /* 5 seconds */ 25317658Sjulian#endif 25417658Sjulian } else { 25517658Sjulian printf("done\n"); 25617658Sjulian /* 25717658Sjulian * Unmount filesystems 25817658Sjulian */ 25917658Sjulian if (panicstr == 0) 26017658Sjulian vfs_unmountall(); 26117658Sjulian } 26217658Sjulian DELAY(100000); /* wait for console output to finish */ 26317658Sjulian } 26427997Sjulian 26527997Sjulian /* 26627997Sjulian * Ok, now do things that assume all filesystem activity has 26727997Sjulian * been completed. 26827997Sjulian */ 26917768Sjulian ep = shutdown_list2; 27017768Sjulian while (ep) { 27117768Sjulian shutdown_list2 = ep->next; 27217768Sjulian (*ep->function)(howto, ep->arg); 27317768Sjulian ep = ep->next; 27417768Sjulian } 27517658Sjulian splhigh(); 27617658Sjulian if (howto & RB_HALT) { 27726657Swollman cpu_power_down(); 27817658Sjulian printf("\n"); 27917658Sjulian printf("The operating system has halted.\n"); 28017658Sjulian printf("Please press any key to reboot.\n\n"); 28119274Sjulian switch (cngetc()) { 28219274Sjulian case -1: /* No console, just die */ 28319274Sjulian cpu_halt(); 28419274Sjulian /* NOTREACHED */ 28519274Sjulian default: 28619274Sjulian break; 28719274Sjulian } 28817658Sjulian } else { 28917658Sjulian if (howto & RB_DUMP) { 29017658Sjulian if (!cold) { 29117658Sjulian savectx(&dumppcb); 29217658Sjulian dumppcb.pcb_cr3 = rcr3(); 29317658Sjulian dumpsys(); 29417658Sjulian } 29517658Sjulian 29617658Sjulian if (PANIC_REBOOT_WAIT_TIME != 0) { 29717658Sjulian if (PANIC_REBOOT_WAIT_TIME != -1) { 29817658Sjulian int loop; 29917658Sjulian printf("Automatic reboot in %d seconds - press a key on the console to abort\n", 30017658Sjulian PANIC_REBOOT_WAIT_TIME); 30117658Sjulian for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) { 30217658Sjulian DELAY(1000 * 100); /* 1/10th second */ 30318290Sbde /* Did user type a key? */ 30418290Sbde if (cncheckc() != -1) 30517658Sjulian break; 30617658Sjulian } 30717658Sjulian if (!loop) 30817658Sjulian goto die; 30917658Sjulian } 31017658Sjulian } else { /* zero time specified - reboot NOW */ 31117658Sjulian goto die; 31217658Sjulian } 31317658Sjulian printf("--> Press a key on the console to reboot <--\n"); 31417658Sjulian cngetc(); 31517658Sjulian } 31617658Sjulian } 31717658Sjuliandie: 31817658Sjulian printf("Rebooting...\n"); 31917658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 32017677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 32117658Sjulian cpu_reset(); 32217658Sjulian for(;;) ; 32317658Sjulian /* NOTREACHED */ 32417658Sjulian} 32517658Sjulian 32617658Sjulian/* 32717658Sjulian * Magic number for savecore 32817658Sjulian * 32917658Sjulian * exported (symorder) and used at least by savecore(8) 33017658Sjulian * 33117658Sjulian */ 33217658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 33317658Sjulian 33417658Sjulianstatic int dumpsize = 0; /* also for savecore */ 33517658Sjulian 33617658Sjulianstatic int dodump = 1; 33717658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, ""); 33817658Sjulian 33917658Sjulian/* 34017658Sjulian * Doadump comes here after turning off memory management and 34117658Sjulian * getting on the dump stack, either when called above, or by 34217658Sjulian * the auto-restart code. 34317658Sjulian */ 34417658Sjulianstatic void 34517658Sjuliandumpsys(void) 34617658Sjulian{ 34717658Sjulian 34817658Sjulian if (!dodump) 34917658Sjulian return; 35017658Sjulian if (dumpdev == NODEV) 35117658Sjulian return; 35217658Sjulian if ((minor(dumpdev)&07) != 1) 35317658Sjulian return; 35417658Sjulian if (!(bdevsw[major(dumpdev)])) 35517658Sjulian return; 35617658Sjulian if (!(bdevsw[major(dumpdev)]->d_dump)) 35717658Sjulian return; 35817658Sjulian dumpsize = Maxmem; 35917658Sjulian printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo); 36017658Sjulian printf("dump "); 36117658Sjulian switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) { 36217658Sjulian 36317658Sjulian case ENXIO: 36417658Sjulian printf("device bad\n"); 36517658Sjulian break; 36617658Sjulian 36717658Sjulian case EFAULT: 36817658Sjulian printf("device not ready\n"); 36917658Sjulian break; 37017658Sjulian 37117658Sjulian case EINVAL: 37217658Sjulian printf("area improper\n"); 37317658Sjulian break; 37417658Sjulian 37517658Sjulian case EIO: 37617658Sjulian printf("i/o error\n"); 37717658Sjulian break; 37817658Sjulian 37917658Sjulian case EINTR: 38017658Sjulian printf("aborted from console\n"); 38117658Sjulian break; 38217658Sjulian 38317658Sjulian default: 38417658Sjulian printf("succeeded\n"); 38517658Sjulian break; 38617658Sjulian } 38717658Sjulian} 38817658Sjulian 38917658Sjulian/* 39017658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 39117658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 39217658Sjulian * the disks as this often leads to recursive panics. 39317658Sjulian */ 39417658Sjulianvoid 39517658Sjulianpanic(const char *fmt, ...) 39617658Sjulian{ 39717658Sjulian int bootopt; 39817658Sjulian va_list ap; 39917658Sjulian 40017658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 40117658Sjulian if (panicstr) 40217658Sjulian bootopt |= RB_NOSYNC; 40317658Sjulian else 40417658Sjulian panicstr = fmt; 40517658Sjulian 40617658Sjulian printf("panic: "); 40717658Sjulian va_start(ap, fmt); 40817658Sjulian vprintf(fmt, ap); 40917658Sjulian va_end(ap); 41017658Sjulian printf("\n"); 41126100Sfsmp#ifdef SMP 41226812Speter printf(" cpuid %d\n", cpuid); 41326100Sfsmp#endif 41417658Sjulian 41517658Sjulian#if defined(DDB) 41617658Sjulian if (debugger_on_panic) 41717658Sjulian Debugger ("panic"); 41817658Sjulian#endif 41917658Sjulian boot(bootopt); 42017658Sjulian} 42117658Sjulian 42217768Sjulian/* 42317768Sjulian * Two routines to handle adding/deleting items on the 42417768Sjulian * shutdown callout lists 42517768Sjulian * 42617768Sjulian * at_shutdown(): 42717658Sjulian * Take the arguments given and put them onto the shutdown callout list. 42817658Sjulian * However first make sure that it's not already there. 42917658Sjulian * returns 0 on success. 43017658Sjulian */ 43117658Sjulianint 43217768Sjulianat_shutdown(bootlist_fn function, void *arg, int position) 43317658Sjulian{ 43417768Sjulian sle_p ep, *epp; 43517768Sjulian 43617768Sjulian switch(position) { 43717768Sjulian case SHUTDOWN_PRE_SYNC: 43817768Sjulian epp = &shutdown_list1; 43917768Sjulian break; 44017768Sjulian case SHUTDOWN_POST_SYNC: 44117768Sjulian epp = &shutdown_list2; 44217768Sjulian break; 44317768Sjulian default: 44417768Sjulian printf("bad exit callout list specified\n"); 44517768Sjulian return (EINVAL); 44617768Sjulian } 44717768Sjulian if (rm_at_shutdown(function, arg)) 44817658Sjulian printf("exit callout entry already present\n"); 44917768Sjulian ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 45017768Sjulian if (ep == NULL) 45117768Sjulian return (ENOMEM); 45217768Sjulian ep->next = *epp; 45317658Sjulian ep->function = function; 45417658Sjulian ep->arg = arg; 45517768Sjulian *epp = ep; 45617768Sjulian return (0); 45717658Sjulian} 45817768Sjulian 45917658Sjulian/* 46017768Sjulian * Scan the exit callout lists for the given items and remove them. 46117658Sjulian * Returns the number of items removed. 46217658Sjulian */ 46317658Sjulianint 46417658Sjulianrm_at_shutdown(bootlist_fn function, void *arg) 46517658Sjulian{ 46617768Sjulian sle_p *epp, ep; 46717768Sjulian int count; 46817658Sjulian 46917768Sjulian count = 0; 47017768Sjulian epp = &shutdown_list1; 47117658Sjulian ep = *epp; 47217768Sjulian while (ep) { 47317834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 47417658Sjulian *epp = ep->next; 47517768Sjulian free(ep, M_TEMP); 47617658Sjulian count++; 47717658Sjulian } else { 47817658Sjulian epp = &ep->next; 47917658Sjulian } 48017658Sjulian ep = *epp; 48117658Sjulian } 48217768Sjulian epp = &shutdown_list2; 48317768Sjulian ep = *epp; 48417768Sjulian while (ep) { 48517834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 48617768Sjulian *epp = ep->next; 48717768Sjulian free(ep, M_TEMP); 48817768Sjulian count++; 48917768Sjulian } else { 49017768Sjulian epp = &ep->next; 49117768Sjulian } 49217768Sjulian ep = *epp; 49317768Sjulian } 49417768Sjulian return (count); 49517658Sjulian} 49617658Sjulian 497