kern_shutdown.c revision 26812
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
3926812Speter * $Id: kern_shutdown.c,v 1.16 1997/06/15 02:03:03 wollman 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>
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>
6426812Speter#ifdef SMP
6526812Speter#include <machine/smp.h>		/* smp_active, cpuid */
6626812Speter#endif
6717658Sjulian
6817658Sjulian#include <sys/utsname.h>
6917658Sjulian#include <sys/signalvar.h>
7017658Sjulian
7117658Sjulian#ifndef PANIC_REBOOT_WAIT_TIME
7217658Sjulian#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
7317658Sjulian#endif
7417658Sjulian
7517658Sjulian/*
7617658Sjulian * Note that stdarg.h and the ANSI style va_start macro is used for both
7717658Sjulian * ANSI and traditional C compilers.
7817658Sjulian */
7917658Sjulian#include <machine/stdarg.h>
8017658Sjulian
8117658Sjulian#if defined(DDB)
8217658Sjulian#ifdef DDB_UNATTENDED
8317658Sjulian	static int debugger_on_panic = 0;
8417658Sjulian#else
8517658Sjulian	static int debugger_on_panic = 1;
8617658Sjulian#endif
8717658Sjulian
8817658SjulianSYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
8917658Sjulian	&debugger_on_panic, 0, "");
9017658Sjulian#endif
9117658Sjulian
9217658Sjulian
9317658Sjulian/*
9417658Sjulian * Variable panicstr contains argument to first call to panic; used as flag
9517658Sjulian * to indicate that the kernel has already called panic.
9617658Sjulian */
9717658Sjulianconst char *panicstr;
9817658Sjulian
9917658Sjulian/*
10017658Sjulian * callout list for things to do a shutdown
10117658Sjulian */
10217658Sjuliantypedef struct shutdown_list_element {
10317658Sjulian	struct shutdown_list_element *next;
10417658Sjulian	bootlist_fn function;
10517658Sjulian	void *arg;
10617658Sjulian} *sle_p;
10717658Sjulian
10817768Sjulian/*
10917768Sjulian * there are two shutdown lists. Some things need to be shut down
11017768Sjulian * Earlier than others.
11117768Sjulian */
11217768Sjulianstatic sle_p shutdown_list1;
11317768Sjulianstatic sle_p shutdown_list2;
11417658Sjulian
11517658Sjulian
11617658Sjulianstatic void dumpsys(void);
11717658Sjulian
11817658Sjulian#ifndef _SYS_SYSPROTO_H_
11917658Sjulianstruct reboot_args {
12017658Sjulian	int	opt;
12117658Sjulian};
12217658Sjulian#endif
12317658Sjulian/* ARGSUSED */
12417658Sjulian
12517658Sjulian/*
12617658Sjulian * The system call that results in a reboot
12717658Sjulian */
12817658Sjulianint
12917658Sjulianreboot(p, uap, retval)
13017658Sjulian	struct proc *p;
13117658Sjulian	struct reboot_args *uap;
13217658Sjulian	int *retval;
13317658Sjulian{
13417658Sjulian	int error;
13517658Sjulian
13617658Sjulian	if ((error = suser(p->p_ucred, &p->p_acflag)))
13717658Sjulian		return (error);
13817658Sjulian
13917658Sjulian	boot(uap->opt);
14017658Sjulian	return (0);
14117658Sjulian}
14217658Sjulian
14317658Sjulian/*
14417658Sjulian * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
14517658Sjulian */
14617658Sjulianvoid
14717658Sjulianshutdown_nice(void)
14817658Sjulian{
14917658Sjulian	/* Send a signal to init(8) and have it shutdown the world */
15017658Sjulian	if (initproc != NULL) {
15117658Sjulian		psignal(initproc, SIGINT);
15217658Sjulian	} else {
15317658Sjulian		/* No init(8) running, so simply reboot */
15417658Sjulian		boot(RB_NOSYNC);
15517658Sjulian	}
15617658Sjulian	return;
15717658Sjulian}
15817658Sjulianstatic int	waittime = -1;
15917658Sjulianstatic struct pcb dumppcb;
16017658Sjulian
16117658Sjulian/*
16217658Sjulian *  Go through the rigmarole of shutting down..
16317658Sjulian * this used to be in machdep.c but I'll be dammned if I could see
16417658Sjulian * anything machine dependant in it.
16517658Sjulian */
16618277Sbdevoid
16717658Sjulianboot(howto)
16817658Sjulian	int howto;
16917658Sjulian{
17017768Sjulian	sle_p ep;
17117658Sjulian
17225164Speter#ifdef SMP
17325164Speter	int c, spins;
17425164Speter
17525164Speter	/* don't accidently start it */
17625164Speter	if (smp_active) {
17725164Speter		smp_active = 1;
17825164Speter
17925164Speter		spins = 100;
18025164Speter
18126812Speter		printf("boot() called on cpu#%d\n", cpuid);
18226812Speter		while ((c = cpuid) != 0) {
18325164Speter			if (spins-- < 1) {
18425164Speter				printf("timeout waiting for cpu #0!\n");
18525164Speter				break;
18625164Speter			}
18725164Speter			printf("oops, I'm on cpu#%d, I need to be on cpu#0!\n",
18825164Speter				c);
18925164Speter			tsleep((caddr_t)&smp_active, PZERO, "cpu0wt", 10);
19025164Speter		}
19125164Speter	}
19225164Speter#endif
19317768Sjulian	ep = shutdown_list1;
19417768Sjulian	while (ep) {
19517768Sjulian		shutdown_list1 = ep->next;
19617658Sjulian		(*ep->function)(howto, ep->arg);
19717658Sjulian		ep = ep->next;
19817658Sjulian	}
19917658Sjulian	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
20017658Sjulian		register struct buf *bp;
20117658Sjulian		int iter, nbusy;
20217658Sjulian
20317658Sjulian		waittime = 0;
20417658Sjulian		printf("\nsyncing disks... ");
20517658Sjulian
20617658Sjulian		sync(&proc0, NULL, NULL);
20717658Sjulian
20817658Sjulian		for (iter = 0; iter < 20; iter++) {
20917658Sjulian			nbusy = 0;
21017658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
21117658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
21217658Sjulian					nbusy++;
21317658Sjulian				}
21417658Sjulian			}
21517658Sjulian			if (nbusy == 0)
21617658Sjulian				break;
21717658Sjulian			printf("%d ", nbusy);
21817658Sjulian			DELAY(40000 * iter);
21917658Sjulian		}
22017658Sjulian		if (nbusy) {
22117658Sjulian			/*
22217658Sjulian			 * Failed to sync all blocks. Indicate this and don't
22317658Sjulian			 * unmount filesystems (thus forcing an fsck on reboot).
22417658Sjulian			 */
22517658Sjulian			printf("giving up\n");
22617658Sjulian#ifdef SHOW_BUSYBUFS
22717658Sjulian			nbusy = 0;
22817658Sjulian			for (bp = &buf[nbuf]; --bp >= buf; ) {
22917658Sjulian				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
23017658Sjulian					nbusy++;
23117658Sjulian					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
23217658Sjulian				}
23317658Sjulian			}
23417658Sjulian			DELAY(5000000);	/* 5 seconds */
23517658Sjulian#endif
23617658Sjulian		} else {
23717658Sjulian			printf("done\n");
23817658Sjulian			/*
23917658Sjulian			 * Unmount filesystems
24017658Sjulian			 */
24117658Sjulian			if (panicstr == 0)
24217658Sjulian				vfs_unmountall();
24317658Sjulian		}
24417658Sjulian		DELAY(100000);			/* wait for console output to finish */
24517658Sjulian	}
24617768Sjulian	ep = shutdown_list2;
24717768Sjulian	while (ep) {
24817768Sjulian		shutdown_list2 = ep->next;
24917768Sjulian		(*ep->function)(howto, ep->arg);
25017768Sjulian		ep = ep->next;
25117768Sjulian	}
25217658Sjulian	splhigh();
25317658Sjulian	if (howto & RB_HALT) {
25426657Swollman		cpu_power_down();
25517658Sjulian		printf("\n");
25617658Sjulian		printf("The operating system has halted.\n");
25717658Sjulian		printf("Please press any key to reboot.\n\n");
25819274Sjulian		switch (cngetc()) {
25919274Sjulian		case -1:		/* No console, just die */
26019274Sjulian			cpu_halt();
26119274Sjulian			/* NOTREACHED */
26219274Sjulian		default:
26319274Sjulian			break;
26419274Sjulian		}
26517658Sjulian	} else {
26617658Sjulian		if (howto & RB_DUMP) {
26717658Sjulian			if (!cold) {
26817658Sjulian				savectx(&dumppcb);
26917658Sjulian				dumppcb.pcb_cr3 = rcr3();
27017658Sjulian				dumpsys();
27117658Sjulian			}
27217658Sjulian
27317658Sjulian			if (PANIC_REBOOT_WAIT_TIME != 0) {
27417658Sjulian				if (PANIC_REBOOT_WAIT_TIME != -1) {
27517658Sjulian					int loop;
27617658Sjulian					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
27717658Sjulian						PANIC_REBOOT_WAIT_TIME);
27817658Sjulian					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
27917658Sjulian						DELAY(1000 * 100); /* 1/10th second */
28018290Sbde						/* Did user type a key? */
28118290Sbde						if (cncheckc() != -1)
28217658Sjulian							break;
28317658Sjulian					}
28417658Sjulian					if (!loop)
28517658Sjulian						goto die;
28617658Sjulian				}
28717658Sjulian			} else { /* zero time specified - reboot NOW */
28817658Sjulian				goto die;
28917658Sjulian			}
29017658Sjulian			printf("--> Press a key on the console to reboot <--\n");
29117658Sjulian			cngetc();
29217658Sjulian		}
29317658Sjulian	}
29417658Sjuliandie:
29517658Sjulian	printf("Rebooting...\n");
29617658Sjulian	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
29717677Sjulian	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
29817658Sjulian	cpu_reset();
29917658Sjulian	for(;;) ;
30017658Sjulian	/* NOTREACHED */
30117658Sjulian}
30217658Sjulian
30317658Sjulian/*
30417658Sjulian * Magic number for savecore
30517658Sjulian *
30617658Sjulian * exported (symorder) and used at least by savecore(8)
30717658Sjulian *
30817658Sjulian */
30917658Sjulianstatic u_long const	dumpmag = 0x8fca0101UL;
31017658Sjulian
31117658Sjulianstatic int	dumpsize = 0;		/* also for savecore */
31217658Sjulian
31317658Sjulianstatic int	dodump = 1;
31417658SjulianSYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
31517658Sjulian
31617658Sjulian/*
31717658Sjulian * Doadump comes here after turning off memory management and
31817658Sjulian * getting on the dump stack, either when called above, or by
31917658Sjulian * the auto-restart code.
32017658Sjulian */
32117658Sjulianstatic void
32217658Sjuliandumpsys(void)
32317658Sjulian{
32417658Sjulian
32517658Sjulian	if (!dodump)
32617658Sjulian		return;
32717658Sjulian	if (dumpdev == NODEV)
32817658Sjulian		return;
32917658Sjulian	if ((minor(dumpdev)&07) != 1)
33017658Sjulian		return;
33117658Sjulian	if (!(bdevsw[major(dumpdev)]))
33217658Sjulian		return;
33317658Sjulian	if (!(bdevsw[major(dumpdev)]->d_dump))
33417658Sjulian		return;
33517658Sjulian	dumpsize = Maxmem;
33617658Sjulian	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
33717658Sjulian	printf("dump ");
33817658Sjulian	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
33917658Sjulian
34017658Sjulian	case ENXIO:
34117658Sjulian		printf("device bad\n");
34217658Sjulian		break;
34317658Sjulian
34417658Sjulian	case EFAULT:
34517658Sjulian		printf("device not ready\n");
34617658Sjulian		break;
34717658Sjulian
34817658Sjulian	case EINVAL:
34917658Sjulian		printf("area improper\n");
35017658Sjulian		break;
35117658Sjulian
35217658Sjulian	case EIO:
35317658Sjulian		printf("i/o error\n");
35417658Sjulian		break;
35517658Sjulian
35617658Sjulian	case EINTR:
35717658Sjulian		printf("aborted from console\n");
35817658Sjulian		break;
35917658Sjulian
36017658Sjulian	default:
36117658Sjulian		printf("succeeded\n");
36217658Sjulian		break;
36317658Sjulian	}
36417658Sjulian}
36517658Sjulian
36617658Sjulian/*
36717658Sjulian * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
36817658Sjulian * and then reboots.  If we are called twice, then we avoid trying to sync
36917658Sjulian * the disks as this often leads to recursive panics.
37017658Sjulian */
37117658Sjulianvoid
37217658Sjulianpanic(const char *fmt, ...)
37317658Sjulian{
37417658Sjulian	int bootopt;
37517658Sjulian	va_list ap;
37617658Sjulian
37717658Sjulian	bootopt = RB_AUTOBOOT | RB_DUMP;
37817658Sjulian	if (panicstr)
37917658Sjulian		bootopt |= RB_NOSYNC;
38017658Sjulian	else
38117658Sjulian		panicstr = fmt;
38217658Sjulian
38317658Sjulian	printf("panic: ");
38417658Sjulian	va_start(ap, fmt);
38517658Sjulian	vprintf(fmt, ap);
38617658Sjulian	va_end(ap);
38717658Sjulian	printf("\n");
38826100Sfsmp#ifdef SMP
38926812Speter	printf(" cpuid %d\n", cpuid);
39026100Sfsmp#endif
39117658Sjulian
39217658Sjulian#if defined(DDB)
39317658Sjulian	if (debugger_on_panic)
39417658Sjulian		Debugger ("panic");
39517658Sjulian#endif
39617658Sjulian	boot(bootopt);
39717658Sjulian}
39817658Sjulian
39917768Sjulian/*
40017768Sjulian * Two routines to handle adding/deleting items on the
40117768Sjulian * shutdown callout lists
40217768Sjulian *
40317768Sjulian * at_shutdown():
40417658Sjulian * Take the arguments given and put them onto the shutdown callout list.
40517658Sjulian * However first make sure that it's not already there.
40617658Sjulian * returns 0 on success.
40717658Sjulian */
40817658Sjulianint
40917768Sjulianat_shutdown(bootlist_fn function, void *arg, int position)
41017658Sjulian{
41117768Sjulian	sle_p ep, *epp;
41217768Sjulian
41317768Sjulian	switch(position) {
41417768Sjulian	case SHUTDOWN_PRE_SYNC:
41517768Sjulian		epp = &shutdown_list1;
41617768Sjulian		break;
41717768Sjulian	case SHUTDOWN_POST_SYNC:
41817768Sjulian		epp = &shutdown_list2;
41917768Sjulian		break;
42017768Sjulian	default:
42117768Sjulian		printf("bad exit callout list specified\n");
42217768Sjulian		return (EINVAL);
42317768Sjulian	}
42417768Sjulian	if (rm_at_shutdown(function, arg))
42517658Sjulian		printf("exit callout entry already present\n");
42617768Sjulian	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
42717768Sjulian	if (ep == NULL)
42817768Sjulian		return (ENOMEM);
42917768Sjulian	ep->next = *epp;
43017658Sjulian	ep->function = function;
43117658Sjulian	ep->arg = arg;
43217768Sjulian	*epp = ep;
43317768Sjulian	return (0);
43417658Sjulian}
43517768Sjulian
43617658Sjulian/*
43717768Sjulian * Scan the exit callout lists for the given items and remove them.
43817658Sjulian * Returns the number of items removed.
43917658Sjulian */
44017658Sjulianint
44117658Sjulianrm_at_shutdown(bootlist_fn function, void *arg)
44217658Sjulian{
44317768Sjulian	sle_p *epp, ep;
44417768Sjulian	int count;
44517658Sjulian
44617768Sjulian	count = 0;
44717768Sjulian	epp = &shutdown_list1;
44817658Sjulian	ep = *epp;
44917768Sjulian	while (ep) {
45017834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
45117658Sjulian			*epp = ep->next;
45217768Sjulian			free(ep, M_TEMP);
45317658Sjulian			count++;
45417658Sjulian		} else {
45517658Sjulian			epp = &ep->next;
45617658Sjulian		}
45717658Sjulian		ep = *epp;
45817658Sjulian	}
45917768Sjulian	epp = &shutdown_list2;
46017768Sjulian	ep = *epp;
46117768Sjulian	while (ep) {
46217834Sjulian		if ((ep->function == function) && (ep->arg == arg)) {
46317768Sjulian			*epp = ep->next;
46417768Sjulian			free(ep, M_TEMP);
46517768Sjulian			count++;
46617768Sjulian		} else {
46717768Sjulian			epp = &ep->next;
46817768Sjulian		}
46917768Sjulian		ep = *epp;
47017768Sjulian	}
47117768Sjulian	return (count);
47217658Sjulian}
47317658Sjulian
474