OpenCPN Partial API docs
Loading...
Searching...
No Matches
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
36static const char* const kName = "_OpenCPN_SILock";
37
38static int GetLockfilePid(const std::string& path) {
39 std::ifstream f(path.c_str());
40 std::stringstream ss;
41 ss << f.rdbuf();
42 int pid = -1;
43 try {
44 ss >> pid;
45 } catch (...) {
46 pid = -1;
47 }
48 return pid;
49}
50
51StdInstanceCheck::StdInstanceCheck() : m_is_main_instance(false) {
52 wxString dir = g_BasePlatform->GetPrivateDataDir();
53 m_path = (dir + "/" + kName).ToStdString();
54 std::stringstream ss;
55 ss << m_path << "." << getpid();
56 std::ofstream f(ss.str());
57 f << getpid() << "\n";
58 if (!ocpn::exists(m_path.c_str())) {
59 std::rename(ss.str().c_str(), m_path.c_str());
60 m_is_main_instance = true;
61 } else {
62 std::remove(ss.str().c_str());
63 }
64}
65
66bool StdInstanceCheck::IsMainInstance() { return m_is_main_instance; }
67
69 if (!ocpn::exists(m_path)) {
70 return;
71 }
72 int pid = GetLockfilePid(m_path);
73 if (pid == -1) {
74 return;
75 }
76 // Try to kill zombie process and remove lock file
77 for (int i = 0; kill(pid, 0) == 0 && i < 3; i++) {
78 kill(pid, SIGTERM);
79 sleep(1);
80 }
81 if (kill(pid, 0) == 0) kill(pid, SIGKILL);
82 std::remove(m_path.c_str());
83}
84
85StdInstanceCheck::~StdInstanceCheck() {
86 if (!ocpn::exists(m_path)) return;
87 int pid = GetLockfilePid(m_path);
88 if (pid == getpid()) std::remove(m_path.c_str());
89}
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.