diff --git a/Cargo.lock b/Cargo.lock index 0ba0a6ab6..41de134b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1600,12 +1600,12 @@ dependencies = [ [[package]] name = "fs4" -version = "0.12.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c29c30684418547d476f0b48e84f4821639119c483b1eccd566c8cd0cd05f521" +checksum = "8640e34b88f7652208ce9e88b1a37a2ae95227d84abec377ccd3c5cfeb141ed4" dependencies = [ - "rustix 0.38.44", - "windows-sys 0.52.0", + "rustix 1.0.3", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 8777db081..cd104992b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -173,7 +173,7 @@ dhat = "0.3.3" dyn-clone = "^1.0.17" fernet = "^0.2.1" filetime = "^0.2.24" -fs4 = "^0.12.0" +fs4 = "^0.13.0" futures = "^0.3.31" futures-util = { version = "^0.3.30", features = ["sink"] } gix = { version = "0.64.0", default-features = false } diff --git a/server/daemon/src/main.rs b/server/daemon/src/main.rs index 8f00daecd..c3b40faa0 100644 --- a/server/daemon/src/main.rs +++ b/server/daemon/src/main.rs @@ -465,13 +465,13 @@ async fn start_daemon(opt: KanidmdParser, config: Configuration) -> ExitCode { return ExitCode::FAILURE; } - match &opt.commands { + let lock_was_setup = match &opt.commands { // we aren't going to touch the DB so we can carry on KanidmdOpt::ShowReplicationCertificate { .. } | KanidmdOpt::RenewReplicationCertificate { .. } | KanidmdOpt::RefreshReplicationConsumer { .. } | KanidmdOpt::RecoverAccount { .. } - | KanidmdOpt::HealthCheck(_) => (), + | KanidmdOpt::HealthCheck(_) => None, _ => { // Okay - Lets now create our lock and go. #[allow(clippy::expect_used)] @@ -482,24 +482,53 @@ async fn start_daemon(opt: KanidmdParser, config: Configuration) -> ExitCode { let flock = match File::create(&klock_path) { Ok(flock) => flock, - Err(e) => { - error!("ERROR: Refusing to start - unable to create kanidmd exclusive lock at {} - {:?}", klock_path.display(), e); + Err(err) => { + error!( + "ERROR: Refusing to start - unable to create kanidmd exclusive lock at {}", + klock_path.display() + ); + error!(?err); return ExitCode::FAILURE; } }; match flock.try_lock_exclusive() { - Ok(()) => debug!("Acquired kanidm exclusive lock"), - Err(e) => { - error!("ERROR: Refusing to start - unable to lock kanidmd exclusive lock at {} - {:?}", klock_path.display(), e); + Ok(true) => debug!("Acquired kanidm exclusive lock"), + Ok(false) => { + error!( + "ERROR: Refusing to start - unable to lock kanidmd exclusive lock at {}", + klock_path.display() + ); error!("Is another kanidmd process running?"); return ExitCode::FAILURE; } + Err(err) => { + error!( + "ERROR: Refusing to start - unable to lock kanidmd exclusive lock at {}", + klock_path.display() + ); + error!(?err); + return ExitCode::FAILURE; + } }; + + Some(klock_path) + } + }; + + let result_code = kanidm_main(config, opt).await; + + if let Some(klock_path) = lock_was_setup { + if let Err(reason) = std::fs::remove_file(&klock_path) { + warn!( + ?reason, + "WARNING: Unable to clean up kanidmd exclusive lock at {}", + klock_path.display() + ); } } - kanidm_main(config, opt).await + result_code } fn main() -> ExitCode {