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, bad_style)]
13 use io::{self, ErrorKind};
16 #[cfg(target_os = "android")] pub use os::android as platform;
17 #[cfg(target_os = "bitrig")] pub use os::bitrig as platform;
18 #[cfg(target_os = "dragonfly")] pub use os::dragonfly as platform;
19 #[cfg(target_os = "freebsd")] pub use os::freebsd as platform;
20 #[cfg(target_os = "haiku")] pub use os::haiku as platform;
21 #[cfg(target_os = "ios")] pub use os::ios as platform;
22 #[cfg(target_os = "linux")] pub use os::linux as platform;
23 #[cfg(target_os = "macos")] pub use os::macos as platform;
24 #[cfg(target_os = "nacl")] pub use os::nacl as platform;
25 #[cfg(target_os = "netbsd")] pub use os::netbsd as platform;
26 #[cfg(target_os = "openbsd")] pub use os::openbsd as platform;
27 #[cfg(target_os = "solaris")] pub use os::solaris as platform;
28 #[cfg(target_os = "emscripten")] pub use os::emscripten as platform;
35 #[cfg(any(not(cargobuild), feature = "backtrace"))]
52 pub mod stack_overflow;
62 // By default, some platforms will send a *signal* when an EPIPE error
63 // would otherwise be delivered. This runtime doesn't install a SIGPIPE
64 // handler, causing it to kill the program, which isn't exactly what we
67 // Hence, we set SIGPIPE to ignore when the program starts up in order
68 // to prevent this problem.
73 oom::set_oom_handler(oom_handler);
75 // A nicer handler for out-of-memory situations than the default one. This
76 // one prints a message to stderr before aborting. It is critical that this
77 // code does not allocate any memory since we are in an OOM situation. Any
78 // errors are ignored while printing since there's nothing we can do about
79 // them and we are about to exit anyways.
80 fn oom_handler() -> ! {
82 let msg = "fatal runtime error: out of memory\n";
84 libc::write(libc::STDERR_FILENO,
85 msg.as_ptr() as *const libc::c_void,
86 msg.len() as libc::size_t);
91 #[cfg(not(any(target_os = "nacl", target_os = "emscripten")))]
92 unsafe fn reset_sigpipe() {
93 assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != !0);
95 #[cfg(any(target_os = "nacl", target_os = "emscripten"))]
96 unsafe fn reset_sigpipe() {}
99 #[cfg(target_os = "android")]
100 pub use sys::android::signal;
101 #[cfg(not(target_os = "android"))]
102 pub use libc::signal;
104 pub fn decode_error_kind(errno: i32) -> ErrorKind {
105 match errno as libc::c_int {
106 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
107 libc::ECONNRESET => ErrorKind::ConnectionReset,
108 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
109 libc::EPIPE => ErrorKind::BrokenPipe,
110 libc::ENOTCONN => ErrorKind::NotConnected,
111 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
112 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
113 libc::EADDRINUSE => ErrorKind::AddrInUse,
114 libc::ENOENT => ErrorKind::NotFound,
115 libc::EINTR => ErrorKind::Interrupted,
116 libc::EINVAL => ErrorKind::InvalidInput,
117 libc::ETIMEDOUT => ErrorKind::TimedOut,
118 libc::EEXIST => ErrorKind::AlreadyExists,
120 // These two constants can have the same value on some systems,
121 // but different values on others, so we can't use a match
123 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
124 ErrorKind::WouldBlock,
126 _ => ErrorKind::Other,
131 pub trait IsMinusOne {
132 fn is_minus_one(&self) -> bool;
135 macro_rules! impl_is_minus_one {
136 ($($t:ident)*) => ($(impl IsMinusOne for $t {
137 fn is_minus_one(&self) -> bool {
143 impl_is_minus_one! { i8 i16 i32 i64 isize }
145 pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
146 if t.is_minus_one() {
147 Err(io::Error::last_os_error())
153 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
159 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
160 other => return other,