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