1use std::{ffi::OsStr, process::Command, thread, time};
6
7use crate::agent_error::Result;
8
9pub fn systemctl_cmd<I, S>(xs: I) -> Result<std::process::Output>
15where
16 I: IntoIterator<Item = S>,
17 S: AsRef<OsStr>,
18{
19 Command::new("systemctl")
20 .args(xs)
21 .output()
22 .map_err(|e| e.into())
23}
24
25pub fn systemctl_start(x: &str) -> Result<bool> {
31 systemctl_cmd(&["start", x])?;
32
33 for _ in 0..5 {
34 let started = systemctl_status(x).map(did_succeed)?;
35
36 if started {
37 return Ok(started);
38 }
39
40 thread::sleep(time::Duration::from_millis(250));
41 }
42
43 Ok(false)
44}
45
46pub fn systemctl_stop(x: &str) -> Result<bool> {
52 systemctl_cmd(&["stop", x])?;
53
54 for _ in 0..5 {
55 let stopped = !systemctl_status(x).map(did_succeed)?;
56
57 if stopped {
58 return Ok(stopped);
59 }
60
61 thread::sleep(time::Duration::from_millis(250));
62 }
63
64 Ok(false)
65}
66
67pub fn systemctl_status(x: &str) -> Result<std::process::Output> {
73 systemctl_cmd(&["is-active", x, "--quiet"])
74}
75
76pub fn did_succeed(x: std::process::Output) -> bool {
82 x.status.success()
83}