kern_shutdown.c revision 67365
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 3950477Speter * $FreeBSD: head/sys/kern/kern_shutdown.c 67365 2000-10-20 07:58:15Z jhb $ 4017658Sjulian */ 4117658Sjulian 4217658Sjulian#include "opt_ddb.h" 4333445Seivind#include "opt_hw_wdog.h" 4428976Sbde#include "opt_panic.h" 4528976Sbde#include "opt_show_busybufs.h" 4617658Sjulian 4717658Sjulian#include <sys/param.h> 4817658Sjulian#include <sys/systm.h> 4950107Smsmith#include <sys/eventhandler.h> 5060041Sphk#include <sys/bio.h> 5131275Sbde#include <sys/buf.h> 5217658Sjulian#include <sys/reboot.h> 5317658Sjulian#include <sys/proc.h> 5441137Smsmith#include <sys/vnode.h> 5517658Sjulian#include <sys/kernel.h> 5655539Sluoqi#include <sys/kthread.h> 5721776Sbde#include <sys/mount.h> 5867365Sjhb#include <sys/mutex.h> 5939237Sgibbs#include <sys/queue.h> 6017658Sjulian#include <sys/sysctl.h> 6117658Sjulian#include <sys/conf.h> 6217658Sjulian#include <sys/sysproto.h> 6349558Sphk#include <sys/cons.h> 6417658Sjulian 6517658Sjulian#include <machine/pcb.h> 6665557Sjasone#include <machine/lock.h> 6717658Sjulian#include <machine/md_var.h> 6826812Speter#include <machine/smp.h> /* smp_active, cpuid */ 6917658Sjulian 7017658Sjulian#include <sys/signalvar.h> 7117658Sjulian 7217658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7317658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7417658Sjulian#endif 7517658Sjulian 7617658Sjulian/* 7717658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7817658Sjulian * ANSI and traditional C compilers. 7917658Sjulian */ 8017658Sjulian#include <machine/stdarg.h> 8117658Sjulian 8228769Sbde#ifdef DDB 8317658Sjulian#ifdef DDB_UNATTENDED 8442135Smsmithint debugger_on_panic = 0; 8517658Sjulian#else 8642135Smsmithint debugger_on_panic = 1; 8717658Sjulian#endif 8817658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 8946381Sbillf &debugger_on_panic, 0, "Run debugger on kernel panic"); 9017658Sjulian#endif 9117658Sjulian 9243436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 9343436Smsmith 9428000Sjulian#ifdef HW_WDOG 9517658Sjulian/* 9627997Sjulian * If there is a hardware watchdog, point this at the function needed to 9727997Sjulian * hold it off. 9827997Sjulian * It's needed when the kernel needs to do some lengthy operations. 9927997Sjulian * e.g. in wd.c when dumping core.. It's most annoying to have 10027997Sjulian * your precious core-dump only half written because the wdog kicked in. 10127997Sjulian */ 10227997Sjulianwatchdog_tickle_fn wdog_tickler = NULL; 10328000Sjulian#endif /* HW_WDOG */ 10427997Sjulian 10527997Sjulian/* 10617658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 10717658Sjulian * to indicate that the kernel has already called panic. 10817658Sjulian */ 10917658Sjulianconst char *panicstr; 11017658Sjulian 11167093Spsint dumping; /* system is dumping */ 11267093Sps 11365395Speterstatic void boot(int) __dead2; 11465395Speterstatic void dumpsys(void); 11565395Speterstatic void poweroff_wait(void *, int); 11665395Speterstatic void shutdown_halt(void *junk, int howto); 11765395Speterstatic void shutdown_panic(void *junk, int howto); 11865395Speterstatic void shutdown_reset(void *junk, int howto); 11917658Sjulian 12050107Smsmith/* register various local shutdown events */ 12150107Smsmithstatic void 12250107Smsmithshutdown_conf(void *unused) 12350107Smsmith{ 12450107Smsmith EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 12550107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 12650107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 12750107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 12850107Smsmith} 12948868Sphk 13050107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 13150107Smsmith 13217658Sjulian/* ARGSUSED */ 13317658Sjulian 13417658Sjulian/* 13517658Sjulian * The system call that results in a reboot 13617658Sjulian */ 13717658Sjulianint 13865395Speterreboot(struct proc *p, struct reboot_args *uap) 13917658Sjulian{ 14017658Sjulian int error; 14117658Sjulian 14246112Sphk if ((error = suser(p))) 14317658Sjulian return (error); 14417658Sjulian 14517658Sjulian boot(uap->opt); 14617658Sjulian return (0); 14717658Sjulian} 14817658Sjulian 14917658Sjulian/* 15017658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 15117658Sjulian */ 15265268Smsmithstatic int shutdown_howto = 0; 15365268Smsmith 15417658Sjulianvoid 15565268Smsmithshutdown_nice(int howto) 15617658Sjulian{ 15765268Smsmith shutdown_howto = howto; 15865268Smsmith 15917658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 16017658Sjulian if (initproc != NULL) { 16117658Sjulian psignal(initproc, SIGINT); 16217658Sjulian } else { 16317658Sjulian /* No init(8) running, so simply reboot */ 16417658Sjulian boot(RB_NOSYNC); 16517658Sjulian } 16617658Sjulian return; 16717658Sjulian} 16817658Sjulianstatic int waittime = -1; 16917658Sjulianstatic struct pcb dumppcb; 17017658Sjulian 17154233Sphkstatic void 17265395Speterprint_uptime(void) 17354233Sphk{ 17454233Sphk int f; 17554233Sphk struct timespec ts; 17654233Sphk 17754233Sphk getnanouptime(&ts); 17854233Sphk printf("Uptime: "); 17954233Sphk f = 0; 18054233Sphk if (ts.tv_sec >= 86400) { 18165764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 18254233Sphk ts.tv_sec %= 86400; 18354233Sphk f = 1; 18454233Sphk } 18554233Sphk if (f || ts.tv_sec >= 3600) { 18665764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 18754233Sphk ts.tv_sec %= 3600; 18854233Sphk f = 1; 18954233Sphk } 19054233Sphk if (f || ts.tv_sec >= 60) { 19165764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 19254233Sphk ts.tv_sec %= 60; 19354233Sphk f = 1; 19454233Sphk } 19565764Sjhb printf("%lds\n", (long)ts.tv_sec); 19654233Sphk} 19754233Sphk 19817658Sjulian/* 19917658Sjulian * Go through the rigmarole of shutting down.. 20017658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 20117658Sjulian * anything machine dependant in it. 20217658Sjulian */ 20331275Sbdestatic void 20465395Speterboot(int howto) 20517658Sjulian{ 20617658Sjulian 20765268Smsmith /* collect extra flags that shutdown_nice might have set */ 20865268Smsmith howto |= shutdown_howto; 20965268Smsmith 21025164Speter#ifdef SMP 21165395Speter if (smp_active) 21226812Speter printf("boot() called on cpu#%d\n", cpuid); 21325164Speter#endif 21427997Sjulian /* 21527997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 21627997Sjulian */ 21750107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 21827997Sjulian 21927997Sjulian /* 22027997Sjulian * Now sync filesystems 22127997Sjulian */ 22217658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 22317658Sjulian register struct buf *bp; 22465707Sjasone int iter, nbusy, pbusy; 22565707Sjasone int subiter; 22617658Sjulian 22717658Sjulian waittime = 0; 22817658Sjulian printf("\nsyncing disks... "); 22917658Sjulian 23030994Sphk sync(&proc0, NULL); 23117658Sjulian 23234266Sjulian /* 23334266Sjulian * With soft updates, some buffers that are 23434266Sjulian * written will be remarked as dirty until other 23534266Sjulian * buffers are written. 23634266Sjulian */ 23765707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 23817658Sjulian nbusy = 0; 23917658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 24048225Smckusick if ((bp->b_flags & B_INVAL) == 0 && 24148225Smckusick BUF_REFCNT(bp) > 0) { 24217658Sjulian nbusy++; 24334266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 24434266Sjulian == B_DELWRI) { 24534266Sjulian /* bawrite(bp);*/ 24634266Sjulian nbusy++; 24717658Sjulian } 24817658Sjulian } 24917658Sjulian if (nbusy == 0) 25017658Sjulian break; 25117658Sjulian printf("%d ", nbusy); 25265707Sjasone if (nbusy < pbusy) 25365707Sjasone iter = 0; 25465707Sjasone pbusy = nbusy; 25534266Sjulian sync(&proc0, NULL); 25665707Sjasone if (curproc != NULL) { 25765707Sjasone for (subiter = 0; subiter < 50 * iter; subiter++) { 25865707Sjasone mtx_enter(&sched_lock, MTX_SPIN); 25965707Sjasone setrunqueue(curproc); 26065707Sjasone mi_switch(); /* Allow interrupt threads to run */ 26165707Sjasone mtx_exit(&sched_lock, MTX_SPIN); 26265707Sjasone DELAY(1000); 26365707Sjasone } 26465707Sjasone } else 26534266Sjulian DELAY(50000 * iter); 26617658Sjulian } 26753023Sphk printf("\n"); 26841137Smsmith /* 26941137Smsmith * Count only busy local buffers to prevent forcing 27041137Smsmith * a fsck if we're just a client of a wedged NFS server 27141137Smsmith */ 27241137Smsmith nbusy = 0; 27341137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 27448225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 27548225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 27653023Sphk if (bp->b_dev == NODEV) { 27753452Sphk TAILQ_REMOVE(&mountlist, 27848225Smckusick bp->b_vp->v_mount, mnt_list); 27953023Sphk continue; 28053023Sphk } 28153023Sphk nbusy++; 28253023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 28353023Sphk printf( 28453023Sphk "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 28553023Sphk nbusy, devtoname(bp->b_dev), 28653023Sphk bp->b_flags, (long)bp->b_blkno, 28753023Sphk (long)bp->b_lblkno); 28853023Sphk#endif 28946568Speter } 29041137Smsmith } 29117658Sjulian if (nbusy) { 29217658Sjulian /* 29317658Sjulian * Failed to sync all blocks. Indicate this and don't 29417658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 29517658Sjulian */ 29653023Sphk printf("giving up on %d buffers\n", nbusy); 29717658Sjulian DELAY(5000000); /* 5 seconds */ 29817658Sjulian } else { 29917658Sjulian printf("done\n"); 30017658Sjulian /* 30117658Sjulian * Unmount filesystems 30217658Sjulian */ 30317658Sjulian if (panicstr == 0) 30417658Sjulian vfs_unmountall(); 30517658Sjulian } 30639237Sgibbs DELAY(100000); /* wait for console output to finish */ 30717658Sjulian } 30827997Sjulian 30954233Sphk print_uptime(); 31054233Sphk 31127997Sjulian /* 31227997Sjulian * Ok, now do things that assume all filesystem activity has 31327997Sjulian * been completed. 31427997Sjulian */ 31550107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 31639237Sgibbs splhigh(); 31765394Speter if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) 31839237Sgibbs dumpsys(); 31939237Sgibbs 32039237Sgibbs /* Now that we're going to really halt the system... */ 32150107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 32239237Sgibbs 32350107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 32450107Smsmith /* NOTREACHED */ 32550107Smsmith} 32650107Smsmith 32750107Smsmith/* 32850107Smsmith * If the shutdown was a clean halt, behave accordingly. 32950107Smsmith */ 33050107Smsmithstatic void 33150107Smsmithshutdown_halt(void *junk, int howto) 33250107Smsmith{ 33317658Sjulian if (howto & RB_HALT) { 33417658Sjulian printf("\n"); 33517658Sjulian printf("The operating system has halted.\n"); 33617658Sjulian printf("Please press any key to reboot.\n\n"); 33719274Sjulian switch (cngetc()) { 33819274Sjulian case -1: /* No console, just die */ 33919274Sjulian cpu_halt(); 34019274Sjulian /* NOTREACHED */ 34119274Sjulian default: 34239237Sgibbs howto &= ~RB_HALT; 34319274Sjulian break; 34419274Sjulian } 34550107Smsmith } 34650107Smsmith} 34717658Sjulian 34850107Smsmith/* 34950107Smsmith * Check to see if the system paniced, pause and then reboot 35050107Smsmith * according to the specified delay. 35150107Smsmith */ 35250107Smsmithstatic void 35350107Smsmithshutdown_panic(void *junk, int howto) 35450107Smsmith{ 35550107Smsmith int loop; 35650107Smsmith 35750107Smsmith if (howto & RB_DUMP) { 35839237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 35939237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 36039237Sgibbs printf("Automatic reboot in %d seconds - " 36139237Sgibbs "press a key on the console to abort\n", 36239237Sgibbs PANIC_REBOOT_WAIT_TIME); 36339237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 36439237Sgibbs loop > 0; --loop) { 36539237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 36639237Sgibbs /* Did user type a key? */ 36739237Sgibbs if (cncheckc() != -1) 36839237Sgibbs break; 36917658Sjulian } 37039237Sgibbs if (!loop) 37150107Smsmith return; 37217658Sjulian } 37339237Sgibbs } else { /* zero time specified - reboot NOW */ 37450107Smsmith return; 37517658Sjulian } 37639237Sgibbs printf("--> Press a key on the console to reboot <--\n"); 37739237Sgibbs cngetc(); 37817658Sjulian } 37950107Smsmith} 38050107Smsmith 38150107Smsmith/* 38250107Smsmith * Everything done, now reset 38350107Smsmith */ 38450107Smsmithstatic void 38550107Smsmithshutdown_reset(void *junk, int howto) 38650107Smsmith{ 38717658Sjulian printf("Rebooting...\n"); 38817658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 38917677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 39017658Sjulian cpu_reset(); 39150107Smsmith /* NOTREACHED */ /* assuming reset worked */ 39217658Sjulian} 39317658Sjulian 39417658Sjulian/* 39517658Sjulian * Magic number for savecore 39617658Sjulian * 39717658Sjulian * exported (symorder) and used at least by savecore(8) 39817658Sjulian * 39917658Sjulian */ 40017658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 40117658Sjulian 40217658Sjulianstatic int dumpsize = 0; /* also for savecore */ 40317658Sjulian 40417658Sjulianstatic int dodump = 1; 40517658Sjulian 40648868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 40748868Sphk "Try to perform coredump on kernel panic"); 40848868Sphk 40948868Sphkstatic int 41065395Spetersetdumpdev(dev_t dev) 41148868Sphk{ 41250571Sphk int psize; 41348868Sphk long newdumplo; 41448868Sphk 41548868Sphk if (dev == NODEV) { 41648868Sphk dumpdev = dev; 41748868Sphk return (0); 41848868Sphk } 41949679Sphk if (devsw(dev) == NULL) 42048868Sphk return (ENXIO); /* XXX is this right? */ 42149679Sphk if (devsw(dev)->d_psize == NULL) 42248868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 42349679Sphk psize = devsw(dev)->d_psize(dev); 42448868Sphk if (psize == -1) 42548868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 42648868Sphk /* 42750571Sphk * XXX should clean up checking in dumpsys() to be more like this. 42848868Sphk */ 42948868Sphk newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE; 43048868Sphk if (newdumplo < 0) 43148868Sphk return (ENOSPC); 43248868Sphk dumpdev = dev; 43348868Sphk dumplo = newdumplo; 43448868Sphk return (0); 43548868Sphk} 43648868Sphk 43748868Sphk 43831403Sjulian/* ARGSUSED */ 43931403Sjulianstatic void 44065395Speterdump_conf(void *dummy) 44131403Sjulian{ 44248868Sphk if (setdumpdev(dumpdev) != 0) 44348868Sphk dumpdev = NODEV; 44431403Sjulian} 44548868Sphk 44631403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 44731403Sjulian 44848868Sphkstatic int 44962573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 45048868Sphk{ 45148868Sphk int error; 45248868Sphk udev_t ndumpdev; 45348868Sphk 45453838Sphk ndumpdev = dev2udev(dumpdev); 45548868Sphk error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 45648868Sphk if (error == 0 && req->newptr != NULL) 45753838Sphk error = setdumpdev(udev2dev(ndumpdev, 0)); 45848868Sphk return (error); 45948868Sphk} 46048868Sphk 46148868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 46248868Sphk 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 46348868Sphk 46417658Sjulian/* 46517658Sjulian * Doadump comes here after turning off memory management and 46617658Sjulian * getting on the dump stack, either when called above, or by 46717658Sjulian * the auto-restart code. 46817658Sjulian */ 46917658Sjulianstatic void 47017658Sjuliandumpsys(void) 47117658Sjulian{ 47249627Salfred int error; 47317658Sjulian 47465394Speter savectx(&dumppcb); 47550571Sphk if (dumping++) { 47650571Sphk printf("Dump already in progress, bailing...\n"); 47750571Sphk return; 47850571Sphk } 47917658Sjulian if (!dodump) 48017658Sjulian return; 48117658Sjulian if (dumpdev == NODEV) 48217658Sjulian return; 48349679Sphk if (!(devsw(dumpdev))) 48417658Sjulian return; 48549679Sphk if (!(devsw(dumpdev)->d_dump)) 48617658Sjulian return; 48717658Sjulian dumpsize = Maxmem; 48850571Sphk printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 48917658Sjulian printf("dump "); 49049679Sphk error = (*devsw(dumpdev)->d_dump)(dumpdev); 49149627Salfred if (error == 0) { 49249627Salfred printf("succeeded\n"); 49349627Salfred return; 49449627Salfred } 49549627Salfred printf("failed, reason: "); 49649627Salfred switch (error) { 49749627Salfred case ENODEV: 49849627Salfred printf("device doesn't support a dump routine\n"); 49949627Salfred break; 50017658Sjulian 50117658Sjulian case ENXIO: 50217658Sjulian printf("device bad\n"); 50317658Sjulian break; 50417658Sjulian 50517658Sjulian case EFAULT: 50617658Sjulian printf("device not ready\n"); 50717658Sjulian break; 50817658Sjulian 50917658Sjulian case EINVAL: 51017658Sjulian printf("area improper\n"); 51117658Sjulian break; 51217658Sjulian 51317658Sjulian case EIO: 51417658Sjulian printf("i/o error\n"); 51517658Sjulian break; 51617658Sjulian 51717658Sjulian case EINTR: 51817658Sjulian printf("aborted from console\n"); 51917658Sjulian break; 52017658Sjulian 52117658Sjulian default: 52249627Salfred printf("unknown, error = %d\n", error); 52317658Sjulian break; 52417658Sjulian } 52517658Sjulian} 52617658Sjulian 52717658Sjulian/* 52817658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 52917658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 53017658Sjulian * the disks as this often leads to recursive panics. 53117658Sjulian */ 53217658Sjulianvoid 53317658Sjulianpanic(const char *fmt, ...) 53417658Sjulian{ 53517658Sjulian int bootopt; 53617658Sjulian va_list ap; 53738874Sache static char buf[256]; 53817658Sjulian 53965557Sjasone#ifdef SMP 54065557Sjasone /* Only 1 CPU can panic at a time */ 54165557Sjasone s_lock(&panic_lock); 54265557Sjasone#endif 54365557Sjasone 54417658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 54517658Sjulian if (panicstr) 54617658Sjulian bootopt |= RB_NOSYNC; 54717658Sjulian else 54817658Sjulian panicstr = fmt; 54917658Sjulian 55017658Sjulian va_start(ap, fmt); 55141514Sarchie (void)vsnprintf(buf, sizeof(buf), fmt, ap); 55238874Sache if (panicstr == fmt) 55338874Sache panicstr = buf; 55417658Sjulian va_end(ap); 55538874Sache printf("panic: %s\n", buf); 55626100Sfsmp#ifdef SMP 55765557Sjasone /* two seperate prints in case of an unmapped page and trap */ 55829128Speter printf("cpuid = %d; ", cpuid); 55929128Speter printf("lapic.id = %08x\n", lapic.id); 56026100Sfsmp#endif 56117658Sjulian 56217658Sjulian#if defined(DDB) 56317658Sjulian if (debugger_on_panic) 56417658Sjulian Debugger ("panic"); 56517658Sjulian#endif 56617658Sjulian boot(bootopt); 56717658Sjulian} 56817658Sjulian 56917768Sjulian/* 57043436Smsmith * Support for poweroff delay. 57143436Smsmith */ 57254248Smsmith#ifndef POWEROFF_DELAY 57354248Smsmith# define POWEROFF_DELAY 5000 57454248Smsmith#endif 57554248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 57654248Smsmith 57743436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 57843436Smsmith &poweroff_delay, 0, ""); 57943436Smsmith 58050107Smsmithstatic void 58150107Smsmithpoweroff_wait(void *junk, int howto) 58243436Smsmith{ 58343436Smsmith if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 58443436Smsmith return; 58543436Smsmith DELAY(poweroff_delay * 1000); 58643436Smsmith} 58755539Sluoqi 58855539Sluoqi/* 58955539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 59055539Sluoqi * points in their main loops prior to a system shutdown, so that they 59155539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 59255539Sluoqi * to cause sync to fail). For each of these system processes, register 59355539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 59455539Sluoqi */ 59555539Sluoqistatic int kproc_shutdown_wait = 60; 59655539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 59755539Sluoqi &kproc_shutdown_wait, 0, ""); 59855539Sluoqi 59955539Sluoqivoid 60055539Sluoqishutdown_kproc(void *arg, int howto) 60155539Sluoqi{ 60255539Sluoqi struct proc *p; 60355539Sluoqi int error; 60455539Sluoqi 60555539Sluoqi if (panicstr) 60655539Sluoqi return; 60755539Sluoqi 60855539Sluoqi p = (struct proc *)arg; 60955539Sluoqi printf("Waiting (max %d seconds) for system process `%s' to stop...", 61055862Sluoqi kproc_shutdown_wait, p->p_comm); 61155862Sluoqi error = suspend_kproc(p, kproc_shutdown_wait * hz); 61255539Sluoqi 61355539Sluoqi if (error == EWOULDBLOCK) 61455539Sluoqi printf("timed out\n"); 61555539Sluoqi else 61655539Sluoqi printf("stopped\n"); 61755539Sluoqi} 618