Skip to content

Commit

Permalink
Merge pull request containers#375 from flouthoc/lock-aardvark-commit
Browse files Browse the repository at this point in the history
aardvark,commit: acquire fs lock when performing commit to avoid `race` across parallel invocations.
  • Loading branch information
openshift-merge-robot authored Aug 30, 2022
2 parents aca2e8e + 0dcbbc0 commit c080296
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tokio = { version = "1.20.1", features = ["full"] }
zvariant = "3.4.1"
sha2 = "0.10.1"
netlink-packet-route = "0.13"
fs2 = "0.4.3"

[build-dependencies]
chrono = "0.4.20"
52 changes: 52 additions & 0 deletions src/dns/aardvark.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::network::types;
use fs2::FileExt;
use nix::sys::signal::{self, Signal};
use nix::unistd::Pid;
use std::collections::HashMap;
Expand All @@ -14,6 +15,7 @@ use std::process::{Command, Stdio};

const SYSTEMD_CHECK_PATH: &str = "/run/systemd/system";
const SYSTEMD_RUN: &str = "systemd-run";
const AARDVARK_COMMIT_LOCK: &str = "aardvark.lock";

#[derive(Clone, Debug)]
pub struct AardvarkEntry {
Expand Down Expand Up @@ -144,6 +146,36 @@ impl Aardvark {
Ok(())
}
pub fn commit_entries(&self, entries: Vec<AardvarkEntry>) -> Result<()> {
// Acquire fs lock to ensure other instance of aardvark cannot commit
// or start aardvark instance till already running instance has not
// completed its `commit` phase.
let lockfile_path = Path::new(&self.config)
.join("..")
.join(AARDVARK_COMMIT_LOCK);
let lockfile = match OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(lockfile_path.clone())
{
Ok(file) => file,
Err(e) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to open/create lockfile {:?}: {}", lockfile_path, e),
));
}
};
if let Err(er) = lockfile.lock_exclusive() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Failed to acquire exclusive lock on {:?}: {}",
lockfile_path, er
),
));
}

for entry in &entries {
let path = Path::new(&self.config).join(&entry.network_name);
if !path.exists() {
Expand All @@ -162,6 +194,16 @@ impl Aardvark {
}
match self.commit_entry(entry) {
Err(er) => {
// drop lockfile when commit is completed
if let Err(er) = lockfile.unlock() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Failed to unlock exclusive lock on {:?}: {}",
lockfile_path, er
),
));
}
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to commit entry {:?}: {}", entry, er),
Expand All @@ -171,6 +213,16 @@ impl Aardvark {
}
}

// drop lockfile when commit is completed
if let Err(er) = lockfile.unlock() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Failed to unlock exclusive lock on {:?}: {}",
lockfile_path, er
),
));
}
Ok(())
}

Expand Down

0 comments on commit c080296

Please sign in to comment.