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

@ -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<u32>) {
let connection = Connection::session().await.unwrap();
let systemd_manager = SystemdManagerProxy::new(&connection).await.unwrap();
pub async fn spawn_scope(mut command: String, pids: Vec<u32>) -> 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<u32>) {
properties,
Vec::new(),
)
.await
.unwrap();
.await?;
Ok(())
}
/// run a command, but log errors instead of returning them or panicking