]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/mod.rs
dc410cba89e04e4bfb4b52a0d39d30b75921ef85
[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
30 #[macro_use]
31 pub mod weak;
32
33 pub mod args;
34 pub mod android;
35 #[cfg(any(not(cargobuild), feature = "backtrace"))]
36 pub mod backtrace;
37 pub mod condvar;
38 pub mod env;
39 pub mod ext;
40 pub mod fd;
41 pub mod fs;
42 pub mod memchr;
43 pub mod mutex;
44 pub mod net;
45 pub mod os;
46 pub mod os_str;
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 #[cfg(not(test))]
59 pub fn init() {
60     use alloc::oom;
61
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     oom::set_oom_handler(oom_handler);
74
75     // A nicer handler for out-of-memory situations than the default one. This
76     // one prints a message to stderr before aborting. It is critical that this
77     // code does not allocate any memory since we are in an OOM situation. Any
78     // errors are ignored while printing since there's nothing we can do about
79     // them and we are about to exit anyways.
80     fn oom_handler() -> ! {
81         use intrinsics;
82         let msg = "fatal runtime error: out of memory\n";
83         unsafe {
84             libc::write(libc::STDERR_FILENO,
85                         msg.as_ptr() as *const libc::c_void,
86                         msg.len() as libc::size_t);
87             intrinsics::abort();
88         }
89     }
90
91     #[cfg(not(any(target_os = "nacl", target_os = "emscripten")))]
92     unsafe fn reset_sigpipe() {
93         assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != !0);
94     }
95     #[cfg(any(target_os = "nacl", target_os = "emscripten"))]
96     unsafe fn reset_sigpipe() {}
97 }
98
99 #[cfg(target_os = "android")]
100 pub use sys::android::signal;
101 #[cfg(not(target_os = "android"))]
102 pub use libc::signal;
103
104 pub fn decode_error_kind(errno: i32) -> ErrorKind {
105     match errno as libc::c_int {
106         libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
107         libc::ECONNRESET => ErrorKind::ConnectionReset,
108         libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
109         libc::EPIPE => ErrorKind::BrokenPipe,
110         libc::ENOTCONN => ErrorKind::NotConnected,
111         libc::ECONNABORTED => ErrorKind::ConnectionAborted,
112         libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
113         libc::EADDRINUSE => ErrorKind::AddrInUse,
114         libc::ENOENT => ErrorKind::NotFound,
115         libc::EINTR => ErrorKind::Interrupted,
116         libc::EINVAL => ErrorKind::InvalidInput,
117         libc::ETIMEDOUT => ErrorKind::TimedOut,
118         libc::EEXIST => ErrorKind::AlreadyExists,
119
120         // These two constants can have the same value on some systems,
121         // but different values on others, so we can't use a match
122         // clause
123         x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
124             ErrorKind::WouldBlock,
125
126         _ => ErrorKind::Other,
127     }
128 }
129
130 #[doc(hidden)]
131 pub trait IsMinusOne {
132     fn is_minus_one(&self) -> bool;
133 }
134
135 macro_rules! impl_is_minus_one {
136     ($($t:ident)*) => ($(impl IsMinusOne for $t {
137         fn is_minus_one(&self) -> bool {
138             *self == -1
139         }
140     })*)
141 }
142
143 impl_is_minus_one! { i8 i16 i32 i64 isize }
144
145 pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
146     if t.is_minus_one() {
147         Err(io::Error::last_os_error())
148     } else {
149         Ok(t)
150     }
151 }
152
153 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
154     where T: IsMinusOne,
155           F: FnMut() -> T
156 {
157     loop {
158         match cvt(f()) {
159             Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
160             other => return other,
161         }
162     }
163 }