Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file: wrap struct fdtable && files_struct #922

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions rust/bindings/bindings_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/cdev.h>
#include <linux/clk.h>
#include <linux/errname.h>
#include <linux/fdtable.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fs_parser.h>
Expand Down
13 changes: 13 additions & 0 deletions rust/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <linux/build_bug.h>
#include <linux/clk.h>
#include <linux/errname.h>
#include <linux/fdtable.h>
#include <linux/fs_parser.h>
#include <linux/gfp.h>
#include <linux/highmem.h>
Expand Down Expand Up @@ -655,6 +656,18 @@ int rust_helper_fs_parse(struct fs_context *fc,
}
EXPORT_SYMBOL_GPL(rust_helper_fs_parse);

bool rust_helper_close_on_exec(unsigned int fd, const struct fdtable *fdt)
{
return close_on_exec(fd, fdt);
}
EXPORT_SYMBOL_GPL(rust_helper_close_on_exec);

bool rust_helper_fd_is_open(unsigned int fd, const struct fdtable *fdt)
{
return fd_is_open(fd, fdt);
}
EXPORT_SYMBOL_GPL(rust_helper_fd_is_open);

/*
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
* as the Rust `usize` type, so we can use it in contexts where Rust
Expand Down
75 changes: 75 additions & 0 deletions rust/kernel/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,78 @@ pub trait Operations {
Ok(bindings::POLLIN | bindings::POLLOUT | bindings::POLLRDNORM | bindings::POLLWRNORM)
}
}

/// Wrap the kernel's `struct fdtable`.
///
/// # Invariants
///
/// The pointer `Fdtable::ptr` is null or valid.
#[repr(transparent)]
pub struct Fdtable {
ptr: *mut bindings::fdtable,
}

impl Fdtable {
/// Constructors a new `struct fdtable` wrapper
///
/// #Safety
///
/// The pointer `ptr` must be either null or valid pointer for the lifetime of the object.
unsafe fn from_ptr(ptr: *mut bindings::fdtable) -> Self {
Self { ptr }
}

/// Returns the max_fds of the fdtable instance.
pub fn get_max_fds(&self) -> u32 {
// SAFETY: By the invariant, we know that `self.ptr` is valid.
unsafe { core::ptr::addr_of!((*(self.ptr)).max_fds).read() }
}

/// Determines whether the given fd is closed.
pub fn close_on_exec(&self, fd: u32) -> bool {
// SAFETY: By the type invariant, we know that `self.ptr` is valid.
unsafe { bindings::close_on_exec(fd, self.ptr) }
}

/// Determines whether the given fd is opened.
pub fn fd_is_open(&self, fd: u32) -> bool {
// SAFETY: By the type invariant, we know that `self.ptr` is valid.
unsafe { bindings::fd_is_open(fd, self.ptr) }
}
}


/// Wrap the kernel's `struct files_struct`.
///
/// # Invariants
///
/// The pointer `FilesStruct::ptr` is null or valid.
#[repr(transparent)]
pub struct FilesStruct {
ptr: *mut bindings::files_struct,
}

impl FilesStruct {
/// Constructors a new `struct files_struct` wrapper
///
/// #Safety
///
/// The pointer `ptr` must be either null or valid pointer for the lifetime of the object.
unsafe fn from_ptr(ptr: *mut bindings::files_struct) -> Self {
Self { ptr }
}

/// Close open files of current task.
pub fn do_close_on_exec(&self) {
// SAFETY: By the type invariant, we know that `self.ptr` is valid.
unsafe { bindings::do_close_on_exec(self.ptr) };
}

}

impl Drop for FilesStruct {
fn drop(&mut self) {
// SAFETY: By the type invariant, we know that `self.ptr` is valid.
unsafe { bindings::put_files_struct(self.ptr) }
}
}