diff --git a/src/config/network/helper.rs b/src/config/network/helper.rs index e32d3e20..97645c01 100644 --- a/src/config/network/helper.rs +++ b/src/config/network/helper.rs @@ -149,7 +149,7 @@ pub fn compute_file_diff(filename: &str, shadow: &str) -> Result .output() .map_err(|err| format_err!("failed to execute diff - {}", err))?; - let diff = crate::tools::command_output(output) + let diff = crate::tools::command_output(output, Some(|c| c == 0 || c == 1)) .map_err(|err| format_err!("diff failed: {}", err))?; Ok(diff) @@ -165,17 +165,14 @@ pub fn assert_ifupdown2_installed() -> Result<(), Error> { pub fn network_reload() -> Result<(), Error> { - let status = Command::new("/sbin/ifreload") + let output = Command::new("/sbin/ifreload") .arg("-a") - .status() - .map_err(|err| format_err!("failed to execute ifreload: - {}", err))?; + .output() + .map_err(|err| format_err!("failed to execute '/sbin/ifreload' - {}", err))?; + + crate::tools::command_output(output, None) + .map_err(|err| format_err!("ifreload failed: {}", err))?; - if !status.success() { - match status.code() { - Some(code) => bail!("ifreload failed with status code: {}", code), - None => bail!("ifreload terminated by signal") - } - } Ok(()) } diff --git a/src/tools.rs b/src/tools.rs index 06682480..2fedd848 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -475,13 +475,22 @@ pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> { } /// Helper to check result from std::process::Command output -pub fn command_output(output: std::process::Output) -> Result { +/// +/// The exit_code_check() function should return true if the exit code +/// is considered successful. +pub fn command_output( + output: std::process::Output, + exit_code_check: Option bool> +) -> Result { if !output.status.success() { match output.status.code() { Some(code) => { - if code == 0 { return Ok(String::new()); } - if code != 1 { + let is_ok = match exit_code_check { + Some(check_fn) => check_fn(code), + None => code == 0, + }; + if !is_ok { let msg = String::from_utf8(output.stderr) .map(|m| if m.is_empty() { String::from("no error message") } else { m }) .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));