-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmachine_info_mac.cpp
More file actions
185 lines (157 loc) · 3.91 KB
/
machine_info_mac.cpp
File metadata and controls
185 lines (157 loc) · 3.91 KB
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
180
181
182
183
184
185
#include "machine_info.h"
#include <cstdio>
#include <memory>
#include <string>
#include <regex>
#include <algorithm>
#include <cctype>
std::string exc_value("invalid");
std::string& rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(),
s.rend(),
[](unsigned char ch) { return !std::isspace(ch); }).base(),
s.end());
return s;
}
std::string exec(const char* cmd)
{
char buffer[128];
std::string result = "";
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe)
{
return exc_value;
}
while (!feof(pipe.get()))
{
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
rtrim(result);
if (result.empty())
{
return exc_value;
}
return result;
}
std::string match_regex(std::string const& input,
char const* r_string)
{
std::regex r(r_string);
std::smatch sm;
if (std::regex_search(input, sm, r))
{
return sm[1];
}
return exc_value;
}
bool machine_info_init()
{
return true;
}
bool machine_info_free()
{
return false;
}
std::string machine_info_uuid()
{
return exec("ioreg -rd1 -c IOPlatformExpertDevice "
"| awk '/IOPlatformUUID/ { gsub(/\"/, \"\", $3); print $3 }'");
}
std::string machine_info_model()
{
return exec("sysctl -n hw.model");
}
std::string machine_info_manufacturer()
{
return std::string("Apple Inc.");
}
// system_profiler SPDisplaysDataType can take several seconds; cache its
// output so the width and height accessors share a single invocation.
static std::string const& display_data()
{
static std::string const data = exec("system_profiler SPDisplaysDataType");
return data;
}
std::string machine_info_display_width()
{
return match_regex(display_data(), "Resolution:\\s+(\\d+)\\s+x\\s+\\d+");
}
std::string machine_info_display_height()
{
return match_regex(display_data(), "Resolution:\\s+\\d+\\s+x\\s+(\\d+)");
}
std::string machine_info_memory_serial0()
{
return exc_value;
}
std::string machine_info_motherboard_vendor()
{
return std::string("Apple Inc.");
}
std::string machine_info_motherboard_name()
{
return exec("sysctl -n hw.model");
}
std::string machine_info_disks_controllerid()
{
return exc_value;
}
std::string machine_info_disks_vserial()
{
// Anchor to the exact "UUID" key — ioreg output for IOMedia can
// include other keys containing the substring UUID (e.g.
// "VolGroupUUID", "Content Hint UUID"), and an unanchored /UUID/
// would match those first on any machine where they appear above
// the plain "UUID" line.
return exec("ioreg -rd1 -c IOMedia "
"| awk '/\"UUID\" =/ { gsub(/\"/, \"\", $3); print $3; exit }'");
}
std::string machine_info_bios_manufacturer()
{
return std::string("Apple Inc.");
}
std::string machine_info_bios_smbbversion()
{
return machine_info_bios_version();
}
std::string machine_info_bios_serial()
{
return exec("ioreg -rd1 -c IOPlatformExpertDevice "
"| awk '/IOPlatformSerialNumber/ { gsub(/\"/, \"\", $3); print $3 }'");
}
std::string machine_info_bios_description()
{
return exc_value;
}
std::string machine_info_bios_date()
{
return exc_value;
}
std::string machine_info_bios_version()
{
// Apple Silicon has no BIOS; the equivalent is the BootROM /
// System Firmware Version. This is stable across macOS updates
// (only changes on firmware updates), so it gives a durable
// fingerprint contribution — unlike kern.osrelease, which changes
// with every OS point release.
return exec("system_profiler SPHardwareDataType "
"| awk -F': ' '/System Firmware Version/ {print $2; exit}'");
}
std::string machine_info_processor_name()
{
return exec("sysctl -n machdep.cpu.brand_string");
}
std::string machine_info_processor_id()
{
return exc_value;
}
std::string machine_info_os_type()
{
return exec("uname -s");
}
std::string machine_info_os_version()
{
return exec("sw_vers -productVersion");
}