gcontini
2020-05-02 4bd7da9ff2eb06c11b1f54e2b8e2cfb44af56776
src/library/os/linux/execution_environment.cpp
@@ -18,14 +18,14 @@
#include "../cpu_info.hpp"
#include "../execution_environment.hpp"
#include "../../base/file_utils.hpp"
#include "../../base/StringUtils.h"
#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};
@@ -40,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) {
@@ -52,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;
      }
   }
@@ -65,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;
         }
      }
@@ -83,33 +83,15 @@
   return result;
}
ExecutionEnvironment::ExecutionEnvironment() {
   try {
      m_bios_vendor = toupper_copy(trim_copy(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 {
      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 (...) {
   }
   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