1#![allow(missing_docs, nonstandard_style)]
2
3use crate::io;
4
5#[cfg(target_os = "fuchsia")]
6pub mod fuchsia;
7pub mod futex;
8#[cfg(target_os = "linux")]
9pub mod linux;
10pub mod os;
11pub mod stack_overflow;
12pub mod sync;
13pub mod thread_parking;
14pub mod time;
15pub mod weak;
16
17#[cfg(target_os = "espidf")]
18pub fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {}
19
20#[cfg(not(target_os = "espidf"))]
21#[cfg_attr(target_os = "vita", allow(unused_variables))]
22pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
26 sanitize_standard_fds();
30
31 reset_sigpipe(sigpipe);
40
41 stack_overflow::init();
42 #[cfg(not(target_os = "vita"))]
43 crate::sys::args::init(argc, argv);
44
45 if cfg!(target_vendor = "apple") {
51 crate::sys::thread::set_name(c"main");
52 }
53
54 unsafe fn sanitize_standard_fds() {
55 #[allow(dead_code, unused_variables, unused_mut)]
56 let mut opened_devnull = -1;
57 #[allow(dead_code, unused_variables, unused_mut)]
58 let mut open_devnull = || {
59 #[cfg(not(all(target_os = "linux", target_env = "gnu")))]
60 use libc::open;
61 #[cfg(all(target_os = "linux", target_env = "gnu"))]
62 use libc::open64 as open;
63
64 if opened_devnull != -1 {
65 if libc::dup(opened_devnull) != -1 {
66 return;
67 }
68 }
69 opened_devnull = open(c"/dev/null".as_ptr(), libc::O_RDWR, 0);
70 if opened_devnull == -1 {
71 libc::abort();
76 }
77 };
78
79 #[cfg(not(any(
81 miri,
82 target_os = "emscripten",
83 target_os = "fuchsia",
84 target_os = "vxworks",
85 target_os = "redox",
86 target_os = "l4re",
87 target_os = "horizon",
88 target_os = "vita",
89 target_os = "rtems",
90 target_vendor = "apple",
92 )))]
93 'poll: {
94 use crate::sys::os::errno;
95 let pfds: &mut [_] = &mut [
96 libc::pollfd { fd: 0, events: 0, revents: 0 },
97 libc::pollfd { fd: 1, events: 0, revents: 0 },
98 libc::pollfd { fd: 2, events: 0, revents: 0 },
99 ];
100
101 while libc::poll(pfds.as_mut_ptr(), 3, 0) == -1 {
102 match errno() {
103 libc::EINTR => continue,
104 #[cfg(target_vendor = "unikraft")]
105 libc::ENOSYS => {
106 break 'poll;
108 }
109 libc::EINVAL | libc::EAGAIN | libc::ENOMEM => {
110 break 'poll;
113 }
114 _ => libc::abort(),
115 }
116 }
117 for pfd in pfds {
118 if pfd.revents & libc::POLLNVAL == 0 {
119 continue;
120 }
121 open_devnull();
122 }
123 return;
124 }
125
126 #[cfg(not(any(
128 miri,
130 target_os = "emscripten",
131 target_os = "fuchsia",
132 target_os = "vxworks",
133 target_os = "l4re",
134 target_os = "horizon",
135 target_os = "vita",
136 )))]
137 {
138 use crate::sys::os::errno;
139 for fd in 0..3 {
140 if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
141 open_devnull();
142 }
143 }
144 }
145 }
146
147 unsafe fn reset_sigpipe(#[allow(unused_variables)] sigpipe: u8) {
148 #[cfg(not(any(
149 target_os = "emscripten",
150 target_os = "fuchsia",
151 target_os = "horizon",
152 target_os = "vxworks",
153 target_os = "vita",
154 target_vendor = "unikraft",
157 )))]
158 {
159 mod sigpipe {
166 pub const DEFAULT: u8 = 0;
167 pub const INHERIT: u8 = 1;
168 pub const SIG_IGN: u8 = 2;
169 pub const SIG_DFL: u8 = 3;
170 }
171
172 let (sigpipe_attr_specified, handler) = match sigpipe {
173 sigpipe::DEFAULT => (false, Some(libc::SIG_IGN)),
174 sigpipe::INHERIT => (true, None),
175 sigpipe::SIG_IGN => (true, Some(libc::SIG_IGN)),
176 sigpipe::SIG_DFL => (true, Some(libc::SIG_DFL)),
177 _ => unreachable!(),
178 };
179 if sigpipe_attr_specified {
180 ON_BROKEN_PIPE_FLAG_USED.store(true, crate::sync::atomic::Ordering::Relaxed);
181 }
182 if let Some(handler) = handler {
183 rtassert!(signal(libc::SIGPIPE, handler) != libc::SIG_ERR);
184 #[cfg(target_os = "hurd")]
185 {
186 rtassert!(signal(libc::SIGLOST, handler) != libc::SIG_ERR);
187 }
188 }
189 }
190 }
191}
192
193#[cfg(not(any(
195 target_os = "espidf",
196 target_os = "emscripten",
197 target_os = "fuchsia",
198 target_os = "horizon",
199 target_os = "vxworks",
200 target_os = "vita",
201)))]
202static ON_BROKEN_PIPE_FLAG_USED: crate::sync::atomic::Atomic<bool> =
203 crate::sync::atomic::AtomicBool::new(false);
204
205#[cfg(not(any(
206 target_os = "espidf",
207 target_os = "emscripten",
208 target_os = "fuchsia",
209 target_os = "horizon",
210 target_os = "vxworks",
211 target_os = "vita",
212 target_os = "nuttx",
213)))]
214pub(crate) fn on_broken_pipe_flag_used() -> bool {
215 ON_BROKEN_PIPE_FLAG_USED.load(crate::sync::atomic::Ordering::Relaxed)
216}
217
218pub unsafe fn cleanup() {
221 stack_overflow::cleanup();
222}
223
224#[allow(unused_imports)]
225pub use libc::signal;
226
227#[inline]
228pub(crate) fn is_interrupted(errno: i32) -> bool {
229 errno == libc::EINTR
230}
231
232pub fn decode_error_kind(errno: i32) -> io::ErrorKind {
233 use io::ErrorKind::*;
234 match errno as libc::c_int {
235 libc::E2BIG => ArgumentListTooLong,
236 libc::EADDRINUSE => AddrInUse,
237 libc::EADDRNOTAVAIL => AddrNotAvailable,
238 libc::EBUSY => ResourceBusy,
239 libc::ECONNABORTED => ConnectionAborted,
240 libc::ECONNREFUSED => ConnectionRefused,
241 libc::ECONNRESET => ConnectionReset,
242 libc::EDEADLK => Deadlock,
243 libc::EDQUOT => QuotaExceeded,
244 libc::EEXIST => AlreadyExists,
245 libc::EFBIG => FileTooLarge,
246 libc::EHOSTUNREACH => HostUnreachable,
247 libc::EINTR => Interrupted,
248 libc::EINVAL => InvalidInput,
249 libc::EISDIR => IsADirectory,
250 libc::ELOOP => FilesystemLoop,
251 libc::ENOENT => NotFound,
252 libc::ENOMEM => OutOfMemory,
253 libc::ENOSPC => StorageFull,
254 libc::ENOSYS => Unsupported,
255 libc::EMLINK => TooManyLinks,
256 libc::ENAMETOOLONG => InvalidFilename,
257 libc::ENETDOWN => NetworkDown,
258 libc::ENETUNREACH => NetworkUnreachable,
259 libc::ENOTCONN => NotConnected,
260 libc::ENOTDIR => NotADirectory,
261 #[cfg(not(target_os = "aix"))]
262 libc::ENOTEMPTY => DirectoryNotEmpty,
263 libc::EPIPE => BrokenPipe,
264 libc::EROFS => ReadOnlyFilesystem,
265 libc::ESPIPE => NotSeekable,
266 libc::ESTALE => StaleNetworkFileHandle,
267 libc::ETIMEDOUT => TimedOut,
268 libc::ETXTBSY => ExecutableFileBusy,
269 libc::EXDEV => CrossesDevices,
270 libc::EINPROGRESS => InProgress,
271 libc::EOPNOTSUPP => Unsupported,
272
273 libc::EACCES | libc::EPERM => PermissionDenied,
274
275 x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => WouldBlock,
279
280 _ => Uncategorized,
281 }
282}
283
284#[doc(hidden)]
285pub trait IsMinusOne {
286 fn is_minus_one(&self) -> bool;
287}
288
289macro_rules! impl_is_minus_one {
290 ($($t:ident)*) => ($(impl IsMinusOne for $t {
291 fn is_minus_one(&self) -> bool {
292 *self == -1
293 }
294 })*)
295}
296
297impl_is_minus_one! { i8 i16 i32 i64 isize }
298
299pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
302 if t.is_minus_one() { Err(io::Error::last_os_error()) } else { Ok(t) }
303}
304
305pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
307where
308 T: IsMinusOne,
309 F: FnMut() -> T,
310{
311 loop {
312 match cvt(f()) {
313 Err(ref e) if e.is_interrupted() => {}
314 other => return other,
315 }
316 }
317}
318
319#[allow(dead_code)] pub fn cvt_nz(error: libc::c_int) -> io::Result<()> {
322 if error == 0 { Ok(()) } else { Err(io::Error::from_raw_os_error(error)) }
323}
324
325#[cfg_attr(miri, track_caller)] pub fn abort_internal() -> ! {
362 unsafe { libc::abort() }
363}
364
365cfg_select! {
366 target_os = "android" => {
367 #[link(name = "dl", kind = "static", modifiers = "-bundle",
368 cfg(target_feature = "crt-static"))]
369 #[link(name = "dl", cfg(not(target_feature = "crt-static")))]
370 #[link(name = "log", cfg(not(target_feature = "crt-static")))]
371 unsafe extern "C" {}
372 }
373 target_os = "freebsd" => {
374 #[link(name = "execinfo")]
375 #[link(name = "pthread")]
376 unsafe extern "C" {}
377 }
378 target_os = "netbsd" => {
379 #[link(name = "execinfo")]
380 #[link(name = "pthread")]
381 #[link(name = "rt")]
382 unsafe extern "C" {}
383 }
384 any(target_os = "dragonfly", target_os = "openbsd", target_os = "cygwin") => {
385 #[link(name = "pthread")]
386 unsafe extern "C" {}
387 }
388 target_os = "solaris" => {
389 #[link(name = "socket")]
390 #[link(name = "posix4")]
391 #[link(name = "pthread")]
392 #[link(name = "resolv")]
393 unsafe extern "C" {}
394 }
395 target_os = "illumos" => {
396 #[link(name = "socket")]
397 #[link(name = "posix4")]
398 #[link(name = "pthread")]
399 #[link(name = "resolv")]
400 #[link(name = "nsl")]
401 #[link(name = "umem")]
403 unsafe extern "C" {}
404 }
405 target_vendor = "apple" => {
406 #[link(name = "System")]
411 unsafe extern "C" {}
412 }
413 target_os = "fuchsia" => {
414 #[link(name = "zircon")]
415 #[link(name = "fdio")]
416 unsafe extern "C" {}
417 }
418 all(target_os = "linux", target_env = "uclibc") => {
419 #[link(name = "dl")]
420 unsafe extern "C" {}
421 }
422 target_os = "vita" => {
423 #[link(name = "pthread", kind = "static", modifiers = "-bundle")]
424 unsafe extern "C" {}
425 }
426 _ => {}
427}
428
429#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita", target_os = "nuttx"))]
430pub mod unsupported {
431 use crate::io;
432
433 pub fn unsupported<T>() -> io::Result<T> {
434 Err(unsupported_err())
435 }
436
437 pub fn unsupported_err() -> io::Error {
438 io::Error::UNSUPPORTED_PLATFORM
439 }
440}