Skip to content

Commit 43210ab

Browse files
committed
Add wrapper for linux kernel module loading
- init_module and finit_module to load kernel modules - delete_module to unload kernel modules
1 parent 237ec7b commit 43210ab

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/kmod.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Load and unload kernel modules.
2+
3+
use libc;
4+
use std::os::unix::io::AsRawFd;
5+
use std::ffi::CStr;
6+
7+
use errno::Errno;
8+
use Result;
9+
10+
/// Loads an ELF image into kernel space, performs any necessary symbol relocations,
11+
/// initializes module parameters to values provided by the caller, and then runs the module's init function.
12+
/// This function requires privilege.
13+
///
14+
/// The module_image argument points to a buffer containing the binary image to be loaded.
15+
/// The module image should be a valid ELF image, built for the running kernel.
16+
///
17+
/// The param_values argument is a string containing space-delimited specifications of the values for module parameters.
18+
/// Each of the parameter specifications has the form:
19+
///
20+
/// `name[=value[,value...]]`
21+
///
22+
/// Example usage:
23+
///
24+
/// ```
25+
/// let mut f = File::open("mykernel.ko").unwrap();
26+
/// let mut contents: Vec<u8> = Vec::new();
27+
/// f.read_to_end(&mut contents).unwrap();
28+
/// init_module(&mut contents, &CString::new("").unwrap()).unwrap();
29+
/// ```
30+
///
31+
pub fn init_module(module_image: &[u8], param_values: &CStr) -> Result<()> {
32+
let res = unsafe {
33+
libc::syscall(
34+
libc::SYS_init_module,
35+
module_image.as_ptr(),
36+
module_image.len(),
37+
param_values.as_ptr(),
38+
)
39+
};
40+
41+
Errno::result(res).map(drop)
42+
}
43+
44+
45+
libc_bitflags!(
46+
/// Flags used by the `finit_module` function.
47+
pub struct ModuleInitFlags: libc::c_uint {
48+
/// Ignore symbol version hashes.
49+
MODULE_INIT_IGNORE_MODVERSIONS;
50+
/// Ignore kernel version magic.
51+
MODULE_INIT_IGNORE_VERMAGIC;
52+
}
53+
);
54+
55+
/// Loads a kernel module from a given file descriptor.
56+
///
57+
/// Example usage:
58+
///
59+
/// ```
60+
/// let f = File::open("mymod.ko").unwrap();
61+
/// finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty()).unwrap();
62+
/// ```
63+
///
64+
pub fn finit_module<T: AsRawFd>(fd: &T, param_values: &CStr, flags:ModuleInitFlags) -> Result<()> {
65+
let res = unsafe { libc::syscall(libc::SYS_finit_module, fd.as_raw_fd(), param_values.as_ptr(), flags.bits()) };
66+
67+
Errno::result(res).map(drop)
68+
}
69+
70+
libc_bitflags!(
71+
/// Flags used by `delete_module`.
72+
pub struct OFlags: libc::c_int {
73+
O_NONBLOCK;
74+
O_TRUNC;
75+
}
76+
);
77+
78+
/// Unloads the kernel module with the given name.
79+
///
80+
/// Example usage:
81+
///
82+
/// ```
83+
/// delete_module(&CString::new("mymod").unwrap(), OFlags::O_NONBLOCK).unwrap();
84+
/// ```
85+
///
86+
pub fn delete_module(name: &CStr, flags: OFlags) -> Result<()> {
87+
let res = unsafe { libc::syscall(libc::SYS_delete_module, name.as_ptr(), flags.bits()) };
88+
89+
Errno::result(res).map(drop)
90+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub mod fcntl;
4343
target_os = "openbsd"))]
4444
pub mod ifaddrs;
4545
#[cfg(any(target_os = "linux", target_os = "android"))]
46+
pub mod kmod;
47+
#[cfg(any(target_os = "linux", target_os = "android"))]
4648
pub mod mount;
4749
#[cfg(any(target_os = "dragonfly",
4850
target_os = "freebsd",

0 commit comments

Comments
 (0)