kern_shutdown.c revision 62573
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 62573 2000-07-04 11:25:35Z phk $ 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> 5839237Sgibbs#include <sys/queue.h> 5917658Sjulian#include <sys/sysctl.h> 6017658Sjulian#include <sys/conf.h> 6117658Sjulian#include <sys/sysproto.h> 6249558Sphk#include <sys/cons.h> 6317658Sjulian 6417658Sjulian#include <machine/pcb.h> 6517658Sjulian#include <machine/clock.h> 6617658Sjulian#include <machine/md_var.h> 6726812Speter#include <machine/smp.h> /* smp_active, cpuid */ 6817658Sjulian 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 8128769Sbde#ifdef DDB 8217658Sjulian#ifdef DDB_UNATTENDED 8342135Smsmithint debugger_on_panic = 0; 8417658Sjulian#else 8542135Smsmithint debugger_on_panic = 1; 8617658Sjulian#endif 8717658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 8846381Sbillf &debugger_on_panic, 0, "Run debugger on kernel panic"); 8917658Sjulian#endif 9017658Sjulian 9143436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 9243436Smsmith 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 11031275Sbdestatic void boot __P((int)) __dead2; 11131275Sbdestatic void dumpsys __P((void)); 11248868Sphkstatic int setdumpdev __P((dev_t dev)); 11350107Smsmithstatic void poweroff_wait __P((void *, int)); 11454233Sphkstatic void print_uptime __P((void)); 11550107Smsmithstatic void shutdown_halt __P((void *junk, int howto)); 11650107Smsmithstatic void shutdown_panic __P((void *junk, int howto)); 11750107Smsmithstatic void shutdown_reset __P((void *junk, int howto)); 11817658Sjulian 11950107Smsmith/* register various local shutdown events */ 12050107Smsmithstatic void 12150107Smsmithshutdown_conf(void *unused) 12250107Smsmith{ 12350107Smsmith EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, SHUTDOWN_PRI_FIRST); 12450107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, SHUTDOWN_PRI_LAST + 100); 12550107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, SHUTDOWN_PRI_LAST + 100); 12650107Smsmith EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, SHUTDOWN_PRI_LAST + 200); 12750107Smsmith} 12848868Sphk 12950107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 13050107Smsmith 13117658Sjulian/* ARGSUSED */ 13217658Sjulian 13317658Sjulian/* 13417658Sjulian * The system call that results in a reboot 13517658Sjulian */ 13617658Sjulianint 13730994Sphkreboot(p, uap) 13817658Sjulian struct proc *p; 13917658Sjulian struct reboot_args *uap; 14017658Sjulian{ 14117658Sjulian int error; 14217658Sjulian 14346112Sphk if ((error = suser(p))) 14417658Sjulian return (error); 14517658Sjulian 14617658Sjulian boot(uap->opt); 14717658Sjulian return (0); 14817658Sjulian} 14917658Sjulian 15017658Sjulian/* 15117658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 15217658Sjulian */ 15317658Sjulianvoid 15428769Sbdeshutdown_nice() 15517658Sjulian{ 15617658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 15717658Sjulian if (initproc != NULL) { 15817658Sjulian psignal(initproc, SIGINT); 15917658Sjulian } else { 16017658Sjulian /* No init(8) running, so simply reboot */ 16117658Sjulian boot(RB_NOSYNC); 16217658Sjulian } 16317658Sjulian return; 16417658Sjulian} 16517658Sjulianstatic int waittime = -1; 16617658Sjulianstatic struct pcb dumppcb; 16717658Sjulian 16854233Sphkstatic void 16954233Sphkprint_uptime() 17054233Sphk{ 17154233Sphk int f; 17254233Sphk struct timespec ts; 17354233Sphk 17454233Sphk getnanouptime(&ts); 17554233Sphk printf("Uptime: "); 17654233Sphk f = 0; 17754233Sphk if (ts.tv_sec >= 86400) { 17854233Sphk printf("%ldd", ts.tv_sec / 86400); 17954233Sphk ts.tv_sec %= 86400; 18054233Sphk f = 1; 18154233Sphk } 18254233Sphk if (f || ts.tv_sec >= 3600) { 18354233Sphk printf("%ldh", ts.tv_sec / 3600); 18454233Sphk ts.tv_sec %= 3600; 18554233Sphk f = 1; 18654233Sphk } 18754233Sphk if (f || ts.tv_sec >= 60) { 18854233Sphk printf("%ldm", ts.tv_sec / 60); 18954233Sphk ts.tv_sec %= 60; 19054233Sphk f = 1; 19154233Sphk } 19254233Sphk printf("%lds\n", ts.tv_sec); 19354233Sphk} 19454233Sphk 19517658Sjulian/* 19617658Sjulian * Go through the rigmarole of shutting down.. 19717658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 19817658Sjulian * anything machine dependant in it. 19917658Sjulian */ 20031275Sbdestatic void 20117658Sjulianboot(howto) 20217658Sjulian int howto; 20317658Sjulian{ 20417658Sjulian 20525164Speter#ifdef SMP 20625164Speter if (smp_active) { 20726812Speter printf("boot() called on cpu#%d\n", cpuid); 20825164Speter } 20925164Speter#endif 21027997Sjulian /* 21127997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 21227997Sjulian */ 21350107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 21427997Sjulian 21527997Sjulian /* 21627997Sjulian * Now sync filesystems 21727997Sjulian */ 21817658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 21917658Sjulian register struct buf *bp; 22017658Sjulian int iter, nbusy; 22117658Sjulian 22217658Sjulian waittime = 0; 22317658Sjulian printf("\nsyncing disks... "); 22417658Sjulian 22530994Sphk sync(&proc0, NULL); 22617658Sjulian 22734266Sjulian /* 22834266Sjulian * With soft updates, some buffers that are 22934266Sjulian * written will be remarked as dirty until other 23034266Sjulian * buffers are written. 23134266Sjulian */ 23217658Sjulian for (iter = 0; iter < 20; iter++) { 23317658Sjulian nbusy = 0; 23417658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 23548225Smckusick if ((bp->b_flags & B_INVAL) == 0 && 23648225Smckusick BUF_REFCNT(bp) > 0) { 23717658Sjulian nbusy++; 23834266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 23934266Sjulian == B_DELWRI) { 24034266Sjulian /* bawrite(bp);*/ 24134266Sjulian nbusy++; 24217658Sjulian } 24317658Sjulian } 24417658Sjulian if (nbusy == 0) 24517658Sjulian break; 24617658Sjulian printf("%d ", nbusy); 24734266Sjulian sync(&proc0, NULL); 24834266Sjulian DELAY(50000 * iter); 24917658Sjulian } 25053023Sphk printf("\n"); 25141137Smsmith /* 25241137Smsmith * Count only busy local buffers to prevent forcing 25341137Smsmith * a fsck if we're just a client of a wedged NFS server 25441137Smsmith */ 25541137Smsmith nbusy = 0; 25641137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 25748225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 25848225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 25953023Sphk if (bp->b_dev == NODEV) { 26053452Sphk TAILQ_REMOVE(&mountlist, 26148225Smckusick bp->b_vp->v_mount, mnt_list); 26253023Sphk continue; 26353023Sphk } 26453023Sphk nbusy++; 26553023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 26653023Sphk printf( 26753023Sphk "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n", 26853023Sphk nbusy, devtoname(bp->b_dev), 26953023Sphk bp->b_flags, (long)bp->b_blkno, 27053023Sphk (long)bp->b_lblkno); 27153023Sphk#endif 27246568Speter } 27341137Smsmith } 27417658Sjulian if (nbusy) { 27517658Sjulian /* 27617658Sjulian * Failed to sync all blocks. Indicate this and don't 27717658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 27817658Sjulian */ 27953023Sphk printf("giving up on %d buffers\n", nbusy); 28017658Sjulian DELAY(5000000); /* 5 seconds */ 28117658Sjulian } else { 28217658Sjulian printf("done\n"); 28317658Sjulian /* 28417658Sjulian * Unmount filesystems 28517658Sjulian */ 28617658Sjulian if (panicstr == 0) 28717658Sjulian vfs_unmountall(); 28817658Sjulian } 28939237Sgibbs DELAY(100000); /* wait for console output to finish */ 29017658Sjulian } 29127997Sjulian 29254233Sphk print_uptime(); 29354233Sphk 29427997Sjulian /* 29527997Sjulian * Ok, now do things that assume all filesystem activity has 29627997Sjulian * been completed. 29727997Sjulian */ 29850107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 29939237Sgibbs splhigh(); 30039522Sdt if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) { 30139237Sgibbs savectx(&dumppcb); 30239237Sgibbs#ifdef __i386__ 30339237Sgibbs dumppcb.pcb_cr3 = rcr3(); 30439237Sgibbs#endif 30539237Sgibbs dumpsys(); 30617768Sjulian } 30739237Sgibbs 30839237Sgibbs /* Now that we're going to really halt the system... */ 30950107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 31039237Sgibbs 31150107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 31250107Smsmith /* NOTREACHED */ 31350107Smsmith} 31450107Smsmith 31550107Smsmith/* 31650107Smsmith * If the shutdown was a clean halt, behave accordingly. 31750107Smsmith */ 31850107Smsmithstatic void 31950107Smsmithshutdown_halt(void *junk, int howto) 32050107Smsmith{ 32117658Sjulian if (howto & RB_HALT) { 32217658Sjulian printf("\n"); 32317658Sjulian printf("The operating system has halted.\n"); 32417658Sjulian printf("Please press any key to reboot.\n\n"); 32519274Sjulian switch (cngetc()) { 32619274Sjulian case -1: /* No console, just die */ 32719274Sjulian cpu_halt(); 32819274Sjulian /* NOTREACHED */ 32919274Sjulian default: 33039237Sgibbs howto &= ~RB_HALT; 33119274Sjulian break; 33219274Sjulian } 33350107Smsmith } 33450107Smsmith} 33517658Sjulian 33650107Smsmith/* 33750107Smsmith * Check to see if the system paniced, pause and then reboot 33850107Smsmith * according to the specified delay. 33950107Smsmith */ 34050107Smsmithstatic void 34150107Smsmithshutdown_panic(void *junk, int howto) 34250107Smsmith{ 34350107Smsmith int loop; 34450107Smsmith 34550107Smsmith if (howto & RB_DUMP) { 34639237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 34739237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 34839237Sgibbs printf("Automatic reboot in %d seconds - " 34939237Sgibbs "press a key on the console to abort\n", 35039237Sgibbs PANIC_REBOOT_WAIT_TIME); 35139237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 35239237Sgibbs loop > 0; --loop) { 35339237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 35439237Sgibbs /* Did user type a key? */ 35539237Sgibbs if (cncheckc() != -1) 35639237Sgibbs break; 35717658Sjulian } 35839237Sgibbs if (!loop) 35950107Smsmith return; 36017658Sjulian } 36139237Sgibbs } else { /* zero time specified - reboot NOW */ 36250107Smsmith return; 36317658Sjulian } 36439237Sgibbs printf("--> Press a key on the console to reboot <--\n"); 36539237Sgibbs cngetc(); 36617658Sjulian } 36750107Smsmith} 36850107Smsmith 36950107Smsmith/* 37050107Smsmith * Everything done, now reset 37150107Smsmith */ 37250107Smsmithstatic void 37350107Smsmithshutdown_reset(void *junk, int howto) 37450107Smsmith{ 37517658Sjulian printf("Rebooting...\n"); 37617658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 37717677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 37817658Sjulian cpu_reset(); 37950107Smsmith /* NOTREACHED */ /* assuming reset worked */ 38017658Sjulian} 38117658Sjulian 38217658Sjulian/* 38317658Sjulian * Magic number for savecore 38417658Sjulian * 38517658Sjulian * exported (symorder) and used at least by savecore(8) 38617658Sjulian * 38717658Sjulian */ 38817658Sjulianstatic u_long const dumpmag = 0x8fca0101UL; 38917658Sjulian 39017658Sjulianstatic int dumpsize = 0; /* also for savecore */ 39117658Sjulian 39217658Sjulianstatic int dodump = 1; 39317658Sjulian 39448868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, 39548868Sphk "Try to perform coredump on kernel panic"); 39648868Sphk 39748868Sphkstatic int 39848868Sphksetdumpdev(dev) 39948868Sphk dev_t dev; 40048868Sphk{ 40150571Sphk int psize; 40248868Sphk long newdumplo; 40348868Sphk 40448868Sphk if (dev == NODEV) { 40548868Sphk dumpdev = dev; 40648868Sphk return (0); 40748868Sphk } 40849679Sphk if (devsw(dev) == NULL) 40948868Sphk return (ENXIO); /* XXX is this right? */ 41049679Sphk if (devsw(dev)->d_psize == NULL) 41148868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 41249679Sphk psize = devsw(dev)->d_psize(dev); 41348868Sphk if (psize == -1) 41448868Sphk return (ENXIO); /* XXX should be ENODEV ? */ 41548868Sphk /* 41650571Sphk * XXX should clean up checking in dumpsys() to be more like this. 41748868Sphk */ 41848868Sphk newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE; 41948868Sphk if (newdumplo < 0) 42048868Sphk return (ENOSPC); 42148868Sphk dumpdev = dev; 42248868Sphk dumplo = newdumplo; 42348868Sphk return (0); 42448868Sphk} 42548868Sphk 42648868Sphk 42731403Sjulian/* ARGSUSED */ 42831403Sjulianstatic void dump_conf __P((void *dummy)); 42931403Sjulianstatic void 43031403Sjuliandump_conf(dummy) 43131403Sjulian void *dummy; 43231403Sjulian{ 43348868Sphk if (setdumpdev(dumpdev) != 0) 43448868Sphk dumpdev = NODEV; 43531403Sjulian} 43648868Sphk 43731403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL) 43831403Sjulian 43948868Sphkstatic int 44062573Sphksysctl_kern_dumpdev(SYSCTL_HANDLER_ARGS) 44148868Sphk{ 44248868Sphk int error; 44348868Sphk udev_t ndumpdev; 44448868Sphk 44553838Sphk ndumpdev = dev2udev(dumpdev); 44648868Sphk error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req); 44748868Sphk if (error == 0 && req->newptr != NULL) 44853838Sphk error = setdumpdev(udev2dev(ndumpdev, 0)); 44948868Sphk return (error); 45048868Sphk} 45148868Sphk 45248868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW, 45348868Sphk 0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", ""); 45448868Sphk 45517658Sjulian/* 45617658Sjulian * Doadump comes here after turning off memory management and 45717658Sjulian * getting on the dump stack, either when called above, or by 45817658Sjulian * the auto-restart code. 45917658Sjulian */ 46017658Sjulianstatic void 46117658Sjuliandumpsys(void) 46217658Sjulian{ 46349627Salfred int error; 46450571Sphk static int dumping; 46517658Sjulian 46650571Sphk if (dumping++) { 46750571Sphk printf("Dump already in progress, bailing...\n"); 46850571Sphk return; 46950571Sphk } 47017658Sjulian if (!dodump) 47117658Sjulian return; 47217658Sjulian if (dumpdev == NODEV) 47317658Sjulian return; 47449679Sphk if (!(devsw(dumpdev))) 47517658Sjulian return; 47649679Sphk if (!(devsw(dumpdev)->d_dump)) 47717658Sjulian return; 47817658Sjulian dumpsize = Maxmem; 47950571Sphk printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo); 48017658Sjulian printf("dump "); 48149679Sphk error = (*devsw(dumpdev)->d_dump)(dumpdev); 48249627Salfred if (error == 0) { 48349627Salfred printf("succeeded\n"); 48449627Salfred return; 48549627Salfred } 48649627Salfred printf("failed, reason: "); 48749627Salfred switch (error) { 48849627Salfred case ENODEV: 48949627Salfred printf("device doesn't support a dump routine\n"); 49049627Salfred break; 49117658Sjulian 49217658Sjulian case ENXIO: 49317658Sjulian printf("device bad\n"); 49417658Sjulian break; 49517658Sjulian 49617658Sjulian case EFAULT: 49717658Sjulian printf("device not ready\n"); 49817658Sjulian break; 49917658Sjulian 50017658Sjulian case EINVAL: 50117658Sjulian printf("area improper\n"); 50217658Sjulian break; 50317658Sjulian 50417658Sjulian case EIO: 50517658Sjulian printf("i/o error\n"); 50617658Sjulian break; 50717658Sjulian 50817658Sjulian case EINTR: 50917658Sjulian printf("aborted from console\n"); 51017658Sjulian break; 51117658Sjulian 51217658Sjulian default: 51349627Salfred printf("unknown, error = %d\n", error); 51417658Sjulian break; 51517658Sjulian } 51617658Sjulian} 51717658Sjulian 51817658Sjulian/* 51917658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 52017658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 52117658Sjulian * the disks as this often leads to recursive panics. 52217658Sjulian */ 52317658Sjulianvoid 52417658Sjulianpanic(const char *fmt, ...) 52517658Sjulian{ 52617658Sjulian int bootopt; 52717658Sjulian va_list ap; 52838874Sache static char buf[256]; 52917658Sjulian 53017658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 53117658Sjulian if (panicstr) 53217658Sjulian bootopt |= RB_NOSYNC; 53317658Sjulian else 53417658Sjulian panicstr = fmt; 53517658Sjulian 53617658Sjulian va_start(ap, fmt); 53741514Sarchie (void)vsnprintf(buf, sizeof(buf), fmt, ap); 53838874Sache if (panicstr == fmt) 53938874Sache panicstr = buf; 54017658Sjulian va_end(ap); 54138874Sache printf("panic: %s\n", buf); 54226100Sfsmp#ifdef SMP 54329128Speter /* three seperate prints in case of an unmapped page and trap */ 54429128Speter printf("mp_lock = %08x; ", mp_lock); 54529128Speter printf("cpuid = %d; ", cpuid); 54629128Speter printf("lapic.id = %08x\n", lapic.id); 54726100Sfsmp#endif 54817658Sjulian 54917658Sjulian#if defined(DDB) 55017658Sjulian if (debugger_on_panic) 55117658Sjulian Debugger ("panic"); 55217658Sjulian#endif 55317658Sjulian boot(bootopt); 55417658Sjulian} 55517658Sjulian 55617768Sjulian/* 55743436Smsmith * Support for poweroff delay. 55843436Smsmith */ 55954248Smsmith#ifndef POWEROFF_DELAY 56054248Smsmith# define POWEROFF_DELAY 5000 56154248Smsmith#endif 56254248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 56354248Smsmith 56443436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 56543436Smsmith &poweroff_delay, 0, ""); 56643436Smsmith 56750107Smsmithstatic void 56850107Smsmithpoweroff_wait(void *junk, int howto) 56943436Smsmith{ 57043436Smsmith if(!(howto & RB_POWEROFF) || poweroff_delay <= 0) 57143436Smsmith return; 57243436Smsmith DELAY(poweroff_delay * 1000); 57343436Smsmith} 57455539Sluoqi 57555539Sluoqi/* 57655539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 57755539Sluoqi * points in their main loops prior to a system shutdown, so that they 57855539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 57955539Sluoqi * to cause sync to fail). For each of these system processes, register 58055539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 58155539Sluoqi */ 58255539Sluoqistatic int kproc_shutdown_wait = 60; 58355539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 58455539Sluoqi &kproc_shutdown_wait, 0, ""); 58555539Sluoqi 58655539Sluoqivoid 58755539Sluoqishutdown_kproc(void *arg, int howto) 58855539Sluoqi{ 58955539Sluoqi struct proc *p; 59055539Sluoqi int error; 59155539Sluoqi 59255539Sluoqi if (panicstr) 59355539Sluoqi return; 59455539Sluoqi 59555539Sluoqi p = (struct proc *)arg; 59655539Sluoqi printf("Waiting (max %d seconds) for system process `%s' to stop...", 59755862Sluoqi kproc_shutdown_wait, p->p_comm); 59855862Sluoqi error = suspend_kproc(p, kproc_shutdown_wait * hz); 59955539Sluoqi 60055539Sluoqi if (error == EWOULDBLOCK) 60155539Sluoqi printf("timed out\n"); 60255539Sluoqi else 60355539Sluoqi printf("stopped\n"); 60455539Sluoqi} 605