-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
platform.rs
179 lines (155 loc) 路 6.09 KB
/
platform.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::ffi::{CStr, OsString};
use std::fmt;
use std::io;
use std::mem;
use std::os::windows::ffi::OsStringExt;
use winapi::shared::{minwindef, ntdef, ntstatus};
use winapi::um::{libloaderapi, sysinfoapi, winbase, winnt};
use heim_common::prelude::*;
use heim_common::sys::windows::get_ntdll;
use crate::Arch;
// TODO: Is not declared in `winapi` crate
// See https://github.com/retep998/winapi-rs/issues/780
const MAX_COMPUTERNAME_LENGTH: minwindef::DWORD = 31;
// Partial copy of the `sysinfoapi::SYSTEM_INFO`,
// because it contains pointers and we need to sent it between threads.
// TODO: It would be better to make `SYSTEM_INFO` Sendable somehow?
#[derive(Debug)]
struct SystemInfo {
processor_arch: minwindef::WORD,
}
impl From<sysinfoapi::SYSTEM_INFO> for SystemInfo {
fn from(info: sysinfoapi::SYSTEM_INFO) -> SystemInfo {
let s = unsafe { info.u.s() };
SystemInfo {
processor_arch: s.wProcessorArchitecture,
}
}
}
pub struct Platform {
sysinfo: SystemInfo,
version: winnt::OSVERSIONINFOEXW,
hostname: String,
build: String,
}
impl Platform {
pub fn system(&self) -> &str {
match self.version.wProductType {
winnt::VER_NT_WORKSTATION => "Windows",
winnt::VER_NT_SERVER => "Windows Server",
other => unreachable!("Unknown Windows product type: {}", other),
}
}
pub fn release(&self) -> &str {
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/ns-wdm-_osversioninfoexw#remarks
let major = self.version.dwMajorVersion;
let minor = self.version.dwMinorVersion;
let suite_mask = minwindef::DWORD::from(self.version.wSuiteMask);
let is_workstation = self.version.wProductType == winnt::VER_NT_WORKSTATION;
match (major, minor) {
(10, 0) => "10",
(6, 3) => "8.1",
(6, 2) if is_workstation => "8",
(6, 2) if !is_workstation => "2012",
(6, 1) if is_workstation => "7",
(6, 1) if !is_workstation => "2008 R2",
(6, 0) if is_workstation => "Vista",
(6, 0) if !is_workstation => "2008",
(5, 2) if suite_mask == winnt::VER_SUITE_WH_SERVER => "Home Server",
(5, 2) if is_workstation => "XP Professional x64 Edition",
(5, 2) => "2003",
(5, 1) => "XP",
(5, 0) => "2000",
_ => "unknown",
}
}
pub fn version(&self) -> &str {
self.build.as_str()
}
pub fn hostname(&self) -> &str {
self.hostname.as_str()
}
pub fn architecture(&self) -> Arch {
match self.sysinfo.processor_arch {
// While there are other `PROCESSOR_ARCHITECTURE_*` consts exists,
// MSDN described only the following.
// https://docs.microsoft.com/ru-ru/windows/desktop/api/sysinfoapi/ns-sysinfoapi-_system_info#members
winnt::PROCESSOR_ARCHITECTURE_AMD64 => Arch::X86_64,
winnt::PROCESSOR_ARCHITECTURE_ARM => Arch::ARM,
winnt::PROCESSOR_ARCHITECTURE_ARM64 => Arch::AARCH64,
// TODO: Is it okay to match Ia64 to unknown arch?
// `platforms::Arch` enum does not have specific member for Itanium.
winnt::PROCESSOR_ARCHITECTURE_IA64 => Arch::Unknown,
winnt::PROCESSOR_ARCHITECTURE_INTEL => Arch::X86,
_ => Arch::Unknown,
}
}
}
impl fmt::Debug for Platform {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Platform")
.field("system", &self.system())
.field("release", &self.release())
.field("version", &self.version())
.field("hostname", &self.hostname())
.field("architecture", &self.architecture())
.finish()
}
}
fn get_native_system_info() -> SystemInfo {
let mut info = mem::MaybeUninit::<sysinfoapi::SYSTEM_INFO>::uninit();
unsafe {
sysinfoapi::GetNativeSystemInfo(info.as_mut_ptr());
info.assume_init().into()
}
}
fn rtl_get_version() -> Result<winnt::OSVERSIONINFOEXW> {
unsafe {
// Based on the `platform-info` crate source:
// https://github.com/uutils/platform-info/blob/8fa071f764d55bd8e41a96cf42009da9ae20a650/src/windows.rs
// TODO: Use `ntapi` crate instead
let module = get_ntdll()?;
let funcname = CStr::from_bytes_with_nul_unchecked(b"RtlGetVersion\0");
let func = libloaderapi::GetProcAddress(module, funcname.as_ptr());
if !func.is_null() {
let func: extern "stdcall" fn(*mut winnt::RTL_OSVERSIONINFOEXW) -> ntdef::NTSTATUS =
mem::transmute(func as *const ());
let mut osinfo = mem::MaybeUninit::<winnt::RTL_OSVERSIONINFOEXW>::uninit();
(*osinfo.as_mut_ptr()).dwOSVersionInfoSize =
mem::size_of::<winnt::RTL_OSVERSIONINFOEXW>() as minwindef::DWORD;
if func(osinfo.as_mut_ptr()) == ntstatus::STATUS_SUCCESS {
Ok(osinfo.assume_init())
} else {
// https://docs.microsoft.com/en-us/windows/desktop/devnotes/rtlgetversion#return-value
unreachable!("RtlGetVersion should just work");
}
} else {
Err(io::Error::last_os_error().into())
}
}
}
fn get_computer_name() -> Result<String> {
let mut buffer: Vec<winnt::WCHAR> = Vec::with_capacity((MAX_COMPUTERNAME_LENGTH + 1) as usize);
let mut size: minwindef::DWORD = MAX_COMPUTERNAME_LENGTH + 1;
let result = unsafe { winbase::GetComputerNameW(buffer.as_mut_ptr(), &mut size) };
if result == 0 {
Err(Error::last_os_error())
} else {
unsafe {
buffer.set_len(size as usize + 1);
}
let str = OsString::from_wide(&buffer[..(size as usize)])
.to_string_lossy()
.to_string();
Ok(str)
}
}
pub async fn platform() -> Result<Platform> {
let version = rtl_get_version()?;
Ok(Platform {
sysinfo: get_native_system_info(),
version,
hostname: get_computer_name()?,
build: format!("{}", version.dwBuildNumber),
})
}