| 1 |
// This file is part of the platform-info package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE file |
| 4 |
// that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (API) nodename osname sysname gethostname |
| 7 |
// spell-checker:ignore (runtimes) wasmtime wasmer wazero |
| 8 |
|
| 9 |
#![warn(unused_results)] // enable warnings for unused results |
| 10 |
|
| 11 |
use std::ffi::{OsStr, OsString}; |
| 12 |
|
| 13 |
use crate::{PlatformInfoAPI, PlatformInfoError, UNameAPI}; |
| 14 |
|
| 15 |
/// Platform information for WASI (WebAssembly System Interface). |
| 16 |
/// |
| 17 |
/// WASI does not provide a `uname()` syscall, so we return values |
| 18 |
/// that describe the WebAssembly platform. |
| 19 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 20 |
pub struct PlatformInfo { |
| 21 |
sysname: OsString, |
| 22 |
nodename: OsString, |
| 23 |
release: OsString, |
| 24 |
version: OsString, |
| 25 |
machine: OsString, |
| 26 |
processor: OsString, |
| 27 |
osname: OsString, |
| 28 |
} |
| 29 |
|
| 30 |
/// Try to detect the hostname from environment variables. |
| 31 |
/// |
| 32 |
/// WASI (neither preview 1 nor preview 2) provides a `gethostname` syscall or |
| 33 |
/// equivalent WIT interface. The `wasi-sockets` proposal lists hostname as a |
| 34 |
/// future goal. Until then, the best we can do is check environment variables |
| 35 |
/// that the host runtime may pass (e.g. `wasmtime --env HOSTNAME=$(hostname)`). |
| 36 |
fn detect_nodename() -> OsString { |
| 37 |
for var in &["HOSTNAME", "COMPUTERNAME"] { |
| 38 |
if let Ok(val) = std::env::var(var) { |
| 39 |
if !val.is_empty() { |
| 40 |
return OsString::from(val); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
OsString::from("localhost") |
| 45 |
} |
| 46 |
|
| 47 |
impl PlatformInfoAPI for PlatformInfo { |
| 48 |
/// Create a new `PlatformInfo` for WASI. |
| 49 |
/// |
| 50 |
/// WASI does not provide a `uname()` syscall, and no WASI runtime (wasmtime, |
| 51 |
/// wasmer, `WasmEdge`, wazero) exposes its identity or version to guest modules. |
| 52 |
/// There is no standard or convention for runtime self-identification. |
| 53 |
/// |
| 54 |
/// We follow the same approach as wasi-libc's `uname()` implementation, which |
| 55 |
/// returns hardcoded values: `sysname="wasi"`, `release="0.0.0"`, |
| 56 |
/// `version="0.0.0"`, `machine="wasm32"`. |
| 57 |
/// |
| 58 |
/// ref: <https://github.com/WebAssembly/wasi-libc/blob/main/libc-top-half/musl/src/misc/uname.c> |
| 59 |
fn new() -> Result<Self, PlatformInfoError> { |
| 60 |
Ok(Self { |
| 61 |
sysname: OsString::from("wasi"), |
| 62 |
nodename: detect_nodename(), |
| 63 |
release: OsString::from("0.0.0"), |
| 64 |
version: OsString::from("0.0.0"), |
| 65 |
machine: OsString::from(std::env::consts::ARCH), |
| 66 |
processor: OsString::from(crate::lib_impl::map_processor(std::env::consts::ARCH)), |
| 67 |
osname: OsString::from(crate::lib_impl::HOST_OS_NAME), |
| 68 |
}) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl UNameAPI for PlatformInfo { |
| 73 |
fn sysname(&self) -> &OsStr { |
| 74 |
&self.sysname |
| 75 |
} |
| 76 |
|
| 77 |
fn nodename(&self) -> &OsStr { |
| 78 |
&self.nodename |
| 79 |
} |
| 80 |
|
| 81 |
fn release(&self) -> &OsStr { |
| 82 |
&self.release |
| 83 |
} |
| 84 |
|
| 85 |
fn version(&self) -> &OsStr { |
| 86 |
&self.version |
| 87 |
} |
| 88 |
|
| 89 |
fn machine(&self) -> &OsStr { |
| 90 |
&self.machine |
| 91 |
} |
| 92 |
|
| 93 |
fn processor(&self) -> &OsStr { |
| 94 |
&self.processor |
| 95 |
} |
| 96 |
|
| 97 |
fn osname(&self) -> &OsStr { |
| 98 |
&self.osname |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_wasi() { |
| 104 |
let info = PlatformInfo::new().unwrap(); |
| 105 |
|
| 106 |
assert_eq!(info.sysname().to_string_lossy(), "wasi"); |
| 107 |
// nodename comes from env or defaults to "localhost" |
| 108 |
assert!(!info.nodename().to_string_lossy().is_empty()); |
| 109 |
assert_eq!(info.release().to_string_lossy(), "0.0.0"); |
| 110 |
assert_eq!(info.version().to_string_lossy(), "0.0.0"); |
| 111 |
assert_eq!(info.machine().to_string_lossy(), std::env::consts::ARCH); |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_wasi_nodename_env() { |
| 116 |
let orig = std::env::var("HOSTNAME").ok(); |
| 117 |
|
| 118 |
// SAFETY: This test runs single-threaded (--test-threads 1) so env mutation is safe. |
| 119 |
unsafe { |
| 120 |
std::env::set_var("HOSTNAME", "my-wasi-host"); |
| 121 |
} |
| 122 |
let info = PlatformInfo::new().unwrap(); |
| 123 |
assert_eq!(info.nodename().to_string_lossy(), "my-wasi-host"); |
| 124 |
|
| 125 |
// SAFETY: This test runs single-threaded (--test-threads 1) so env mutation is safe. |
| 126 |
unsafe { |
| 127 |
std::env::remove_var("HOSTNAME"); |
| 128 |
std::env::remove_var("COMPUTERNAME"); |
| 129 |
} |
| 130 |
let info = PlatformInfo::new().unwrap(); |
| 131 |
assert_eq!(info.nodename().to_string_lossy(), "localhost"); |
| 132 |
|
| 133 |
// Restore |
| 134 |
// SAFETY: This test runs single-threaded (--test-threads 1) so env mutation is safe. |
| 135 |
unsafe { |
| 136 |
match orig { |
| 137 |
Some(v) => std::env::set_var("HOSTNAME", v), |
| 138 |
None => std::env::remove_var("HOSTNAME"), |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn structure_clone() { |
| 145 |
let info = PlatformInfo::new().unwrap(); |
| 146 |
println!("{info:?}"); |
| 147 |
let info_copy = info.clone(); |
| 148 |
assert_eq!(info_copy, info); |
| 149 |
} |