kern_shutdown.c revision 18277
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
3918277Sbde * $Id: kern_shutdown.c,v 1.6 1996/09/07 19:13:09 sos 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/conf.h>
5717658Sjulian#include <sys/sysproto.h>
5817658Sjulian
5917658Sjulian#include <machine/pcb.h>
6017658Sjulian#include <machine/clock.h>
6117658Sjulian#include <machine/cons.h>
6217658Sjulian#include <machine/md_var.h>
6317658Sjulian
6417658Sjulian#include <sys/utsname.h>
6517658Sjulian#include <sys/signalvar.h>
6617658Sjulian
6717658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
6817658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
6917658Sjulian#endif
7017658Sjulian
7117658Sjulian/*
7217658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7317658Sjulian * ANSI and traditional C compilers.
7417658Sjulian */
7517658Sjulian#include <machine/stdarg.h>
7617658Sjulian
7717658Sjulian#if defined(DDB)
7817658Sjulian#ifdef DDB_UNATTENDED
7917658Sjulian	static int debugger_on_panic = 0;
8017658Sjulian#else
8117658Sjulian	static int debugger_on_panic = 1;
8217658Sjulian#endif
8317658Sjulian
8417658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
8517658Sjulian	&debugger_on_panic, 0, "");
8617658Sjulian#endif
8717658Sjulian
8817658Sjulian
8917658Sjulian/*
9017658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
9117658Sjulian * to indicate that the kernel has already called panic.
9217658Sjulian */
9317658Sjulianconst char *panicstr;
9417658Sjulian
9517658Sjulian/*
9617658Sjulian * callout list for things to do a shutdown
9717658Sjulian */
9817658Sjuliantypedef struct shutdown_list_element {
9917658Sjulian	struct shutdown_list_element *next;
10017658Sjulian	bootlist_fn function;
10117658Sjulian	void *arg;
10217658Sjulian} *sle_p;
10317658Sjulian
10417768Sjulian/*
10517768Sjulian * there are two shutdown lists. Some things need to be shut down
10617768Sjulian * Earlier than others.
10717768Sjulian */
10817768Sjulianstatic sle_p shutdown_list1;
10917768Sjulianstatic sle_p shutdown_list2;
11017658Sjulian
11117658Sjulian
11217658Sjulianstatic void dumpsys(void);
11317658Sjulian
11417658Sjulian#ifndef _SYS_SYSPROTO_H_
11517658Sjulianstruct reboot_args {
11617658Sjulian	int	opt;
11717658Sjulian};
11817658Sjulian#endif
11917658Sjulian/* ARGSUSED */
12017658Sjulian
12117658Sjulian/*
12217658Sjulian * The system call that results in a reboot
12317658Sjulian */
12417658Sjulianint
12517658Sjulianreboot(p, uap, retval)
12617658Sjulian	struct proc *p;
12717658Sjulian	struct reboot_args *uap;
12817658Sjulian	int *retval;
12917658Sjulian{
13017658Sjulian	int error;
13117658Sjulian
13217658Sjulian	if ((error = suser(p->p_ucred, &p->p_acflag)))
13317658Sjulian		return (error);
13417658Sjulian
13517658Sjulian	boot(uap->opt);
13617658Sjulian	return (0);
13717658Sjulian}
13817658Sjulian
13917658Sjulian/*
14017658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
14117658Sjulian */
14217658Sjulianvoid
14317658Sjulianshutdown_nice(void)
14417658Sjulian{
14517658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
14617658Sjulian	if (initproc != NULL) {
14717658Sjulian		psignal(initproc, SIGINT);
14817658Sjulian	} else {
14917658Sjulian		/* No init(8) running, so simply reboot */
15017658Sjulian		boot(RB_NOSYNC);
15117658Sjulian	}
15217658Sjulian	return;
15317658Sjulian}
15417658Sjulianstatic int	waittime = -1;
15517658Sjulianstatic struct pcb dumppcb;
15617658Sjulian
15717658Sjulian/*
15817658Sjulian *  Go through the rigmarole of shutting down..
15917658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
16017658Sjulian * anything machine dependant in it.
16117658Sjulian */
16218277Sbdevoid
16317658Sjulianboot(howto)
16417658Sjulian	int howto;
16517658Sjulian{
16617768Sjulian	sle_p ep;
16717658Sjulian
16817768Sjulian	ep = shutdown_list1;
16917768Sjulian	while (ep) {
17017768Sjulian		shutdown_list1 = ep->next;
17117658Sjulian		(*ep->function)(howto, ep->arg);
17217658Sjulian		ep = ep->next;
17317658Sjulian	}
17417658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
17517658Sjulian		register struct buf *bp;
17617658Sjulian		int iter, nbusy;
17717658Sjulian
17817658Sjulian		waittime = 0;
17917658Sjulian		printf("\nsyncing disks... ");
18017658Sjulian
18117658Sjulian		sync(&proc0, NULL, NULL);
18217658Sjulian
18317658Sjulian		for (iter = 0; iter < 20; iter++) {
18417658Sjulian			nbusy = 0;
18517658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
18617658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
18717658Sjulian					nbusy++;
18817658Sjulian				}
18917658Sjulian			}
19017658Sjulian			if (nbusy == 0)
19117658Sjulian				break;
19217658Sjulian			printf("%d ", nbusy);
19317658Sjulian			DELAY(40000 * iter);
19417658Sjulian		}
19517658Sjulian		if (nbusy) {
19617658Sjulian			/*
19717658Sjulian			 * Failed to sync all blocks. Indicate this and don't
19817658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
19917658Sjulian			 */
20017658Sjulian			printf("giving up\n");
20117658Sjulian#ifdef SHOW_BUSYBUFS
20217658Sjulian			nbusy = 0;
20317658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
20417658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
20517658Sjulian					nbusy++;
20617658Sjulian					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
20717658Sjulian				}
20817658Sjulian			}
20917658Sjulian			DELAY(5000000);	/* 5 seconds */
21017658Sjulian#endif
21117658Sjulian		} else {
21217658Sjulian			printf("done\n");
21317658Sjulian			/*
21417658Sjulian			 * Unmount filesystems
21517658Sjulian			 */
21617658Sjulian			if (panicstr == 0)
21717658Sjulian				vfs_unmountall();
21817658Sjulian		}
21917658Sjulian		DELAY(100000);			/* wait for console output to finish */
22017658Sjulian	}
22117768Sjulian	ep = shutdown_list2;
22217768Sjulian	while (ep) {
22317768Sjulian		shutdown_list2 = ep->next;
22417768Sjulian		(*ep->function)(howto, ep->arg);
22517768Sjulian		ep = ep->next;
22617768Sjulian	}
22717658Sjulian	splhigh();
22817658Sjulian	if (howto & RB_HALT) {
22917658Sjulian		printf("\n");
23017658Sjulian		printf("The operating system has halted.\n");
23117658Sjulian		printf("Please press any key to reboot.\n\n");
23217658Sjulian		cngetc();
23317658Sjulian	} else {
23417658Sjulian		if (howto & RB_DUMP) {
23517658Sjulian			if (!cold) {
23617658Sjulian				savectx(&dumppcb);
23717658Sjulian				dumppcb.pcb_cr3 = rcr3();
23817658Sjulian				dumpsys();
23917658Sjulian			}
24017658Sjulian
24117658Sjulian			if (PANIC_REBOOT_WAIT_TIME != 0) {
24217658Sjulian				if (PANIC_REBOOT_WAIT_TIME != -1) {
24317658Sjulian					int loop;
24417658Sjulian					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
24517658Sjulian						PANIC_REBOOT_WAIT_TIME);
24617658Sjulian					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
24717658Sjulian						DELAY(1000 * 100); /* 1/10th second */
24817658Sjulian						if (cncheckc()) /* Did user type a key? */
24917658Sjulian							break;
25017658Sjulian					}
25117658Sjulian					if (!loop)
25217658Sjulian						goto die;
25317658Sjulian				}
25417658Sjulian			} else { /* zero time specified - reboot NOW */
25517658Sjulian				goto die;
25617658Sjulian			}
25717658Sjulian			printf("--> Press a key on the console to reboot <--\n");
25817658Sjulian			cngetc();
25917658Sjulian		}
26017658Sjulian	}
26117658Sjuliandie:
26217658Sjulian	printf("Rebooting...\n");
26317658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
26417677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
26517658Sjulian	cpu_reset();
26617658Sjulian	for(;;) ;
26717658Sjulian	/* NOTREACHED */
26817658Sjulian}
26917658Sjulian
27017658Sjulian/*
27117658Sjulian * Magic number for savecore
27217658Sjulian *
27317658Sjulian * exported (symorder) and used at least by savecore(8)
27417658Sjulian *
27517658Sjulian */
27617658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
27717658Sjulian
27817658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
27917658Sjulian
28017658Sjulianstatic int	dodump = 1;
28117658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
28217658Sjulian
28317658Sjulian/*
28417658Sjulian * Doadump comes here after turning off memory management and
28517658Sjulian * getting on the dump stack, either when called above, or by
28617658Sjulian * the auto-restart code.
28717658Sjulian */
28817658Sjulianstatic void
28917658Sjuliandumpsys(void)
29017658Sjulian{
29117658Sjulian
29217658Sjulian	if (!dodump)
29317658Sjulian		return;
29417658Sjulian	if (dumpdev == NODEV)
29517658Sjulian		return;
29617658Sjulian	if ((minor(dumpdev)&07) != 1)
29717658Sjulian		return;
29817658Sjulian	if (!(bdevsw[major(dumpdev)]))
29917658Sjulian		return;
30017658Sjulian	if (!(bdevsw[major(dumpdev)]->d_dump))
30117658Sjulian		return;
30217658Sjulian	dumpsize = Maxmem;
30317658Sjulian	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
30417658Sjulian	printf("dump ");
30517658Sjulian	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
30617658Sjulian
30717658Sjulian	case ENXIO:
30817658Sjulian		printf("device bad\n");
30917658Sjulian		break;
31017658Sjulian
31117658Sjulian	case EFAULT:
31217658Sjulian		printf("device not ready\n");
31317658Sjulian		break;
31417658Sjulian
31517658Sjulian	case EINVAL:
31617658Sjulian		printf("area improper\n");
31717658Sjulian		break;
31817658Sjulian
31917658Sjulian	case EIO:
32017658Sjulian		printf("i/o error\n");
32117658Sjulian		break;
32217658Sjulian
32317658Sjulian	case EINTR:
32417658Sjulian		printf("aborted from console\n");
32517658Sjulian		break;
32617658Sjulian
32717658Sjulian	default:
32817658Sjulian		printf("succeeded\n");
32917658Sjulian		break;
33017658Sjulian	}
33117658Sjulian}
33217658Sjulian
33317658Sjulian/*
33417658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
33517658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
33617658Sjulian * the disks as this often leads to recursive panics.
33717658Sjulian */
33817658Sjulianvoid
33917658Sjulianpanic(const char *fmt, ...)
34017658Sjulian{
34117658Sjulian	int bootopt;
34217658Sjulian	va_list ap;
34317658Sjulian
34417658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
34517658Sjulian	if (panicstr)
34617658Sjulian		bootopt |= RB_NOSYNC;
34717658Sjulian	else
34817658Sjulian		panicstr = fmt;
34917658Sjulian
35017658Sjulian	printf("panic: ");
35117658Sjulian	va_start(ap, fmt);
35217658Sjulian	vprintf(fmt, ap);
35317658Sjulian	va_end(ap);
35417658Sjulian	printf("\n");
35517658Sjulian
35617658Sjulian#if defined(DDB)
35717658Sjulian	if (debugger_on_panic)
35817658Sjulian		Debugger ("panic");
35917658Sjulian#endif
36017658Sjulian	boot(bootopt);
36117658Sjulian}
36217658Sjulian
36317768Sjulian/*
36417768Sjulian * Two routines to handle adding/deleting items on the
36517768Sjulian * shutdown callout lists
36617768Sjulian *
36717768Sjulian * at_shutdown():
36817658Sjulian * Take the arguments given and put them onto the shutdown callout list.
36917658Sjulian * However first make sure that it's not already there.
37017658Sjulian * returns 0 on success.
37117658Sjulian */
37217658Sjulianint
37317768Sjulianat_shutdown(bootlist_fn function, void *arg, int position)
37417658Sjulian{
37517768Sjulian	sle_p ep, *epp;
37617768Sjulian
37717768Sjulian	switch(position) {
37817768Sjulian	case SHUTDOWN_PRE_SYNC:
37917768Sjulian		epp = &shutdown_list1;
38017768Sjulian		break;
38117768Sjulian	case SHUTDOWN_POST_SYNC:
38217768Sjulian		epp = &shutdown_list2;
38317768Sjulian		break;
38417768Sjulian	default:
38517768Sjulian		printf("bad exit callout list specified\n");
38617768Sjulian		return (EINVAL);
38717768Sjulian	}
38817768Sjulian	if (rm_at_shutdown(function, arg))
38917658Sjulian		printf("exit callout entry already present\n");
39017768Sjulian	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
39117768Sjulian	if (ep == NULL)
39217768Sjulian		return (ENOMEM);
39317768Sjulian	ep->next = *epp;
39417658Sjulian	ep->function = function;
39517658Sjulian	ep->arg = arg;
39617768Sjulian	*epp = ep;
39717768Sjulian	return (0);
39817658Sjulian}
39917768Sjulian
40017658Sjulian/*
40117768Sjulian * Scan the exit callout lists for the given items and remove them.
40217658Sjulian * Returns the number of items removed.
40317658Sjulian */
40417658Sjulianint
40517658Sjulianrm_at_shutdown(bootlist_fn function, void *arg)
40617658Sjulian{
40717768Sjulian	sle_p *epp, ep;
40817768Sjulian	int count;
40917658Sjulian
41017768Sjulian	count = 0;
41117768Sjulian	epp = &shutdown_list1;
41217658Sjulian	ep = *epp;
41317768Sjulian	while (ep) {
41417834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
41517658Sjulian			*epp = ep->next;
41617768Sjulian			free(ep, M_TEMP);
41717658Sjulian			count++;
41817658Sjulian		} else {
41917658Sjulian			epp = &ep->next;
42017658Sjulian		}
42117658Sjulian		ep = *epp;
42217658Sjulian	}
42317768Sjulian	epp = &shutdown_list2;
42417768Sjulian	ep = *epp;
42517768Sjulian	while (ep) {
42617834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
42717768Sjulian			*epp = ep->next;
42817768Sjulian			free(ep, M_TEMP);
42917768Sjulian			count++;
43017768Sjulian		} else {
43117768Sjulian			epp = &ep->next;
43217768Sjulian		}
43317768Sjulian		ep = *epp;
43417768Sjulian	}
43517768Sjulian	return (count);
43617658Sjulian}
43717658Sjulian
438