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