29 #include <wx/string.h>
31 #include "model/base_platform.h"
32 #include "model/ocpn_utils.h"
34 #include "model/std_instance_chk.h"
37 static const char*
const kName =
"_OpenCPN_SILock";
39 static int GetLockfilePid(
const std::string& path){
40 std::ifstream f(path.c_str());
44 try { ss >> pid; }
catch (...) { pid = -1; }
48 StdInstanceCheck::StdInstanceCheck() : m_is_main_instance(false) {
50 m_path = (dir +
"/" + kName).ToStdString();
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;
59 std::remove(ss.str().c_str());
66 if (!ocpn::exists(m_path)) {
69 int pid = GetLockfilePid(m_path);
74 for (
int i = 0; kill(pid, 0) == 0 && i < 3; i++) {
78 if (kill(pid, 0) == 0) kill(pid, SIGKILL);
79 std::remove(m_path.c_str());
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());
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.