]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/mod.rs
Merge commit '645ef505da378b6f810b1567806d1bcc2856395f' into clippyup
[rust.git] / library / std / src / sys / unix / mod.rs
1 #![allow(missing_docs, nonstandard_style)]
2
3 use crate::io::ErrorKind;
4
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;
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;
36
37 pub use self::rand::hashmap_random_keys;
38 pub use libc::strlen;
39
40 #[macro_use]
41 pub mod weak;
42
43 pub mod alloc;
44 pub mod android;
45 pub mod args;
46 pub mod cmath;
47 pub mod condvar;
48 pub mod env;
49 pub mod ext;
50 pub mod fd;
51 pub mod fs;
52 pub mod futex;
53 pub mod io;
54 #[cfg(target_os = "l4re")]
55 mod l4re;
56 pub mod memchr;
57 pub mod mutex;
58 #[cfg(not(target_os = "l4re"))]
59 pub mod net;
60 #[cfg(target_os = "l4re")]
61 pub use self::l4re::net;
62 pub mod os;
63 pub mod path;
64 pub mod pipe;
65 pub mod process;
66 pub mod rand;
67 pub mod rwlock;
68 pub mod stack_overflow;
69 pub mod stdio;
70 pub mod thread;
71 pub mod thread_local_dtor;
72 pub mod thread_local_key;
73 pub mod time;
74
75 pub use crate::sys_common::os_str_bytes as os_str;
76
77 #[cfg(not(test))]
78 pub fn init() {
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.
82     unsafe {
83         sanitize_standard_fds();
84     }
85
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
89     // want!
90     //
91     // Hence, we set SIGPIPE to ignore when the program starts up in order
92     // to prevent this problem.
93     unsafe {
94         reset_sigpipe();
95     }
96
97     cfg_if::cfg_if! {
98         if #[cfg(miri)] {
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.
105             target_os = "macos",
106             target_os = "ios",
107             target_os = "redox",
108         )))] {
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 },
117                 ];
118                 while libc::poll(pfds.as_mut_ptr(), 3, 0) == -1 {
119                     if errno() == libc::EINTR {
120                         continue;
121                     }
122                     libc::abort();
123                 }
124                 for pfd in pfds {
125                     if pfd.revents & libc::POLLNVAL == 0 {
126                         continue;
127                     }
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
132                         // Stderr.
133                         libc::abort();
134                     }
135                 }
136             }
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;
140                 for fd in 0..3 {
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 {
143                             libc::abort();
144                         }
145                     }
146                 }
147             }
148         } else {
149             unsafe fn sanitize_standard_fds() {}
150         }
151     }
152
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);
156     }
157     #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
158     unsafe fn reset_sigpipe() {}
159 }
160
161 #[cfg(target_os = "android")]
162 pub use crate::sys::android::signal;
163 #[cfg(not(target_os = "android"))]
164 pub use libc::signal;
165
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,
181
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
184         // clause
185         x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
186
187         _ => ErrorKind::Other,
188     }
189 }
190
191 #[doc(hidden)]
192 pub trait IsMinusOne {
193     fn is_minus_one(&self) -> bool;
194 }
195
196 macro_rules! impl_is_minus_one {
197     ($($t:ident)*) => ($(impl IsMinusOne for $t {
198         fn is_minus_one(&self) -> bool {
199             *self == -1
200         }
201     })*)
202 }
203
204 impl_is_minus_one! { i8 i16 i32 i64 isize }
205
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) }
208 }
209
210 pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T>
211 where
212     T: IsMinusOne,
213     F: FnMut() -> T,
214 {
215     loop {
216         match cvt(f()) {
217             Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
218             other => return other,
219         }
220     }
221 }
222
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)) }
225 }
226
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() }
236 }