]> git.proxmox.com Git - proxmox-backup.git/blame - src/tools/systemd.rs
api2/node/../disks/directory: added DELETE endpoint for removal of mount-units
[proxmox-backup.git] / src / tools / systemd.rs
CommitLineData
f486e9e5 1pub mod types;
f3a96b2c 2pub mod config;
e05b637c
DM
3
4mod parse_time;
ee8b4644 5pub mod tm_editor;
e05b637c 6pub mod time;
da84cc52
DM
7
8use anyhow::{bail, Error};
9
10/// Escape strings for usage in systemd unit names
11pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
12
13 if is_path {
14 unit = unit.trim_matches('/');
15 if unit.is_empty() {
16 return String::from("-");
17 }
18 }
19
20 let unit = unit.as_bytes();
21
22 let mut escaped = String::new();
23
24 for (i, c) in unit.iter().enumerate() {
25 if *c == b'/' {
26 escaped.push('-');
27 continue;
28 }
29 if (i == 0 && *c == b'.') || !((*c >= b'0' && *c <= b'9') || (*c >= b'A' && *c <= b'Z') || (*c >= b'a' && *c <= b'z')) {
30 escaped.push_str(&format!("\\x{:0x}", c));
31 } else {
32 escaped.push(*c as char);
33 }
34 }
35 escaped
36}
37
38fn parse_hex_digit(d: u8) -> Result<u8, Error> {
39 if d >= b'0' && d <= b'9' { return Ok(d - b'0'); }
40 if d >= b'A' && d <= b'F' { return Ok(d - b'A' + 10); }
41 if d >= b'a' && d <= b'f' { return Ok(d - b'a' + 10); }
42 bail!("got invalid hex digit");
43}
44
45/// Unescape strings used in systemd unit names
46pub fn unescape_unit(text: &str) -> Result<String, Error> {
47
48 let mut i = text.as_bytes();
49
50 let mut data: Vec<u8> = Vec::new();
51
52 loop {
53 if i.is_empty() { break; }
54 let next = i[0];
55 if next == b'\\' {
56 if i.len() < 4 { bail!("short input"); }
57 if i[1] != b'x' { bail!("unkwnown escape sequence"); }
58 let h1 = parse_hex_digit(i[2])?;
59 let h0 = parse_hex_digit(i[3])?;
60 data.push(h1<<4|h0);
61 i = &i[4..]
62 } else if next == b'-' {
63 data.push(b'/');
64 i = &i[1..]
65 } else {
66 data.push(next);
67 i = &i[1..]
68 }
69 }
70
71 let text = String::from_utf8(data)?;
72
73 Ok(text)
74}
669c137f
DM
75
76pub fn reload_daemon() -> Result<(), Error> {
77
cbef49bf 78 let mut command = std::process::Command::new("systemctl");
669c137f
DM
79 command.arg("daemon-reload");
80
81 crate::tools::run_command(command, None)?;
82
83 Ok(())
84}
85
be614c62
HL
86pub fn disable_unit(unit: &str) -> Result<(), Error> {
87
88 let mut command = std::process::Command::new("systemctl");
89 command.arg("disable");
90 command.arg(unit);
91
92 crate::tools::run_command(command, None)?;
93
94 Ok(())
95}
96
669c137f
DM
97pub fn enable_unit(unit: &str) -> Result<(), Error> {
98
cbef49bf 99 let mut command = std::process::Command::new("systemctl");
669c137f
DM
100 command.arg("enable");
101 command.arg(unit);
102
103 crate::tools::run_command(command, None)?;
104
105 Ok(())
106}
107
108pub fn start_unit(unit: &str) -> Result<(), Error> {
109
cbef49bf 110 let mut command = std::process::Command::new("systemctl");
669c137f
DM
111 command.arg("start");
112 command.arg(unit);
113
114 crate::tools::run_command(command, None)?;
115
116 Ok(())
117}
118
119pub fn stop_unit(unit: &str) -> Result<(), Error> {
120
cbef49bf 121 let mut command = std::process::Command::new("systemctl");
669c137f
DM
122 command.arg("stop");
123 command.arg(unit);
124
125 crate::tools::run_command(command, None)?;
126
127 Ok(())
128}