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

Commit c6da976

Browse files
committed
Add the json-sysctl demo from the LSSNA talk
Depends on a byteorder crate locally patched to make std not a default feature, see #121 for why.
1 parent 5416d8f commit c6da976

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

tests/json-sysctl/Cargo.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "json-sysctl"
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+
10+
[dependencies]
11+
linux-kernel-module = { path = "../.." }
12+
serde = { version = "*", default-features = false , features = ["derive"] }
13+
serde-json-core = { git = "https://github.com/japaric/serde-json-core" }
14+
typenum = "*"
15+
16+
[patch.crates-io]
17+
# You'll need a locally patched version of the byteorder crate that
18+
# disables the `std` feature:
19+
# git clone https://github.com/burntsushi/byteorder
20+
# sed -i 's/"std"//' Cargo.toml
21+
# Then uncomment the line below, pointing to your patched byteorder.
22+
# This is because the build script indirectly depends on byteorder with
23+
# all features, so even though our runtime dependency serde-json-core
24+
# indirectly depends on byteorder without default features, Cargo
25+
# confuses the two. See https://github.com/rust-lang/cargo/issues/5730
26+
#byteorder = { path = "/root/byteorder" }

tests/json-sysctl/src/lib.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#![no_std]
2+
#![feature(const_str_as_bytes)]
3+
4+
use core::cmp::min;
5+
use core::convert::TryInto;
6+
use core::sync::atomic::{AtomicBool, Ordering};
7+
8+
use serde::Serialize;
9+
use serde_json_core;
10+
11+
use linux_kernel_module::cstr;
12+
use linux_kernel_module::sysctl::Sysctl;
13+
use linux_kernel_module::Mode;
14+
15+
static A: AtomicBool = AtomicBool::new(false);
16+
static B: AtomicBool = AtomicBool::new(false);
17+
static C: AtomicBool = AtomicBool::new(false);
18+
19+
struct JsonChrdev;
20+
21+
impl linux_kernel_module::chrdev::FileOperations for JsonChrdev {
22+
const VTABLE: linux_kernel_module::chrdev::FileOperationsVtable =
23+
linux_kernel_module::chrdev::FileOperationsVtable::new::<Self>();
24+
25+
fn open() -> linux_kernel_module::KernelResult<Self> {
26+
Ok(JsonChrdev)
27+
}
28+
29+
fn read(
30+
&self,
31+
buf: &mut linux_kernel_module::user_ptr::UserSlicePtrWriter,
32+
offset: u64,
33+
) -> linux_kernel_module::KernelResult<()> {
34+
let o = Output {
35+
a: A.load(Ordering::Relaxed),
36+
b: B.load(Ordering::Relaxed),
37+
c: C.load(Ordering::Relaxed),
38+
};
39+
let mut s = serde_json_core::to_vec::<typenum::U32, _>(&o)
40+
.map_err(|_| linux_kernel_module::Error::ENOMEM)?;
41+
s.push(b'\n')
42+
.map_err(|_| linux_kernel_module::Error::ENOMEM)?;
43+
let start = min(offset.try_into()?, s.len());
44+
let end = min(start + buf.len(), s.len());
45+
buf.write(&s[start..end])?;
46+
Ok(())
47+
}
48+
}
49+
50+
struct JsonSysctlModule {
51+
_a: Sysctl<&'static AtomicBool>,
52+
_b: Sysctl<&'static AtomicBool>,
53+
_c: Sysctl<&'static AtomicBool>,
54+
_chrdev_registration: linux_kernel_module::chrdev::Registration,
55+
}
56+
57+
#[derive(Serialize)]
58+
struct Output {
59+
a: bool,
60+
b: bool,
61+
c: bool,
62+
}
63+
64+
impl linux_kernel_module::KernelModule for JsonSysctlModule {
65+
fn init() -> linux_kernel_module::KernelResult<Self> {
66+
let chrdev_registration = linux_kernel_module::chrdev::builder(cstr!("json"), 0..1)?
67+
.register_device::<JsonChrdev>()
68+
.build()?;
69+
Ok(JsonSysctlModule {
70+
_a: Sysctl::register(cstr!("json-sysctl"), cstr!("a"), &A, Mode::from_int(0o666))?,
71+
_b: Sysctl::register(cstr!("json-sysctl"), cstr!("b"), &B, Mode::from_int(0o666))?,
72+
_c: Sysctl::register(cstr!("json-sysctl"), cstr!("c"), &C, Mode::from_int(0o666))?,
73+
_chrdev_registration: chrdev_registration,
74+
})
75+
}
76+
}
77+
78+
linux_kernel_module::kernel_module!(
79+
JsonSysctlModule,
80+
author: "Alex Gaynor and Geoffrey Thomas",
81+
description: "Use JSON serialization in kernelspace",
82+
license: "GPL"
83+
);

0 commit comments

Comments
 (0)