]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/mod.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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 use num::One;
16 use ops::Neg;
17
18 #[cfg(target_os = "android")] pub use os::android as platform;
19 #[cfg(target_os = "bitrig")] pub use os::bitrig as platform;
20 #[cfg(target_os = "dragonfly")] pub use os::dragonfly as platform;
21 #[cfg(target_os = "freebsd")] pub use os::freebsd as platform;
22 #[cfg(target_os = "ios")] pub use os::ios as platform;
23 #[cfg(target_os = "linux")] pub use os::linux as platform;
24 #[cfg(target_os = "macos")] pub use os::macos as platform;
25 #[cfg(target_os = "nacl")] pub use os::nacl as platform;
26 #[cfg(target_os = "netbsd")] pub use os::netbsd as platform;
27 #[cfg(target_os = "openbsd")] pub use os::openbsd as platform;
28 #[cfg(target_os = "solaris")] pub use os::solaris as platform;
29 #[cfg(target_os = "emscripten")] pub use os::emscripten as platform;
30
31 #[macro_use]
32 pub mod weak;
33
34 pub mod backtrace;
35 pub mod condvar;
36 pub mod ext;
37 pub mod fd;
38 pub mod fs;
39 pub mod mutex;
40 pub mod net;
41 pub mod os;
42 pub mod os_str;
43 pub mod pipe;
44 pub mod process;
45 pub mod rand;
46 pub mod rwlock;
47 pub mod stack_overflow;
48 pub mod thread;
49 pub mod thread_local;
50 pub mod time;
51 pub mod stdio;
52
53 #[cfg(not(test))]
54 pub fn init() {
55 use alloc::oom;
56
57 // By default, some platforms will send a *signal* when an EPIPE error
58 // would otherwise be delivered. This runtime doesn't install a SIGPIPE
59 // handler, causing it to kill the program, which isn't exactly what we
60 // want!
61 //
62 // Hence, we set SIGPIPE to ignore when the program starts up in order
63 // to prevent this problem.
64 unsafe {
65 reset_sigpipe();
66 }
67
68 oom::set_oom_handler(oom_handler);
69
70 // A nicer handler for out-of-memory situations than the default one. This
71 // one prints a message to stderr before aborting. It is critical that this
72 // code does not allocate any memory since we are in an OOM situation. Any
73 // errors are ignored while printing since there's nothing we can do about
74 // them and we are about to exit anyways.
75 fn oom_handler() -> ! {
76 use intrinsics;
77 let msg = "fatal runtime error: out of memory\n";
78 unsafe {
79 libc::write(libc::STDERR_FILENO,
80 msg.as_ptr() as *const libc::c_void,
81 msg.len() as libc::size_t);
82 intrinsics::abort();
83 }
84 }
85
86 #[cfg(not(target_os = "nacl"))]
87 unsafe fn reset_sigpipe() {
88 assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != !0);
89 }
90 #[cfg(target_os = "nacl")]
91 unsafe fn reset_sigpipe() {}
92 }
93
94 // Currently the minimum supported Android version of the standard library is
95 // API level 18 (android-18). Back in those days [1] the `signal` function was
96 // just an inline wrapper around `bsd_signal`, but starting in API level
97 // android-20 the `signal` symbols was introduced [2]. Finally, in android-21
98 // the API `bsd_signal` was removed [3].
99 //
100 // Basically this means that if we want to be binary compatible with multiple
101 // Android releases (oldest being 18 and newest being 21) then we need to check
102 // for both symbols and not actually link against either.
103 //
104 // Note that if we're not on android we just link against the `android` symbol
105 // itself.
106 //
107 // [1]: https://chromium.googlesource.com/android_tools/+/20ee6d20/ndk/platforms
108 // /android-18/arch-arm/usr/include/signal.h
109 // [2]: https://chromium.googlesource.com/android_tools/+/fbd420/ndk_experimental
110 // /platforms/android-20/arch-arm
111 // /usr/include/signal.h
112 // [3]: https://chromium.googlesource.com/android_tools/+/20ee6d/ndk/platforms
113 // /android-21/arch-arm/usr/include/signal.h
114 #[cfg(target_os = "android")]
115 unsafe fn signal(signum: libc::c_int,
116 handler: libc::sighandler_t) -> libc::sighandler_t {
117 weak!(fn signal(libc::c_int, libc::sighandler_t) -> libc::sighandler_t);
118 weak!(fn bsd_signal(libc::c_int, libc::sighandler_t) -> libc::sighandler_t);
119
120 let f = signal.get().or_else(|| bsd_signal.get());
121 let f = f.expect("neither `signal` nor `bsd_signal` symbols found");
122 f(signum, handler)
123 }
124
125 #[cfg(not(target_os = "android"))]
126 pub use libc::signal;
127
128 pub fn decode_error_kind(errno: i32) -> ErrorKind {
129 match errno as libc::c_int {
130 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
131 libc::ECONNRESET => ErrorKind::ConnectionReset,
132 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
133 libc::EPIPE => ErrorKind::BrokenPipe,
134 libc::ENOTCONN => ErrorKind::NotConnected,
135 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
136 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
137 libc::EADDRINUSE => ErrorKind::AddrInUse,
138 libc::ENOENT => ErrorKind::NotFound,
139 libc::EINTR => ErrorKind::Interrupted,
140 libc::EINVAL => ErrorKind::InvalidInput,
141 libc::ETIMEDOUT => ErrorKind::TimedOut,
142 libc::EEXIST => ErrorKind::AlreadyExists,
143
144 // These two constants can have the same value on some systems,
145 // but different values on others, so we can't use a match
146 // clause
147 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
148 ErrorKind::WouldBlock,
149
150 _ => ErrorKind::Other,
151 }
152 }
153
154 pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
155 let one: T = T::one();
156 if t == -one {
157 Err(io::Error::last_os_error())
158 } else {
159 Ok(t)
160 }
161 }
162
163 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
164 where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T
165 {
166 loop {
167 match cvt(f()) {
168 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
169 other => return other,
170 }
171 }
172 }