Skip to content

Commit

Permalink
Unification of the 'is_process_running(pid pid)' function for all pla…
Browse files Browse the repository at this point in the history
…tforms
  • Loading branch information
Gasparoken committed Jan 23, 2024
1 parent 028e445 commit 959b8f5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
62 changes: 23 additions & 39 deletions base/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#if LAF_WINDOWS
#include <windows.h>
#include <iostream>
// #include <iostream>
#include <tlhelp32.h>
#else
#include <signal.h>
Expand All @@ -23,12 +23,12 @@

#if LAF_MACOS
#include <libproc.h>
#include <string.h>
// #include <string.h>
#endif

#if LAF_LINUX
#include "base/fs.h"
#include <stdlib.h>
// #include <stdlib.h>
#include <cstring>
#endif
namespace base {
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
20 changes: 19 additions & 1 deletion base/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@

#include "base/ints.h"

#if LAF_WINDOWS
// #include <windows.h>
#include <iostream>
// #include <tlhelp32.h>
#endif

#if LAF_MACOS
#include <string.h>
#endif

#if LAF_LINUX
#include <stdlib.h>
#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
Expand Down

0 comments on commit 959b8f5

Please sign in to comment.