]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
11 #![allow(missing_docs, bad_style)]
13 use io
::{self, ErrorKind}
;
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;
47 pub mod stack_overflow
;
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
62 // Hence, we set SIGPIPE to ignore when the program starts up in order
63 // to prevent this problem.
68 oom
::set_oom_handler(oom_handler
);
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() -> ! {
77 let msg
= "fatal runtime error: out of memory\n";
79 libc
::write(libc
::STDERR_FILENO
,
80 msg
.as_ptr() as *const libc
::c_void
,
81 msg
.len() as libc
::size_t
);
86 #[cfg(not(target_os = "nacl"))]
87 unsafe fn reset_sigpipe() {
88 assert
!(signal(libc
::SIGPIPE
, libc
::SIG_IGN
) != !0);
90 #[cfg(target_os = "nacl")]
91 unsafe fn reset_sigpipe() {}
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].
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.
104 // Note that if we're not on android we just link against the `android` symbol
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
);
120 let f
= signal
.get().or_else(|| bsd_signal
.get());
121 let f
= f
.expect("neither `signal` nor `bsd_signal` symbols found");
125 #[cfg(not(target_os = "android"))]
126 pub use libc
::signal
;
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
,
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
147 x
if x
== libc
::EAGAIN
|| x
== libc
::EWOULDBLOCK
=>
148 ErrorKind
::WouldBlock
,
150 _
=> ErrorKind
::Other
,
154 pub fn cvt
<T
: One
+ PartialEq
+ Neg
<Output
=T
>>(t
: T
) -> io
::Result
<T
> {
155 let one
: T
= T
::one();
157 Err(io
::Error
::last_os_error())
163 pub fn cvt_r
<T
, F
>(mut f
: F
) -> io
::Result
<T
>
164 where T
: One
+ PartialEq
+ Neg
<Output
=T
>, F
: FnMut() -> T
168 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> {}
169 other
=> return other
,