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