| 1 |
use crate::Error; |
| 2 |
use core::mem::MaybeUninit; |
| 3 |
|
| 4 |
cfg_if! { |
| 5 |
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android", target_os = "cygwin"))] { |
| 6 |
use libc::__errno as errno_location; |
| 7 |
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] { |
| 8 |
use libc::__errno_location as errno_location; |
| 9 |
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] { |
| 10 |
use libc::___errno as errno_location; |
| 11 |
} else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] { |
| 12 |
use libc::__error as errno_location; |
| 13 |
} else if #[cfg(target_os = "haiku")] { |
| 14 |
use libc::_errnop as errno_location; |
| 15 |
} else if #[cfg(target_os = "nto")] { |
| 16 |
use libc::__get_errno_ptr as errno_location; |
| 17 |
} else if #[cfg(any(all(target_os = "horizon", target_arch = "arm"), target_os = "vita"))] { |
| 18 |
extern "C" { |
| 19 |
// Not provided by libc: https://github.com/rust-lang/libc/issues/1995 |
| 20 |
fn __errno() -> *mut libc::c_int; |
| 21 |
} |
| 22 |
use __errno as errno_location; |
| 23 |
} else if #[cfg(target_os = "aix")] { |
| 24 |
use libc::_Errno as errno_location; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
cfg_if! { |
| 29 |
if #[cfg(target_os = "vxworks")] { |
| 30 |
use libc::errnoGet as get_errno; |
| 31 |
} else { |
| 32 |
unsafe fn get_errno() -> libc::c_int { *errno_location() } |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
pub(crate) fn last_os_error() -> Error { |
| 37 |
// We assume that on all targets which use the `util_libc` module `c_int` is equal to `i32` |
| 38 |
let errno: i32 = unsafe { get_errno() }; |
| 39 |
|
| 40 |
if errno > 0 { |
| 41 |
let code = errno |
| 42 |
.checked_neg() |
| 43 |
.expect("Positive number can be always negated"); |
| 44 |
Error::from_neg_error_code(code) |
| 45 |
} else { |
| 46 |
Error::ERRNO_NOT_POSITIVE |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/// Fill a buffer by repeatedly invoking `sys_fill`. |
| 51 |
/// |
| 52 |
/// The `sys_fill` function: |
| 53 |
/// - should return -1 and set errno on failure |
| 54 |
/// - should return the number of bytes written on success |
| 55 |
#[allow(dead_code)] |
| 56 |
pub(crate) fn sys_fill_exact( |
| 57 |
mut buf: &mut [MaybeUninit<u8>], |
| 58 |
sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t, |
| 59 |
) -> Result<(), Error> { |
| 60 |
while !buf.is_empty() { |
| 61 |
let res = sys_fill(buf); |
| 62 |
match res { |
| 63 |
res if res > 0 => { |
| 64 |
let len = usize::try_from(res).map_err(|_| Error::UNEXPECTED)?; |
| 65 |
buf = buf.get_mut(len..).ok_or(Error::UNEXPECTED)?; |
| 66 |
} |
| 67 |
-1 => { |
| 68 |
let err = last_os_error(); |
| 69 |
// We should try again if the call was interrupted. |
| 70 |
if err.raw_os_error() != Some(libc::EINTR) { |
| 71 |
return Err(err); |
| 72 |
} |
| 73 |
} |
| 74 |
// Negative return codes not equal to -1 should be impossible. |
| 75 |
// EOF (ret = 0) should be impossible, as the data we are reading |
| 76 |
// should be an infinite stream of random bytes. |
| 77 |
_ => return Err(Error::UNEXPECTED), |
| 78 |
} |
| 79 |
} |
| 80 |
Ok(()) |
| 81 |
} |