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