Skip to content

Commit

Permalink
query: system: query lsb-release as backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 10, 2024
1 parent 71b65e5 commit b76baf6
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions src/query/unix/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view, 3> 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<std::string_view, 3> os_paths = { "/etc/os-release", "/usr/lib/os-release", "/usr/share/os-release" };
for (const std::string_view path : os_paths)
{
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit b76baf6

Please sign in to comment.