nix/sys/
reboot.rs

1//! Reboot/shutdown or enable/disable Ctrl-Alt-Delete.
2
3use crate::errno::Errno;
4use crate::Result;
5use std::convert::Infallible;
6use std::mem::drop;
7
8libc_enum! {
9    /// How exactly should the system be rebooted.
10    ///
11    /// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
12    /// enabling/disabling Ctrl-Alt-Delete.
13    #[repr(i32)]
14    #[non_exhaustive]
15    pub enum RebootMode {
16        /// Halt the system.
17        RB_HALT_SYSTEM,
18        /// Execute a kernel that has been loaded earlier with
19        /// [`kexec_load(2)`](https://man7.org/linux/man-pages/man2/kexec_load.2.html).
20        RB_KEXEC,
21        /// Stop the system and switch off power, if possible.
22        RB_POWER_OFF,
23        /// Restart the system.
24        RB_AUTOBOOT,
25        // we do not support Restart2.
26        /// Suspend the system using software suspend.
27        RB_SW_SUSPEND,
28    }
29}
30
31/// Reboots or shuts down the system.
32pub fn reboot(how: RebootMode) -> Result<Infallible> {
33    unsafe { libc::reboot(how as libc::c_int) };
34    Err(Errno::last())
35}
36
37/// Enable or disable the reboot keystroke (Ctrl-Alt-Delete).
38///
39/// Corresponds to calling `reboot(RB_ENABLE_CAD)` or `reboot(RB_DISABLE_CAD)` in C.
40pub fn set_cad_enabled(enable: bool) -> Result<()> {
41    let cmd = if enable {
42        libc::RB_ENABLE_CAD
43    } else {
44        libc::RB_DISABLE_CAD
45    };
46    let res = unsafe { libc::reboot(cmd) };
47    Errno::result(res).map(drop)
48}