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;
29 #[cfg(target_os = "fuchsia")] pub use os::fuchsia as platform;
36 #[cfg(feature = "backtrace")]
41 pub mod fast_thread_local;
54 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 #[cfg(not(any(target_os = "nacl", target_os = "emscripten", target_os="fuchsia")))]
74 unsafe fn reset_sigpipe() {
75 assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
77 #[cfg(any(target_os = "nacl", target_os = "emscripten", target_os="fuchsia"))]
78 unsafe fn reset_sigpipe() {}
81 #[cfg(target_os = "android")]
82 pub use sys::android::signal;
83 #[cfg(not(target_os = "android"))]
86 pub fn decode_error_kind(errno: i32) -> ErrorKind {
87 match errno as libc::c_int {
88 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
89 libc::ECONNRESET => ErrorKind::ConnectionReset,
90 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
91 libc::EPIPE => ErrorKind::BrokenPipe,
92 libc::ENOTCONN => ErrorKind::NotConnected,
93 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
94 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
95 libc::EADDRINUSE => ErrorKind::AddrInUse,
96 libc::ENOENT => ErrorKind::NotFound,
97 libc::EINTR => ErrorKind::Interrupted,
98 libc::EINVAL => ErrorKind::InvalidInput,
99 libc::ETIMEDOUT => ErrorKind::TimedOut,
100 libc::EEXIST => ErrorKind::AlreadyExists,
102 // These two constants can have the same value on some systems,
103 // but different values on others, so we can't use a match
105 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
106 ErrorKind::WouldBlock,
108 _ => ErrorKind::Other,
113 pub trait IsMinusOne {
114 fn is_minus_one(&self) -> bool;
117 macro_rules! impl_is_minus_one {
118 ($($t:ident)*) => ($(impl IsMinusOne for $t {
119 fn is_minus_one(&self) -> bool {
125 impl_is_minus_one! { i8 i16 i32 i64 isize }
127 pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
128 if t.is_minus_one() {
129 Err(io::Error::last_os_error())
135 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
141 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
142 other => return other,
147 // On Unix-like platforms, libc::abort will unregister signal handlers
148 // including the SIGABRT handler, preventing the abort from being blocked, and
149 // fclose streams, with the side effect of flushing them so libc bufferred
150 // output will be printed. Additionally the shell will generally print a more
151 // understandable error message like "Abort trap" rather than "Illegal
152 // instruction" that intrinsics::abort would cause, as intrinsics::abort is
153 // implemented as an illegal instruction.
154 pub unsafe fn abort_internal() -> ! {