mirror of
https://github.com/eclipse-openvehicle-api/openvehicle-api.git
synced 2026-07-02 05:35:11 +00:00
update parser (#5)
This commit is contained in:
@@ -3,7 +3,11 @@
|
||||
|
||||
#include <thread>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <atomic>
|
||||
#ifdef _WIN32
|
||||
// Prevent reassignment of "interface"
|
||||
#pragma push_macro("interface")
|
||||
@@ -65,11 +69,49 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
#elif defined __linux__
|
||||
/**
|
||||
* @brief Check whether the debugger is present and running the application.
|
||||
* @return Returns true when the debugger is present or false otherwise.
|
||||
*/
|
||||
bool IsDebuggerPresent()
|
||||
{
|
||||
std::string ssFilename = "/proc/self/status"; // For the current process
|
||||
std::ifstream fstreamStatus(ssFilename);
|
||||
if (!fstreamStatus.is_open()) return false; // Cannot open file, assume not debugged
|
||||
|
||||
std::string ssLine;
|
||||
while (std::getline(fstreamStatus, ssLine))
|
||||
{
|
||||
if (ssLine.substr(0, 9) == "TracerPid")
|
||||
{
|
||||
std::stringstream sstreamLine(ssLine);
|
||||
std::string ssKey;
|
||||
int iTracerPid = 0;
|
||||
sstreamLine >> ssKey >> iTracerPid;
|
||||
return iTracerPid != 0;
|
||||
}
|
||||
}
|
||||
return false; // TracerPid not found
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @brief Check whether the debugger is present and running the application.
|
||||
* @return Returns true when the debugger is present or false otherwise.
|
||||
*/
|
||||
bool IsDebuggerPresent()
|
||||
{
|
||||
// No implementation available to check for a debugger.
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Watch thread function.
|
||||
* @param[in] iWatchdogTimeS The duration the watchdog is monitoring before process termination in seconds (default 120s).
|
||||
* @param[in] iWatchdogTimeS The duration the watchdog is monitoring before process termination in seconds.
|
||||
*/
|
||||
void WatchdogThreadFunc(int64_t iWatchdogTimeS = 120ll)
|
||||
void WatchdogThreadFunc(int64_t iWatchdogTimeS)
|
||||
{
|
||||
// Run for the most the set time; then terminate...
|
||||
auto tpStart = std::chrono::steady_clock::now();
|
||||
@@ -79,6 +121,9 @@ private:
|
||||
auto tpNow = std::chrono::steady_clock::now();
|
||||
if (std::chrono::duration_cast<std::chrono::seconds>(tpNow - tpStart).count() > iWatchdogTimeS)
|
||||
{
|
||||
// Do not end the process when a debugger is present.
|
||||
if (IsDebuggerPresent()) continue;
|
||||
|
||||
std::cerr << "WATCHDOG TERMINATION ENFORCED!!!" << std::endl;
|
||||
std::cerr.flush();
|
||||
#ifdef _WIN32
|
||||
@@ -97,8 +142,8 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
bool m_bTerminateWatchdog = false; ///< When set, allows the thread to terminate.
|
||||
std::thread m_threadWatchdog; ///< The watchdog thread.
|
||||
std::atomic_bool m_bTerminateWatchdog = false; ///< When set, allows the thread to terminate.
|
||||
std::thread m_threadWatchdog; ///< The watchdog thread.
|
||||
};
|
||||
|
||||
#endif // !defined PROCESS_WATCHDOG_H
|
||||
|
||||
Reference in New Issue
Block a user