Skip to content

Commit

Permalink
fix: don't wait on systemctl if we don't need to
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Dec 16, 2023
1 parent c80f428 commit b08881d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn main() -> anyhow::Result<()> {
service.add_profile_fragment(&mode)?;
if !no_restart {
service.reload_unit_config()?;
service.action("restart")?;
service.action("restart", false)?;
} else {
log::warn!("Profiling config will only be applied when systemd config is reloaded, and service restarted");
}
Expand All @@ -138,7 +138,7 @@ fn main() -> anyhow::Result<()> {
no_restart,
}) => {
let service = systemd::Service::new(&service);
service.action("stop")?;
service.action("stop", true)?;
service.remove_profile_fragment()?;
let resolved_opts = service.profiling_result()?;
log::info!(
Expand All @@ -154,15 +154,15 @@ fn main() -> anyhow::Result<()> {
}
service.reload_unit_config()?;
if !no_restart {
service.action("start")?;
service.action("start", false)?;
}
}
cl::Action::Service(cl::ServiceAction::Reset { service }) => {
let service = systemd::Service::new(&service);
let _ = service.remove_profile_fragment();
let _ = service.remove_hardening_fragment();
service.reload_unit_config()?;
service.action("try-restart")?;
service.action("try-restart", false)?;
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/systemd/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ impl Service {
Ok(())
}

pub fn action(&self, verb: &str) -> anyhow::Result<()> {
log::info!("{} {}", verb, &self.unit_name());
let status = Command::new("systemctl")
.args([verb, &self.unit_name()])
.status()?;
pub fn action(&self, verb: &str, block: bool) -> anyhow::Result<()> {
let unit_name = self.unit_name();
log::info!("{} {}", verb, unit_name);
let mut cmd = vec![verb];
if !block {
cmd.push("--no-block");
}
cmd.push(&unit_name);
let status = Command::new("systemctl").args(cmd).status()?;
if !status.success() {
anyhow::bail!("systemctl failed: {status}");
}
Expand Down

0 comments on commit b08881d

Please sign in to comment.