Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 86a8004

Browse files
committed
Make modinfo work properly (fixes #80)
1 parent 1e8ca1d commit 86a8004

File tree

14 files changed

+141
-37
lines changed

14 files changed

+141
-37
lines changed

hello-world/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Drop for HelloWorldModule {
3030

3131
linux_kernel_module::kernel_module!(
3232
HelloWorldModule,
33-
author: "Fish in a Barrel Contributors",
34-
description: "An extremely simple kernel module",
35-
license: "GPL"
33+
author: b"Fish in a Barrel Contributors",
34+
description: b"An extremely simple kernel module",
35+
license: b"GPL"
3636
);

src/lib.rs

+39-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ pub use crate::types::{CStr, Mode};
3636
///
3737
/// linux_kernel_module::kernel_module!(
3838
/// MyKernelModule,
39-
/// author: "Fish in a Barrel Contributors",
40-
/// description: "My very own kernel module!",
41-
/// license: "GPL"
39+
/// author: b"Fish in a Barrel Contributors",
40+
/// description: b"My very own kernel module!",
41+
/// license: b"GPL"
4242
/// );
4343
#[macro_export]
4444
macro_rules! kernel_module {
@@ -72,13 +72,43 @@ macro_rules! kernel_module {
7272
)*
7373
};
7474

75-
(@attribute $name:ident, $value:expr) => {
75+
// TODO: The modinfo attributes below depend on the compiler placing
76+
// the variables in order in the .modinfo section, so that you end up
77+
// with b"key=value\0" in order in the section. This is a reasonably
78+
// standard trick in C, but I'm not sure that rustc guarantees it.
79+
//
80+
// Ideally we'd be able to use concat_bytes! + stringify_bytes! +
81+
// some way of turning a string literal (or at least a string
82+
// literal token) into a bytes literal, and get a single static
83+
// [u8; * N] with the whole thing, but those don't really exist yet.
84+
// Most of the alternatives (e.g. .as_bytes() as a const fn) give
85+
// you a pointer, not an array, which isn't right.
86+
87+
(@attribute author, $value:expr) => {
88+
#[link_section = ".modinfo"]
89+
pub static AUTHOR_KEY: [u8; 7] = *b"author=";
90+
#[link_section = ".modinfo"]
91+
pub static AUTHOR_VALUE: [u8; $value.len()] = *$value;
92+
#[link_section = ".modinfo"]
93+
pub static AUTHOR_NUL: [u8; 1] = *b"\0";
94+
};
95+
96+
(@attribute description, $value:expr) => {
97+
#[link_section = ".modinfo"]
98+
pub static DESCRIPTION_KEY: [u8; 12] = *b"description=";
99+
#[link_section = ".modinfo"]
100+
pub static DESCRIPTION_VALUE: [u8; $value.len()] = *$value;
101+
#[link_section = ".modinfo"]
102+
pub static DESCRIPTION_NUL: [u8; 1] = *b"\0";
103+
};
104+
105+
(@attribute license, $value:expr) => {
106+
#[link_section = ".modinfo"]
107+
pub static LICENSE_KEY: [u8; 8] = *b"license=";
108+
#[link_section = ".modinfo"]
109+
pub static LICENSE_VALUE: [u8; $value.len()] = *$value;
76110
#[link_section = ".modinfo"]
77-
#[allow(non_upper_case_globals)]
78-
// TODO: Generate a name the same way the kernel's `__MODULE_INFO` does.
79-
// TODO: This needs to be a `[u8; _]`, since the kernel defines this as a `const char []`.
80-
// See https://github.com/rust-lang/rfcs/pull/2545
81-
pub static $name: &'static [u8] = concat!(stringify!($name), "=", $value, '\0').as_bytes();
111+
pub static LICENSE_NUL: [u8; 1] = *b"\0";
82112
};
83113
}
84114

tests/chrdev-region-allocation/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl linux_kernel_module::KernelModule for ChrdevRegionAllocationTestModule {
2020

2121
linux_kernel_module::kernel_module!(
2222
ChrdevRegionAllocationTestModule,
23-
author: "Fish in a Barrel Contributors",
24-
description: "A module for testing character device region allocation",
25-
license: "GPL"
23+
author: b"Fish in a Barrel Contributors",
24+
description: b"A module for testing character device region allocation",
25+
license: b"GPL"
2626
);

tests/chrdev/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl linux_kernel_module::KernelModule for ChrdevTestModule {
124124

125125
linux_kernel_module::kernel_module!(
126126
ChrdevTestModule,
127-
author: "Fish in a Barrel Contributors",
128-
description: "A module for testing character devices",
129-
license: "GPL"
127+
author: b"Fish in a Barrel Contributors",
128+
description: b"A module for testing character devices",
129+
license: b"GPL"
130130
);

tests/filesystem/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl linux_kernel_module::KernelModule for TestFSModule {
2727

2828
linux_kernel_module::kernel_module!(
2929
TestFSModule,
30-
author: "Fish in a Barrel Contributors",
31-
description: "A module for testing filesystem::register",
32-
license: "GPL"
30+
author: b"Fish in a Barrel Contributors",
31+
description: b"A module for testing filesystem::register",
32+
license: b"GPL"
3333
);

tests/modinfo/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "modinfo-tests"
3+
version = "0.1.0"
4+
authors = ["Alex Gaynor <[email protected]>", "Geoffrey Thomas <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["staticlib"]
9+
test = false
10+
11+
[features]
12+
default = ["linux-kernel-module"]
13+
14+
[dependencies]
15+
linux-kernel-module = { path = "../..", optional = true }
16+
17+
[dev-dependencies]
18+
kernel-module-testlib = { path = "../../testlib" }

tests/modinfo/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![no_std]
2+
3+
use linux_kernel_module;
4+
5+
struct ModinfoTestModule;
6+
7+
impl linux_kernel_module::KernelModule for ModinfoTestModule {
8+
fn init() -> linux_kernel_module::KernelResult<Self> {
9+
Ok(ModinfoTestModule)
10+
}
11+
}
12+
13+
linux_kernel_module::kernel_module!(
14+
ModinfoTestModule,
15+
author: b"Fish in a Barrel Contributors",
16+
description: b"Empty module for testing modinfo",
17+
license: b"GPL"
18+
);

tests/modinfo/tests/tests.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::env;
2+
use std::fs;
3+
use std::path::Path;
4+
use std::process::Command;
5+
6+
use kernel_module_testlib::with_kernel_module;
7+
8+
#[test]
9+
fn test_modinfo() {
10+
let module = env::var("KERNEL_MODULE").unwrap();
11+
12+
for (key, value) in &[
13+
("author", "Fish in a Barrel Contributors"),
14+
("description", "Empty module for testing modinfo"),
15+
("license", "GPL"),
16+
] {
17+
let modinfo = Command::new("modinfo")
18+
.arg("-F")
19+
.arg(key)
20+
.arg(&module)
21+
.output()
22+
.unwrap();
23+
assert!(modinfo.status.success());
24+
assert_eq!(&std::str::from_utf8(&modinfo.stdout).unwrap().trim(), value);
25+
}
26+
}
27+
28+
#[test]
29+
fn test_no_proprietary_taint() {
30+
let module = env::var("KERNEL_MODULE").unwrap();
31+
let module_name = Path::new(&module).file_stem().unwrap();
32+
let sysfs_path = Path::new("/sys/module").join(module_name).join("taint");
33+
34+
with_kernel_module(|| {
35+
let taints = fs::read_to_string(&sysfs_path).unwrap();
36+
assert!(!taints.contains("P"));
37+
});
38+
}

tests/printk/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl linux_kernel_module::KernelModule for PrintkTestModule {
1616

1717
linux_kernel_module::kernel_module!(
1818
PrintkTestModule,
19-
author: "Fish in a Barrel Contributors",
20-
description: "A module for testing println!()",
21-
license: "GPL"
19+
author: b"Fish in a Barrel Contributors",
20+
description: b"A module for testing println!()",
21+
license: b"GPL"
2222
);

tests/random/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl linux_kernel_module::KernelModule for RandomTestModule {
4444

4545
linux_kernel_module::kernel_module!(
4646
RandomTestModule,
47-
author: "Fish in a Barrel Contributors",
48-
description: "A module for testing the CSPRNG",
49-
license: "GPL"
47+
author: b"Fish in a Barrel Contributors",
48+
description: b"A module for testing the CSPRNG",
49+
license: b"GPL"
5050
);

tests/run_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def run(*args, **kwargs):
1818

1919

2020
def main(argv):
21-
for path in os.listdir(BASE_DIR):
21+
for path in argv[1:] or os.listdir(BASE_DIR):
2222
if (
2323
not os.path.isdir(os.path.join(BASE_DIR, path)) or
2424
not os.path.exists(os.path.join(BASE_DIR, path, "tests"))

tests/sysctl-get/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Drop for SysctlGetTestModule {
3838

3939
linux_kernel_module::kernel_module!(
4040
SysctlGetTestModule,
41-
author: "Fish in a Barrel Contributors",
42-
description: "A module for testing sysctls",
43-
license: "GPL"
41+
author: b"Fish in a Barrel Contributors",
42+
description: b"A module for testing sysctls",
43+
license: b"GPL"
4444
);

tests/sysctl/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl linux_kernel_module::KernelModule for SysctlTestModule {
3333

3434
linux_kernel_module::kernel_module!(
3535
SysctlTestModule,
36-
author: "Fish in a Barrel Contributors",
37-
description: "A module for testing sysctls",
38-
license: "GPL"
36+
author: b"Fish in a Barrel Contributors",
37+
description: b"A module for testing sysctls",
38+
license: b"GPL"
3939
);

tests/utils/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl linux_kernel_module::KernelModule for UtilsTestModule {
1515

1616
linux_kernel_module::kernel_module!(
1717
UtilsTestModule,
18-
author: "Fish in a Barrel Contributors",
19-
description: "A module for testing various utilities",
20-
license: "GPL"
18+
author: b"Fish in a Barrel Contributors",
19+
description: b"A module for testing various utilities",
20+
license: b"GPL"
2121
);

0 commit comments

Comments
 (0)