1 //! The libc backend.
2 //!
3 //! On most platforms, this uses the `libc` crate to make system calls. On
4 //! Windows, this uses the Winsock2 API in `windows-sys`, which can be adapted
5 //! to have a very `libc`-like interface.
6
7 // Every FFI call requires an unsafe block, and there are a lot of FFI
8 // calls. For now, set this to allow for the libc backend.
9 #![allow(clippy::undocumented_unsafe_blocks)]
10 // Lots of libc types vary between platforms, so we often need a `.into()` on
11 // one platform where it's redundant on another.
12 #![allow(clippy::useless_conversion)]
13
14 #[cfg(not(any(windows, target_os = "wasi")))]
15 #[macro_use]
16 mod weak;
17
18 mod conv;
19 mod offset;
20
21 #[cfg(windows)]
22 mod io_lifetimes;
23 #[cfg(not(windows))]
24 #[cfg(not(feature = "std"))]
25 pub(crate) mod fd {
26 pub(crate) use super::c::c_int as LibcFd;
27 pub use crate::io::fd::*;
28 }
29 #[cfg(windows)]
30 pub(crate) mod fd {
31 pub use super::io_lifetimes::*;
32 }
33 #[cfg(not(windows))]
34 #[cfg(feature = "std")]
35 pub(crate) mod fd {
36 pub use io_lifetimes::*;
37
38 #[cfg(target_os = "wasi")]
39 #[allow(unused_imports)]
40 pub(crate) use super::c::c_int as LibcFd;
41 #[cfg(unix)]
42 #[allow(unused_imports)]
43 pub(crate) use std::os::unix::io::RawFd as LibcFd;
44 #[cfg(unix)]
45 pub use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
46 #[cfg(target_os = "wasi")]
47 pub use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
48 }
49
50 // On Windows we emulate selected libc-compatible interfaces. On non-Windows,
51 // we just use libc here, since this is the libc backend.
52 #[cfg(windows)]
53 #[path = "winsock_c.rs"]
54 pub(crate) mod c;
55 #[cfg(not(windows))]
56 pub(crate) use libc as c;
57
58 #[cfg(not(windows))]
59 #[cfg(feature = "fs")]
60 pub(crate) mod fs;
61 pub(crate) mod io;
62 #[cfg(any(target_os = "android", target_os = "linux"))]
63 #[cfg(feature = "io_uring")]
64 pub(crate) mod io_uring;
65 #[cfg(not(any(windows, target_os = "wasi")))]
66 #[cfg(feature = "mm")]
67 pub(crate) mod mm;
68 #[cfg(not(any(target_os = "redox", target_os = "wasi")))]
69 #[cfg(feature = "net")]
70 pub(crate) mod net;
71 #[cfg(not(windows))]
72 #[cfg(any(
73 feature = "param",
74 feature = "runtime",
75 feature = "time",
76 target_arch = "x86",
77 ))]
78 pub(crate) mod param;
79 #[cfg(not(windows))]
80 pub(crate) mod process;
81 #[cfg(not(windows))]
82 #[cfg(feature = "rand")]
83 pub(crate) mod rand;
84 #[cfg(not(windows))]
85 #[cfg(feature = "termios")]
86 pub(crate) mod termios;
87 #[cfg(not(windows))]
88 #[cfg(feature = "thread")]
89 pub(crate) mod thread;
90 #[cfg(not(windows))]
91 pub(crate) mod time;
92
93 /// If the host libc is glibc, return `true` if it is less than version 2.25.
94 ///
95 /// To restate and clarify, this function returning true does not mean the libc
96 /// is glibc just that if it is glibc, it is less than version 2.25.
97 ///
98 /// For now, this function is only available on Linux, but if it ends up being
99 /// used beyond that, this could be changed to e.g. `#[cfg(unix)]`.
100 #[cfg(all(unix, target_env = "gnu"))]
if_glibc_is_less_than_2_25() -> bool101 pub(crate) fn if_glibc_is_less_than_2_25() -> bool {
102 // This is also defined inside `weak_or_syscall!` in
103 // backend/libc/rand/syscalls.rs, but it's not convenient to re-export the weak
104 // symbol from that macro, so we duplicate it at a small cost here.
105 weak! { fn getrandom(*mut c::c_void, c::size_t, c::c_uint) -> c::ssize_t }
106
107 // glibc 2.25 has `getrandom`, which is how we satisfy the API contract of
108 // this function. But, there are likely other libc versions which have it.
109 getrandom.get().is_none()
110 }
111