From 7255c7e5a122dedd102c03a10172be68c2ffd53c Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Mon, 21 Nov 2022 12:54:32 -0800 Subject: [PATCH] Split up Linux Daemon disable and stop (#60) * Split up Linux Daemon disable and stop * Detect state and act on it * Fixup pathes * Handle linked --- .../base/configure_nix_daemon_service.rs | 90 ++++++++++++++++--- 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/src/action/base/configure_nix_daemon_service.rs b/src/action/base/configure_nix_daemon_service.rs index d239342..03dffb5 100644 --- a/src/action/base/configure_nix_daemon_service.rs +++ b/src/action/base/configure_nix_daemon_service.rs @@ -207,21 +207,52 @@ impl Action for ConfigureNixDaemonService { .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; }, _ => { - execute_command( - Command::new("systemctl") - .args(["disable", SOCKET_SRC, "--now"]) - .stdin(std::process::Stdio::null()), - ) - .await - .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; + // We separate stop and disable (instead of using `--now`) to avoid cases where the service isn't started, but is enabled. - execute_command( - Command::new("systemctl") - .args(["disable", SERVICE_SRC, "--now"]) - .stdin(std::process::Stdio::null()), - ) - .await - .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; + let socket_is_active = is_active("nix-daemon.socket").await?; + let socket_is_enabled = is_enabled("nix-daemon.socket").await?; + let service_is_active = is_active("nix-daemon.service").await?; + let service_is_enabled = is_enabled("nix-daemon.service").await?; + + if socket_is_active { + execute_command( + Command::new("systemctl") + .args(["stop", "nix-daemon.socket"]) + .stdin(std::process::Stdio::null()), + ) + .await + .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; + } + + if socket_is_enabled { + execute_command( + Command::new("systemctl") + .args(["disable", "nix-daemon.socket"]) + .stdin(std::process::Stdio::null()), + ) + .await + .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; + } + + if service_is_active { + execute_command( + Command::new("systemctl") + .args(["stop", "nix-daemon.service"]) + .stdin(std::process::Stdio::null()), + ) + .await + .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; + } + + if service_is_enabled { + execute_command( + Command::new("systemctl") + .args(["disable", "nix-daemon.service"]) + .stdin(std::process::Stdio::null()), + ) + .await + .map_err(|e| ConfigureNixDaemonServiceError::Command(e).boxed())?; + } execute_command( Command::new("systemd-tmpfiles") @@ -278,3 +309,34 @@ pub enum ConfigureNixDaemonServiceError { #[error("No supported init system found")] InitNotSupported, } + +async fn is_active(unit: &str) -> Result> { + let output = Command::new("systemctl") + .arg("is-active") + .arg(unit) + .output() + .await?; + if String::from_utf8(output.stdout)?.starts_with("active") { + tracing::trace!(%unit, "Is active"); + Ok(true) + } else { + tracing::trace!(%unit, "Is not active"); + Ok(false) + } +} + +async fn is_enabled(unit: &str) -> Result> { + let output = Command::new("systemctl") + .arg("is-enabled") + .arg(unit) + .output() + .await?; + let stdout = String::from_utf8(output.stdout)?; + if stdout.starts_with("enabled") || stdout.starts_with("linked") { + tracing::trace!(%unit, "Is enabled"); + Ok(true) + } else { + tracing::trace!(%unit, "Is not enabled"); + Ok(false) + } +}