1 use std
::process
::Command
;
3 use anyhow
::{bail, format_err, Error}
;
5 fn run_command(mut command
: Command
) -> Result
<(), Error
> {
8 .map_err(|err
| format_err
!("failed to execute {:?} - {}", command
, err
))?
;
10 proxmox_lang
::try_block
!({
11 if !output
.status
.success() {
12 match output
.status
.code() {
15 let msg
= String
::from_utf8(output
.stderr
)
18 String
::from("no error message")
23 .unwrap_or_else(|_
| String
::from("non utf8 error message (suppressed)"));
25 bail
!("status code: {} - {}", code
, msg
);
28 None
=> bail
!("terminated by signal"),
32 }).map_err(|err
| format_err
!("command {:?} failed - {}", command
, err
))?
;
37 pub fn reload_daemon() -> Result
<(), Error
> {
38 let mut command
= std
::process
::Command
::new("systemctl");
39 command
.arg("daemon-reload");
41 run_command(command
)?
;
46 pub fn disable_unit(unit
: &str) -> Result
<(), Error
> {
47 let mut command
= std
::process
::Command
::new("systemctl");
48 command
.arg("disable");
51 run_command(command
)?
;
56 pub fn enable_unit(unit
: &str) -> Result
<(), Error
> {
57 let mut command
= std
::process
::Command
::new("systemctl");
58 command
.arg("enable");
61 run_command(command
)?
;
66 pub fn start_unit(unit
: &str) -> Result
<(), Error
> {
67 let mut command
= std
::process
::Command
::new("systemctl");
71 run_command(command
)?
;
76 pub fn stop_unit(unit
: &str) -> Result
<(), Error
> {
77 let mut command
= std
::process
::Command
::new("systemctl");
81 run_command(command
)?
;
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");
91 run_command(command
)?
;
97 fn test_escape_unit() -> Result
<(), Error
> {
98 fn test_escape(i
: &str, expected
: &str, is_path
: bool
) {
100 use proxmox_sys
::systemd
::{escape_unit, unescape_unit}
;
102 let escaped
= escape_unit(i
, is_path
);
103 assert_eq
!(escaped
, expected
);
104 let unescaped
= unescape_unit(&escaped
).unwrap();
106 let mut p
= i
.trim_matches('
/'
);
110 assert_eq
!(p
, unescaped
);
112 assert_eq
!(i
, unescaped
);
116 test_escape(".test", "\\x2etest", false);
117 test_escape("t.est", "t.est", false);
118 test_escape("_test_", "_test_", false);
120 test_escape("/", "-", false);
121 test_escape("//", "--", false);
123 test_escape("/", "-", true);
124 test_escape("//", "-", true);