1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(missing_docs)]
12 #![allow(non_camel_case_types)]
13 #![allow(unused_imports)]
15 #![allow(unused_unsafe)]
21 use num::{Int, SignedInt};
23 use io::{mod, IoResult, IoError};
24 use sys_common::mkerr_libc;
26 macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => (
27 static $name: Helper<$m> = Helper {
28 lock: ::rustrt::mutex::NATIVE_MUTEX_INIT,
29 chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> },
30 signal: ::cell::UnsafeCell { value: 0 },
31 initialized: ::cell::UnsafeCell { value: false },
37 pub mod helper_signal;
48 pub use sys_common::net::get_host_addresses;
51 // FIXME: move these to c module
52 pub type sock_t = self::fs::fd_t;
53 pub type wrlen = libc::size_t;
54 pub type msglen_t = libc::size_t;
55 pub unsafe fn close_sock(sock: sock_t) { let _ = libc::close(sock); }
57 pub fn last_error() -> IoError {
58 decode_error_detailed(os::errno() as i32)
61 pub fn last_net_error() -> IoError {
66 fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char;
69 pub fn last_gai_error(s: libc::c_int) -> IoError {
72 let mut err = decode_error(s);
73 err.detail = Some(unsafe {
74 CString::new(gai_strerror(s), false).as_str().unwrap().to_string()
79 /// Convert an `errno` value into a high-level error variant and description.
80 pub fn decode_error(errno: i32) -> IoError {
81 // FIXME: this should probably be a bit more descriptive...
82 let (kind, desc) = match errno {
83 libc::EOF => (io::EndOfFile, "end of file"),
84 libc::ECONNREFUSED => (io::ConnectionRefused, "connection refused"),
85 libc::ECONNRESET => (io::ConnectionReset, "connection reset"),
86 libc::EPERM | libc::EACCES =>
87 (io::PermissionDenied, "permission denied"),
88 libc::EPIPE => (io::BrokenPipe, "broken pipe"),
89 libc::ENOTCONN => (io::NotConnected, "not connected"),
90 libc::ECONNABORTED => (io::ConnectionAborted, "connection aborted"),
91 libc::EADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
92 libc::EADDRINUSE => (io::ConnectionRefused, "address in use"),
93 libc::ENOENT => (io::FileNotFound, "no such file or directory"),
94 libc::EISDIR => (io::InvalidInput, "illegal operation on a directory"),
95 libc::ENOSYS => (io::IoUnavailable, "function not implemented"),
96 libc::EINVAL => (io::InvalidInput, "invalid argument"),
98 (io::MismatchedFileTypeForOperation,
99 "file descriptor is not a TTY"),
100 libc::ETIMEDOUT => (io::TimedOut, "operation timed out"),
101 libc::ECANCELED => (io::TimedOut, "operation aborted"),
103 // These two constants can have the same value on some systems,
104 // but different values on others, so we can't use a match
106 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
107 (io::ResourceUnavailable, "resource temporarily unavailable"),
109 _ => (io::OtherIoError, "unknown error")
111 IoError { kind: kind, desc: desc, detail: None }
114 pub fn decode_error_detailed(errno: i32) -> IoError {
115 let mut err = decode_error(errno);
116 err.detail = Some(os::error_string(errno));
121 pub fn retry<T: SignedInt> (f: || -> T) -> T {
122 let one: T = Int::one();
125 if n == -one && os::errno() == libc::EINTR as int { }
130 pub fn ms_to_timeval(ms: u64) -> libc::timeval {
132 tv_sec: (ms / 1000) as libc::time_t,
133 tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t,
137 pub fn wouldblock() -> bool {
138 let err = os::errno();
139 err == libc::EWOULDBLOCK as int || err == libc::EAGAIN as int
142 pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
143 let set = nb as libc::c_int;
144 mkerr_libc(retry(|| unsafe { c::ioctl(fd, c::FIONBIO, &set) }))
147 // nothing needed on unix platforms