Skip to content

Commit c4589e2

Browse files
committed
fix: Add intelligent eBPF object file path resolution
- Fix inconsistent path handling for sentinel.bpf.o across different working directories - Add fallback path resolution in both lib.rs and real_ebpf_tests.rs - Eliminate "No such file or directory" errors when running from different directories - Support execution from both project root and kernel-agent subdirectory
1 parent 84022c6 commit c4589e2

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

kernel-agent/src/lib.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,36 @@ impl EbpfLoader {
350350

351351
#[cfg(target_os = "linux")]
352352
async fn initialize_linux(&self) -> Result<Object> {
353-
let mut object = ObjectBuilder::default()
354-
.open_file("kernel-agent/src/sentinel.bpf.o")
355-
.context("Cannot open eBPF object file")?
353+
// Try multiple possible paths for the eBPF object file
354+
let possible_paths = [
355+
"src/sentinel.bpf.o", // When running from kernel-agent directory
356+
"kernel-agent/src/sentinel.bpf.o", // When running from project root
357+
"./src/sentinel.bpf.o", // Explicit relative path
358+
"./kernel-agent/src/sentinel.bpf.o", // Explicit relative path from root
359+
];
360+
361+
let mut object_builder = None;
362+
let mut last_error = None;
363+
364+
for path in &possible_paths {
365+
match ObjectBuilder::default().open_file(path) {
366+
Ok(builder) => {
367+
debug!("Successfully found eBPF object at: {}", path);
368+
object_builder = Some(builder);
369+
break;
370+
}
371+
Err(e) => {
372+
debug!("Failed to open eBPF object at {}: {}", path, e);
373+
last_error = Some(e);
374+
}
375+
}
376+
}
377+
378+
let mut object = object_builder
379+
.ok_or_else(|| {
380+
anyhow::anyhow!("Cannot find eBPF object file at any of the expected paths: {:?}. Last error: {:?}",
381+
possible_paths, last_error)
382+
})?
356383
.load()
357384
.context("Cannot load eBPF program")?;
358385

kernel-agent/src/real_ebpf_tests.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,27 @@ impl RealEbpfTester {
147147
source: e
148148
})?;
149149

150-
// Use correct relative path from current working directory
151-
println!("✅ Using working eBPF object: src/sentinel.bpf.o");
152-
let bpf_object_path = "src/sentinel.bpf.o";
150+
// Try multiple possible paths for the eBPF object file
151+
let possible_paths = [
152+
"src/sentinel.bpf.o", // When running from kernel-agent directory
153+
"kernel-agent/src/sentinel.bpf.o", // When running from project root
154+
"./src/sentinel.bpf.o", // Explicit relative path
155+
"./kernel-agent/src/sentinel.bpf.o", // Explicit relative path from root
156+
];
157+
158+
let mut bpf_object_path = None;
159+
for path in &possible_paths {
160+
if std::path::Path::new(path).exists() {
161+
println!("✅ Using working eBPF object: {}", path);
162+
bpf_object_path = Some(path);
163+
break;
164+
}
165+
}
166+
167+
let bpf_object_path = bpf_object_path
168+
.ok_or_else(|| crate::error::SentinelError::EbpfLoad(
169+
format!("Cannot find eBPF object file at any of the expected paths: {:?}", possible_paths)
170+
))?;
153171

154172
let compile_output = std::process::Output {
155173
status: std::process::ExitStatus::from_raw(0),

0 commit comments

Comments
 (0)