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

Simplify the SysctlStorage API #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::user_ptr::{UserSlicePtr, UserSlicePtrWriter};

pub trait SysctlStorage: Sync {
fn store_value(&self, data: &[u8]) -> (usize, error::KernelResult<()>);
fn read_value(&self, data: &mut UserSlicePtrWriter) -> (usize, error::KernelResult<()>);
fn read_value(&self, data: &mut UserSlicePtrWriter) -> error::KernelResult<()>;
}

fn trim_whitespace(mut data: &[u8]) -> &[u8] {
Expand All @@ -37,7 +37,7 @@ where
(*self).store_value(data)
}

fn read_value(&self, data: &mut UserSlicePtrWriter) -> (usize, error::KernelResult<()>) {
fn read_value(&self, data: &mut UserSlicePtrWriter) -> error::KernelResult<()> {
(*self).read_value(data)
}
}
Expand All @@ -58,13 +58,13 @@ impl SysctlStorage for atomic::AtomicBool {
(data.len(), result)
}

fn read_value(&self, data: &mut UserSlicePtrWriter) -> (usize, error::KernelResult<()>) {
fn read_value(&self, data: &mut UserSlicePtrWriter) -> error::KernelResult<()> {
let value = if self.load(atomic::Ordering::Relaxed) {
b"1\n"
} else {
b"0\n"
};
(value.len(), data.write(value))
data.write(value)
}
}

Expand Down Expand Up @@ -106,7 +106,9 @@ unsafe extern "C" fn proc_handler<T: SysctlStorage>(
storage.store_value(&data)
} else {
let mut writer = data.writer();
storage.read_value(&mut writer)
let starting_len = writer.len();
let res = storage.read_value(&mut writer);
(starting_len - writer.len(), res)
};
*len = bytes_processed;
*ppos += *len as bindings::loff_t;
Expand Down
8 changes: 3 additions & 5 deletions tests/random/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ impl SysctlStorage for EntropySource {
fn read_value(
&self,
data: &mut linux_kernel_module::user_ptr::UserSlicePtrWriter,
) -> (usize, linux_kernel_module::KernelResult<()>) {
) -> linux_kernel_module::KernelResult<()> {
let mut storage = vec![0; data.len()];
if let Err(e) = random::getrandom(&mut storage) {
return (0, Err(e));
}
(storage.len(), data.write(&storage))
random::getrandom(&mut storage)?;
data.write(&storage)
}
}

Expand Down