bootc_lib/
reboot.rs

1//! Handling of system restarts/reboot
2
3use std::{io::Write, process::Command};
4
5use bootc_utils::CommandRunExt;
6use fn_error_context::context;
7
8/// Initiate a system reboot.
9/// This function will only return in case of error.
10#[context("Initiating reboot")]
11pub(crate) fn reboot() -> anyhow::Result<()> {
12    // Flush output streams
13    let _ = std::io::stdout().flush();
14    let _ = std::io::stderr().flush();
15    Command::new("systemd-run")
16        .args([
17            "--quiet",
18            "--",
19            "systemctl",
20            "reboot",
21            "--message=Initiated by bootc",
22        ])
23        .run_capture_stderr()?;
24    // We expect to be terminated via SIGTERM here. We sleep
25    // instead of exiting an exit would necessarily appear
26    // racy to calling processes in that sometimes we'd
27    // win the race to exit, other times might get killed
28    // via SIGTERM.
29    tracing::debug!("Initiated reboot, sleeping");
30    loop {
31        std::thread::park();
32    }
33}