teardown.rs (762B)
1 use std::fs::write; 2 3 use nix::sys::reboot::{RebootMode, reboot}; 4 use nix::unistd::getuid; 5 6 use kanit_common::constants::KAN_PIPE; 7 use kanit_common::error::{Context, Result, StaticError}; 8 9 pub fn teardown(cmd: &str, force: bool) -> Result<()> { 10 if !getuid().is_root() { 11 Err(StaticError("operation not permitted"))?; 12 } 13 14 if force { 15 match cmd { 16 "poweroff" => reboot(RebootMode::RB_POWER_OFF), 17 "reboot" => reboot(RebootMode::RB_AUTOBOOT), 18 "halt" => reboot(RebootMode::RB_HALT_SYSTEM), 19 "kexec" => reboot(RebootMode::RB_KEXEC), 20 _ => unreachable!(), 21 } 22 .context("Failed to reboot")?; 23 } 24 25 write(KAN_PIPE, cmd).context("failed to write to pipe")?; 26 27 Ok(()) 28 }