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