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
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModulesList(HashSet<String>);
impl ModulesList
{
pub fn unload(moduleName: &str) -> Result<bool, io::Error>
{
let name = CString::new(moduleName).unwrap();
let flags = ::libc::O_NONBLOCK;
match unsafe { syscall(SYS_delete_module as i64, name.as_ptr(), flags) }
{
0 => Ok(true),
-1 => match errno().0
{
E::EPERM => Err(io::Error::new(ErrorKind::PermissionDenied, "permission denied")),
E::EBUSY => Err(io::Error::new(ErrorKind::PermissionDenied, "busy")),
E::ENOENT => Ok(false),
E::EWOULDBLOCK => Err(io::Error::new(ErrorKind::PermissionDenied, "In use")),
E::EFAULT => panic!("EFAULT should not occur"),
unknown @ _ => panic!("syscall delete_module failed with illegal error code '{}'", unknown),
},
illegal @ _ => panic!("syscall(SYS_finit_module) returned illegal value '{}'", illegal),
}
}
pub fn loadModuleFromKoFile(modulePath: &Path) -> Result<bool, io::Error>
{
let file = try!(OpenOptions::new().read(true).open(modulePath));
let fileDescriptor = file.as_raw_fd();
let options = CString::new("").unwrap();
let flags = 0;
match unsafe { syscall(SYS_finit_module as i64, fileDescriptor, options.as_ptr(), flags) }
{
0 => Ok(true),
-1 => match errno().0
{
E::EPERM => Err(io::Error::new(ErrorKind::PermissionDenied, "permission denied")),
unknown @ _ => Err(io::Error::new(ErrorKind::Other, format!("Error Code was '{}'", unknown))),
},
illegal @ _ => panic!("syscall(SYS_finit_module) returned illegal value '{}'", illegal),
}
}
pub fn loadModuleIfAbsentFromKoFile(&self, moduleName: &str, moduleFileBaseName: &str, modulesPath: &Path) -> Result<bool, io::Error>
{
if self.hasModule(moduleName)
{
Ok(false)
}
else
{
let mut modulePath = PathBuf::from(modulesPath);
modulePath.push(format!("{}.ko", moduleFileBaseName));
Self::loadModuleFromKoFile(&modulePath)
}
}
pub fn loadModuleIfAbsent(&self, moduleName: &str, moduleFileBaseName: &str) -> Result<(), ModProbeError>
{
if self.hasModule(moduleName)
{
Ok(())
}
else
{
modprobe(moduleFileBaseName)
}
}
pub fn hasAfPacket(&self) -> bool
{
self.hasModule("af_packet")
}
pub fn hasVirtioNet(&self) -> bool
{
self.hasModule("virtio_net")
}
pub fn hasKni(&self) -> bool
{
self.hasModule("rte_kni")
}
pub fn hasVfioPci(&self) -> bool
{
self.hasModule("vfio_pci")
}
pub fn hasModule(&self, moduleName: &str) -> bool
{
self.0.contains(moduleName)
}
pub fn parse(procPath: &Path) -> Result<Self, ModulesListParseError>
{
let mut modulesFilePath = PathBuf::from(procPath);
modulesFilePath.push("modules");
let openFile = try!(File::open(modulesFilePath));
let mut reader = BufReader::with_capacity(4096, openFile);
let mut modulesList = HashSet::new();
let mut lineCount = 0;
let mut line = String::with_capacity(512);
while try!(reader.read_line(&mut line)) > 0
{
{
let mut split = line.splitn(2, ' ');
let moduleName = split.next().unwrap();
if moduleName.is_empty()
{
return Err(ModulesListParseError::CouldNotParseEmptyModuleName(lineCount))
}
let isOriginal = modulesList.insert(moduleName.to_owned());
if !isOriginal
{
return Err(ModulesListParseError::DuplicateModuleName(lineCount, moduleName.to_owned()));
}
}
line.clear();
lineCount += 1;
}
Ok(ModulesList(modulesList))
}
}