From 6524462e924adf2ddd7e86da9df7fda878632bef Mon Sep 17 00:00:00 2001 From: Gabriele Contini <contini.mailing@gmail.com> Date: 周六, 14 3月 2020 10:19:09 +0800 Subject: [PATCH] tests and improvements --- src/library/os/linux/execution_environment.cpp | 65 +++++++++++++++++++++++++------- 1 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/library/os/linux/execution_environment.cpp b/src/library/os/linux/execution_environment.cpp index 81c5b16..1f14d62 100644 --- a/src/library/os/linux/execution_environment.cpp +++ b/src/library/os/linux/execution_environment.cpp @@ -7,6 +7,8 @@ #include <paths.h> #include <sys/stat.h> #include <unistd.h> +#include <fstream> +#include <iostream> #include <stdio.h> #include <string.h> #include <dirent.h> @@ -15,8 +17,12 @@ #include "../../base/base.h" #include "../cpu_info.hpp" #include "../execution_environment.hpp" +#include "../../base/file_utils.hpp" +#include "../../base/StringUtils.h" namespace license { +namespace os { +using namespace std; // 0=NO 1=Docker/2=Lxc static int checkContainerProc() { @@ -58,21 +64,52 @@ return result; } -// 0=NO 1=Docker/Lxc -static int checkLXC() { return (access("/var/run/systemd/container", F_OK) == 0) ? 1 : 0; } - -VIRTUALIZATION ExecutionEnvironment::getVirtualization() { - VIRTUALIZATION result = NONE; - CpuInfo cpuInfo; - int isContainer = checkContainerProc(); - if (isContainer == 1) { - result = CONTAINER; - } else if (isContainer == 2 || checkLXC()) { - result = CONTAINER; - } else if (cpuInfo.cpu_virtual()) { - result = VM; - } else { +// 0=NO 1=Docker/2=Lxc +static int checkSystemdContainer() { + ifstream systemd_container("/var/run/systemd/container"); + int result = 0; + if (systemd_container.good()) { + result = 1; + for (string line; getline(systemd_container, line);) { + if (line.find("docker") != string::npos) { + result = 1; + break; + } else if (line.find("lxc") != string::npos) { + result = 2; + break; + } + } } return result; } + +ExecutionEnvironment::ExecutionEnvironment() { + try { + m_bios_vendor = toupper_copy(trim_copy(get_file_contents("/sys/class/dmi/id/sys_vendor", 256))); + + } catch (...) { + } + try { + m_bios_description = toupper_copy(trim_copy(get_file_contents("/sys/class/dmi/id/modalias", 256))); + char last_char = m_bios_description[m_bios_description.length() - 1]; + if (last_char == '\r' || last_char == '\n') { + m_bios_description = m_bios_description.erase(m_bios_description.length() - 1); + } + } catch (...) { + } + try { + m_sys_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256); + char last_char = m_sys_vendor[m_sys_vendor.length() - 2]; + if (last_char == '\r' || last_char == '\n') { + m_sys_vendor = m_sys_vendor.erase(m_sys_vendor.length() - 1); + } + } catch (...) { + } +} + +bool ExecutionEnvironment::is_container() const { return (checkContainerProc() != 0 || checkSystemdContainer() != 0); } + +bool ExecutionEnvironment::is_docker() const { return (checkContainerProc() == 1 || checkSystemdContainer() == 1); } + +} // namespace os } // namespace license -- Gitblit v1.9.1