diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2022-11-04 04:00:53 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-04 04:00:53 +0900 |
commit | 6fe9428805f80222daaf4ccdf9995f2e53b511ff (patch) | |
tree | 0a6c26a8b90c305dcc3459a8b786e228cb0d3ee8 /runtime/ops/os/sys_info.rs | |
parent | dae3940519d626ddfeb954e3f3d7ebe8b83067bf (diff) |
fix(runtime): fix Deno.hostname on windows (#16530)
Diffstat (limited to 'runtime/ops/os/sys_info.rs')
-rw-r--r-- | runtime/ops/os/sys_info.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/runtime/ops/os/sys_info.rs b/runtime/ops/os/sys_info.rs index 4515d8148..f4bc6a9c6 100644 --- a/runtime/ops/os/sys_info.rs +++ b/runtime/ops/os/sys_info.rs @@ -1,4 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +#[cfg(target_family = "windows")] +use std::sync::Once; type LoadAvg = (f64, f64, f64); const DEFAULT_LOADAVG: LoadAvg = (0.0, 0.0, 0.0); @@ -112,6 +114,9 @@ pub fn os_release() -> String { } } +#[cfg(target_family = "windows")] +static WINSOCKET_INIT: Once = Once::new(); + pub fn hostname() -> String { #[cfg(target_family = "unix")] // SAFETY: `sysconf` returns a system constant. @@ -131,13 +136,25 @@ pub fn hostname() -> String { #[cfg(target_family = "windows")] { use std::ffi::OsString; + use std::mem; use std::os::windows::ffi::OsStringExt; + use winapi::shared::minwindef::MAKEWORD; use winapi::um::winsock2::GetHostNameW; + use winapi::um::winsock2::WSAStartup; let namelen = 256; let mut name: Vec<u16> = vec![0u16; namelen]; + // Start winsock to make `GetHostNameW` work correctly + // https://github.com/retep998/winapi-rs/issues/296 + WINSOCKET_INIT.call_once(|| unsafe { + let mut data = mem::zeroed(); + let wsa_startup_result = WSAStartup(MAKEWORD(2, 2), &mut data); + if wsa_startup_result != 0 { + panic!("Failed to start winsocket"); + } + }); let err = - // SAFETY: length of wide string is 256 chars or less. + // SAFETY: length of wide string is 256 chars or less. // https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-gethostnamew unsafe { GetHostNameW(name.as_mut_ptr(), namelen as libc::c_int) }; |