public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG
@ 2024-12-03 13:53 Shannon Sterz
  2024-12-03 13:53 ` [pbs-devel] [PATCH proxmox 2/2] product-config: use inner mutability for PRODUCT_CONFIG Shannon Sterz
  2024-12-04  8:56 ` [pbs-devel] applied: [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Dietmar Maurer
  0 siblings, 2 replies; 3+ messages in thread
From: Shannon Sterz @ 2024-12-03 13:53 UTC (permalink / raw)
  To: pbs-devel

in edition 2024 references to mutable static will be disallowed [1]. the
recommended way around this is to use types with inner mutability. so
use a `OnceLock` for `ACME_ACME_CONFIG` as the directoryfor ACME
configuration purposes shouldn't change throughout the run time of an
application.

[1]:
https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
 proxmox-acme-api/src/init.rs | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/proxmox-acme-api/src/init.rs b/proxmox-acme-api/src/init.rs
index a5287a8e..5dcf048c 100644
--- a/proxmox-acme-api/src/init.rs
+++ b/proxmox-acme-api/src/init.rs
@@ -1,26 +1,28 @@
 use std::path::{Path, PathBuf};
+use std::sync::OnceLock;
 
 use anyhow::Error;
 
 use proxmox_product_config::create_secret_dir;
 
+#[derive(Debug)]
 struct AcmeApiConfig {
     acme_config_dir: PathBuf,
     acme_account_dir: PathBuf,
 }
 
-static mut ACME_ACME_CONFIG: Option<AcmeApiConfig> = None;
+static ACME_ACME_CONFIG: OnceLock<AcmeApiConfig> = OnceLock::new();
 
 /// Initialize the global product configuration.
 pub fn init<P: AsRef<Path>>(acme_config_dir: P, create_subdirs: bool) -> Result<(), Error> {
     let acme_config_dir = acme_config_dir.as_ref().to_owned();
 
-    unsafe {
-        ACME_ACME_CONFIG = Some(AcmeApiConfig {
+    ACME_ACME_CONFIG
+        .set(AcmeApiConfig {
             acme_account_dir: acme_config_dir.join("accounts"),
             acme_config_dir,
-        });
-    }
+        })
+        .expect("cannot set acme configuration twice");
 
     if create_subdirs {
         create_secret_dir(self::acme_config_dir(), false)?;
@@ -31,11 +33,9 @@ pub fn init<P: AsRef<Path>>(acme_config_dir: P, create_subdirs: bool) -> Result<
 }
 
 fn acme_api_config() -> &'static AcmeApiConfig {
-    unsafe {
-        ACME_ACME_CONFIG
-            .as_ref()
-            .expect("ProxmoxProductConfig is not initialized!")
-    }
+    ACME_ACME_CONFIG
+        .get()
+        .expect("ProxmoxProductConfig is not initialized!")
 }
 
 fn acme_config_dir() -> &'static Path {
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] [PATCH proxmox 2/2] product-config: use inner mutability for PRODUCT_CONFIG
  2024-12-03 13:53 [pbs-devel] [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Shannon Sterz
@ 2024-12-03 13:53 ` Shannon Sterz
  2024-12-04  8:56 ` [pbs-devel] applied: [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Dietmar Maurer
  1 sibling, 0 replies; 3+ messages in thread
From: Shannon Sterz @ 2024-12-03 13:53 UTC (permalink / raw)
  To: pbs-devel

with edition 2024 `static mut` references will be disallowed [1]. the
recommended way to work around this is to use inner mutability, so use a
`OnceLock` for the `PRODUCT_CONFIG` as that should not change throughout
the run time of an application.

[1]:
https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html

Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
 proxmox-product-config/src/init.rs | 33 +++++++++++++++---------------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/proxmox-product-config/src/init.rs b/proxmox-product-config/src/init.rs
index a244559a..1bc47110 100644
--- a/proxmox-product-config/src/init.rs
+++ b/proxmox-product-config/src/init.rs
@@ -1,18 +1,21 @@
+use std::sync::OnceLock;
+
+#[derive(Debug)]
 struct ProxmoxProductConfig {
     api_user: nix::unistd::User,
     priv_user: nix::unistd::User,
 }
 
-static mut PRODUCT_CONFIG: Option<ProxmoxProductConfig> = None;
+static PRODUCT_CONFIG: OnceLock<ProxmoxProductConfig> = OnceLock::new();
 
 /// Initialize the global product configuration.
 pub fn init(api_user: nix::unistd::User, priv_user: nix::unistd::User) {
-    unsafe {
-        PRODUCT_CONFIG = Some(ProxmoxProductConfig {
+    PRODUCT_CONFIG
+        .set(ProxmoxProductConfig {
             api_user,
             priv_user,
-        });
-    }
+        })
+        .expect("cannot init proxmox product config twice");
 }
 
 /// Returns the global api user set with [init].
@@ -21,12 +24,10 @@ pub fn init(api_user: nix::unistd::User, priv_user: nix::unistd::User) {
 ///
 /// Panics if [init] wasn't called before.
 pub fn get_api_user() -> &'static nix::unistd::User {
-    unsafe {
-        &PRODUCT_CONFIG
-            .as_ref()
-            .expect("ProxmoxProductConfig is not initialized!")
-            .api_user
-    }
+    &PRODUCT_CONFIG
+        .get()
+        .expect("ProxmoxProductConfig is not initialized!")
+        .api_user
 }
 
 // Returns the global privileged user set with [init].
@@ -35,10 +36,8 @@ pub fn get_api_user() -> &'static nix::unistd::User {
 ///
 /// Panics if [init] wasn't called before.
 pub fn get_priv_user() -> &'static nix::unistd::User {
-    unsafe {
-        &PRODUCT_CONFIG
-            .as_ref()
-            .expect("ProxmoxProductConfig is not initialized!")
-            .priv_user
-    }
+    &PRODUCT_CONFIG
+        .get()
+        .expect("ProxmoxProductConfig is not initialized!")
+        .priv_user
 }
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] applied: [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG
  2024-12-03 13:53 [pbs-devel] [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Shannon Sterz
  2024-12-03 13:53 ` [pbs-devel] [PATCH proxmox 2/2] product-config: use inner mutability for PRODUCT_CONFIG Shannon Sterz
@ 2024-12-04  8:56 ` Dietmar Maurer
  1 sibling, 0 replies; 3+ messages in thread
From: Dietmar Maurer @ 2024-12-04  8:56 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Shannon Sterz

applied both patches


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-04  8:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-03 13:53 [pbs-devel] [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Shannon Sterz
2024-12-03 13:53 ` [pbs-devel] [PATCH proxmox 2/2] product-config: use inner mutability for PRODUCT_CONFIG Shannon Sterz
2024-12-04  8:56 ` [pbs-devel] applied: [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Dietmar Maurer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal