Skip to content

Commit

Permalink
Improve failure loggin when reading kernel config
Browse files Browse the repository at this point in the history
  • Loading branch information
davibe committed Sep 27, 2024
1 parent 005fb60 commit d1e8243
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
4 changes: 3 additions & 1 deletion aya-obj/src/btf/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ impl Btf {
target_var_name: &str,
) -> Result<(String, Var), BtfError> {
for t in &self.types.types {
let BtfType::DataSec(d) = t else { continue; };
let BtfType::DataSec(d) = t else {
continue;
};
let sec_name = self.string_at(d.name_offset)?;

for d in &d.entries {
Expand Down
77 changes: 46 additions & 31 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,37 +159,8 @@ fn compute_kconfig_definition(features: &Features) -> HashMap<String, Vec<u8>> {
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;
}
Expand Down Expand Up @@ -230,6 +201,50 @@ fn compute_kconfig_definition(features: &Features) -> HashMap<String, Vec<u8>> {
result
}

fn read_kconfig() -> Option<String> {
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<String> {
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
Expand Down

0 comments on commit d1e8243

Please sign in to comment.