Improve failure loggin when reading kernel config

pull/1017/merge^2
Davide Bertola 4 months ago
parent 8b2ef5d5c1
commit 41a2461756

@ -159,37 +159,8 @@ fn compute_kconfig_definition(features: &Features) -> HashMap<String, Vec<u8>> {
to_bytes(bpf_syscall_wrapper).to_vec(), to_bytes(bpf_syscall_wrapper).to_vec(),
); );
let mut raw_config_opt = None; if let Some(raw_config) = read_kconfig() {
for line in raw_config.lines() {
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 !line.starts_with("CONFIG_") { if !line.starts_with("CONFIG_") {
continue; continue;
} }
@ -230,6 +201,50 @@ fn compute_kconfig_definition(features: &Features) -> HashMap<String, Vec<u8>> {
result 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. /// Builder style API for advanced loading of eBPF programs.
/// ///
/// Loading eBPF code involves a few steps, including loading maps and applying /// Loading eBPF code involves a few steps, including loading maps and applying

Loading…
Cancel
Save