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

Commit

Permalink
Simplify the SysctlStorage API
Browse files Browse the repository at this point in the history
Automatically compute how many bytes were read.
  • Loading branch information
alex committed Dec 25, 2019
1 parent 1e4c558 commit 1f1b74d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
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
4 changes: 2 additions & 2 deletions tests/random/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ 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))
data.write(&storage)
}
}

Expand Down

0 comments on commit 1f1b74d

Please sign in to comment.