1#![doc = include_str!("../README.md")]
2#![no_std]
4#![feature(thread_local)] #![feature(c_variadic)] #![feature(sync_unsafe_cell)] #![feature(linkage)] #![allow(unexpected_cfgs)]
11#![allow(unreachable_patterns)]
13#![allow(irrefutable_let_patterns)]
15
16#[cfg(all(feature = "coexist-with-libc", feature = "take-charge"))]
18compile_error!("Enable only one of \"coexist-with-libc\" and \"take-charge\".");
19#[cfg(all(not(feature = "coexist-with-libc"), not(feature = "take-charge")))]
20compile_error!("Enable one \"coexist-with-libc\" and \"take-charge\".");
21
22extern crate alloc;
23
24pub use libc::*;
27
28#[macro_use]
29mod use_libc;
30
31#[cfg(not(target_os = "wasi"))]
32mod at_fork;
33mod error_str;
34mod sync_ptr;
35
36#[cfg(feature = "take-charge")]
48use core::ptr::addr_of;
49use errno::{set_errno, Errno};
50
51mod arpa_inet;
52mod atoi;
53mod base64;
54mod brk;
55mod ctype;
56mod env;
57mod errno_;
58mod error;
59mod exec;
60#[cfg(feature = "take-charge")]
61mod exit;
62mod fs;
63mod glibc_versioning;
64mod int;
65mod io;
66mod jmp;
67mod locale;
68#[cfg(feature = "take-charge")]
69mod malloc;
70mod math;
71mod mem;
72mod mkostemps;
73#[cfg(not(target_os = "wasi"))]
74mod mm;
75mod net;
76mod nss;
77mod path;
78mod pause;
79mod posix_spawn;
80#[cfg(not(target_os = "wasi"))]
81mod process;
82mod process_;
83mod pty;
84mod rand;
85mod rand48;
86mod rand_;
87mod regex;
88mod shm;
89#[cfg(not(target_os = "wasi"))]
90#[cfg(feature = "take-charge")]
91mod signal;
92mod sort;
93#[cfg(feature = "take-charge")]
94mod stdio;
95mod strtod;
96mod strtol;
97mod syscall;
98mod system;
99mod termios_;
100#[cfg(feature = "thread")]
101#[cfg(feature = "take-charge")]
102mod thread;
103mod time;
104
105#[cfg(feature = "deprecated-and-unimplemented")]
106mod deprecated;
107#[cfg(feature = "todo")]
108mod todo;
109
110#[cfg(feature = "take-charge")]
112#[no_mangle]
113static __dso_handle: UnsafeSendSyncVoidStar =
114 UnsafeSendSyncVoidStar(addr_of!(__dso_handle) as *const _);
115
116#[repr(transparent)]
126#[cfg(feature = "take-charge")]
127struct UnsafeSendSyncVoidStar(*const core::ffi::c_void);
128#[cfg(feature = "take-charge")]
129unsafe impl Send for UnsafeSendSyncVoidStar {}
130#[cfg(feature = "take-charge")]
131unsafe impl Sync for UnsafeSendSyncVoidStar {}
132
133#[cfg(feature = "take-charge")]
138#[cfg(feature = "call-main")]
139#[no_mangle]
140unsafe fn origin_main(argc: usize, argv: *mut *mut u8, envp: *mut *mut u8) -> i32 {
141 extern "C" {
142 fn main(argc: i32, argv: *const *const u8, envp: *const *const u8) -> i32;
143 }
144 main(argc as _, argv as _, envp as _)
145}
146
147fn convert_res<T>(result: Result<T, rustix::io::Errno>) -> Option<T> {
152 result
153 .map_err(|err| set_errno(Errno(err.raw_os_error())))
154 .ok()
155}
156
157#[cfg(feature = "thread")]
160#[cfg(feature = "take-charge")]
161pub(crate) struct GetThreadId;
162
163#[cfg(feature = "thread")]
164#[cfg(feature = "take-charge")]
165unsafe impl rustix_futex_sync::lock_api::GetThreadId for GetThreadId {
166 const INIT: Self = Self;
167
168 #[inline]
169 fn nonzero_thread_id(&self) -> core::num::NonZeroUsize {
170 origin::thread::current().to_raw_non_null().addr()
174 }
175}
176
177#[cfg(feature = "global-allocator")]
179#[global_allocator]
180static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;
181
182#[cfg(feature = "take-charge")]
184pub(crate) fn expand_sigset(set: rustix::runtime::KernelSigSet) -> libc::sigset_t {
185 let mut lc = core::mem::MaybeUninit::<libc::sigset_t>::uninit();
186 unsafe {
187 let r = libc::sigemptyset(lc.as_mut_ptr());
189 assert_eq!(r, 0);
190 lc.as_mut_ptr()
193 .cast::<rustix::runtime::KernelSigSet>()
194 .write(set);
195 lc.assume_init()
196 }
197}