nix/sys/
reboot.rs

1//! Reboot/shutdown or enable/disable Ctrl-Alt-Delete.
2
3use crate::Result;
4use crate::errno::Errno;
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 {
34        libc::reboot(how as libc::c_int)
35    };
36    Err(Errno::last())
37}
38
39/// Enable or disable the reboot keystroke (Ctrl-Alt-Delete).
40///
41/// Corresponds to calling `reboot(RB_ENABLE_CAD)` or `reboot(RB_DISABLE_CAD)` in C.
42pub fn set_cad_enabled(enable: bool) -> Result<()> {
43    let cmd = if enable {
44        libc::RB_ENABLE_CAD
45    } else {
46        libc::RB_DISABLE_CAD
47    };
48    let res = unsafe {
49        libc::reboot(cmd)
50    };
51    Errno::result(res).map(drop)
52}