kern_shutdown.c revision 18277
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.6 1996/09/07 19:13:09 sos 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/conf.h>
57#include <sys/sysproto.h>
58
59#include <machine/pcb.h>
60#include <machine/clock.h>
61#include <machine/cons.h>
62#include <machine/md_var.h>
63
64#include <sys/utsname.h>
65#include <sys/signalvar.h>
66
67#ifndef PANIC_REBOOT_WAIT_TIME
68#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
69#endif
70
71/*
72 * Note that stdarg.h and the ANSI style va_start macro is used for both
73 * ANSI and traditional C compilers.
74 */
75#include <machine/stdarg.h>
76
77#if defined(DDB)
78#ifdef DDB_UNATTENDED
79	static int debugger_on_panic = 0;
80#else
81	static int debugger_on_panic = 1;
82#endif
83
84SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
85	&debugger_on_panic, 0, "");
86#endif
87
88
89/*
90 * Variable panicstr contains argument to first call to panic; used as flag
91 * to indicate that the kernel has already called panic.
92 */
93const char *panicstr;
94
95/*
96 * callout list for things to do a shutdown
97 */
98typedef struct shutdown_list_element {
99	struct shutdown_list_element *next;
100	bootlist_fn function;
101	void *arg;
102} *sle_p;
103
104/*
105 * there are two shutdown lists. Some things need to be shut down
106 * Earlier than others.
107 */
108static sle_p shutdown_list1;
109static sle_p shutdown_list2;
110
111
112static void dumpsys(void);
113
114#ifndef _SYS_SYSPROTO_H_
115struct reboot_args {
116	int	opt;
117};
118#endif
119/* ARGSUSED */
120
121/*
122 * The system call that results in a reboot
123 */
124int
125reboot(p, uap, retval)
126	struct proc *p;
127	struct reboot_args *uap;
128	int *retval;
129{
130	int error;
131
132	if ((error = suser(p->p_ucred, &p->p_acflag)))
133		return (error);
134
135	boot(uap->opt);
136	return (0);
137}
138
139/*
140 * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
141 */
142void
143shutdown_nice(void)
144{
145	/* Send a signal to init(8) and have it shutdown the world */
146	if (initproc != NULL) {
147		psignal(initproc, SIGINT);
148	} else {
149		/* No init(8) running, so simply reboot */
150		boot(RB_NOSYNC);
151	}
152	return;
153}
154static int	waittime = -1;
155static struct pcb dumppcb;
156
157/*
158 *  Go through the rigmarole of shutting down..
159 * this used to be in machdep.c but I'll be dammned if I could see
160 * anything machine dependant in it.
161 */
162void
163boot(howto)
164	int howto;
165{
166	sle_p ep;
167
168	ep = shutdown_list1;
169	while (ep) {
170		shutdown_list1 = ep->next;
171		(*ep->function)(howto, ep->arg);
172		ep = ep->next;
173	}
174	if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
175		register struct buf *bp;
176		int iter, nbusy;
177
178		waittime = 0;
179		printf("\nsyncing disks... ");
180
181		sync(&proc0, NULL, NULL);
182
183		for (iter = 0; iter < 20; iter++) {
184			nbusy = 0;
185			for (bp = &buf[nbuf]; --bp >= buf; ) {
186				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
187					nbusy++;
188				}
189			}
190			if (nbusy == 0)
191				break;
192			printf("%d ", nbusy);
193			DELAY(40000 * iter);
194		}
195		if (nbusy) {
196			/*
197			 * Failed to sync all blocks. Indicate this and don't
198			 * unmount filesystems (thus forcing an fsck on reboot).
199			 */
200			printf("giving up\n");
201#ifdef SHOW_BUSYBUFS
202			nbusy = 0;
203			for (bp = &buf[nbuf]; --bp >= buf; ) {
204				if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) {
205					nbusy++;
206					printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno);
207				}
208			}
209			DELAY(5000000);	/* 5 seconds */
210#endif
211		} else {
212			printf("done\n");
213			/*
214			 * Unmount filesystems
215			 */
216			if (panicstr == 0)
217				vfs_unmountall();
218		}
219		DELAY(100000);			/* wait for console output to finish */
220	}
221	ep = shutdown_list2;
222	while (ep) {
223		shutdown_list2 = ep->next;
224		(*ep->function)(howto, ep->arg);
225		ep = ep->next;
226	}
227	splhigh();
228	if (howto & RB_HALT) {
229		printf("\n");
230		printf("The operating system has halted.\n");
231		printf("Please press any key to reboot.\n\n");
232		cngetc();
233	} else {
234		if (howto & RB_DUMP) {
235			if (!cold) {
236				savectx(&dumppcb);
237				dumppcb.pcb_cr3 = rcr3();
238				dumpsys();
239			}
240
241			if (PANIC_REBOOT_WAIT_TIME != 0) {
242				if (PANIC_REBOOT_WAIT_TIME != -1) {
243					int loop;
244					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
245						PANIC_REBOOT_WAIT_TIME);
246					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
247						DELAY(1000 * 100); /* 1/10th second */
248						if (cncheckc()) /* Did user type a key? */
249							break;
250					}
251					if (!loop)
252						goto die;
253				}
254			} else { /* zero time specified - reboot NOW */
255				goto die;
256			}
257			printf("--> Press a key on the console to reboot <--\n");
258			cngetc();
259		}
260	}
261die:
262	printf("Rebooting...\n");
263	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
264	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
265	cpu_reset();
266	for(;;) ;
267	/* NOTREACHED */
268}
269
270/*
271 * Magic number for savecore
272 *
273 * exported (symorder) and used at least by savecore(8)
274 *
275 */
276static u_long const	dumpmag = 0x8fca0101UL;
277
278static int	dumpsize = 0;		/* also for savecore */
279
280static int	dodump = 1;
281SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
282
283/*
284 * Doadump comes here after turning off memory management and
285 * getting on the dump stack, either when called above, or by
286 * the auto-restart code.
287 */
288static void
289dumpsys(void)
290{
291
292	if (!dodump)
293		return;
294	if (dumpdev == NODEV)
295		return;
296	if ((minor(dumpdev)&07) != 1)
297		return;
298	if (!(bdevsw[major(dumpdev)]))
299		return;
300	if (!(bdevsw[major(dumpdev)]->d_dump))
301		return;
302	dumpsize = Maxmem;
303	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
304	printf("dump ");
305	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
306
307	case ENXIO:
308		printf("device bad\n");
309		break;
310
311	case EFAULT:
312		printf("device not ready\n");
313		break;
314
315	case EINVAL:
316		printf("area improper\n");
317		break;
318
319	case EIO:
320		printf("i/o error\n");
321		break;
322
323	case EINTR:
324		printf("aborted from console\n");
325		break;
326
327	default:
328		printf("succeeded\n");
329		break;
330	}
331}
332
333/*
334 * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
335 * and then reboots.  If we are called twice, then we avoid trying to sync
336 * the disks as this often leads to recursive panics.
337 */
338void
339panic(const char *fmt, ...)
340{
341	int bootopt;
342	va_list ap;
343
344	bootopt = RB_AUTOBOOT | RB_DUMP;
345	if (panicstr)
346		bootopt |= RB_NOSYNC;
347	else
348		panicstr = fmt;
349
350	printf("panic: ");
351	va_start(ap, fmt);
352	vprintf(fmt, ap);
353	va_end(ap);
354	printf("\n");
355
356#if defined(DDB)
357	if (debugger_on_panic)
358		Debugger ("panic");
359#endif
360	boot(bootopt);
361}
362
363/*
364 * Two routines to handle adding/deleting items on the
365 * shutdown callout lists
366 *
367 * at_shutdown():
368 * Take the arguments given and put them onto the shutdown callout list.
369 * However first make sure that it's not already there.
370 * returns 0 on success.
371 */
372int
373at_shutdown(bootlist_fn function, void *arg, int position)
374{
375	sle_p ep, *epp;
376
377	switch(position) {
378	case SHUTDOWN_PRE_SYNC:
379		epp = &shutdown_list1;
380		break;
381	case SHUTDOWN_POST_SYNC:
382		epp = &shutdown_list2;
383		break;
384	default:
385		printf("bad exit callout list specified\n");
386		return (EINVAL);
387	}
388	if (rm_at_shutdown(function, arg))
389		printf("exit callout entry already present\n");
390	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
391	if (ep == NULL)
392		return (ENOMEM);
393	ep->next = *epp;
394	ep->function = function;
395	ep->arg = arg;
396	*epp = ep;
397	return (0);
398}
399
400/*
401 * Scan the exit callout lists for the given items and remove them.
402 * Returns the number of items removed.
403 */
404int
405rm_at_shutdown(bootlist_fn function, void *arg)
406{
407	sle_p *epp, ep;
408	int count;
409
410	count = 0;
411	epp = &shutdown_list1;
412	ep = *epp;
413	while (ep) {
414		if ((ep->function == function) && (ep->arg == arg)) {
415			*epp = ep->next;
416			free(ep, M_TEMP);
417			count++;
418		} else {
419			epp = &ep->next;
420		}
421		ep = *epp;
422	}
423	epp = &shutdown_list2;
424	ep = *epp;
425	while (ep) {
426		if ((ep->function == function) && (ep->arg == arg)) {
427			*epp = ep->next;
428			free(ep, M_TEMP);
429			count++;
430		} else {
431			epp = &ep->next;
432		}
433		ep = *epp;
434	}
435	return (count);
436}
437
438