From 352744a709aec2cb55a3e1706afa16eec726ea87 Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周六, 25 4月 2020 21:47:02 +0800 Subject: [PATCH] try run inspector before tests --- src/library/os/linux/execution_environment.cpp | 46 +++++++++++++++++++--------------------------- 1 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/library/os/linux/execution_environment.cpp b/src/library/os/linux/execution_environment.cpp index bbddd75..a00df29 100644 --- a/src/library/os/linux/execution_environment.cpp +++ b/src/library/os/linux/execution_environment.cpp @@ -17,12 +17,15 @@ #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() { +static CONTAINER_TYPE checkContainerProc() { // in docer /proc/self/cgroups contains the "docker" or "lxc" string // https://stackoverflow.com/questions/23513045/how-to-check-if-a-process-is-running-inside-docker-container char path[MAX_PATH] = {0}; @@ -37,11 +40,11 @@ char *line = NULL; size_t len = 0; ssize_t read; - int result = 0; + CONTAINER_TYPE result = CONTAINER_TYPE::NONE; fp = fopen(proc_path, "r"); if (fp == NULL) { - return 0; + return CONTAINER_TYPE::NONE; } while ((read = getline(&line, &len, fp)) != -1 && result == 0) { @@ -49,10 +52,10 @@ // printf("Retrieved line of length %zu:\n", read); // printf("%s", line); if (strstr(line, "docker") != NULL) { - result = 1; + result = CONTAINER_TYPE::DOCKER; } if (strstr(line, "lxc") != NULL) { - result = 2; + result = CONTAINER_TYPE::LXC; } } @@ -62,17 +65,17 @@ } // 0=NO 1=Docker/2=Lxc -static int checkSystemdContainer() { +static CONTAINER_TYPE checkSystemdContainer() { ifstream systemd_container("/var/run/systemd/container"); - int result = 0; + CONTAINER_TYPE result = CONTAINER_TYPE::NONE; if (systemd_container.good()) { - result = 1; + result = CONTAINER_TYPE::DOCKER; for (string line; getline(systemd_container, line);) { if (line.find("docker") != string::npos) { - result = 1; + result = CONTAINER_TYPE::DOCKER; break; } else if (line.find("lxc") != string::npos) { - result = 2; + result = CONTAINER_TYPE::LXC; break; } } @@ -80,26 +83,15 @@ return result; } -VIRTUALIZATION ExecutionEnvironment::getVirtualization() { - VIRTUALIZATION result; - CpuInfo cpuInfo; - bool isContainer = checkContainerProc() != 0 || checkSystemdContainer() != 0; - if (isContainer) { - result = CONTAINER; - } else if (cpuInfo.cpu_virtual() || is_cloud()) { - result = VM; - } else { - result = NONE; +static CONTAINER_TYPE get_container_type() { + CONTAINER_TYPE result = checkContainerProc(); + if (result == CONTAINER_TYPE::NONE) { + result = checkSystemdContainer(); } return result; } -bool ExecutionEnvironment::is_cloud() { return getCloudProvider() != ON_PREMISE; } +ExecutionEnvironment::ExecutionEnvironment() : m_container_type(get_container_type()) {} -bool ExecutionEnvironment::is_docker() { return (checkContainerProc() == 1 || checkSystemdContainer() == 1); } - -CLOUD_PROVIDER ExecutionEnvironment::getCloudProvider() { - // TODO -} - +} // namespace os } // namespace license -- Gitblit v1.9.1