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