1b8a62b91Sopenharmony_ci<div align="center"> 2b8a62b91Sopenharmony_ci <h1><code>rustix</code></h1> 3b8a62b91Sopenharmony_ci 4b8a62b91Sopenharmony_ci <p> 5b8a62b91Sopenharmony_ci <strong>Safe Rust bindings to POSIX/Unix/Linux/Winsock2 syscalls</strong> 6b8a62b91Sopenharmony_ci </p> 7b8a62b91Sopenharmony_ci 8b8a62b91Sopenharmony_ci <strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong> 9b8a62b91Sopenharmony_ci 10b8a62b91Sopenharmony_ci <p> 11b8a62b91Sopenharmony_ci <a href="https://github.com/bytecodealliance/rustix/actions?query=workflow%3ACI"><img src="https://github.com/bytecodealliance/rustix/workflows/CI/badge.svg" alt="Github Actions CI Status" /></a> 12b8a62b91Sopenharmony_ci <a href="https://bytecodealliance.zulipchat.com/#narrow/stream/206238-general"><img src="https://img.shields.io/badge/zulip-join_chat-brightgreen.svg" alt="zulip chat" /></a> 13b8a62b91Sopenharmony_ci <a href="https://crates.io/crates/rustix"><img src="https://img.shields.io/crates/v/rustix.svg" alt="crates.io page" /></a> 14b8a62b91Sopenharmony_ci <a href="https://docs.rs/rustix"><img src="https://docs.rs/rustix/badge.svg" alt="docs.rs docs" /></a> 15b8a62b91Sopenharmony_ci </p> 16b8a62b91Sopenharmony_ci</div> 17b8a62b91Sopenharmony_ci 18b8a62b91Sopenharmony_ci`rustix` provides efficient memory-safe and [I/O-safe] wrappers to POSIX-like, 19b8a62b91Sopenharmony_ciUnix-like, Linux, and Winsock2 syscall-like APIs, with configurable backends. 20b8a62b91Sopenharmony_ciIt uses Rust references, slices, and return values instead of raw pointers, and 21b8a62b91Sopenharmony_ci[`io-lifetimes`] instead of raw file descriptors, providing memory safety, 22b8a62b91Sopenharmony_ci[I/O safety], and [provenance]. It uses `Result`s for reporting errors, 23b8a62b91Sopenharmony_ci[`bitflags`] instead of bare integer flags, an [`Arg`] trait with optimizations 24b8a62b91Sopenharmony_cito efficiently accept any Rust string type, and several other efficient 25b8a62b91Sopenharmony_ciconveniences. 26b8a62b91Sopenharmony_ci 27b8a62b91Sopenharmony_ci`rustix` is low-level and, and while the `net` API supports Winsock2 on 28b8a62b91Sopenharmony_ciWindows, the rest of the APIs do not support Windows; for higher-level and more 29b8a62b91Sopenharmony_ciportable APIs built on this functionality, see the [`system-interface`], 30b8a62b91Sopenharmony_ci[`cap-std`], and [`fs-set-times`] crates, for example. 31b8a62b91Sopenharmony_ci 32b8a62b91Sopenharmony_ci`rustix` currently has two backends available: 33b8a62b91Sopenharmony_ci 34b8a62b91Sopenharmony_ci * linux_raw, which uses raw Linux system calls and vDSO calls, and is 35b8a62b91Sopenharmony_ci supported on Linux on x86-64, x86, aarch64, riscv64gc, powerpc64le, 36b8a62b91Sopenharmony_ci arm (v5 onwards), mipsel, and mips64el, with stable, nightly, and 1.48 Rust. 37b8a62b91Sopenharmony_ci - By being implemented entirely in Rust, avoiding `libc`, `errno`, and pthread 38b8a62b91Sopenharmony_ci cancellation, and employing some specialized optimizations, most functions 39b8a62b91Sopenharmony_ci compile down to very efficient code. On nightly Rust, they can often be 40b8a62b91Sopenharmony_ci fully inlined into user code. 41b8a62b91Sopenharmony_ci - Most functions in `linux_raw` preserve memory, I/O safety, and pointer 42b8a62b91Sopenharmony_ci provenance all the way down to the syscalls. 43b8a62b91Sopenharmony_ci 44b8a62b91Sopenharmony_ci * libc, which uses the [`libc`] crate which provides bindings to native `libc` 45b8a62b91Sopenharmony_ci libraries on Unix-family platforms, and [`windows-sys`] for Winsock2 on 46b8a62b91Sopenharmony_ci Windows, and is portable to many OS's. 47b8a62b91Sopenharmony_ci 48b8a62b91Sopenharmony_ciThe linux_raw backend is enabled by default on platforms which support it. To 49b8a62b91Sopenharmony_cienable the libc backend instead, either enable the "use-libc" cargo feature, 50b8a62b91Sopenharmony_cior set the `RUSTFLAGS` environment variable to `--cfg=rustix_use_libc` when 51b8a62b91Sopenharmony_cibuilding. 52b8a62b91Sopenharmony_ci 53b8a62b91Sopenharmony_ci## Cargo features 54b8a62b91Sopenharmony_ci 55b8a62b91Sopenharmony_ciThe modules [`rustix::io`], [`rustix::fd`], and [`rustix::ffi`] are enabled 56b8a62b91Sopenharmony_ciby default. The rest of the API is conditional with cargo feature flags: 57b8a62b91Sopenharmony_ci 58b8a62b91Sopenharmony_ci| Name | Description 59b8a62b91Sopenharmony_ci| ---------- | --------------------- 60b8a62b91Sopenharmony_ci| `fs` | [`rustix::fs`] and [`rustix::path`]—Filesystem operations. 61b8a62b91Sopenharmony_ci| `io_uring` | [`rustix::io_uring`]—Linux io_uring. 62b8a62b91Sopenharmony_ci| `mm` | [`rustix::mm`]—Memory map operations. 63b8a62b91Sopenharmony_ci| `net` | [`rustix::net`] and [`rustix::path`]—Network-related operations. 64b8a62b91Sopenharmony_ci| `param` | [`rustix::param`]—Process parameters. 65b8a62b91Sopenharmony_ci| `process` | [`rustix::process`]—Process-associated operations. 66b8a62b91Sopenharmony_ci| `rand` | [`rustix::rand`]—Random-related operations. 67b8a62b91Sopenharmony_ci| `termios` | [`rustix::termios`]—Terminal I/O stream operations. 68b8a62b91Sopenharmony_ci| `thread` | [`rustix::thread`]—Thread-associated operations. 69b8a62b91Sopenharmony_ci| `time` | [`rustix::time`]—Time-related operations. 70b8a62b91Sopenharmony_ci| | 71b8a62b91Sopenharmony_ci| `use-libc` | Enable the libc backend. 72b8a62b91Sopenharmony_ci 73b8a62b91Sopenharmony_ci[`rustix::fs`]: https://docs.rs/rustix/latest/rustix/fs/index.html 74b8a62b91Sopenharmony_ci[`rustix::io_uring`]: https://docs.rs/rustix/latest/rustix/io_uring/index.html 75b8a62b91Sopenharmony_ci[`rustix::mm`]: https://docs.rs/rustix/latest/rustix/mm/index.html 76b8a62b91Sopenharmony_ci[`rustix::net`]: https://docs.rs/rustix/latest/rustix/net/index.html 77b8a62b91Sopenharmony_ci[`rustix::param`]: https://docs.rs/rustix/latest/rustix/param/index.html 78b8a62b91Sopenharmony_ci[`rustix::process`]: https://docs.rs/rustix/latest/rustix/process/index.html 79b8a62b91Sopenharmony_ci[`rustix::rand`]: https://docs.rs/rustix/latest/rustix/rand/index.html 80b8a62b91Sopenharmony_ci[`rustix::termios`]: https://docs.rs/rustix/latest/rustix/termios/index.html 81b8a62b91Sopenharmony_ci[`rustix::thread`]: https://docs.rs/rustix/latest/rustix/thread/index.html 82b8a62b91Sopenharmony_ci[`rustix::time`]: https://docs.rs/rustix/latest/rustix/time/index.html 83b8a62b91Sopenharmony_ci[`rustix::io`]: https://docs.rs/rustix/latest/rustix/io/index.html 84b8a62b91Sopenharmony_ci[`rustix::fd`]: https://docs.rs/rustix/latest/rustix/fd/index.html 85b8a62b91Sopenharmony_ci[`rustix::ffi`]: https://docs.rs/rustix/latest/rustix/ffi/index.html 86b8a62b91Sopenharmony_ci[`rustix::path`]: https://docs.rs/rustix/latest/rustix/path/index.html 87b8a62b91Sopenharmony_ci 88b8a62b91Sopenharmony_ci## Similar crates 89b8a62b91Sopenharmony_ci 90b8a62b91Sopenharmony_ci`rustix` is similar to [`nix`], [`simple_libc`], [`unix`], [`nc`], and 91b8a62b91Sopenharmony_ci[`uapi`]. `rustix` is architected for [I/O safety] with most APIs using 92b8a62b91Sopenharmony_ci[`OwnedFd`] and [`AsFd`] to manipulate file descriptors rather than `File` or 93b8a62b91Sopenharmony_cieven `c_int`, and supporting multiple backends so that it can use direct 94b8a62b91Sopenharmony_cisyscalls while still being usable on all platforms `libc` supports. Like `nix`, 95b8a62b91Sopenharmony_ci`rustix` has an optimized and flexible filename argument mechanism that allows 96b8a62b91Sopenharmony_ciusers to use a variety of string types, including non-UTF-8 string types. 97b8a62b91Sopenharmony_ci 98b8a62b91Sopenharmony_ci[`relibc`] is a similar project which aims to be a full "libc", including 99b8a62b91Sopenharmony_ciC-compatible interfaces and higher-level C/POSIX standard-library 100b8a62b91Sopenharmony_cifunctionality; `rustix` just aims to provide safe and idiomatic Rust interfaces 101b8a62b91Sopenharmony_cito low-level syscalls. `relibc` also doesn't tend to support features not 102b8a62b91Sopenharmony_cisupported on Redox, such as `*at` functions like `openat`, which are important 103b8a62b91Sopenharmony_cifeatures for `rustix`. 104b8a62b91Sopenharmony_ci 105b8a62b91Sopenharmony_ci`rustix` has its own code for making direct syscalls, similar to the [`sc`] and 106b8a62b91Sopenharmony_ci[`scall`] crates, though `rustix` can use either the Rust `asm!` macro or 107b8a62b91Sopenharmony_ciout-of-line `.s` files so it supports Rust versions from 1.48 through Nightly. 108b8a62b91Sopenharmony_ci`rustix` can also use Linux's vDSO mechanism to optimize Linux `clock_gettime` 109b8a62b91Sopenharmony_cion all architectures, and all Linux system calls on x86. And `rustix`'s 110b8a62b91Sopenharmony_cisyscalls report errors using an optimized `Errno` type. 111b8a62b91Sopenharmony_ci 112b8a62b91Sopenharmony_ci`rustix`'s `*at` functions are similar to the [`openat`] crate, but `rustix` 113b8a62b91Sopenharmony_ciprovides them as free functions rather than associated functions of a `Dir` 114b8a62b91Sopenharmony_citype. `rustix`'s `cwd()` function exposes the special `AT_FDCWD` value in a safe 115b8a62b91Sopenharmony_ciway, so users don't need to open `.` to get a current-directory handle. 116b8a62b91Sopenharmony_ci 117b8a62b91Sopenharmony_ci`rustix`'s `openat2` function is similar to the [`openat2`] crate, but uses 118b8a62b91Sopenharmony_ciI/O safety types rather than `RawFd`. `rustix` does not provide dynamic feature 119b8a62b91Sopenharmony_cidetection, so users must handle the [`NOSYS`] error themselves. 120b8a62b91Sopenharmony_ci 121b8a62b91Sopenharmony_ci`rustix`'s `termios` module is similar to the [`termios`] crate, but uses 122b8a62b91Sopenharmony_ciI/O safety types rather than `RawFd`, and the flags parameters to functions 123b8a62b91Sopenharmony_cisuch as `tcsetattr` are `enum`s rather than bare integers. And, rustix calls 124b8a62b91Sopenharmony_ciits `tcgetattr` function `tcgetattr`, rather than `Termios::from_fd`. 125b8a62b91Sopenharmony_ci 126b8a62b91Sopenharmony_ci## Minimum Supported Rust Version (MSRV) 127b8a62b91Sopenharmony_ci 128b8a62b91Sopenharmony_ciThis crate currently works on the version of [Rust on Debian stable], which is 129b8a62b91Sopenharmony_cicurrently Rust 1.48. This policy may change in the future, in minor version 130b8a62b91Sopenharmony_cireleases, so users using a fixed version of Rust should pin to a specific 131b8a62b91Sopenharmony_civersion of this crate. 132b8a62b91Sopenharmony_ci 133b8a62b91Sopenharmony_ci[Rust on Debian stable]: https://packages.debian.org/stable/rust/rustc 134b8a62b91Sopenharmony_ci[`nix`]: https://crates.io/crates/nix 135b8a62b91Sopenharmony_ci[`unix`]: https://crates.io/crates/unix 136b8a62b91Sopenharmony_ci[`nc`]: https://crates.io/crates/nc 137b8a62b91Sopenharmony_ci[`simple_libc`]: https://crates.io/crates/simple_libc 138b8a62b91Sopenharmony_ci[`uapi`]: https://crates.io/crates/uapi 139b8a62b91Sopenharmony_ci[`relibc`]: https://github.com/redox-os/relibc 140b8a62b91Sopenharmony_ci[`syscall`]: https://crates.io/crates/syscall 141b8a62b91Sopenharmony_ci[`sc`]: https://crates.io/crates/sc 142b8a62b91Sopenharmony_ci[`scall`]: https://crates.io/crates/scall 143b8a62b91Sopenharmony_ci[`system-interface`]: https://crates.io/crates/system-interface 144b8a62b91Sopenharmony_ci[`openat`]: https://crates.io/crates/openat 145b8a62b91Sopenharmony_ci[`openat2`]: https://crates.io/crates/openat2 146b8a62b91Sopenharmony_ci[`fs-set-times`]: https://crates.io/crates/fs-set-times 147b8a62b91Sopenharmony_ci[`io-lifetimes`]: https://crates.io/crates/io-lifetimes 148b8a62b91Sopenharmony_ci[`termios`]: https://crates.io/crates/termios 149b8a62b91Sopenharmony_ci[`libc`]: https://crates.io/crates/libc 150b8a62b91Sopenharmony_ci[`windows-sys`]: https://crates.io/crates/windows-sys 151b8a62b91Sopenharmony_ci[`cap-std`]: https://crates.io/crates/cap-std 152b8a62b91Sopenharmony_ci[`bitflags`]: https://crates.io/crates/bitflags 153b8a62b91Sopenharmony_ci[`Arg`]: https://docs.rs/rustix/latest/rustix/path/trait.Arg.html 154b8a62b91Sopenharmony_ci[I/O-safe]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md 155b8a62b91Sopenharmony_ci[I/O safety]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md 156b8a62b91Sopenharmony_ci[provenance]: https://github.com/rust-lang/rust/issues/95228 157b8a62b91Sopenharmony_ci[`OwnedFd`]: https://docs.rs/io-lifetimes/latest/io_lifetimes/struct.OwnedFd.html 158b8a62b91Sopenharmony_ci[`AsFd`]: https://docs.rs/io-lifetimes/latest/io_lifetimes/trait.AsFd.html 159b8a62b91Sopenharmony_ci[`NOSYS`]: https://docs.rs/rustix/latest/rustix/io/struct.Errno.html#associatedconstant.NOSYS 160