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

pull/1043/head
Tamir Duberstein 4 months ago
parent 0f163633e3
commit 2b299d4fba

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

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

@ -7,7 +7,7 @@ use std::{
raw::c_int, raw::c_int,
}, },
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::{Arc, LazyLock},
}; };
use aya_obj::{ 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}; pub use aya_obj::maps::{bpf_map_def, PinningType};
lazy_static::lazy_static! { pub(crate) static FEATURES: LazyLock<Features> = LazyLock::new(detect_features);
pub(crate) static ref FEATURES: Features = detect_features();
}
fn detect_features() -> Features { fn detect_features() -> Features {
let btf = if is_btf_supported() { let btf = if is_btf_supported() {

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

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

Loading…
Cancel
Save