]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/mod.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / libstd / sys / unix / mod.rs
CommitLineData
b7449926 1#![allow(missing_docs, nonstandard_style)]
1a4d82fc 2
532ac7d7
XL
3use crate::io::ErrorKind;
4
dfeec247
XL
5#[cfg(any(doc, target_os = "linux"))]
6pub use crate::os::linux as platform;
7
8#[cfg(all(not(doc), target_os = "android"))]
9pub use crate::os::android as platform;
10#[cfg(all(not(doc), target_os = "dragonfly"))]
11pub use crate::os::dragonfly as platform;
12#[cfg(all(not(doc), target_os = "emscripten"))]
13pub use crate::os::emscripten as platform;
14#[cfg(all(not(doc), target_os = "freebsd"))]
15pub use crate::os::freebsd as platform;
16#[cfg(all(not(doc), target_os = "fuchsia"))]
17pub use crate::os::fuchsia as platform;
18#[cfg(all(not(doc), target_os = "haiku"))]
19pub use crate::os::haiku as platform;
20#[cfg(all(not(doc), target_os = "ios"))]
21pub use crate::os::ios as platform;
22#[cfg(all(not(doc), target_os = "l4re"))]
23pub use crate::os::linux as platform;
24#[cfg(all(not(doc), target_os = "macos"))]
25pub use crate::os::macos as platform;
26#[cfg(all(not(doc), target_os = "netbsd"))]
27pub use crate::os::netbsd as platform;
28#[cfg(all(not(doc), target_os = "openbsd"))]
29pub use crate::os::openbsd as platform;
30#[cfg(all(not(doc), target_os = "redox"))]
31pub use crate::os::redox as platform;
32#[cfg(all(not(doc), target_os = "solaris"))]
33pub use crate::os::solaris as platform;
7453a54e 34
abe05a73
XL
35pub use self::rand::hashmap_random_keys;
36pub use libc::strlen;
37
7453a54e
SL
38#[macro_use]
39pub mod weak;
d9579d0f 40
a1dfa0c6 41pub mod alloc;
a7813a04 42pub mod android;
dfeec247 43pub mod args;
abe05a73 44pub mod cmath;
1a4d82fc 45pub mod condvar;
c30ab7b3 46pub mod env;
85aaf69f 47pub mod ext;
c30ab7b3 48pub mod fast_thread_local;
85aaf69f 49pub mod fd;
d9579d0f 50pub mod fs;
9fa01778 51pub mod io;
dfeec247
XL
52#[cfg(target_os = "l4re")]
53mod l4re;
54pub mod memchr;
1a4d82fc 55pub mod mutex;
ea8adc8c 56#[cfg(not(target_os = "l4re"))]
85aaf69f 57pub mod net;
ea8adc8c 58#[cfg(target_os = "l4re")]
ea8adc8c 59pub use self::l4re::net;
1a4d82fc 60pub mod os;
c30ab7b3 61pub mod path;
d9579d0f
AL
62pub mod pipe;
63pub mod process;
7453a54e 64pub mod rand;
1a4d82fc
JJ
65pub mod rwlock;
66pub mod stack_overflow;
dfeec247 67pub mod stdio;
1a4d82fc
JJ
68pub mod thread;
69pub mod thread_local;
85aaf69f 70pub mod time;
1a4d82fc 71
532ac7d7
XL
72pub use crate::sys_common::os_str_bytes as os_str;
73
7453a54e 74#[cfg(not(test))]
e9174d1e 75pub fn init() {
b039eaaf 76 // By default, some platforms will send a *signal* when an EPIPE error
e9174d1e
SL
77 // would otherwise be delivered. This runtime doesn't install a SIGPIPE
78 // handler, causing it to kill the program, which isn't exactly what we
79 // want!
80 //
81 // Hence, we set SIGPIPE to ignore when the program starts up in order
82 // to prevent this problem.
83 unsafe {
7453a54e 84 reset_sigpipe();
e9174d1e 85 }
9cc50fc6 86
83c7162d 87 #[cfg(not(any(target_os = "emscripten", target_os = "fuchsia")))]
7453a54e 88 unsafe fn reset_sigpipe() {
cc61c64b 89 assert!(signal(libc::SIGPIPE, libc::SIG_IGN) != libc::SIG_ERR);
7453a54e 90 }
83c7162d 91 #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
7453a54e 92 unsafe fn reset_sigpipe() {}
e9174d1e
SL
93}
94
54a0048b 95#[cfg(target_os = "android")]
532ac7d7 96pub use crate::sys::android::signal;
54a0048b
SL
97#[cfg(not(target_os = "android"))]
98pub use libc::signal;
99
85aaf69f
SL
100pub 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,
c34b1796
AL
108 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
109 libc::EADDRINUSE => ErrorKind::AddrInUse,
110 libc::ENOENT => ErrorKind::NotFound,
85aaf69f
SL
111 libc::EINTR => ErrorKind::Interrupted,
112 libc::EINVAL => ErrorKind::InvalidInput,
85aaf69f 113 libc::ETIMEDOUT => ErrorKind::TimedOut,
92a42be0 114 libc::EEXIST => ErrorKind::AlreadyExists,
85aaf69f
SL
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
dfeec247 119 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
85aaf69f
SL
120
121 _ => ErrorKind::Other,
122 }
123}
124
3157f602
XL
125#[doc(hidden)]
126pub trait IsMinusOne {
127 fn is_minus_one(&self) -> bool;
128}
129
130macro_rules! impl_is_minus_one {
131 ($($t:ident)*) => ($(impl IsMinusOne for $t {
132 fn is_minus_one(&self) -> bool {
133 *self == -1
134 }
135 })*)
136}
137
138impl_is_minus_one! { i8 i16 i32 i64 isize }
139
532ac7d7 140pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
dfeec247 141 if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) }
85aaf69f
SL
142}
143
532ac7d7 144pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T>
dfeec247
XL
145where
146 T: IsMinusOne,
147 F: FnMut() -> T,
85aaf69f
SL
148{
149 loop {
150 match cvt(f()) {
151 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
152 other => return other,
153 }
154 }
155}
c30ab7b3
SL
156
157// On Unix-like platforms, libc::abort will unregister signal handlers
158// including the SIGABRT handler, preventing the abort from being blocked, and
74b04a01 159// fclose streams, with the side effect of flushing them so libc buffered
c30ab7b3
SL
160// output will be printed. Additionally the shell will generally print a more
161// understandable error message like "Abort trap" rather than "Illegal
162// instruction" that intrinsics::abort would cause, as intrinsics::abort is
163// implemented as an illegal instruction.
164pub unsafe fn abort_internal() -> ! {
532ac7d7 165 libc::abort()
c30ab7b3 166}