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