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 Dec 27, 2023
1 parent b8edff5 commit 82e2792
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 24 additions & 4 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-2023 Igara Studio S.A.
// Copyright (c) 2015-2016 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -19,6 +19,11 @@
#include <unistd.h>
#endif

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

namespace base {

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

bool is_process_running(pid pid)
bool is_process_running(pid pid, const char* pname)
{
bool running = false;

Expand All @@ -44,14 +49,29 @@ 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);
}

#elif LAF_LINUX

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

bool is_process_running(pid pid)
bool is_process_running(pid pid, const char* pname)
{
return (kill(pid, 0) == 0);
}
Expand Down
3 changes: 2 additions & 1 deletion base/process.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// LAF Base Library
// Copyright (c) 2023 Igara Studio S.A.
// Copyright (c) 2015-2016 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -16,7 +17,7 @@ namespace base {

pid get_current_process_id();

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

} // namespace base

Expand Down

0 comments on commit 82e2792

Please sign in to comment.