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