From dceb697355b6fa4b9df105a2515ea304499d75e8 Mon Sep 17 00:00:00 2001 From: SteveLauC Date: Sat, 20 Jul 2024 10:13:14 +0800 Subject: [PATCH] feat: don't run reboot with sudo on Linux with systemd (#866) --- src/steps/os/unix.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index dda0471a..dd029da4 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -724,5 +724,19 @@ pub fn run_maza(ctx: &ExecutionContext) -> Result<()> { pub fn reboot() -> Result<()> { print!("Rebooting..."); + + cfg_if::cfg_if! { + if #[cfg(target_os = "linux")] { + // Per this doc: https://www.freedesktop.org/software/systemd/man/latest/sd_booted.html + // + // If this directory exists, then this Linux uses systemd as the init program. + let systemd_dir = Path::new("/run/systemd/system"); + if let Ok(true) = systemd_dir.try_exists() { + // On Linux with systemd, `reboot` can be invoded without `sudo`. + return Command::new("reboot").status_checked(); + } + } + } + Command::new("sudo").arg("reboot").status_checked() }