Browse Source

Remove procfs dependency

Tamir Duberstein 1 year ago
parent
commit
cc2bc0acc1

+ 0 - 1
aya/Cargo.toml

@@ -31,7 +31,6 @@ tokio = { version = "1.24.0", features = [
     "rt-multi-thread",
     "net",
 ], optional = true }
-procfs = { version = "0.15.1", default-features = false }
 
 [dev-dependencies]
 futures = { version = "0.3.12", default-features = false, features = ["std"] }

+ 1 - 1
aya/src/maps/mod.rs

@@ -47,9 +47,9 @@ use std::{
     ptr,
 };
 
+use crate::util::KernelVersion;
 use libc::{getrlimit, rlimit, RLIMIT_MEMLOCK, RLIM_INFINITY};
 use log::warn;
-use procfs::KernelVersion;
 use thiserror::Error;
 
 use crate::{

+ 1 - 1
aya/src/programs/cgroup_device.rs

@@ -1,6 +1,6 @@
 //! Cgroup device programs.
 
-use procfs::KernelVersion;
+use crate::util::KernelVersion;
 use std::os::fd::{AsRawFd, RawFd};
 
 use crate::{

+ 1 - 1
aya/src/programs/cgroup_skb.rs

@@ -1,6 +1,6 @@
 //! Cgroup skb programs.
 
-use procfs::KernelVersion;
+use crate::util::KernelVersion;
 use std::{
     hash::Hash,
     os::fd::{AsRawFd, RawFd},

+ 1 - 1
aya/src/programs/cgroup_sock.rs

@@ -2,7 +2,7 @@
 
 pub use aya_obj::programs::CgroupSockAttachType;
 
-use procfs::KernelVersion;
+use crate::util::KernelVersion;
 use std::{
     hash::Hash,
     os::fd::{AsRawFd, RawFd},

+ 1 - 1
aya/src/programs/cgroup_sock_addr.rs

@@ -2,7 +2,7 @@
 
 pub use aya_obj::programs::CgroupSockAddrAttachType;
 
-use procfs::KernelVersion;
+use crate::util::KernelVersion;
 use std::{
     hash::Hash,
     os::fd::{AsRawFd, RawFd},

+ 1 - 1
aya/src/programs/cgroup_sockopt.rs

@@ -2,7 +2,7 @@
 
 pub use aya_obj::programs::CgroupSockoptAttachType;
 
-use procfs::KernelVersion;
+use crate::util::KernelVersion;
 use std::{
     hash::Hash,
     os::fd::{AsRawFd, RawFd},

+ 1 - 1
aya/src/programs/cgroup_sysctl.rs

@@ -1,6 +1,6 @@
 //! Cgroup sysctl programs.
 
-use procfs::KernelVersion;
+use crate::util::KernelVersion;
 use std::{
     hash::Hash,
     os::fd::{AsRawFd, RawFd},

+ 1 - 1
aya/src/programs/mod.rs

@@ -64,8 +64,8 @@ pub mod uprobe;
 mod utils;
 pub mod xdp;
 
+use crate::util::KernelVersion;
 use libc::ENOSPC;
-use procfs::KernelVersion;
 use std::{
     ffi::CString,
     io,

+ 1 - 1
aya/src/programs/probe.rs

@@ -1,5 +1,5 @@
+use crate::util::KernelVersion;
 use libc::pid_t;
-use procfs::KernelVersion;
 use std::{
     fs::{self, OpenOptions},
     io::{self, Write},

+ 1 - 1
aya/src/programs/xdp.rs

@@ -1,8 +1,8 @@
 //! eXpress Data Path (XDP) programs.
 
+use crate::util::KernelVersion;
 use bitflags;
 use libc::if_nametoindex;
-use procfs::KernelVersion;
 use std::{convert::TryFrom, ffi::CString, hash::Hash, io, mem, os::unix::io::RawFd};
 use thiserror::Error;
 

+ 1 - 1
aya/src/sys/bpf.rs

@@ -7,12 +7,12 @@ use std::{
     slice,
 };
 
+use crate::util::KernelVersion;
 use libc::{c_char, c_long, close, ENOENT, ENOSPC};
 use obj::{
     maps::{bpf_map_def, LegacyMap},
     BpfSectionKind,
 };
-use procfs::KernelVersion;
 
 use crate::{
     generated::{

+ 63 - 3
aya/src/util.rs

@@ -3,8 +3,10 @@ use std::{
     collections::BTreeMap,
     ffi::CString,
     fs::{self, File},
-    io::{self, BufReader},
-    mem, slice,
+    io::{self, BufRead, BufReader},
+    mem,
+    num::ParseIntError,
+    slice,
     str::FromStr,
 };
 
@@ -15,7 +17,65 @@ use crate::{
 
 use libc::{if_nametoindex, sysconf, _SC_PAGESIZE};
 
-use io::BufRead;
+/// Represents a kernel version, in major.minor.release version.
+// Adapted from https://docs.rs/procfs/latest/procfs/sys/kernel/struct.Version.html.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
+pub struct KernelVersion {
+    pub(crate) major: u8,
+    pub(crate) minor: u8,
+    pub(crate) patch: u16,
+}
+
+impl KernelVersion {
+    /// Constructor.
+    pub fn new(major: u8, minor: u8, patch: u16) -> Self {
+        Self {
+            major,
+            minor,
+            patch,
+        }
+    }
+
+    /// Returns the kernel version of the currently running kernel.
+    ///
+    /// This is taken from `/proc/sys/kernel/osrelease`;
+    pub fn current() -> Result<Self, String> {
+        let s =
+            fs::read_to_string("/proc/sys/kernel/osrelease").map_err(|err| format!("{err:?}"))?;
+        let s = s.as_str();
+
+        let pos = s.find(|c: char| c != '.' && !c.is_ascii_digit());
+        let kernel = if let Some(pos) = pos {
+            let (s, _) = s.split_at(pos);
+            s
+        } else {
+            s
+        };
+        let mut kernel_split = kernel.split('.');
+
+        let major = kernel_split
+            .next()
+            .ok_or("Missing major version component")?;
+        let minor = kernel_split
+            .next()
+            .ok_or("Missing minor version component")?;
+        let patch = kernel_split
+            .next()
+            .ok_or("Missing patch version component")?;
+
+        let major = major
+            .parse()
+            .map_err(|ParseIntError { .. }| "Failed to parse major version")?;
+        let minor = minor
+            .parse()
+            .map_err(|ParseIntError { .. }| "Failed to parse minor version")?;
+        let patch = patch
+            .parse()
+            .map_err(|ParseIntError { .. }| "Failed to parse patch version")?;
+
+        Ok(Self::new(major, minor, patch))
+    }
+}
 
 const ONLINE_CPUS: &str = "/sys/devices/system/cpu/online";
 pub(crate) const POSSIBLE_CPUS: &str = "/sys/devices/system/cpu/possible";

+ 0 - 1
test/integration-test/Cargo.toml

@@ -12,7 +12,6 @@ aya-obj = { path = "../../aya-obj" }
 libc = { version = "0.2.105" }
 log = "0.4"
 object = { version = "0.31", default-features = false, features = ["std", "read_core", "elf"] }
-procfs = "0.15.1"
 rbpf = "0.2.0"
 tempfile = "3.3.0"
 tokio = { version = "1.24", features = ["rt", "rt-multi-thread", "sync", "time"] }

+ 1 - 2
test/integration-test/tests/btf_relocations.rs

@@ -1,9 +1,8 @@
 use anyhow::{bail, Context as _, Result};
-use procfs::KernelVersion;
 use std::{path::PathBuf, process::Command, thread::sleep, time::Duration};
 use tempfile::TempDir;
 
-use aya::{maps::Array, programs::TracePoint, BpfLoader, Btf, Endianness};
+use aya::{maps::Array, programs::TracePoint, util::KernelVersion, BpfLoader, Btf, Endianness};
 
 // In the tests below we often use values like 0xAAAAAAAA or -0x7AAAAAAA. Those values have no
 // special meaning, they just have "nice" bit patterns that can be helpful while debugging.

+ 1 - 1
test/integration-test/tests/load.rs

@@ -1,4 +1,3 @@
-use procfs::KernelVersion;
 use std::{convert::TryInto as _, thread, time};
 
 use aya::{
@@ -8,6 +7,7 @@ use aya::{
         links::{FdLink, PinnedLink},
         loaded_programs, KProbe, TracePoint, Xdp, XdpFlags,
     },
+    util::KernelVersion,
     Bpf,
 };
 

+ 1 - 2
test/integration-test/tests/smoke.rs

@@ -1,8 +1,7 @@
-use procfs::KernelVersion;
-
 use aya::{
     include_bytes_aligned,
     programs::{Extension, Xdp, XdpFlags},
+    util::KernelVersion,
     Bpf, BpfLoader,
 };