diff --git a/src/query/unix/system.cpp b/src/query/unix/system.cpp index c5dd5fb..30d49a7 100644 --- a/src/query/unix/system.cpp +++ b/src/query/unix/system.cpp @@ -46,12 +46,53 @@ static void get_host_paths(System::System_t& paths) paths.host_version.pop_back(); } -static System::System_t get_system_infos() +static System::System_t get_system_infos_lsb_releases() { System::System_t ret; debug("calling in System {}", __PRETTY_FUNCTION__); - std::string_view os_release_path; + std::string lsb_release_path; + constexpr std::array lsb_paths = { "/etc/lsb-release", "/usr/lib/lsb-release" }; + for (const std::string_view path : lsb_paths) + { + if (std::filesystem::exists(path)) + { + lsb_release_path = path; + break; + } + } + + std::ifstream os_release_file(lsb_release_path, std::ios::in); + if (!os_release_file.is_open()) + { + error("Failed to get OS infos", lsb_release_path); + return ret; + } + + // get OS /etc/lsb-release infos + u_short iter_index = 0; + std::string line; + while (std::getline(os_release_file, line) && iter_index < 3) + { + if (hasStart(line, "DISTRIB_DESCRIPTION=")) + getFileValue(iter_index, line, ret.os_pretty_name, "DISTRIB_DESCRIPTION="_len); + + else if (hasStart(line, "DISTRIB_ID=")) + getFileValue(iter_index, line, ret.os_id, "DISTRIB_ID="_len); + + else if (hasStart(line, "DISTRIB_CODENAME=")) + getFileValue(iter_index, line, ret.os_version_codename, "DISTRIB_CODENAME="_len); + } + + return ret; +} + +static System::System_t get_system_infos_os_releases() +{ + System::System_t ret; + + debug("calling in System {}", __PRETTY_FUNCTION__); + std::string os_release_path; constexpr std::array os_paths = { "/etc/os-release", "/usr/lib/os-release", "/usr/share/os-release" }; for (const std::string_view path : os_paths) { @@ -62,10 +103,10 @@ static System::System_t get_system_infos() } } - std::ifstream os_release_file(os_release_path.data()); + std::ifstream os_release_file(os_release_path, std::ios::in); if (!os_release_file.is_open()) { - error("Could not open {}\nFailed to get OS infos", os_release_path); + //error("Could not open '{}'\nFailed to get OS infos", os_release_path); return ret; } @@ -77,16 +118,16 @@ static System::System_t get_system_infos() if (hasStart(line, "PRETTY_NAME=")) getFileValue(iter_index, line, ret.os_pretty_name, "PRETTY_NAME="_len); - if (hasStart(line, "NAME=")) + else if (hasStart(line, "NAME=")) getFileValue(iter_index, line, ret.os_name, "NAME="_len); - if (hasStart(line, "ID=")) + else if (hasStart(line, "ID=")) getFileValue(iter_index, line, ret.os_id, "ID="_len); - if (hasStart(line, "VERSION_ID=")) + else if (hasStart(line, "VERSION_ID=")) getFileValue(iter_index, line, ret.os_version_id, "VERSION_ID="_len); - if (hasStart(line, "VERSION_CODENAME=")) + else if (hasStart(line, "VERSION_CODENAME=")) getFileValue(iter_index, line, ret.os_version_codename, "VERSION_CODENAME="_len); } @@ -103,7 +144,10 @@ System::System() if (sysinfo(&m_sysInfos) != 0) die("sysinfo() failed: {}\nCould not get system infos", strerror(errno)); - m_system_infos = get_system_infos(); + m_system_infos = get_system_infos_os_releases(); + if (m_system_infos.os_name == UNKNOWN || m_system_infos.os_pretty_name == UNKNOWN) + m_system_infos = get_system_infos_lsb_releases(); + get_host_paths(m_system_infos); m_bInit = true; }