20 #include <wx/filename.h>
21 #include <wx/string.h>
23 #include "model/base_platform.h"
24 #include "model/logger.h"
25 #include "model/wx_instance_chk.h"
33 static const char*
const kName =
"_OpenCPN_SILock";
35 static 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);
51 WxInstanceCheck::WxInstanceCheck()
52 : m_checker(new wxSingleInstanceChecker), is_inited(false) { }
54 void 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();
70 wxFileName lockfile(g_BasePlatform ->GetPrivateDataDir(), kName);
71 if (!wxFileExists(lockfile.GetFullPath()))
return;
75 std::ifstream f(lockfile.GetFullPath().ToStdString());
80 pid = std::stoi(ss.str());
83 wxRemoveFile(lockfile.GetFullPath());
84 if (pid != -1) KillProcess(pid);
88 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.