From 6595a6c2263d6181b1721138a90c72706b565f36 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Tue, 11 Aug 2026 01:57:08 +0100 Subject: [PATCH] Serialize bootpd preference updates --- src/main.rs | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4e6d9a6..9d4ce7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,8 @@ use system_configuration::core_foundation::number::CFNumber; use system_configuration::core_foundation::string::CFString; use system_configuration::preferences::SCPreferences; use system_configuration::sys::preferences::{ - SCPreferencesApplyChanges, SCPreferencesCommitChanges, SCPreferencesSetValue, + SCPreferencesApplyChanges, SCPreferencesCommitChanges, SCPreferencesLock, + SCPreferencesSetValue, SCPreferencesUnlock, }; use uzers::{get_current_groupname, get_current_username, get_effective_uid}; @@ -289,24 +290,38 @@ fn configure_bootpd(lease_time: u32) -> anyhow::Result<()> { ]); unsafe { + let prefs = prefs.as_concrete_TypeRef(); anyhow::ensure!( - SCPreferencesSetValue( - prefs.as_concrete_TypeRef(), - CFString::new("bootpd").as_concrete_TypeRef(), - bootpd_dict.as_concrete_TypeRef().cast(), - ) != 0, - "failed to set bootpd preferences" + SCPreferencesLock(prefs, 1) != 0, + "failed to lock bootpd preferences" ); - anyhow::ensure!( - SCPreferencesCommitChanges(prefs.as_concrete_TypeRef()) != 0, - "failed to commit bootpd preferences" - ); + let result = (|| -> anyhow::Result<()> { + anyhow::ensure!( + SCPreferencesSetValue( + prefs, + CFString::new("bootpd").as_concrete_TypeRef(), + bootpd_dict.as_concrete_TypeRef().cast(), + ) != 0, + "failed to set bootpd preferences" + ); - anyhow::ensure!( - SCPreferencesApplyChanges(prefs.as_concrete_TypeRef()) != 0, - "failed to apply bootpd preferences" - ); + anyhow::ensure!( + SCPreferencesCommitChanges(prefs) != 0, + "failed to commit bootpd preferences" + ); + + anyhow::ensure!( + SCPreferencesApplyChanges(prefs) != 0, + "failed to apply bootpd preferences" + ); + + Ok(()) + })(); + + let unlocked = SCPreferencesUnlock(prefs) != 0; + result?; + anyhow::ensure!(unlocked, "failed to unlock bootpd preferences"); } Ok(())