2016-11-20 11:32:27 +00:00
|
|
|
#include "sys.h"
|
|
|
|
|
2018-03-11 22:00:54 +00:00
|
|
|
#include "distroutils.h"
|
|
|
|
|
2016-11-20 11:32:27 +00:00
|
|
|
#include <sys/utsname.h>
|
2016-11-21 23:57:15 +00:00
|
|
|
#include <fstream>
|
2021-04-29 03:42:49 +01:00
|
|
|
#include <limits>
|
2016-11-20 11:32:27 +00:00
|
|
|
|
2021-09-04 20:27:09 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2021-09-04 21:10:57 +01:00
|
|
|
#include <QDebug>
|
2021-09-04 20:27:09 +01:00
|
|
|
|
2016-11-26 01:18:05 +00:00
|
|
|
Sys::KernelInfo Sys::getKernelInfo()
|
2016-11-20 11:32:27 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
Sys::KernelInfo out;
|
|
|
|
struct utsname buf;
|
|
|
|
uname(&buf);
|
2021-09-04 20:27:09 +01:00
|
|
|
// NOTE: we assume linux here. this needs further elaboration
|
|
|
|
out.kernelType = KernelType::Linux;
|
2018-07-15 13:51:05 +01:00
|
|
|
out.kernelName = buf.sysname;
|
2021-09-04 20:27:09 +01:00
|
|
|
QString release = out.kernelVersion = buf.release;
|
|
|
|
|
|
|
|
// linux binary running on WSL is cursed.
|
|
|
|
out.isCursed = release.contains("WSL", Qt::CaseInsensitive) || release.contains("Microsoft", Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
out.kernelMajor = 0;
|
|
|
|
out.kernelMinor = 0;
|
|
|
|
out.kernelPatch = 0;
|
|
|
|
auto sections = release.split('-');
|
|
|
|
if(sections.size() >= 1) {
|
|
|
|
auto versionParts = sections[0].split('.');
|
2021-09-04 21:10:57 +01:00
|
|
|
if(versionParts.size() >= 3) {
|
2021-09-04 21:18:29 +01:00
|
|
|
out.kernelMajor = versionParts[0].toInt();
|
|
|
|
out.kernelMinor = versionParts[1].toInt();
|
|
|
|
out.kernelPatch = versionParts[2].toInt();
|
2021-09-04 20:27:09 +01:00
|
|
|
}
|
2021-09-04 21:10:57 +01:00
|
|
|
else {
|
|
|
|
qWarning() << "Not enough version numbers in " << sections[0] << " found " << versionParts.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qWarning() << "Not enough '-' sections in " << release << " found " << sections.size();
|
2021-09-04 20:27:09 +01:00
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
return out;
|
2016-11-20 11:32:27 +00:00
|
|
|
}
|
2016-11-21 23:57:15 +00:00
|
|
|
|
|
|
|
uint64_t Sys::getSystemRam()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
std::string token;
|
2021-12-12 00:35:46 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
2018-07-15 13:51:05 +01:00
|
|
|
std::ifstream file("/proc/meminfo");
|
|
|
|
while(file >> token)
|
|
|
|
{
|
|
|
|
if(token == "MemTotal:")
|
|
|
|
{
|
|
|
|
uint64_t mem;
|
|
|
|
if(file >> mem)
|
|
|
|
{
|
|
|
|
return mem * 1024ull;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ignore rest of the line
|
|
|
|
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
|
|
}
|
2021-12-12 00:35:46 +00:00
|
|
|
#elif defined(Q_OS_FREEBSD)
|
|
|
|
char buff[512];
|
|
|
|
FILE *fp = popen("sysctl hw.physmem", "r");
|
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
while(fgets(buff, 512, fp) != NULL)
|
|
|
|
{
|
|
|
|
std::string str(buff);
|
|
|
|
uint64_t mem = std::stoull(str.substr(12, std::string::npos));
|
|
|
|
return mem * 1024ull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-15 13:51:05 +01:00
|
|
|
return 0; // nothing found
|
2016-11-21 23:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sys::isCPU64bit()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return isSystem64bit();
|
2016-11-21 23:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sys::isSystem64bit()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
// kernel build arch on linux
|
|
|
|
return QSysInfo::currentCpuArchitecture() == "x86_64";
|
2016-11-21 23:57:15 +00:00
|
|
|
}
|
2018-03-11 22:00:54 +00:00
|
|
|
|
|
|
|
Sys::DistributionInfo Sys::getDistributionInfo()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
DistributionInfo systemd_info = read_os_release();
|
|
|
|
DistributionInfo lsb_info = read_lsb_release();
|
|
|
|
DistributionInfo legacy_info = read_legacy_release();
|
|
|
|
DistributionInfo result = systemd_info + lsb_info + legacy_info;
|
|
|
|
if(result.distributionName.isNull())
|
|
|
|
{
|
|
|
|
result.distributionName = "unknown";
|
|
|
|
}
|
|
|
|
if(result.distributionVersion.isNull())
|
|
|
|
{
|
|
|
|
if(result.distributionName == "arch")
|
|
|
|
{
|
|
|
|
result.distributionVersion = "rolling";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.distributionVersion = "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-03-11 22:00:54 +00:00
|
|
|
}
|