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 d829127
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 48 deletions.
76 changes: 29 additions & 47 deletions base/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@
#include "base/process.h"

#if LAF_WINDOWS
#include <windows.h>
#include <iostream>
#include <tlhelp32.h>
// #include <windows.h>
// #include <iostream>
// #include <tlhelp32.h>
#else
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#endif

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

#if LAF_LINUX
#include "base/fs.h"
#include <stdlib.h>
#include <cstring>
// #include <libproc.h>
// #include <string.h>
#elif LAF_LINUX
// #include "base/fs.h"
// #include <stdlib.h>
// #include <cstring>
#endif
namespace base {

Expand All @@ -40,9 +38,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 +52,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 +69,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 +84,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
21 changes: 20 additions & 1 deletion base/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,33 @@

#include "base/ints.h"

#if LAF_WINDOWS
#include <windows.h>
// #include <iostream>
#include <tlhelp32.h>
#elif LAF_MACOS
#include <libproc.h>
// #include <string.h>
// #include <iosfwd>
#elif LAF_LINUX
// #include <stdlib.h>

#endif

#include <string>

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 d829127

Please sign in to comment.