Skip to main content

remowt_systemd/
lib.rs

1use bifrostlink::declarative::endpoints;
2use bifrostlink::Config;
3use serde::{Deserialize, Serialize};
4use zbus::proxy;
5use zbus::zvariant::OwnedObjectPath;
6
7pub struct Systemd;
8
9#[derive(Serialize, Deserialize, Debug, thiserror::Error)]
10pub enum Error {
11	#[error("systemd request failed: {0}")]
12	Failed(String),
13}
14
15#[proxy(
16	interface = "org.freedesktop.systemd1.Manager",
17	default_service = "org.freedesktop.systemd1",
18	default_path = "/org/freedesktop/systemd1"
19)]
20trait Manager {
21	fn start_unit(&self, name: &str, mode: &str) -> zbus::Result<OwnedObjectPath>;
22	fn stop_unit(&self, name: &str, mode: &str) -> zbus::Result<OwnedObjectPath>;
23}
24
25async fn manager() -> Result<ManagerProxy<'static>, Error> {
26	let conn = zbus::Connection::system()
27		.await
28		.map_err(|e| Error::Failed(e.to_string()))?;
29	ManagerProxy::new(&conn)
30		.await
31		.map_err(|e| Error::Failed(e.to_string()))
32}
33
34#[endpoints(ns = 5)]
35impl Systemd {
36	#[endpoints(id = 1)]
37	async fn start(&self, unit: String) -> Result<(), Error> {
38		manager()
39			.await?
40			.start_unit(&unit, "replace")
41			.await
42			.map_err(|e| Error::Failed(e.to_string()))?;
43		Ok(())
44	}
45	#[endpoints(id = 2)]
46	async fn stop(&self, unit: String) -> Result<(), Error> {
47		manager()
48			.await?
49			.stop_unit(&unit, "replace")
50			.await
51			.map_err(|e| Error::Failed(e.to_string()))?;
52		Ok(())
53	}
54}