2022-07-12 11:55:53 -04:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2024-01-11 14:45:02 +11:00
|
|
|
use std::process::{Command, Stdio};
|
2024-06-20 16:58:34 -04:00
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
|
|
|
|
|
use zbus::zvariant::{Array, OwnedValue};
|
2024-06-21 17:07:11 -04:00
|
|
|
use zbus::Connection;
|
2022-07-12 11:55:53 -04:00
|
|
|
|
2024-06-21 14:45:46 -04:00
|
|
|
#[cfg(feature = "systemd")]
|
|
|
|
|
use zbus_systemd::systemd1::ManagerProxy as SystemdManagerProxy;
|
|
|
|
|
|
2024-01-10 11:36:44 +11:00
|
|
|
pub async fn set_systemd_environment(key: &str, value: &str) {
|
|
|
|
|
run_optional_command(
|
|
|
|
|
"systemctl",
|
|
|
|
|
&["--user", "set-environment", &format!("{key}={value}")],
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 14:45:02 +11:00
|
|
|
pub async fn start_systemd_target() {
|
|
|
|
|
run_optional_command(
|
|
|
|
|
"systemctl",
|
|
|
|
|
&["--user", "start", "--no-block", "cosmic-session.target"],
|
|
|
|
|
)
|
2022-07-12 11:55:53 -04:00
|
|
|
}
|
2022-07-21 14:26:32 -04:00
|
|
|
|
2024-01-11 14:45:02 +11:00
|
|
|
pub fn stop_systemd_target() {
|
|
|
|
|
run_optional_command(
|
|
|
|
|
"systemctl",
|
|
|
|
|
&["--user", "stop", "--no-block", "cosmic-session.target"],
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 16:58:34 -04:00
|
|
|
///Determine if systemd is used as the init system. This should work on all linux distributions.
|
|
|
|
|
pub fn is_systemd_used() -> &'static bool {
|
|
|
|
|
static IS_SYSTEMD_USED: OnceLock<bool> = OnceLock::new();
|
|
|
|
|
IS_SYSTEMD_USED.get_or_init(
|
|
|
|
|
|| match Command::new("readlink").args(&["/sbin/init"]).output() {
|
|
|
|
|
Ok(output) => {
|
|
|
|
|
let init = String::from_utf8_lossy(&output.stdout);
|
|
|
|
|
init.trim().ends_with("/lib/systemd/systemd")
|
|
|
|
|
}
|
|
|
|
|
Err(error) => {
|
|
|
|
|
warn!("unable to check if systemd is used: {}", error);
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 18:03:30 -04:00
|
|
|
#[cfg(feature = "systemd")]
|
2024-06-20 16:58:34 -04:00
|
|
|
///Spawn a systemd scope unit with the given name and PIDs.
|
2024-06-21 17:07:11 -04:00
|
|
|
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?;
|
2024-06-21 16:41:59 -04:00
|
|
|
let pids = OwnedValue::try_from(Array::from(pids)).unwrap();
|
2024-06-21 14:45:46 -04:00
|
|
|
let properties = vec![(String::from("PIDs"), pids)];
|
2024-06-21 17:07:11 -04:00
|
|
|
if command.starts_with("/") {
|
2024-06-21 16:41:59 -04:00
|
|
|
// use the last component of the path as the unit name
|
2024-06-21 17:07:11 -04:00
|
|
|
command = command.rsplit('/').next().unwrap().to_string();
|
2024-06-21 16:41:59 -04:00
|
|
|
}
|
2024-06-21 17:07:11 -04:00
|
|
|
let scope_name = format!("{}.scope", command);
|
2024-06-20 16:58:34 -04:00
|
|
|
systemd_manager
|
2024-06-21 14:45:46 -04:00
|
|
|
.start_transient_unit(
|
|
|
|
|
scope_name.to_string(),
|
|
|
|
|
String::from("fail"),
|
|
|
|
|
properties,
|
|
|
|
|
Vec::new(),
|
|
|
|
|
)
|
2024-06-21 17:07:11 -04:00
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
2024-06-20 16:58:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 14:45:02 +11:00
|
|
|
/// run a command, but log errors instead of returning them or panicking
|
|
|
|
|
fn run_optional_command(cmd: &str, args: &[&str]) {
|
|
|
|
|
match Command::new(cmd).args(args).stdin(Stdio::null()).status() {
|
|
|
|
|
Ok(status) => {
|
|
|
|
|
if !status.success() {
|
|
|
|
|
match status.code() {
|
|
|
|
|
Some(code) => warn!("{} {}: exit code {}", cmd, args.join(" "), code),
|
|
|
|
|
None => warn!("{} {}: terminated by signal", cmd, args.join(" ")),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(error) => {
|
|
|
|
|
warn!("unable to start {} {}: {}", cmd, args.join(" "), error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-21 14:26:32 -04:00
|
|
|
}
|