diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 828de7ba..4454fcc3 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -207,9 +207,9 @@ pub enum ProgramError { name: String, }, - /// TraceFS not found. - #[error("tracefs non found")] - TraceFsNotFound, + /// An error occurred while working with IO. + #[error(transparent)] + IOError(#[from] io::Error), } /// A [`Program`] file descriptor. diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index f5b0f939..f595ca53 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -9,8 +9,8 @@ use std::{ use crate::{ programs::{ kprobe::KProbeError, perf_attach, perf_attach::PerfLink, perf_attach_debugfs, - trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, utils::get_tracefs, Link, - ProgramData, ProgramError, + trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, utils::find_tracefs_path, + Link, ProgramData, ProgramError, }, sys::{kernel_version, perf_event_open_probe, perf_event_open_trace_point}, }; @@ -61,7 +61,7 @@ pub(crate) fn attach>( pub(crate) fn detach_debug_fs(kind: ProbeKind, event_alias: &str) -> Result<(), ProgramError> { use ProbeKind::*; - let tracefs = get_tracefs()?; + let tracefs = find_tracefs_path()?; match kind { KProbe | KRetProbe => delete_probe_event(tracefs, kind, event_alias) @@ -118,7 +118,7 @@ fn create_as_trace_point( ) -> Result<(i32, String), ProgramError> { use ProbeKind::*; - let tracefs = get_tracefs()?; + let tracefs = find_tracefs_path()?; let event_alias = match kind { KProbe | KRetProbe => create_probe_event(tracefs, kind, name, offset) diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index a5f25e1d..d4bf158d 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -7,13 +7,12 @@ use crate::{ programs::{ define_link_wrapper, load_program, perf_attach::{perf_attach, PerfLink, PerfLinkId}, + utils::find_tracefs_path, ProgramData, ProgramError, }, sys::perf_event_open_trace_point, }; -use super::utils::get_tracefs; - /// The type returned when attaching a [`TracePoint`] fails. #[derive(Debug, Error)] pub enum TracePointError { @@ -79,7 +78,7 @@ impl TracePoint { /// /// The returned value can be used to detach, see [TracePoint::detach]. pub fn attach(&mut self, category: &str, name: &str) -> Result { - let tracefs = get_tracefs()?; + let tracefs = find_tracefs_path()?; let id = read_sys_fs_trace_point_id(tracefs, category, name)?; let fd = perf_event_open_trace_point(id, None).map_err(|(_code, io_error)| { ProgramError::SyscallError { diff --git a/aya/src/programs/utils.rs b/aya/src/programs/utils.rs index e9c431b0..143e8664 100644 --- a/aya/src/programs/utils.rs +++ b/aya/src/programs/utils.rs @@ -1,5 +1,5 @@ //! Common functions shared between multiple eBPF program types. -use std::{ffi::CStr, os::unix::io::RawFd, path::Path}; +use std::{ffi::CStr, io, os::unix::io::RawFd, path::Path}; use crate::{ programs::{FdLink, Link, ProgramData, ProgramError}, @@ -23,24 +23,25 @@ pub(crate) fn attach_raw_tracepoint>( program_data.links.insert(FdLink::new(pfd).into()) } -// Get tracefs filesystem -pub(crate) fn get_tracefs() -> Result<&'static Path, ProgramError> { +/// 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 mounts = [ + let known_mounts = [ Path::new("/sys/kernel/tracing"), Path::new("/sys/kernel/debug/tracing"), ]; - for mount in mounts { + for mount in known_mounts { if mount.exists() { return Some(mount); } } None - }; } - TRACE_FS.as_deref().ok_or(ProgramError::TraceFsNotFound) + TRACE_FS + .as_deref() + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "tracefs not found").into()) }