From 10d66ac6c675d7e05d79d9b5ec52402c8fa3e542 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Fri, 27 Sep 2024 18:38:48 -0600 Subject: [PATCH] Put stuff in a blank namespace --- main.cpp | 235 +++++++++++++++++++++++----------------------- package | 2 +- sysfs_preload.pro | 2 +- 3 files changed, 120 insertions(+), 119 deletions(-) diff --git a/main.cpp b/main.cpp index 43eefb1..bae1aab 100644 --- a/main.cpp +++ b/main.cpp @@ -13,142 +13,143 @@ #define forever for(;;) -static int(*func_open)(const char*, int, mode_t) = nullptr; -static std::mutex logMutex; -static std::thread stateThread; -static std::atomic stateThreadRunning = false; -static bool exitThread = false; -static int stateFds[2] = {-1, -1}; -static std::mutex stateMutex; +namespace { + static int(*func_open)(const char*, int, mode_t) = nullptr; + static std::mutex logMutex; + static std::thread stateThread; + static std::atomic stateThreadRunning = false; + static bool exitThread = false; + static int stateFds[2] = {-1, -1}; + static std::mutex stateMutex; -inline void __printf_header(int priority){ - std::string level; - switch(priority){ - case LOG_INFO: - level = "Info"; - break; - case LOG_WARNING: - level = "Warning"; - break; - case LOG_CRIT: - level = "Critical"; - break; - default: - level = "Debug"; + inline void __printf_header(int priority){ + std::string level; + switch(priority){ + case LOG_INFO: + level = "Info"; + break; + case LOG_WARNING: + level = "Warning"; + break; + case LOG_CRIT: + level = "Critical"; + break; + default: + level = "Debug"; + } + char name[16]; + prctl(PR_GET_NAME, name); + auto selfpath = realpath("/proc/self/exe", NULL); + fprintf( + stderr, + "[%i:%i:%i %s - %s] %s: ", + getpgrp(), + getpid(), + gettid(), + selfpath, + name, + level.c_str() + ); + free(selfpath); } - char name[16]; - prctl(PR_GET_NAME, name); - auto selfpath = realpath("/proc/self/exe", NULL); - fprintf( - stderr, - "[%i:%i:%i %s - %s] %s: ", - getpgrp(), - getpid(), - gettid(), - selfpath, - name, - level.c_str() - ); - free(selfpath); -} -inline void __printf_footer(const char* file, unsigned int line, const char* func){ - fprintf( - stderr, - " (%s:%u, %s)\n", - file, - line, - func - ); -} -#define _PRINTF(priority, ...) \ - if(std::getenv("OXIDE_PRELOAD_DEBUG") != nullptr){ \ - logMutex.lock(); \ - __printf_header(priority); \ - fprintf(stderr, __VA_ARGS__); \ - __printf_footer(__FILE__, __LINE__, __PRETTY_FUNCTION__); \ - logMutex.unlock(); \ + inline void __printf_footer(const char* file, unsigned int line, const char* func){ + fprintf( + stderr, + " (%s:%u, %s)\n", + file, + line, + func + ); } -#define _DEBUG(...) _PRINTF(LOG_DEBUG, __VA_ARGS__) -#define _WARN(...) _PRINTF(LOG_WARNING, __VA_ARGS__) -#define _INFO(...) _PRINTF(LOG_INFO, __VA_ARGS__) -#define _CRIT(...) _PRINTF(LOG_CRIT, __VA_ARGS__) + #define _PRINTF(priority, ...) \ + if(std::getenv("OXIDE_PRELOAD_DEBUG") != nullptr){ \ + logMutex.lock(); \ + __printf_header(priority); \ + fprintf(stderr, __VA_ARGS__); \ + __printf_footer(__FILE__, __LINE__, __PRETTY_FUNCTION__); \ + logMutex.unlock(); \ + } + #define _DEBUG(...) _PRINTF(LOG_DEBUG, __VA_ARGS__) + #define _WARN(...) _PRINTF(LOG_WARNING, __VA_ARGS__) + #define _INFO(...) _PRINTF(LOG_INFO, __VA_ARGS__) + #define _CRIT(...) _PRINTF(LOG_CRIT, __VA_ARGS__) -inline std::string trim(std::string& str){ - str.erase(str.find_last_not_of(' ') + 1); - str.erase(0, str.find_first_not_of(' ')); - return str; -} + inline std::string trim(std::string& str){ + str.erase(str.find_last_not_of(' ') + 1); + str.erase(0, str.find_first_not_of(' ')); + return str; + } -void __thread_run(int fd){ - char line[PIPE_BUF]; - forever{ - if(exitThread){ - break; - } - int res = read(fd, line, PIPE_BUF); - if(res == -1){ - if(errno == EINTR){ - continue; + void __thread_run(int fd){ + char line[PIPE_BUF]; + forever{ + if(exitThread){ + break; + } + int res = read(fd, line, PIPE_BUF); + if(res == -1){ + if(errno == EINTR){ + continue; + } + if(errno == EAGAIN || errno == EIO){ + std::this_thread::sleep_for(std::chrono::seconds(1)); + continue; + } + _WARN("/sys/power/state pipe failed to read: %s", strerror(errno)); + _DEBUG("/sys/power/state pip fd: %d", fd); + break; } - if(errno == EAGAIN || errno == EIO){ - std::this_thread::sleep_for(std::chrono::seconds(1)); + if(res == 0){ continue; } - _WARN("/sys/power/state pipe failed to read: %s", strerror(errno)); - _DEBUG("/sys/power/state pip fd: %d", fd); - break; - } - if(res == 0){ - continue; - } - auto data = std::string(line, res); - trim(data); - if(data == "mem" || data == "freeze" || data == "standby"){ - _INFO("Suspending system due to %s request", data.c_str()); - system("systemctl suspend"); - }else{ - _WARN("Unknown power state call: %s", data.c_str()); + auto data = std::string(line, res); + trim(data); + if(data == "mem" || data == "freeze" || data == "standby"){ + _INFO("Suspending system due to %s request", data.c_str()); + system("systemctl suspend"); + }else{ + _WARN("Unknown power state call: %s", data.c_str()); + } } + stateThreadRunning = false; } - stateThreadRunning = false; -} -int __open(const char* pathname, int flags){ - if(strcmp(pathname, "/sys/power/state") != 0){ - return -2; - } - // TODO - handle O_RDWR somehow - if(flags == O_RDONLY){ - return -2; - } - stateMutex.lock(); - if(stateFds[0] != -1){ - if(!stateThreadRunning){ - stateThread.join(); + int __open(const char* pathname, int flags){ + if(strcmp(pathname, "/sys/power/state") != 0){ + return -2; + } + // TODO - handle O_RDWR somehow + if(flags == O_RDONLY){ + return -2; + } + stateMutex.lock(); + if(stateFds[0] != -1){ + if(!stateThreadRunning){ + stateThread.join(); + stateThreadRunning = true; + stateThread = std::thread(__thread_run, stateFds[1]); + } + stateMutex.unlock(); + _INFO("Getting /sys/power/state pipe"); + return stateFds[0]; + } + _INFO("Opening /sys/power/state pipe"); + int socketFlags = SOCK_STREAM; + if((flags & O_NONBLOCK) || (flags & O_NDELAY)){ + socketFlags |= SOCK_NONBLOCK; + } + if(socketpair(AF_UNIX, socketFlags, 0, stateFds) == 0){ stateThreadRunning = true; stateThread = std::thread(__thread_run, stateFds[1]); + _INFO("/sys/power/state pipe opened"); + }else{ + _WARN("Unable to open /sys/power/state pipe: %s", strerror(errno)); } stateMutex.unlock(); - _INFO("Getting /sys/power/state pipe"); return stateFds[0]; } - _INFO("Opening /sys/power/state pipe"); - int socketFlags = SOCK_STREAM; - if((flags & O_NONBLOCK) || (flags & O_NDELAY)){ - socketFlags |= SOCK_NONBLOCK; - } - if(socketpair(AF_UNIX, socketFlags, 0, stateFds) == 0){ - stateThreadRunning = true; - stateThread = std::thread(__thread_run, stateFds[1]); - _INFO("/sys/power/state pipe opened"); - }else{ - _WARN("Unable to open /sys/power/state pipe: %s", strerror(errno)); - } - stateMutex.unlock(); - return stateFds[0]; } - extern "C" { __attribute__((visibility("default"))) int open64(const char* pathname, int flags, mode_t mode = 0){ diff --git a/package b/package index 3d53252..e5a3e48 100644 --- a/package +++ b/package @@ -1,7 +1,7 @@ pkgnames=(sysfs_preload) pkgdesc="A simple preload that forces any calls to /sys/power/state to use systemd instead." url=https://github.com/Eeems-Org/sysfs_preload -pkgver=1.0.1-1 +pkgver=1.0.2-1 timestamp=2024-06-22T05:19Z section=util maintainer="Eeems " diff --git a/sysfs_preload.pro b/sysfs_preload.pro index 0baf51c..07b40c0 100644 --- a/sysfs_preload.pro +++ b/sysfs_preload.pro @@ -2,7 +2,7 @@ TARGET = sysfs_preload TEMPLATE = lib QMAKE_RPATHDIR += /lib /usr/lib /opt/lib /opt/usr/lib -VERSION = 1.0.1 +VERSION = 1.0.2 CONFIG += hide_symbols CONFIG += c++17