A userspace model of Linux /dev/null, on the way to a Rust-for-Linux /dev/rust-null device.
git clone https://github.com/bunnysi/rust-null.git
cd rust-null
cargo test
echo hello | cargo run --example discardRust 1.80 or newer. cargo test is the userspace contract; it does not load a kernel module.
NullDevice is the same three operations as drivers/char/mem.c:
use std::io::{Read, Write};
use rust_null::NullDevice;
let mut n = NullDevice::new();
assert_eq!(n.write(b"secret").unwrap(), 6); // discarded
let mut buf = [0xAAu8; 4];
assert_eq!(n.read(&mut buf).unwrap(), 0); // EOF, buffer untouched| Call | Result |
|---|---|
read |
0 (EOF). The buffer is not filled. |
write |
count. Bytes are thrown away. |
seek |
0. Offset and whence are ignored. |
That last row is the surprising one: lseek on /dev/null always returns 0, even for SEEK_END.
- Userspace (this tree) —
NullDevice+ contract tests. - Rust for Linux — out-of-tree module against a
CONFIG_RUST=ykernel. /dev/rust-null— misc device with the same read/write/llseek contract.