#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
compile_error!("oscall does not support this architecture on macOS");
pub mod errno;
pub(crate) mod event;
pub(crate) mod fs;
pub(crate) mod io;
pub(crate) mod mem;
pub(crate) mod net;
pub(crate) mod process;
pub(crate) mod rand;
pub(crate) mod sync;
pub(crate) mod thread;
pub(crate) mod time;
use core::ffi::c_int;
use crate::{Error, ErrorKind, Result};
pub(crate) type RawHandle = i32;
unsafe extern "C" {
fn __error() -> *mut c_int;
#[link_name = "close$NOCANCEL"]
fn close_nocancel(fd: c_int) -> c_int;
}
#[inline]
pub(crate) fn last_error() -> Error {
Error::from_raw_os_error(unsafe { *__error() })
}
#[inline]
#[allow(dead_code)]
pub(crate) fn set_errno(value: c_int) {
unsafe { *__error() = value };
}
#[inline]
#[allow(dead_code)]
pub(crate) fn cvt(ret: c_int) -> Result<c_int> {
if ret == -1 {
Err(last_error())
} else {
Ok(ret)
}
}
#[inline]
#[allow(dead_code)]
pub(crate) fn cvt_long(ret: i64) -> Result<i64> {
if ret == -1 {
Err(last_error())
} else {
Ok(ret)
}
}
pub(crate) unsafe fn close(fd: RawHandle) {
unsafe { close_nocancel(fd) };
}
pub(crate) const OTHER_ERROR: i32 = errno::EIO;
pub(crate) const ERRORS: &[(i32, ErrorKind)] = {
use ErrorKind::*;
use errno::*;
&[
(ENOENT, NotFound),
(EACCES, PermissionDenied),
(EPERM, PermissionDenied),
(EEXIST, AlreadyExists),
(EAGAIN, WouldBlock),
(EINPROGRESS, InProgress),
(EALREADY, InProgress),
(EINTR, Interrupted),
(EINVAL, InvalidInput),
(ETIMEDOUT, TimedOut),
(ENOTSUP, Unsupported),
(ENOSYS, Unsupported),
(EOPNOTSUPP, Unsupported),
(EAFNOSUPPORT, Unsupported),
(EPROTONOSUPPORT, Unsupported),
(ENOMEM, OutOfMemory),
(EBADF, BadHandle),
(ENOTSOCK, BadHandle),
(EPIPE, BrokenPipe),
(ENOTDIR, NotADirectory),
(EISDIR, IsADirectory),
(ENOTEMPTY, DirectoryNotEmpty),
(ENAMETOOLONG, InvalidFilename),
(ERANGE, BufferTooSmall),
(ELOOP, FilesystemLoop),
(ENOSPC, StorageFull),
(EDQUOT, QuotaExceeded),
(EFBIG, FileTooLarge),
(EROFS, ReadOnlyFilesystem),
(EXDEV, CrossesDevices),
(EMLINK, TooManyLinks),
(EMFILE, TooManyOpenFiles),
(ENFILE, TooManyOpenFiles),
(EBUSY, ResourceBusy),
(EDEADLK, Deadlock),
(ESPIPE, NotSeekable),
(ETXTBSY, ExecutableFileBusy),
(ECONNREFUSED, ConnectionRefused),
(ECONNRESET, ConnectionReset),
(ECONNABORTED, ConnectionAborted),
(ENOTCONN, NotConnected),
(EISCONN, AlreadyConnected),
(EADDRINUSE, AddrInUse),
(EADDRNOTAVAIL, AddrNotAvailable),
(ENETDOWN, NetworkDown),
(ENETUNREACH, NetworkUnreachable),
(EHOSTUNREACH, HostUnreachable),
(EMSGSIZE, MessageTooLong),
(ESRCH, NoSuchProcess),
]
};
#[cfg(test)]
mod tests {
use super::*;
unsafe extern "C" {
fn getpid() -> c_int;
fn close(fd: c_int) -> c_int;
}
#[test]
fn calls() {
let pid = unsafe { getpid() };
assert!(pid > 0);
#[cfg(feature = "std")]
assert_eq!(pid as u32, std::process::id());
let err = cvt(unsafe { close(-1) }).unwrap_err();
assert_eq!(err.raw_os_error(), errno::EBADF);
assert_eq!(err.kind(), ErrorKind::BadHandle);
}
}