From f778490d8e2d4089ae3acd06064ffea01fffe682 Mon Sep 17 00:00:00 2001
From: gcontini <1121667+gcontini@users.noreply.github.com>
Date: 周三, 14 10月 2020 23:13:20 +0800
Subject: [PATCH] debug logs

---
 src/library/os/linux/execution_environment.cpp |   57 ++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/src/library/os/linux/execution_environment.cpp b/src/library/os/linux/execution_environment.cpp
index 81c5b16..75520eb 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,11 +17,15 @@
 #include "../../base/base.h"
 #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};
@@ -34,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) {
@@ -46,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;
 		}
 	}
 
@@ -58,21 +64,34 @@
 	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 CONTAINER_TYPE checkSystemdContainer() {
+	ifstream systemd_container("/var/run/systemd/container");
+	CONTAINER_TYPE result = CONTAINER_TYPE::NONE;
+	if (systemd_container.good()) {
+		result = CONTAINER_TYPE::DOCKER;
+		for (string line; getline(systemd_container, line);) {
+			if (line.find("docker") != string::npos) {
+				result = CONTAINER_TYPE::DOCKER;
+				break;
+			} else if (line.find("lxc") != string::npos) {
+				result = CONTAINER_TYPE::LXC;
+				break;
+			}
+		}
 	}
 	return result;
 }
+
+static CONTAINER_TYPE get_container_type() {
+	CONTAINER_TYPE result = checkContainerProc();
+	if (result == CONTAINER_TYPE::NONE) {
+		result = checkSystemdContainer();
+	}
+	return result;
+}
+
+ExecutionEnvironment::ExecutionEnvironment() : m_container_type(get_container_type()) {}
+
+}  // namespace os
 }  // namespace license

--
Gitblit v1.9.1