]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | #![allow(missing_docs, nonstandard_style)] |
1a4d82fc | 2 | |
532ac7d7 XL |
3 | use crate::io::ErrorKind; |
4 | ||
dfeec247 XL |
5 | #[cfg(any(doc, target_os = "linux"))] |
6 | pub use crate::os::linux as platform; | |
7 | ||
8 | #[cfg(all(not(doc), target_os = "android"))] | |
9 | pub use crate::os::android as platform; | |
10 | #[cfg(all(not(doc), target_os = "dragonfly"))] | |
11 | pub use crate::os::dragonfly as platform; | |
12 | #[cfg(all(not(doc), target_os = "emscripten"))] | |
13 | pub use crate::os::emscripten as platform; | |
14 | #[cfg(all(not(doc), target_os = "freebsd"))] | |
15 | pub use crate::os::freebsd as platform; | |
16 | #[cfg(all(not(doc), target_os = "fuchsia"))] | |
17 | pub use crate::os::fuchsia as platform; | |
18 | #[cfg(all(not(doc), target_os = "haiku"))] | |
19 | pub use crate::os::haiku as platform; | |
ba9703b0 XL |
20 | #[cfg(all(not(doc), target_os = "illumos"))] |
21 | pub use crate::os::illumos as platform; | |
dfeec247 XL |
22 | #[cfg(all(not(doc), target_os = "ios"))] |
23 | pub use crate::os::ios as platform; | |
24 | #[cfg(all(not(doc), target_os = "l4re"))] | |
25 | pub use crate::os::linux as platform; | |
26 | #[cfg(all(not(doc), target_os = "macos"))] | |
27 | pub use crate::os::macos as platform; | |
28 | #[cfg(all(not(doc), target_os = "netbsd"))] | |
29 | pub use crate::os::netbsd as platform; | |
30 | #[cfg(all(not(doc), target_os = "openbsd"))] | |
31 | pub use crate::os::openbsd as platform; | |
32 | #[cfg(all(not(doc), target_os = "redox"))] | |
33 | pub use crate::os::redox as platform; | |
34 | #[cfg(all(not(doc), target_os = "solaris"))] | |
35 | pub use crate::os::solaris as platform; | |
7453a54e | 36 | |
abe05a73 XL |
37 | pub use self::rand::hashmap_random_keys; |
38 | pub use libc::strlen; | |
39 | ||
7453a54e SL |
40 | #[macro_use] |
41 | pub mod weak; | |
d9579d0f | 42 | |
a1dfa0c6 | 43 | pub mod alloc; |
a7813a04 | 44 | pub mod android; |
dfeec247 | 45 | pub mod args; |
abe05a73 | 46 | pub mod cmath; |
1a4d82fc | 47 | pub mod condvar; |
c30ab7b3 | 48 | pub mod env; |
85aaf69f | 49 | pub mod ext; |
c30ab7b3 | 50 | pub mod fast_thread_local; |
85aaf69f | 51 | pub mod fd; |
d9579d0f | 52 | pub mod fs; |
9fa01778 | 53 | pub mod io; |
dfeec247 XL |
54 | #[cfg(target_os = "l4re")] |
55 | mod l4re; | |
56 | pub mod memchr; | |
1a4d82fc | 57 | pub mod mutex; |
ea8adc8c | 58 | #[cfg(not(target_os = "l4re"))] |
85aaf69f | 59 | pub mod net; |
ea8adc8c | 60 | #[cfg(target_os = "l4re")] |
ea8adc8c | 61 | pub use self::l4re::net; |
1a4d82fc | 62 | pub mod os; |
c30ab7b3 | 63 | pub mod path; |
d9579d0f AL |
64 | pub mod pipe; |
65 | pub mod process; | |
7453a54e | 66 | pub mod rand; |
1a4d82fc JJ |
67 | pub mod rwlock; |
68 | pub mod stack_overflow; | |
dfeec247 | 69 | pub mod stdio; |
1a4d82fc JJ |
70 | pub mod thread; |
71 | pub mod thread_local; | |
85aaf69f | 72 | pub mod time; |
1a4d82fc | 73 | |
532ac7d7 XL |
74 | pub use crate::sys_common::os_str_bytes as os_str; |
75 | ||
7453a54e | 76 | #[cfg(not(test))] |
e9174d1e | 77 | pub fn init() { |
b039eaaf | 78 | // By default, some platforms will send a *signal* when an EPIPE error |
e9174d1e SL |
79 | // would otherwise be delivered. This runtime doesn't install a SIGPIPE |
80 | // handler, causing it to kill the program, which isn't exactly what we | |
81 | // want! | |
82 | // | |
83 | // Hence, we set SIGPIPE to ignore when the program starts up in order | |
84 | // to prevent this problem. | |
85 | unsafe { | |
7453a54e | 86 | reset_sigpipe(); |
e9174d1e | 87 | } |
9cc50fc6 | 88 | |
83c7162d | 89 | #[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))] |
7453a54e | 90 | unsafe fn reset_sigpipe() { |
cc61c64b | 91 | assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR); |
7453a54e | 92 | } |
83c7162d | 93 | #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))] |
7453a54e | 94 | unsafe fn reset_sigpipe() {} |
e9174d1e SL |
95 | } |
96 | ||
54a0048b | 97 | #[cfg(target_os = "android")] |
532ac7d7 | 98 | pub use crate::sys::android::signal; |
54a0048b SL |
99 | #[cfg(not(target_os = "android"))] |
100 | pub use libc::signal; | |
101 | ||
85aaf69f SL |
102 | pub fn decode_error_kind(errno: i32) -> ErrorKind { |
103 | match errno as libc::c_int { | |
104 | libc::ECONNREFUSED => ErrorKind::ConnectionRefused, | |
105 | libc::ECONNRESET => ErrorKind::ConnectionReset, | |
106 | libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied, | |
107 | libc::EPIPE => ErrorKind::BrokenPipe, | |
108 | libc::ENOTCONN => ErrorKind::NotConnected, | |
109 | libc::ECONNABORTED => ErrorKind::ConnectionAborted, | |
c34b1796 AL |
110 | libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, |
111 | libc::EADDRINUSE => ErrorKind::AddrInUse, | |
112 | libc::ENOENT => ErrorKind::NotFound, | |
85aaf69f SL |
113 | libc::EINTR => ErrorKind::Interrupted, |
114 | libc::EINVAL => ErrorKind::InvalidInput, | |
85aaf69f | 115 | libc::ETIMEDOUT => ErrorKind::TimedOut, |
92a42be0 | 116 | libc::EEXIST => ErrorKind::AlreadyExists, |
85aaf69f SL |
117 | |
118 | // These two constants can have the same value on some systems, | |
119 | // but different values on others, so we can't use a match | |
120 | // clause | |
dfeec247 | 121 | x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock, |
85aaf69f SL |
122 | |
123 | _ => ErrorKind::Other, | |
124 | } | |
125 | } | |
126 | ||
3157f602 XL |
127 | #[doc(hidden)] |
128 | pub trait IsMinusOne { | |
129 | fn is_minus_one(&self) -> bool; | |
130 | } | |
131 | ||
132 | macro_rules! impl_is_minus_one { | |
133 | ($($t:ident)*) => ($(impl IsMinusOne for $t { | |
134 | fn is_minus_one(&self) -> bool { | |
135 | *self == -1 | |
136 | } | |
137 | })*) | |
138 | } | |
139 | ||
140 | impl_is_minus_one! { i8 i16 i32 i64 isize } | |
141 | ||
532ac7d7 | 142 | pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> { |
dfeec247 | 143 | if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) } |
85aaf69f SL |
144 | } |
145 | ||
532ac7d7 | 146 | pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T> |
dfeec247 XL |
147 | where |
148 | T: IsMinusOne, | |
149 | F: FnMut() -> T, | |
85aaf69f SL |
150 | { |
151 | loop { | |
152 | match cvt(f()) { | |
153 | Err(ref e) if e.kind() == ErrorKind::Interrupted => {} | |
154 | other => return other, | |
155 | } | |
156 | } | |
157 | } | |
c30ab7b3 SL |
158 | |
159 | // On Unix-like platforms, libc::abort will unregister signal handlers | |
160 | // including the SIGABRT handler, preventing the abort from being blocked, and | |
74b04a01 | 161 | // fclose streams, with the side effect of flushing them so libc buffered |
c30ab7b3 SL |
162 | // output will be printed. Additionally the shell will generally print a more |
163 | // understandable error message like "Abort trap" rather than "Illegal | |
164 | // instruction" that intrinsics::abort would cause, as intrinsics::abort is | |
165 | // implemented as an illegal instruction. | |
f9f354fc XL |
166 | pub fn abort_internal() -> ! { |
167 | unsafe { libc::abort() } | |
c30ab7b3 | 168 | } |