Răsfoiți Sursa

修改部分进程功能

xx 2 ani în urmă
părinte
comite
1d7299786c
2 a modificat fișierele cu 23 adăugiri și 40 ștergeri
  1. 3 9
      include/util/process.h
  2. 20 31
      src/util/process.cpp

+ 3 - 9
include/util/process.h

@@ -4,6 +4,7 @@
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
 #include "base/define.h"
 #include "base/define.h"
+#include "util/strutils.h"
 namespace ylib
 namespace ylib
 {
 {
 	namespace process
 	namespace process
@@ -16,7 +17,7 @@ namespace ylib
 			std::string path()
 			std::string path()
 			{
 			{
 				if (__filepath.empty())
 				if (__filepath.empty())
-					__filepath = ylib::process::getpath(pid);
+					__filepath = strutils::replace(ylib::process::getpath(pid), '\\', '/');
 				return __filepath;
 				return __filepath;
 			}
 			}
 			std::string __filepath;
 			std::string __filepath;
@@ -31,15 +32,8 @@ namespace ylib
 		// 系统进程列表
 		// 系统进程列表
 		std::list<ylib::process::proc_info> list();
 		std::list<ylib::process::proc_info> list();
 		// 是否存在
 		// 是否存在
-		size_t exist(const std::string& filepath);
+		std::vector<size_t> exist(const std::string& filepath);
 		bool exist(size_t pid);
 		bool exist(size_t pid);
-		/// <summary>
-		/// 取工作目录
-		/// </summary>
-		/// <param name="pid"></param>
-		/// <returns></returns>
-		std::string work_directory(size_t pid);
-
 #ifdef _WIN32
 #ifdef _WIN32
 		// 检测多开
 		// 检测多开
 		bool already_running(const std::string& name);
 		bool already_running(const std::string& name);

+ 20 - 31
src/util/process.cpp

@@ -125,6 +125,12 @@ bool ylib::process::create(const std::string& filepath, const std::string& worki
             close(dev_null);
             close(dev_null);
         }
         }
 
 
+        // 设置工作目录
+        if (!working_directory.empty() && chdir(working_directory.c_str()) != 0) {
+            // 如果chdir失败,退出子进程
+            exit(EXIT_FAILURE);
+        }
+
         // 构建参数列表
         // 构建参数列表
         std::vector<char*> argv;
         std::vector<char*> argv;
         argv.push_back(const_cast<char*>(filepath.c_str()));
         argv.push_back(const_cast<char*>(filepath.c_str()));
@@ -148,7 +154,15 @@ bool ylib::process::create(const std::string& filepath, const std::string& worki
         if (wait_close) {
         if (wait_close) {
             int status;
             int status;
             waitpid(pid_fork, &status, 0);
             waitpid(pid_fork, &status, 0);
-        }
+            if (return_code != nullptr) {
+                if (WIFEXITED(status)) {
+                    *return_code = WEXITSTATUS(status);
+                }
+                else {
+                    *return_code = -1; // 表示子进程未正常退出
+                }
+            }
+}
 
 
         return true;
         return true;
     }
     }
@@ -249,17 +263,18 @@ std::string ylib::process::getpath(uint32 process_id)
     return path;
     return path;
 #endif
 #endif
 }
 }
-size_t ylib::process::exist(const std::string& filepath)
+std::vector<size_t> ylib::process::exist(const std::string& filepath)
 {
 {
+    std::vector<size_t> result;
     auto list = process::list();
     auto list = process::list();
     for_iter(iter, list)
     for_iter(iter, list)
     {
     {
         if (strutils::change_case(iter->path(), false) == strutils::change_case(filepath, false))
         if (strutils::change_case(iter->path(), false) == strutils::change_case(filepath, false))
         {
         {
-            return iter->pid;
+            result.push_back(iter->pid);
         }
         }
     }
     }
-    return 0;
+    return result;
 }
 }
 bool ylib::process::exist(size_t pid)
 bool ylib::process::exist(size_t pid)
 {
 {
@@ -284,36 +299,10 @@ bool ylib::process::exist(size_t pid)
     }
     }
 #endif
 #endif
 }
 }
-std::string ylib::process::work_directory(size_t pid)
-{
+
 #ifdef _WIN32
 #ifdef _WIN32
-    char buffer[MAX_PATH];
-    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
-    if (hProcess) {
-        if (GetModuleFileNameEx(hProcess, NULL, buffer, MAX_PATH)) {
-            CloseHandle(hProcess);
-            std::string::size_type pos = std::string(buffer).find_last_of("\\/");
-            return std::string(buffer).substr(0, pos);
-        }
-        CloseHandle(hProcess);
-    }
-#else
-    char buffer[PATH_MAX];
-    std::ostringstream path;
-    path << "/proc/" << pid << "/exe";
-    ssize_t len = readlink(path.str().c_str(), buffer, sizeof(buffer) - 1);
-    if (len != -1) {
-        buffer[len] = '\0';
-        std::string fullPath(buffer);
-        std::string::size_type pos = fullPath.find_last_of("/");
-        return fullPath.substr(0, pos);
-    }
-#endif
-    return "";
-}
 
 
 
 
-#ifdef _WIN32
 
 
 // 检测多开
 // 检测多开
 bool ylib::process::already_running(const std::string& name)
 bool ylib::process::already_running(const std::string& name)