oscall 0.1.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! WASI preview 1: the `wasi_snapshot_preview1` imports, declared next to
//! their users:
//!
//! ```ignore
//! #[link(wasm_import_module = "wasi_snapshot_preview1")]
//! unsafe extern "C" {
//!     fn fd_close(fd: i32) -> i32;
//! }
//! ```
//!
//! Every import returns an `errno` (0 for success) and writes its results
//! through pointer arguments; 64-bit arguments are plain `i64`.
//!
//! WASI has no memory mapping, no processes, no threads and no way to create
//! a socket: the portable functions for those fail with `Unsupported`.

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 crate::{Error, ErrorKind, Result};

pub(crate) type RawHandle = i32;

#[link(wasm_import_module = "wasi_snapshot_preview1")]
unsafe extern "C" {
    fn fd_close(fd: i32) -> i32;
}

/// Converts a WASI `errno` return value to a `Result`.
#[inline]
pub(crate) fn cvt(errno: i32) -> Result<()> {
    if errno == 0 {
        Ok(())
    } else {
        Err(Error::from_raw_os_error(errno))
    }
}

/// # Safety
///
/// `fd` must be an open descriptor that nothing uses afterwards.
pub(crate) unsafe fn close(fd: RawHandle) {
    // SAFETY: closing a descriptor the caller owns.
    let _ = cvt(unsafe { fd_close(fd) });
}

/// The generic I/O error.
pub(crate) const OTHER_ERROR: i32 = errno::EIO;

/// The classification of error codes. The first entry of a kind is the code
/// `Error::from(kind)` produces.
pub(crate) const ERRORS: &[(i32, ErrorKind)] = {
    use ErrorKind::*;
    use errno::*;
    &[
        (ENOENT, NotFound),
        (EACCES, PermissionDenied),
        (EPERM, PermissionDenied),
        (ENOTCAPABLE, PermissionDenied),
        (EEXIST, AlreadyExists),
        (EAGAIN, WouldBlock),
        (EINPROGRESS, InProgress),
        (EALREADY, InProgress),
        (EINTR, Interrupted),
        (EINVAL, InvalidInput),
        (ETIMEDOUT, TimedOut),
        (ENOTSUP, Unsupported),
        (ENOSYS, 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::*;

    #[test]
    fn calls() {
        // SAFETY: closing a descriptor that is not open only fails.
        let err = cvt(unsafe { fd_close(12345) }).unwrap_err();
        assert_eq!(err.raw_os_error(), errno::EBADF);
        assert_eq!(err.kind(), ErrorKind::BadHandle);
    }
}