kern_shutdown.c revision 21776
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
3921673Sjkh * $FreeBSD: head/sys/kern/kern_shutdown.c 21776 1997-01-16 15:58:32Z bde $
4017658Sjulian */
4117658Sjulian
4217658Sjulian#include "opt_ddb.h"
4317658Sjulian
4417658Sjulian#include <sys/param.h>
4517658Sjulian#include <sys/systm.h>
4617658Sjulian#include <sys/reboot.h>
4717658Sjulian#include <sys/msgbuf.h>
4817658Sjulian#include <sys/proc.h>
4917658Sjulian#include <sys/vnode.h>
5017658Sjulian#include <sys/tty.h>
5117658Sjulian#include <sys/tprintf.h>
5217658Sjulian#include <sys/syslog.h>
5317658Sjulian#include <sys/malloc.h>
5417658Sjulian#include <sys/kernel.h>
5521776Sbde#include <sys/mount.h>
5617658Sjulian#include <sys/sysctl.h>
5717658Sjulian#include <sys/conf.h>
5817658Sjulian#include <sys/sysproto.h>
5917658Sjulian
6017658Sjulian#include <machine/pcb.h>
6117658Sjulian#include <machine/clock.h>
6217658Sjulian#include <machine/cons.h>
6317658Sjulian#include <machine/md_var.h>
6417658Sjulian
6517658Sjulian#include <sys/utsname.h>
6617658Sjulian#include <sys/signalvar.h>
6717658Sjulian
6817658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
6917658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7017658Sjulian#endif
7117658Sjulian
7217658Sjulian/*
7317658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7417658Sjulian * ANSI and traditional C compilers.
7517658Sjulian */
7617658Sjulian#include <machine/stdarg.h>
7717658Sjulian
7817658Sjulian#if defined(DDB)
7917658Sjulian#ifdef DDB_UNATTENDED
8017658Sjulian	static int debugger_on_panic = 0;
8117658Sjulian#else
8217658Sjulian	static int debugger_on_panic = 1;
8317658Sjulian#endif
8417658Sjulian
8517658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
8617658Sjulian	&debugger_on_panic, 0, "");
8717658Sjulian#endif
8817658Sjulian
8917658Sjulian
9017658Sjulian/*
9117658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
9217658Sjulian * to indicate that the kernel has already called panic.
9317658Sjulian */
9417658Sjulianconst char *panicstr;
9517658Sjulian
9617658Sjulian/*
9717658Sjulian * callout list for things to do a shutdown
9817658Sjulian */
9917658Sjuliantypedef struct shutdown_list_element {
10017658Sjulian	struct shutdown_list_element *next;
10117658Sjulian	bootlist_fn function;
10217658Sjulian	void *arg;
10317658Sjulian} *sle_p;
10417658Sjulian
10517768Sjulian/*
10617768Sjulian * there are two shutdown lists. Some things need to be shut down
10717768Sjulian * Earlier than others.
10817768Sjulian */
10917768Sjulianstatic sle_p shutdown_list1;
11017768Sjulianstatic sle_p shutdown_list2;
11117658Sjulian
11217658Sjulian
11317658Sjulianstatic void dumpsys(void);
11417658Sjulian
11517658Sjulian#ifndef _SYS_SYSPROTO_H_
11617658Sjulianstruct reboot_args {
11717658Sjulian	int	opt;
11817658Sjulian};
11917658Sjulian#endif
12017658Sjulian/* ARGSUSED */
12117658Sjulian
12217658Sjulian/*
12317658Sjulian * The system call that results in a reboot
12417658Sjulian */
12517658Sjulianint
12617658Sjulianreboot(p, uap, retval)
12717658Sjulian	struct proc *p;
12817658Sjulian	struct reboot_args *uap;
12917658Sjulian	int *retval;
13017658Sjulian{
13117658Sjulian	int error;
13217658Sjulian
13317658Sjulian	if ((error = suser(p->p_ucred, &p->p_acflag)))
13417658Sjulian		return (error);
13517658Sjulian
13617658Sjulian	boot(uap->opt);
13717658Sjulian	return (0);
13817658Sjulian}
13917658Sjulian
14017658Sjulian/*
14117658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
14217658Sjulian */
14317658Sjulianvoid
14417658Sjulianshutdown_nice(void)
14517658Sjulian{
14617658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
14717658Sjulian	if (initproc != NULL) {
14817658Sjulian		psignal(initproc, SIGINT);
14917658Sjulian	} else {
15017658Sjulian		/* No init(8) running, so simply reboot */
15117658Sjulian		boot(RB_NOSYNC);
15217658Sjulian	}
15317658Sjulian	return;
15417658Sjulian}
15517658Sjulianstatic int	waittime = -1;
15617658Sjulianstatic struct pcb dumppcb;
15717658Sjulian
15817658Sjulian/*
15917658Sjulian *  Go through the rigmarole of shutting down..
16017658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
16117658Sjulian * anything machine dependant in it.
16217658Sjulian */
16318277Sbdevoid
16417658Sjulianboot(howto)
16517658Sjulian	int howto;
16617658Sjulian{
16717768Sjulian	sle_p ep;
16817658Sjulian
16917768Sjulian	ep = shutdown_list1;
17017768Sjulian	while (ep) {
17117768Sjulian		shutdown_list1 = ep->next;
17217658Sjulian		(*ep->function)(howto, ep->arg);
17317658Sjulian		ep = ep->next;
17417658Sjulian	}
17517658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
17617658Sjulian		register struct buf *bp;
17717658Sjulian		int iter, nbusy;
17817658Sjulian
17917658Sjulian		waittime = 0;
18017658Sjulian		printf("\nsyncing disks... ");
18117658Sjulian
18217658Sjulian		sync(&proc0, NULL, NULL);
18317658Sjulian
18417658Sjulian		for (iter = 0; iter < 20; iter++) {
18517658Sjulian			nbusy = 0;
18617658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
18717658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
18817658Sjulian					nbusy++;
18917658Sjulian				}
19017658Sjulian			}
19117658Sjulian			if (nbusy == 0)
19217658Sjulian				break;
19317658Sjulian			printf("%d ", nbusy);
19417658Sjulian			DELAY(40000 * iter);
19517658Sjulian		}
19617658Sjulian		if (nbusy) {
19717658Sjulian			/*
19817658Sjulian			 * Failed to sync all blocks. Indicate this and don't
19917658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
20017658Sjulian			 */
20117658Sjulian			printf("giving up\n");
20217658Sjulian#ifdef SHOW_BUSYBUFS
20317658Sjulian			nbusy = 0;
20417658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
20517658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
20617658Sjulian					nbusy++;
20717658Sjulian					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
20817658Sjulian				}
20917658Sjulian			}
21017658Sjulian			DELAY(5000000);	/* 5 seconds */
21117658Sjulian#endif
21217658Sjulian		} else {
21317658Sjulian			printf("done\n");
21417658Sjulian			/*
21517658Sjulian			 * Unmount filesystems
21617658Sjulian			 */
21717658Sjulian			if (panicstr == 0)
21817658Sjulian				vfs_unmountall();
21917658Sjulian		}
22017658Sjulian		DELAY(100000);			/* wait for console output to finish */
22117658Sjulian	}
22217768Sjulian	ep = shutdown_list2;
22317768Sjulian	while (ep) {
22417768Sjulian		shutdown_list2 = ep->next;
22517768Sjulian		(*ep->function)(howto, ep->arg);
22617768Sjulian		ep = ep->next;
22717768Sjulian	}
22817658Sjulian	splhigh();
22917658Sjulian	if (howto & RB_HALT) {
23017658Sjulian		printf("\n");
23117658Sjulian		printf("The operating system has halted.\n");
23217658Sjulian		printf("Please press any key to reboot.\n\n");
23319274Sjulian		switch (cngetc()) {
23419274Sjulian		case -1:		/* No console, just die */
23519274Sjulian			cpu_halt();
23619274Sjulian			/* NOTREACHED */
23719274Sjulian		default:
23819274Sjulian			break;
23919274Sjulian		}
24017658Sjulian	} else {
24117658Sjulian		if (howto & RB_DUMP) {
24217658Sjulian			if (!cold) {
24317658Sjulian				savectx(&dumppcb);
24417658Sjulian				dumppcb.pcb_cr3 = rcr3();
24517658Sjulian				dumpsys();
24617658Sjulian			}
24717658Sjulian
24817658Sjulian			if (PANIC_REBOOT_WAIT_TIME != 0) {
24917658Sjulian				if (PANIC_REBOOT_WAIT_TIME != -1) {
25017658Sjulian					int loop;
25117658Sjulian					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
25217658Sjulian						PANIC_REBOOT_WAIT_TIME);
25317658Sjulian					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
25417658Sjulian						DELAY(1000 * 100); /* 1/10th second */
25518290Sbde						/* Did user type a key? */
25618290Sbde						if (cncheckc() != -1)
25717658Sjulian							break;
25817658Sjulian					}
25917658Sjulian					if (!loop)
26017658Sjulian						goto die;
26117658Sjulian				}
26217658Sjulian			} else { /* zero time specified - reboot NOW */
26317658Sjulian				goto die;
26417658Sjulian			}
26517658Sjulian			printf("--> Press a key on the console to reboot <--\n");
26617658Sjulian			cngetc();
26717658Sjulian		}
26817658Sjulian	}
26917658Sjuliandie:
27017658Sjulian	printf("Rebooting...\n");
27117658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
27217677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
27317658Sjulian	cpu_reset();
27417658Sjulian	for(;;) ;
27517658Sjulian	/* NOTREACHED */
27617658Sjulian}
27717658Sjulian
27817658Sjulian/*
27917658Sjulian * Magic number for savecore
28017658Sjulian *
28117658Sjulian * exported (symorder) and used at least by savecore(8)
28217658Sjulian *
28317658Sjulian */
28417658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
28517658Sjulian
28617658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
28717658Sjulian
28817658Sjulianstatic int	dodump = 1;
28917658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
29017658Sjulian
29117658Sjulian/*
29217658Sjulian * Doadump comes here after turning off memory management and
29317658Sjulian * getting on the dump stack, either when called above, or by
29417658Sjulian * the auto-restart code.
29517658Sjulian */
29617658Sjulianstatic void
29717658Sjuliandumpsys(void)
29817658Sjulian{
29917658Sjulian
30017658Sjulian	if (!dodump)
30117658Sjulian		return;
30217658Sjulian	if (dumpdev == NODEV)
30317658Sjulian		return;
30417658Sjulian	if ((minor(dumpdev)&07) != 1)
30517658Sjulian		return;
30617658Sjulian	if (!(bdevsw[major(dumpdev)]))
30717658Sjulian		return;
30817658Sjulian	if (!(bdevsw[major(dumpdev)]->d_dump))
30917658Sjulian		return;
31017658Sjulian	dumpsize = Maxmem;
31117658Sjulian	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
31217658Sjulian	printf("dump ");
31317658Sjulian	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
31417658Sjulian
31517658Sjulian	case ENXIO:
31617658Sjulian		printf("device bad\n");
31717658Sjulian		break;
31817658Sjulian
31917658Sjulian	case EFAULT:
32017658Sjulian		printf("device not ready\n");
32117658Sjulian		break;
32217658Sjulian
32317658Sjulian	case EINVAL:
32417658Sjulian		printf("area improper\n");
32517658Sjulian		break;
32617658Sjulian
32717658Sjulian	case EIO:
32817658Sjulian		printf("i/o error\n");
32917658Sjulian		break;
33017658Sjulian
33117658Sjulian	case EINTR:
33217658Sjulian		printf("aborted from console\n");
33317658Sjulian		break;
33417658Sjulian
33517658Sjulian	default:
33617658Sjulian		printf("succeeded\n");
33717658Sjulian		break;
33817658Sjulian	}
33917658Sjulian}
34017658Sjulian
34117658Sjulian/*
34217658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
34317658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
34417658Sjulian * the disks as this often leads to recursive panics.
34517658Sjulian */
34617658Sjulianvoid
34717658Sjulianpanic(const char *fmt, ...)
34817658Sjulian{
34917658Sjulian	int bootopt;
35017658Sjulian	va_list ap;
35117658Sjulian
35217658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
35317658Sjulian	if (panicstr)
35417658Sjulian		bootopt |= RB_NOSYNC;
35517658Sjulian	else
35617658Sjulian		panicstr = fmt;
35717658Sjulian
35817658Sjulian	printf("panic: ");
35917658Sjulian	va_start(ap, fmt);
36017658Sjulian	vprintf(fmt, ap);
36117658Sjulian	va_end(ap);
36217658Sjulian	printf("\n");
36317658Sjulian
36417658Sjulian#if defined(DDB)
36517658Sjulian	if (debugger_on_panic)
36617658Sjulian		Debugger ("panic");
36717658Sjulian#endif
36817658Sjulian	boot(bootopt);
36917658Sjulian}
37017658Sjulian
37117768Sjulian/*
37217768Sjulian * Two routines to handle adding/deleting items on the
37317768Sjulian * shutdown callout lists
37417768Sjulian *
37517768Sjulian * at_shutdown():
37617658Sjulian * Take the arguments given and put them onto the shutdown callout list.
37717658Sjulian * However first make sure that it's not already there.
37817658Sjulian * returns 0 on success.
37917658Sjulian */
38017658Sjulianint
38117768Sjulianat_shutdown(bootlist_fn function, void *arg, int position)
38217658Sjulian{
38317768Sjulian	sle_p ep, *epp;
38417768Sjulian
38517768Sjulian	switch(position) {
38617768Sjulian	case SHUTDOWN_PRE_SYNC:
38717768Sjulian		epp = &shutdown_list1;
38817768Sjulian		break;
38917768Sjulian	case SHUTDOWN_POST_SYNC:
39017768Sjulian		epp = &shutdown_list2;
39117768Sjulian		break;
39217768Sjulian	default:
39317768Sjulian		printf("bad exit callout list specified\n");
39417768Sjulian		return (EINVAL);
39517768Sjulian	}
39617768Sjulian	if (rm_at_shutdown(function, arg))
39717658Sjulian		printf("exit callout entry already present\n");
39817768Sjulian	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
39917768Sjulian	if (ep == NULL)
40017768Sjulian		return (ENOMEM);
40117768Sjulian	ep->next = *epp;
40217658Sjulian	ep->function = function;
40317658Sjulian	ep->arg = arg;
40417768Sjulian	*epp = ep;
40517768Sjulian	return (0);
40617658Sjulian}
40717768Sjulian
40817658Sjulian/*
40917768Sjulian * Scan the exit callout lists for the given items and remove them.
41017658Sjulian * Returns the number of items removed.
41117658Sjulian */
41217658Sjulianint
41317658Sjulianrm_at_shutdown(bootlist_fn function, void *arg)
41417658Sjulian{
41517768Sjulian	sle_p *epp, ep;
41617768Sjulian	int count;
41717658Sjulian
41817768Sjulian	count = 0;
41917768Sjulian	epp = &shutdown_list1;
42017658Sjulian	ep = *epp;
42117768Sjulian	while (ep) {
42217834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
42317658Sjulian			*epp = ep->next;
42417768Sjulian			free(ep, M_TEMP);
42517658Sjulian			count++;
42617658Sjulian		} else {
42717658Sjulian			epp = &ep->next;
42817658Sjulian		}
42917658Sjulian		ep = *epp;
43017658Sjulian	}
43117768Sjulian	epp = &shutdown_list2;
43217768Sjulian	ep = *epp;
43317768Sjulian	while (ep) {
43417834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
43517768Sjulian			*epp = ep->next;
43617768Sjulian			free(ep, M_TEMP);
43717768Sjulian			count++;
43817768Sjulian		} else {
43917768Sjulian			epp = &ep->next;
44017768Sjulian		}
44117768Sjulian		ep = *epp;
44217768Sjulian	}
44317768Sjulian	return (count);
44417658Sjulian}
44517658Sjulian
446