stdex Additional custom or not Standard C++ covered algorithms

Loading...
Searching...
No Matches
sys_info.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#include "assert.hpp"
9#include "compat.hpp"
10#include "string.hpp"
11#include "system.hpp"
12#if defined(_WIN32)
13#include "windows.h"
14#include <security.h>
15#include <stdlib.h>
16#include <tchar.h>
17#else
18#include <sys/utsname.h>
19#endif
20#include <map>
21#include <memory>
22
23#if defined(__GNUC__)
24#pragma GCC diagnostic push
25#pragma GCC diagnostic ignored "-Wexit-time-destructors"
26#endif
27
28 namespace stdex
29{
33 enum class platform_id : uint16_t {
34#ifdef _WIN32
35 unknown = IMAGE_FILE_MACHINE_UNKNOWN,
36 i386 = IMAGE_FILE_MACHINE_I386,
37 x86_64 = IMAGE_FILE_MACHINE_AMD64,
38 arm = IMAGE_FILE_MACHINE_ARMNT,
39 aarch64 = IMAGE_FILE_MACHINE_ARM64,
40#else
41 unknown = 0,
42 i386 = 0x014c,
43 x86_64 = 0x8664,
44 arm = 0x01c4,
45 aarch64 = 0xaa64,
46#endif
47 };
48
56 inline platform_id platform_from_name(_In_z_ const char* name)
57 {
58 struct platform_less {
59 bool operator()(_In_z_ const char* a, _In_z_ const char* b) const
60 {
61 return stricmp(a, b) < 0;
62 }
63 };
64 static const std::map<const char*, platform_id, platform_less> platforms = {
65 { "aarch64", platform_id::aarch64 },
66 { "arm", platform_id::arm },
67 { "i386", platform_id::i386 },
68 { "x86_64", platform_id::x86_64 },
69 };
70 if (auto el = platforms.find(name); el != platforms.end())
71 return el->second;
72 return platform_id::unknown;
73 }
74
78 inline const struct sys_info_t
79 {
83#if _M_IX86 || __i386__
84 static constexpr platform_id process_platform = platform_id::i386;
85#elif _M_X64 /* _M_ARM64EC is introducing as x64 */ || __x86_64__
86 static constexpr platform_id process_platform = platform_id::x86_64;
87#elif _M_ARM || __arm__
88 static constexpr platform_id process_platform = platform_id::arm;
89#elif _M_ARM64 || __aarch64__
90 static constexpr platform_id process_platform = platform_id::aarch64;
91#else
92 #error Unknown platform
93#endif
94
98 platform_id os_platform;
99
100#ifdef _WIN32
104 bool wow64;
105#endif
106
110 bool interactive_process;
111
115 bool admin;
116
120 bool elevated;
121
125 sstring username;
126
127 sys_info_t() :
128 os_platform(platform_id::unknown),
129#ifdef _WIN32
130 wow64(false),
131#endif
132 interactive_process(true),
133 admin(false),
134 elevated(false)
135 {
136#ifdef _WIN32
137 HMODULE kernel32_handle;
138 kernel32_handle = LoadLibrary(_T("kernel32.dll"));
139 stdex_assert(kernel32_handle);
140 BOOL(WINAPI * IsWow64Process2)(HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
141 *reinterpret_cast<FARPROC*>(&IsWow64Process2) = GetProcAddress(kernel32_handle, "IsWow64Process2");
142 HANDLE process = GetCurrentProcess();
143 USHORT process_machine;
144#ifndef _WIN64
145 BOOL Wow64Process;
146#endif
147 if (IsWow64Process2 && IsWow64Process2(process, &process_machine, reinterpret_cast<USHORT*>(&os_platform))) {
148 wow64 = process_machine != IMAGE_FILE_MACHINE_UNKNOWN;
149 }
150#ifdef _WIN64
151 else {
152 os_platform = process_platform;
153 wow64 = false;
154 }
155#else
156 else if (IsWow64Process(process, &Wow64Process)) {
157 if (Wow64Process) {
158 os_platform = platform_id::x86_64;
159 wow64 = true;
160 }
161 else {
162 os_platform = process_platform;
163 wow64 = false;
164 }
165 }
166#endif
167 FreeLibrary(kernel32_handle);
168#else
169 memset(&m_utsn, 0, sizeof(m_utsn));
170 if (uname(&m_utsn) != -1)
171 os_platform = platform_from_name(m_utsn.machine);
172#endif
173
174#ifdef _WIN32
175 HWINSTA hWinSta = GetProcessWindowStation();
176 if (hWinSta) {
177 TCHAR sName[MAX_PATH];
178 if (GetUserObjectInformation(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
179 sName[_countof(sName) - 1] = 0;
180 // Only "WinSta0" is interactive (Source: KB171890)
181 interactive_process = _tcsicmp(sName, _T("WinSta0")) == 0;
182 }
183 }
184#else
185 // TODO: Research interactive process vs service/agent/daemon on this platform.
186#endif
187
188#if defined(_WIN32)
189 {
190 HANDLE token_h;
191 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token_h)) {
192 sys_object token(token_h);
193
194 TOKEN_ELEVATION elevation;
195 DWORD size = sizeof(TOKEN_ELEVATION);
196 if (GetTokenInformation(token_h, TokenElevation, &elevation, sizeof(elevation), &size))
197 elevated = elevation.TokenIsElevated;
198
199 GetTokenInformation(token.get(), TokenGroups, NULL, 0, &size);
200 std::unique_ptr<TOKEN_GROUPS> groups((TOKEN_GROUPS*)new uint8_t[size]);
201 if (GetTokenInformation(token.get(), TokenGroups, (LPVOID)groups.get(), size, &size)) {
202 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
203 PSID sid_admins_h = NULL;
204 if (AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid_admins_h)) {
205 struct SID_delete { void operator()(_In_ PSID p) const { FreeSid(p); } };
206 std::unique_ptr<void, SID_delete> sid_admins(sid_admins_h);
207 for (DWORD i = 0; i < groups->GroupCount; ++i)
208 if (EqualSid(sid_admins.get(), groups->Groups[i].Sid)) {
209 admin = true;
210 break;
211 }
212 }
213 }
214 }
215 }
216#elif defined(__APPLE__)
217 {
218 gid_t gids[NGROUPS_MAX];
219 for (int i = 0, n = getgroups(NGROUPS_MAX, gids); i < n; ++i) {
220 struct group* group = getgrgid(gids[i]);
221 if (!group) continue;
222 if (strcmp(group->gr_name, "admin") == 0) {
223 admin = true;
224 break;
225 }
226 }
227 }
228
229 elevated = geteuid() == 0;
230#else
231 // TODO: Set admin.
232 elevated = geteuid() == 0;
233#endif
234
235#ifdef _WIN32
236#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL)
237 {
238 TCHAR szStackBuffer[0x100];
239 ULONG ulSize = _countof(szStackBuffer);
240 if (GetUserNameEx(NameSamCompatible, szStackBuffer, &ulSize))
241 username.assign(szStackBuffer, ulSize);
242 if (GetLastError() == ERROR_MORE_DATA) {
243 // Allocate buffer on heap and retry.
244 username.resize(ulSize - 1);
245 if (!GetUserNameEx(NameSamCompatible, &username[0], &ulSize))
246 username.clear();
247 }
248 }
249#endif
250#else
251 {
252 struct passwd *pw = getpwuid(geteuid());
253 if (pw && pw->pw_name)
254 username = pw->pw_name;
255 }
256#endif
257 }
258
262 static bool is_screen_reader()
263 {
264#ifdef _WIN32
265 BOOL b;
266 return SystemParametersInfo(SPI_GETSCREENREADER, 0, &b, 0) && b;
267#else
268 return false;
269#endif
270 }
271
272 protected:
273#ifndef _WIN32
274 struct utsname m_utsn;
275#endif
276 } sys_info;
277}
278
279#if defined(__GNUC__)
280#pragma GCC diagnostic pop
281#endif
stdex::basic_sys_object
Operating system object base class.
Definition system.hpp:142
stdex::basic_sys_object::get
T get() const noexcept
Returns object handle.
Definition system.hpp:199
stdex::sys_info_t
System information.
Definition sys_info.hpp:79
stdex::sys_info_t::admin
bool admin
Is member of local group Administrators (Windows) or member of group wheel/sudoers (others)?
Definition sys_info.hpp:115
stdex::sys_info_t::elevated
bool elevated
Is elevated process (Windows) or running as root (others)?
Definition sys_info.hpp:120
stdex::sys_info_t::os_platform
platform_id os_platform
The platform this process was compiled for.
Definition sys_info.hpp:98
stdex::sys_info_t::is_screen_reader
static bool is_screen_reader()
Is screen reader currently active?
Definition sys_info.hpp:262
stdex::sys_info_t::username
sstring username
Currently signed in user.
Definition sys_info.hpp:125
stdex::sys_info_t::interactive_process
bool interactive_process
Is interactive process?
Definition sys_info.hpp:110