Skip to content

Expose file descriptor #19116

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions src/libstd/io/fs.rs
Original file line number Diff line number Diff line change
@@ -71,6 +71,9 @@ use vec::Vec;
use sys::fs as fs_imp;
use sys_common;

pub use sys_common::AsFileDesc;
pub use sys::fs::{fd_t, FileDesc};

/// Unconstrained file access type that exposes read and write operations
///
/// Can be constructed via `File::open()`, `File::create()`, and
10 changes: 10 additions & 0 deletions src/libstd/io/net/pipe.rs
Original file line number Diff line number Diff line change
@@ -29,6 +29,9 @@ use prelude::*;
use io::{Listener, Acceptor, IoResult, TimedOut, standard_error};
use time::Duration;

#[cfg(unix)]
use io::fs::{AsFileDesc, FileDesc};

use sys::pipe::UnixStream as UnixStreamImp;
use sys::pipe::UnixListener as UnixListenerImp;
use sys::pipe::UnixAcceptor as UnixAcceptorImp;
@@ -127,6 +130,13 @@ impl UnixStream {
}
}

#[cfg(unix)]
impl AsFileDesc for UnixStream {
fn as_fd(&self) -> &FileDesc {
self.inner.as_fd()
}
}

impl Clone for UnixStream {
fn clone(&self) -> UnixStream {
UnixStream { inner: self.inner.clone() }
26 changes: 14 additions & 12 deletions src/libstd/sys/unix/pipe.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ use prelude::*;
use sys::{mod, timer, retry, c, set_nonblocking, wouldblock};
use sys::fs::{fd_t, FileDesc};
use sys_common::net::*;
use sys_common::{eof, mkerr_libc};
use sys_common::{AsFileDesc, eof, mkerr_libc};

fn unix_socket(ty: libc::c_int) -> IoResult<fd_t> {
match unsafe { libc::socket(libc::AF_UNIX, ty, 0) } {
@@ -56,7 +56,7 @@ fn addr_to_sockaddr_un(addr: &CString,
}

struct Inner {
fd: fd_t,
fd: FileDesc,

// Unused on Linux, where this lock is not necessary.
#[allow(dead_code)]
@@ -65,14 +65,10 @@ struct Inner {

impl Inner {
fn new(fd: fd_t) -> Inner {
Inner { fd: fd, lock: unsafe { mutex::NativeMutex::new() } }
Inner { fd: FileDesc::new(fd, true), lock: unsafe { mutex::NativeMutex::new() } }
}
}

impl Drop for Inner {
fn drop(&mut self) { unsafe { let _ = libc::close(self.fd); } }
}

fn connect(addr: &CString, ty: libc::c_int,
timeout: Option<u64>) -> IoResult<Inner> {
let mut storage = unsafe { mem::zeroed() };
@@ -82,13 +78,13 @@ fn connect(addr: &CString, ty: libc::c_int,

match timeout {
None => {
match retry(|| unsafe { libc::connect(inner.fd, addrp, len) }) {
match retry(|| unsafe { libc::connect(inner.fd.fd(), addrp, len) }) {
-1 => Err(super::last_error()),
_ => Ok(inner)
}
}
Some(timeout_ms) => {
try!(connect_timeout(inner.fd, addrp, len, timeout_ms));
try!(connect_timeout(inner.fd.fd(), addrp, len, timeout_ms));
Ok(inner)
}
}
@@ -100,7 +96,7 @@ fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
let inner = Inner::new(try!(unix_socket(ty)));
let addrp = &storage as *const _ as *const libc::sockaddr;
match unsafe {
libc::bind(inner.fd, addrp, len)
libc::bind(inner.fd.fd(), addrp, len)
} {
-1 => Err(super::last_error()),
_ => Ok(inner)
@@ -133,7 +129,7 @@ impl UnixStream {
}
}

fn fd(&self) -> fd_t { self.inner.fd }
fn fd(&self) -> fd_t { self.inner.fd.fd() }

#[cfg(target_os = "linux")]
fn lock_nonblocking(&self) {}
@@ -200,6 +196,12 @@ impl UnixStream {
}
}

impl AsFileDesc for UnixStream {
fn as_fd(&self) -> &FileDesc {
&self.inner.fd
}
}

impl Clone for UnixStream {
fn clone(&self) -> UnixStream {
UnixStream::new(self.inner.clone())
@@ -222,7 +224,7 @@ impl UnixListener {
})
}

fn fd(&self) -> fd_t { self.inner.fd }
fn fd(&self) -> fd_t { self.inner.fd.fd() }

pub fn listen(self) -> IoResult<UnixAcceptor> {
match unsafe { libc::listen(self.fd(), 128) } {