27#elif defined(__APPLE__)
49#include "std_filesystem.h"
51static const char*
const kName =
"_OpenCPN_SILock";
53static int GetLockfilePid(
const std::string& path) {
54 std::ifstream f(path.c_str());
66static bool DoesProcessExist(
int pid) {
68 HANDLE process = OpenProcess(SYNCHRONIZE, FALSE,
static_cast<DWORD
>(pid));
69 DWORD ret = WaitForSingleObject(process, 0);
71 return ret == WAIT_TIMEOUT;
72#elif defined(__APPLE__)
73 char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
75 int rv = proc_pidpath(pid, pathbuf,
sizeof(pathbuf));
78 int rv = kill(pid, 0);
79 return rv == 0 || errno != ESRCH;
91StdInstanceCheck::StdInstanceCheck()
92 : m_is_main_instance(false), is_inited(false), is_my_lock(false) {}
94void StdInstanceCheck::Init() {
97 for (
int i = 0; i < 5; i += 1) {
98 std::FILE* f = std::fopen(m_path.generic_u8string().c_str(),
"wx");
100 std::stringstream ss;
101 ss << getpid() <<
"\n";
102 std::fputs(ss.str().c_str(), f);
104 m_is_main_instance =
true;
109 wxLogDebug(
"Removing unusable lock file");
115 std::ifstream stream(m_path);
119 using namespace std::chrono;
120 std::this_thread::sleep_for(50ms);
123 if (pid == GetPid()) {
124 m_is_main_instance =
true;
127 if (pid != -1 && !DoesProcessExist(pid)) {
128 wxLogDebug(
"Removing orphaned lock for pid: %d", pid);
131 if (pid != -1 && fs::exists(m_path))
break;
137 if (!is_inited) Init();
138 return m_is_main_instance;
143 if (fs::exists(m_path)) fs::remove(m_path);
146StdInstanceCheck::~StdInstanceCheck() {
147 if (fs::exists(m_path) && is_my_lock) fs::remove(m_path);
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.
Miscellaneous utilities, many of which string related.
Posix native instance check implementation.