]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/systemd/unit.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / src / tools / systemd / unit.rs
1 use std::process::Command;
2
3 use anyhow::{bail, format_err, Error};
4
5 fn run_command(mut command: Command) -> Result<(), Error> {
6 let output = command
7 .output()
8 .map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?;
9
10 proxmox_lang::try_block!({
11 if !output.status.success() {
12 match output.status.code() {
13 Some(code) => {
14 if code != 0 {
15 let msg = String::from_utf8(output.stderr)
16 .map(|m| {
17 if m.is_empty() {
18 String::from("no error message")
19 } else {
20 m
21 }
22 })
23 .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
24
25 bail!("status code: {} - {}", code, msg);
26 }
27 }
28 None => bail!("terminated by signal"),
29 }
30 }
31 Ok(())
32 }).map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
33
34 Ok(())
35 }
36
37 pub fn reload_daemon() -> Result<(), Error> {
38 let mut command = std::process::Command::new("systemctl");
39 command.arg("daemon-reload");
40
41 run_command(command)?;
42
43 Ok(())
44 }
45
46 pub fn disable_unit(unit: &str) -> Result<(), Error> {
47 let mut command = std::process::Command::new("systemctl");
48 command.arg("disable");
49 command.arg(unit);
50
51 run_command(command)?;
52
53 Ok(())
54 }
55
56 pub fn enable_unit(unit: &str) -> Result<(), Error> {
57 let mut command = std::process::Command::new("systemctl");
58 command.arg("enable");
59 command.arg(unit);
60
61 run_command(command)?;
62
63 Ok(())
64 }
65
66 pub fn start_unit(unit: &str) -> Result<(), Error> {
67 let mut command = std::process::Command::new("systemctl");
68 command.arg("start");
69 command.arg(unit);
70
71 run_command(command)?;
72
73 Ok(())
74 }
75
76 pub fn stop_unit(unit: &str) -> Result<(), Error> {
77 let mut command = std::process::Command::new("systemctl");
78 command.arg("stop");
79 command.arg(unit);
80
81 run_command(command)?;
82
83 Ok(())
84 }
85
86 pub fn reload_unit(unit: &str) -> Result<(), Error> {
87 let mut command = std::process::Command::new("systemctl");
88 command.arg("try-reload-or-restart");
89 command.arg(unit);
90
91 run_command(command)?;
92
93 Ok(())
94 }
95
96 #[test]
97 fn test_escape_unit() -> Result<(), Error> {
98 fn test_escape(i: &str, expected: &str, is_path: bool) {
99
100 use proxmox_sys::systemd::{escape_unit, unescape_unit};
101
102 let escaped = escape_unit(i, is_path);
103 assert_eq!(escaped, expected);
104 let unescaped = unescape_unit(&escaped).unwrap();
105 if is_path {
106 let mut p = i.trim_matches('/');
107 if p.is_empty() {
108 p = "/";
109 }
110 assert_eq!(p, unescaped);
111 } else {
112 assert_eq!(i, unescaped);
113 }
114 }
115
116 test_escape(".test", "\\x2etest", false);
117 test_escape("t.est", "t.est", false);
118 test_escape("_test_", "_test_", false);
119
120 test_escape("/", "-", false);
121 test_escape("//", "--", false);
122
123 test_escape("/", "-", true);
124 test_escape("//", "-", true);
125
126 Ok(())
127 }