1b8a62b91Sopenharmony_ci//! `rustix` provides efficient memory-safe and [I/O-safe] wrappers to 2b8a62b91Sopenharmony_ci//! POSIX-like, Unix-like, Linux, and Winsock2 syscall-like APIs, with 3b8a62b91Sopenharmony_ci//! configurable backends. 4b8a62b91Sopenharmony_ci//! 5b8a62b91Sopenharmony_ci//! With rustix, you can write code like this: 6b8a62b91Sopenharmony_ci//! 7b8a62b91Sopenharmony_ci//! ```rust 8b8a62b91Sopenharmony_ci//! # #[cfg(feature = "net")] 9b8a62b91Sopenharmony_ci//! # fn read(sock: std::net::TcpStream, buf: &mut [u8]) -> std::io::Result<()> { 10b8a62b91Sopenharmony_ci//! # use rustix::net::RecvFlags; 11b8a62b91Sopenharmony_ci//! let nread: usize = rustix::net::recv(&sock, buf, RecvFlags::PEEK)?; 12b8a62b91Sopenharmony_ci//! # let _ = nread; 13b8a62b91Sopenharmony_ci//! # Ok(()) 14b8a62b91Sopenharmony_ci//! # } 15b8a62b91Sopenharmony_ci//! ``` 16b8a62b91Sopenharmony_ci//! 17b8a62b91Sopenharmony_ci//! instead of like this: 18b8a62b91Sopenharmony_ci//! 19b8a62b91Sopenharmony_ci//! ```rust 20b8a62b91Sopenharmony_ci//! # #[cfg(feature = "net")] 21b8a62b91Sopenharmony_ci//! # fn read(sock: std::net::TcpStream, buf: &mut [u8]) -> std::io::Result<()> { 22b8a62b91Sopenharmony_ci//! # use std::convert::TryInto; 23b8a62b91Sopenharmony_ci//! # #[cfg(unix)] 24b8a62b91Sopenharmony_ci//! # use std::os::unix::io::AsRawFd; 25b8a62b91Sopenharmony_ci//! # #[cfg(target_os = "wasi")] 26b8a62b91Sopenharmony_ci//! # use std::os::wasi::io::AsRawFd; 27b8a62b91Sopenharmony_ci//! # #[cfg(windows)] 28b8a62b91Sopenharmony_ci//! # use windows_sys::Win32::Networking::WinSock as libc; 29b8a62b91Sopenharmony_ci//! # #[cfg(windows)] 30b8a62b91Sopenharmony_ci//! # use std::os::windows::io::AsRawSocket; 31b8a62b91Sopenharmony_ci//! # const MSG_PEEK: i32 = libc::MSG_PEEK; 32b8a62b91Sopenharmony_ci//! let nread: usize = unsafe { 33b8a62b91Sopenharmony_ci//! #[cfg(any(unix, target_os = "wasi"))] 34b8a62b91Sopenharmony_ci//! let raw = sock.as_raw_fd(); 35b8a62b91Sopenharmony_ci//! #[cfg(windows)] 36b8a62b91Sopenharmony_ci//! let raw = sock.as_raw_socket(); 37b8a62b91Sopenharmony_ci//! match libc::recv( 38b8a62b91Sopenharmony_ci//! raw as _, 39b8a62b91Sopenharmony_ci//! buf.as_mut_ptr().cast(), 40b8a62b91Sopenharmony_ci//! buf.len().try_into().unwrap_or(i32::MAX as _), 41b8a62b91Sopenharmony_ci//! MSG_PEEK, 42b8a62b91Sopenharmony_ci//! ) { 43b8a62b91Sopenharmony_ci//! -1 => return Err(std::io::Error::last_os_error()), 44b8a62b91Sopenharmony_ci//! nread => nread as usize, 45b8a62b91Sopenharmony_ci//! } 46b8a62b91Sopenharmony_ci//! }; 47b8a62b91Sopenharmony_ci//! # let _ = nread; 48b8a62b91Sopenharmony_ci//! # Ok(()) 49b8a62b91Sopenharmony_ci//! # } 50b8a62b91Sopenharmony_ci//! ``` 51b8a62b91Sopenharmony_ci//! 52b8a62b91Sopenharmony_ci//! rustix's APIs perform the following tasks: 53b8a62b91Sopenharmony_ci//! - Error values are translated to [`Result`]s. 54b8a62b91Sopenharmony_ci//! - Buffers are passed as Rust slices. 55b8a62b91Sopenharmony_ci//! - Out-parameters are presented as return values. 56b8a62b91Sopenharmony_ci//! - Path arguments use [`Arg`], so they accept any string type. 57b8a62b91Sopenharmony_ci//! - File descriptors are passed and returned via [`AsFd`] and [`OwnedFd`] 58b8a62b91Sopenharmony_ci//! instead of bare integers, ensuring I/O safety. 59b8a62b91Sopenharmony_ci//! - Constants use `enum`s and [`bitflags`] types. 60b8a62b91Sopenharmony_ci//! - Multiplexed functions (eg. `fcntl`, `ioctl`, etc.) are de-multiplexed. 61b8a62b91Sopenharmony_ci//! - Variadic functions (eg. `openat`, etc.) are presented as non-variadic. 62b8a62b91Sopenharmony_ci//! - Functions and types which need `l` prefixes or `64` suffixes to enable 63b8a62b91Sopenharmony_ci//! large-file support are used automatically, and file sizes and offsets 64b8a62b91Sopenharmony_ci//! are presented as `u64` and `i64`. 65b8a62b91Sopenharmony_ci//! - Behaviors that depend on the sizes of C types like `long` are hidden. 66b8a62b91Sopenharmony_ci//! - In some places, more human-friendly and less historical-accident names 67b8a62b91Sopenharmony_ci//! are used (and documentation aliases are used so that the original names 68b8a62b91Sopenharmony_ci//! can still be searched for). 69b8a62b91Sopenharmony_ci//! - Provide y2038 compatibility, on platforms which support this. 70b8a62b91Sopenharmony_ci//! - Correct selected platform bugs, such as behavioral differences when 71b8a62b91Sopenharmony_ci//! running under seccomp. 72b8a62b91Sopenharmony_ci//! 73b8a62b91Sopenharmony_ci//! Things they don't do include: 74b8a62b91Sopenharmony_ci//! - Detecting whether functions are supported at runtime. 75b8a62b91Sopenharmony_ci//! - Hiding significant differences between platforms. 76b8a62b91Sopenharmony_ci//! - Restricting ambient authorities. 77b8a62b91Sopenharmony_ci//! - Imposing sandboxing features such as filesystem path or network address 78b8a62b91Sopenharmony_ci//! sandboxing. 79b8a62b91Sopenharmony_ci//! 80b8a62b91Sopenharmony_ci//! See [`cap-std`], [`system-interface`], and [`io-streams`] for libraries 81b8a62b91Sopenharmony_ci//! which do hide significant differences between platforms, and [`cap-std`] 82b8a62b91Sopenharmony_ci//! which does perform sandboxing and restricts ambient authorities. 83b8a62b91Sopenharmony_ci//! 84b8a62b91Sopenharmony_ci//! [`cap-std`]: https://crates.io/crates/cap-std 85b8a62b91Sopenharmony_ci//! [`system-interface`]: https://crates.io/crates/system-interface 86b8a62b91Sopenharmony_ci//! [`io-streams`]: https://crates.io/crates/io-streams 87b8a62b91Sopenharmony_ci//! [`getrandom`]: https://crates.io/crates/getrandom 88b8a62b91Sopenharmony_ci//! [`bitflags`]: https://crates.io/crates/bitflags 89b8a62b91Sopenharmony_ci//! [`AsFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsFd.html 90b8a62b91Sopenharmony_ci//! [`OwnedFd`]: https://docs.rs/io-lifetimes/latest/io_lifetimes/struct.OwnedFd.html 91b8a62b91Sopenharmony_ci//! [io-lifetimes crate]: https://crates.io/crates/io-lifetimes 92b8a62b91Sopenharmony_ci//! [I/O-safe]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md 93b8a62b91Sopenharmony_ci//! [`Result`]: https://docs.rs/rustix/latest/rustix/io/type.Result.html 94b8a62b91Sopenharmony_ci//! [`Arg`]: https://docs.rs/rustix/latest/rustix/path/trait.Arg.html 95b8a62b91Sopenharmony_ci 96b8a62b91Sopenharmony_ci#![deny(missing_docs)] 97b8a62b91Sopenharmony_ci#![allow(stable_features)] 98b8a62b91Sopenharmony_ci#![cfg_attr(linux_raw, deny(unsafe_code))] 99b8a62b91Sopenharmony_ci#![cfg_attr(rustc_attrs, feature(rustc_attrs))] 100b8a62b91Sopenharmony_ci#![cfg_attr(doc_cfg, feature(doc_cfg))] 101b8a62b91Sopenharmony_ci#![cfg_attr(all(target_os = "wasi", feature = "std"), feature(wasi_ext))] 102b8a62b91Sopenharmony_ci#![cfg_attr( 103b8a62b91Sopenharmony_ci all(linux_raw, naked_functions, target_arch = "x86"), 104b8a62b91Sopenharmony_ci feature(naked_functions) 105b8a62b91Sopenharmony_ci)] 106b8a62b91Sopenharmony_ci#![cfg_attr(io_lifetimes_use_std, feature(io_safety))] 107b8a62b91Sopenharmony_ci#![cfg_attr(core_ffi_c, feature(core_ffi_c))] 108b8a62b91Sopenharmony_ci#![cfg_attr(core_c_str, feature(core_c_str))] 109b8a62b91Sopenharmony_ci#![cfg_attr(alloc_c_string, feature(alloc_ffi))] 110b8a62b91Sopenharmony_ci#![cfg_attr(alloc_c_string, feature(alloc_c_string))] 111b8a62b91Sopenharmony_ci#![cfg_attr(not(feature = "std"), no_std)] 112b8a62b91Sopenharmony_ci#![cfg_attr(feature = "rustc-dep-of-std", feature(core_intrinsics))] 113b8a62b91Sopenharmony_ci#![cfg_attr(feature = "rustc-dep-of-std", feature(ip))] 114b8a62b91Sopenharmony_ci#![cfg_attr( 115b8a62b91Sopenharmony_ci all(not(feature = "rustc-dep-of-std"), core_intrinsics), 116b8a62b91Sopenharmony_ci feature(core_intrinsics) 117b8a62b91Sopenharmony_ci)] 118b8a62b91Sopenharmony_ci#![cfg_attr(asm_experimental_arch, feature(asm_experimental_arch))] 119b8a62b91Sopenharmony_ci#![cfg_attr(not(feature = "all-apis"), allow(dead_code))] 120b8a62b91Sopenharmony_ci// Clamp depends on Rust 1.50 which is newer than our MSRV. 121b8a62b91Sopenharmony_ci#![allow(clippy::manual_clamp)] 122b8a62b91Sopenharmony_ci// It is common in linux and libc APIs for types to vary between platforms. 123b8a62b91Sopenharmony_ci#![allow(clippy::unnecessary_cast)] 124b8a62b91Sopenharmony_ci// It is common in linux and libc APIs for types to vary between platforms. 125b8a62b91Sopenharmony_ci#![allow(clippy::useless_conversion)] 126b8a62b91Sopenharmony_ci 127b8a62b91Sopenharmony_ci#[cfg(not(feature = "rustc-dep-of-std"))] 128b8a62b91Sopenharmony_ciextern crate alloc; 129b8a62b91Sopenharmony_ci 130b8a62b91Sopenharmony_ci// Internal utilities. 131b8a62b91Sopenharmony_ci#[cfg(not(windows))] 132b8a62b91Sopenharmony_ci#[macro_use] 133b8a62b91Sopenharmony_cipub(crate) mod cstr; 134b8a62b91Sopenharmony_ci#[macro_use] 135b8a62b91Sopenharmony_cipub(crate) mod const_assert; 136b8a62b91Sopenharmony_cipub(crate) mod utils; 137b8a62b91Sopenharmony_ci 138b8a62b91Sopenharmony_ci// Pick the backend implementation to use. 139b8a62b91Sopenharmony_ci#[cfg_attr(libc, path = "backend/libc/mod.rs")] 140b8a62b91Sopenharmony_ci#[cfg_attr(linux_raw, path = "backend/linux_raw/mod.rs")] 141b8a62b91Sopenharmony_ci#[cfg_attr(wasi, path = "backend/wasi/mod.rs")] 142b8a62b91Sopenharmony_cimod backend; 143b8a62b91Sopenharmony_ci 144b8a62b91Sopenharmony_ci/// Export the `*Fd` types and traits that are used in rustix's public API. 145b8a62b91Sopenharmony_ci/// 146b8a62b91Sopenharmony_ci/// Users can use this to avoid needing to import anything else to use the same 147b8a62b91Sopenharmony_ci/// versions of these types and traits. 148b8a62b91Sopenharmony_cipub mod fd { 149b8a62b91Sopenharmony_ci use super::backend; 150b8a62b91Sopenharmony_ci #[cfg(windows)] 151b8a62b91Sopenharmony_ci pub use backend::fd::AsSocket; 152b8a62b91Sopenharmony_ci pub use backend::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; 153b8a62b91Sopenharmony_ci} 154b8a62b91Sopenharmony_ci 155b8a62b91Sopenharmony_ci// The public API modules. 156b8a62b91Sopenharmony_ci#[cfg(not(windows))] 157b8a62b91Sopenharmony_cipub mod ffi; 158b8a62b91Sopenharmony_ci#[cfg(not(windows))] 159b8a62b91Sopenharmony_ci#[cfg(feature = "fs")] 160b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "fs")))] 161b8a62b91Sopenharmony_cipub mod fs; 162b8a62b91Sopenharmony_cipub mod io; 163b8a62b91Sopenharmony_ci#[cfg(any(target_os = "android", target_os = "linux"))] 164b8a62b91Sopenharmony_ci#[cfg(feature = "io_uring")] 165b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "io_uring")))] 166b8a62b91Sopenharmony_cipub mod io_uring; 167b8a62b91Sopenharmony_ci#[cfg(not(any(windows, target_os = "wasi")))] 168b8a62b91Sopenharmony_ci#[cfg(feature = "mm")] 169b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "mm")))] 170b8a62b91Sopenharmony_cipub mod mm; 171b8a62b91Sopenharmony_ci#[cfg(not(any(target_os = "redox", target_os = "wasi")))] 172b8a62b91Sopenharmony_ci#[cfg(feature = "net")] 173b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "net")))] 174b8a62b91Sopenharmony_cipub mod net; 175b8a62b91Sopenharmony_ci#[cfg(not(windows))] 176b8a62b91Sopenharmony_ci#[cfg(feature = "param")] 177b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "param")))] 178b8a62b91Sopenharmony_cipub mod param; 179b8a62b91Sopenharmony_ci#[cfg(not(windows))] 180b8a62b91Sopenharmony_ci#[cfg(any(feature = "fs", feature = "net"))] 181b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(any(feature = "fs", feature = "net"))))] 182b8a62b91Sopenharmony_cipub mod path; 183b8a62b91Sopenharmony_ci#[cfg(not(windows))] 184b8a62b91Sopenharmony_ci#[cfg(feature = "process")] 185b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "process")))] 186b8a62b91Sopenharmony_cipub mod process; 187b8a62b91Sopenharmony_ci#[cfg(not(windows))] 188b8a62b91Sopenharmony_ci#[cfg(feature = "rand")] 189b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "rand")))] 190b8a62b91Sopenharmony_cipub mod rand; 191b8a62b91Sopenharmony_ci#[cfg(not(windows))] 192b8a62b91Sopenharmony_ci#[cfg(feature = "termios")] 193b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "termios")))] 194b8a62b91Sopenharmony_cipub mod termios; 195b8a62b91Sopenharmony_ci#[cfg(not(windows))] 196b8a62b91Sopenharmony_ci#[cfg(feature = "thread")] 197b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "thread")))] 198b8a62b91Sopenharmony_cipub mod thread; 199b8a62b91Sopenharmony_ci#[cfg(not(windows))] 200b8a62b91Sopenharmony_ci#[cfg(feature = "time")] 201b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "time")))] 202b8a62b91Sopenharmony_cipub mod time; 203b8a62b91Sopenharmony_ci 204b8a62b91Sopenharmony_ci// "runtime" is also a public API module, but it's only for libc-like users. 205b8a62b91Sopenharmony_ci#[cfg(not(windows))] 206b8a62b91Sopenharmony_ci#[cfg(feature = "runtime")] 207b8a62b91Sopenharmony_ci#[doc(hidden)] 208b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "runtime")))] 209b8a62b91Sopenharmony_cipub mod runtime; 210b8a62b91Sopenharmony_ci 211b8a62b91Sopenharmony_ci// We have some internal interdependencies in the API features, so for now, 212b8a62b91Sopenharmony_ci// for API features that aren't enabled, declare them as `pub(crate)` so 213b8a62b91Sopenharmony_ci// that they're not public, but still available for internal use. 214b8a62b91Sopenharmony_ci 215b8a62b91Sopenharmony_ci#[cfg(not(windows))] 216b8a62b91Sopenharmony_ci#[cfg(all( 217b8a62b91Sopenharmony_ci not(feature = "param"), 218b8a62b91Sopenharmony_ci any(feature = "runtime", feature = "time", target_arch = "x86"), 219b8a62b91Sopenharmony_ci))] 220b8a62b91Sopenharmony_cipub(crate) mod param; 221b8a62b91Sopenharmony_ci#[cfg(not(windows))] 222b8a62b91Sopenharmony_ci#[cfg(not(any(feature = "fs", feature = "net")))] 223b8a62b91Sopenharmony_cipub(crate) mod path; 224b8a62b91Sopenharmony_ci#[cfg(not(windows))] 225b8a62b91Sopenharmony_ci#[cfg(not(feature = "process"))] 226b8a62b91Sopenharmony_cipub(crate) mod process; 227