• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Reboot/shutdown or enable/disable Ctrl-Alt-Delete.
2 
3 use crate::errno::Errno;
4 use crate::Result;
5 use std::convert::Infallible;
6 use std::mem::drop;
7 
8 libc_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.
reboot(how: RebootMode) -> Result<Infallible>32 pub 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.
set_cad_enabled(enable: bool) -> Result<()>40 pub 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 }
49