From 41a2461756f069f5781271d43a3ef6f4f6c07500 Mon Sep 17 00:00:00 2001 From: Davide Bertola Date: Fri, 27 Sep 2024 14:49:32 +0200 Subject: [PATCH] Improve failure loggin when reading kernel config --- aya/src/bpf.rs | 77 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index bb979a39..79432d4d 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -159,37 +159,8 @@ fn compute_kconfig_definition(features: &Features) -> HashMap> { to_bytes(bpf_syscall_wrapper).to_vec(), ); - let mut raw_config_opt = None; - - let proc_config_path = PathBuf::from("/proc/config.gz"); - - if proc_config_path.exists() { - if let Ok(file) = File::open(proc_config_path) { - let mut file = GzDecoder::new(file); - let mut output = String::new(); - if file.read_to_string(&mut output).is_ok() { - raw_config_opt = Some(output); - } - } - } - - if raw_config_opt.is_none() { - if let Ok(release) = sys::kernel_release() { - let config_path = PathBuf::from("/boot").join(format!("config-{}", release)); - - if config_path.exists() { - if let Ok(mut file) = File::open(config_path) { - let mut output = String::new(); - if file.read_to_string(&mut output).is_ok() { - raw_config_opt = Some(output); - } - } - } - } - } - - if let Some(raw_config) = raw_config_opt { - for line in raw_config.split('\n') { + if let Some(raw_config) = read_kconfig() { + for line in raw_config.lines() { if !line.starts_with("CONFIG_") { continue; } @@ -230,6 +201,50 @@ fn compute_kconfig_definition(features: &Features) -> HashMap> { result } +fn read_kconfig() -> Option { + let config_path = PathBuf::from("/proc/config.gz"); + if config_path.exists() { + debug!("Found kernel config at {}", config_path.to_string_lossy()); + return read_kconfig_file(&config_path, true); + } + + let Ok(release) = sys::kernel_release() else { + return None; + }; + + let config_path = PathBuf::from("/boot").join(format!("config-{}", release)); + if config_path.exists() { + debug!("Found kernel config at {}", config_path.to_string_lossy()); + return read_kconfig_file(&config_path, false); + } + + None +} + +fn read_kconfig_file(path: &PathBuf, gzip: bool) -> Option { + let mut output = String::new(); + + let res = if gzip { + File::open(path) + .map(GzDecoder::new) + .and_then(|mut file| file.read_to_string(&mut output)) + } else { + File::open(path).and_then(|mut file| file.read_to_string(&mut output)) + }; + + match res { + Ok(_) => Some(output), + Err(e) => { + warn!( + "Unable to read kernel config {}: {:?}", + path.to_string_lossy(), + e + ); + None + } + } +} + /// Builder style API for advanced loading of eBPF programs. /// /// Loading eBPF code involves a few steps, including loading maps and applying