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 "compat.hpp"
9#include "system.hpp"
10#if defined(_WIN32)
11#include "windows.h"
12#include <stdlib.h>
13#include <tchar.h>
14#else
15#include <sys/utsname.h>
16#endif
17#include <memory>
18
19#if defined(__GNUC__)
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wexit-time-destructors"
22#endif
23
24namespace stdex
25{
29 enum class platform_id : uint16_t {
30#ifdef _WIN32
31 unknown = IMAGE_FILE_MACHINE_UNKNOWN,
32 i386 = IMAGE_FILE_MACHINE_I386,
33 x86_64 = IMAGE_FILE_MACHINE_AMD64,
34 arm = IMAGE_FILE_MACHINE_ARMNT,
35 aarch64 = IMAGE_FILE_MACHINE_ARM64,
36#else
37 unknown = 0,
38 i386 = 0x014c,
39 x86_64 = 0x8664,
40 arm = 0x01c4,
41 aarch64 = 0xaa64,
42#endif
43 };
44
52 inline platform_id platform_from_name(_In_z_ const char* name)
53 {
54 struct platform_less {
55 bool operator()(_In_z_ const char* a, _In_z_ const char* b) const
56 {
57 return stricmp(a, b) < 0;
58 }
59 };
60 static const std::map<const char*, platform_id, platform_less> platforms = {
61 { "aarch64", platform_id::aarch64 },
62 { "arm", platform_id::arm },
63 { "i386", platform_id::i386 },
64 { "x86_64", platform_id::x86_64 },
65 };
66 if (auto el = platforms.find(name); el != platforms.end())
67 return el->second;
68 return platform_id::unknown;
69 }
70
74 inline const struct sys_info_t
75 {
79#if _M_IX86 || __i386__
80 static constexpr platform_id process_platform = platform_id::i386;
81#elif _M_X64 /* _M_ARM64EC is introducing as x64 */ || __x86_64__
82 static constexpr platform_id process_platform = platform_id::x86_64;
83#elif _M_ARM || __arm__
84 static constexpr platform_id process_platform = platform_id::arm;
85#elif _M_ARM64 || __aarch64__
86 static constexpr platform_id process_platform = platform_id::aarch64;
87#else
88 #error Unknown platform
89#endif
90
94 platform_id os_platform;
95
96#ifdef _WIN32
100 bool wow64;
101#endif
102
107
111 bool admin;
112
117
118 sys_info_t() :
119 os_platform(platform_id::unknown),
120#ifdef _WIN32
121 wow64(false),
122#endif
124 admin(false),
125 elevated(false)
126 {
127#ifdef _WIN32
128 HMODULE kernel32_handle;
129 kernel32_handle = LoadLibrary(_T("kernel32.dll"));
130 _Assume_(kernel32_handle);
131 BOOL(WINAPI * IsWow64Process2)(HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
132 *reinterpret_cast<FARPROC*>(&IsWow64Process2) = GetProcAddress(kernel32_handle, "IsWow64Process2");
133 HANDLE process = GetCurrentProcess();
134 USHORT process_machine;
135#ifndef _WIN64
136 BOOL Wow64Process;
137#endif
138 if (IsWow64Process2 && IsWow64Process2(process, &process_machine, reinterpret_cast<USHORT*>(&os_platform))) {
139 wow64 = process_machine != IMAGE_FILE_MACHINE_UNKNOWN;
140 }
141#ifdef _WIN64
142 else {
143 os_platform = process_platform;
144 wow64 = false;
145 }
146#else
147 else if (IsWow64Process(process, &Wow64Process)) {
148 if (Wow64Process) {
149 os_platform = platform_id::x86_64;
150 wow64 = true;
151 }
152 else {
153 os_platform = process_platform;
154 wow64 = false;
155 }
156 }
157#endif
158 FreeLibrary(kernel32_handle);
159#else
160 memset(&m_utsn, 0, sizeof(m_utsn));
161 if (uname(&m_utsn) != -1)
162 os_platform = platform_from_name(m_utsn.machine);
163#endif
164
165#ifdef _WIN32
166 HWINSTA hWinSta = GetProcessWindowStation();
167 if (hWinSta) {
168 TCHAR sName[MAX_PATH];
169 if (GetUserObjectInformation(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
170 sName[_countof(sName) - 1] = 0;
171 // Only "WinSta0" is interactive (Source: KB171890)
172 interactive_process = _tcsicmp(sName, _T("WinSta0")) == 0;
173 }
174 }
175#else
176 // TODO: Research interactive process vs service/agent/daemon on this platform.
177#endif
178
179#if defined(_WIN32)
180 {
181 HANDLE token_h;
182 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token_h)) {
183 sys_object token(token_h);
184
185 TOKEN_ELEVATION elevation;
186 DWORD size = sizeof(TOKEN_ELEVATION);
187 if (GetTokenInformation(token_h, TokenElevation, &elevation, sizeof(elevation), &size))
188 elevated = elevation.TokenIsElevated;
189
190 GetTokenInformation(token.get(), TokenGroups, NULL, 0, &size);
191 std::unique_ptr<TOKEN_GROUPS> groups((TOKEN_GROUPS*)new uint8_t[size]);
192 if (GetTokenInformation(token.get(), TokenGroups, (LPVOID)groups.get(), size, &size)) {
193 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
194 PSID sid_admins_h = NULL;
195 if (AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid_admins_h)) {
196 struct SID_delete { void operator()(_In_ PSID p) const { FreeSid(p); } };
197 std::unique_ptr<void, SID_delete> sid_admins(sid_admins_h);
198 for (DWORD i = 0; i < groups->GroupCount; ++i)
199 if (EqualSid(sid_admins.get(), groups->Groups[i].Sid)) {
200 admin = true;
201 break;
202 }
203 }
204 }
205 }
206 }
207#elif defined(__APPLE__)
208 {
209 gid_t gids[NGROUPS_MAX];
210 for (int i = 0, n = getgroups(NGROUPS_MAX, gids); i < n; ++i) {
211 struct group* group = getgrgid(gids[i]);
212 if (!group) continue;
213 if (strcmp(group->gr_name, "admin") == 0) {
214 admin = true;
215 break;
216 }
217 }
218 }
219
220 elevated = geteuid() == 0;
221#else
222 // TODO: Set admin.
223 elevated = geteuid() == 0;
224#endif
225 }
226
230 static bool is_screen_reader()
231 {
232#ifdef _WIN32
233 BOOL b;
234 return SystemParametersInfo(SPI_GETSCREENREADER, 0, &b, 0) && b;
235#else
236 return false;
237#endif
238 }
239
240 protected:
241#ifndef _WIN32
242 struct utsname m_utsn;
243#endif
244 } sys_info;
245}
246
247#if defined(__GNUC__)
248#pragma GCC diagnostic pop
249#endif
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:99
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:156
System information.
Definition sys_info.hpp:75
bool admin
Is member of local group Administrators (Windows) or member of group wheel/sudoers (others)?
Definition sys_info.hpp:111
bool elevated
Is elevated process (Windows) or running as root (others)?
Definition sys_info.hpp:116
platform_id os_platform
The platform this process was compiled for.
Definition sys_info.hpp:94
static bool is_screen_reader()
Is screen reader currently active?
Definition sys_info.hpp:230
bool interactive_process
Is interactive process?
Definition sys_info.hpp:106