failure to create scopes should not stop session from launching

This commit is contained in:
Joshua Ferguson 2024-06-21 17:07:11 -04:00 committed by Ashley Wulber
parent d6741d5786
commit 9409cae3f1
2 changed files with 15 additions and 10 deletions

View file

@ -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 takes a vec of pids in case we want to spawn a scope for multiple processes
spawn_scope(cmd.to_string(), vec![pids]) spawn_scope(cmd.to_string(), vec![pids])
.await .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(); process_manager.get_pid(key).await.unwrap();
} }

View file

@ -4,7 +4,7 @@ use std::process::{Command, Stdio};
use std::sync::OnceLock; use std::sync::OnceLock;
use zbus::zvariant::{Array, OwnedValue}; use zbus::zvariant::{Array, OwnedValue};
use zbus::{proxy, zvariant::Value, Connection}; use zbus::Connection;
#[cfg(feature = "systemd")] #[cfg(feature = "systemd")]
use zbus_systemd::systemd1::ManagerProxy as SystemdManagerProxy; 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. ///Spawn a systemd scope unit with the given name and PIDs.
pub async fn spawn_scope(mut scope_name: String, pids: Vec<u32>) { pub async fn spawn_scope(mut command: String, pids: Vec<u32>) -> Result<(), zbus::Error> {
let connection = Connection::session().await.unwrap(); let connection = Connection::session().await?;
let systemd_manager = SystemdManagerProxy::new(&connection).await.unwrap(); let systemd_manager = SystemdManagerProxy::new(&connection).await?;
let pids = OwnedValue::try_from(Array::from(pids)).unwrap(); let pids = OwnedValue::try_from(Array::from(pids)).unwrap();
let properties = vec![(String::from("PIDs"), pids)]; 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 // 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); let scope_name = format!("{}.scope", command);
info!("scope name is {}", scope_name);
systemd_manager systemd_manager
.start_transient_unit( .start_transient_unit(
scope_name.to_string(), scope_name.to_string(),
@ -66,8 +65,8 @@ pub async fn spawn_scope(mut scope_name: String, pids: Vec<u32>) {
properties, properties,
Vec::new(), Vec::new(),
) )
.await .await?;
.unwrap(); Ok(())
} }
/// run a command, but log errors instead of returning them or panicking /// run a command, but log errors instead of returning them or panicking