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