| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License.. |
| |
| use sgx_trts::libc; |
| use sgx_trts::trts; |
| use crate::io::ErrorKind; |
| |
| pub use self::rand::hashmap_random_keys; |
| |
| pub mod mutex; |
| pub mod condvar; |
| pub mod rwlock; |
| pub mod fd; |
| pub mod fs; |
| pub mod sgxfs; |
| pub mod io; |
| #[cfg(feature = "thread")] |
| pub mod thread; |
| #[cfg(feature = "thread")] |
| pub mod fast_thread_local; |
| #[cfg(feature = "thread")] |
| pub mod thread_local; |
| #[cfg(feature = "net")] |
| pub mod net; |
| pub mod path; |
| pub mod ext; |
| pub mod rand; |
| pub mod os; |
| #[cfg(feature = "stdio")] |
| pub mod stdio; |
| #[cfg(feature = "backtrace")] |
| pub mod backtrace; |
| pub mod time; |
| pub mod memchr; |
| pub mod cmath; |
| pub mod env; |
| #[cfg(feature = "pipe")] |
| pub mod pipe; |
| |
| pub use crate::sys_common::os_str_bytes as os_str; |
| |
| pub fn decode_error_kind(errno: i32) -> ErrorKind { |
| match errno as libc::c_int { |
| libc::ECONNREFUSED => ErrorKind::ConnectionRefused, |
| libc::ECONNRESET => ErrorKind::ConnectionReset, |
| libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied, |
| libc::EPIPE => ErrorKind::BrokenPipe, |
| libc::ENOTCONN => ErrorKind::NotConnected, |
| libc::ECONNABORTED => ErrorKind::ConnectionAborted, |
| libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable, |
| libc::EADDRINUSE => ErrorKind::AddrInUse, |
| libc::ENOENT => ErrorKind::NotFound, |
| libc::EINTR => ErrorKind::Interrupted, |
| libc::EINVAL => ErrorKind::InvalidInput, |
| libc::ETIMEDOUT => ErrorKind::TimedOut, |
| libc::EEXIST => ErrorKind::AlreadyExists, |
| |
| // These two constants can have the same value on some systems, |
| // but different values on others, so we can't use a match |
| // clause |
| x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock, |
| |
| _ => ErrorKind::Other, |
| } |
| } |
| |
| pub trait IsMinusOne { |
| fn is_minus_one(&self) -> bool; |
| } |
| |
| macro_rules! impl_is_minus_one { |
| ($($t:ident)*) => ($(impl IsMinusOne for $t { |
| fn is_minus_one(&self) -> bool { |
| *self == -1 |
| } |
| })*) |
| } |
| |
| impl_is_minus_one! { i8 i16 i32 i64 isize } |
| |
| pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> { |
| if t.is_minus_one() { Err(crate::io::Error::last_os_error()) } else { Ok(t) } |
| } |
| |
| pub fn cvt_r<T, F>(mut f: F) -> crate::io::Result<T> |
| where |
| T: IsMinusOne, |
| F: FnMut() -> T, |
| { |
| loop { |
| match cvt(f()) { |
| Err(ref e) if e.kind() == ErrorKind::Interrupted => {} |
| other => return other, |
| } |
| } |
| } |
| |
| pub unsafe fn abort_internal() -> ! { |
| trts::rsgx_abort() |
| } |