Use platform abstraction around libc::__errno_location.

It has a different name on Android.

BUG=b:158290206
TEST=cargo test

Change-Id: I424d0ddf24c008a06570c709354c7c2dd9395738
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2294865
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Andrew Walbran <qwandor@google.com>
This commit is contained in:
Andrew Walbran 2020-07-13 14:53:27 +01:00 committed by Commit Bot
parent fe368791f8
commit 67448289e5
2 changed files with 13 additions and 5 deletions

View file

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::io::Error;
use std::os::unix::io::RawFd;
use std::ptr::null_mut;
@ -20,7 +21,7 @@ pub unsafe fn io_uring_setup(num_entries: usize, params: &io_uring_params) -> Re
params as *const _,
);
if ret < 0 {
return Err(*libc::__errno_location());
return Err(Error::last_os_error().raw_os_error().unwrap());
}
Ok(ret as RawFd)
}
@ -35,7 +36,7 @@ pub unsafe fn io_uring_enter(fd: RawFd, to_submit: u64, to_wait: u64, flags: u32
null_mut::<*mut c_void>(),
);
if ret < 0 {
return Err(*libc::__errno_location());
return Err(Error::last_os_error().raw_os_error().unwrap());
}
Ok(())
}

View file

@ -6,7 +6,14 @@ use std::fmt::{self, Display};
use std::io;
use std::result;
use libc::__errno_location;
#[cfg(all(target_os = "android", test))]
unsafe fn errno_location() -> *mut libc::c_int {
libc::__errno()
}
#[cfg(all(target_os = "linux", test))]
unsafe fn errno_location() -> *mut libc::c_int {
libc::__errno_location()
}
/// An error number, retrieved from errno (man 3 errno), set by a libc
/// function that returned an error.
@ -25,7 +32,7 @@ impl Error {
/// The result of this only has any meaning just after a libc call that returned a value
/// indicating errno was set.
pub fn last() -> Error {
Error(unsafe { *__errno_location() })
Error(io::Error::last_os_error().raw_os_error().unwrap())
}
/// Gets the errno for this error
@ -65,6 +72,6 @@ pub fn errno_result<T>() -> Result<T> {
#[cfg(test)]
pub fn set_errno(e: i32) {
unsafe {
*__errno_location() = e;
*errno_location() = e;
}
}