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#ifdef _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#ifdef _WIN32
25 typedef uint16_t platform_id;
26#else
27 typedef const char* platform_id;
28#endif
29}
30
31#ifndef _WIN32
32constexpr stdex::platform_id IMAGE_FILE_MACHINE_UNKNOWN = nullptr;
33constexpr stdex::platform_id IMAGE_FILE_MACHINE_I386 = "i386";
34constexpr stdex::platform_id IMAGE_FILE_MACHINE_AMD64 = "x86_64";
35constexpr stdex::platform_id IMAGE_FILE_MACHINE_ARMNT = "arm";
36constexpr stdex::platform_id IMAGE_FILE_MACHINE_ARM64 = "aarch64";
37
38inline bool operator ==(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a == b; }
39inline bool operator !=(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a != b; }
40inline bool operator <(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a == IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) < 0; }
41inline bool operator <=(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) <= 0; }
42inline bool operator >(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return a != IMAGE_FILE_MACHINE_UNKNOWN && b == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) > 0; }
43inline bool operator >=(_In_ const stdex::platform_id a, _In_ const stdex::platform_id b) { return b == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) >= 0; }
44#endif
45
46namespace stdex
47{
51 inline const struct sys_info_t
52 {
56#if _M_IX86
57 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_I386;
58#elif _M_X64 // _M_ARM64EC is introducing as x64
59 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_AMD64;
60#elif _M_ARM
61 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_ARMNT;
62#elif _M_ARM64
63 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_ARM64;
64#elif __i386__
65 static constexpr platform_id process_platform = "i386";
66#elif __x86_64__
67 static constexpr platform_id process_platform = "x86_64";
68#elif __aarch64__
69 static constexpr platform_id process_platform = "aarch64";
70#else
71 #error Unknown platform
72#endif
73
77 platform_id os_platform;
78
79#ifdef _WIN32
83 bool wow64;
84#endif
85
90
94 bool admin;
95
100
101 sys_info_t() :
102 os_platform(IMAGE_FILE_MACHINE_UNKNOWN),
103#ifdef _WIN32
104 wow64(false),
105#endif
107 admin(false),
108 elevated(false)
109 {
110#ifdef _WIN32
111 HMODULE kernel32_handle;
112 kernel32_handle = LoadLibrary(_T("kernel32.dll"));
113 _Assume_(kernel32_handle);
114 BOOL(WINAPI * IsWow64Process2)(HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
115 *reinterpret_cast<FARPROC*>(&IsWow64Process2) = GetProcAddress(kernel32_handle, "IsWow64Process2");
116 HANDLE process = GetCurrentProcess();
117 USHORT process_machine;
118#ifndef _WIN64
119 BOOL Wow64Process;
120#endif
121 if (IsWow64Process2 && IsWow64Process2(process, &process_machine, &os_platform)) {
122 wow64 = process_machine != IMAGE_FILE_MACHINE_UNKNOWN;
123 }
124#ifdef _WIN64
125 else {
126 os_platform = process_platform;
127 wow64 = false;
128 }
129#else
130 else if (IsWow64Process(process, &Wow64Process)) {
131 if (Wow64Process) {
132 os_platform = IMAGE_FILE_MACHINE_AMD64;
133 wow64 = true;
134 }
135 else {
136 os_platform = process_platform;
137 wow64 = false;
138 }
139 }
140#endif
141 FreeLibrary(kernel32_handle);
142#else
143 memset(&m_utsn, 0, sizeof(m_utsn));
144 if (uname(&m_utsn) != -1)
145 os_platform = reinterpret_cast<platform_id>(m_utsn.machine);
146#endif
147
148#ifdef _WIN32
149 HWINSTA hWinSta = GetProcessWindowStation();
150 if (hWinSta) {
151 TCHAR sName[MAX_PATH];
152 if (GetUserObjectInformation(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
153 sName[_countof(sName) - 1] = 0;
154 // Only "WinSta0" is interactive (Source: KB171890)
155 interactive_process = _tcsicmp(sName, _T("WinSta0")) == 0;
156 }
157 }
158#else
159 // TODO: Research interactive process vs service/agent/daemon on this platform.
160#endif
161
162#if defined(_WIN32)
163 {
164 HANDLE token_h;
165 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token_h)) {
166 sys_object token(token_h);
167
168 TOKEN_ELEVATION elevation;
169 DWORD size = sizeof(TOKEN_ELEVATION);
170 if (GetTokenInformation(token_h, TokenElevation, &elevation, sizeof(elevation), &size))
171 elevated = elevation.TokenIsElevated;
172
173 GetTokenInformation(token.get(), TokenGroups, NULL, 0, &size);
174 std::unique_ptr<TOKEN_GROUPS> groups((TOKEN_GROUPS*)new uint8_t[size]);
175 if (GetTokenInformation(token.get(), TokenGroups, (LPVOID)groups.get(), size, &size)) {
176 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
177 PSID sid_admins_h = NULL;
178 if (AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid_admins_h)) {
179 struct SID_delete { void operator()(_In_ PSID p) const { FreeSid(p); } };
180 std::unique_ptr<void, SID_delete> sid_admins(sid_admins_h);
181 for (DWORD i = 0; i < groups->GroupCount; ++i)
182 if (EqualSid(sid_admins.get(), groups->Groups[i].Sid)) {
183 admin = true;
184 break;
185 }
186 }
187 }
188 }
189 }
190#elif defined(__APPLE__)
191 {
192 gid_t gids[NGROUPS + 1]; // A user cannot be member in more than NGROUPS groups, not counting the default group (hence the + 1)
193 for (int i = 0, n = getgroups(_countof(gids), gids); i < n; ++i) {
194 struct group* group = getgrgid(gids[i]);
195 if (!group) continue;
196 if (strcmp(group->gr_name, "admin") == 0) {
197 admin = true;
198 break;
199 }
200 }
201 }
202
203 elevated = geteuid() == 0;
204#else
205 // TODO: Set admin.
206 elevated = geteuid() == 0;
207#endif
208 }
209
213 static bool is_screen_reader()
214 {
215#ifdef _WIN32
216 BOOL b;
217 return SystemParametersInfo(SPI_GETSCREENREADER, 0, &b, 0) && b;
218#else
219 return false;
220#endif
221 }
222
223 protected:
224#ifndef _WIN32
225 struct utsname m_utsn;
226#endif
227 } sys_info;
228}
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:52
bool admin
Is member of local group Administrators (Windows) or member of group wheel/sudoers (others)?
Definition sys_info.hpp:94
bool elevated
Is elevated process (Windows) or running as root (others)?
Definition sys_info.hpp:99
platform_id os_platform
The platform this process was compiled for.
Definition sys_info.hpp:77
static bool is_screen_reader()
Is screen reader currently active?
Definition sys_info.hpp:213
bool interactive_process
Is interactive process?
Definition sys_info.hpp:89