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