kern_shutdown.c revision 17834
1/*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
39 * $Id: kern_shutdown.c,v 1.3 1996/08/22 03:50:20 julian Exp $
40 */
41
42#include "opt_ddb.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/reboot.h>
47#include <sys/msgbuf.h>
48#include <sys/proc.h>
49#include <sys/vnode.h>
50#include <sys/tty.h>
51#include <sys/tprintf.h>
52#include <sys/syslog.h>
53#include <sys/malloc.h>
54#include <sys/kernel.h>
55#include <sys/sysctl.h>
56#include <sys/devconf.h>
57#include <sys/conf.h>
58#include <sys/sysproto.h>
59
60#include <machine/pcb.h>
61#include <machine/clock.h>
62#include <machine/cons.h>
63#include <machine/md_var.h>
64
65#include <sys/utsname.h>
66#include <sys/signalvar.h>
67
68#ifndef PANIC_REBOOT_WAIT_TIME
69#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
70#endif
71
72/*
73 * Note that stdarg.h and the ANSI style va_start macro is used for both
74 * ANSI and traditional C compilers.
75 */
76#include <machine/stdarg.h>
77
78#if defined(DDB)
79#ifdef DDB_UNATTENDED
80	static int debugger_on_panic = 0;
81#else
82	static int debugger_on_panic = 1;
83#endif
84
85SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
86	&debugger_on_panic, 0, "");
87#endif
88
89
90/*
91 * Variable panicstr contains argument to first call to panic; used as flag
92 * to indicate that the kernel has already called panic.
93 */
94const char *panicstr;
95
96/*
97 * callout list for things to do a shutdown
98 */
99typedef struct shutdown_list_element {
100	struct shutdown_list_element *next;
101	bootlist_fn function;
102	void *arg;
103} *sle_p;
104
105/*
106 * there are two shutdown lists. Some things need to be shut down
107 * Earlier than others.
108 */
109static sle_p shutdown_list1;
110static sle_p shutdown_list2;
111
112
113static void dumpsys(void);
114
115#ifndef _SYS_SYSPROTO_H_
116struct reboot_args {
117	int	opt;
118};
119#endif
120/* ARGSUSED */
121
122/*
123 * The system call that results in a reboot
124 */
125int
126reboot(p, uap, retval)
127	struct proc *p;
128	struct reboot_args *uap;
129	int *retval;
130{
131	int error;
132
133	if ((error = suser(p->p_ucred, &p->p_acflag)))
134		return (error);
135
136	boot(uap->opt);
137	return (0);
138}
139
140/*
141 * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
142 */
143void
144shutdown_nice(void)
145{
146	/* Send a signal to init(8) and have it shutdown the world */
147	if (initproc != NULL) {
148		psignal(initproc, SIGINT);
149	} else {
150		/* No init(8) running, so simply reboot */
151		boot(RB_NOSYNC);
152	}
153	return;
154}
155static int	waittime = -1;
156static struct pcb dumppcb;
157
158/*
159 *  Go through the rigmarole of shutting down..
160 * this used to be in machdep.c but I'll be dammned if I could see
161 * anything machine dependant in it.
162 */
163__dead void
164boot(howto)
165	int howto;
166{
167	sle_p ep;
168
169	ep = shutdown_list1;
170	while (ep) {
171		shutdown_list1 = ep->next;
172		(*ep->function)(howto, ep->arg);
173		ep = ep->next;
174	}
175	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
176		register struct buf *bp;
177		int iter, nbusy;
178
179		waittime = 0;
180		printf("\nsyncing disks... ");
181
182		sync(&proc0, NULL, NULL);
183
184		for (iter = 0; iter < 20; iter++) {
185			nbusy = 0;
186			for (bp = &buf[nbuf]; --bp >= buf; ) {
187				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
188					nbusy++;
189				}
190			}
191			if (nbusy == 0)
192				break;
193			printf("%d ", nbusy);
194			DELAY(40000 * iter);
195		}
196		if (nbusy) {
197			/*
198			 * Failed to sync all blocks. Indicate this and don't
199			 * unmount filesystems (thus forcing an fsck on reboot).
200			 */
201			printf("giving up\n");
202#ifdef SHOW_BUSYBUFS
203			nbusy = 0;
204			for (bp = &buf[nbuf]; --bp >= buf; ) {
205				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
206					nbusy++;
207					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
208				}
209			}
210			DELAY(5000000);	/* 5 seconds */
211#endif
212		} else {
213			printf("done\n");
214			/*
215			 * Unmount filesystems
216			 */
217			if (panicstr == 0)
218				vfs_unmountall();
219		}
220		DELAY(100000);			/* wait for console output to finish */
221		dev_shutdownall(FALSE);
222	}
223	ep = shutdown_list2;
224	while (ep) {
225		shutdown_list2 = ep->next;
226		(*ep->function)(howto, ep->arg);
227		ep = ep->next;
228	}
229	splhigh();
230	if (howto & RB_HALT) {
231		printf("\n");
232		printf("The operating system has halted.\n");
233		printf("Please press any key to reboot.\n\n");
234		cngetc();
235	} else {
236		if (howto & RB_DUMP) {
237			if (!cold) {
238				savectx(&dumppcb);
239				dumppcb.pcb_cr3 = rcr3();
240				dumpsys();
241			}
242
243			if (PANIC_REBOOT_WAIT_TIME != 0) {
244				if (PANIC_REBOOT_WAIT_TIME != -1) {
245					int loop;
246					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
247						PANIC_REBOOT_WAIT_TIME);
248					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
249						DELAY(1000 * 100); /* 1/10th second */
250						if (cncheckc()) /* Did user type a key? */
251							break;
252					}
253					if (!loop)
254						goto die;
255				}
256			} else { /* zero time specified - reboot NOW */
257				goto die;
258			}
259			printf("--> Press a key on the console to reboot <--\n");
260			cngetc();
261		}
262	}
263die:
264	printf("Rebooting...\n");
265	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
266	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
267	cpu_reset();
268	for(;;) ;
269	/* NOTREACHED */
270}
271
272/*
273 * Magic number for savecore
274 *
275 * exported (symorder) and used at least by savecore(8)
276 *
277 */
278static u_long const	dumpmag = 0x8fca0101UL;
279
280static int	dumpsize = 0;		/* also for savecore */
281
282static int	dodump = 1;
283SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
284
285/*
286 * Doadump comes here after turning off memory management and
287 * getting on the dump stack, either when called above, or by
288 * the auto-restart code.
289 */
290static void
291dumpsys(void)
292{
293
294	if (!dodump)
295		return;
296	if (dumpdev == NODEV)
297		return;
298	if ((minor(dumpdev)&07) != 1)
299		return;
300	if (!(bdevsw[major(dumpdev)]))
301		return;
302	if (!(bdevsw[major(dumpdev)]->d_dump))
303		return;
304	dumpsize = Maxmem;
305	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
306	printf("dump ");
307	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
308
309	case ENXIO:
310		printf("device bad\n");
311		break;
312
313	case EFAULT:
314		printf("device not ready\n");
315		break;
316
317	case EINVAL:
318		printf("area improper\n");
319		break;
320
321	case EIO:
322		printf("i/o error\n");
323		break;
324
325	case EINTR:
326		printf("aborted from console\n");
327		break;
328
329	default:
330		printf("succeeded\n");
331		break;
332	}
333}
334
335/*
336 * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
337 * and then reboots.  If we are called twice, then we avoid trying to sync
338 * the disks as this often leads to recursive panics.
339 */
340#ifdef __GNUC__
341__dead			/* panic() does not return */
342#endif
343void
344panic(const char *fmt, ...)
345{
346	int bootopt;
347	va_list ap;
348
349	bootopt = RB_AUTOBOOT | RB_DUMP;
350	if (panicstr)
351		bootopt |= RB_NOSYNC;
352	else
353		panicstr = fmt;
354
355	printf("panic: ");
356	va_start(ap, fmt);
357	vprintf(fmt, ap);
358	va_end(ap);
359	printf("\n");
360
361#if defined(DDB)
362	if (debugger_on_panic)
363		Debugger ("panic");
364#endif
365	boot(bootopt);
366}
367
368/*
369 * Two routines to handle adding/deleting items on the
370 * shutdown callout lists
371 *
372 * at_shutdown():
373 * Take the arguments given and put them onto the shutdown callout list.
374 * However first make sure that it's not already there.
375 * returns 0 on success.
376 */
377int
378at_shutdown(bootlist_fn function, void *arg, int position)
379{
380	sle_p ep, *epp;
381
382	switch(position) {
383	case SHUTDOWN_PRE_SYNC:
384		epp = &shutdown_list1;
385		break;
386	case SHUTDOWN_POST_SYNC:
387		epp = &shutdown_list2;
388		break;
389	default:
390		printf("bad exit callout list specified\n");
391		return (EINVAL);
392	}
393	if (rm_at_shutdown(function, arg))
394		printf("exit callout entry already present\n");
395	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
396	if (ep == NULL)
397		return (ENOMEM);
398	ep->next = *epp;
399	ep->function = function;
400	ep->arg = arg;
401	*epp = ep;
402	return (0);
403}
404
405/*
406 * Scan the exit callout lists for the given items and remove them.
407 * Returns the number of items removed.
408 */
409int
410rm_at_shutdown(bootlist_fn function, void *arg)
411{
412	sle_p *epp, ep;
413	int count;
414
415	count = 0;
416	epp = &shutdown_list1;
417	ep = *epp;
418	while (ep) {
419		if ((ep->function == function) && (ep->arg == arg)) {
420			*epp = ep->next;
421			free(ep, M_TEMP);
422			count++;
423		} else {
424			epp = &ep->next;
425		}
426		ep = *epp;
427	}
428	epp = &shutdown_list2;
429	ep = *epp;
430	while (ep) {
431		if ((ep->function == function) && (ep->arg == arg)) {
432			*epp = ep->next;
433			free(ep, M_TEMP);
434			count++;
435		} else {
436			epp = &ep->next;
437		}
438		ep = *epp;
439	}
440	return (count);
441}
442
443