aboutsummaryrefslogtreecommitdiff
path: root/libraries/systeminfo/src/sys_unix.cpp
blob: 303ead1fbb7ab9d43758aed7138681e3ef718ee9 (plain)
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
#include "sys.h"

#include "distroutils.h"

#include <sys/utsname.h>
#include <fstream>
#include <limits>

#include <QString>
#include <QStringList>

Sys::KernelInfo Sys::getKernelInfo()
{
    Sys::KernelInfo out;
    struct utsname buf;
    uname(&buf);
    // NOTE: we assume linux here. this needs further elaboration
    out.kernelType = KernelType::Linux;
    out.kernelName = buf.sysname;
    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('.');
        if(sections.size() >= 3) {
            out.kernelMajor = sections[0].toInt();
            out.kernelMinor = sections[1].toInt();
            out.kernelPatch = sections[2].toInt();
        }
    }
    return out;
}

uint64_t Sys::getSystemRam()
{
    std::string token;
    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');
    }
    return 0; // nothing found
}

bool Sys::isCPU64bit()
{
    return isSystem64bit();
}

bool Sys::isSystem64bit()
{
    // kernel build arch on linux
    return QSysInfo::currentCpuArchitecture() == "x86_64";
}

Sys::DistributionInfo Sys::getDistributionInfo()
{
    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;
}