31#include "model/base_platform.h"
32#include "model/ocpn_utils.h"
34#include "model/std_instance_chk.h"
36static const char*
const kName =
"_OpenCPN_SILock";
38static int GetLockfilePid(
const std::string& path) {
39 std::ifstream f(path.c_str());
51StdInstanceCheck::StdInstanceCheck() : m_is_main_instance(false) {
53 m_path = (dir +
"/" + kName).ToStdString();
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;
62 std::remove(ss.str().c_str());
69 if (!ocpn::exists(m_path)) {
72 int pid = GetLockfilePid(m_path);
77 for (
int i = 0; kill(pid, 0) == 0 && i < 3; i++) {
81 if (kill(pid, 0) == 0) kill(pid, SIGKILL);
82 std::remove(m_path.c_str());
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());
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.