Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stability changes #3

Merged
merged 6 commits into from
Oct 11, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
#include <chrono>
#include <dirent.h>
#include <dlfcn.h>
#include <filesystem>
#include <linux/fcntl.h>
#include <mutex>
#include <sys/socket.h>
#include <systemd/sd-journal.h>
#include <thread>
#include <unistd.h>
#include <sys/prctl.h>
#include <condition_variable>

#define forever for(;;)

namespace {
static int(*func_open)(const char*, int, mode_t) = nullptr;
static std::mutex logMutex;
std::condition_variable stateThreadCondition;
std::mutex stateThreadConditionMutex;
static std::thread stateThread;
static std::atomic<bool> stateThreadRunning = false;
static bool exitThread = false;
Expand Down Expand Up @@ -74,6 +76,7 @@ namespace {
#define _WARN(...) _PRINTF(LOG_WARNING, __VA_ARGS__)
#define _INFO(...) _PRINTF(LOG_INFO, __VA_ARGS__)
#define _CRIT(...) _PRINTF(LOG_CRIT, __VA_ARGS__)
#define _UNUSED(x)

inline std::string trim(std::string& str){
str.erase(str.find_last_not_of(' ') + 1);
Expand Down Expand Up @@ -112,7 +115,12 @@ namespace {
_WARN("Unknown power state call: %s", data.c_str());
}
}
stateThreadRunning = false;
{
std::lock_guard<std::mutex> lock(stateThreadConditionMutex);
_UNUSED(lock);
Eeems marked this conversation as resolved.
Show resolved Hide resolved
stateThreadRunning = false;
}
stateThreadCondition.notify_one();
}

int __open(const char* pathname, int flags){
Expand Down Expand Up @@ -218,10 +226,15 @@ extern "C" {
rtld_fini,
stack_end
);
if(stateThreadRunning){
_DEBUG("Waiting for thread to exit");
exitThread = true;
stateThread.join();
exitThread = true;
Eeems marked this conversation as resolved.
Show resolved Hide resolved
_DEBUG("Waiting for thread to exit");
{
std::unique_lock<std::mutex> lock(stateThreadConditionMutex);
if(!stateThreadCondition.wait_for(lock, std::chrono::seconds(5), []{
return !stateThreadRunning;
})){
_WARN("Timeout waiting for state thread to exit");
Eeems marked this conversation as resolved.
Show resolved Hide resolved
}
}
return res;
}
Expand Down
Loading