OpenCPN Partial API docs
wx_instance_chk.cpp
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, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  **************************************************************************/
19 
20 #include <wx/filename.h>
21 #include <wx/string.h>
22 
23 #include "model/base_platform.h"
24 #include "model/logger.h"
25 #include "model/wx_instance_chk.h"
26 
27 #ifdef _MSC_VER
28 #include <process.h>
29 #else
30 #include <signal.h>
31 #endif
32 
33 static const char* const kName = "_OpenCPN_SILock";
34 
35 static void KillProcess(int pid) {
36 #ifdef _MSC_VER
37  if (GetCurrentProcessId() != pid) {
38  const auto proc = OpenProcess(PROCESS_TERMINATE, false, pid);
39  TerminateProcess(proc, 1);
40  CloseHandle(proc);
41  }
42 #else
43  if (pid != getpid()) kill(static_cast<pid_t>(pid), SIGKILL);
44 #endif
45 }
46 
47 //
48 // Since required global variables does not exist from the beginning we
49 // use lazy init, postponed until object is actually used. At this point
50 // required globals should be in place
51 WxInstanceCheck::WxInstanceCheck()
52  : m_checker(new wxSingleInstanceChecker), is_inited(false) { }
53 
54 void WxInstanceCheck::Init() {
55  assert(g_BasePlatform && "NULL g_BasePlatform");
56  wxString dir = g_BasePlatform ->GetPrivateDataDir();
57  if (!m_checker->Create(kName, dir)) {
58  WARNING_LOG << "Cannot create instance locker (!)";
59  }
60  is_inited = true;
61 }
62 
64  if (!is_inited) Init();
65  return !m_checker->IsAnotherRunning();
66 }
67 
69  if (!is_inited) Init();
70  wxFileName lockfile(g_BasePlatform ->GetPrivateDataDir(), kName);
71  if (!wxFileExists(lockfile.GetFullPath())) return;
72 
73  // Best effort try to read pid from lock file and kill it.
74  int pid = -1;
75  std::ifstream f(lockfile.GetFullPath().ToStdString());
76  if (f.good()) {
77  std::stringstream ss;
78  ss << f.rdbuf();
79  try {
80  pid = std::stoi(ss.str());
81  } catch (...) {}
82  }
83  wxRemoveFile(lockfile.GetFullPath());
84  if (pid != -1) KillProcess(pid);
85 }
86 
88  if (!is_inited) Init();
89  delete m_checker;
90  m_checker = 0;
91 }
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.