sysd_manager_comcontroler/sysdbus/
to_proxy.rs1#![allow(dead_code)]
2use base::{
3 enums::UnitDBusLevel,
4 proxy::{DisEnAbleUnitFiles, DisEnAbleUnitFilesResponse},
5};
6use futures_util::stream::StreamExt;
7use log::{info, warn};
8use tokio::time::{Duration, timeout};
9use zbus::proxy;
10
11use crate::{
12 errors::SystemdErrors,
13 sysdbus::{get_blocking_connection, get_connection},
14};
15
16#[proxy(
17 interface = "io.github.plrigaux.SysDManager",
18 default_service = "io.github.plrigaux.SysDManager",
19 default_path = "/io/github/plrigaux/SysDManager"
20)]
21pub trait SysDManagerComLink {
22 fn clean_unit(&self, unit_name: &str, what: &[&str]) -> zbus::Result<()>;
23 fn freeze_unit(&self, unit_name: &str) -> zbus::fdo::Result<()>;
24 fn thaw_unit(&self, unit_name: &str) -> zbus::fdo::Result<()>;
25 fn reload(&self) -> zbus::fdo::Result<()>;
26
27 fn create_drop_in(
28 &mut self,
29 runtime: bool,
30 unit_name: &str,
31 file_name: &str,
32 content: &str,
33 ) -> zbus::fdo::Result<()>;
34 fn save_file(&mut self, file_name: &str, content: &str) -> zbus::fdo::Result<u64>;
35
36 fn revert_unit_files(&self, file_names: &[&str]) -> zbus::fdo::Result<Vec<DisEnAbleUnitFiles>>;
37
38 fn enable_unit_files_with_flags(
39 &mut self,
40 files: &[&str],
41 flags: u64,
42 ) -> zbus::fdo::Result<DisEnAbleUnitFilesResponse>;
43
44 fn disable_unit_files_with_flags(
45 &mut self,
46 files: &[&str],
47 flags: u64,
48 ) -> zbus::fdo::Result<DisEnAbleUnitFilesResponse>;
49
50 #[zbus(signal)]
51 fn hello(msg: String) -> zbus::fdo::Result<()>;
52}
53
54fn ensure_proxy_up() {
58 }
60
61fn get_proxy<'a>() -> Result<SysDManagerComLinkProxyBlocking<'a>, SystemdErrors> {
62 let destination = super::RUN_CONTEXT
63 .get()
64 .expect("Supposed to be init")
65 .destination_address();
66 let connection = get_blocking_connection(UnitDBusLevel::System)?;
67 let proxy = SysDManagerComLinkProxyBlocking::builder(&connection)
68 .destination(destination)?
70 .build()?;
71
72 Ok(proxy)
73}
74
75async fn get_proxy_async<'a>() -> Result<SysDManagerComLinkProxy<'a>, SystemdErrors> {
76 let destination = super::RUN_CONTEXT
77 .get()
78 .expect("Supposed to be init")
79 .destination_address();
80 let connection = get_connection(UnitDBusLevel::System).await?;
81 let proxy = SysDManagerComLinkProxy::builder(&connection)
82 .destination(destination)?
84 .build()
85 .await?;
86
87 Ok(proxy)
88}
89
90pub fn clean_unit(unit_name: &str, what: &[&str]) -> Result<(), SystemdErrors> {
91 let proxy = get_proxy()?;
92
93 proxy.clean_unit(unit_name, what)?;
94 Ok(())
95}
96
97pub fn freeze_unit(unit_name: &str) -> Result<(), SystemdErrors> {
98 let proxy = get_proxy()?;
99 proxy.freeze_unit(unit_name)?;
100 Ok(())
101}
102
103pub fn thaw_unit(unit_name: &str) -> Result<(), SystemdErrors> {
104 let proxy: SysDManagerComLinkProxyBlocking<'_> = get_proxy()?;
105 proxy.thaw_unit(unit_name)?;
106 Ok(())
107}
108
109pub async fn reload() -> Result<(), SystemdErrors> {
110 let proxy = get_proxy_async().await?;
111 proxy.reload().await?;
112 Ok(())
113}
114
115fn extract_job_id(job: &str) -> Option<u32> {
116 job.rsplit_once('/')
117 .and_then(|(_, id)| id.parse::<u32>().ok())
118}
119
120pub async fn lazy_start_proxy_async() -> Result<(), SystemdErrors> {
121 let proxy = get_proxy_async().await?;
122 let hello_stream = proxy.receive_hello().await?;
123 crate::sysdbus::init_proxy_async2().await?;
124
125 let timeout_results = timeout(Duration::from_secs(2), wait_hello(hello_stream)).await;
126
127 match timeout_results {
128 Ok(rr) => rr?,
129 Err(elapsed) => warn!("Proxy start time up : {}", elapsed),
130 }
131 Ok(())
132}
133
134async fn wait_hello(mut hello_stream: HelloStream) -> Result<(), SystemdErrors> {
135 if let Some(msg) = hello_stream.next().await {
136 let args = msg.args()?;
137 info!("Hello Proxy Args {:?}", args);
138 }
139 Ok(())
140}
141
142pub fn lazy_start_proxy_block() -> Result<(), SystemdErrors> {
143 crate::runtime().block_on(async move {
144 warn!("lazy 1");
145 lazy_start_proxy_async().await;
146 warn!("lazy 2");
147 });
148 Ok(())
149}
150
151#[macro_export]
152macro_rules! proxy_call {
153 ($f:ident,$($p:expr),+) => {
154 match $crate::to_proxy::$f($($p),+) {
155 Ok(ok) => Ok(ok),
156 Err(SystemdErrors::ZFdoServiceUnknowm(s)) => {
157 warn!("ServiceUnkown: {}", s);
158 $crate::to_proxy::lazy_start_proxy_block();
159
160 $crate::to_proxy::$f($($p),+)
161 },
162 Err(err) => Err(err)
163 }
164 }
165}
166
167#[macro_export]
168macro_rules! proxy_call_async {
169 ($f:ident) => {
170 proxy_call_async!($f,)
171 };
172
173 ($f:ident, $($p:expr),*) => {
174 match $crate::to_proxy::$f($($p),*).await {
175 Ok(ok) => Ok(ok),
176 Err(SystemdErrors::ZFdoServiceUnknowm(s)) => {
177 warn!("ServiceUnkown: {}", s);
178 $crate::to_proxy::lazy_start_proxy_async();
179
180 $crate::to_proxy::$f($($p),*).await
181 },
182 Err(err) => Err(err)
183 }
184 }
185}
186
187pub(crate) async fn create_drop_in(
188 runtime: bool,
189 unit_name: &str,
190 file_name: &str,
191 content: &str,
192) -> Result<(), SystemdErrors> {
193 let mut proxy = get_proxy_async().await?;
194 proxy
195 .create_drop_in(runtime, unit_name, file_name, content)
196 .await
197 .map_err(|e| e.into())
198}
199
200pub async fn save_file(file_path: &str, content: &str) -> Result<u64, SystemdErrors> {
201 let mut proxy = get_proxy_async().await?;
202 proxy
203 .save_file(file_path, content)
204 .await
205 .map_err(|e| e.into())
206}
207
208pub async fn revert_unit_files(
209 unit_names: &[&str],
210) -> Result<Vec<DisEnAbleUnitFiles>, SystemdErrors> {
211 let proxy = get_proxy_async().await?;
212 proxy
213 .revert_unit_files(unit_names)
214 .await
215 .map_err(|e| e.into())
216}
217
218pub fn enable_unit_files_with_flags(
219 unit_files: &[&str],
220 flags: u64,
221) -> Result<DisEnAbleUnitFilesResponse, SystemdErrors> {
222 let mut proxy: SysDManagerComLinkProxyBlocking<'_> = get_proxy()?;
223 proxy
224 .enable_unit_files_with_flags(unit_files, flags)
225 .map_err(|err| err.into())
226}
227
228pub fn disable_unit_files_with_flags(
229 unit_files: &[&str],
230 flags: u64,
231) -> Result<DisEnAbleUnitFilesResponse, SystemdErrors> {
232 let mut proxy: SysDManagerComLinkProxyBlocking<'_> = get_proxy()?;
233 proxy
234 .disable_unit_files_with_flags(unit_files, flags)
235 .map_err(|err| err.into())
236}