OpenCPN Partial API docs
Loading...
Searching...
No Matches
wx_instance_chk.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2023 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/ *
16 **************************************************************************/
17
25#ifdef _MSC_VER
26#include <process.h>
27#else
28#include <signal.h>
29#endif
30
31#include <wx/filename.h>
32#include <wx/string.h>
33
34#include "model/base_platform.h"
35#include "model/logger.h"
37
38static const char* const kName = "_OpenCPN_SILock";
39
40static void KillProcess(int pid) {
41#ifdef _MSC_VER
42 if (GetCurrentProcessId() != pid) {
43 const auto proc = OpenProcess(PROCESS_TERMINATE, false, pid);
44 TerminateProcess(proc, 1);
45 CloseHandle(proc);
46 }
47#else
48 if (pid != getpid()) kill(static_cast<pid_t>(pid), SIGKILL);
49#endif
50}
51
52//
53// Since required global variables does not exist from the beginning we
54// use lazy init, postponed until object is actually used. At this point
55// required globals should be in place
56WxInstanceCheck::WxInstanceCheck()
57 : m_checker(new wxSingleInstanceChecker), is_inited(false) {}
58
59void WxInstanceCheck::Init() {
60 assert(g_BasePlatform && "NULL g_BasePlatform");
61 wxString dir = g_BasePlatform->GetPrivateDataDir();
62 if (!m_checker->Create(kName, dir)) {
63 WARNING_LOG << "Cannot create instance locker (!)";
64 }
65 is_inited = true;
66}
67
69 if (!is_inited) Init();
70 return !m_checker->IsAnotherRunning();
71}
72
74 if (!is_inited) Init();
75 wxFileName lockfile(g_BasePlatform->GetPrivateDataDir(), kName);
76 if (!wxFileExists(lockfile.GetFullPath())) return;
77
78 // Best effort try to read pid from lock file and kill it.
79 int pid = -1;
80 std::ifstream f(lockfile.GetFullPath().ToStdString());
81 if (f.good()) {
82 std::stringstream ss;
83 ss << f.rdbuf();
84 try {
85 pid = std::stoi(ss.str());
86 } catch (...) {
87 }
88 }
89 wxRemoveFile(lockfile.GetFullPath());
90 if (pid != -1) KillProcess(pid);
91}
92
94 if (!is_inited) Init();
95 delete m_checker;
96 m_checker = 0;
97}
BasePlatform * g_BasePlatform
points to g_platform, handles brain-dead MS linker.
Basic platform specific support utilities without GUI deps.
wxString & GetPrivateDataDir()
Return dir path for opencpn.log, etc., respecting -c cli option.
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.
Single instance check based on wxWidgets functions.