2014-09-30 17:03:56 -07:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-09 13:59:23 +01:00
|
|
|
#![allow(missing_docs)]
|
2014-10-17 13:39:27 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![allow(unused_imports)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_unsafe)]
|
|
|
|
#![allow(unused_mut)]
|
2014-10-10 10:11:49 -07:00
|
|
|
|
2014-09-30 17:03:56 -07:00
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
use num;
|
|
|
|
use prelude::*;
|
|
|
|
use io::{mod, IoResult, IoError};
|
2014-10-10 10:11:49 -07:00
|
|
|
use sys_common::mkerr_libc;
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2014-10-15 15:45:59 -07:00
|
|
|
macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => (
|
|
|
|
static $name: Helper<$m> = Helper {
|
|
|
|
lock: ::rt::mutex::NATIVE_MUTEX_INIT,
|
|
|
|
chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> },
|
|
|
|
signal: ::cell::UnsafeCell { value: 0 },
|
|
|
|
initialized: ::cell::UnsafeCell { value: false },
|
|
|
|
};
|
|
|
|
) )
|
|
|
|
|
2014-10-10 10:11:49 -07:00
|
|
|
pub mod c;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub mod fs;
|
|
|
|
pub mod os;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub mod tcp;
|
|
|
|
pub mod udp;
|
|
|
|
pub mod pipe;
|
2014-10-15 15:45:59 -07:00
|
|
|
pub mod helper_signal;
|
2014-10-09 16:27:28 -07:00
|
|
|
pub mod process;
|
2014-10-16 18:57:11 -07:00
|
|
|
pub mod timer;
|
2014-10-17 13:33:08 -07:00
|
|
|
pub mod tty;
|
2014-10-10 10:11:49 -07:00
|
|
|
|
|
|
|
pub mod addrinfo {
|
|
|
|
pub use sys_common::net::get_host_addresses;
|
|
|
|
}
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2014-10-10 10:11:49 -07:00
|
|
|
// FIXME: move these to c module
|
|
|
|
pub type sock_t = self::fs::fd_t;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub type wrlen = libc::size_t;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub type msglen_t = libc::size_t;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub unsafe fn close_sock(sock: sock_t) { let _ = libc::close(sock); }
|
|
|
|
|
|
|
|
pub fn last_error() -> IoError {
|
2014-10-10 10:11:49 -07:00
|
|
|
decode_error_detailed(os::errno() as i32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_net_error() -> IoError {
|
|
|
|
last_error()
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "system" {
|
|
|
|
fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_gai_error(s: libc::c_int) -> IoError {
|
|
|
|
use c_str::CString;
|
|
|
|
|
|
|
|
let mut err = decode_error(s);
|
|
|
|
err.detail = Some(unsafe {
|
|
|
|
CString::new(gai_strerror(s), false).as_str().unwrap().to_string()
|
|
|
|
});
|
2014-09-30 17:03:56 -07:00
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert an `errno` value into a high-level error variant and description.
|
|
|
|
pub fn decode_error(errno: i32) -> IoError {
|
|
|
|
// FIXME: this should probably be a bit more descriptive...
|
|
|
|
let (kind, desc) = 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"),
|
|
|
|
libc::ENOENT => (io::FileNotFound, "no such file or directory"),
|
|
|
|
libc::EISDIR => (io::InvalidInput, "illegal operation on a directory"),
|
|
|
|
libc::ENOSYS => (io::IoUnavailable, "function not implemented"),
|
|
|
|
libc::EINVAL => (io::InvalidInput, "invalid argument"),
|
|
|
|
libc::ENOTTY =>
|
|
|
|
(io::MismatchedFileTypeForOperation,
|
|
|
|
"file descriptor is not a TTY"),
|
|
|
|
libc::ETIMEDOUT => (io::TimedOut, "operation timed out"),
|
|
|
|
libc::ECANCELED => (io::TimedOut, "operation aborted"),
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
};
|
|
|
|
IoError { kind: kind, desc: desc, detail: None }
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:11:49 -07:00
|
|
|
pub fn decode_error_detailed(errno: i32) -> IoError {
|
|
|
|
let mut err = decode_error(errno);
|
|
|
|
err.detail = Some(os::error_string(errno));
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2014-09-30 17:03:56 -07:00
|
|
|
#[inline]
|
|
|
|
pub fn retry<I: PartialEq + num::One + Neg<I>> (f: || -> I) -> I {
|
|
|
|
let minus_one = -num::one::<I>();
|
|
|
|
loop {
|
|
|
|
let n = f();
|
|
|
|
if n == minus_one && os::errno() == libc::EINTR as int { }
|
|
|
|
else { return n }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ms_to_timeval(ms: u64) -> libc::timeval {
|
|
|
|
libc::timeval {
|
|
|
|
tv_sec: (ms / 1000) as libc::time_t,
|
|
|
|
tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wouldblock() -> bool {
|
|
|
|
let err = os::errno();
|
|
|
|
err == libc::EWOULDBLOCK as int || err == libc::EAGAIN as int
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:11:49 -07:00
|
|
|
pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
|
2014-09-30 17:03:56 -07:00
|
|
|
let set = nb as libc::c_int;
|
2014-10-10 10:11:49 -07:00
|
|
|
mkerr_libc(retry(|| unsafe { c::ioctl(fd, c::FIONBIO, &set) }))
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
2014-10-10 10:11:49 -07:00
|
|
|
|
|
|
|
// nothing needed on unix platforms
|
|
|
|
pub fn init_net() {}
|