Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support compilation to wasm target #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ match_cfg = "^0.1"
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
libc = "^0.2"

[target.'cfg(target_family = "wasm")'.dependencies]
libc = "^0.2"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "^0.3", features = ["sysinfoapi"] }

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ match_cfg! {
mod nix;
use ::nix as sys;
}
#[cfg(target_family = "wasm")] => {
extern crate libc;

mod wasm;

use ::wasm as sys;
}
#[cfg(target_os = "windows")] => {
extern crate winapi;

Expand Down
81 changes: 81 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::io;
#[cfg(feature = "set")]
use std::ffi::OsStr;
use std::ffi::OsString;

use libc;

const _SC_HOST_NAME_MAX: libc::c_int = 180;

pub fn get() -> io::Result<OsString> {
// According to the POSIX specification,
// host names are limited to `HOST_NAME_MAX` bytes
//
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html
let size =
unsafe { libc::sysconf(_SC_HOST_NAME_MAX) as libc::size_t };

// "wasihost" string buffer
let mut buffer = vec![0x77,0x61,0x73,0x69,0x68,0x6f,0x73,0x74,0x00; size];

Ok(wrap_buffer(buffer))
}

fn wrap_buffer(mut bytes: Vec<u8>) -> OsString {
// Returned name might be truncated if it does not fit
// and `buffer` will not contain the trailing \0 in that case.
// Manually capping the buffer length here.
let end = bytes
.iter()
.position(|&byte| byte == 0x00)
.unwrap_or_else(|| bytes.len());
bytes.resize(end, 0x00);

OsString::from_vec(bytes)
}

#[cfg(feature = "set")]
pub fn set(hostname: &OsStr) -> io::Result<()> {
Ok(())
}

#[cfg(test)]
mod tests {
use std::ffi::OsStr;

use super::wrap_buffer;

// Happy path case: there is a correct null terminated C string in a buffer
// and a bunch of NULL characters from the pre-allocated buffer
#[test]
fn test_non_overflowed_buffer() {
let buf = b"potato\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".to_vec();

assert_eq!(wrap_buffer(buf), OsStr::new("potato"));
}

#[test]
fn test_empty_buffer() {
let buf = b"".to_vec();

assert_eq!(wrap_buffer(buf), OsStr::new(""));
}

#[test]
fn test_filled_with_null_buffer() {
let buf = b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".to_vec();

assert_eq!(wrap_buffer(buf), OsStr::new(""));
}

// Hostname value had overflowed the buffer, so it was truncated
// and according to the POSIX documentation of the `gethostname`:
//
// > it is unspecified whether the returned name is null-terminated.
#[test]
fn test_overflowed_buffer() {
let buf = b"potat".to_vec();

assert_eq!(wrap_buffer(buf), OsStr::new("potat"));
}
}