sys_util: Move set_errno to the only place it's used

Move the OS specific errno handling to the handle_eintr test code which
is the only place that uses it.

The function was already only compiled for test code and the ifdefs have
started to spread with the added android support. Moving the code to the
test that uses it makes it more obvious that it is test-only code.

TEST=cargo test passes

Change-Id: I9fea1bb30052c0edc41c9609e2698221daefa580
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2296828
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Tested-by: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Dylan Reid 2020-07-14 11:29:47 -07:00 committed by Commit Bot
parent ff7796372b
commit bae84398e5
2 changed files with 16 additions and 20 deletions

View file

@ -6,15 +6,6 @@ use std::fmt::{self, Display};
use std::io;
use std::result;
#[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.
#[derive(Clone, Copy, Debug, PartialEq)]
@ -65,13 +56,3 @@ impl Display for Error {
pub fn errno_result<T>() -> Result<T> {
Err(Error::last())
}
/// Sets errno to given error code.
/// Only defined when we compile tests as normal code does not
/// normally need set errno.
#[cfg(test)]
pub fn set_errno(e: i32) {
unsafe {
*errno_location() = e;
}
}

View file

@ -178,9 +178,24 @@ macro_rules! handle_eintr_errno {
#[cfg(test)]
mod tests {
use super::*;
use crate::errno::set_errno;
use crate::Error as SysError;
// Sets errno to the given error code.
fn set_errno(e: i32) {
#[cfg(target_os = "android")]
unsafe fn errno_location() -> *mut libc::c_int {
libc::__errno()
}
#[cfg(target_os = "linux")]
unsafe fn errno_location() -> *mut libc::c_int {
libc::__errno_location()
}
unsafe {
*errno_location() = e;
}
}
#[test]
fn i32_eintr_rc() {
let mut count = 3;