From baa1d67f299385100a37e6e6dbec396c2ae60650 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Thu, 5 Nov 2020 19:08:55 +0100 Subject: [PATCH] Optimise daemon action behaviour --- sfsmcctl/src/main.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/sfsmcctl/src/main.rs b/sfsmcctl/src/main.rs index 5fa7574..fe198e9 100644 --- a/sfsmcctl/src/main.rs +++ b/sfsmcctl/src/main.rs @@ -48,7 +48,34 @@ impl<'proxy> NativeObject<'proxy> { // _ => Self::no_path("start", self) }; - proxy.method_call(iface_name("Service"), "Start", ()).await + if proxy.path != dbus::Path::new("/").unwrap() && !Self::new(proxy.connection.clone(), "/").is_running().await { + return Err(Error::new_failed("Service is not running")) + } + + match proxy.method_call(iface_name("Service"), "Start", ()).await { + Err(err) + if proxy.path == dbus::Path::new("/").unwrap() + && err.name() == Some("org.freedesktop.DBus.Error.ServiceUnknown") + => { + #[cfg(debug_assertions)] + let args = format!("start sfsmc-debug@{}", std::env::current_dir() + .map_err(|err| dbus::MethodErr::failed(&err))? + .parent() + .ok_or(Error::new_failed("called `Option::unwrap()` on a `None` value"))? + .display() + ); + #[cfg(not(debug_assertions))] + let args = "start sfsmc".to_owned(); + tokio::process::Command::new("systemctl") + .args(args.split_whitespace()) + .spawn() + .map_err(|err| dbus::MethodErr::failed(&err))? + .await + .map(|_| ()) + .map_err(|err| dbus::MethodErr::failed(&err).into()) + }, + res => res + } } pub async fn stop(&self) -> Result<(), Error> { @@ -175,7 +202,6 @@ async fn main() -> Result<(), Box> { // Get a proxy connection to the daemon let object = NativeObject::new(c, path); - // TODO: // Perform operations let res = match action { "start" => object.start().await,