From 67448289e538aadbf47eab77625901edc8760f07 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 13 Jul 2020 14:53:27 +0100 Subject: [PATCH] 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 Reviewed-by: Zach Reizner Tested-by: kokoro Tested-by: Dylan Reid Commit-Queue: Andrew Walbran --- io_uring/src/syscalls.rs | 5 +++-- sys_util/src/errno.rs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/io_uring/src/syscalls.rs b/io_uring/src/syscalls.rs index e52892b87d..e3c9f8630b 100644 --- a/io_uring/src/syscalls.rs +++ b/io_uring/src/syscalls.rs @@ -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(()) } diff --git a/sys_util/src/errno.rs b/sys_util/src/errno.rs index 607d6b3b1f..ed0b38b294 100644 --- a/sys_util/src/errno.rs +++ b/sys_util/src/errno.rs @@ -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() -> Result { #[cfg(test)] pub fn set_errno(e: i32) { unsafe { - *__errno_location() = e; + *errno_location() = e; } }