Debian Codegraph @

debian/rust-vendor/rustix/src/lib.rs

raw
1 //! `rustix` provides efficient memory-safe and [I/O-safe] wrappers to
2 //! POSIX-like, Unix-like, Linux, and Winsock syscall-like APIs, with
3 //! configurable backends.
4 //!
5 //! With rustix, you can write code like this:
6 //!
7 //! ```
8 //! # #[cfg(feature = "net")]
9 //! # fn read(sock: std::net::TcpStream, buf: &mut [u8]) -> std::io::Result<()> {
10 //! # use rustix::net::RecvFlags;
11 //! let (nread, _received) = rustix::net::recv(&sock, buf, RecvFlags::PEEK)?;
12 //! # let _ = nread;
13 //! # Ok(())
14 //! # }
15 //! ```
16 //!
17 //! instead of like this:
18 //!
19 //! ```
20 //! # #[cfg(feature = "net")]
21 //! # fn read(sock: std::net::TcpStream, buf: &mut [u8]) -> std::io::Result<()> {
22 //! # #[cfg(unix)]
23 //! # use std::os::unix::io::AsRawFd;
24 //! # #[cfg(target_os = "wasi")]
25 //! # use std::os::wasi::io::AsRawFd;
26 //! # #[cfg(windows)]
27 //! # use windows_sys::Win32::Networking::WinSock as libc;
28 //! # #[cfg(windows)]
29 //! # use std::os::windows::io::AsRawSocket;
30 //! # const MSG_PEEK: i32 = libc::MSG_PEEK;
31 //! let nread = unsafe {
32 //! #[cfg(any(unix, target_os = "wasi"))]
33 //! let raw = sock.as_raw_fd();
34 //! #[cfg(windows)]
35 //! let raw = sock.as_raw_socket();
36 //! match libc::recv(
37 //! raw as _,
38 //! buf.as_mut_ptr().cast(),
39 //! buf.len().try_into().unwrap_or(i32::MAX as _),
40 //! MSG_PEEK,
41 //! ) {
42 //! -1 => return Err(std::io::Error::last_os_error()),
43 //! nread => nread as usize,
44 //! }
45 //! };
46 //! # let _ = nread;
47 //! # Ok(())
48 //! # }
49 //! ```
50 //!
51 //! rustix's APIs perform the following tasks:
52 //! - Error values are translated to [`Result`]s.
53 //! - Buffers are passed as Rust slices.
54 //! - Out-parameters are presented as return values.
55 //! - Path arguments use [`Arg`], so they accept any string type.
56 //! - File descriptors are passed and returned via [`AsFd`] and [`OwnedFd`]
57 //! instead of bare integers, ensuring I/O safety.
58 //! - Constants use `enum`s and [`bitflags`] types, and enable [support for
59 //! externally defined flags].
60 //! - Multiplexed functions (eg. `fcntl`, `ioctl`, etc.) are de-multiplexed.
61 //! - Variadic functions (eg. `openat`, etc.) are presented as non-variadic.
62 //! - Functions that return strings automatically allocate sufficient memory
63 //! and retry the syscall as needed to determine the needed length.
64 //! - Functions and types which need `l` prefixes or `64` suffixes to enable
65 //! large-file support (LFS) are used automatically. File sizes and offsets
66 //! are always presented as `u64` and `i64`.
67 //! - Behaviors that depend on the sizes of C types like `long` are hidden.
68 //! - In some places, more human-friendly and less historical-accident names
69 //! are used (and documentation aliases are used so that the original names
70 //! can still be searched for).
71 //! - Provide y2038 compatibility, on platforms which support this.
72 //! - Correct selected platform bugs, such as behavioral differences when
73 //! running under seccomp.
74 //! - Use `timespec` for timestamps and timeouts instead of `timeval` and
75 //! `c_int` milliseconds.
76 //!
77 //! Things they don't do include:
78 //! - Detecting whether functions are supported at runtime, except in specific
79 //! cases where new interfaces need to be detected to support y2038 and LFS.
80 //! - Hiding significant differences between platforms.
81 //! - Restricting ambient authorities.
82 //! - Imposing sandboxing features such as filesystem path or network address
83 //! sandboxing.
84 //!
85 //! See [`cap-std`], [`system-interface`], and [`io-streams`] for libraries
86 //! which do hide significant differences between platforms, and [`cap-std`]
87 //! which does perform sandboxing and restricts ambient authorities.
88 //!
89 //! [`cap-std`]: https://crates.io/crates/cap-std
90 //! [`system-interface`]: https://crates.io/crates/system-interface
91 //! [`io-streams`]: https://crates.io/crates/io-streams
92 //! [`bitflags`]: bitflags
93 //! [`AsFd`]: crate::fd::AsFd
94 //! [`OwnedFd`]: crate::fd::OwnedFd
95 //! [I/O-safe]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md
96 //! [`Arg`]: path::Arg
97 //! [support for externally defined flags]: bitflags#externally-defined-flags
98
99 #![deny(missing_docs)]
100 #![allow(stable_features)]
101 #![cfg_attr(linux_raw, deny(unsafe_code))]
102 #![cfg_attr(rustc_attrs, feature(rustc_attrs))]
103 #![cfg_attr(docsrs, feature(doc_cfg))]
104 #![cfg_attr(all(wasi_ext, target_os = "wasi", feature = "std"), feature(wasi_ext))]
105 #![cfg_attr(core_ffi_c, feature(core_ffi_c))]
106 #![cfg_attr(core_c_str, feature(core_c_str))]
107 #![cfg_attr(error_in_core, feature(error_in_core))]
108 #![cfg_attr(all(feature = "alloc", alloc_c_string), feature(alloc_c_string))]
109 #![cfg_attr(all(feature = "alloc", alloc_ffi), feature(alloc_ffi))]
110 #![cfg_attr(not(feature = "std"), no_std)]
111 #![cfg_attr(feature = "rustc-dep-of-std", feature(ip))]
112 #![cfg_attr(feature = "rustc-dep-of-std", allow(internal_features))]
113 #![cfg_attr(
114 any(feature = "rustc-dep-of-std", core_intrinsics),
115 feature(core_intrinsics)
116 )]
117 #![cfg_attr(asm_experimental_arch, feature(asm_experimental_arch))]
118 #![cfg_attr(not(feature = "all-apis"), allow(dead_code))]
119 // It is common in Linux and libc APIs for types to vary between platforms.
120 #![allow(clippy::unnecessary_cast)]
121 // It is common in Linux and libc APIs for types to vary between platforms.
122 #![allow(clippy::useless_conversion)]
123 // This clippy lint gets too many false positives.
124 #![allow(clippy::needless_lifetimes)]
125 // Until `unnecessary_transmutes` is recognized by our MSRV, don't warn about
126 // it being unrecognized.
127 #![allow(unknown_lints)]
128 // Until `cast_signed` and `cast_unsigned` are supported by our MSRV, don't
129 // warn about transmutes that could be changed to them.
130 #![allow(unnecessary_transmutes)]
131 // Redox and WASI have enough differences that it isn't worth precisely
132 // conditionalizing all the `use`s for them. Similar for if we don't have
133 // "all-apis".
134 #![cfg_attr(
135 any(target_os = "redox", target_os = "wasi", not(feature = "all-apis")),
136 allow(unused_imports)
137 )]
138 // wasip2 conditionally gates stdlib APIs such as `OsStrExt`.
139 // <https://github.com/rust-lang/rust/issues/130323>
140 #![cfg_attr(
141 all(
142 target_os = "wasi",
143 target_env = "p2",
144 any(feature = "fs", feature = "mount", feature = "net"),
145 wasip2,
146 ),
147 feature(wasip2)
148 )]
149
150 #[cfg(all(feature = "alloc", feature = "rustc-dep-of-std"))]
151 extern crate rustc_std_workspace_alloc as alloc;
152
153 #[cfg(all(feature = "alloc", not(feature = "rustc-dep-of-std")))]
154 extern crate alloc;
155
156 // Use `static_assertions` macros if we have them, or a polyfill otherwise.
157 #[cfg(all(test, static_assertions))]
158 #[macro_use]
159 #[allow(unused_imports)]
160 extern crate static_assertions;
161 #[cfg(all(test, not(static_assertions)))]
162 #[macro_use]
163 #[allow(unused_imports)]
164 mod static_assertions;
165
166 pub mod buffer;
167 #[cfg(not(windows))]
168 #[macro_use]
169 pub(crate) mod cstr;
170 #[macro_use]
171 pub(crate) mod utils;
172 // Polyfill for `std` in `no_std` builds.
173 #[cfg_attr(feature = "std", path = "maybe_polyfill/std/mod.rs")]
174 #[cfg_attr(not(feature = "std"), path = "maybe_polyfill/no_std/mod.rs")]
175 pub(crate) mod maybe_polyfill;
176 #[cfg(test)]
177 #[macro_use]
178 pub(crate) mod check_types;
179 #[macro_use]
180 pub(crate) mod bitcast;
181 #[cfg(sanitize_memory)]
182 pub(crate) mod msan;
183
184 // linux_raw: Weak symbols are used by the use-libc-auxv feature for
185 // glibc 2.15 support.
186 //
187 // libc: Weak symbols are used to call various functions available in some
188 // versions of libc and not others.
189 #[cfg(any(
190 all(linux_raw, feature = "use-libc-auxv"),
191 all(libc, not(any(windows, target_os = "espidf", target_os = "wasi")))
192 ))]
193 #[macro_use]
194 mod weak;
195
196 // Pick the backend implementation to use.
197 #[cfg_attr(libc, path = "backend/libc/mod.rs")]
198 #[cfg_attr(linux_raw, path = "backend/linux_raw/mod.rs")]
199 mod backend;
200
201 /// Export the `*Fd` types and traits that are used in rustix's public API.
202 ///
203 /// This module exports the types and traits from [`std::os::fd`], or polyills
204 /// on Rust < 1.66 or on Windows.
205 ///
206 /// On Windows, the polyfill consists of aliases of the socket types and
207 /// traits, For example, [`OwnedSocket`] is aliased to `OwnedFd`, and so on,
208 /// and there are blanket impls for `AsFd` etc. that map to `AsSocket` impls.
209 /// These blanket impls suffice for using the traits, however not for
210 /// implementing them, so this module also exports `AsSocket` and the other
211 /// traits as-is so that users can implement them if needed.
212 ///
213 /// [`OwnedSocket`]: https://doc.rust-lang.org/stable/std/os/windows/io/struct.OwnedSocket.html
214 pub mod fd {
215 pub use super::backend::fd::*;
216 }
217
218 // The public API modules.
219 #[cfg(feature = "event")]
220 #[cfg_attr(docsrs, doc(cfg(feature = "event")))]
221 pub mod event;
222 pub mod ffi;
223 #[cfg(not(windows))]
224 #[cfg(feature = "fs")]
225 #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
226 pub mod fs;
227 pub mod io;
228 #[cfg(all(linux_kernel, not(target_os = "android")))]
229 #[cfg(feature = "io_uring")]
230 #[cfg_attr(docsrs, doc(cfg(feature = "io_uring")))]
231 pub mod io_uring;
232 pub mod ioctl;
233 #[cfg(not(any(
234 windows,
235 target_os = "espidf",
236 target_os = "horizon",
237 target_os = "vita",
238 target_os = "wasi"
239 )))]
240 #[cfg(feature = "mm")]
241 #[cfg_attr(docsrs, doc(cfg(feature = "mm")))]
242 pub mod mm;
243 #[cfg(linux_kernel)]
244 #[cfg(feature = "mount")]
245 #[cfg_attr(docsrs, doc(cfg(feature = "mount")))]
246 pub mod mount;
247 #[cfg(not(target_os = "wasi"))]
248 #[cfg(feature = "net")]
249 #[cfg_attr(docsrs, doc(cfg(feature = "net")))]
250 pub mod net;
251 #[cfg(not(any(windows, target_os = "espidf")))]
252 #[cfg(feature = "param")]
253 #[cfg_attr(docsrs, doc(cfg(feature = "param")))]
254 pub mod param;
255 #[cfg(not(windows))]
256 #[cfg(any(feature = "fs", feature = "mount", feature = "net"))]
257 #[cfg_attr(
258 docsrs,
259 doc(cfg(any(feature = "fs", feature = "mount", feature = "net")))
260 )]
261 pub mod path;
262 #[cfg(feature = "pipe")]
263 #[cfg_attr(docsrs, doc(cfg(feature = "pipe")))]
264 #[cfg(not(any(windows, target_os = "wasi")))]
265 pub mod pipe;
266 #[cfg(not(windows))]
267 #[cfg(feature = "process")]
268 #[cfg_attr(docsrs, doc(cfg(feature = "process")))]
269 pub mod process;
270 #[cfg(not(windows))]
271 #[cfg(not(target_os = "wasi"))]
272 #[cfg(feature = "pty")]
273 #[cfg_attr(docsrs, doc(cfg(feature = "pty")))]
274 pub mod pty;
275 #[cfg(not(windows))]
276 #[cfg(feature = "rand")]
277 #[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
278 pub mod rand;
279 #[cfg(not(any(
280 windows,
281 target_os = "android",
282 target_os = "espidf",
283 target_os = "horizon",
284 target_os = "vita",
285 target_os = "wasi"
286 )))]
287 #[cfg(feature = "shm")]
288 #[cfg_attr(docsrs, doc(cfg(feature = "shm")))]
289 pub mod shm;
290 #[cfg(not(windows))]
291 #[cfg(feature = "stdio")]
292 #[cfg_attr(docsrs, doc(cfg(feature = "stdio")))]
293 pub mod stdio;
294 #[cfg(feature = "system")]
295 #[cfg(not(any(windows, target_os = "wasi")))]
296 #[cfg_attr(docsrs, doc(cfg(feature = "system")))]
297 pub mod system;
298 #[cfg(not(any(windows, target_os = "horizon", target_os = "vita")))]
299 #[cfg(feature = "termios")]
300 #[cfg_attr(docsrs, doc(cfg(feature = "termios")))]
301 pub mod termios;
302 #[cfg(not(windows))]
303 #[cfg(feature = "thread")]
304 #[cfg_attr(docsrs, doc(cfg(feature = "thread")))]
305 pub mod thread;
306 #[cfg(not(any(windows, target_os = "espidf")))]
307 #[cfg(feature = "time")]
308 #[cfg_attr(docsrs, doc(cfg(feature = "time")))]
309 pub mod time;
310
311 // "runtime" is also a public API module, but it's only for libc-like users.
312 #[cfg(not(windows))]
313 #[cfg(feature = "runtime")]
314 #[cfg(linux_raw)]
315 #[cfg_attr(not(document_experimental_runtime_api), doc(hidden))]
316 #[cfg_attr(docsrs, doc(cfg(feature = "runtime")))]
317 pub mod runtime;
318
319 // Declare "fs" as a non-public module if "fs" isn't enabled but we need it for
320 // reading procfs.
321 #[cfg(not(windows))]
322 #[cfg(not(feature = "fs"))]
323 #[cfg(all(
324 linux_raw,
325 not(feature = "use-libc-auxv"),
326 not(feature = "use-explicitly-provided-auxv"),
327 any(
328 feature = "param",
329 feature = "runtime",
330 feature = "thread",
331 feature = "time",
332 target_arch = "x86",
333 )
334 ))]
335 #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
336 pub(crate) mod fs;
337
338 // Similarly, declare `path` as a non-public module if needed.
339 #[cfg(not(windows))]
340 #[cfg(not(any(feature = "fs", feature = "mount", feature = "net")))]
341 #[cfg(all(
342 linux_raw,
343 not(feature = "use-libc-auxv"),
344 not(feature = "use-explicitly-provided-auxv"),
345 any(
346 feature = "param",
347 feature = "runtime",
348 feature = "thread",
349 feature = "time",
350 target_arch = "x86",
351 )
352 ))]
353 pub(crate) mod path;
354
355 // Private modules used by multiple public modules.
356 #[cfg(not(any(windows, target_os = "espidf")))]
357 #[cfg(any(feature = "thread", feature = "time"))]
358 mod clockid;
359 #[cfg(linux_kernel)]
360 #[cfg(any(feature = "io_uring", feature = "runtime"))]
361 mod kernel_sigset;
362 #[cfg(not(any(windows, target_os = "wasi")))]
363 #[cfg(any(
364 feature = "process",
365 feature = "runtime",
366 feature = "termios",
367 feature = "thread",
368 all(bsd, feature = "event"),
369 all(linux_kernel, feature = "net")
370 ))]
371 mod pid;
372 #[cfg(any(feature = "process", feature = "thread"))]
373 #[cfg(linux_kernel)]
374 mod prctl;
375 #[cfg(not(any(windows, target_os = "espidf", target_os = "wasi")))]
376 #[cfg(any(
377 feature = "io_uring",
378 feature = "process",
379 feature = "runtime",
380 all(bsd, feature = "event")
381 ))]
382 mod signal;
383 #[cfg(any(
384 feature = "fs",
385 feature = "event",
386 feature = "process",
387 feature = "runtime",
388 feature = "thread",
389 feature = "time",
390 all(feature = "event", any(bsd, linux_kernel, windows, target_os = "wasi")),
391 all(
392 linux_raw,
393 not(feature = "use-libc-auxv"),
394 not(feature = "use-explicitly-provided-auxv"),
395 any(
396 feature = "param",
397 feature = "process",
398 feature = "runtime",
399 feature = "time",
400 target_arch = "x86",
401 )
402 )
403 ))]
404 mod timespec;
405 #[cfg(not(any(windows, target_os = "wasi")))]
406 #[cfg(any(
407 feature = "fs",
408 feature = "process",
409 feature = "thread",
410 all(
411 linux_raw,
412 not(feature = "use-libc-auxv"),
413 not(feature = "use-explicitly-provided-auxv"),
414 any(
415 feature = "param",
416 feature = "runtime",
417 feature = "time",
418 target_arch = "x86",
419 )
420 ),
421 all(linux_kernel, feature = "net")
422 ))]
423 mod ugid;
424
425 #[cfg(doc)]
426 #[cfg_attr(docsrs, doc(cfg(doc)))]
427 pub mod not_implemented;