Skip to content

Commit

Permalink
Improve documentation of kmod functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bachp committed Sep 2, 2018
1 parent 9422dfb commit 2fb2fcd
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/kmod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Load and unload kernel modules.
//!
//! For more details see
use libc;
use std::ffi::CStr;
Expand All @@ -7,14 +9,21 @@ use std::os::unix::io::AsRawFd;
use errno::Errno;
use Result;

/// Loads an ELF image into kernel space, performs any necessary symbol relocations,
/// initializes module parameters to values provided by the caller, and then runs the module's init function.
/// This function requires privilege.
/// Loads a kernel module from a buffer.
///
/// The module_image argument points to a buffer containing the binary image to be loaded.
/// The module image should be a valid ELF image, built for the running kernel.
/// It loads an ELF image into kernel space,
/// performs any necessary symbol relocations,
/// initializes module parameters to values provided by the caller,
/// and then runs the module's init function.
///
/// The param_values argument is a string containing space-delimited specifications of the values for module parameters.
/// This function requires `CAP_SYS_MODULE` privilege.
///
/// The `module_image` argument points to a buffer containing the binary image
/// to be loaded. The buffer should contain a valid ELF image
/// built for the running kernel.
///
/// The `param_values` argument is a string containing space-delimited specifications
/// of the values for module parameters.
/// Each of the parameter specifications has the form:
///
/// `name[=value[,value...]]`
Expand All @@ -30,9 +39,10 @@ use Result;
/// let mut f = File::open("mykernel.ko").unwrap();
/// let mut contents: Vec<u8> = Vec::new();
/// f.read_to_end(&mut contents).unwrap();
/// init_module(&mut contents, &CString::new("").unwrap()).unwrap();
/// init_module(&mut contents, &CString::new("who=Rust when=Now,12").unwrap()).unwrap();
/// ```
///
/// See [`man init_module(2)`](http://man7.org/linux/man-pages/man2/init_module.2.html) for more information.
pub fn init_module(module_image: &[u8], param_values: &CStr) -> Result<()> {
let res = unsafe {
libc::syscall(
Expand Down Expand Up @@ -69,6 +79,7 @@ libc_bitflags!(
/// finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty()).unwrap();
/// ```
///
/// See [`man init_module(2)`](http://man7.org/linux/man-pages/man2/init_module.2.html) for more information.
pub fn finit_module<T: AsRawFd>(fd: &T, param_values: &CStr, flags: ModuleInitFlags) -> Result<()> {
let res = unsafe {
libc::syscall(
Expand All @@ -84,7 +95,9 @@ pub fn finit_module<T: AsRawFd>(fd: &T, param_values: &CStr, flags: ModuleInitFl

libc_bitflags!(
/// Flags used by `delete_module`.
/// see man delete_module(2) for a detailed description how these flags work.
///
/// See [`man delete_module(2)`](http://man7.org/linux/man-pages/man2/delete_module.2.html)
/// for a detailed description how these flags work.
pub struct DeleteModuleFlags: libc::c_int {
O_NONBLOCK;
O_TRUNC;
Expand All @@ -102,6 +115,7 @@ libc_bitflags!(
/// delete_module(&CString::new("mymod").unwrap(), DeleteModuleFlags::O_NONBLOCK).unwrap();
/// ```
///
/// See [`man delete_module(2)`](http://man7.org/linux/man-pages/man2/delete_module.2.html) for more information.
pub fn delete_module(name: &CStr, flags: DeleteModuleFlags) -> Result<()> {
let res = unsafe { libc::syscall(libc::SYS_delete_module, name.as_ptr(), flags.bits()) };

Expand Down

0 comments on commit 2fb2fcd

Please sign in to comment.