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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include "opt_panic.h"
43 #include "opt_sched.h"
44 #include "opt_watchdog.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
52 #include <sys/eventhandler.h>
53 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/kerneldump.h>
58 #include <sys/kthread.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
64 #include <sys/reboot.h>
65 #include <sys/resourcevar.h>
66 #include <sys/rwlock.h>
67 #include <sys/sched.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysproto.h>
71 #include <sys/vnode.h>
72 #include <sys/watchdog.h>
76 #include <machine/cpu.h>
77 #include <machine/pcb.h>
78 #include <machine/smp.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pager.h>
86 #include <vm/swap_pager.h>
88 #include <sys/signalvar.h>
90 #ifndef PANIC_REBOOT_WAIT_TIME
91 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
93 static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME;
94 SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RW | CTLFLAG_TUN,
95 &panic_reboot_wait_time, 0,
96 "Seconds to wait before rebooting after a panic");
97 TUNABLE_INT("kern.panic_reboot_wait_time", &panic_reboot_wait_time);
100 * Note that stdarg.h and the ANSI style va_start macro is used for both
101 * ANSI and traditional C compilers.
103 #include <machine/stdarg.h>
106 #ifdef KDB_UNATTENDED
107 int debugger_on_panic = 0;
109 int debugger_on_panic = 1;
111 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
112 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
113 &debugger_on_panic, 0, "Run debugger on kernel panic");
114 TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
117 static int trace_on_panic = 1;
119 static int trace_on_panic = 0;
121 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic,
122 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN,
123 &trace_on_panic, 0, "Print stack trace on kernel panic");
124 TUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
127 static int sync_on_panic = 0;
128 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
129 &sync_on_panic, 0, "Do a sync before rebooting from a panic");
130 TUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
132 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0,
133 "Shutdown environment");
136 static int show_busybufs;
138 static int show_busybufs = 1;
140 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
141 &show_busybufs, 0, "");
143 int suspend_blocked = 0;
144 SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW,
145 &suspend_blocked, 0, "Block suspend due to a pending shutdown");
148 * Variable panicstr contains argument to first call to panic; used as flag
149 * to indicate that the kernel has already called panic.
151 const char *panicstr;
153 int dumping; /* system is dumping */
154 int rebooting; /* system is rebooting */
155 static struct dumperinfo dumper; /* our selected dumper */
157 /* Context information for dump-debuggers. */
158 static struct pcb dumppcb; /* Registers. */
159 lwpid_t dumptid; /* Thread ID. */
161 static struct cdevsw reroot_cdevsw = {
162 .d_version = D_VERSION,
166 static void poweroff_wait(void *, int);
167 static void shutdown_halt(void *junk, int howto);
168 static void shutdown_panic(void *junk, int howto);
169 static void shutdown_reset(void *junk, int howto);
170 static int kern_reroot(void);
172 /* register various local shutdown events */
174 shutdown_conf(void *unused)
177 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
179 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
180 SHUTDOWN_PRI_LAST + 100);
181 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
182 SHUTDOWN_PRI_LAST + 100);
183 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
184 SHUTDOWN_PRI_LAST + 200);
187 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
190 * The only reason this exists is to create the /dev/reroot/ directory,
191 * used by reroot code in init(8) as a mountpoint for tmpfs.
194 reroot_conf(void *unused)
199 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &cdev,
200 &reroot_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "reroot/reroot");
202 printf("%s: failed to create device node, error %d",
207 SYSINIT(reroot_conf, SI_SUB_DEVFS, SI_ORDER_ANY, reroot_conf, NULL);
210 * The system call that results in a reboot.
214 sys_reboot(struct thread *td, struct reboot_args *uap)
220 error = mac_system_check_reboot(td->td_ucred, uap->opt);
223 error = priv_check(td, PRIV_REBOOT);
225 if (uap->opt & RB_REROOT) {
226 error = kern_reroot();
229 kern_reboot(uap->opt);
237 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC
239 static int shutdown_howto = 0;
242 shutdown_nice(int howto)
245 shutdown_howto = howto;
247 /* Send a signal to init(8) and have it shutdown the world */
248 if (initproc != NULL) {
250 kern_psignal(initproc, SIGINT);
251 PROC_UNLOCK(initproc);
253 /* No init(8) running, so simply reboot */
254 kern_reboot(RB_NOSYNC);
258 static int waittime = -1;
269 if (ts.tv_sec >= 86400) {
270 printf("%ldd", (long)ts.tv_sec / 86400);
274 if (f || ts.tv_sec >= 3600) {
275 printf("%ldh", (long)ts.tv_sec / 3600);
279 if (f || ts.tv_sec >= 60) {
280 printf("%ldm", (long)ts.tv_sec / 60);
284 printf("%lds\n", (long)ts.tv_sec);
288 doadump(boolean_t textdump)
294 if (dumper.dumper == NULL)
298 dumptid = curthread->td_tid;
303 if (textdump && textdump_pending) {
305 textdump_dumpsys(&dumper);
316 isbufbusy(struct buf *bp)
318 if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
320 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
326 * Shutdown the system cleanly to prepare for reboot, halt, or power off.
329 kern_reboot(int howto)
331 static int first_buf_printf = 1;
335 * Bind us to CPU 0 so that all shutdown code runs there. Some
336 * systems don't shutdown properly (i.e., ACPI power off) if we
337 * run on another processor.
339 if (!SCHEDULER_STOPPED()) {
340 thread_lock(curthread);
341 sched_bind(curthread, 0);
342 thread_unlock(curthread);
343 KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
346 /* We're in the process of rebooting. */
349 /* collect extra flags that shutdown_nice might have set */
350 howto |= shutdown_howto;
352 /* We are out of the debugger now. */
356 * Do any callouts that should be done BEFORE syncing the filesystems.
358 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
361 * Now sync filesystems
363 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
364 register struct buf *bp;
365 int iter, nbusy, pbusy;
372 wdog_kern_pat(WD_LASTVAL);
373 sys_sync(curthread, NULL);
376 * With soft updates, some buffers that are
377 * written will be remarked as dirty until other
378 * buffers are written.
380 for (iter = pbusy = 0; iter < 20; iter++) {
382 for (bp = &buf[nbuf]; --bp >= buf; )
386 if (first_buf_printf)
387 printf("All buffers synced.");
390 if (first_buf_printf) {
391 printf("Syncing disks, buffers remaining... ");
392 first_buf_printf = 0;
394 printf("%d ", nbusy);
399 wdog_kern_pat(WD_LASTVAL);
400 sys_sync(curthread, NULL);
404 * Drop Giant and spin for a while to allow
405 * interrupt threads to run.
412 * Drop Giant and context switch several times to
413 * allow interrupt threads to run.
416 for (subiter = 0; subiter < 50 * iter; subiter++) {
417 thread_lock(curthread);
418 mi_switch(SW_VOL, NULL);
419 thread_unlock(curthread);
427 * Count only busy local buffers to prevent forcing
428 * a fsck if we're just a client of a wedged NFS server
431 for (bp = &buf[nbuf]; --bp >= buf; ) {
434 /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */
435 if (bp->b_dev == NULL) {
436 TAILQ_REMOVE(&mountlist,
437 bp->b_vp->v_mount, mnt_list);
442 if (show_busybufs > 0) {
444 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
445 nbusy, bp, bp->b_vp, bp->b_flags,
446 (intmax_t)bp->b_blkno,
447 (intmax_t)bp->b_lblkno);
448 BUF_LOCKPRINTINFO(bp);
449 if (show_busybufs > 1)
457 * Failed to sync all blocks. Indicate this and don't
458 * unmount filesystems (thus forcing an fsck on reboot).
460 printf("Giving up on %d buffers\n", nbusy);
461 DELAY(5000000); /* 5 seconds */
463 if (!first_buf_printf)
464 printf("Final sync complete\n");
466 * Unmount filesystems
472 DELAY(100000); /* wait for console output to finish */
480 * Ok, now do things that assume all filesystem activity has
483 EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
485 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping)
488 /* Now that we're going to really halt the system... */
489 EVENTHANDLER_INVOKE(shutdown_final, howto);
491 for(;;) ; /* safety against shutdown_reset not working */
496 * The system call that results in changing the rootfs.
501 struct vnode *oldrootvnode, *vp;
502 struct mount *mp, *devmp;
505 if (curproc != initproc)
509 * Mark the filesystem containing currently-running executable
510 * (the temporary copy of init(8)) busy.
512 vp = curproc->p_textvp;
513 error = vn_lock(vp, LK_SHARED);
517 error = vfs_busy(mp, MBF_NOWAIT);
521 error = vfs_busy(mp, 0);
522 vn_lock(vp, LK_SHARED | LK_RETRY);
528 if (vp->v_iflag & VI_DOOMED) {
537 * Remove the filesystem containing currently-running executable
538 * from the mount list, to prevent it from being unmounted
539 * by vfs_unmountall(), and to avoid confusing vfs_mountroot().
541 * Also preserve /dev - forcibly unmounting it could cause driver
549 mtx_lock(&mountlist_mtx);
550 TAILQ_REMOVE(&mountlist, mp, mnt_list);
551 TAILQ_REMOVE(&mountlist, devmp, mnt_list);
552 mtx_unlock(&mountlist_mtx);
554 oldrootvnode = rootvnode;
557 * Unmount everything except for the two filesystems preserved above.
562 * Add /dev back; vfs_mountroot() will move it into its new place.
564 mtx_lock(&mountlist_mtx);
565 TAILQ_INSERT_HEAD(&mountlist, devmp, mnt_list);
566 mtx_unlock(&mountlist_mtx);
571 * Mount the new rootfs.
576 * Update all references to the old rootvnode.
578 mountcheckdirs(oldrootvnode, rootvnode);
581 * Add the temporary filesystem back and unbusy it.
583 mtx_lock(&mountlist_mtx);
584 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
585 mtx_unlock(&mountlist_mtx);
592 * If the shutdown was a clean halt, behave accordingly.
595 shutdown_halt(void *junk, int howto)
598 if (howto & RB_HALT) {
600 printf("The operating system has halted.\n");
601 printf("Please press any key to reboot.\n\n");
603 case -1: /* No console, just die */
614 * Check to see if the system paniced, pause and then reboot
615 * according to the specified delay.
618 shutdown_panic(void *junk, int howto)
622 if (howto & RB_DUMP) {
623 if (panic_reboot_wait_time != 0) {
624 if (panic_reboot_wait_time != -1) {
625 printf("Automatic reboot in %d seconds - "
626 "press a key on the console to abort\n",
627 panic_reboot_wait_time);
628 for (loop = panic_reboot_wait_time * 10;
630 DELAY(1000 * 100); /* 1/10th second */
631 /* Did user type a key? */
632 if (cncheckc() != -1)
638 } else { /* zero time specified - reboot NOW */
641 printf("--> Press a key on the console to reboot,\n");
642 printf("--> or switch off the system now.\n");
648 * Everything done, now reset
651 shutdown_reset(void *junk, int howto)
654 printf("Rebooting...\n");
655 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
658 * Acquiring smp_ipi_mtx here has a double effect:
659 * - it disables interrupts avoiding CPU0 preemption
660 * by fast handlers (thus deadlocking against other CPUs)
661 * - it avoids deadlocks against smp_rendezvous() or, more
662 * generally, threads busy-waiting, with this spinlock held,
663 * and waiting for responses by threads on other CPUs
664 * (ie. smp_tlb_shootdown()).
666 * For the !SMP case it just needs to handle the former problem.
669 mtx_lock_spin(&smp_ipi_mtx);
674 /* cpu_boot(howto); */ /* doesn't do anything at the moment */
676 /* NOTREACHED */ /* assuming reset worked */
679 #if defined(WITNESS) || defined(INVARIANTS)
680 static int kassert_warn_only = 0;
682 static int kassert_do_kdb = 0;
685 static int kassert_do_ktr = 0;
687 static int kassert_do_log = 1;
688 static int kassert_log_pps_limit = 4;
689 static int kassert_log_mute_at = 0;
690 static int kassert_log_panic_at = 0;
691 static int kassert_warnings = 0;
693 SYSCTL_NODE(_debug, OID_AUTO, kassert, CTLFLAG_RW, NULL, "kassert options");
695 SYSCTL_INT(_debug_kassert, OID_AUTO, warn_only, CTLFLAG_RW | CTLFLAG_TUN,
696 &kassert_warn_only, 0,
697 "KASSERT triggers a panic (1) or just a warning (0)");
698 TUNABLE_INT("debug.kassert.warn_only", &kassert_warn_only);
701 SYSCTL_INT(_debug_kassert, OID_AUTO, do_kdb, CTLFLAG_RW | CTLFLAG_TUN,
702 &kassert_do_kdb, 0, "KASSERT will enter the debugger");
703 TUNABLE_INT("debug.kassert.do_kdb", &kassert_do_kdb);
707 SYSCTL_UINT(_debug_kassert, OID_AUTO, do_ktr, CTLFLAG_RW | CTLFLAG_TUN,
709 "KASSERT does a KTR, set this to the KTRMASK you want");
710 TUNABLE_INT("debug.kassert.do_ktr", &kassert_do_ktr);
713 SYSCTL_INT(_debug_kassert, OID_AUTO, do_log, CTLFLAG_RW | CTLFLAG_TUN,
714 &kassert_do_log, 0, "KASSERT triggers a panic (1) or just a warning (0)");
715 TUNABLE_INT("debug.kassert.do_log", &kassert_do_log);
717 SYSCTL_INT(_debug_kassert, OID_AUTO, warnings, CTLFLAG_RW | CTLFLAG_TUN,
718 &kassert_warnings, 0, "number of KASSERTs that have been triggered");
719 TUNABLE_INT("debug.kassert.warnings", &kassert_warnings);
721 SYSCTL_INT(_debug_kassert, OID_AUTO, log_panic_at, CTLFLAG_RW | CTLFLAG_TUN,
722 &kassert_log_panic_at, 0, "max number of KASSERTS before we will panic");
723 TUNABLE_INT("debug.kassert.log_panic_at", &kassert_log_panic_at);
725 SYSCTL_INT(_debug_kassert, OID_AUTO, log_pps_limit, CTLFLAG_RW | CTLFLAG_TUN,
726 &kassert_log_pps_limit, 0, "limit number of log messages per second");
727 TUNABLE_INT("debug.kassert.log_pps_limit", &kassert_log_pps_limit);
729 SYSCTL_INT(_debug_kassert, OID_AUTO, log_mute_at, CTLFLAG_RW | CTLFLAG_TUN,
730 &kassert_log_mute_at, 0, "max number of KASSERTS to log");
731 TUNABLE_INT("debug.kassert.log_mute_at", &kassert_log_mute_at);
733 static int kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS);
735 SYSCTL_PROC(_debug_kassert, OID_AUTO, kassert,
736 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, NULL, 0,
737 kassert_sysctl_kassert, "I", "set to trigger a test kassert");
740 kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS)
744 error = sysctl_wire_old_buffer(req, sizeof(int));
747 error = sysctl_handle_int(oidp, &i, 0, req);
749 if (error != 0 || req->newptr == NULL)
751 KASSERT(0, ("kassert_sysctl_kassert triggered kassert %d", i));
756 * Called by KASSERT, this decides if we will panic
757 * or if we will log via printf and/or ktr.
760 kassert_panic(const char *fmt, ...)
762 static char buf[256];
766 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
770 * panic if we're not just warning, or if we've exceeded
771 * kassert_log_panic_at warnings.
773 if (!kassert_warn_only ||
774 (kassert_log_panic_at > 0 &&
775 kassert_warnings >= kassert_log_panic_at)) {
785 * log if we've not yet met the mute limit.
787 if (kassert_do_log &&
788 (kassert_log_mute_at == 0 ||
789 kassert_warnings < kassert_log_mute_at)) {
790 static struct timeval lasterr;
793 if (ppsratecheck(&lasterr, &curerr, kassert_log_pps_limit)) {
794 printf("KASSERT failed: %s\n", buf);
799 if (kassert_do_kdb) {
800 kdb_enter(KDB_WHY_KASSERT, buf);
803 atomic_add_int(&kassert_warnings, 1);
808 * Panic is called on unresolvable fatal errors. It prints "panic: mesg",
809 * and then reboots. If we are called twice, then we avoid trying to sync
810 * the disks as this often leads to recursive panics.
813 panic(const char *fmt, ...)
822 vpanic(const char *fmt, va_list ap)
827 struct thread *td = curthread;
828 int bootopt, newpanic;
829 static char buf[256];
835 * stop_cpus_hard(other_cpus) should prevent multiple CPUs from
836 * concurrently entering panic. Only the winner will proceed
839 if (panicstr == NULL && !kdb_active) {
840 other_cpus = all_cpus;
841 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
842 stop_cpus_hard(other_cpus);
846 * We set stop_scheduler here and not in the block above,
847 * because we want to ensure that if panic has been called and
848 * stop_scheduler_on_panic is true, then stop_scheduler will
849 * always be set. Even if panic has been entered from kdb.
851 td->td_stopsched = 1;
854 bootopt = RB_AUTOBOOT;
857 bootopt |= RB_NOSYNC;
865 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
868 printf("panic: %s\n", buf);
875 printf("cpuid = %d\n", PCPU_GET(cpuid));
879 if (newpanic && trace_on_panic)
881 if (debugger_on_panic)
882 kdb_enter(KDB_WHY_PANIC, "panic");
884 /*thread_lock(td); */
885 td->td_flags |= TDF_INPANIC;
886 /* thread_unlock(td); */
888 bootopt |= RB_NOSYNC;
889 kern_reboot(bootopt);
893 * Support for poweroff delay.
895 * Please note that setting this delay too short might power off your machine
896 * before the write cache on your hard disk has been flushed, leading to
897 * soft-updates inconsistencies.
899 #ifndef POWEROFF_DELAY
900 # define POWEROFF_DELAY 5000
902 static int poweroff_delay = POWEROFF_DELAY;
904 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
905 &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
908 poweroff_wait(void *junk, int howto)
911 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
913 DELAY(poweroff_delay * 1000);
917 * Some system processes (e.g. syncer) need to be stopped at appropriate
918 * points in their main loops prior to a system shutdown, so that they
919 * won't interfere with the shutdown process (e.g. by holding a disk buf
920 * to cause sync to fail). For each of these system processes, register
921 * shutdown_kproc() as a handler for one of shutdown events.
923 static int kproc_shutdown_wait = 60;
924 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
925 &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
928 kproc_shutdown(void *arg, int howto)
936 p = (struct proc *)arg;
937 printf("Waiting (max %d seconds) for system process `%s' to stop...",
938 kproc_shutdown_wait, p->p_comm);
939 error = kproc_suspend(p, kproc_shutdown_wait * hz);
941 if (error == EWOULDBLOCK)
942 printf("timed out\n");
948 kthread_shutdown(void *arg, int howto)
956 td = (struct thread *)arg;
957 printf("Waiting (max %d seconds) for system thread `%s' to stop...",
958 kproc_shutdown_wait, td->td_name);
959 error = kthread_suspend(td, kproc_shutdown_wait * hz);
961 if (error == EWOULDBLOCK)
962 printf("timed out\n");
967 static char dumpdevname[sizeof(((struct cdev*)NULL)->si_name)];
968 SYSCTL_STRING(_kern_shutdown, OID_AUTO, dumpdevname, CTLFLAG_RD,
969 dumpdevname, 0, "Device for kernel dumps");
971 /* Registration of dumpers */
973 set_dumper(struct dumperinfo *di, const char *devname, struct thread *td)
978 error = priv_check(td, PRIV_SETDUMPER);
983 bzero(&dumper, sizeof dumper);
984 dumpdevname[0] = '\0';
987 if (dumper.dumper != NULL)
990 wantcopy = strlcpy(dumpdevname, devname, sizeof(dumpdevname));
991 if (wantcopy >= sizeof(dumpdevname)) {
992 printf("set_dumper: device name truncated from '%s' -> '%s'\n",
993 devname, dumpdevname);
998 /* Call dumper with bounds checking. */
1000 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
1001 off_t offset, size_t length)
1004 if (length != 0 && (offset < di->mediaoffset ||
1005 offset - di->mediaoffset + length > di->mediasize)) {
1006 printf("Attempt to write outside dump device boundaries.\n"
1007 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
1008 (intmax_t)offset, (intmax_t)di->mediaoffset,
1009 (uintmax_t)length, (intmax_t)di->mediasize);
1012 return (di->dumper(di->priv, virtual, physical, offset, length));
1016 mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
1017 uint64_t dumplen, uint32_t blksz)
1020 bzero(kdh, sizeof(*kdh));
1021 strlcpy(kdh->magic, magic, sizeof(kdh->magic));
1022 strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
1023 kdh->version = htod32(KERNELDUMPVERSION);
1024 kdh->architectureversion = htod32(archver);
1025 kdh->dumplength = htod64(dumplen);
1026 kdh->dumptime = htod64(time_second);
1027 kdh->blocksize = htod32(blksz);
1028 strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
1029 strlcpy(kdh->versionstring, version, sizeof(kdh->versionstring));
1030 if (panicstr != NULL)
1031 strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
1032 kdh->parity = kerneldump_parity(kdh);
1036 DB_SHOW_COMMAND(panic, db_show_panic)
1039 if (panicstr == NULL)
1040 db_printf("panicstr not set\n");
1042 db_printf("panic: %s\n", panicstr);