From 669c137fec54b8acf1cff2e4471627a363849c55 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 10 Jun 2020 08:54:41 +0200 Subject: [PATCH] src/tools/systemd.rs: implement daemon_reload, start_unit, stop_unit and enable_unit --- src/tools/systemd.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/tools/systemd.rs b/src/tools/systemd.rs index 20088f80..15bb165d 100644 --- a/src/tools/systemd.rs +++ b/src/tools/systemd.rs @@ -7,6 +7,8 @@ pub mod time; use anyhow::{bail, Error}; +pub const SYSTEMCTL_BIN_PATH: &str = "/usr/bin/systemctl"; + /// Escape strings for usage in systemd unit names pub fn escape_unit(mut unit: &str, is_path: bool) -> String { @@ -72,3 +74,46 @@ pub fn unescape_unit(text: &str) -> Result { Ok(text) } + +pub fn reload_daemon() -> Result<(), Error> { + + let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH); + command.arg("daemon-reload"); + + crate::tools::run_command(command, None)?; + + Ok(()) +} + +pub fn enable_unit(unit: &str) -> Result<(), Error> { + + let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH); + command.arg("enable"); + command.arg(unit); + + crate::tools::run_command(command, None)?; + + Ok(()) +} + +pub fn start_unit(unit: &str) -> Result<(), Error> { + + let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH); + command.arg("start"); + command.arg(unit); + + crate::tools::run_command(command, None)?; + + Ok(()) +} + +pub fn stop_unit(unit: &str) -> Result<(), Error> { + + let mut command = std::process::Command::new(SYSTEMCTL_BIN_PATH); + command.arg("stop"); + command.arg(unit); + + crate::tools::run_command(command, None)?; + + Ok(()) +}