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