Main index | Section 9 | Options |
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/reboot.h>extern int rebooting;
#include <sys/eventhandler.h>
The relevant flags are:
RB_HALT | Halt the system in-place rather than restarting. |
RB_POWEROFF | Power down the system rather than restarting. |
RB_POWERCYCLE | Request a power-cycle in addition to restarting. |
RB_NOSYNC | Do not sync filesystems during shutdown. |
RB_DUMP | Dump kernel memory during shutdown. |
The howto field, and its full list of flags are described in additional detail by reboot(2).
kern_reboot() performs the following actions:
kern_reboot() may be called from a typical kernel execution context, when the system is running normally. It may also be called as the final step of a kernel panic, or from the kernel debugger. Therefore, the code in this function is subject to restrictions described by the EXECUTION CONTEXT section of the panic(9) man page.
The shutdown_nice() function is the intended path for performing a clean reboot or shutdown when the system is operating under normal conditions. Calling this function will send a signal to the init(8) process, instructing it to perform a shutdown. When init(8) has cleanly terminated its children, it will perform the reboot(2) system call, which in turn calls kern_reboot().
If shutdown_nice() is called before the init(8) process has been spawned, or if the system has panicked or otherwise halted, kern_reboot() will be called directly.
The shutdown_pre_sync event is invoked before syncing filesystems to disk. It enables any action or state transition that must happen before this point to take place.
The shutdown_post_sync event is invoked at the point immediately after the filesystem sync has finished. It enables, for example, disk drivers to complete the sync by flushing their cache to disk. Note that this event still takes place before the optional kernel core dump.
The shutdown_final event is invoked as the very last step of kern_reboot(). Drivers and subsystems such as acpi(4) can register handlers to this event that will perform the actual reboot, power-off, or halt.
Notably, the shutdown_final event is also the point at which all kernel modules will have their shutdown ( MOD_SHUTDOWN ) hooks executed, and when the DEVICE_SHUTDOWN(9) method will be executed recursively on all devices.
All event handlers, like kern_reboot() itself, may be run in either normal shutdown context or a kernel panic or debugger context. Handler functions are expected to take care not to trigger recursive panics.
The shutdown_nice() function will usually return to its caller, having initiated the asynchronous system shutdown. It will not return when called from a panic or debugger context, or during early boot.
void foo_poweroff_handler(struct void *arg, int howto) { struct foo_softc *sc = arg; uint32_t reg;if ((howto & RB_POWEROFF) != 0) { reg = FOO_POWEROFF; WRITE4(sc, FOO_POWEROFF_REG, reg); } }
The handler is then registered in the device attach routine:
int foo_attach(device_t dev) { struct foo_softc *sc;...
/* Pass the device's software context as the private arg. */ EVENTHANDLER_REGISTER(shutdown_final, foo_poweroff_handler, sc, SHUTDOWN_PRI_DEFAULT);
... }
This shutdown_final handler uses the RB_NOSYNC flag to detect that a panic or other unusual condition has occurred, and returns early:
void bar_shutdown_final(struct void *arg, int howto) {if ((howto & RB_NOSYNC) != 0) return;
/* Some code that is not panic-safe. */ ... }
REBOOT (9) | March 20, 2023 |
Main index | Section 9 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | How do you pronounce UNIX ? You Nix ! | ” |