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 2954800
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
26 changes: 22 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 @@ -11,9 +11,12 @@

#include "base/process.h"

#include <string.h>

#if LAF_WINDOWS
#include <windows.h>
#else
#include <libproc.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
Expand All @@ -28,7 +31,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 +47,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 2954800

Please sign in to comment.