From 9c33ad84278353286bbb4118cd11ca3fcfec439a Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周六, 31 10月 2020 23:36:25 +0800 Subject: [PATCH] codacy cleanup --- src/library/os/linux/execution_environment.cpp | 56 +++++++++++++++++++++++--------------------------------- 1 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/library/os/linux/execution_environment.cpp b/src/library/os/linux/execution_environment.cpp index 9aad864..1ca0cd0 100644 --- a/src/library/os/linux/execution_environment.cpp +++ b/src/library/os/linux/execution_environment.cpp @@ -1,9 +1,10 @@ /* - * virtualization.cpp + * execution_environment.cpp * * Created on: Dec 15, 2019 * Author: GC */ +#define __STDC_WANT_LIB_EXT1__1 #include <paths.h> #include <sys/stat.h> #include <unistd.h> @@ -18,43 +19,41 @@ #include "../cpu_info.hpp" #include "../execution_environment.hpp" #include "../../base/file_utils.hpp" +#include "../../base/string_utils.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}; char proc_path[MAX_PATH], pidStr[64]; pid_t pid = getpid(); - sprintf(pidStr, "%d", pid); - strcpy(proc_path, "/proc/"); - strcat(proc_path, pidStr); - strcat(proc_path, "/cgroup"); + snprintf(pidStr, sizeof(pidStr), "%d", pid); + strncpy(proc_path, "/proc/", sizeof(proc_path)); + strncat(proc_path, pidStr, sizeof(proc_path)); + strncpy(proc_path, "/cgroup", sizeof(proc_path)); FILE *fp; 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) { - // line[len]=0; - // 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; } } @@ -64,17 +63,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; } } @@ -82,24 +81,15 @@ return result; } -ExecutionEnvironment::ExecutionEnvironment() { - try { - bios_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256); - } catch (...) { +static CONTAINER_TYPE get_container_type() { + CONTAINER_TYPE result = checkContainerProc(); + if (result == CONTAINER_TYPE::NONE) { + result = checkSystemdContainer(); } - try { - bios_description = get_file_contents("/sys/class/dmi/id/modalias", 256); - } catch (...) { - } - try { - sys_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256); - } catch (...) { - } + return result; } -bool ExecutionEnvironment::is_container() const { return (checkContainerProc() != 0 || checkSystemdContainer() != 0); } - -bool ExecutionEnvironment::is_docker() const { return (checkContainerProc() == 1 || checkSystemdContainer() == 1); } +ExecutionEnvironment::ExecutionEnvironment() : m_container_type(get_container_type()) {} } // namespace os } // namespace license -- Gitblit v1.9.1