♻️ Use session proxy when stopping systemd target

This commit is contained in:
Lucy 2022-07-21 14:26:32 -04:00
parent c450a9ed0d
commit d13768607c
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
2 changed files with 13 additions and 3 deletions

View file

@ -42,8 +42,8 @@ async fn main() -> Result<()> {
.wrap_err("failed to start systemd target")?;
// Always stop the target when the process exits or panics.
scopeguard::defer! {
if let Ok(manager) = systemd_client::manager::build_blocking_proxy() {
manager.stop_unit("cosmic-session.target", "replace").ok();
if let Err(error) = systemd::stop_systemd_target() {
error!("failed to stop systemd target: {:?}", error);
}
}
let env_vars = env_rx

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
use color_eyre::{eyre::WrapErr, Result};
use systemd_client::manager::SystemdManagerProxy;
use systemd_client::manager::{SystemdManagerProxy, SystemdManagerProxyBlocking};
pub async fn start_systemd_target() -> Result<()> {
let connection = zbus::Connection::session().await?;
@ -14,3 +14,13 @@ pub async fn start_systemd_target() -> Result<()> {
.wrap_err("failed to start cosmic-session.target")?;
Ok(())
}
pub fn stop_systemd_target() -> Result<()> {
let connection = zbus::blocking::Connection::session().wrap_err("failed to connect to zbus")?;
let manager = SystemdManagerProxyBlocking::new(&connection)
.wrap_err("failed to connect to org.freedesktop.systemd1.Manager")?;
manager
.stop_unit("cosmic-session.target", "replace")
.wrap_err("failed to stop cosmic-session.target")?;
Ok(())
}