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 18, 2024
1 parent 028e445 commit 5dda814
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 39 deletions.
53 changes: 16 additions & 37 deletions base/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,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
4 changes: 2 additions & 2 deletions base/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5dda814

Please sign in to comment.