1 #![allow(missing_docs, nonstandard_style)]
3 use crate::io::ErrorKind;
5 pub use self::rand::hashmap_random_keys;
22 #[cfg(any(target_os = "linux", target_os = "android"))]
24 #[cfg(target_os = "l4re")]
28 #[cfg(not(target_os = "l4re"))]
30 #[cfg(target_os = "l4re")]
31 pub use self::l4re::net;
38 pub mod stack_overflow;
41 pub mod thread_local_dtor;
42 pub mod thread_local_key;
45 pub use crate::sys_common::os_str_bytes as os_str;
49 // The standard streams might be closed on application startup. To prevent
50 // std::io::{stdin, stdout,stderr} objects from using other unrelated file
51 // resources opened later, we reopen standards streams when they are closed.
53 sanitize_standard_fds();
56 // By default, some platforms will send a *signal* when an EPIPE error
57 // would otherwise be delivered. This runtime doesn't install a SIGPIPE
58 // handler, causing it to kill the program, which isn't exactly what we
61 // Hence, we set SIGPIPE to ignore when the program starts up in order
62 // to prevent this problem.
69 // The standard fds are always available in Miri.
70 unsafe fn sanitize_standard_fds() {}
71 } else if #[cfg(not(any(
72 target_os = "emscripten",
73 target_os = "fuchsia",
74 target_os = "vxworks",
75 // The poll on Darwin doesn't set POLLNVAL for closed fds.
80 // In the case when all file descriptors are open, the poll has been
81 // observed to perform better than fcntl (on GNU/Linux).
82 unsafe fn sanitize_standard_fds() {
83 use crate::sys::os::errno;
84 let pfds: &mut [_] = &mut [
85 libc::pollfd { fd: 0, events: 0, revents: 0 },
86 libc::pollfd { fd: 1, events: 0, revents: 0 },
87 libc::pollfd { fd: 2, events: 0, revents: 0 },
89 while libc::poll(pfds.as_mut_ptr(), 3, 0) == -1 {
90 if errno() == libc::EINTR {
96 if pfd.revents & libc::POLLNVAL == 0 {
99 if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
100 // If the stream is closed but we failed to reopen it, abort the
101 // process. Otherwise we wouldn't preserve the safety of
102 // operations on the corresponding Rust object Stdin, Stdout, or
108 } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "redox"))] {
109 unsafe fn sanitize_standard_fds() {
110 use crate::sys::os::errno;
112 if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
113 if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
120 unsafe fn sanitize_standard_fds() {}
124 #[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))]
125 unsafe fn reset_sigpipe() {
126 assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
128 #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
129 unsafe fn reset_sigpipe() {}
132 #[cfg(target_os = "android")]
133 pub use crate::sys::android::signal;
134 #[cfg(not(target_os = "android"))]
135 pub use libc::signal;
137 pub fn decode_error_kind(errno: i32) -> ErrorKind {
138 match errno as libc::c_int {
139 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
140 libc::ECONNRESET => ErrorKind::ConnectionReset,
141 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
142 libc::EPIPE => ErrorKind::BrokenPipe,
143 libc::ENOTCONN => ErrorKind::NotConnected,
144 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
145 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
146 libc::EADDRINUSE => ErrorKind::AddrInUse,
147 libc::ENOENT => ErrorKind::NotFound,
148 libc::EINTR => ErrorKind::Interrupted,
149 libc::EINVAL => ErrorKind::InvalidInput,
150 libc::ETIMEDOUT => ErrorKind::TimedOut,
151 libc::EEXIST => ErrorKind::AlreadyExists,
152 libc::ENOSYS => ErrorKind::Unsupported,
154 // These two constants can have the same value on some systems,
155 // but different values on others, so we can't use a match
157 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
159 _ => ErrorKind::Other,
164 pub trait IsMinusOne {
165 fn is_minus_one(&self) -> bool;
168 macro_rules! impl_is_minus_one {
169 ($($t:ident)*) => ($(impl IsMinusOne for $t {
170 fn is_minus_one(&self) -> bool {
176 impl_is_minus_one! { i8 i16 i32 i64 isize }
178 pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
179 if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) }
182 pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T>
189 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
190 other => return other,
195 pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> {
196 if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) }
199 // On Unix-like platforms, libc::abort will unregister signal handlers
200 // including the SIGABRT handler, preventing the abort from being blocked, and
201 // fclose streams, with the side effect of flushing them so libc buffered
202 // output will be printed. Additionally the shell will generally print a more
203 // understandable error message like "Abort trap" rather than "Illegal
204 // instruction" that intrinsics::abort would cause, as intrinsics::abort is
205 // implemented as an illegal instruction.
206 pub fn abort_internal() -> ! {
207 unsafe { libc::abort() }
211 if #[cfg(target_os = "android")] {
213 #[link(name = "log")]
214 #[link(name = "gcc")]
216 } else if #[cfg(target_os = "freebsd")] {
217 #[link(name = "execinfo")]
218 #[link(name = "pthread")]
220 } else if #[cfg(target_os = "netbsd")] {
221 #[link(name = "pthread")]
224 } else if #[cfg(any(target_os = "dragonfly", target_os = "openbsd"))] {
225 #[link(name = "pthread")]
227 } else if #[cfg(target_os = "solaris")] {
228 #[link(name = "socket")]
229 #[link(name = "posix4")]
230 #[link(name = "pthread")]
231 #[link(name = "resolv")]
233 } else if #[cfg(target_os = "illumos")] {
234 #[link(name = "socket")]
235 #[link(name = "posix4")]
236 #[link(name = "pthread")]
237 #[link(name = "resolv")]
238 #[link(name = "nsl")]
239 // Use libumem for the (malloc-compatible) allocator
240 #[link(name = "umem")]
242 } else if #[cfg(target_os = "macos")] {
243 #[link(name = "System")]
244 // res_init and friends require -lresolv on macOS/iOS.
245 // See #41582 and http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
246 #[link(name = "resolv")]
248 } else if #[cfg(target_os = "ios")] {
249 #[link(name = "System")]
250 #[link(name = "objc")]
251 #[link(name = "Security", kind = "framework")]
252 #[link(name = "Foundation", kind = "framework")]
253 #[link(name = "resolv")]
255 } else if #[cfg(target_os = "fuchsia")] {
256 #[link(name = "zircon")]
257 #[link(name = "fdio")]