From 906a1259c338bc72c4554c641e37557281981abf Mon Sep 17 00:00:00 2001 From: scorpion-26 <58082714+scorpion-26@users.noreply.github.com> Date: Sun, 12 Feb 2023 20:45:05 +0100 Subject: [PATCH] Add Net sanity checking This disables network if the tx_bytes file is not found on Init() --- src/Bar.cpp | 2 +- src/Config.h | 2 ++ src/System.cpp | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Bar.cpp b/src/Bar.cpp index c6aaf1b..d4982fa 100644 --- a/src/Bar.cpp +++ b/src/Bar.cpp @@ -555,7 +555,7 @@ namespace Bar if (RuntimeConfig::Get().hasBlueZ) WidgetBluetooth(*right); #endif - if (Config::Get().networkWidget) + if (Config::Get().networkWidget && RuntimeConfig::Get().hasNet) WidgetNetwork(*right); WidgetSensors(*right); diff --git a/src/Config.h b/src/Config.h index fe3c03b..ab11249 100644 --- a/src/Config.h +++ b/src/Config.h @@ -57,5 +57,7 @@ public: bool hasBlueZ = false; #endif + bool hasNet = true; + static RuntimeConfig& Get(); }; diff --git a/src/System.cpp b/src/System.cpp index fd427d7..61e42cd 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -467,10 +467,23 @@ namespace System } #endif + void CheckNetwork() + { + std::ifstream bytes("/sys/class/net/" + Config::Get().networkAdapter + "/statistics/tx_bytes"); + if (!bytes.is_open()) + { + LOG("Cannot open network device! Disabling Network widget."); + RuntimeConfig::Get().hasNet = false; + } + } + double GetNetworkBpsCommon(double dt, uint64_t& prevBytes, const std::string& deviceFile) { + if (!RuntimeConfig::Get().hasNet) + { + return 0.f; + } std::ifstream bytes(deviceFile); - ASSERT(bytes.is_open(), "Couldn't open " << deviceFile); std::string bytesStr; std::getline(bytes, bytesStr); @@ -563,6 +576,8 @@ namespace System #endif PulseAudio::Init(); + + CheckNetwork(); } void FreeResources() {