Explorar o código

#IQRV #comment [Guardian] 监护拉起时清除版本相关信息

gifur %!s(int64=4) %!d(string=hai) anos
pai
achega
ee19e79c42
Modificáronse 1 ficheiros con 114 adicións e 0 borrados
  1. 114 0
      Tool/guardian/guardian.cpp

+ 114 - 0
Tool/guardian/guardian.cpp

@@ -28,6 +28,7 @@
 #include "memutil.h"
 #include <winpr/library.h>
 #include <winpr/synch.h>
+#include <winpr/environment.h>
 
 int epfd;
 struct epoll_event ev;
@@ -52,6 +53,7 @@ struct epoll_event ev;
 #include <iostream>
 #include <fstream>
 #include <string>
+#include <vector>
 #include "GuardianBase.h"
 #include "guardian.h"
 
@@ -399,6 +401,107 @@ static void ComfirmSpShellDead()
     }
 }
 
+inline static std::vector<std::string> Split(std::string str, char splitElem)
+{
+    std::vector<std::string> strs;
+    std::string::size_type pos1, pos2;
+    pos2 = str.find(splitElem);
+    pos1 = 0;
+    while (std::string::npos != pos2) {
+
+        strs.push_back(str.substr(pos1, pos2 - pos1));
+        pos1 = pos2 + 1;
+        pos2 = str.find(splitElem, pos1);
+    }
+    strs.push_back(str.substr(pos1));
+    return strs;
+}
+
+static bool IsSpPathType(const std::string& value)
+{
+
+#if defined(RVC_OS_WIN)
+    const int leastLength = strlen("C:\\version\\1.0.0.0\\");
+#else
+    const int leastLength = strlen("Run/version/1.0.0.0/");
+#endif //RVC_OS_WIN
+
+	std::string path(value.c_str());
+    if (path.empty() || path.size() < leastLength) return false;
+	const std::size_t sionPos = path.find("version");
+	const std::size_t verPos = sionPos + strlen("version\\");
+    if (sionPos == std::string::npos) return false;
+	std::size_t suffixPos = std::string::npos, i;
+	std::size_t dotCnt = 0;
+    bool lastIsNum = false;
+    for (i = verPos; i < path.size() && (path[i] >= '0' && path[i] <= '9' || path[i] == '.'); ++i) {
+        lastIsNum = !(path[i] == '.');
+        if (!lastIsNum) dotCnt++;
+    }
+
+    if (i >= path.size() || dotCnt != 3 || !lastIsNum)
+        return false;
+
+    return true;
+}
+
+static std::string CutSpPathFromPathValue(const char* value, const char* prefix)
+{
+	std::string path(value);
+	std::string result("");
+
+	std::vector<std::string> values = Split(path, ':');
+    if (values.size() <= 0) {
+        return path;
+    }
+
+	for (auto i = values.cbegin(); i != values.cend(); i++) {
+        if (i->find(prefix) != std::string::npos || !IsSpPathType(*i)) {
+            if (!result.empty()) result += ":";
+			result += (*i);
+        }
+	}
+
+    if (!result.empty() && path[path.size() - 1] == ':') {
+        result += ":";
+    }
+
+    return result;
+}
+
+static void ResetEnviromentVars(const char* env, const char* prefix)
+{
+    DWORD size;
+    char* buf;
+	int len = 0;
+    size = GetEnvironmentVariableA(env, NULL, 0);
+	len = size + MAX_PATH * 3;
+    buf = (char*)malloc(len);
+    size = GetEnvironmentVariableA(env, buf, size);
+    std::string newValue = CutSpPathFromPathValue(buf, prefix);
+    if (newValue.empty()) {
+        SetEnvironmentVariableA(env, NULL);
+    } else {
+        strcpy(buf, newValue.c_str());
+        SetEnvironmentVariableA(env, buf);
+    }
+
+#if 1
+	memset(buf, 0, sizeof(buf));
+    size = GetEnvironmentVariableA(env, buf, len);
+	LogToFile(true, true, false, buf, env, size);
+#endif
+
+    FREE(buf);
+}
+
+static void ResetRelateEnviromentVars( const char* prefix)
+{
+	LogSingleMsg(prefix);
+	ResetEnviromentVars("LD_LIBRARY_PATH", prefix);
+    ResetEnviromentVars("PATH", prefix);
+}
+
 #endif //NOT _WIN32
 
 int FrameworkShutdown(bool bUpgrade=false,bool bRestart = true)
@@ -422,6 +525,13 @@ int FrameworkShutdown(bool bUpgrade=false,bool bRestart = true)
 	LogSingleMsg(tmp);
 	if ((pos = strstr(tmp, "/version")) != NULL) {
 		pos[strlen("/version")+1] = '\0';
+
+		ResetRelateEnviromentVars(tmp);
+
+		char path[MAX_PATH] = { '\0' };
+		GetCurrentDirectoryA(MAX_PATH, path);
+		SetCurrentDirectoryA(tmp);
+
 		strcat(tmp, "spexplorer.sh  --restart");
 		LogSingleMsg(tmp);
         tk_process_t* process = NULL;
@@ -437,10 +547,14 @@ int FrameworkShutdown(bool bUpgrade=false,bool bRestart = true)
 			Sleep(1000);
 			g_SpShellPID = GetSpshellProcID();
 			LogToFile(false, true, false, "get spshell proc id", NULL, g_SpShellPID);
+
+			SetCurrentDirectoryA(path);
             return 0;
 		} else {
 			FREE(process);
 			LogSingleMsg("run spexplorer.sh scripts failed!");
+
+			SetCurrentDirectoryA(path);
 			return -1;
 		}
 	}