Skip to content

Commit

Permalink
Merge pull request #171 from ids1024/io-safety
Browse files Browse the repository at this point in the history
Use `OwnedFd` and `BorrowedFd` where `RawFd` is used in API
  • Loading branch information
Drakulix authored Oct 12, 2023
2 parents 61a7415 + 79004ce commit 250fa4a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
5 changes: 2 additions & 3 deletions examples/syncobj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ extern crate nix;
pub mod utils;

use nix::poll::PollFlags;
use std::os::unix::io::AsFd;
use std::os::unix::io::{AsFd, OwnedFd};
use utils::*;

use drm::control::syncobj::SyncFile;
use drm::SystemError;

impl Card {
fn simulate_command_submission(&self) -> Result<SyncFile, SystemError> {
fn simulate_command_submission(&self) -> Result<OwnedFd, SystemError> {
// Create a temporary syncobj to receive the command fence.
let syncobj = self.create_syncobj(false)?;

Expand Down
23 changes: 14 additions & 9 deletions src/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use std::iter::Zip;
use std::mem;
use std::num::NonZeroUsize;
use std::ops::RangeBounds;
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd};
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd};
use std::time::Duration;

use core::num::NonZeroU32;
Expand Down Expand Up @@ -777,15 +777,19 @@ pub trait Device: super::Device {
}

/// Convert a prime file descriptor to a GEM buffer handle
fn prime_fd_to_buffer(&self, fd: RawFd) -> Result<buffer::Handle, SystemError> {
let info = ffi::gem::fd_to_handle(self.as_fd().as_raw_fd(), fd)?;
fn prime_fd_to_buffer(&self, fd: BorrowedFd<'_>) -> Result<buffer::Handle, SystemError> {
let info = ffi::gem::fd_to_handle(self.as_fd().as_raw_fd(), fd.as_raw_fd())?;
Ok(from_u32(info.handle).unwrap())
}

/// Convert a GEM buffer handle to a prime file descriptor
fn buffer_to_prime_fd(&self, handle: buffer::Handle, flags: u32) -> Result<RawFd, SystemError> {
fn buffer_to_prime_fd(
&self,
handle: buffer::Handle,
flags: u32,
) -> Result<OwnedFd, SystemError> {
let info = ffi::gem::handle_to_fd(self.as_fd().as_raw_fd(), handle.into(), flags)?;
Ok(info.fd)
Ok(unsafe { OwnedFd::from_raw_fd(info.fd) })
}

/// Queue a page flip on the given crtc
Expand Down Expand Up @@ -838,19 +842,20 @@ pub trait Device: super::Device {
&self,
handle: syncobj::Handle,
export_sync_file: bool,
) -> Result<syncobj::SyncFile, SystemError> {
) -> Result<OwnedFd, SystemError> {
let info =
ffi::syncobj::handle_to_fd(self.as_fd().as_raw_fd(), handle.into(), export_sync_file)?;
Ok(unsafe { syncobj::SyncFile::from_raw_fd(info.fd) })
Ok(unsafe { OwnedFd::from_raw_fd(info.fd) })
}

/// Imports a file descriptor exported by [`Self::syncobj_to_fd`] back into a process-local handle.
fn fd_to_syncobj(
&self,
fd: RawFd,
fd: BorrowedFd<'_>,
import_sync_file: bool,
) -> Result<syncobj::Handle, SystemError> {
let info = ffi::syncobj::fd_to_handle(self.as_fd().as_raw_fd(), fd, import_sync_file)?;
let info =
ffi::syncobj::fd_to_handle(self.as_fd().as_raw_fd(), fd.as_raw_fd(), import_sync_file)?;
Ok(from_u32(info.handle).unwrap())
}

Expand Down
29 changes: 0 additions & 29 deletions src/control/syncobj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//! [`tokio::io::unix::AsyncFd`]: <https://docs.rs/tokio/latest/tokio/io/unix/struct.AsyncFd.html>
use control;
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, RawFd};

/// A handle to a specific syncobj
#[repr(transparent)]
Expand Down Expand Up @@ -48,31 +47,3 @@ impl std::fmt::Debug for Handle {
f.debug_tuple("syncobj::Handle").field(&self.0).finish()
}
}

#[derive(Debug)]
/// A simple wrapper for a syncobj fd.
pub struct SyncFile(std::fs::File);

impl FromRawFd for SyncFile {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self(std::fs::File::from_raw_fd(fd))
}
}

/// Implementing [`AsFd`] is a prerequisite to implementing the traits found in this crate.
/// Here, we are just calling [`std::fs::File::as_fd()`] on the inner File.
impl AsFd for SyncFile {
fn as_fd(&self) -> std::os::unix::io::BorrowedFd<'_> {
self.0.as_fd()
}
}

/// Implementing [`AsRawFd`] allows SyncFile to be owned by [`tokio::io::unix::AsyncFd`];
/// thereby integrating with its async/await runtime.
///
/// [`tokio::io::unix::AsyncFd`]: <https://docs.rs/tokio/latest/tokio/io/unix/struct.AsyncFd.html>
impl AsRawFd for SyncFile {
fn as_raw_fd(&self) -> RawFd {
self.as_fd().as_raw_fd()
}
}

0 comments on commit 250fa4a

Please sign in to comment.