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

Commit 62a5394

Browse files
authored
Give access to file flags in read file_operations callback (#215)
1 parent 2877b71 commit 62a5394

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const INCLUDED_VARS: &[&str] = &[
4444
"SEEK_SET",
4545
"SEEK_CUR",
4646
"SEEK_END",
47+
"O_NONBLOCK",
4748
];
4849
const OPAQUE_TYPES: &[&str] = &[
4950
// These need to be opaque because they're both packed and aligned, which rustc

src/file_operations.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ use crate::c_types;
88
use crate::error::{Error, KernelResult};
99
use crate::user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter};
1010

11+
bitflags::bitflags! {
12+
pub struct FileFlags: c_types::c_uint {
13+
const NONBLOCK = bindings::O_NONBLOCK;
14+
}
15+
}
16+
1117
pub struct File {
1218
ptr: *const bindings::file,
1319
}
@@ -20,6 +26,10 @@ impl File {
2026
pub fn pos(&self) -> u64 {
2127
unsafe { (*self.ptr).f_pos as u64 }
2228
}
29+
30+
pub fn flags(&self) -> FileFlags {
31+
FileFlags::from_bits_truncate(unsafe { (*self.ptr).f_flags })
32+
}
2333
}
2434

2535
// Matches std::io::SeekFrom in the Rust stdlib
@@ -60,7 +70,7 @@ unsafe extern "C" fn read_callback<T: Read>(
6070
Ok(v) => v,
6171
Err(_) => return Error::EINVAL.to_kernel_errno().try_into().unwrap(),
6272
};
63-
match f.read(&mut data, positive_offset) {
73+
match f.read(&File::from_ptr(file), &mut data, positive_offset) {
6474
Ok(()) => {
6575
let written = len - data.len();
6676
(*offset) += bindings::loff_t::try_from(written).unwrap();
@@ -230,7 +240,7 @@ pub trait FileOperations: Sync + Sized {
230240
pub trait Read {
231241
/// Reads data from this file to userspace. Corresponds to the `read`
232242
/// function pointer in `struct file_operations`.
233-
fn read(&self, buf: &mut UserSlicePtrWriter, offset: u64) -> KernelResult<()>;
243+
fn read(&self, file: &File, buf: &mut UserSlicePtrWriter, offset: u64) -> KernelResult<()>;
234244
}
235245

236246
pub trait Write {

tests/chrdev/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl linux_kernel_module::file_operations::FileOperations for CycleFile {
2222
impl linux_kernel_module::file_operations::Read for CycleFile {
2323
fn read(
2424
&self,
25+
_file: &linux_kernel_module::file_operations::File,
2526
buf: &mut linux_kernel_module::user_ptr::UserSlicePtrWriter,
2627
offset: u64,
2728
) -> linux_kernel_module::KernelResult<()> {
@@ -81,6 +82,7 @@ impl linux_kernel_module::file_operations::FileOperations for WriteFile {
8182
impl linux_kernel_module::file_operations::Read for WriteFile {
8283
fn read(
8384
&self,
85+
_file: &linux_kernel_module::file_operations::File,
8486
buf: &mut linux_kernel_module::user_ptr::UserSlicePtrWriter,
8587
_offset: u64,
8688
) -> linux_kernel_module::KernelResult<()> {

0 commit comments

Comments
 (0)