kern_shutdown.c revision 55539
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 55539 2000-01-07 08:36:44Z luoqi $
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>
5031275Sbde#include <sys/buf.h>
5117658Sjulian#include <sys/reboot.h>
5217658Sjulian#include <sys/proc.h>
5341137Smsmith#include <sys/vnode.h>
5417658Sjulian#include <sys/kernel.h>
5555539Sluoqi#include <sys/kthread.h>
5621776Sbde#include <sys/mount.h>
5739237Sgibbs#include <sys/queue.h>
5817658Sjulian#include <sys/sysctl.h>
5917658Sjulian#include <sys/conf.h>
6017658Sjulian#include <sys/sysproto.h>
6149558Sphk#include <sys/cons.h>
6217658Sjulian
6317658Sjulian#include <machine/pcb.h>
6417658Sjulian#include <machine/clock.h>
6517658Sjulian#include <machine/md_var.h>
6626812Speter#ifdef SMP
6726812Speter#include <machine/smp.h>		/* smp_active, cpuid */
6826812Speter#endif
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
11131275Sbdestatic void boot __P((int)) __dead2;
11231275Sbdestatic void dumpsys __P((void));
11348868Sphkstatic int setdumpdev __P((dev_t dev));
11450107Smsmithstatic void poweroff_wait __P((void *, int));
11554233Sphkstatic void print_uptime __P((void));
11650107Smsmithstatic void shutdown_halt __P((void *junk, int howto));
11750107Smsmithstatic void shutdown_panic __P((void *junk, int howto));
11850107Smsmithstatic void shutdown_reset __P((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
13830994Sphkreboot(p, uap)
13917658Sjulian	struct proc *p;
14017658Sjulian	struct reboot_args *uap;
14117658Sjulian{
14217658Sjulian	int error;
14317658Sjulian
14446112Sphk	if ((error = suser(p)))
14517658Sjulian		return (error);
14617658Sjulian
14717658Sjulian	boot(uap->opt);
14817658Sjulian	return (0);
14917658Sjulian}
15017658Sjulian
15117658Sjulian/*
15217658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
15317658Sjulian */
15417658Sjulianvoid
15528769Sbdeshutdown_nice()
15617658Sjulian{
15717658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
15817658Sjulian	if (initproc != NULL) {
15917658Sjulian		psignal(initproc, SIGINT);
16017658Sjulian	} else {
16117658Sjulian		/* No init(8) running, so simply reboot */
16217658Sjulian		boot(RB_NOSYNC);
16317658Sjulian	}
16417658Sjulian	return;
16517658Sjulian}
16617658Sjulianstatic int	waittime = -1;
16717658Sjulianstatic struct pcb dumppcb;
16817658Sjulian
16954233Sphkstatic void
17054233Sphkprint_uptime()
17154233Sphk{
17254233Sphk	int f;
17354233Sphk	struct timespec ts;
17454233Sphk
17554233Sphk	getnanouptime(&ts);
17654233Sphk	printf("Uptime: ");
17754233Sphk	f = 0;
17854233Sphk	if (ts.tv_sec >= 86400) {
17954233Sphk		printf("%ldd", ts.tv_sec / 86400);
18054233Sphk		ts.tv_sec %= 86400;
18154233Sphk		f = 1;
18254233Sphk	}
18354233Sphk	if (f || ts.tv_sec >= 3600) {
18454233Sphk		printf("%ldh", ts.tv_sec / 3600);
18554233Sphk		ts.tv_sec %= 3600;
18654233Sphk		f = 1;
18754233Sphk	}
18854233Sphk	if (f || ts.tv_sec >= 60) {
18954233Sphk		printf("%ldm", ts.tv_sec / 60);
19054233Sphk		ts.tv_sec %= 60;
19154233Sphk		f = 1;
19254233Sphk	}
19354233Sphk	printf("%lds\n", ts.tv_sec);
19454233Sphk}
19554233Sphk
19617658Sjulian/*
19717658Sjulian *  Go through the rigmarole of shutting down..
19817658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
19917658Sjulian * anything machine dependant in it.
20017658Sjulian */
20131275Sbdestatic void
20217658Sjulianboot(howto)
20317658Sjulian	int howto;
20417658Sjulian{
20517658Sjulian
20625164Speter#ifdef SMP
20725164Speter	if (smp_active) {
20826812Speter		printf("boot() called on cpu#%d\n", cpuid);
20925164Speter	}
21025164Speter#endif
21127997Sjulian	/*
21227997Sjulian	 * Do any callouts that should be done BEFORE syncing the filesystems.
21327997Sjulian	 */
21450107Smsmith	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
21527997Sjulian
21627997Sjulian	/*
21727997Sjulian	 * Now sync filesystems
21827997Sjulian	 */
21917658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
22017658Sjulian		register struct buf *bp;
22117658Sjulian		int iter, nbusy;
22217658Sjulian
22317658Sjulian		waittime = 0;
22417658Sjulian		printf("\nsyncing disks... ");
22517658Sjulian
22630994Sphk		sync(&proc0, NULL);
22717658Sjulian
22834266Sjulian		/*
22934266Sjulian		 * With soft updates, some buffers that are
23034266Sjulian		 * written will be remarked as dirty until other
23134266Sjulian		 * buffers are written.
23234266Sjulian		 */
23317658Sjulian		for (iter = 0; iter < 20; iter++) {
23417658Sjulian			nbusy = 0;
23517658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
23648225Smckusick				if ((bp->b_flags & B_INVAL) == 0 &&
23748225Smckusick				    BUF_REFCNT(bp) > 0) {
23817658Sjulian					nbusy++;
23934266Sjulian				} else if ((bp->b_flags & (B_DELWRI | B_INVAL))
24034266Sjulian						== B_DELWRI) {
24134266Sjulian					/* bawrite(bp);*/
24234266Sjulian					nbusy++;
24317658Sjulian				}
24417658Sjulian			}
24517658Sjulian			if (nbusy == 0)
24617658Sjulian				break;
24717658Sjulian			printf("%d ", nbusy);
24834266Sjulian			sync(&proc0, NULL);
24934266Sjulian			DELAY(50000 * iter);
25017658Sjulian		}
25153023Sphk		printf("\n");
25241137Smsmith		/*
25341137Smsmith		 * Count only busy local buffers to prevent forcing
25441137Smsmith		 * a fsck if we're just a client of a wedged NFS server
25541137Smsmith		 */
25641137Smsmith		nbusy = 0;
25741137Smsmith		for (bp = &buf[nbuf]; --bp >= buf; ) {
25848225Smckusick			if (((bp->b_flags&B_INVAL) == 0 && BUF_REFCNT(bp)) ||
25948225Smckusick			    ((bp->b_flags & (B_DELWRI|B_INVAL)) == B_DELWRI)) {
26053023Sphk				if (bp->b_dev == NODEV) {
26153452Sphk					TAILQ_REMOVE(&mountlist,
26248225Smckusick					    bp->b_vp->v_mount, mnt_list);
26353023Sphk					continue;
26453023Sphk				}
26553023Sphk				nbusy++;
26653023Sphk#if defined(SHOW_BUSYBUFS) || defined(DIAGNOSTIC)
26753023Sphk				printf(
26853023Sphk			    "%d: dev:%s, flags:%08lx, blkno:%ld, lblkno:%ld\n",
26953023Sphk				    nbusy, devtoname(bp->b_dev),
27053023Sphk				    bp->b_flags, (long)bp->b_blkno,
27153023Sphk				    (long)bp->b_lblkno);
27253023Sphk#endif
27346568Speter			}
27441137Smsmith		}
27517658Sjulian		if (nbusy) {
27617658Sjulian			/*
27717658Sjulian			 * Failed to sync all blocks. Indicate this and don't
27817658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
27917658Sjulian			 */
28053023Sphk			printf("giving up on %d buffers\n", nbusy);
28117658Sjulian			DELAY(5000000);	/* 5 seconds */
28217658Sjulian		} else {
28317658Sjulian			printf("done\n");
28417658Sjulian			/*
28517658Sjulian			 * Unmount filesystems
28617658Sjulian			 */
28717658Sjulian			if (panicstr == 0)
28817658Sjulian				vfs_unmountall();
28917658Sjulian		}
29039237Sgibbs		DELAY(100000);		/* wait for console output to finish */
29117658Sjulian	}
29227997Sjulian
29354233Sphk	print_uptime();
29454233Sphk
29527997Sjulian	/*
29627997Sjulian	 * Ok, now do things that assume all filesystem activity has
29727997Sjulian	 * been completed.
29827997Sjulian	 */
29950107Smsmith	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
30039237Sgibbs	splhigh();
30139522Sdt	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold) {
30239237Sgibbs		savectx(&dumppcb);
30339237Sgibbs#ifdef __i386__
30439237Sgibbs		dumppcb.pcb_cr3 = rcr3();
30539237Sgibbs#endif
30639237Sgibbs		dumpsys();
30717768Sjulian	}
30839237Sgibbs
30939237Sgibbs	/* Now that we're going to really halt the system... */
31050107Smsmith	EVENTHANDLER_INVOKE(shutdown_final, howto);
31139237Sgibbs
31250107Smsmith	for(;;) ;	/* safety against shutdown_reset not working */
31350107Smsmith	/* NOTREACHED */
31450107Smsmith}
31550107Smsmith
31650107Smsmith/*
31750107Smsmith * If the shutdown was a clean halt, behave accordingly.
31850107Smsmith */
31950107Smsmithstatic void
32050107Smsmithshutdown_halt(void *junk, int howto)
32150107Smsmith{
32217658Sjulian	if (howto & RB_HALT) {
32317658Sjulian		printf("\n");
32417658Sjulian		printf("The operating system has halted.\n");
32517658Sjulian		printf("Please press any key to reboot.\n\n");
32619274Sjulian		switch (cngetc()) {
32719274Sjulian		case -1:		/* No console, just die */
32819274Sjulian			cpu_halt();
32919274Sjulian			/* NOTREACHED */
33019274Sjulian		default:
33139237Sgibbs			howto &= ~RB_HALT;
33219274Sjulian			break;
33319274Sjulian		}
33450107Smsmith	}
33550107Smsmith}
33617658Sjulian
33750107Smsmith/*
33850107Smsmith * Check to see if the system paniced, pause and then reboot
33950107Smsmith * according to the specified delay.
34050107Smsmith */
34150107Smsmithstatic void
34250107Smsmithshutdown_panic(void *junk, int howto)
34350107Smsmith{
34450107Smsmith	int loop;
34550107Smsmith
34650107Smsmith	if (howto & RB_DUMP) {
34739237Sgibbs		if (PANIC_REBOOT_WAIT_TIME != 0) {
34839237Sgibbs			if (PANIC_REBOOT_WAIT_TIME != -1) {
34939237Sgibbs				printf("Automatic reboot in %d seconds - "
35039237Sgibbs				       "press a key on the console to abort\n",
35139237Sgibbs					PANIC_REBOOT_WAIT_TIME);
35239237Sgibbs				for (loop = PANIC_REBOOT_WAIT_TIME * 10;
35339237Sgibbs				     loop > 0; --loop) {
35439237Sgibbs					DELAY(1000 * 100); /* 1/10th second */
35539237Sgibbs					/* Did user type a key? */
35639237Sgibbs					if (cncheckc() != -1)
35739237Sgibbs						break;
35817658Sjulian				}
35939237Sgibbs				if (!loop)
36050107Smsmith					return;
36117658Sjulian			}
36239237Sgibbs		} else { /* zero time specified - reboot NOW */
36350107Smsmith			return;
36417658Sjulian		}
36539237Sgibbs		printf("--> Press a key on the console to reboot <--\n");
36639237Sgibbs		cngetc();
36717658Sjulian	}
36850107Smsmith}
36950107Smsmith
37050107Smsmith/*
37150107Smsmith * Everything done, now reset
37250107Smsmith */
37350107Smsmithstatic void
37450107Smsmithshutdown_reset(void *junk, int howto)
37550107Smsmith{
37617658Sjulian	printf("Rebooting...\n");
37717658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
37817677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
37917658Sjulian	cpu_reset();
38050107Smsmith	/* NOTREACHED */ /* assuming reset worked */
38117658Sjulian}
38217658Sjulian
38317658Sjulian/*
38417658Sjulian * Magic number for savecore
38517658Sjulian *
38617658Sjulian * exported (symorder) and used at least by savecore(8)
38717658Sjulian *
38817658Sjulian */
38917658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
39017658Sjulian
39117658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
39217658Sjulian
39317658Sjulianstatic int	dodump = 1;
39417658Sjulian
39548868SphkSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0,
39648868Sphk    "Try to perform coredump on kernel panic");
39748868Sphk
39848868Sphkstatic int
39948868Sphksetdumpdev(dev)
40048868Sphk	dev_t dev;
40148868Sphk{
40250571Sphk	int psize;
40348868Sphk	long newdumplo;
40448868Sphk
40548868Sphk	if (dev == NODEV) {
40648868Sphk		dumpdev = dev;
40748868Sphk		return (0);
40848868Sphk	}
40949679Sphk	if (devsw(dev) == NULL)
41048868Sphk		return (ENXIO);		/* XXX is this right? */
41149679Sphk	if (devsw(dev)->d_psize == NULL)
41248868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
41349679Sphk	psize = devsw(dev)->d_psize(dev);
41448868Sphk	if (psize == -1)
41548868Sphk		return (ENXIO);		/* XXX should be ENODEV ? */
41648868Sphk	/*
41750571Sphk	 * XXX should clean up checking in dumpsys() to be more like this.
41848868Sphk	 */
41948868Sphk	newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE;
42048868Sphk	if (newdumplo < 0)
42148868Sphk		return (ENOSPC);
42248868Sphk	dumpdev = dev;
42348868Sphk	dumplo = newdumplo;
42448868Sphk	return (0);
42548868Sphk}
42648868Sphk
42748868Sphk
42831403Sjulian/* ARGSUSED */
42931403Sjulianstatic void dump_conf __P((void *dummy));
43031403Sjulianstatic void
43131403Sjuliandump_conf(dummy)
43231403Sjulian	void *dummy;
43331403Sjulian{
43448868Sphk	if (setdumpdev(dumpdev) != 0)
43548868Sphk		dumpdev = NODEV;
43631403Sjulian}
43748868Sphk
43831403SjulianSYSINIT(dump_conf, SI_SUB_DUMP_CONF, SI_ORDER_FIRST, dump_conf, NULL)
43931403Sjulian
44048868Sphkstatic int
44148868Sphksysctl_kern_dumpdev SYSCTL_HANDLER_ARGS
44248868Sphk{
44348868Sphk	int error;
44448868Sphk	udev_t ndumpdev;
44548868Sphk
44653838Sphk	ndumpdev = dev2udev(dumpdev);
44748868Sphk	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
44848868Sphk	if (error == 0 && req->newptr != NULL)
44953838Sphk		error = setdumpdev(udev2dev(ndumpdev, 0));
45048868Sphk	return (error);
45148868Sphk}
45248868Sphk
45348868SphkSYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
45448868Sphk	0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");
45548868Sphk
45617658Sjulian/*
45717658Sjulian * Doadump comes here after turning off memory management and
45817658Sjulian * getting on the dump stack, either when called above, or by
45917658Sjulian * the auto-restart code.
46017658Sjulian */
46117658Sjulianstatic void
46217658Sjuliandumpsys(void)
46317658Sjulian{
46449627Salfred	int	error;
46550571Sphk	static int dumping;
46617658Sjulian
46750571Sphk	if (dumping++) {
46850571Sphk		printf("Dump already in progress, bailing...\n");
46950571Sphk		return;
47050571Sphk	}
47117658Sjulian	if (!dodump)
47217658Sjulian		return;
47317658Sjulian	if (dumpdev == NODEV)
47417658Sjulian		return;
47549679Sphk	if (!(devsw(dumpdev)))
47617658Sjulian		return;
47749679Sphk	if (!(devsw(dumpdev)->d_dump))
47817658Sjulian		return;
47917658Sjulian	dumpsize = Maxmem;
48050571Sphk	printf("\ndumping to dev %s, offset %ld\n", devtoname(dumpdev), dumplo);
48117658Sjulian	printf("dump ");
48249679Sphk	error = (*devsw(dumpdev)->d_dump)(dumpdev);
48349627Salfred	if (error == 0) {
48449627Salfred		printf("succeeded\n");
48549627Salfred		return;
48649627Salfred	}
48749627Salfred	printf("failed, reason: ");
48849627Salfred	switch (error) {
48949627Salfred	case ENODEV:
49049627Salfred		printf("device doesn't support a dump routine\n");
49149627Salfred		break;
49217658Sjulian
49317658Sjulian	case ENXIO:
49417658Sjulian		printf("device bad\n");
49517658Sjulian		break;
49617658Sjulian
49717658Sjulian	case EFAULT:
49817658Sjulian		printf("device not ready\n");
49917658Sjulian		break;
50017658Sjulian
50117658Sjulian	case EINVAL:
50217658Sjulian		printf("area improper\n");
50317658Sjulian		break;
50417658Sjulian
50517658Sjulian	case EIO:
50617658Sjulian		printf("i/o error\n");
50717658Sjulian		break;
50817658Sjulian
50917658Sjulian	case EINTR:
51017658Sjulian		printf("aborted from console\n");
51117658Sjulian		break;
51217658Sjulian
51317658Sjulian	default:
51449627Salfred		printf("unknown, error = %d\n", error);
51517658Sjulian		break;
51617658Sjulian	}
51717658Sjulian}
51817658Sjulian
51917658Sjulian/*
52017658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
52117658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
52217658Sjulian * the disks as this often leads to recursive panics.
52317658Sjulian */
52417658Sjulianvoid
52517658Sjulianpanic(const char *fmt, ...)
52617658Sjulian{
52717658Sjulian	int bootopt;
52817658Sjulian	va_list ap;
52938874Sache	static char buf[256];
53017658Sjulian
53117658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
53217658Sjulian	if (panicstr)
53317658Sjulian		bootopt |= RB_NOSYNC;
53417658Sjulian	else
53517658Sjulian		panicstr = fmt;
53617658Sjulian
53717658Sjulian	va_start(ap, fmt);
53841514Sarchie	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
53938874Sache	if (panicstr == fmt)
54038874Sache		panicstr = buf;
54117658Sjulian	va_end(ap);
54238874Sache	printf("panic: %s\n", buf);
54326100Sfsmp#ifdef SMP
54429128Speter	/* three seperate prints in case of an unmapped page and trap */
54529128Speter	printf("mp_lock = %08x; ", mp_lock);
54629128Speter	printf("cpuid = %d; ", cpuid);
54729128Speter	printf("lapic.id = %08x\n", lapic.id);
54826100Sfsmp#endif
54917658Sjulian
55017658Sjulian#if defined(DDB)
55117658Sjulian	if (debugger_on_panic)
55217658Sjulian		Debugger ("panic");
55317658Sjulian#endif
55417658Sjulian	boot(bootopt);
55517658Sjulian}
55617658Sjulian
55717768Sjulian/*
55843436Smsmith * Support for poweroff delay.
55943436Smsmith */
56054248Smsmith#ifndef POWEROFF_DELAY
56154248Smsmith# define POWEROFF_DELAY 5000
56254248Smsmith#endif
56354248Smsmithstatic int poweroff_delay = POWEROFF_DELAY;
56454248Smsmith
56543436SmsmithSYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
56643436Smsmith	&poweroff_delay, 0, "");
56743436Smsmith
56850107Smsmithstatic void
56950107Smsmithpoweroff_wait(void *junk, int howto)
57043436Smsmith{
57143436Smsmith	if(!(howto & RB_POWEROFF) || poweroff_delay <= 0)
57243436Smsmith		return;
57343436Smsmith	DELAY(poweroff_delay * 1000);
57443436Smsmith}
57555539Sluoqi
57655539Sluoqi/*
57755539Sluoqi * Some system processes (e.g. syncer) need to be stopped at appropriate
57855539Sluoqi * points in their main loops prior to a system shutdown, so that they
57955539Sluoqi * won't interfere with the shutdown process (e.g. by holding a disk buf
58055539Sluoqi * to cause sync to fail).  For each of these system processes, register
58155539Sluoqi * shutdown_kproc() as a handler for one of shutdown events.
58255539Sluoqi */
58355539Sluoqistatic int kproc_shutdown_wait = 60;
58455539SluoqiSYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
58555539Sluoqi    &kproc_shutdown_wait, 0, "");
58655539Sluoqi
58755539Sluoqivoid
58855539Sluoqishutdown_kproc(void *arg, int howto)
58955539Sluoqi{
59055539Sluoqi	struct proc *p;
59155539Sluoqi	int error;
59255539Sluoqi
59355539Sluoqi	if (panicstr)
59455539Sluoqi		return;
59555539Sluoqi
59655539Sluoqi	p = (struct proc *)arg;
59755539Sluoqi	printf("Waiting (max %d seconds) for system process `%s' to stop...",
59855539Sluoqi	    kproc_shutdown_wait * hz, p->p_comm);
59955539Sluoqi	error = suspend_kproc(p, kproc_shutdown_wait);
60055539Sluoqi
60155539Sluoqi	if (error == EWOULDBLOCK)
60255539Sluoqi		printf("timed out\n");
60355539Sluoqi	else
60455539Sluoqi		printf("stopped\n");
60555539Sluoqi}
606