]>
Commit | Line | Data |
---|---|---|
064997fb FG |
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")] // TODO: Enable this once `OwnedFd` moves out of the tree. | |
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 | #[cfg_attr(doc_cfg, doc(cfg(feature = "io_uring")))] | |
65 | pub(crate) mod io_uring; | |
66 | #[cfg(not(any(windows, target_os = "wasi")))] | |
67 | #[cfg(any(feature = "mm", feature = "time", target_arch = "x86"))] // vdso.rs uses `madvise` | |
68 | pub(crate) mod mm; | |
69 | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] | |
70 | #[cfg(feature = "net")] | |
71 | pub(crate) mod net; | |
72 | #[cfg(not(windows))] | |
73 | #[cfg(any( | |
74 | feature = "param", | |
75 | feature = "runtime", | |
76 | feature = "time", | |
77 | target_arch = "x86", | |
78 | ))] | |
79 | pub(crate) mod param; | |
80 | #[cfg(not(windows))] | |
81 | pub(crate) mod process; | |
82 | #[cfg(not(windows))] | |
83 | #[cfg(feature = "rand")] | |
84 | pub(crate) mod rand; | |
85 | #[cfg(not(any(windows, target_os = "wasi")))] | |
86 | #[cfg(feature = "termios")] | |
87 | pub(crate) mod termios; | |
88 | #[cfg(not(windows))] | |
89 | #[cfg(feature = "thread")] | |
90 | pub(crate) mod thread; | |
91 | #[cfg(not(windows))] | |
92 | pub(crate) mod time; | |
93 | ||
94 | /// If the host libc is glibc, return `true` if it is less than version 2.25. | |
95 | /// | |
96 | /// To restate and clarify, this function returning true does not mean the libc | |
97 | /// is glibc just that if it is glibc, it is less than version 2.25. | |
98 | /// | |
99 | /// For now, this function is only available on Linux, but if it ends up being | |
100 | /// used beyond that, this could be changed to e.g. `#[cfg(unix)]`. | |
101 | #[cfg(all(unix, target_env = "gnu"))] | |
102 | pub(crate) fn if_glibc_is_less_than_2_25() -> bool { | |
103 | // This is also defined inside `weak_or_syscall!` in | |
104 | // imp/libc/rand/syscalls.rs, but it's not convenient to re-export the weak | |
105 | // symbol from that macro, so we duplicate it at a small cost here. | |
106 | weak! { fn getrandom(*mut c::c_void, c::size_t, c::c_uint) -> c::ssize_t } | |
107 | ||
108 | // glibc 2.25 has `getrandom`, which is how we satisfy the API contract of | |
109 | // this function. But, there are likely other libc versions which have it. | |
110 | getrandom.get().is_none() | |
111 | } |