From 28b3c4044af06fdc32c9c85bf8381b5058306427 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 4 May 2025 19:06:10 -0400 Subject: [Rust] Simplify enterprise initialization We don't need to introduce extra global state when the goal is to not release floating licenses in shutdown. --- rust/src/enterprise.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'rust/src/enterprise.rs') diff --git a/rust/src/enterprise.rs b/rust/src/enterprise.rs index 9b97a387..d09ac333 100644 --- a/rust/src/enterprise.rs +++ b/rust/src/enterprise.rs @@ -19,13 +19,23 @@ pub enum EnterpriseCheckoutError { RefreshExpiredLicenseFailed(String), } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum EnterpriseCheckoutStatus { + /// The UI is managing the enterprise checkout. + AlreadyManaged, + /// Checkout was successful, attached duration is the duration of the license, if any. + Success(Option), +} + /// Initialize the enterprise server connection to check out a floating license. /// Result value is if we actually checked out a license (i.e. Ok(false) means we already have a /// license checked out and will not need to release it later) -pub fn checkout_license(duration: Duration) -> Result { +pub fn checkout_license( + duration: Duration, +) -> Result { if crate::is_ui_enabled() { // We only need to check out a license if running headlessly. - return Ok(false); + return Ok(EnterpriseCheckoutStatus::AlreadyManaged); } // The disparate core functions we call here might already have mutexes to guard. @@ -67,20 +77,19 @@ pub fn checkout_license(duration: Duration) -> Result String { unsafe { BnString::into_string(binaryninjacore_sys::BNGetEnterpriseServerToken()) } } -pub fn license_duration() -> Duration { - Duration::from_secs(unsafe { binaryninjacore_sys::BNGetEnterpriseServerLicenseDuration() }) +pub fn license_duration() -> Option { + let duration = + Duration::from_secs(unsafe { binaryninjacore_sys::BNGetEnterpriseServerLicenseDuration() }); + match duration { + // If the core returns 0 there is no license duration. + Duration::ZERO => None, + _ => Some(duration), + } } pub fn license_expiration_time() -> SystemTime { -- cgit v1.3.1