lx/
lib.rs

1//! A `no_std` interface to the userspace Linux ABI designed to have no overhead, but still be safe
2//! when possible by leveraging Rust's type system.
3//!
4//! # Result overhead
5//!
6//! Although types like `Result<(), i32>` are sometimes used in place of the smaller `i32` which
7//! can both represent errors and values on success, it doesn't add memory overhead or register
8//! pressure in practice because functions are inlined.
9//!
10//! To verify this, try to look at the assembler generated by this code when optimizations are
11//! enabled:
12//!
13//! ```no_run
14//! extern "C" {
15//!     fn fallible() -> i32;
16//!
17//!     fn test_succeeded();
18//!     fn test_failed(err: i32);
19//! }
20//!
21//! fn fallible_safe() -> Result<(), i32> {
22//!     let ret = unsafe { fallible() };
23//!     if ret < 0 {
24//!         return Err(ret);
25//!     }
26//!     Ok(())
27//! }
28//!
29//! pub fn test() {
30//!     if let Err(e) = fallible_safe() {
31//!         unsafe { test_failed(e) };
32//!         return;
33//!     }
34//!     unsafe { test_succeeded() };
35//! }
36//! ```
37#![no_std]
38#![feature(allocator_api)]
39#![feature(extern_types)]
40#![feature(never_type)]
41
42mod auto_unmount;
43mod cstr_array;
44mod error;
45mod fd;
46pub mod ioctl;
47mod mmap_alloc;
48mod net;
49mod print_macros;
50mod spawn;
51mod start;
52mod syscalls;
53
54pub use auto_unmount::*;
55pub use cstr_array::CStrArray;
56pub use error::*;
57pub use fd::*;
58pub use mmap_alloc::*;
59pub use net::*;
60pub use print_macros::*;
61pub use spawn::*;
62pub use start::*;
63pub use syscalls::*;
64
65pub const FD_CLOEXEC: i32 = 1;
66
67pub const SIGTERM: i32 = 15;
68pub const SIGCHLD: i32 = 17;
69
70pub const SOL_SOCKET: i32 = 1;
71
72pub const SCM_RIGHTS: i32 = 1;
73
74pub const MSG_DONTWAIT: u32 = 0x40;
75
76pub const SIG_BLOCK: i32 = 0;
77
78pub const PAGE_SIZE: usize = 0x1000;
79
80#[allow(non_camel_case_types)]
81#[repr(C)]
82#[derive(Clone, Copy)]
83pub struct iovec {
84    pub iov_base: *mut u8,
85    pub iov_len: usize,
86}
87
88#[allow(non_camel_case_types)]
89#[repr(C)]
90#[derive(Clone, Copy)]
91pub struct timespec {
92    pub tv_sec: i64,
93    pub tv_nsec: i64,
94}
95
96#[allow(non_camel_case_types)]
97#[repr(C)]
98#[derive(Clone, Copy)]
99pub struct timeval {
100    pub tv_sec: i64,
101    pub tv_usec: i64,
102}
103
104#[allow(non_camel_case_types)]
105pub type sigset_t = usize;
106
107#[allow(non_camel_case_types)]
108pub type uid_t = u32;
109
110#[allow(non_camel_case_types)]
111pub type gid_t = u32;
112
113#[allow(non_camel_case_types)]
114pub type pid_t = i32;