Browse Source

Replace `lazy_static` with `std::sync::LazyLock`

Tamir Duberstein 5 months ago
parent
commit
2b299d4fba
5 changed files with 26 additions and 32 deletions
  1. 0 1
      Cargo.toml
  2. 1 2
      aya/Cargo.toml
  3. 2 4
      aya/src/bpf.rs
  4. 3 5
      aya/src/programs/uprobe.rs
  5. 20 20
      aya/src/programs/utils.rs

+ 0 - 1
Cargo.toml

@@ -72,7 +72,6 @@ futures = { version = "0.3.28", default-features = false }
 hashbrown = { version = "0.14.3", default-features = false }
 indoc = { version = "2.0", default-features = false }
 integration-ebpf = { path = "test/integration-ebpf", default-features = false }
-lazy_static = { version = "1", default-features = false }
 libc = { version = "0.2.105", default-features = false }
 log = { version = "0.4", default-features = false }
 netns-rs = { version = "0.1", default-features = false }

+ 1 - 2
aya/Cargo.toml

@@ -5,7 +5,7 @@ description = "An eBPF library with a focus on developer experience and operabil
 keywords = ["bpf", "ebpf", "kernel", "linux"]
 readme = "README.md"
 documentation = "https://docs.rs/aya"
-rust-version = "1.66"
+rust-version = "1.80.0"
 authors.workspace = true
 license.workspace = true
 repository.workspace = true
@@ -18,7 +18,6 @@ async-io = { workspace = true, optional = true }
 aya-obj = { path = "../aya-obj", version = "^0.1.0", features = ["std"] }
 bitflags = { workspace = true }
 bytes = { workspace = true }
-lazy_static = { workspace = true }
 libc = { workspace = true }
 log = { workspace = true }
 object = { workspace = true, features = ["elf", "read_core", "std", "write"] }

+ 2 - 4
aya/src/bpf.rs

@@ -7,7 +7,7 @@ use std::{
         raw::c_int,
     },
     path::{Path, PathBuf},
-    sync::Arc,
+    sync::{Arc, LazyLock},
 };
 
 use aya_obj::{
@@ -70,9 +70,7 @@ unsafe impl<T: Pod, const N: usize> Pod for [T; N] {}
 
 pub use aya_obj::maps::{bpf_map_def, PinningType};
 
-lazy_static::lazy_static! {
-    pub(crate) static ref FEATURES: Features = detect_features();
-}
+pub(crate) static FEATURES: LazyLock<Features> = LazyLock::new(detect_features);
 
 fn detect_features() -> Features {
     let btf = if is_btf_supported() {

+ 3 - 5
aya/src/programs/uprobe.rs

@@ -8,7 +8,7 @@ use std::{
     mem,
     os::{fd::AsFd as _, raw::c_char, unix::ffi::OsStrExt},
     path::{Path, PathBuf},
-    sync::Arc,
+    sync::{Arc, LazyLock},
 };
 
 use libc::pid_t;
@@ -29,10 +29,8 @@ use crate::{
 
 const LD_SO_CACHE_FILE: &str = "/etc/ld.so.cache";
 
-lazy_static::lazy_static! {
-    static ref LD_SO_CACHE: Result<LdSoCache, Arc<io::Error>> =
-        LdSoCache::load(LD_SO_CACHE_FILE).map_err(Arc::new);
-}
+static LD_SO_CACHE: LazyLock<Result<LdSoCache, Arc<io::Error>>> =
+    LazyLock::new(|| LdSoCache::load(LD_SO_CACHE_FILE).map_err(Arc::new));
 const LD_SO_CACHE_HEADER_OLD: &str = "ld.so-1.7.0\0";
 const LD_SO_CACHE_HEADER_NEW: &str = "glibc-ld.so.cache1.1";
 

+ 20 - 20
aya/src/programs/utils.rs

@@ -5,6 +5,7 @@ use std::{
     io::{self, BufRead, BufReader},
     os::fd::{AsFd as _, AsRawFd as _, BorrowedFd},
     path::Path,
+    sync::LazyLock,
     time::{Duration, SystemTime, UNIX_EPOCH},
 };
 
@@ -31,27 +32,26 @@ pub(crate) fn attach_raw_tracepoint<T: Link + From<FdLink>>(
 
 /// Find tracefs filesystem path.
 pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
-    lazy_static::lazy_static! {
-        static ref TRACE_FS: Option<&'static Path> = {
-            let known_mounts = [
-                Path::new("/sys/kernel/tracing"),
-                Path::new("/sys/kernel/debug/tracing"),
-            ];
-
-            for mount in known_mounts {
-                // Check that the mount point exists and is not empty
-                // Documented here: (https://www.kernel.org/doc/Documentation/trace/ftrace.txt)
-                // In some cases, tracefs will only mount at /sys/kernel/debug/tracing
-                // but, the kernel will still create the directory /sys/kernel/tracing.
-                // The user may be expected to manually mount the directory in order for it to
-                // exist in /sys/kernel/tracing according to the documentation.
-                if mount.exists() && mount.read_dir().ok()?.next().is_some() {
-                    return Some(mount);
+    static TRACE_FS: LazyLock<Option<&'static Path>> = LazyLock::new(|| {
+        [
+            Path::new("/sys/kernel/tracing"),
+            Path::new("/sys/kernel/debug/tracing"),
+        ]
+        .into_iter()
+        .find(|&mount| {
+            // Check that the mount point exists and is not empty
+            // Documented here: (https://www.kernel.org/doc/Documentation/trace/ftrace.txt)
+            // In some cases, tracefs will only mount at /sys/kernel/debug/tracing
+            // but, the kernel will still create the directory /sys/kernel/tracing.
+            // The user may be expected to manually mount the directory in order for it to
+            // exist in /sys/kernel/tracing according to the documentation.
+            mount.exists()
+                && match mount.read_dir() {
+                    Ok(mut entries) => entries.next().is_some(),
+                    Err(io::Error { .. }) => false,
                 }
-            }
-            None
-        };
-    }
+        })
+    });
 
     TRACE_FS
         .as_deref()