1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use flate2::bufread::GzDecoder;
use nix::sys::utsname;
use std::{
collections::HashMap,
fmt::Display,
fs::File,
io::{BufRead, BufReader},
};
use thiserror::Error as ThisError;
#[cfg(feature = "serde")]
use bpf_rs_macros::SerializeFromDisplay;
#[cfg(feature = "serde")]
use serde::Serialize;
pub const KERNEL_CONFIG_KEYS: [&'static str; 35] = [
"CONFIG_BPF",
"CONFIG_BPF_SYSCALL",
"CONFIG_HAVE_EBPF_JIT",
"CONFIG_BPF_JIT",
"CONFIG_BPF_JIT_ALWAYS_ON",
"CONFIG_DEBUG_INFO_BTF",
"CONFIG_DEBUG_INFO_BTF_MODULES",
"CONFIG_CGROUPS",
"CONFIG_CGROUP_BPF",
"CONFIG_CGROUP_NET_CLASSID",
"CONFIG_SOCK_CGROUP_DATA",
"CONFIG_BPF_EVENTS",
"CONFIG_KPROBE_EVENTS",
"CONFIG_UPROBE_EVENTS",
"CONFIG_TRACING",
"CONFIG_FTRACE_SYSCALLS",
"CONFIG_FUNCTION_ERROR_INJECTION",
"CONFIG_BPF_KPROBE_OVERRIDE",
"CONFIG_NET",
"CONFIG_XDP_SOCKETS",
"CONFIG_LWTUNNEL_BPF",
"CONFIG_NET_ACT_BPF",
"CONFIG_NET_CLS_BPF",
"CONFIG_NET_CLS_ACT",
"CONFIG_NET_SCH_INGRESS",
"CONFIG_XFRM",
"CONFIG_IP_ROUTE_CLASSID",
"CONFIG_IPV6_SEG6_BPF",
"CONFIG_BPF_LIRC_MODE2",
"CONFIG_BPF_STREAM_PARSER",
"CONFIG_NETFILTER_XT_MATCH_BPF",
"CONFIG_BPFILTER",
"CONFIG_BPFILTER_UMH",
"CONFIG_TEST_BPF",
"CONFIG_HZ",
];
#[non_exhaustive]
#[derive(ThisError, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum KernelConfigError {
#[error("can't open file")]
NotFound,
#[error("file data format unknown")]
ContentsUnknown,
#[error("can't read from file")]
ReadFail,
}
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(SerializeFromDisplay))]
pub enum ConfigValue {
Y,
N,
M,
Other(String),
}
impl Display for ConfigValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConfigValue::Y => write!(f, "y"),
ConfigValue::N => write!(f, "n"),
ConfigValue::M => write!(f, "m"),
ConfigValue::Other(value) => write!(f, "{}", value),
}
}
}
type KernelConfigValues = HashMap<&'static str, ConfigValue>;
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct KernelConfig {
#[cfg_attr(feature = "serde", serde(flatten))]
pub values: KernelConfigValues,
}
impl KernelConfig {
fn probe_kernel_config() -> Result<KernelConfigValues, KernelConfigError> {
let utsn = utsname::uname();
let config_reader: Box<dyn BufRead> =
match File::open(format!("/boot/config-{}", utsn.release())) {
Err(_) => {
let compressed_config =
File::open("/proc/config.gz").map_err(|_| KernelConfigError::NotFound)?;
let decoder = GzDecoder::new(BufReader::new(compressed_config));
Box::new(BufReader::new(decoder))
}
Ok(f) => Box::new(BufReader::new(f)),
};
let mut lines_iter = config_reader.lines();
let _ = lines_iter
.next()
.transpose()
.map_err(|_| KernelConfigError::ReadFail)?
.ok_or(KernelConfigError::ReadFail)?;
let line = lines_iter
.next()
.transpose()
.map_err(|_| KernelConfigError::ReadFail)?
.ok_or(KernelConfigError::ReadFail)?;
if !line.starts_with("# Automatically generated file; DO NOT EDIT.") {
return Err(KernelConfigError::ContentsUnknown);
}
let mut options = HashMap::new();
for line_item in lines_iter {
let line = line_item.map_err(|_| KernelConfigError::ReadFail)?;
if !line.starts_with("CONFIG_") {
continue;
}
let pieces: Vec<_> = line.split("=").collect();
if pieces.len() < 2 {
continue;
}
for key in KERNEL_CONFIG_KEYS {
if key != pieces[0] {
continue;
}
options.insert(
key,
match pieces[1] {
"y" => ConfigValue::Y,
"m" => ConfigValue::M,
"n" => ConfigValue::N,
_ => ConfigValue::Other(pieces[1].to_string()),
},
);
}
}
return Ok(options);
}
}
pub fn features() -> Result<KernelConfig, KernelConfigError> {
return Ok(KernelConfig {
values: KernelConfig::probe_kernel_config()?,
});
}