1#![allow(dead_code)]
2use crate::Error;
3use core::{
4 mem::MaybeUninit,
5 num::NonZeroU32,
6 ptr::NonNull,
7 sync::atomic::{fence, AtomicPtr, Ordering},
8};
9use libc::c_void;
10
11cfg_if! {
12 if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android"))] {
13 use libc::__errno as errno_location;
14 } else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
15 use libc::__errno_location as errno_location;
16 } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
17 use libc::___errno as errno_location;
18 } else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
19 use libc::__error as errno_location;
20 } else if #[cfg(target_os = "haiku")] {
21 use libc::_errnop as errno_location;
22 } else if #[cfg(target_os = "nto")] {
23 use libc::__get_errno_ptr as errno_location;
24 } else if #[cfg(any(all(target_os = "horizon", target_arch = "arm"), target_os = "vita"))] {
25 extern "C" {
26 fn __errno() -> *mut libc::c_int;
28 }
29 use __errno as errno_location;
30 } else if #[cfg(target_os = "aix")] {
31 use libc::_Errno as errno_location;
32 }
33}
34
35cfg_if! {
36 if #[cfg(target_os = "vxworks")] {
37 use libc::errnoGet as get_errno;
38 } else {
39 unsafe fn get_errno() -> libc::c_int { *errno_location() }
40 }
41}
42
43pub fn last_os_error() -> Error {
44 let errno = unsafe { get_errno() };
45 if errno > 0 {
46 Error::from(NonZeroU32::new(errno as u32).unwrap())
47 } else {
48 Error::ERRNO_NOT_POSITIVE
49 }
50}
51
52pub fn sys_fill_exact(
56 mut buf: &mut [MaybeUninit<u8>],
57 sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
58) -> Result<(), Error> {
59 while !buf.is_empty() {
60 let res = sys_fill(buf);
61 match res {
62 res if res > 0 => buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?,
63 -1 => {
64 let err = last_os_error();
65 if err.raw_os_error() != Some(libc::EINTR) {
67 return Err(err);
68 }
69 }
70 _ => return Err(Error::UNEXPECTED),
74 }
75 }
76 Ok(())
77}
78
79pub struct Weak {
85 name: &'static str,
86 addr: AtomicPtr<c_void>,
87}
88
89impl Weak {
90 const UNINIT: *mut c_void = 1 as *mut c_void;
97
98 pub const unsafe fn new(name: &'static str) -> Self {
101 Self {
102 name,
103 addr: AtomicPtr::new(Self::UNINIT),
104 }
105 }
106
107 pub fn ptr(&self) -> Option<NonNull<c_void>> {
112 match self.addr.load(Ordering::Relaxed) {
119 Self::UNINIT => {
120 let symbol = self.name.as_ptr() as *const _;
121 let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, symbol) };
122 self.addr.store(addr, Ordering::Release);
124 NonNull::new(addr)
125 }
126 addr => {
127 let func = NonNull::new(addr)?;
128 fence(Ordering::Acquire);
129 Some(func)
130 }
131 }
132 }
133}
134
135pub unsafe fn open_readonly(path: &str) -> Result<libc::c_int, Error> {
137 debug_assert_eq!(path.as_bytes().last(), Some(&0));
138 loop {
139 let fd = libc::open(path.as_ptr() as *const _, libc::O_RDONLY | libc::O_CLOEXEC);
140 if fd >= 0 {
141 return Ok(fd);
142 }
143 let err = last_os_error();
144 if err.raw_os_error() != Some(libc::EINTR) {
146 return Err(err);
147 }
148 }
149}
150
151#[cfg(any(target_os = "android", target_os = "linux"))]
153pub fn getrandom_syscall(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
154 unsafe {
155 libc::syscall(
156 libc::SYS_getrandom,
157 buf.as_mut_ptr() as *mut libc::c_void,
158 buf.len(),
159 0,
160 ) as libc::ssize_t
161 }
162}