diff --git a/base/process.cpp b/base/process.cpp index 5a79366a0..10dfc1a26 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -40,9 +40,8 @@ pid get_current_process_id() return (pid)GetCurrentProcessId(); } -bool is_process_running(pid pid, const char* pname) +std::string get_process_name(pid pid) { - bool running = false; HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (handle) { PROCESSENTRY32 pe; @@ -55,32 +54,14 @@ bool is_process_running(pid pid, const char* pname) for (char& c : str) { c = tolower(c); } - if (pe.th32ProcessID == pid && - str == pname) { - running = true; + if (str && pe.th32ProcessID == pid) { + return str; } } while (Process32Next(handle, &pe)); } CloseHandle(handle); } - - return running; -} - -bool is_process_running(pid pid) -{ - bool running = false; - - HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); - if (handle) { - DWORD exitCode = 0; - if (GetExitCodeProcess(handle, &exitCode)) { - running = (exitCode == STILL_ACTIVE); - } - CloseHandle(handle); - } - - return running; + return ""; } #elif LAF_MACOS @@ -90,17 +71,12 @@ pid get_current_process_id() return (pid)getpid(); } -bool is_process_running(pid pid, const char* pname) +std::string get_process_name(pid pid) { struct proc_bsdinfo process; proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &process, PROC_PIDTBSDINFO_SIZE); - return (strcmp(pname, process.pbi_name) == 0); -} - -bool is_process_running(pid pid) -{ - return (kill(pid, 0) == 0); + return process.pbi_name; } #elif LAF_LINUX @@ -110,26 +86,29 @@ pid get_current_process_id() return (pid)getpid(); } -bool is_process_running(pid pid, const char* pname) +std::string get_process_name(pid pid) { char path[128]; memset(path, 0, 128); sprintf(path, "/proc/%d/exe", pid); char* exepath = realpath(path, nullptr); if (!exepath) - return false; + return ""; auto exename = base::get_file_name(exepath); free(exepath); - return exename == std::string(pname); + return exename; } -bool is_process_running(pid pid) +#endif + +bool is_process_running(pid pid, std::string currentProcessName) { - return (kill(pid, 0) == 0); + std::string pidProcessName = get_process_name(pid); + if (pidProcessName == "") + return false; + return pidProcessName == currentProcessName; } -#endif - } // namespace base diff --git a/base/process.h b/base/process.h index 3c4e4058f..9ba2de62f 100644 --- a/base/process.h +++ b/base/process.h @@ -17,9 +17,9 @@ namespace base { pid get_current_process_id(); - bool is_process_running(pid pid, const char* pname); + std::string get_process_name(pid pid); - bool is_process_running(pid pid); + bool is_process_running(pid pid, std::string currentProcessName); } // namespace base