1 #![allow(missing_docs, nonstandard_style)]
3 use crate::io::ErrorKind;
5 #[cfg(any(doc, target_os = "linux"))]
6 pub use crate::os::linux as platform;
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;
20 #[cfg(all(not(doc), target_os = "illumos"))]
21 pub use crate::os::illumos as platform;
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;
37 pub use self::rand::hashmap_random_keys;
54 #[cfg(target_os = "l4re")]
58 #[cfg(not(target_os = "l4re"))]
60 #[cfg(target_os = "l4re")]
61 pub use self::l4re::net;
68 pub mod stack_overflow;
71 pub mod thread_local_dtor;
72 pub mod thread_local_key;
75 pub use crate::sys_common::os_str_bytes as os_str;
79 // The standard streams might be closed on application startup. To prevent
80 // std::io::{stdin, stdout,stderr} objects from using other unrelated file
81 // resources opened later, we reopen standards streams when they are closed.
83 sanitize_standard_fds();
86 // By default, some platforms will send a *signal* when an EPIPE error
87 // would otherwise be delivered. This runtime doesn't install a SIGPIPE
88 // handler, causing it to kill the program, which isn't exactly what we
91 // Hence, we set SIGPIPE to ignore when the program starts up in order
92 // to prevent this problem.
99 // The standard fds are always available in Miri.
100 unsafe fn sanitize_standard_fds() {}
101 } else if #[cfg(not(any(
102 target_os = "emscripten",
103 target_os = "fuchsia",
104 // The poll on Darwin doesn't set POLLNVAL for closed fds.
109 // In the case when all file descriptors are open, the poll has been
110 // observed to perform better than fcntl (on GNU/Linux).
111 unsafe fn sanitize_standard_fds() {
112 use crate::sys::os::errno;
113 let pfds: &mut [_] = &mut [
114 libc::pollfd { fd: 0, events: 0, revents: 0 },
115 libc::pollfd { fd: 1, events: 0, revents: 0 },
116 libc::pollfd { fd: 2, events: 0, revents: 0 },
118 while libc::poll(pfds.as_mut_ptr(), 3, 0) == -1 {
119 if errno() == libc::EINTR {
125 if pfd.revents & libc::POLLNVAL == 0 {
128 if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
129 // If the stream is closed but we failed to reopen it, abort the
130 // process. Otherwise we wouldn't preserve the safety of
131 // operations on the corresponding Rust object Stdin, Stdout, or
137 } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "redox"))] {
138 unsafe fn sanitize_standard_fds() {
139 use crate::sys::os::errno;
141 if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
142 if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
149 unsafe fn sanitize_standard_fds() {}
153 #[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))]
154 unsafe fn reset_sigpipe() {
155 assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
157 #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
158 unsafe fn reset_sigpipe() {}
161 #[cfg(target_os = "android")]
162 pub use crate::sys::android::signal;
163 #[cfg(not(target_os = "android"))]
164 pub use libc::signal;
166 pub fn decode_error_kind(errno: i32) -> ErrorKind {
167 match errno as libc::c_int {
168 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
169 libc::ECONNRESET => ErrorKind::ConnectionReset,
170 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
171 libc::EPIPE => ErrorKind::BrokenPipe,
172 libc::ENOTCONN => ErrorKind::NotConnected,
173 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
174 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
175 libc::EADDRINUSE => ErrorKind::AddrInUse,
176 libc::ENOENT => ErrorKind::NotFound,
177 libc::EINTR => ErrorKind::Interrupted,
178 libc::EINVAL => ErrorKind::InvalidInput,
179 libc::ETIMEDOUT => ErrorKind::TimedOut,
180 libc::EEXIST => ErrorKind::AlreadyExists,
182 // These two constants can have the same value on some systems,
183 // but different values on others, so we can't use a match
185 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
187 _ => ErrorKind::Other,
192 pub trait IsMinusOne {
193 fn is_minus_one(&self) -> bool;
196 macro_rules! impl_is_minus_one {
197 ($($t:ident)*) => ($(impl IsMinusOne for $t {
198 fn is_minus_one(&self) -> bool {
204 impl_is_minus_one! { i8 i16 i32 i64 isize }
206 pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
207 if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) }
210 pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T>
217 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
218 other => return other,
223 pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> {
224 if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) }
227 // On Unix-like platforms, libc::abort will unregister signal handlers
228 // including the SIGABRT handler, preventing the abort from being blocked, and
229 // fclose streams, with the side effect of flushing them so libc buffered
230 // output will be printed. Additionally the shell will generally print a more
231 // understandable error message like "Abort trap" rather than "Illegal
232 // instruction" that intrinsics::abort would cause, as intrinsics::abort is
233 // implemented as an illegal instruction.
234 pub fn abort_internal() -> ! {
235 unsafe { libc::abort() }