diff --git a/base/process.cpp b/base/process.cpp index 887f2d475..a0d11e338 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -1,5 +1,5 @@ // LAF Base Library -// Copyright (c) 2021 Igara Studio S.A. +// Copyright (c) 2021-2024 Igara Studio S.A. // Copyright (c) 2015-2016 David Capello // // This file is released under the terms of the MIT license. @@ -13,12 +13,20 @@ #if LAF_WINDOWS #include + #include #else #include #include #include #endif +#if LAF_MACOS + #include +#elif LAF_LINUX + #include "base/fs.h" + #include +#endif + namespace base { #if LAF_WINDOWS @@ -28,34 +36,80 @@ pid get_current_process_id() return (pid)GetCurrentProcessId(); } -bool is_process_running(pid pid) +std::string get_process_name(pid pid) { - bool running = false; - - HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); + HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (handle) { - DWORD exitCode = 0; - if (GetExitCodeProcess(handle, &exitCode)) { - running = (exitCode == STILL_ACTIVE); + PROCESSENTRY32 pe; + pe.dwSize = sizeof(PROCESSENTRY32); + if (Process32First(handle, &pe)) { + do { + char buf[64]; + wcstombs(buf, pe.szExeFile, 64); + std::string str(buf); + for (char& c : str) { + c = tolower(c); + } + if (pe.th32ProcessID == pid) { + return str; + } + } while (Process32Next(handle, &pe)); } CloseHandle(handle); } + return ""; +} + +#elif LAF_MACOS - return running; +pid get_current_process_id() +{ + return (pid)getpid(); } -#else // !LAF_WINDOWS +std::string get_process_name(pid pid) +{ + struct proc_bsdinfo process; + proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, + &process, PROC_PIDTBSDINFO_SIZE); + return process.pbi_name; +} + +#elif LAF_LINUX pid get_current_process_id() { return (pid)getpid(); } -bool is_process_running(pid pid) +std::string get_process_name(pid pid) { - return (kill(pid, 0) == 0); + char path[128]; + memset(path, 0, 128); + sprintf(path, "/proc/%d/exe", pid); + char* exepath = realpath(path, nullptr); + if (!exepath) + return ""; + + auto exename = base::get_file_name(exepath); + free(exepath); + + return exename; } #endif +bool is_process_running(pid pid, std::string currentProcessName) +{ + std::string pidProcessName = get_process_name(pid); + if (pidProcessName == "") + return false; + return pidProcessName == currentProcessName; +} + +bool is_process_running(pid pid) +{ + return false; +}; + } // namespace base diff --git a/base/process.h b/base/process.h index 445b87cd6..5aa5d79b1 100644 --- a/base/process.h +++ b/base/process.h @@ -1,4 +1,5 @@ // LAF Base Library +// Copyright (c) 2023-2024 Igara Studio S.A. // Copyright (c) 2015-2016 David Capello // // This file is released under the terms of the MIT license. @@ -10,12 +11,20 @@ #include "base/ints.h" +#include + namespace base { typedef uint32_t pid; pid get_current_process_id(); + 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