Skip to content

Commit

Permalink
Fix 'is_process_running' returns an incorrect 'true' when process 'pi…
Browse files Browse the repository at this point in the history
…d' exists, but whose process does not belong to the current application.

Effect on Aseprite: some recovery sessions do not appear in the Recovery menu.
  • Loading branch information
Gasparoken committed Jan 12, 2024
1 parent 157533d commit 917917f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
63 changes: 61 additions & 2 deletions base/process.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -13,12 +13,19 @@

#if LAF_WINDOWS
#include <windows.h>
#include <ctype.h>
#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

namespace base {

#if LAF_WINDOWS
Expand All @@ -28,6 +35,33 @@ pid get_current_process_id()
return (pid)GetCurrentProcessId();
}

bool is_process_running(pid pid, const char* pname)
{
bool running = false;
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
if (handle) {
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 &&
str == pname) {
running = true;
}
} while (Process32Next(handle, &pe));
}
CloseHandle(handle);
}

return running;
}

bool is_process_running(pid pid)
{
bool running = false;
Expand All @@ -44,18 +78,43 @@ bool is_process_running(pid pid)
return running;
}

#else // !LAF_WINDOWS
#elif LAF_MACOS

pid get_current_process_id()
{
return (pid)getpid();
}

bool is_process_running(pid pid, const char* pname)
{
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);
}

#elif LAF_LINUX

pid get_current_process_id()
{
return (pid)getpid();
}

bool is_process_running(pid pid, const char* pname)
{
return (kill(pid, 0) == 0);
}

bool is_process_running(pid pid)
{
return is_process_running(pid, "");
}

#endif

} // namespace base
3 changes: 3 additions & 0 deletions base/process.h
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +17,8 @@ namespace base {

pid get_current_process_id();

bool is_process_running(pid pid, const char* pname);

bool is_process_running(pid pid);

} // namespace base
Expand Down

0 comments on commit 917917f

Please sign in to comment.