OpenCPN Partial API docs
std_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 <cstdio>
21 #include <fstream>
22 #include <sstream>
23 
24 #ifdef HAVE_UNISTD_H
25 #include <signal.h>
26 #include <unistd.h>
27 #endif
28 
29 #include <wx/string.h>
30 
31 #include "model/base_platform.h"
32 #include "model/ocpn_utils.h"
33 
34 #include "model/std_instance_chk.h"
35 
36 
37 static const char* const kName = "_OpenCPN_SILock";
38 
39 static int GetLockfilePid(const std::string& path){
40  std::ifstream f(path.c_str());
41  std::stringstream ss;
42  ss << f.rdbuf();
43  int pid = -1;
44  try { ss >> pid; } catch (...) { pid = -1; }
45  return pid;
46 }
47 
48 StdInstanceCheck::StdInstanceCheck() : m_is_main_instance(false) {
49  wxString dir = g_BasePlatform ->GetPrivateDataDir();
50  m_path = (dir + "/" + kName).ToStdString();
51  std::stringstream ss;
52  ss << m_path << "." << getpid();
53  std::ofstream f(ss.str());
54  f << getpid() << "\n";
55  if (!ocpn::exists(m_path.c_str())) {
56  std::rename(ss.str().c_str(), m_path.c_str());
57  m_is_main_instance = true;
58  } else {
59  std::remove(ss.str().c_str());
60  }
61 }
62 
63 bool StdInstanceCheck::IsMainInstance() { return m_is_main_instance; }
64 
66  if (!ocpn::exists(m_path)) {
67  return;
68  }
69  int pid = GetLockfilePid(m_path);
70  if (pid == -1) {
71  return;
72  }
73  // Try to kill zombie process and remove lock file
74  for (int i = 0; kill(pid, 0) == 0 && i < 3; i++) {
75  kill(pid, SIGTERM);
76  sleep(1);
77  }
78  if (kill(pid, 0) == 0) kill(pid, SIGKILL);
79  std::remove(m_path.c_str());
80 }
81 
82 StdInstanceCheck::~StdInstanceCheck() {
83  if (!ocpn::exists(m_path)) return;
84  int pid = GetLockfilePid(m_path);
85  if (pid == getpid()) std::remove(m_path.c_str());
86 }
wxString & GetPrivateDataDir()
Return dir path for opencpn.log, etc., respecting -c cli option.
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.