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

Fixes #263: Ability to send arbitrary handles/file descriptors over ipc channel #276

Closed
wants to merge 9 commits into from
Closed
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ sc = { version = "0.2.2", optional = true }
crossbeam-utils = "0.7"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = {version = "0.3.7", features = ["minwindef", "ioapiset", "memoryapi", "namedpipeapi", "handleapi", "fileapi", "impl-default"]}
winapi = {version = "0.3.7", features = ["minwindef", "ioapiset", "memoryapi", "namedpipeapi", "handleapi", "fileapi", "impl-default", "std"]}
4 changes: 2 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod platform {
let wait_rx = wait_rx.lock().unwrap();
let tx = tx;
for _ in 0..ITERATIONS {
tx.send(&data, vec![], vec![]).unwrap();
tx.send(&data, vec![], vec![], vec![]).unwrap();
if ITERATIONS > 1 {
// Prevent beginning of the next send
// from overlapping with receive of last fragment,
Expand All @@ -72,7 +72,7 @@ mod platform {
} else {
b.iter(|| {
for _ in 0..ITERATIONS {
tx.send(&data, vec![], vec![]).unwrap();
tx.send(&data, vec![], vec![], vec![]).unwrap();
rx.recv().unwrap();
}
0
Expand Down
197 changes: 197 additions & 0 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
use std::cell::RefCell;
use std::default::Default;
use std::fs::File;
use std::io;
use std::mem;
use std::thread;

#[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())
}
}

const WINDOWS_APP_MODULE_NAME: &'static str = "api-ms-win-core-handle-l1-1-0";
const COMPARE_OBJECT_HANDLES_FUNCTION_NAME: &'static str = "CompareObjectHandles";

lazy_static! {
static ref WINDOWS_APP_MODULE_NAME_CSTRING: std::ffi::CString = std::ffi::CString::new(WINDOWS_APP_MODULE_NAME).unwrap();
static ref COMPARE_OBJECT_HANDLES_FUNCTION_NAME_CSTRING: std::ffi::CString = std::ffi::CString::new(COMPARE_OBJECT_HANDLES_FUNCTION_NAME).unwrap();
}

#[cfg(feature = "windows-shared-memory-equality")]
impl PartialEq for OwnedDescriptor {
fn eq(&self, other: &Self) -> bool {
unsafe {
// Calling LoadLibraryA every time seems to be ok since libraries are refcounted and multiple calls won't produce multiple instances.
let module_handle = winapi::um::libloaderapi::LoadLibraryA(WINDOWS_APP_MODULE_NAME_CSTRING.as_ptr());
if module_handle.is_null() {
panic!("Error loading library {}. {}", WINDOWS_APP_MODULE_NAME, std::io::Error::last_os_error());
}
let proc = winapi::um::libloaderapi::GetProcAddress(module_handle, COMPARE_OBJECT_HANDLES_FUNCTION_NAME_CSTRING.as_ptr());
if proc.is_null() {
panic!("Error calling GetProcAddress to use {}. {}", COMPARE_OBJECT_HANDLES_FUNCTION_NAME, std::io::Error::last_os_error());
}
let compare_object_handles: unsafe extern "stdcall" fn(RawDescriptor, RawDescriptor) -> winapi::shared::minwindef::BOOL = std::mem::transmute(proc);
compare_object_handles(*self.0.borrow(), *other.0.borrow()) != 0
}
}
}

#[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
}
}
Loading