kern_shutdown.c revision 18290
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 3918290Sbde * $Id: kern_shutdown.c,v 1.7 1996/09/13 09:17:06 bde 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> 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"); 23217658Sjulian cngetc(); 23317658Sjulian } else { 23417658Sjulian if (howto & RB_DUMP) { 23517658Sjulian if (!cold) { 23617658Sjulian savectx(&dumppcb); 23717658Sjulian dumppcb.pcb_cr3 = rcr3(); 23817658Sjulian dumpsys(); 23917658Sjulian } 24017658Sjulian 24117658Sjulian if (PANIC_REBOOT_WAIT_TIME != 0) { 24217658Sjulian if (PANIC_REBOOT_WAIT_TIME != -1) { 24317658Sjulian int loop; 24417658Sjulian printf("Automatic reboot in %d seconds - press a key on the console to abort\n", 24517658Sjulian PANIC_REBOOT_WAIT_TIME); 24617658Sjulian for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) { 24717658Sjulian DELAY(1000 * 100); /* 1/10th second */ 24818290Sbde /* Did user type a key? */ 24918290Sbde if (cncheckc() != -1) 25017658Sjulian break; 25117658Sjulian } 25217658Sjulian if (!loop) 25317658Sjulian goto die; 25417658Sjulian } 25517658Sjulian } else { /* zero time specified - reboot NOW */ 25617658Sjulian goto die; 25717658Sjulian } 25817658Sjulian printf("--> Press a key on the console to reboot <--\n"); 25917658Sjulian cngetc(); 26017658Sjulian } 26117658Sjulian } 26217658Sjuliandie: 26317658Sjulian printf("Rebooting...\n"); 26417658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 26517677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 26617658Sjulian cpu_reset(); 26717658Sjulian for(;;) ; 26817658Sjulian /* NOTREACHED */ 26917658Sjulian} 27017658Sjulian 27117658Sjulian/* 27217658Sjulian * Magic number for savecore 27317658Sjulian * 27417658Sjulian * exported (symorder) and used at least by savecore(8) 27517658Sjulian * 27617658Sjulian */ 27717658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 27817658Sjulian 27917658Sjulianstatic int dumpsize = 0; /* also for savecore */ 28017658Sjulian 28117658Sjulianstatic int dodump = 1; 28217658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, ""); 28317658Sjulian 28417658Sjulian/* 28517658Sjulian * Doadump comes here after turning off memory management and 28617658Sjulian * getting on the dump stack, either when called above, or by 28717658Sjulian * the auto-restart code. 28817658Sjulian */ 28917658Sjulianstatic void 29017658Sjuliandumpsys(void) 29117658Sjulian{ 29217658Sjulian 29317658Sjulian if (!dodump) 29417658Sjulian return; 29517658Sjulian if (dumpdev == NODEV) 29617658Sjulian return; 29717658Sjulian if ((minor(dumpdev)&07) != 1) 29817658Sjulian return; 29917658Sjulian if (!(bdevsw[major(dumpdev)])) 30017658Sjulian return; 30117658Sjulian if (!(bdevsw[major(dumpdev)]->d_dump)) 30217658Sjulian return; 30317658Sjulian dumpsize = Maxmem; 30417658Sjulian printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo); 30517658Sjulian printf("dump "); 30617658Sjulian switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) { 30717658Sjulian 30817658Sjulian case ENXIO: 30917658Sjulian printf("device bad\n"); 31017658Sjulian break; 31117658Sjulian 31217658Sjulian case EFAULT: 31317658Sjulian printf("device not ready\n"); 31417658Sjulian break; 31517658Sjulian 31617658Sjulian case EINVAL: 31717658Sjulian printf("area improper\n"); 31817658Sjulian break; 31917658Sjulian 32017658Sjulian case EIO: 32117658Sjulian printf("i/o error\n"); 32217658Sjulian break; 32317658Sjulian 32417658Sjulian case EINTR: 32517658Sjulian printf("aborted from console\n"); 32617658Sjulian break; 32717658Sjulian 32817658Sjulian default: 32917658Sjulian printf("succeeded\n"); 33017658Sjulian break; 33117658Sjulian } 33217658Sjulian} 33317658Sjulian 33417658Sjulian/* 33517658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 33617658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 33717658Sjulian * the disks as this often leads to recursive panics. 33817658Sjulian */ 33917658Sjulianvoid 34017658Sjulianpanic(const char *fmt, ...) 34117658Sjulian{ 34217658Sjulian int bootopt; 34317658Sjulian va_list ap; 34417658Sjulian 34517658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 34617658Sjulian if (panicstr) 34717658Sjulian bootopt |= RB_NOSYNC; 34817658Sjulian else 34917658Sjulian panicstr = fmt; 35017658Sjulian 35117658Sjulian printf("panic: "); 35217658Sjulian va_start(ap, fmt); 35317658Sjulian vprintf(fmt, ap); 35417658Sjulian va_end(ap); 35517658Sjulian printf("\n"); 35617658Sjulian 35717658Sjulian#if defined(DDB) 35817658Sjulian if (debugger_on_panic) 35917658Sjulian Debugger ("panic"); 36017658Sjulian#endif 36117658Sjulian boot(bootopt); 36217658Sjulian} 36317658Sjulian 36417768Sjulian/* 36517768Sjulian * Two routines to handle adding/deleting items on the 36617768Sjulian * shutdown callout lists 36717768Sjulian * 36817768Sjulian * at_shutdown(): 36917658Sjulian * Take the arguments given and put them onto the shutdown callout list. 37017658Sjulian * However first make sure that it's not already there. 37117658Sjulian * returns 0 on success. 37217658Sjulian */ 37317658Sjulianint 37417768Sjulianat_shutdown(bootlist_fn function, void *arg, int position) 37517658Sjulian{ 37617768Sjulian sle_p ep, *epp; 37717768Sjulian 37817768Sjulian switch(position) { 37917768Sjulian case SHUTDOWN_PRE_SYNC: 38017768Sjulian epp = &shutdown_list1; 38117768Sjulian break; 38217768Sjulian case SHUTDOWN_POST_SYNC: 38317768Sjulian epp = &shutdown_list2; 38417768Sjulian break; 38517768Sjulian default: 38617768Sjulian printf("bad exit callout list specified\n"); 38717768Sjulian return (EINVAL); 38817768Sjulian } 38917768Sjulian if (rm_at_shutdown(function, arg)) 39017658Sjulian printf("exit callout entry already present\n"); 39117768Sjulian ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 39217768Sjulian if (ep == NULL) 39317768Sjulian return (ENOMEM); 39417768Sjulian ep->next = *epp; 39517658Sjulian ep->function = function; 39617658Sjulian ep->arg = arg; 39717768Sjulian *epp = ep; 39817768Sjulian return (0); 39917658Sjulian} 40017768Sjulian 40117658Sjulian/* 40217768Sjulian * Scan the exit callout lists for the given items and remove them. 40317658Sjulian * Returns the number of items removed. 40417658Sjulian */ 40517658Sjulianint 40617658Sjulianrm_at_shutdown(bootlist_fn function, void *arg) 40717658Sjulian{ 40817768Sjulian sle_p *epp, ep; 40917768Sjulian int count; 41017658Sjulian 41117768Sjulian count = 0; 41217768Sjulian epp = &shutdown_list1; 41317658Sjulian ep = *epp; 41417768Sjulian while (ep) { 41517834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 41617658Sjulian *epp = ep->next; 41717768Sjulian free(ep, M_TEMP); 41817658Sjulian count++; 41917658Sjulian } else { 42017658Sjulian epp = &ep->next; 42117658Sjulian } 42217658Sjulian ep = *epp; 42317658Sjulian } 42417768Sjulian epp = &shutdown_list2; 42517768Sjulian ep = *epp; 42617768Sjulian while (ep) { 42717834Sjulian if ((ep->function == function) && (ep->arg == arg)) { 42817768Sjulian *epp = ep->next; 42917768Sjulian free(ep, M_TEMP); 43017768Sjulian count++; 43117768Sjulian } else { 43217768Sjulian epp = &ep->next; 43317768Sjulian } 43417768Sjulian ep = *epp; 43517768Sjulian } 43617768Sjulian return (count); 43717658Sjulian} 43817658Sjulian 439