20#include <wx/filename.h>
23#include "model/base_platform.h"
25#include "model/wx_instance_chk.h"
33static const char*
const kName =
"_OpenCPN_SILock";
35static void KillProcess(
int pid) {
37 if (GetCurrentProcessId() != pid) {
38 const auto proc = OpenProcess(PROCESS_TERMINATE,
false, pid);
39 TerminateProcess(proc, 1);
43 if (pid != getpid()) kill(
static_cast<pid_t
>(pid), SIGKILL);
51WxInstanceCheck::WxInstanceCheck()
52 : m_checker(new wxSingleInstanceChecker), is_inited(false) {}
54void WxInstanceCheck::Init() {
55 assert(g_BasePlatform &&
"NULL g_BasePlatform");
57 if (!m_checker->Create(kName, dir)) {
58 WARNING_LOG <<
"Cannot create instance locker (!)";
64 if (!is_inited) Init();
65 return !m_checker->IsAnotherRunning();
69 if (!is_inited) Init();
71 if (!wxFileExists(lockfile.GetFullPath()))
return;
75 std::ifstream f(lockfile.GetFullPath().ToStdString());
80 pid = std::stoi(ss.str());
84 wxRemoveFile(lockfile.GetFullPath());
85 if (pid != -1) KillProcess(pid);
89 if (!is_inited) Init();
void OnExit() override
Do whatever needed before wxWidget's checks triggers.
bool IsMainInstance() override
Return true if this process is the primary opencpn instance.
void CleanUp() override
Remove all persistent instance state, including possible lock file and defunct opencpn processes.
Enhanced logging interface on top of wx/log.h.