Skip to content

Commit 30739be

Browse files
authored
Add init_module, finit_module, and delete_module syscalls (#1005)
1 parent bbe8464 commit 30739be

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/backend/linux_raw/system/syscalls.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
use super::types::RawUname;
99
use crate::backend::c;
10-
use crate::backend::conv::{c_int, ret, ret_infallible, slice};
10+
use crate::backend::conv::{c_int, ret, ret_error, ret_infallible, slice};
11+
#[cfg(feature = "fs")]
12+
use crate::fd::BorrowedFd;
13+
use crate::ffi::CStr;
1114
use crate::io;
1215
use crate::system::{RebootCommand, Sysinfo};
1316
use core::mem::MaybeUninit;
@@ -47,3 +50,38 @@ pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
4750
))
4851
}
4952
}
53+
54+
#[cfg(any(target_os = "linux", target_os = "android"))]
55+
#[inline]
56+
pub(crate) fn init_module(image: &[u8], param_values: &CStr) -> io::Errno {
57+
let (image, len) = slice(image);
58+
unsafe {
59+
ret_error(syscall_readonly!(
60+
__NR_init_module,
61+
image,
62+
len,
63+
param_values
64+
))
65+
}
66+
}
67+
68+
#[cfg(any(target_os = "linux", target_os = "android"))]
69+
#[cfg(feature = "fs")]
70+
#[inline]
71+
pub(crate) fn finit_module(fd: BorrowedFd<'_>, param_values: &CStr, flags: c::c_int) -> io::Errno {
72+
unsafe {
73+
ret_error(syscall_readonly!(
74+
__NR_finit_module,
75+
fd,
76+
param_values,
77+
c_int(flags)
78+
))
79+
}
80+
}
81+
82+
#[cfg(any(target_os = "linux", target_os = "android"))]
83+
#[cfg(feature = "fs")]
84+
#[inline]
85+
pub(crate) fn delete_module(name: &CStr, flags: c::c_int) -> io::Errno {
86+
unsafe { ret_error(syscall_readonly!(__NR_delete_module, name, c_int(flags))) }
87+
}

src/system.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ use core::fmt;
1717
#[cfg(linux_kernel)]
1818
pub use backend::system::types::Sysinfo;
1919

20+
#[cfg(linux_raw)]
21+
use crate::fd::AsFd;
22+
#[cfg(linux_raw)]
23+
use core::ffi::c_int;
24+
2025
/// `uname()`—Returns high-level information about the runtime OS and
2126
/// hardware.
2227
///
@@ -218,3 +223,40 @@ pub enum RebootCommand {
218223
pub fn reboot(cmd: RebootCommand) -> io::Result<()> {
219224
backend::system::syscalls::reboot(cmd)
220225
}
226+
227+
/// `init_module`—Load a kernel module
228+
///
229+
/// # References
230+
/// - [Linux]
231+
///
232+
/// [Linux]: https://man7.org/linux/man-pages/man2/init_module.2.html
233+
#[inline]
234+
#[cfg(linux_raw)]
235+
pub fn init_module(image: &[u8], param_values: &CStr) -> io::Errno {
236+
backend::system::syscalls::init_module(image, param_values)
237+
}
238+
239+
/// `finit_module`—Load a kernel module from a file descriptor
240+
///
241+
/// # References
242+
/// - [Linux]
243+
///
244+
/// [Linux]: https://man7.org/linux/man-pages/man2/finit_module.2.html
245+
#[inline]
246+
#[cfg(linux_raw)]
247+
#[cfg(feature = "fs")]
248+
pub fn finit_module<Fd: AsFd>(fd: Fd, param_values: &CStr, flags: c_int) -> io::Errno {
249+
backend::system::syscalls::finit_module(fd.as_fd(), param_values, flags)
250+
}
251+
252+
/// `delete_module`—Unload a kernel module
253+
///
254+
/// # References
255+
/// - [Linux]
256+
///
257+
/// [Linux]: https://man7.org/linux/man-pages/man2/delete_module.2.html
258+
#[inline]
259+
#[cfg(linux_raw)]
260+
pub fn delete_module(name: &CStr, flags: c_int) -> io::Errno {
261+
backend::system::syscalls::delete_module(name, flags)
262+
}

0 commit comments

Comments
 (0)