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

Commit b22b8b4

Browse files
committed
WIP: json-sysctl: add a chrdev
Depends on some changes to core that we should land with a bit more consideration
1 parent a97e3a4 commit b22b8b4

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

src/chrdev.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ unsafe extern "C" fn read_callback<T: FileOperations>(
125125
};
126126
let f = &*((*file).private_data as *const T);
127127
// TODO: Pass offset to read()?
128-
match f.read(&mut data) {
128+
match f.read(&mut data, *offset) {
129129
Ok(()) => {
130130
let written = len - data.len();
131131
(*offset) += written as bindings::loff_t;
@@ -197,5 +197,5 @@ pub trait FileOperations: Sync + Sized {
197197
const VTABLE: FileOperationsVtable;
198198

199199
fn open() -> KernelResult<Self>;
200-
fn read(&self, buf: &mut UserSlicePtrWriter) -> KernelResult<()>;
200+
fn read(&self, buf: &mut UserSlicePtrWriter, offset: i64) -> KernelResult<()>;
201201
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod allocator;
99
pub mod bindings;
1010
mod c_types;
1111
pub mod chrdev;
12-
mod error;
12+
pub mod error;
1313
pub mod filesystem;
1414
pub mod printk;
1515
pub mod sysctl;

tests/json-sysctl/src/lib.rs

+47-21
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,51 @@
22
#![feature(const_str_as_bytes)]
33

44
use core::sync::atomic::{AtomicBool, Ordering};
5+
use core::convert::TryInto;
56

67
use serde::Serialize;
78
use serde_json_core;
89

910
use linux_kernel_module::sysctl::Sysctl;
1011
use 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

1345
struct 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

2659
impl 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-
6288
linux_kernel_module::kernel_module!(
6389
JsonSysctlModule,
6490
author: "Alex Gaynor and Geoffrey Thomas",

0 commit comments

Comments
 (0)