Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsysinfo.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2022 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qsysinfo.h"
6
7#include <QtCore/qbytearray.h>
8#include <QtCore/qoperatingsystemversion.h>
9#include <QtCore/qstring.h>
10
11#include <private/qoperatingsystemversion_p.h>
12
13#ifdef Q_OS_UNIX
14# include <sys/utsname.h>
15# include <private/qcore_unix_p.h>
16#endif
17
18#ifdef Q_OS_ANDROID
19#include <QtCore/private/qjnihelpers_p.h>
20#include <qjniobject.h>
21#endif
22
23#if defined(Q_OS_SOLARIS)
24# include <sys/systeminfo.h>
25#endif
26
27#if defined(Q_OS_DARWIN)
28# include "qnamespace.h"
29# include <private/qcore_mac_p.h>
30# if __has_include(<IOKit/IOKitLib.h>)
31# include <IOKit/IOKitLib.h>
32# endif
33#endif
34
35#ifdef Q_OS_BSD4
36# include <sys/sysctl.h>
37#endif
38
39#if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
41# include "private/qwinregistry_p.h"
42# include "qt_windows.h"
43#endif // Q_OS_WIN || Q_OS_CYGWIN
44
45#include "archdetect.cpp"
46
48
49using namespace Qt::StringLiterals;
50
89#if defined(Q_OS_DARWIN)
90
91static const char *osVer_helper(QOperatingSystemVersion version = QOperatingSystemVersion::current())
92{
93#ifdef Q_OS_MACOS
94 if (version.majorVersion() == 13)
95 return "Ventura";
96 if (version.majorVersion() == 12)
97 return "Monterey";
98 // Compare against predefined constant to handle 10.16/11.0
99 if (QOperatingSystemVersion::MacOSBigSur.version().isPrefixOf(version.version()))
100 return "Big Sur";
101 if (version.majorVersion() == 10) {
102 switch (version.minorVersion()) {
103 case 9:
104 return "Mavericks";
105 case 10:
106 return "Yosemite";
107 case 11:
108 return "El Capitan";
109 case 12:
110 return "Sierra";
111 case 13:
112 return "High Sierra";
113 case 14:
114 return "Mojave";
115 case 15:
116 return "Catalina";
117 }
118 }
119 // unknown, future version
120#else
121 Q_UNUSED(version);
122#endif
123 return nullptr;
124}
125
126#elif defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
127
128# ifndef QT_BOOTSTRAPPED
129class QWindowsSockInit
130{
131public:
132 QWindowsSockInit();
133 ~QWindowsSockInit();
134 int version;
135};
136
137QWindowsSockInit::QWindowsSockInit()
138: version(0)
139{
140 //### should we try for 2.2 on all platforms ??
141 WSAData wsadata;
142
143 // IPv6 requires Winsock v2.0 or better.
144 if (WSAStartup(MAKEWORD(2, 0), &wsadata) != 0) {
145 qWarning("QTcpSocketAPI: WinSock v2.0 initialization failed.");
146 } else {
147 version = 0x20;
148 }
149}
150
151QWindowsSockInit::~QWindowsSockInit()
152{
153 WSACleanup();
154}
155Q_GLOBAL_STATIC(QWindowsSockInit, winsockInit)
156# endif // QT_BOOTSTRAPPED
157
158static QString readVersionRegistryString(const wchar_t *subKey)
159{
160 return QWinRegistryKey(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)")
161 .stringValue(subKey);
162}
163
164static inline QString windowsDisplayVersion()
165{
166 // https://tickets.puppetlabs.com/browse/FACT-3058
167 // The "ReleaseId" key stopped updating since Windows 10 20H2.
169 return readVersionRegistryString(L"DisplayVersion");
170 else
171 return readVersionRegistryString(L"ReleaseId");
172}
173
174static QString winSp_helper()
175{
176 const auto osv = qWindowsVersionInfo();
177 const qint16 major = osv.wServicePackMajor;
178 if (major) {
179 QString sp = QStringLiteral("SP ") + QString::number(major);
180 const qint16 minor = osv.wServicePackMinor;
181 if (minor)
182 sp += u'.' + QString::number(minor);
183
184 return sp;
185 }
186 return QString();
187}
188
189static const char *osVer_helper(QOperatingSystemVersion version = QOperatingSystemVersion::current())
190{
191 Q_UNUSED(version);
192 const OSVERSIONINFOEX osver = qWindowsVersionInfo();
193 const bool workstation = osver.wProductType == VER_NT_WORKSTATION;
194
195#define Q_WINVER(major, minor) (major << 8 | minor)
196 switch (Q_WINVER(osver.dwMajorVersion, osver.dwMinorVersion)) {
197 case Q_WINVER(10, 0):
198 if (workstation) {
199 if (osver.dwBuildNumber >= 22000)
200 return "11";
201 return "10";
202 }
203 // else: Server
204 if (osver.dwBuildNumber >= 20348)
205 return "Server 2022";
206 if (osver.dwBuildNumber >= 17763)
207 return "Server 2019";
208 return "Server 2016";
209 }
210#undef Q_WINVER
211 // unknown, future version
212 return nullptr;
213}
214
215#endif
216#if defined(Q_OS_UNIX)
217# if (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || defined(Q_OS_FREEBSD)
218# define USE_ETC_OS_RELEASE
219struct QUnixOSVersion
220{
221 // from /etc/os-release older /etc/lsb-release // redhat /etc/redhat-release // debian /etc/debian_version
222 QString productType; // $ID $DISTRIB_ID // single line file containing: // Debian
223 QString productVersion; // $VERSION_ID $DISTRIB_RELEASE // <Vendor_ID release Version_ID> // single line file <Release_ID/sid>
224 QString prettyName; // $PRETTY_NAME $DISTRIB_DESCRIPTION
225};
226
228{
229 // man os-release says:
230 // Variable assignment values must be enclosed in double
231 // or single quotes if they include spaces, semicolons or
232 // other special characters outside of A–Z, a–z, 0–9. Shell
233 // special characters ("$", quotes, backslash, backtick)
234 // must be escaped with backslashes, following shell style.
235 // All strings should be in UTF-8 format, and non-printable
236 // characters should not be used. It is not supported to
237 // concatenate multiple individually quoted strings.
238 if (str.size() >= 2 && str.front() == '"' && str.back() == '"')
239 str = str.sliced(1).chopped(1);
240 return QString::fromUtf8(str);
241}
242
243static QByteArray getEtcFileContent(const char *filename)
244{
245 // we're avoiding QFile here
246 int fd = qt_safe_open(filename, O_RDONLY);
247 if (fd == -1)
248 return QByteArray();
249
250 QT_STATBUF sbuf;
251 if (QT_FSTAT(fd, &sbuf) == -1) {
253 return QByteArray();
254 }
255
256 QByteArray buffer(sbuf.st_size, Qt::Uninitialized);
257 buffer.resize(qt_safe_read(fd, buffer.data(), sbuf.st_size));
259 return buffer;
260}
261
262static bool readEtcFile(QUnixOSVersion &v, const char *filename,
263 const QByteArray &idKey, const QByteArray &versionKey, const QByteArray &prettyNameKey)
264{
265
266 QByteArray buffer = getEtcFileContent(filename);
267 if (buffer.isEmpty())
268 return false;
269
270 const char *ptr = buffer.constData();
271 const char *end = buffer.constEnd();
272 const char *eol;
274 for (; ptr != end; ptr = eol + 1) {
275 // find the end of the line after ptr
276 eol = static_cast<const char *>(memchr(ptr, '\n', end - ptr));
277 if (!eol)
278 eol = end - 1;
279 line.setRawData(ptr, eol - ptr);
280
281 if (line.startsWith(idKey)) {
282 ptr += idKey.size();
283 v.productType = unquote({ptr, eol});
284 continue;
285 }
286
287 if (line.startsWith(prettyNameKey)) {
288 ptr += prettyNameKey.size();
289 v.prettyName = unquote({ptr, eol});
290 continue;
291 }
292
293 if (line.startsWith(versionKey)) {
294 ptr += versionKey.size();
295 v.productVersion = unquote({ptr, eol});
296 continue;
297 }
298 }
299
300 return true;
301}
302
303static bool readOsRelease(QUnixOSVersion &v)
304{
305 QByteArray id = QByteArrayLiteral("ID=");
306 QByteArray versionId = QByteArrayLiteral("VERSION_ID=");
307 QByteArray prettyName = QByteArrayLiteral("PRETTY_NAME=");
308
309 // man os-release(5) says:
310 // The file /etc/os-release takes precedence over /usr/lib/os-release.
311 // Applications should check for the former, and exclusively use its data
312 // if it exists, and only fall back to /usr/lib/os-release if it is
313 // missing.
314 return readEtcFile(v, "/etc/os-release", id, versionId, prettyName) ||
315 readEtcFile(v, "/usr/lib/os-release", id, versionId, prettyName);
316}
317
318static bool readEtcLsbRelease(QUnixOSVersion &v)
319{
320 bool ok = readEtcFile(v, "/etc/lsb-release", QByteArrayLiteral("DISTRIB_ID="),
321 QByteArrayLiteral("DISTRIB_RELEASE="), QByteArrayLiteral("DISTRIB_DESCRIPTION="));
322 if (ok && (v.prettyName.isEmpty() || v.prettyName == v.productType)) {
323 // some distributions have redundant information for the pretty name,
324 // so try /etc/<lowercasename>-release
325
326 // we're still avoiding QFile here
327 QByteArray distrorelease = "/etc/" + v.productType.toLatin1().toLower() + "-release";
328 int fd = qt_safe_open(distrorelease, O_RDONLY);
329 if (fd != -1) {
330 QT_STATBUF sbuf;
331 if (QT_FSTAT(fd, &sbuf) != -1 && sbuf.st_size > v.prettyName.size()) {
332 // file apparently contains interesting information
333 QByteArray buffer(sbuf.st_size, Qt::Uninitialized);
334 buffer.resize(qt_safe_read(fd, buffer.data(), sbuf.st_size));
335 v.prettyName = QString::fromLatin1(buffer.trimmed());
336 }
338 }
339 }
340
341 // some distributions have a /etc/lsb-release file that does not provide the values
342 // we are looking for, i.e. DISTRIB_ID, DISTRIB_RELEASE and DISTRIB_DESCRIPTION.
343 // Assuming that neither DISTRIB_ID nor DISTRIB_RELEASE were found, or contained valid values,
344 // returning false for readEtcLsbRelease will allow further /etc/<lowercasename>-release parsing.
345 return ok && !(v.productType.isEmpty() && v.productVersion.isEmpty());
346}
347
348#if defined(Q_OS_LINUX)
349static QByteArray getEtcFileFirstLine(const char *fileName)
350{
351 QByteArray buffer = getEtcFileContent(fileName);
352 if (buffer.isEmpty())
353 return QByteArray();
354
355 const char *ptr = buffer.constData();
356 return QByteArray(ptr, buffer.indexOf("\n")).trimmed();
357}
358
359static bool readEtcRedHatRelease(QUnixOSVersion &v)
360{
361 // /etc/redhat-release analysed should be a one line file
362 // the format of its content is <Vendor_ID release Version>
363 // i.e. "Red Hat Enterprise Linux Workstation release 6.5 (Santiago)"
364 QByteArray line = getEtcFileFirstLine("/etc/redhat-release");
365 if (line.isEmpty())
366 return false;
367
368 v.prettyName = QString::fromLatin1(line);
369
370 const char keyword[] = "release ";
371 const qsizetype releaseIndex = line.indexOf(keyword);
372 v.productType = QString::fromLatin1(line.mid(0, releaseIndex)).remove(u' ');
373 const qsizetype spaceIndex = line.indexOf(' ', releaseIndex + strlen(keyword));
374 v.productVersion = QString::fromLatin1(line.mid(releaseIndex + strlen(keyword),
375 spaceIndex > -1 ? spaceIndex - releaseIndex - int(strlen(keyword)) : -1));
376 return true;
377}
378
379static bool readEtcDebianVersion(QUnixOSVersion &v)
380{
381 // /etc/debian_version analysed should be a one line file
382 // the format of its content is <Release_ID/sid>
383 // i.e. "jessie/sid"
384 QByteArray line = getEtcFileFirstLine("/etc/debian_version");
385 if (line.isEmpty())
386 return false;
387
388 v.productType = QStringLiteral("Debian");
389 v.productVersion = QString::fromLatin1(line);
390 return true;
391}
392#endif
393
394static bool findUnixOsVersion(QUnixOSVersion &v)
395{
396 if (readOsRelease(v))
397 return true;
398 if (readEtcLsbRelease(v))
399 return true;
400#if defined(Q_OS_LINUX)
401 if (readEtcRedHatRelease(v))
402 return true;
403 if (readEtcDebianVersion(v))
404 return true;
405#endif
406 return false;
407}
408# endif // USE_ETC_OS_RELEASE
409#endif // Q_OS_UNIX
410
411#ifdef Q_OS_ANDROID
412static const char *osVer_helper(QOperatingSystemVersion)
413{
414 // https://source.android.com/source/build-numbers.html
415 // https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
416 const int sdk_int = QtAndroidPrivate::androidSdkVersion();
417 switch (sdk_int) {
418 case 3:
419 return "Cupcake";
420 case 4:
421 return "Donut";
422 case 5:
423 case 6:
424 case 7:
425 return "Eclair";
426 case 8:
427 return "Froyo";
428 case 9:
429 case 10:
430 return "Gingerbread";
431 case 11:
432 case 12:
433 case 13:
434 return "Honeycomb";
435 case 14:
436 case 15:
437 return "Ice Cream Sandwich";
438 case 16:
439 case 17:
440 case 18:
441 return "Jelly Bean";
442 case 19:
443 case 20:
444 return "KitKat";
445 case 21:
446 case 22:
447 return "Lollipop";
448 case 23:
449 return "Marshmallow";
450 case 24:
451 case 25:
452 return "Nougat";
453 case 26:
454 case 27:
455 return "Oreo";
456 case 28:
457 return "Pie";
458 case 29:
459 return "10";
460 case 30:
461 return "11";
462 case 31:
463 return "12";
464 case 32:
465 return "12L";
466 case 33:
467 return "13";
468 default:
469 break;
470 }
471
472 return "";
473}
474#endif
475
510
544{
545#if defined(Q_OS_WIN)
546 // We don't need to catch all the CPU architectures in this function;
547 // only those where the host CPU might be different than the build target
548 // (usually, 64-bit platforms).
549 SYSTEM_INFO info;
550 GetNativeSystemInfo(&info);
551 switch (info.wProcessorArchitecture) {
552# ifdef PROCESSOR_ARCHITECTURE_AMD64
553 case PROCESSOR_ARCHITECTURE_AMD64:
554 return QStringLiteral("x86_64");
555# endif
556# ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
557 case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
558# endif
559 case PROCESSOR_ARCHITECTURE_IA64:
560 return QStringLiteral("ia64");
561 }
562#elif defined(Q_OS_DARWIN) && !defined(Q_OS_MACOS)
563 // iOS-based OSes do not return the architecture on uname(2)'s result.
564 return buildCpuArchitecture();
565#elif defined(Q_OS_UNIX)
566 long ret = -1;
567 struct utsname u;
568
569# if defined(Q_OS_SOLARIS)
570 // We need a special call for Solaris because uname(2) on x86 returns "i86pc" for
571 // both 32- and 64-bit CPUs. Reference:
572 // http://docs.oracle.com/cd/E18752_01/html/816-5167/sysinfo-2.html#REFMAN2sysinfo-2
573 // http://fxr.watson.org/fxr/source/common/syscall/systeminfo.c?v=OPENSOLARIS
574 // http://fxr.watson.org/fxr/source/common/conf/param.c?v=OPENSOLARIS;im=10#L530
575 if (ret == -1)
576 ret = sysinfo(SI_ARCHITECTURE_64, u.machine, sizeof u.machine);
577# endif
578
579 if (ret == -1)
580 ret = uname(&u);
581
582 // we could use detectUnixVersion() above, but we only need a field no other function does
583 if (ret != -1) {
584 // the use of QT_BUILD_INTERNAL here is simply to ensure all branches build
585 // as we don't often build on some of the less common platforms
586# if defined(Q_PROCESSOR_ARM) || defined(QT_BUILD_INTERNAL)
587 if (strcmp(u.machine, "aarch64") == 0)
588 return QStringLiteral("arm64");
589 if (strncmp(u.machine, "armv", 4) == 0)
590 return QStringLiteral("arm");
591# endif
592# if defined(Q_PROCESSOR_POWER) || defined(QT_BUILD_INTERNAL)
593 // harmonize "powerpc" and "ppc" to "power"
594 if (strncmp(u.machine, "ppc", 3) == 0)
595 return "power"_L1 + QLatin1StringView(u.machine + 3);
596 if (strncmp(u.machine, "powerpc", 7) == 0)
597 return "power"_L1 + QLatin1StringView(u.machine + 7);
598 if (strcmp(u.machine, "Power Macintosh") == 0)
599 return "power"_L1;
600# endif
601# if defined(Q_PROCESSOR_SPARC) || defined(QT_BUILD_INTERNAL)
602 // Solaris sysinfo(2) (above) uses "sparcv9", but uname -m says "sun4u";
603 // Linux says "sparc64"
604 if (strcmp(u.machine, "sun4u") == 0 || strcmp(u.machine, "sparc64") == 0)
605 return QStringLiteral("sparcv9");
606 if (strcmp(u.machine, "sparc32") == 0)
607 return QStringLiteral("sparc");
608# endif
609# if defined(Q_PROCESSOR_X86) || defined(QT_BUILD_INTERNAL)
610 // harmonize all "i?86" to "i386"
611 if (strlen(u.machine) == 4 && u.machine[0] == 'i'
612 && u.machine[2] == '8' && u.machine[3] == '6')
613 return QStringLiteral("i386");
614 if (strcmp(u.machine, "amd64") == 0) // Solaris
615 return QStringLiteral("x86_64");
616# endif
617 return QString::fromLatin1(u.machine);
618 }
619#endif
620 return buildCpuArchitecture();
621}
622
658{
659 // ARCH_FULL is a concatenation of strings (incl. ARCH_PROCESSOR), which breaks
660 // QStringLiteral on MSVC. Since the concatenation behavior we want is specified
661 // the same C++11 paper as the Unicode strings, we'll use that macro and hope
662 // that Microsoft implements the new behavior when they add support for Unicode strings.
664}
665
667{
668 return QStringLiteral("unknown");
669}
670
695{
696#if defined(Q_OS_WIN)
697 return QStringLiteral("winnt");
698#elif defined(Q_OS_UNIX)
699 struct utsname u;
700 if (uname(&u) == 0)
701 return QString::fromLatin1(u.sysname).toLower();
702#endif
703 return unknownText();
704}
705
720{
721#ifdef Q_OS_WIN
722 const auto osver = QOperatingSystemVersion::current();
723 return QString::asprintf("%d.%d.%d",
724 osver.majorVersion(), osver.minorVersion(), osver.microVersion());
725#else
726 struct utsname u;
727 if (uname(&u) == 0)
728 return QString::fromLatin1(u.release);
729 return QString();
730#endif
731}
732
733
770{
771 // similar, but not identical to QFileSelectorPrivate::platformSelectors
772#if defined(Q_OS_WIN)
773 return QStringLiteral("windows");
774
775#elif defined(Q_OS_QNX)
776 return QStringLiteral("qnx");
777
778#elif defined(Q_OS_ANDROID)
779 return QStringLiteral("android");
780
781#elif defined(Q_OS_IOS)
782 return QStringLiteral("ios");
783#elif defined(Q_OS_TVOS)
784 return QStringLiteral("tvos");
785#elif defined(Q_OS_WATCHOS)
786 return QStringLiteral("watchos");
787#elif defined(Q_OS_VISIONOS)
788 return QStringLiteral("visionos");
789#elif defined(Q_OS_MACOS)
790 return QStringLiteral("macos");
791#elif defined(Q_OS_DARWIN)
792 return QStringLiteral("darwin");
793#elif defined(Q_OS_WASM)
794 return QStringLiteral("wasm");
795
796#elif defined(USE_ETC_OS_RELEASE) // Q_OS_UNIX
797 QUnixOSVersion unixOsVersion;
798 findUnixOsVersion(unixOsVersion);
799 if (!unixOsVersion.productType.isEmpty())
800 return unixOsVersion.productType;
801#endif
802 return unknownText();
803}
804
840{
841#if defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN)
842 const auto version = QOperatingSystemVersion::current();
843 return QString::asprintf("%d.%d", version.majorVersion(), version.minorVersion());
844#elif defined(Q_OS_WIN)
845 const char *version = osVer_helper();
846 if (version) {
847 const QLatin1Char spaceChar(' ');
848 return QString::fromLatin1(version).remove(spaceChar).toLower() + winSp_helper().remove(spaceChar).toLower();
849 }
850 // fall through
851
852#elif defined(USE_ETC_OS_RELEASE) // Q_OS_UNIX
853 QUnixOSVersion unixOsVersion;
854 findUnixOsVersion(unixOsVersion);
855 if (!unixOsVersion.productVersion.isEmpty())
856 return unixOsVersion.productVersion;
857#endif
858
859 // fallback
860 return unknownText();
861}
862
878{
879#if defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN) || defined(Q_OS_WIN)
880 const auto version = QOperatingSystemVersion::current();
881 const int majorVersion = version.majorVersion();
882 const QString versionString = QString::asprintf("%d.%d", majorVersion, version.minorVersion());
883 QString result = version.name() + u' ';
884 const char *name = osVer_helper(version);
885 if (!name)
886 return result + versionString;
888# if !defined(Q_OS_WIN)
889 return result + " ("_L1 + versionString + u')';
890# else
891 // (resembling winver.exe): Windows 10 "Windows 10 Version 1809"
892 const auto displayVersion = windowsDisplayVersion();
893 if (!displayVersion.isEmpty())
894 result += " Version "_L1 + displayVersion;
895 return result;
896# endif // Windows
897#elif defined(Q_OS_HAIKU)
898 return "Haiku "_L1 + productVersion();
899#elif defined(Q_OS_UNIX)
900# ifdef USE_ETC_OS_RELEASE
901 QUnixOSVersion unixOsVersion;
902 findUnixOsVersion(unixOsVersion);
903 if (!unixOsVersion.prettyName.isEmpty())
904 return unixOsVersion.prettyName;
905# endif
906 struct utsname u;
907 if (uname(&u) == 0)
908 return QString::fromLatin1(u.sysname) + u' ' + QString::fromLatin1(u.release);
909#endif
910 return unknownText();
911}
912
913#ifndef QT_BOOTSTRAPPED
930{
931 // the hostname can change, so we can't cache it
932#if defined(Q_OS_LINUX)
933 // gethostname(3) on Linux just calls uname(2), so do it ourselves
934 // and avoid a memcpy
935 struct utsname u;
936 if (uname(&u) == 0)
937 return QString::fromLocal8Bit(u.nodename);
938 return QString();
939#else
940# ifdef Q_OS_WIN
941 // Important: QtNetwork depends on machineHostName() initializing ws2_32.dll
942 winsockInit();
943 QString hostName;
944 hostName.resize(512);
945 unsigned long len = hostName.size();
946 BOOL res = GetComputerNameEx(ComputerNameDnsHostname,
947 reinterpret_cast<wchar_t *>(const_cast<quint16 *>(hostName.utf16())), &len);
948 if (!res && len > 512) {
949 hostName.resize(len - 1);
950 GetComputerNameEx(ComputerNameDnsHostname,
951 reinterpret_cast<wchar_t *>(const_cast<quint16 *>(hostName.utf16())), &len);
952 }
953 hostName.truncate(len);
954 return hostName;
955# else // !Q_OS_WIN
956
957 char hostName[512];
958 if (gethostname(hostName, sizeof(hostName)) == -1)
959 return QString();
960 hostName[sizeof(hostName) - 1] = '\0';
961 return QString::fromLocal8Bit(hostName);
962# endif
963#endif
964}
965#endif // QT_BOOTSTRAPPED
966
967enum {
968 UuidStringLen = sizeof("00000000-0000-0000-0000-000000000000") - 1
970
994{
995#if defined(Q_OS_DARWIN) && __has_include(<IOKit/IOKitLib.h>)
996 char uuid[UuidStringLen + 1];
997 io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
998 QCFString stringRef = (CFStringRef)IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
999 CFStringGetCString(stringRef, uuid, sizeof(uuid), kCFStringEncodingMacRoman);
1000 return QByteArray(uuid);
1001#elif defined(Q_OS_BSD4) && defined(KERN_HOSTUUID)
1002 char uuid[UuidStringLen + 1];
1003 size_t uuidlen = sizeof(uuid);
1004 int name[] = { CTL_KERN, KERN_HOSTUUID };
1005 if (sysctl(name, sizeof name / sizeof name[0], &uuid, &uuidlen, nullptr, 0) == 0
1006 && uuidlen == sizeof(uuid))
1007 return QByteArray(uuid, uuidlen - 1);
1008#elif defined(Q_OS_UNIX)
1009 // The modern name on Linux is /etc/machine-id, but that path is
1010 // unlikely to exist on non-Linux (non-systemd) systems. The old
1011 // path is more than enough.
1012 static const char fullfilename[] = "/usr/local/var/lib/dbus/machine-id";
1013 const char *firstfilename = fullfilename + sizeof("/usr/local") - 1;
1014 int fd = qt_safe_open(firstfilename, O_RDONLY);
1015 if (fd == -1 && errno == ENOENT)
1016 fd = qt_safe_open(fullfilename, O_RDONLY);
1017
1018 if (fd != -1) {
1019 char buffer[32]; // 128 bits, hex-encoded
1020 qint64 len = qt_safe_read(fd, buffer, sizeof(buffer));
1022
1023 if (len != -1)
1024 return QByteArray(buffer, len);
1025 }
1026#elif defined(Q_OS_WIN)
1027 // Let's poke at the registry
1028 const QString machineGuid = QWinRegistryKey(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Cryptography)")
1029 .stringValue(u"MachineGuid"_s);
1030 if (!machineGuid.isEmpty())
1031 return machineGuid.toLatin1();
1032#endif
1033 return QByteArray();
1034}
1035
1050{
1051#ifdef Q_OS_LINUX
1052 // use low-level API here for simplicity
1053 int fd = qt_safe_open("/proc/sys/kernel/random/boot_id", O_RDONLY);
1054 if (fd != -1) {
1055 char uuid[UuidStringLen];
1056 qint64 len = qt_safe_read(fd, uuid, sizeof(uuid));
1058 if (len == UuidStringLen)
1059 return QByteArray(uuid, UuidStringLen);
1060 }
1061#elif defined(Q_OS_DARWIN)
1062 // "kern.bootsessionuuid" is only available by name
1063 char uuid[UuidStringLen + 1];
1064 size_t uuidlen = sizeof(uuid);
1065 if (sysctlbyname("kern.bootsessionuuid", uuid, &uuidlen, nullptr, 0) == 0
1066 && uuidlen == sizeof(uuid))
1067 return QByteArray(uuid, uuidlen - 1);
1068#endif
1069 return QByteArray();
1070};
1071
#define ARCH_PROCESSOR
#define ARCH_FULL
\inmodule QtCore
Definition qbytearray.h:57
QByteArray toLower() const &
Definition qbytearray.h:254
static Q_CORE_EXPORT QOperatingSystemVersionBase current()
static constexpr QOperatingSystemVersionBase Windows10_20H2
\variable QOperatingSystemVersion::Windows10_20H2
static constexpr QOperatingSystemVersionBase MacOSBigSur
\variable QOperatingSystemVersion::MacOSBigSur
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toLatin1() const &
Definition qstring.h:630
qsizetype indexOf(QLatin1StringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4517
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
QString sliced(qsizetype pos) const &
Definition qstring.h:394
QString chopped(qsizetype n) const &
Definition qstring.h:398
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
Definition qstring.cpp:6995
void truncate(qsizetype pos)
Truncates the string at the given position index.
Definition qstring.cpp:6319
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
QChar back() const
Definition qstring.h:232
QChar front() const
Definition qstring.h:230
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
QString & setRawData(const QChar *unicode, qsizetype size)
Definition qstring.cpp:9501
static QString static QString asprintf(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
Definition qstring.cpp:7263
void resize(qsizetype size)
Sets the size of the string to size characters.
Definition qstring.cpp:2668
static QString currentCpuArchitecture()
Definition qsysinfo.cpp:543
static QByteArray bootUniqueId()
static QString buildAbi()
Definition qsysinfo.cpp:657
static QByteArray machineUniqueId()
Definition qsysinfo.cpp:993
static QString kernelVersion()
Definition qsysinfo.cpp:719
static QString machineHostName()
Definition qsysinfo.cpp:929
static QString productType()
Definition qsysinfo.cpp:769
static QString kernelType()
Definition qsysinfo.cpp:694
static QString buildCpuArchitecture()
Definition qsysinfo.cpp:506
static QString productVersion()
Definition qsysinfo.cpp:839
static QString prettyProductName()
Definition qsysinfo.cpp:877
QString stringValue(QStringView subKey) const
QString str
[2]
Combined button and popup list for selecting options.
Q_CORE_EXPORT jint androidSdkVersion()
constexpr Initialization Uninitialized
QString eol
Definition language.cpp:59
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
static int qt_safe_close(int fd)
static QString unquote(QStringView str)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qWarning
Definition qlogging.h:166
return ret
static ControlElement< T > * ptr(QWidget *widget)
GLsizei const GLfloat * v
[13]
GLuint GLuint end
GLenum GLuint buffer
GLuint64 GLenum GLint fd
GLuint name
GLuint res
GLuint64EXT * result
[6]
GLenum GLsizei len
OSVERSIONINFOEX qWindowsVersionInfo()
#define QStringLiteral(str)
static QString unknownText()
Definition qsysinfo.cpp:666
@ UuidStringLen
Definition qsysinfo.cpp:968
#define sp
#define Q_UNUSED(x)
short qint16
Definition qtypes.h:47
unsigned short quint16
Definition qtypes.h:48
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
if(qFloatDistance(a, b)<(1<< 7))
[0]
QHostInfo info
[0]
\inmodule QtCore \reentrant
Definition qchar.h:18