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};
|
2022-07-12 11:55:53 -04:00
|
|
|
|
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"],
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|