kern_shutdown.c revision 137186
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 * 4. Neither the name of the University nor the names of its contributors 1917658Sjulian * may be used to endorse or promote products derived from this software 2017658Sjulian * without specific prior written permission. 2117658Sjulian * 2217658Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2317658Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2417658Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2517658Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2617658Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2717658Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2817658Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2917658Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3017658Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3117658Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3217658Sjulian * SUCH DAMAGE. 3317658Sjulian * 3417658Sjulian * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 3517658Sjulian */ 3617658Sjulian 37116182Sobrien#include <sys/cdefs.h> 38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_shutdown.c 137186 2004-11-04 07:59:57Z phk $"); 39116182Sobrien 40131927Smarcel#include "opt_kdb.h" 41106024Srwatson#include "opt_mac.h" 4228976Sbde#include "opt_panic.h" 4328976Sbde#include "opt_show_busybufs.h" 44134649Sscottl#include "opt_sched.h" 4517658Sjulian 4617658Sjulian#include <sys/param.h> 4717658Sjulian#include <sys/systm.h> 4860041Sphk#include <sys/bio.h> 4931275Sbde#include <sys/buf.h> 5078767Sjhb#include <sys/conf.h> 5178767Sjhb#include <sys/cons.h> 5278767Sjhb#include <sys/eventhandler.h> 53131927Smarcel#include <sys/kdb.h> 5417658Sjulian#include <sys/kernel.h> 5555539Sluoqi#include <sys/kthread.h> 56106024Srwatson#include <sys/mac.h> 5789601Ssobomax#include <sys/malloc.h> 5821776Sbde#include <sys/mount.h> 5978767Sjhb#include <sys/proc.h> 6078767Sjhb#include <sys/reboot.h> 6178767Sjhb#include <sys/resourcevar.h> 6278767Sjhb#include <sys/smp.h> /* smp_active */ 6317658Sjulian#include <sys/sysctl.h> 6417658Sjulian#include <sys/sysproto.h> 6578767Sjhb#include <sys/vnode.h> 6617658Sjulian 67118990Smarcel#include <machine/cpu.h> 6894169Sphk#include <machine/pcb.h> 6991778Sjake#include <machine/smp.h> 7017658Sjulian 7117658Sjulian#include <sys/signalvar.h> 7217658Sjulian 7317658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME 7417658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 7517658Sjulian#endif 7617658Sjulian 7717658Sjulian/* 7817658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both 7917658Sjulian * ANSI and traditional C compilers. 8017658Sjulian */ 8117658Sjulian#include <machine/stdarg.h> 8217658Sjulian 83131927Smarcel#ifdef KDB 84131927Smarcel#ifdef KDB_UNATTENDED 8542135Smsmithint debugger_on_panic = 0; 8617658Sjulian#else 8742135Smsmithint debugger_on_panic = 1; 8817658Sjulian#endif 8917658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW, 9046381Sbillf &debugger_on_panic, 0, "Run debugger on kernel panic"); 91103647Sjhb 92131927Smarcel#ifdef KDB_TRACE 93103647Sjhbint trace_on_panic = 1; 94103647Sjhb#else 95103647Sjhbint trace_on_panic = 0; 9617658Sjulian#endif 97103647SjhbSYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW, 98103647Sjhb &trace_on_panic, 0, "Print stack trace on kernel panic"); 99131927Smarcel#endif /* KDB */ 10017658Sjulian 101132506Srwatsonint sync_on_panic = 0; 10285202SpeterSYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW, 10385202Speter &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 10485202Speter 10543436SmsmithSYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment"); 10643436Smsmith 10717658Sjulian/* 10817658Sjulian * Variable panicstr contains argument to first call to panic; used as flag 10917658Sjulian * to indicate that the kernel has already called panic. 11017658Sjulian */ 11117658Sjulianconst char *panicstr; 11217658Sjulian 11393496Sphkint dumping; /* system is dumping */ 11493496Sphkstatic struct dumperinfo dumper; /* our selected dumper */ 11567093Sps 116131927Smarcel/* Context information for dump-debuggers. */ 117131927Smarcelstatic struct pcb dumppcb; /* Registers. */ 118131927Smarcelstatic lwpid_t dumptid; /* Thread ID. */ 119131927Smarcel 12065395Speterstatic void boot(int) __dead2; 12165395Speterstatic void poweroff_wait(void *, int); 12265395Speterstatic void shutdown_halt(void *junk, int howto); 12365395Speterstatic void shutdown_panic(void *junk, int howto); 12465395Speterstatic void shutdown_reset(void *junk, int howto); 12517658Sjulian 12650107Smsmith/* register various local shutdown events */ 127110859Salfredstatic void 12850107Smsmithshutdown_conf(void *unused) 12950107Smsmith{ 130110859Salfred 131110859Salfred EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 132110859Salfred SHUTDOWN_PRI_FIRST); 133110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 134110859Salfred SHUTDOWN_PRI_LAST + 100); 135110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 136110859Salfred SHUTDOWN_PRI_LAST + 100); 137110859Salfred EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 138110859Salfred SHUTDOWN_PRI_LAST + 200); 13950107Smsmith} 14048868Sphk 14150107SmsmithSYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL) 14250107Smsmith 14317658Sjulian/* 14417658Sjulian * The system call that results in a reboot 14582749Sdillon * 14682749Sdillon * MPSAFE 14717658Sjulian */ 14882749Sdillon/* ARGSUSED */ 14917658Sjulianint 15083366Sjulianreboot(struct thread *td, struct reboot_args *uap) 15117658Sjulian{ 15217658Sjulian int error; 15317658Sjulian 154106024Srwatson error = 0; 155106024Srwatson#ifdef MAC 156106024Srwatson error = mac_check_system_reboot(td->td_ucred, uap->opt); 157106024Srwatson#endif 158106024Srwatson if (error == 0) 159106024Srwatson error = suser(td); 160106024Srwatson if (error == 0) { 161106024Srwatson mtx_lock(&Giant); 16282749Sdillon boot(uap->opt); 163106024Srwatson mtx_unlock(&Giant); 164106024Srwatson } 16582749Sdillon return (error); 16617658Sjulian} 16717658Sjulian 16817658Sjulian/* 16917658Sjulian * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 17017658Sjulian */ 17165268Smsmithstatic int shutdown_howto = 0; 17265268Smsmith 17317658Sjulianvoid 17465268Smsmithshutdown_nice(int howto) 17517658Sjulian{ 176110859Salfred 17765268Smsmith shutdown_howto = howto; 178110859Salfred 17917658Sjulian /* Send a signal to init(8) and have it shutdown the world */ 18017658Sjulian if (initproc != NULL) { 18173913Sjhb PROC_LOCK(initproc); 18217658Sjulian psignal(initproc, SIGINT); 18373913Sjhb PROC_UNLOCK(initproc); 18417658Sjulian } else { 18517658Sjulian /* No init(8) running, so simply reboot */ 18617658Sjulian boot(RB_NOSYNC); 18717658Sjulian } 18817658Sjulian return; 18917658Sjulian} 19017658Sjulianstatic int waittime = -1; 19117658Sjulian 19254233Sphkstatic void 19365395Speterprint_uptime(void) 19454233Sphk{ 19554233Sphk int f; 19654233Sphk struct timespec ts; 19754233Sphk 19854233Sphk getnanouptime(&ts); 19954233Sphk printf("Uptime: "); 20054233Sphk f = 0; 20154233Sphk if (ts.tv_sec >= 86400) { 20265764Sjhb printf("%ldd", (long)ts.tv_sec / 86400); 20354233Sphk ts.tv_sec %= 86400; 20454233Sphk f = 1; 20554233Sphk } 20654233Sphk if (f || ts.tv_sec >= 3600) { 20765764Sjhb printf("%ldh", (long)ts.tv_sec / 3600); 20854233Sphk ts.tv_sec %= 3600; 20954233Sphk f = 1; 21054233Sphk } 21154233Sphk if (f || ts.tv_sec >= 60) { 21265764Sjhb printf("%ldm", (long)ts.tv_sec / 60); 21354233Sphk ts.tv_sec %= 60; 21454233Sphk f = 1; 21554233Sphk } 21665764Sjhb printf("%lds\n", (long)ts.tv_sec); 21754233Sphk} 21854233Sphk 21994169Sphkstatic void 22094169Sphkdoadump(void) 22194169Sphk{ 222110859Salfred 223132412Sjulian /* 224132412Sjulian * Sometimes people have to call this from the kernel debugger. 225132412Sjulian * (if 'panic' can not dump) 226132412Sjulian * Give them a clue as to why they can't dump. 227132412Sjulian */ 228132412Sjulian if (dumper.dumper == NULL) { 229132413Sjulian printf("Cannot dump. No dump device defined.\n"); 230132412Sjulian return; 231132412Sjulian } 232132412Sjulian 23394169Sphk savectx(&dumppcb); 234131927Smarcel dumptid = curthread->td_tid; 23594169Sphk dumping++; 23694169Sphk dumpsys(&dumper); 23794169Sphk} 23894169Sphk 23917658Sjulian/* 24017658Sjulian * Go through the rigmarole of shutting down.. 24117658Sjulian * this used to be in machdep.c but I'll be dammned if I could see 24217658Sjulian * anything machine dependant in it. 24317658Sjulian */ 24431275Sbdestatic void 24565395Speterboot(int howto) 24617658Sjulian{ 247133763Struckman static int first_buf_printf = 1; 24817658Sjulian 24965268Smsmith /* collect extra flags that shutdown_nice might have set */ 25065268Smsmith howto |= shutdown_howto; 25165268Smsmith 25282119Sjhb /* We are out of the debugger now. */ 253131927Smarcel kdb_active = 0; 25482119Sjhb 25525164Speter#ifdef SMP 25665395Speter if (smp_active) 25770861Sjake printf("boot() called on cpu#%d\n", PCPU_GET(cpuid)); 25825164Speter#endif 25927997Sjulian /* 26027997Sjulian * Do any callouts that should be done BEFORE syncing the filesystems. 26127997Sjulian */ 26250107Smsmith EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 26327997Sjulian 26427997Sjulian /* 26527997Sjulian * Now sync filesystems 26627997Sjulian */ 26717658Sjulian if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 26817658Sjulian register struct buf *bp; 26965707Sjasone int iter, nbusy, pbusy; 270131481Sjhb#ifndef PREEMPTION 27165707Sjasone int subiter; 272131481Sjhb#endif 27317658Sjulian 27417658Sjulian waittime = 0; 27517658Sjulian 27690361Sjulian sync(&thread0, NULL); 27717658Sjulian 27834266Sjulian /* 27934266Sjulian * With soft updates, some buffers that are 28034266Sjulian * written will be remarked as dirty until other 28134266Sjulian * buffers are written. 28234266Sjulian */ 28365707Sjasone for (iter = pbusy = 0; iter < 20; iter++) { 28417658Sjulian nbusy = 0; 28517658Sjulian for (bp = &buf[nbuf]; --bp >= buf; ) { 28648225Smckusick if ((bp->b_flags & B_INVAL) == 0 && 28748225Smckusick BUF_REFCNT(bp) > 0) { 28817658Sjulian nbusy++; 28934266Sjulian } else if ((bp->b_flags & (B_DELWRI | B_INVAL)) 29034266Sjulian == B_DELWRI) { 29134266Sjulian /* bawrite(bp);*/ 29234266Sjulian nbusy++; 29317658Sjulian } 29417658Sjulian } 295133763Struckman if (nbusy == 0) { 296133763Struckman if (first_buf_printf) 297136115Sphk printf("All buffers synced."); 29817658Sjulian break; 299133763Struckman } 300133763Struckman if (first_buf_printf) { 301133763Struckman printf("Syncing disks, buffers remaining... "); 302133763Struckman first_buf_printf = 0; 303133763Struckman } 30417658Sjulian printf("%d ", nbusy); 30565707Sjasone if (nbusy < pbusy) 30665707Sjasone iter = 0; 30765707Sjasone pbusy = nbusy; 30890361Sjulian sync(&thread0, NULL); 309131481Sjhb 310131481Sjhb#ifdef PREEMPTION 311131481Sjhb /* 312131481Sjhb * Drop Giant and spin for a while to allow 313131481Sjhb * interrupt threads to run. 314131481Sjhb */ 315131481Sjhb DROP_GIANT(); 31634266Sjulian DELAY(50000 * iter); 317131481Sjhb PICKUP_GIANT(); 318131481Sjhb#else 319131481Sjhb /* 320131481Sjhb * Drop Giant and context switch several times to 321131481Sjhb * allow interrupt threads to run. 322131481Sjhb */ 323131481Sjhb DROP_GIANT(); 324131481Sjhb for (subiter = 0; subiter < 50 * iter; subiter++) { 325131481Sjhb mtx_lock_spin(&sched_lock); 326131481Sjhb mi_switch(SW_VOL, NULL); 327131481Sjhb mtx_unlock_spin(&sched_lock); 328131481Sjhb DELAY(1000); 329131481Sjhb } 330131481Sjhb PICKUP_GIANT(); 331131481Sjhb#endif 33217658Sjulian } 333133418Snjl printf("\n"); 33441137Smsmith /* 33541137Smsmith * Count only busy local buffers to prevent forcing 33641137Smsmith * a fsck if we're just a client of a wedged NFS server 33741137Smsmith */ 33841137Smsmith nbusy = 0; 33941137Smsmith for (bp = &buf[nbuf]; --bp >= buf; ) { 34048225Smckusick if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) || 34148225Smckusick ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) { 342137186Sphk#if 0 343137186Sphk/* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 344130640Sphk if (bp->b_dev == NULL) { 34553452Sphk TAILQ_REMOVE(&mountlist, 34648225Smckusick bp->b_vp->v_mount, mnt_list); 34753023Sphk continue; 34853023Sphk } 349137186Sphk#endif 35053023Sphk nbusy++; 35153023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC) 35253023Sphk printf( 353137186Sphk "%d: bufobj:%p, flags:%0x, blkno:%ld, lblkno:%ld\n", 354137186Sphk nbusy, bp->b_bufobj, 35553023Sphk bp->b_flags, (long)bp->b_blkno, 35653023Sphk (long)bp->b_lblkno); 35753023Sphk#endif 35846568Speter } 35941137Smsmith } 36017658Sjulian if (nbusy) { 36117658Sjulian /* 36217658Sjulian * Failed to sync all blocks. Indicate this and don't 36317658Sjulian * unmount filesystems (thus forcing an fsck on reboot). 36417658Sjulian */ 365133763Struckman printf("Giving up on %d buffers\n", nbusy); 36617658Sjulian DELAY(5000000); /* 5 seconds */ 36717658Sjulian } else { 368133763Struckman if (!first_buf_printf) 369133763Struckman printf("Final sync complete\n"); 37017658Sjulian /* 37117658Sjulian * Unmount filesystems 37217658Sjulian */ 37317658Sjulian if (panicstr == 0) 37417658Sjulian vfs_unmountall(); 37517658Sjulian } 37639237Sgibbs DELAY(100000); /* wait for console output to finish */ 37717658Sjulian } 37827997Sjulian 37954233Sphk print_uptime(); 38054233Sphk 38127997Sjulian /* 38227997Sjulian * Ok, now do things that assume all filesystem activity has 38327997Sjulian * been completed. 38427997Sjulian */ 38550107Smsmith EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 38639237Sgibbs splhigh(); 387132412Sjulian if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 38894169Sphk doadump(); 38939237Sgibbs 39039237Sgibbs /* Now that we're going to really halt the system... */ 39150107Smsmith EVENTHANDLER_INVOKE(shutdown_final, howto); 39239237Sgibbs 39350107Smsmith for(;;) ; /* safety against shutdown_reset not working */ 39450107Smsmith /* NOTREACHED */ 39550107Smsmith} 39650107Smsmith 39750107Smsmith/* 39850107Smsmith * If the shutdown was a clean halt, behave accordingly. 39950107Smsmith */ 40050107Smsmithstatic void 40150107Smsmithshutdown_halt(void *junk, int howto) 40250107Smsmith{ 403110859Salfred 40417658Sjulian if (howto & RB_HALT) { 40517658Sjulian printf("\n"); 40617658Sjulian printf("The operating system has halted.\n"); 40717658Sjulian printf("Please press any key to reboot.\n\n"); 40819274Sjulian switch (cngetc()) { 40919274Sjulian case -1: /* No console, just die */ 41019274Sjulian cpu_halt(); 41119274Sjulian /* NOTREACHED */ 41219274Sjulian default: 41339237Sgibbs howto &= ~RB_HALT; 41419274Sjulian break; 41519274Sjulian } 41650107Smsmith } 41750107Smsmith} 41817658Sjulian 41950107Smsmith/* 42050107Smsmith * Check to see if the system paniced, pause and then reboot 42150107Smsmith * according to the specified delay. 42250107Smsmith */ 42350107Smsmithstatic void 42450107Smsmithshutdown_panic(void *junk, int howto) 42550107Smsmith{ 42650107Smsmith int loop; 42750107Smsmith 42850107Smsmith if (howto & RB_DUMP) { 42939237Sgibbs if (PANIC_REBOOT_WAIT_TIME != 0) { 43039237Sgibbs if (PANIC_REBOOT_WAIT_TIME != -1) { 43139237Sgibbs printf("Automatic reboot in %d seconds - " 43239237Sgibbs "press a key on the console to abort\n", 43339237Sgibbs PANIC_REBOOT_WAIT_TIME); 43439237Sgibbs for (loop = PANIC_REBOOT_WAIT_TIME * 10; 43539237Sgibbs loop > 0; --loop) { 43639237Sgibbs DELAY(1000 * 100); /* 1/10th second */ 43739237Sgibbs /* Did user type a key? */ 43839237Sgibbs if (cncheckc() != -1) 43939237Sgibbs break; 44017658Sjulian } 44139237Sgibbs if (!loop) 44250107Smsmith return; 44317658Sjulian } 44439237Sgibbs } else { /* zero time specified - reboot NOW */ 44550107Smsmith return; 44617658Sjulian } 44789522Snik printf("--> Press a key on the console to reboot,\n"); 44889522Snik printf("--> or switch off the system now.\n"); 44939237Sgibbs cngetc(); 45017658Sjulian } 45150107Smsmith} 45250107Smsmith 45350107Smsmith/* 45450107Smsmith * Everything done, now reset 45550107Smsmith */ 45650107Smsmithstatic void 45750107Smsmithshutdown_reset(void *junk, int howto) 45850107Smsmith{ 459110859Salfred 46017658Sjulian printf("Rebooting...\n"); 46117658Sjulian DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 46217677Sjulian /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 46317658Sjulian cpu_reset(); 46450107Smsmith /* NOTREACHED */ /* assuming reset worked */ 46517658Sjulian} 46617658Sjulian 46775570Sjhb#ifdef SMP 468101155Sjhbstatic u_int panic_cpu = NOCPU; 46975570Sjhb#endif 47075570Sjhb 47117658Sjulian/* 47217658Sjulian * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 47317658Sjulian * and then reboots. If we are called twice, then we avoid trying to sync 47417658Sjulian * the disks as this often leads to recursive panics. 47582749Sdillon * 47682749Sdillon * MPSAFE 47717658Sjulian */ 47817658Sjulianvoid 479130164Sphkpanic(const char *fmt, ...) 48017658Sjulian{ 481100209Sgallatin struct thread *td = curthread; 482103647Sjhb int bootopt, newpanic; 48317658Sjulian va_list ap; 48438874Sache static char buf[256]; 48517658Sjulian 48665557Sjasone#ifdef SMP 48782115Sjhb /* 48882115Sjhb * We don't want multiple CPU's to panic at the same time, so we 489101155Sjhb * use panic_cpu as a simple spinlock. We have to keep checking 490101155Sjhb * panic_cpu if we are spinning in case the panic on the first 49182115Sjhb * CPU is canceled. 49282115Sjhb */ 493101155Sjhb if (panic_cpu != PCPU_GET(cpuid)) 494101155Sjhb while (atomic_cmpset_int(&panic_cpu, NOCPU, 495101155Sjhb PCPU_GET(cpuid)) == 0) 496101155Sjhb while (panic_cpu != NOCPU) 497101155Sjhb ; /* nothing */ 49865557Sjasone#endif 49965557Sjasone 50017658Sjulian bootopt = RB_AUTOBOOT | RB_DUMP; 501103647Sjhb newpanic = 0; 50217658Sjulian if (panicstr) 50317658Sjulian bootopt |= RB_NOSYNC; 504103647Sjhb else { 50517658Sjulian panicstr = fmt; 506103647Sjhb newpanic = 1; 507103647Sjhb } 50817658Sjulian 50917658Sjulian va_start(ap, fmt); 510116398Siedowse if (newpanic) { 511116398Siedowse (void)vsnprintf(buf, sizeof(buf), fmt, ap); 51238874Sache panicstr = buf; 513130164Sphk printf("panic: %s\n", buf); 514116398Siedowse } else { 515116398Siedowse printf("panic: "); 516116398Siedowse vprintf(fmt, ap); 517130164Sphk printf("\n"); 518116398Siedowse } 51917658Sjulian va_end(ap); 52026100Sfsmp#ifdef SMP 521134089Sjhb printf("cpuid = %d\n", PCPU_GET(cpuid)); 52226100Sfsmp#endif 52317658Sjulian 524131927Smarcel#ifdef KDB 525103647Sjhb if (newpanic && trace_on_panic) 526131927Smarcel kdb_backtrace(); 52717658Sjulian if (debugger_on_panic) 528131927Smarcel kdb_enter("panic"); 52982223Sjhb#ifdef RESTARTABLE_PANICS 53082115Sjhb /* See if the user aborted the panic, in which case we continue. */ 53182115Sjhb if (panicstr == NULL) { 53282115Sjhb#ifdef SMP 533101155Sjhb atomic_store_rel_int(&panic_cpu, NOCPU); 53417658Sjulian#endif 53582115Sjhb return; 53682115Sjhb } 53782115Sjhb#endif 53882223Sjhb#endif 539113633Sjhb mtx_lock_spin(&sched_lock); 540100209Sgallatin td->td_flags |= TDF_INPANIC; 541113633Sjhb mtx_unlock_spin(&sched_lock); 54285202Speter if (!sync_on_panic) 54385202Speter bootopt |= RB_NOSYNC; 54417658Sjulian boot(bootopt); 54517658Sjulian} 54617658Sjulian 54717768Sjulian/* 54843436Smsmith * Support for poweroff delay. 54943436Smsmith */ 55054248Smsmith#ifndef POWEROFF_DELAY 55154248Smsmith# define POWEROFF_DELAY 5000 55254248Smsmith#endif 55354248Smsmithstatic int poweroff_delay = POWEROFF_DELAY; 55454248Smsmith 55543436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 55643436Smsmith &poweroff_delay, 0, ""); 55743436Smsmith 558110859Salfredstatic void 55950107Smsmithpoweroff_wait(void *junk, int howto) 56043436Smsmith{ 561110859Salfred 562110859Salfred if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 56343436Smsmith return; 56443436Smsmith DELAY(poweroff_delay * 1000); 56543436Smsmith} 56655539Sluoqi 56755539Sluoqi/* 56855539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate 56955539Sluoqi * points in their main loops prior to a system shutdown, so that they 57055539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf 57155539Sluoqi * to cause sync to fail). For each of these system processes, register 57255539Sluoqi * shutdown_kproc() as a handler for one of shutdown events. 57355539Sluoqi */ 57455539Sluoqistatic int kproc_shutdown_wait = 60; 57555539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 57655539Sluoqi &kproc_shutdown_wait, 0, ""); 57755539Sluoqi 57855539Sluoqivoid 57970063Sjhbkproc_shutdown(void *arg, int howto) 58055539Sluoqi{ 58155539Sluoqi struct proc *p; 582132177Salfred char procname[MAXCOMLEN + 1]; 58355539Sluoqi int error; 58455539Sluoqi 58555539Sluoqi if (panicstr) 58655539Sluoqi return; 58755539Sluoqi 58855539Sluoqi p = (struct proc *)arg; 589132177Salfred strlcpy(procname, p->p_comm, sizeof(procname)); 590132866Snjl printf("Waiting (max %d seconds) for system process `%s' to stop...", 591132177Salfred kproc_shutdown_wait, procname); 59270063Sjhb error = kthread_suspend(p, kproc_shutdown_wait * hz); 59355539Sluoqi 59455539Sluoqi if (error == EWOULDBLOCK) 595132866Snjl printf("timed out\n"); 59655539Sluoqi else 597132866Snjl printf("done\n"); 59855539Sluoqi} 59993496Sphk 60093496Sphk/* Registration of dumpers */ 60193496Sphkint 60293496Sphkset_dumper(struct dumperinfo *di) 60393496Sphk{ 604110859Salfred 60593496Sphk if (di == NULL) { 60693496Sphk bzero(&dumper, sizeof dumper); 60793496Sphk return (0); 60893496Sphk } 60993496Sphk if (dumper.dumper != NULL) 61093496Sphk return (EBUSY); 61193496Sphk dumper = *di; 61293496Sphk return (0); 61393496Sphk} 61493496Sphk 615105531Stmm#if defined(__powerpc__) 61693496Sphkvoid 61793496Sphkdumpsys(struct dumperinfo *di __unused) 61893496Sphk{ 61993496Sphk 62093496Sphk printf("Kernel dumps not implemented on this architecture\n"); 62193496Sphk} 62293496Sphk#endif 623