Add wrapper for linux kernel module loading #930
Conversation
bachp
commented
Jul 20, 2018
|
/// init_module(&mut contents, &CString::new("").unwrap()).unwrap(); | ||
/// ``` | ||
/// | ||
pub fn init_module(module_image: &[u8], param_values: &CStr) -> Result<i64> { |
bachp
Jul 20, 2018
Author
Contributor
Is there a more flexible way then passing the binary as &[u8]
?
Is there a more flexible way then passing the binary as &[u8]
?
@@ -43,6 +43,8 @@ pub mod fcntl; | |||
target_os = "openbsd"))] | |||
pub mod ifaddrs; | |||
#[cfg(any(target_os = "linux", target_os = "android"))] | |||
pub mod kmod; |
bachp
Jul 20, 2018
Author
Contributor
Not sure if this is the right place to put this nor if this is the proper name. Inputs are welcome.
Not sure if this is the right place to put this nor if this is the proper name. Inputs are welcome.
I think it looks good overall, but it could use some tests. Is there a trivial kernel module that you could use for testing? It's ok to have tests that can only run as root; we already have a few. |
@asomers What is the best way to run tests as root? Should I run all of the build as root or is there a better way? |
Do it just like the |
@asomers I added some tests |
printk(KERN_INFO "Goodbye world 1.\n"); | ||
} | ||
|
||
MODULE_LICENSE("GPL"); |
asomers
Aug 5, 2018
Member
Why GPL? The rest of Nix uses MIT.
Why GPL? The rest of Nix uses MIT.
bachp
Aug 5, 2018
Author
Contributor
The module is taken from tldp.org where it is licensed GPL.
The module is taken from tldp.org where it is licensed GPL.
Susurrus
Aug 6, 2018
Contributor
This also isn't specific enough, as the GPL has versions. I assume that means 2.0. Let's get a new one written that we can license MIT. Also, can we name it hello.c
. That -1
is really killing me.
This also isn't specific enough, as the GPL has versions. I assume that means 2.0. Let's get a new one written that we can license MIT. Also, can we name it hello.c
. That -1
is really killing me.
bachp
Aug 6, 2018
Author
Contributor
A pure MIT licensed module would taint the kernel if loaded. The option I see would be: Dual MIT/GPL
. I willl create a new module and license it accordingly.
A pure MIT licensed module would taint the kernel if loaded. The option I see would be: Dual MIT/GPL
. I willl create a new module and license it accordingly.
use std::process::Command; | ||
|
||
fn compile_kernel_module() -> String { | ||
#[allow(unused_variables)] |
asomers
Aug 5, 2018
Member
The #[allow(unused_variables)]
syntax is holdover from an older version of rustc. Nowadays you can just do let _, = ...
and it won't complain.
The #[allow(unused_variables)]
syntax is holdover from an older version of rustc. Nowadays you can just do let _, = ...
and it won't complain.
bachp
Aug 5, 2018
Author
Contributor
I tried it and it doesn't work. I'm not sure what's the scope of _
but it seems it doesn't stick around till the end of the block.
I tried it and it doesn't work. I'm not sure what's the scope of _
but it seems it doesn't stick around till the end of the block.
asomers
Aug 5, 2018
Member
Sorry, my last comment had a typo. You need to use let _m = ...
, not let _ = ...
. There is a difference between _m
and _
.
Sorry, my last comment had a typo. You need to use let _m = ...
, not let _ = ...
. There is a difference between _m
and _
.
bachp
Aug 5, 2018
Author
Contributor
Good to know. I didn't know they behave differently. I update the code.
Good to know. I didn't know they behave differently. I update the code.
On my Debian system the test_finit_and_delete_module test fails with this error:
|
@asomers You most probably don't have the kernel sources installed on your system. Building a kernel module requires the kernel source. I'm not sure what packages provides these on Debian. |
I installed the linux-source package, and now I have sources in /usr/src, but I still get the same error. The fact that the error message mentions a "build" directory suggests that maybe there's a missing step? |
I tried on a debian system and the required package is You can install it via:
|
Ok, installing linux-headers did the trick. But now "cargo test" is trying to create some temporary files in nix's test directory. Could you fix it so those are created in TMPDIR instead?
|
I'd really like to see the kernel module we use allow for parameters to be passed. I don't see those tested as part of this, and I think it's important that it get tested, ideally having 2 parameters or more. |
printk(KERN_INFO "Goodbye world 1.\n"); | ||
} | ||
|
||
MODULE_LICENSE("GPL"); |
Susurrus
Aug 6, 2018
Contributor
This also isn't specific enough, as the GPL has versions. I assume that means 2.0. Let's get a new one written that we can license MIT. Also, can we name it hello.c
. That -1
is really killing me.
This also isn't specific enough, as the GPL has versions. I assume that means 2.0. Let's get a new one written that we can license MIT. Also, can we name it hello.c
. That -1
is really killing me.
@@ -0,0 +1,7 @@ | |||
obj-m += hello-1.o |
Susurrus
Aug 6, 2018
Contributor
Also make this hello.o
Also make this hello.o
mod sys; | ||
mod test_fcntl; | ||
#[cfg(any(target_os = "linux", target_os = "android"))] |
Susurrus
Aug 6, 2018
Contributor
Please alphabetize the OSes
Please alphabetize the OSes
@@ -43,6 +43,8 @@ pub mod fcntl; | |||
target_os = "openbsd"))] | |||
pub mod ifaddrs; | |||
#[cfg(any(target_os = "linux", target_os = "android"))] | |||
pub mod kmod; | |||
#[cfg(any(target_os = "linux", target_os = "android"))] |
Susurrus
Aug 6, 2018
Contributor
Please alphabetize here.
Please alphabetize here.
@@ -43,6 +43,8 @@ pub mod fcntl; | |||
target_os = "openbsd"))] | |||
pub mod ifaddrs; | |||
#[cfg(any(target_os = "linux", target_os = "android"))] |
Susurrus
Aug 6, 2018
Contributor
Please alphabetize this.
Please alphabetize this.
|
||
/// Loads a kernel module from a given file descriptor. | ||
/// | ||
/// Example usage: |
Susurrus
Aug 6, 2018
Contributor
Same as the above, use # Example
instead.
Same as the above, use # Example
instead.
libc_bitflags!( | ||
/// Flags used by `delete_module`. | ||
pub struct OFlags: libc::c_int { | ||
O_NONBLOCK; |
Susurrus
Aug 6, 2018
Contributor
Please add doccomments for these items.
Please add doccomments for these items.
|
||
libc_bitflags!( | ||
/// Flags used by `delete_module`. | ||
pub struct OFlags: libc::c_int { |
Susurrus
Aug 6, 2018
Contributor
Can we rename this to OutputFlags
or something less short-hand?
Can we rename this to OutputFlags
or something less short-hand?
|
||
/// Unloads the kernel module with the given name. | ||
/// | ||
/// Example usage: |
Susurrus
Aug 6, 2018
Contributor
# Example
# Example
Did you try simply cd'ing to ${TMPDIR:-/tmp} before running make? |
@asomers I don't think switching the workdir before doing make would work as the workdir needs to be in the same directory. One option would be to copy everything to a TMP location, do the build and then delete it again after the test. But it seems hacky. |
@bachp could you try that then? No test should be creating files in the source directory. I can't even run these tests, because my source directory is mounted over NFS with rootsquash on. |
@asomers I bumped the minimum required rust version to 1.24.1 as this is the version packaged in Debian. So it should allow them to still package rust tools depending on nix. I think the freebsd jobs should be updated to this version too. |
@Susurrus I incorporated your feedback into the documentation and added some additional information and links. |
Why 1.24.1? If Nix only depends on 1.22.0 features, then the minimum rustc version should be 1.22.0. That shouldn't be a problem for building Debian packages. Nor should Nix depend in any way on Debian. |
@asomers Rust 1.22 produces some compile errors that are not present in 1.20 and are no longer present in 1.24.1. See: https://travis-ci.org/nix-rust/nix/builds/413801114 for details on the error. |
That looks like a bug in Nix. |
But why does this warning only pop up with 1.22 and not with 1.24.1 or 1.28? |
I updated the MR to build with 1.22.1 and changed the offending code. But travis seems to have failed for some other reason now. Seems unrelated to the change. |
The Travis failures were spurious. Restarting the jobs fixed them. I'm upgrading buildbot now. |
Anything else that needs to be fixed? |
|
||
### Changed | ||
- Bump minimum tested Rust version to 1.22.1. |
Susurrus
Aug 15, 2018
Contributor
"tested" -> "required"
"tested" -> "required"
Rebased after #900 got merged |
I rebased again to fix the changelog conflict. |
This looks pretty good to me, so I think it's time to refactor the commits and then we can merge. Please refactor this PR into 4 commits in the following order:
|
This avoids having both 0.4 and 0.5 (required by tempfile)
This macro can be used in tests to skip the test if it requires root to sucssfully run.
- init_module and finit_module to load kernel modules - delete_module to unload kernel modules Signed-off-by: Pascal Bach <pascal.bach@nextrem.ch>
@Susurrus PR reworked into 4 logical commits. |
Anything still missing? |
bors r+ |