From 9409cae3f1888aee2c5e6ee34ef2cdec95e9cfea Mon Sep 17 00:00:00 2001 From: Joshua Ferguson Date: Fri, 21 Jun 2024 17:07:11 -0400 Subject: [PATCH] failure to create scopes should not stop session from launching --- src/main.rs | 6 ++++++ src/systemd.rs | 19 +++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index d707856..05b1bce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -421,6 +421,12 @@ async fn start_component( //spawn_scope takes a vec of pids in case we want to spawn a scope for multiple processes spawn_scope(cmd.to_string(), vec![pids]) .await + .unwrap_or_else(|err| { + warn!( + "Failed to spawn scope for {}. Creating transient unit failed with {}", + cmd, err + ); + }); } process_manager.get_pid(key).await.unwrap(); } diff --git a/src/systemd.rs b/src/systemd.rs index 4727742..f5f8f61 100644 --- a/src/systemd.rs +++ b/src/systemd.rs @@ -4,7 +4,7 @@ use std::process::{Command, Stdio}; use std::sync::OnceLock; use zbus::zvariant::{Array, OwnedValue}; -use zbus::{proxy, zvariant::Value, Connection}; +use zbus::Connection; #[cfg(feature = "systemd")] use zbus_systemd::systemd1::ManagerProxy as SystemdManagerProxy; @@ -48,17 +48,16 @@ pub fn is_systemd_used() -> &'static bool { } ///Spawn a systemd scope unit with the given name and PIDs. -pub async fn spawn_scope(mut scope_name: String, pids: Vec) { - let connection = Connection::session().await.unwrap(); - let systemd_manager = SystemdManagerProxy::new(&connection).await.unwrap(); +pub async fn spawn_scope(mut command: String, pids: Vec) -> Result<(), zbus::Error> { + let connection = Connection::session().await?; + let systemd_manager = SystemdManagerProxy::new(&connection).await?; let pids = OwnedValue::try_from(Array::from(pids)).unwrap(); let properties = vec![(String::from("PIDs"), pids)]; - if scope_name.starts_with("/") { + if command.starts_with("/") { // use the last component of the path as the unit name - scope_name = scope_name.rsplit('/').next().unwrap().to_string(); + command = command.rsplit('/').next().unwrap().to_string(); } - scope_name = format!("{}.scope", scope_name); - info!("scope name is {}", scope_name); + let scope_name = format!("{}.scope", command); systemd_manager .start_transient_unit( scope_name.to_string(), @@ -66,8 +65,8 @@ pub async fn spawn_scope(mut scope_name: String, pids: Vec) { properties, Vec::new(), ) - .await - .unwrap(); + .await?; + Ok(()) } /// run a command, but log errors instead of returning them or panicking