diff --git a/base/process.cpp b/base/process.cpp index 5a79366a0..3d267af7f 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -13,7 +13,7 @@ #if LAF_WINDOWS #include - #include +// #include #include #else #include @@ -23,12 +23,12 @@ #if LAF_MACOS #include - #include +// #include #endif #if LAF_LINUX #include "base/fs.h" - #include +// #include #include #endif namespace base { @@ -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,34 @@ 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 +bool is_process_running(pid pid) +{ + return false; +}; } // namespace base diff --git a/base/process.h b/base/process.h index 3c4e4058f..5efc25065 100644 --- a/base/process.h +++ b/base/process.h @@ -11,14 +11,32 @@ #include "base/ints.h" +#if LAF_WINDOWS +// #include + #include +// #include +#endif + +#if LAF_MACOS + #include +#endif + +#if LAF_LINUX + #include +#endif + namespace base { typedef uint32_t pid; 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, std::string currentProcessName); + // Declaration to avoid errors during testing of Github actions + // TO DO: remove function after the implementation of PR aseprite/aseprite#4266 bool is_process_running(pid pid); } // namespace base