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