-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wrapper for linux kernel module loading #930
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//! Load and unload kernel modules. | ||
//! | ||
//! For more details see | ||
|
||
use libc; | ||
use std::ffi::CStr; | ||
use std::os::unix::io::AsRawFd; | ||
|
||
use errno::Errno; | ||
use Result; | ||
|
||
/// Loads a kernel module from a buffer. | ||
/// | ||
/// 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. | ||
/// | ||
/// 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...]]` | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// use std::fs::File; | ||
/// use std::io::Read; | ||
/// use std::ffi::CString; | ||
/// use nix::kmod::init_module; | ||
/// | ||
/// 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("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( | ||
libc::SYS_init_module, | ||
module_image.as_ptr(), | ||
module_image.len(), | ||
param_values.as_ptr(), | ||
) | ||
}; | ||
|
||
Errno::result(res).map(drop) | ||
} | ||
|
||
libc_bitflags!( | ||
/// Flags used by the `finit_module` function. | ||
pub struct ModuleInitFlags: libc::c_uint { | ||
/// Ignore symbol version hashes. | ||
MODULE_INIT_IGNORE_MODVERSIONS; | ||
/// Ignore kernel version magic. | ||
MODULE_INIT_IGNORE_VERMAGIC; | ||
} | ||
); | ||
|
||
/// Loads a kernel module from a given file descriptor. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// use std::fs::File; | ||
/// use std::ffi::CString; | ||
/// use nix::kmod::{finit_module, ModuleInitFlags}; | ||
/// | ||
/// let f = File::open("mymod.ko").unwrap(); | ||
/// 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( | ||
libc::SYS_finit_module, | ||
fd.as_raw_fd(), | ||
param_values.as_ptr(), | ||
flags.bits(), | ||
) | ||
}; | ||
|
||
Errno::result(res).map(drop) | ||
} | ||
|
||
libc_bitflags!( | ||
/// Flags used by `delete_module`. | ||
/// | ||
/// 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; | ||
} | ||
); | ||
|
||
/// Unloads the kernel module with the given name. | ||
bachp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// use std::ffi::CString; | ||
/// use nix::kmod::{delete_module, DeleteModuleFlags}; | ||
/// | ||
/// 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()) }; | ||
|
||
Errno::result(res).map(drop) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
obj-m += hello.o | ||
|
||
all: | ||
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules | ||
|
||
clean: | ||
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-2.0+ or MIT | ||
*/ | ||
#include <linux/module.h> | ||
#include <linux/kernel.h> | ||
|
||
static int number= 1; | ||
static char *who = "World"; | ||
|
||
module_param(number, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||
MODULE_PARM_DESC(myint, "Just some number"); | ||
module_param(who, charp, 0000); | ||
MODULE_PARM_DESC(who, "Whot to greet"); | ||
|
||
int init_module(void) | ||
{ | ||
printk(KERN_INFO "Hello %s (%d)!\n", who, number); | ||
return 0; | ||
} | ||
|
||
void cleanup_module(void) | ||
{ | ||
printk(KERN_INFO "Goodbye %s (%d)!\n", who, number); | ||
} | ||
|
||
MODULE_LICENSE("Dual MIT/GPL"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this blank line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done