linux_unsafe/lib.rs
1//! A low-level, unsafe Rust interface to Linux system calls.
2//!
3//! The [`raw`] module provides functions wrapping platform-specific assembly
4//! language stubs for making arbitrary system calls by providing a system
5//! call number and arbitrary number of arguments.
6//!
7//! This crate currently supports the following architectures:
8//!
9//! - x86_64
10//! - x86 (32-bit)
11//! - arm
12//! - riscv64
13//!
14//! For this initial release, x86_64 has seen some limited testing and the
15//! other platforms have been barely tested at all. Over time I intend to
16//! support all architectures that Linux supports that are also supported
17//! by Rust inline assembly, but we'll see how it goes.
18//!
19//! The functions in the root of the crate then wrap those stubs with thin
20//! wrappers that just lightly convert their arguments to what the kernel
21//! expects for a particular system call and then delegate to one of the system
22//! call stubs in [`raw`].
23//!
24//! This crate also includes a number of types and type aliases representing
25//! the memory layout of objects the kernel will interpret. For those which
26//! are aliases, calling code must always use the aliases rather than their
27//! underlying types because their exact definitions may vary on different
28//! platforms and in future versions of this crate.
29//!
30//! Where possible the wrapping functions and types are portable across
31//! architectures, as long as callers use the argument types and type aliases
32//! defined in this crate. The raw system call interface has considerable
33//! overlap between platforms but is ultimately architecture-specific and this
34//! crate does not attempt to hide differences at that layer.
35//!
36//! # Be careful mixing with `std`
37//!
38//! The Rust `std` crate has lots of functionality that wraps the target's
39//! libc functions. On Linux systems libc is a wrapper around the same system
40//! call interface this crate is exposing, but also adds other state and
41//! abstractions such as buffers and error codes. Making direct system calls
42//! may violate the assumptions being made by libc.
43//!
44//! To avoid strange problems, avoid interacting with the same system resources
45//! through both the standard library and though direct system calls.
46#![no_std]
47
48static_assertions::assert_cfg!(
49 target_os = "linux",
50 "The linux-unsafe crate targets the Linux system call interface, and so is not compatible with any other operating system.",
51);
52
53mod funcs;
54mod types;
55
56pub use funcs::*;
57pub use types::*;
58pub mod args;
59pub mod result;
60pub(crate) mod sigset;
61
62#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
63#[path = "raw/x86_64.rs"]
64pub mod raw;
65
66#[cfg(all(target_os = "linux", target_arch = "x86"))]
67#[path = "raw/x86.rs"]
68pub mod raw;
69
70#[cfg(all(target_os = "linux", target_arch = "arm"))]
71#[path = "raw/arm.rs"]
72pub mod raw;
73
74#[cfg(all(target_os = "linux", target_arch = "riscv64"))]
75#[path = "raw/riscv64.rs"]
76pub mod raw;
77
78#[cfg(test)]
79mod tests;