22#![feature(const_str_as_bytes)]
33
44use core::sync::atomic::{AtomicBool, Ordering};
5+ use core::convert::TryInto;
56
67use serde::Serialize;
78use serde_json_core;
89
910use linux_kernel_module::sysctl::Sysctl;
1011use linux_kernel_module::Mode;
11- use linux_kernel_module::println;
12+ use linux_kernel_module::error;
13+
14+ static A: AtomicBool = AtomicBool::new(false);
15+ static B: AtomicBool = AtomicBool::new(false);
16+ static C: AtomicBool = AtomicBool::new(false);
17+
18+ struct JsonChrdev;
19+
20+ impl linux_kernel_module::chrdev::FileOperations for JsonChrdev {
21+ const VTABLE: linux_kernel_module::chrdev::FileOperationsVtable =
22+ linux_kernel_module::chrdev::FileOperationsVtable::new::<Self>();
23+
24+ fn open() -> linux_kernel_module::KernelResult<Self> {
25+ Ok(JsonChrdev)
26+ }
27+
28+ fn read(
29+ &self,
30+ buf: &mut linux_kernel_module::user_ptr::UserSlicePtrWriter,
31+ offset: i64,
32+ ) -> linux_kernel_module::KernelResult<()> {
33+ let o = Output {
34+ a: A.load(Ordering::Relaxed),
35+ b: B.load(Ordering::Relaxed),
36+ c: C.load(Ordering::Relaxed),
37+ };
38+ let mut s = serde_json_core::to_string::<typenum::U32, _>(&o).map_err(|_| error::Error::ENOMEM)?;
39+ s.push_str("\n").map_err(|_| error::Error::ENOMEM)?;
40+ buf.write(&s.into_bytes()[offset.try_into()?..])?;
41+ Ok(())
42+ }
43+ }
1244
1345struct JsonSysctlModule {
14- a: Sysctl<AtomicBool>,
15- b: Sysctl<AtomicBool>,
16- c: Sysctl<AtomicBool>,
46+ _a: Sysctl<&'static AtomicBool>,
47+ _b: Sysctl<&'static AtomicBool>,
48+ _c: Sysctl<&'static AtomicBool>,
49+ _chrdev_registration: linux_kernel_module::chrdev::Registration,
1750}
1851
1952#[derive(Serialize)]
@@ -25,40 +58,33 @@ struct Output {
2558
2659impl linux_kernel_module::KernelModule for JsonSysctlModule {
2760 fn init() -> linux_kernel_module::KernelResult<Self> {
61+ let chrdev_registration = linux_kernel_module::chrdev::builder("json\x00", 0..1)?
62+ .register_device::<JsonChrdev>()
63+ .build()?;
2864 Ok(JsonSysctlModule {
29- a : Sysctl::register(
65+ _a : Sysctl::register(
3066 "json-sysctl\x00",
3167 "a\x00",
32- AtomicBool::new(false) ,
68+ &A ,
3369 Mode::from_int(0o666),
3470 )?,
35- b : Sysctl::register(
71+ _b : Sysctl::register(
3672 "json-sysctl\x00",
3773 "b\x00",
38- AtomicBool::new(false) ,
74+ &B ,
3975 Mode::from_int(0o666),
4076 )?,
41- c : Sysctl::register(
77+ _c : Sysctl::register(
4278 "json-sysctl\x00",
4379 "c\x00",
44- AtomicBool::new(false) ,
80+ &C ,
4581 Mode::from_int(0o666),
4682 )?,
83+ _chrdev_registration: chrdev_registration,
4784 })
4885 }
4986}
5087
51- impl Drop for JsonSysctlModule {
52- fn drop(&mut self) {
53- let o = Output {
54- a: self.a.get().load(Ordering::Relaxed),
55- b: self.b.get().load(Ordering::Relaxed),
56- c: self.c.get().load(Ordering::Relaxed),
57- };
58- println!("{}", serde_json_core::to_string::<typenum::U32, _>(&o).unwrap());
59- }
60- }
61-
6288linux_kernel_module::kernel_module!(
6389 JsonSysctlModule,
6490 author: "Alex Gaynor and Geoffrey Thomas",
0 commit comments