1 //! The libc backend.
2 //!
3 //! On most platforms, this uses the `libc` crate to make system calls. On
4 //! Windows, this uses the Winsock 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 mod conv;
15 
16 #[cfg(windows)]
17 pub(crate) mod fd {
18     pub use crate::maybe_polyfill::os::windows::io::{
19         AsRawSocket, AsSocket, BorrowedSocket as BorrowedFd, FromRawSocket, IntoRawSocket,
20         OwnedSocket as OwnedFd, RawSocket as RawFd,
21     };
22     pub(crate) use windows_sys::Win32::Networking::WinSock::SOCKET as LibcFd;
23 
24     /// A version of [`AsRawFd`] for use with Winsock API.
25     ///
26     /// [`AsRawFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.AsRawFd.html
27     pub trait AsRawFd {
28         /// A version of [`as_raw_fd`] for use with Winsock API.
29         ///
30         /// [`as_raw_fd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.as_raw_fd
as_raw_fd(&self) -> RawFd31         fn as_raw_fd(&self) -> RawFd;
32     }
33     impl<T: AsRawSocket> AsRawFd for T {
34         #[inline]
as_raw_fd(&self) -> RawFd35         fn as_raw_fd(&self) -> RawFd {
36             self.as_raw_socket()
37         }
38     }
39 
40     /// A version of [`IntoRawFd`] for use with Winsock API.
41     ///
42     /// [`IntoRawFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.IntoRawFd.html
43     pub trait IntoRawFd {
44         /// A version of [`into_raw_fd`] for use with Winsock API.
45         ///
46         /// [`into_raw_fd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.into_raw_fd
into_raw_fd(self) -> RawFd47         fn into_raw_fd(self) -> RawFd;
48     }
49     impl<T: IntoRawSocket> IntoRawFd for T {
50         #[inline]
into_raw_fd(self) -> RawFd51         fn into_raw_fd(self) -> RawFd {
52             self.into_raw_socket()
53         }
54     }
55 
56     /// A version of [`FromRawFd`] for use with Winsock API.
57     ///
58     /// [`FromRawFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html
59     pub trait FromRawFd {
60         /// A version of [`from_raw_fd`] for use with Winsock API.
61         ///
62         /// # Safety
63         ///
64         /// See the [safety requirements] for [`from_raw_fd`].
65         ///
66         /// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.from_raw_fd
67         /// [safety requirements]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#safety
from_raw_fd(raw_fd: RawFd) -> Self68         unsafe fn from_raw_fd(raw_fd: RawFd) -> Self;
69     }
70     impl<T: FromRawSocket> FromRawFd for T {
71         #[inline]
from_raw_fd(raw_fd: RawFd) -> Self72         unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
73             Self::from_raw_socket(raw_fd)
74         }
75     }
76 
77     /// A version of [`AsFd`] for use with Winsock API.
78     ///
79     /// [`AsFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.AsFd.html
80     pub trait AsFd {
81         /// An `as_fd` function for Winsock, where a `Fd` is a `Socket`.
as_fd(&self) -> BorrowedFd82         fn as_fd(&self) -> BorrowedFd;
83     }
84     impl<T: AsSocket> AsFd for T {
85         #[inline]
as_fd(&self) -> BorrowedFd86         fn as_fd(&self) -> BorrowedFd {
87             self.as_socket()
88         }
89     }
90 }
91 #[cfg(not(windows))]
92 pub(crate) mod fd {
93     pub use crate::maybe_polyfill::os::fd::{
94         AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd,
95     };
96     #[allow(unused_imports)]
97     pub(crate) use RawFd as LibcFd;
98 }
99 
100 // On Windows we emulate selected libc-compatible interfaces. On non-Windows,
101 // we just use libc here, since this is the libc backend.
102 #[cfg_attr(windows, path = "winsock_c.rs")]
103 pub(crate) mod c;
104 
105 #[cfg(feature = "event")]
106 pub(crate) mod event;
107 #[cfg(not(windows))]
108 #[cfg(feature = "fs")]
109 pub(crate) mod fs;
110 pub(crate) mod io;
111 #[cfg(linux_kernel)]
112 #[cfg(feature = "io_uring")]
113 pub(crate) mod io_uring;
114 #[cfg(not(any(windows, target_os = "espidf", target_os = "vita", target_os = "wasi")))]
115 #[cfg(feature = "mm")]
116 pub(crate) mod mm;
117 #[cfg(linux_kernel)]
118 #[cfg(feature = "mount")]
119 pub(crate) mod mount;
120 #[cfg(linux_kernel)]
121 #[cfg(all(feature = "fs", not(feature = "mount")))]
122 pub(crate) mod mount; // for deprecated mount functions in "fs"
123 #[cfg(not(any(target_os = "redox", target_os = "wasi")))]
124 #[cfg(feature = "net")]
125 pub(crate) mod net;
126 #[cfg(not(any(windows, target_os = "espidf")))]
127 #[cfg(any(
128     feature = "param",
129     feature = "runtime",
130     feature = "time",
131     target_arch = "x86",
132 ))]
133 pub(crate) mod param;
134 #[cfg(not(windows))]
135 #[cfg(feature = "pipe")]
136 pub(crate) mod pipe;
137 #[cfg(not(windows))]
138 #[cfg(feature = "process")]
139 pub(crate) mod process;
140 #[cfg(not(windows))]
141 #[cfg(not(target_os = "wasi"))]
142 #[cfg(feature = "pty")]
143 pub(crate) mod pty;
144 #[cfg(not(windows))]
145 #[cfg(feature = "rand")]
146 pub(crate) mod rand;
147 #[cfg(not(windows))]
148 #[cfg(not(target_os = "wasi"))]
149 #[cfg(feature = "system")]
150 pub(crate) mod system;
151 #[cfg(not(any(windows, target_os = "vita")))]
152 #[cfg(feature = "termios")]
153 pub(crate) mod termios;
154 #[cfg(not(windows))]
155 #[cfg(feature = "thread")]
156 pub(crate) mod thread;
157 #[cfg(not(any(windows, target_os = "espidf")))]
158 #[cfg(feature = "time")]
159 pub(crate) mod time;
160 
161 /// If the host libc is glibc, return `true` if it is less than version 2.25.
162 ///
163 /// To restate and clarify, this function returning true does not mean the libc
164 /// is glibc just that if it is glibc, it is less than version 2.25.
165 ///
166 /// For now, this function is only available on Linux, but if it ends up being
167 /// used beyond that, this could be changed to e.g. `#[cfg(unix)]`.
168 #[cfg(all(unix, target_env = "gnu"))]
if_glibc_is_less_than_2_25() -> bool169 pub(crate) fn if_glibc_is_less_than_2_25() -> bool {
170     // This is also defined inside `weak_or_syscall!` in
171     // backend/libc/rand/syscalls.rs, but it's not convenient to re-export the
172     // weak symbol from that macro, so we duplicate it at a small cost here.
173     weak! { fn getrandom(*mut c::c_void, c::size_t, c::c_uint) -> c::ssize_t }
174 
175     // glibc 2.25 has `getrandom`, which is how we satisfy the API contract of
176     // this function. But, there are likely other libc versions which have it.
177     getrandom.get().is_none()
178 }
179 
180 // Private modules used by multiple public modules.
181 #[cfg(any(feature = "procfs", feature = "process", feature = "runtime"))]
182 #[cfg(not(any(windows, target_os = "wasi")))]
183 pub(crate) mod pid;
184 #[cfg(any(feature = "process", feature = "thread"))]
185 #[cfg(linux_kernel)]
186 pub(crate) mod prctl;
187 #[cfg(not(any(
188     windows,
189     target_os = "android",
190     target_os = "espidf",
191     target_os = "vita",
192     target_os = "wasi"
193 )))]
194 #[cfg(feature = "shm")]
195 pub(crate) mod shm;
196 #[cfg(any(feature = "fs", feature = "thread", feature = "process"))]
197 #[cfg(not(any(windows, target_os = "wasi")))]
198 pub(crate) mod ugid;
199 
200 #[cfg(bsd)]
201 const MAX_IOV: usize = c::IOV_MAX as usize;
202 
203 #[cfg(any(linux_kernel, target_os = "emscripten", target_os = "nto"))]
204 const MAX_IOV: usize = c::UIO_MAXIOV as usize;
205 
206 #[cfg(not(any(
207     bsd,
208     linux_kernel,
209     windows,
210     target_os = "emscripten",
211     target_os = "espidf",
212     target_os = "nto",
213     target_os = "horizon",
214 )))]
215 const MAX_IOV: usize = 16; // The minimum value required by POSIX.
216