Skip to content

Commit

Permalink
Fix failing tests and move descriptor to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Zaddach committed Jul 26, 2021
1 parent 7e4d159 commit 7254e51
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 174 deletions.
170 changes: 170 additions & 0 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use std::io;
use std::thread;
use std::mem;
use std::default::Default;
use std::fs::File;
use std::cell::RefCell;

#[cfg(windows)]
pub use {
std::os::windows::io::RawHandle as RawDescriptor,
std::os::windows::io::AsRawHandle,
std::os::windows::io::IntoRawHandle,
std::os::windows::io::FromRawHandle,
};

#[cfg(unix)]
pub use {
std::os::unix::io::RawFd as RawDescriptor,
std::os::unix::io::AsRawFd,
std::os::unix::io::IntoRawFd,
std::os::unix::io::FromRawFd,
};

#[cfg(windows)]
const INVALID_RAW_DESCRIPTOR: RawDescriptor = winapi::um::handleapi::INVALID_HANDLE_VALUE;

#[cfg(windows)]
fn raw_descriptor_close(descriptor: &RawDescriptor) -> Result<(), io::Error> {
unsafe {
let result = winapi::um::handleapi::CloseHandle(*descriptor);
if result == 0 {
Err(io::Error::last_os_error())
}
else {
Ok(())
}
}
}

#[cfg(unix)]
const INVALID_RAW_DESCRIPTOR: RawDescriptor = -1;

#[cfg(unix)]
fn raw_descriptor_close(descriptor: &RawDescriptor) -> Result<(), io::Error> {
unsafe {
let result = libc::close(*descriptor);
if result == 0 {
Ok(())
}
else {
Err(io::Error::last_os_error())
}
}
}

#[derive(Debug)]
pub struct OwnedDescriptor(RefCell<RawDescriptor>);

unsafe impl Send for OwnedDescriptor { }
unsafe impl Sync for OwnedDescriptor { }

impl Drop for OwnedDescriptor {
fn drop(&mut self) {
if *self.0.borrow() != INVALID_RAW_DESCRIPTOR {
let result = raw_descriptor_close(&*self.0.borrow());
assert!( thread::panicking() || result.is_ok() );
}
}
}

impl OwnedDescriptor {
pub fn new(descriptor: RawDescriptor) -> OwnedDescriptor {
OwnedDescriptor(RefCell::new(descriptor))
}

pub fn consume(& self) -> OwnedDescriptor {
OwnedDescriptor::new(self.0.replace(INVALID_RAW_DESCRIPTOR))
}
}

impl Default for OwnedDescriptor {
fn default() -> OwnedDescriptor {
OwnedDescriptor::new(INVALID_RAW_DESCRIPTOR)
}
}

#[cfg(windows)]
impl IntoRawHandle for OwnedDescriptor {
fn into_raw_handle(self) -> RawDescriptor {
let handle = *self.0.borrow();
mem::forget(self);
handle
}
}

#[cfg(windows)]
impl AsRawHandle for OwnedDescriptor {
fn as_raw_handle(& self) -> RawDescriptor {
*self.0.borrow()
}
}

#[cfg(windows)]
impl FromRawHandle for OwnedDescriptor {
unsafe fn from_raw_handle(handle: RawDescriptor) -> OwnedDescriptor {
OwnedDescriptor::new(handle)
}
}

#[cfg(windows)]
impl Into<File> for OwnedDescriptor {
fn into(self) -> File {
unsafe {
File::from_raw_handle(self.into_raw_handle())
}
}
}

#[cfg(windows)]
impl From<File> for OwnedDescriptor {
fn from(file: File) -> Self {
OwnedDescriptor::new(file.into_raw_handle())
}
}

#[cfg(unix)]
impl IntoRawFd for OwnedDescriptor {
fn into_raw_fd(self) -> RawDescriptor {
let fd = self.0.replace(INVALID_RAW_DESCRIPTOR);
mem::forget(self);
fd
}
}

#[cfg(unix)]
impl AsRawFd for OwnedDescriptor {
fn as_raw_fd(& self) -> RawDescriptor {
*self.0.borrow()
}
}

#[cfg(unix)]
impl FromRawFd for OwnedDescriptor {
unsafe fn from_raw_fd(fd: RawDescriptor) -> OwnedDescriptor {
OwnedDescriptor::new(fd)
}
}

#[cfg(unix)]
impl Into<File> for OwnedDescriptor {
fn into(self) -> File {
unsafe {
File::from_raw_fd(self.into_raw_fd())
}
}
}

#[cfg(unix)]
impl From<File> for OwnedDescriptor {
fn from(file: File) -> Self {
OwnedDescriptor::new(file.into_raw_fd())
}
}

#[cfg(unix)]
impl PartialEq for OwnedDescriptor {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
16 changes: 8 additions & 8 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use crate::platform::{self, OsIpcChannel, OsIpcReceiver, OsIpcReceiverSet, OsIpcSender};
use crate::platform::{OsIpcOneShotServer, OsIpcSelectionResult, OsIpcSharedMemory, OsOpaqueIpcChannel};
use crate::platform::Descriptor;
use crate::descriptor::OwnedDescriptor;

use bincode;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand All @@ -31,7 +31,7 @@ thread_local! {
RefCell<Vec<Option<OsIpcSharedMemory>>> = RefCell::new(Vec::new())
}
thread_local! {
static OS_IPC_DESCRIPTORS_FOR_DESERIALIZATION: RefCell<Vec<Descriptor>> = RefCell::new(Vec::new())
static OS_IPC_DESCRIPTORS_FOR_DESERIALIZATION: RefCell<Vec<OwnedDescriptor>> = RefCell::new(Vec::new())
}
thread_local! {
static OS_IPC_CHANNELS_FOR_SERIALIZATION: RefCell<Vec<OsIpcChannel>> = RefCell::new(Vec::new())
Expand All @@ -41,7 +41,7 @@ thread_local! {
RefCell::new(Vec::new())
}
thread_local! {
static OS_IPC_DESCRIPTORS_FOR_SERIALIZATION: RefCell<Vec<Descriptor>> = RefCell::new(Vec::new())
static OS_IPC_DESCRIPTORS_FOR_SERIALIZATION: RefCell<Vec<OwnedDescriptor>> = RefCell::new(Vec::new())
}

#[derive(Debug)]
Expand Down Expand Up @@ -621,7 +621,7 @@ pub struct OpaqueIpcMessage {
data: Vec<u8>,
os_ipc_channels: Vec<OsOpaqueIpcChannel>,
os_ipc_shared_memory_regions: Vec<Option<OsIpcSharedMemory>>,
os_ipc_descriptors: Vec<Descriptor>,
os_ipc_descriptors: Vec<OwnedDescriptor>,
}

impl Debug for OpaqueIpcMessage {
Expand All @@ -637,7 +637,7 @@ impl OpaqueIpcMessage {
fn new(data: Vec<u8>,
os_ipc_channels: Vec<OsOpaqueIpcChannel>,
os_ipc_shared_memory_regions: Vec<OsIpcSharedMemory>,
os_ipc_descriptors: Vec<Descriptor>)
os_ipc_descriptors: Vec<OwnedDescriptor>)
-> OpaqueIpcMessage {
OpaqueIpcMessage {
data: data,
Expand Down Expand Up @@ -924,7 +924,7 @@ fn deserialize_os_ipc_receiver<'de, D>(deserializer: D)
}


impl Serialize for Descriptor {
impl Serialize for OwnedDescriptor {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
let index = OS_IPC_DESCRIPTORS_FOR_SERIALIZATION.with(|os_ipc_descriptors_for_serialization| {
let mut os_ipc_descriptors_for_serialization =
Expand All @@ -937,12 +937,12 @@ impl Serialize for Descriptor {
}
}

impl<'de> Deserialize<'de> for Descriptor {
impl<'de> Deserialize<'de> for OwnedDescriptor {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let index: usize = Deserialize::deserialize(deserializer)?;

OS_IPC_DESCRIPTORS_FOR_DESERIALIZATION.with(|os_ipc_descriptors_for_deserialization| {
os_ipc_descriptors_for_deserialization.borrow_mut().get_mut(index).map(|x| x.consume()).ok_or(serde::de::Error::invalid_value(serde::de::Unexpected::Unsigned(index as u64), &"index for Descriptor"))
os_ipc_descriptors_for_deserialization.borrow_mut().get_mut(index).map(|x| x.consume()).ok_or(serde::de::Error::invalid_value(serde::de::Unexpected::Unsigned(index as u64), &"index for OwnedDescriptor"))
})
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ extern crate winapi;
pub mod ipc;
pub mod platform;
pub mod router;
pub mod descriptor;

#[cfg(test)]
mod test;
Expand Down
100 changes: 0 additions & 100 deletions src/platform/common/fd.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/platform/common/mod.rs

This file was deleted.

Loading

0 comments on commit 7254e51

Please sign in to comment.