kern_shutdown.c revision 17834
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
3917834Sjulian * $Id: kern_shutdown.c,v 1.3 1996/08/22 03:50:20 julian Exp $
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>
5517658Sjulian#include <sys/sysctl.h>
5617658Sjulian#include <sys/devconf.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 */
16317658Sjulian__dead void
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		dev_shutdownall(FALSE);
22217658Sjulian	}
22317768Sjulian	ep = shutdown_list2;
22417768Sjulian	while (ep) {
22517768Sjulian		shutdown_list2 = ep->next;
22617768Sjulian		(*ep->function)(howto, ep->arg);
22717768Sjulian		ep = ep->next;
22817768Sjulian	}
22917658Sjulian	splhigh();
23017658Sjulian	if (howto & RB_HALT) {
23117658Sjulian		printf("\n");
23217658Sjulian		printf("The operating system has halted.\n");
23317658Sjulian		printf("Please press any key to reboot.\n\n");
23417658Sjulian		cngetc();
23517658Sjulian	} else {
23617658Sjulian		if (howto & RB_DUMP) {
23717658Sjulian			if (!cold) {
23817658Sjulian				savectx(&dumppcb);
23917658Sjulian				dumppcb.pcb_cr3 = rcr3();
24017658Sjulian				dumpsys();
24117658Sjulian			}
24217658Sjulian
24317658Sjulian			if (PANIC_REBOOT_WAIT_TIME != 0) {
24417658Sjulian				if (PANIC_REBOOT_WAIT_TIME != -1) {
24517658Sjulian					int loop;
24617658Sjulian					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
24717658Sjulian						PANIC_REBOOT_WAIT_TIME);
24817658Sjulian					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
24917658Sjulian						DELAY(1000 * 100); /* 1/10th second */
25017658Sjulian						if (cncheckc()) /* Did user type a key? */
25117658Sjulian							break;
25217658Sjulian					}
25317658Sjulian					if (!loop)
25417658Sjulian						goto die;
25517658Sjulian				}
25617658Sjulian			} else { /* zero time specified - reboot NOW */
25717658Sjulian				goto die;
25817658Sjulian			}
25917658Sjulian			printf("--> Press a key on the console to reboot <--\n");
26017658Sjulian			cngetc();
26117658Sjulian		}
26217658Sjulian	}
26317658Sjuliandie:
26417658Sjulian	printf("Rebooting...\n");
26517658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
26617677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
26717658Sjulian	cpu_reset();
26817658Sjulian	for(;;) ;
26917658Sjulian	/* NOTREACHED */
27017658Sjulian}
27117658Sjulian
27217658Sjulian/*
27317658Sjulian * Magic number for savecore
27417658Sjulian *
27517658Sjulian * exported (symorder) and used at least by savecore(8)
27617658Sjulian *
27717658Sjulian */
27817658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
27917658Sjulian
28017658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
28117658Sjulian
28217658Sjulianstatic int	dodump = 1;
28317658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
28417658Sjulian
28517658Sjulian/*
28617658Sjulian * Doadump comes here after turning off memory management and
28717658Sjulian * getting on the dump stack, either when called above, or by
28817658Sjulian * the auto-restart code.
28917658Sjulian */
29017658Sjulianstatic void
29117658Sjuliandumpsys(void)
29217658Sjulian{
29317658Sjulian
29417658Sjulian	if (!dodump)
29517658Sjulian		return;
29617658Sjulian	if (dumpdev == NODEV)
29717658Sjulian		return;
29817658Sjulian	if ((minor(dumpdev)&07) != 1)
29917658Sjulian		return;
30017658Sjulian	if (!(bdevsw[major(dumpdev)]))
30117658Sjulian		return;
30217658Sjulian	if (!(bdevsw[major(dumpdev)]->d_dump))
30317658Sjulian		return;
30417658Sjulian	dumpsize = Maxmem;
30517658Sjulian	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
30617658Sjulian	printf("dump ");
30717658Sjulian	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
30817658Sjulian
30917658Sjulian	case ENXIO:
31017658Sjulian		printf("device bad\n");
31117658Sjulian		break;
31217658Sjulian
31317658Sjulian	case EFAULT:
31417658Sjulian		printf("device not ready\n");
31517658Sjulian		break;
31617658Sjulian
31717658Sjulian	case EINVAL:
31817658Sjulian		printf("area improper\n");
31917658Sjulian		break;
32017658Sjulian
32117658Sjulian	case EIO:
32217658Sjulian		printf("i/o error\n");
32317658Sjulian		break;
32417658Sjulian
32517658Sjulian	case EINTR:
32617658Sjulian		printf("aborted from console\n");
32717658Sjulian		break;
32817658Sjulian
32917658Sjulian	default:
33017658Sjulian		printf("succeeded\n");
33117658Sjulian		break;
33217658Sjulian	}
33317658Sjulian}
33417658Sjulian
33517658Sjulian/*
33617658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
33717658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
33817658Sjulian * the disks as this often leads to recursive panics.
33917658Sjulian */
34017658Sjulian#ifdef __GNUC__
34117658Sjulian__dead			/* panic() does not return */
34217658Sjulian#endif
34317658Sjulianvoid
34417658Sjulianpanic(const char *fmt, ...)
34517658Sjulian{
34617658Sjulian	int bootopt;
34717658Sjulian	va_list ap;
34817658Sjulian
34917658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
35017658Sjulian	if (panicstr)
35117658Sjulian		bootopt |= RB_NOSYNC;
35217658Sjulian	else
35317658Sjulian		panicstr = fmt;
35417658Sjulian
35517658Sjulian	printf("panic: ");
35617658Sjulian	va_start(ap, fmt);
35717658Sjulian	vprintf(fmt, ap);
35817658Sjulian	va_end(ap);
35917658Sjulian	printf("\n");
36017658Sjulian
36117658Sjulian#if defined(DDB)
36217658Sjulian	if (debugger_on_panic)
36317658Sjulian		Debugger ("panic");
36417658Sjulian#endif
36517658Sjulian	boot(bootopt);
36617658Sjulian}
36717658Sjulian
36817768Sjulian/*
36917768Sjulian * Two routines to handle adding/deleting items on the
37017768Sjulian * shutdown callout lists
37117768Sjulian *
37217768Sjulian * at_shutdown():
37317658Sjulian * Take the arguments given and put them onto the shutdown callout list.
37417658Sjulian * However first make sure that it's not already there.
37517658Sjulian * returns 0 on success.
37617658Sjulian */
37717658Sjulianint
37817768Sjulianat_shutdown(bootlist_fn function, void *arg, int position)
37917658Sjulian{
38017768Sjulian	sle_p ep, *epp;
38117768Sjulian
38217768Sjulian	switch(position) {
38317768Sjulian	case SHUTDOWN_PRE_SYNC:
38417768Sjulian		epp = &shutdown_list1;
38517768Sjulian		break;
38617768Sjulian	case SHUTDOWN_POST_SYNC:
38717768Sjulian		epp = &shutdown_list2;
38817768Sjulian		break;
38917768Sjulian	default:
39017768Sjulian		printf("bad exit callout list specified\n");
39117768Sjulian		return (EINVAL);
39217768Sjulian	}
39317768Sjulian	if (rm_at_shutdown(function, arg))
39417658Sjulian		printf("exit callout entry already present\n");
39517768Sjulian	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
39617768Sjulian	if (ep == NULL)
39717768Sjulian		return (ENOMEM);
39817768Sjulian	ep->next = *epp;
39917658Sjulian	ep->function = function;
40017658Sjulian	ep->arg = arg;
40117768Sjulian	*epp = ep;
40217768Sjulian	return (0);
40317658Sjulian}
40417768Sjulian
40517658Sjulian/*
40617768Sjulian * Scan the exit callout lists for the given items and remove them.
40717658Sjulian * Returns the number of items removed.
40817658Sjulian */
40917658Sjulianint
41017658Sjulianrm_at_shutdown(bootlist_fn function, void *arg)
41117658Sjulian{
41217768Sjulian	sle_p *epp, ep;
41317768Sjulian	int count;
41417658Sjulian
41517768Sjulian	count = 0;
41617768Sjulian	epp = &shutdown_list1;
41717658Sjulian	ep = *epp;
41817768Sjulian	while (ep) {
41917834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
42017658Sjulian			*epp = ep->next;
42117768Sjulian			free(ep, M_TEMP);
42217658Sjulian			count++;
42317658Sjulian		} else {
42417658Sjulian			epp = &ep->next;
42517658Sjulian		}
42617658Sjulian		ep = *epp;
42717658Sjulian	}
42817768Sjulian	epp = &shutdown_list2;
42917768Sjulian	ep = *epp;
43017768Sjulian	while (ep) {
43117834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
43217768Sjulian			*epp = ep->next;
43317768Sjulian			free(ep, M_TEMP);
43417768Sjulian			count++;
43517768Sjulian		} else {
43617768Sjulian			epp = &ep->next;
43717768Sjulian		}
43817768Sjulian		ep = *epp;
43917768Sjulian	}
44017768Sjulian	return (count);
44117658Sjulian}
44217658Sjulian
443