Skip to content

Upgrade native process bindings and implement native TCP I/O #11159

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

Merged
merged 2 commits into from
Dec 28, 2013
Merged
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
13 changes: 9 additions & 4 deletions src/libnative/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::IoResult;
#[cfg(windows)] use std::ptr;
#[cfg(windows)] use std::str;

fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
pub fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
#[cfg(windows)] static eintr: int = 0; // doesn't matter
#[cfg(not(windows))] static eintr: int = libc::EINTR as int;

Expand All @@ -37,7 +37,7 @@ fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
let mut ret;
loop {
ret = f(data, amt);
if cfg!(not(windows)) { break } // windows has no eintr
if cfg!(windows) { break } // windows has no eintr
// if we get an eintr, then try again
if ret != -1 || os::errno() as int != eintr { break }
}
Expand Down Expand Up @@ -73,7 +73,10 @@ impl FileDesc {
FileDesc { fd: fd, close_on_drop: close_on_drop }
}

fn inner_read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
// FIXME(#10465) these functions should not be public, but anything in
// native::io wanting to use them is forced to have all the
// rtio traits in scope
pub fn inner_read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
#[cfg(windows)] type rlen = libc::c_uint;
#[cfg(not(windows))] type rlen = libc::size_t;
let ret = keep_going(buf, |buf, len| {
Expand All @@ -89,7 +92,7 @@ impl FileDesc {
Ok(ret as uint)
}
}
fn inner_write(&mut self, buf: &[u8]) -> Result<(), IoError> {
pub fn inner_write(&mut self, buf: &[u8]) -> Result<(), IoError> {
#[cfg(windows)] type wlen = libc::c_uint;
#[cfg(not(windows))] type wlen = libc::size_t;
let ret = keep_going(buf, |buf, len| {
Expand All @@ -103,6 +106,8 @@ impl FileDesc {
Ok(())
}
}

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

impl io::Reader for FileDesc {
Expand Down
72 changes: 59 additions & 13 deletions src/libnative/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub use self::process::Process;
// Native I/O implementations
pub mod file;
pub mod process;
pub mod net;

type IoResult<T> = Result<T, IoError>;

Expand All @@ -55,12 +56,25 @@ fn unimpl() -> IoError {
}
}

fn last_error() -> IoError {
fn translate_error(errno: i32, detail: bool) -> IoError {
#[cfg(windows)]
fn get_err(errno: i32) -> (io::IoErrorKind, &'static str) {
match errno {
libc::EOF => (io::EndOfFile, "end of file"),
_ => (io::OtherIoError, "unknown error"),
libc::WSAECONNREFUSED => (io::ConnectionRefused, "connection refused"),
libc::WSAECONNRESET => (io::ConnectionReset, "connection reset"),
libc::WSAEACCES => (io::PermissionDenied, "permission denied"),
libc::WSAEWOULDBLOCK =>
(io::ResourceUnavailable, "resource temporarily unavailable"),
libc::WSAENOTCONN => (io::NotConnected, "not connected"),
libc::WSAECONNABORTED => (io::ConnectionAborted, "connection aborted"),
libc::WSAEADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
libc::WSAEADDRINUSE => (io::ConnectionRefused, "address in use"),

x => {
debug!("ignoring {}: {}", x, os::last_os_error());
(io::OtherIoError, "unknown error")
}
}
}

Expand All @@ -69,24 +83,38 @@ fn last_error() -> IoError {
// XXX: this should probably be a bit more descriptive...
match errno {
libc::EOF => (io::EndOfFile, "end of file"),
libc::ECONNREFUSED => (io::ConnectionRefused, "connection refused"),
libc::ECONNRESET => (io::ConnectionReset, "connection reset"),
libc::EPERM | libc::EACCES =>
(io::PermissionDenied, "permission denied"),
libc::EPIPE => (io::BrokenPipe, "broken pipe"),
libc::ENOTCONN => (io::NotConnected, "not connected"),
libc::ECONNABORTED => (io::ConnectionAborted, "connection aborted"),
libc::EADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
libc::EADDRINUSE => (io::ConnectionRefused, "address in use"),

// These two constants can have the same value on some systems, but
// different values on others, so we can't use a match clause
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
(io::ResourceUnavailable, "resource temporarily unavailable"),

_ => (io::OtherIoError, "unknown error"),
x => {
debug!("ignoring {}: {}", x, os::last_os_error());
(io::OtherIoError, "unknown error")
}
}
}

let (kind, desc) = get_err(os::errno() as i32);
let (kind, desc) = get_err(errno);
IoError {
kind: kind,
desc: desc,
detail: Some(os::last_os_error())
detail: if detail {Some(os::last_os_error())} else {None},
}
}

fn last_error() -> IoError { translate_error(os::errno() as i32, true) }

// unix has nonzero values as errors
fn mkerr_libc(ret: libc::c_int) -> IoResult<()> {
if ret != 0 {
Expand All @@ -106,17 +134,37 @@ fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
}
}

#[cfg(unix)]
fn retry(f: || -> libc::c_int) -> IoResult<libc::c_int> {
loop {
match f() {
-1 if os::errno() as int == libc::EINTR as int => {}
-1 => return Err(last_error()),
n => return Ok(n),
}
}
}

/// Implementation of rt::rtio's IoFactory trait to generate handles to the
/// native I/O functionality.
pub struct IoFactory;
pub struct IoFactory {
priv cannot_construct_outside_of_this_module: ()
}

impl IoFactory {
pub fn new() -> IoFactory {
net::init();
IoFactory { cannot_construct_outside_of_this_module: () }
}
}

impl rtio::IoFactory for IoFactory {
// networking
fn tcp_connect(&mut self, _addr: SocketAddr) -> IoResult<~RtioTcpStream> {
Err(unimpl())
fn tcp_connect(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpStream> {
net::TcpStream::connect(addr).map(|s| ~s as ~RtioTcpStream)
}
fn tcp_bind(&mut self, _addr: SocketAddr) -> IoResult<~RtioTcpListener> {
Err(unimpl())
fn tcp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpListener> {
net::TcpListener::bind(addr).map(|s| ~s as ~RtioTcpListener)
}
fn udp_bind(&mut self, _addr: SocketAddr) -> IoResult<~RtioUdpSocket> {
Err(unimpl())
Expand Down Expand Up @@ -204,9 +252,7 @@ impl rtio::IoFactory for IoFactory {
}
fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
if unsafe { libc::isatty(fd) } != 0 {
// Don't ever close the stdio file descriptors, nothing good really
// comes of that.
Ok(~file::FileDesc::new(fd, fd > libc::STDERR_FILENO) as ~RtioTTY)
Ok(~file::FileDesc::new(fd, true) as ~RtioTTY)
} else {
Err(IoError {
kind: io::MismatchedFileTypeForOperation,
Expand Down
Loading