kern_shutdown.c revision 18084
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.4 1996/08/26 21:47:56 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/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 */
162__dead void
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		dev_shutdownall(FALSE);
221	}
222	ep = shutdown_list2;
223	while (ep) {
224		shutdown_list2 = ep->next;
225		(*ep->function)(howto, ep->arg);
226		ep = ep->next;
227	}
228	splhigh();
229	if (howto & RB_HALT) {
230		printf("\n");
231		printf("The operating system has halted.\n");
232		printf("Please press any key to reboot.\n\n");
233		cngetc();
234	} else {
235		if (howto & RB_DUMP) {
236			if (!cold) {
237				savectx(&dumppcb);
238				dumppcb.pcb_cr3 = rcr3();
239				dumpsys();
240			}
241
242			if (PANIC_REBOOT_WAIT_TIME != 0) {
243				if (PANIC_REBOOT_WAIT_TIME != -1) {
244					int loop;
245					printf("Automatic reboot in %d seconds - press a key on the console to abort\n",
246						PANIC_REBOOT_WAIT_TIME);
247					for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) {
248						DELAY(1000 * 100); /* 1/10th second */
249						if (cncheckc()) /* Did user type a key? */
250							break;
251					}
252					if (!loop)
253						goto die;
254				}
255			} else { /* zero time specified - reboot NOW */
256				goto die;
257			}
258			printf("--> Press a key on the console to reboot <--\n");
259			cngetc();
260		}
261	}
262die:
263	printf("Rebooting...\n");
264	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
265	/* cpu_boot(howto); */ /* doesn't do anything at the moment */
266	cpu_reset();
267	for(;;) ;
268	/* NOTREACHED */
269}
270
271/*
272 * Magic number for savecore
273 *
274 * exported (symorder) and used at least by savecore(8)
275 *
276 */
277static u_long const	dumpmag = 0x8fca0101UL;
278
279static int	dumpsize = 0;		/* also for savecore */
280
281static int	dodump = 1;
282SYSCTL_INT(_machdep, OID_AUTO, do_dump, CTLFLAG_RW, &dodump, 0, "");
283
284/*
285 * Doadump comes here after turning off memory management and
286 * getting on the dump stack, either when called above, or by
287 * the auto-restart code.
288 */
289static void
290dumpsys(void)
291{
292
293	if (!dodump)
294		return;
295	if (dumpdev == NODEV)
296		return;
297	if ((minor(dumpdev)&07) != 1)
298		return;
299	if (!(bdevsw[major(dumpdev)]))
300		return;
301	if (!(bdevsw[major(dumpdev)]->d_dump))
302		return;
303	dumpsize = Maxmem;
304	printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo);
305	printf("dump ");
306	switch ((*bdevsw[major(dumpdev)]->d_dump)(dumpdev)) {
307
308	case ENXIO:
309		printf("device bad\n");
310		break;
311
312	case EFAULT:
313		printf("device not ready\n");
314		break;
315
316	case EINVAL:
317		printf("area improper\n");
318		break;
319
320	case EIO:
321		printf("i/o error\n");
322		break;
323
324	case EINTR:
325		printf("aborted from console\n");
326		break;
327
328	default:
329		printf("succeeded\n");
330		break;
331	}
332}
333
334/*
335 * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
336 * and then reboots.  If we are called twice, then we avoid trying to sync
337 * the disks as this often leads to recursive panics.
338 */
339#ifdef __GNUC__
340__dead			/* panic() does not return */
341#endif
342void
343panic(const char *fmt, ...)
344{
345	int bootopt;
346	va_list ap;
347
348	bootopt = RB_AUTOBOOT | RB_DUMP;
349	if (panicstr)
350		bootopt |= RB_NOSYNC;
351	else
352		panicstr = fmt;
353
354	printf("panic: ");
355	va_start(ap, fmt);
356	vprintf(fmt, ap);
357	va_end(ap);
358	printf("\n");
359
360#if defined(DDB)
361	if (debugger_on_panic)
362		Debugger ("panic");
363#endif
364	boot(bootopt);
365}
366
367/*
368 * Two routines to handle adding/deleting items on the
369 * shutdown callout lists
370 *
371 * at_shutdown():
372 * Take the arguments given and put them onto the shutdown callout list.
373 * However first make sure that it's not already there.
374 * returns 0 on success.
375 */
376int
377at_shutdown(bootlist_fn function, void *arg, int position)
378{
379	sle_p ep, *epp;
380
381	switch(position) {
382	case SHUTDOWN_PRE_SYNC:
383		epp = &shutdown_list1;
384		break;
385	case SHUTDOWN_POST_SYNC:
386		epp = &shutdown_list2;
387		break;
388	default:
389		printf("bad exit callout list specified\n");
390		return (EINVAL);
391	}
392	if (rm_at_shutdown(function, arg))
393		printf("exit callout entry already present\n");
394	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
395	if (ep == NULL)
396		return (ENOMEM);
397	ep->next = *epp;
398	ep->function = function;
399	ep->arg = arg;
400	*epp = ep;
401	return (0);
402}
403
404/*
405 * Scan the exit callout lists for the given items and remove them.
406 * Returns the number of items removed.
407 */
408int
409rm_at_shutdown(bootlist_fn function, void *arg)
410{
411	sle_p *epp, ep;
412	int count;
413
414	count = 0;
415	epp = &shutdown_list1;
416	ep = *epp;
417	while (ep) {
418		if ((ep->function == function) && (ep->arg == arg)) {
419			*epp = ep->next;
420			free(ep, M_TEMP);
421			count++;
422		} else {
423			epp = &ep->next;
424		}
425		ep = *epp;
426	}
427	epp = &shutdown_list2;
428	ep = *epp;
429	while (ep) {
430		if ((ep->function == function) && (ep->arg == arg)) {
431			*epp = ep->next;
432			free(ep, M_TEMP);
433			count++;
434		} else {
435			epp = &ep->next;
436		}
437		ep = *epp;
438	}
439	return (count);
440}
441
442