diff --git a/include/metrics/ProcessMetrics.hpp b/include/metrics/ProcessMetrics.hpp index 5690e040d..1b2c3f01d 100644 --- a/include/metrics/ProcessMetrics.hpp +++ b/include/metrics/ProcessMetrics.hpp @@ -30,10 +30,8 @@ class ProcessMetrics { size_t _oldReadBytes{0}; ///< Variable to store the old read bytes size_t _oldWriteBytes{0}; ///< Variable to store the old write bytes - clock_t _oldCpuTime{0}; ///< Variable to store the old CPU time - struct tms _oldCpu { - 0, 0, 0, 0 - }; ///< Structure to store the old CPU times + clock_t _oldCpuTime{0}; ///< Variable to store the old CPU time + struct tms _oldCpu{0, 0, 0, 0}; ///< Structure to store the old CPU times /** * Counts the number of entries in a directory. diff --git a/include/telnet/TelnetServer.hpp b/include/telnet/TelnetServer.hpp index 16a0b6789..b45866a64 100644 --- a/include/telnet/TelnetServer.hpp +++ b/include/telnet/TelnetServer.hpp @@ -174,7 +174,7 @@ class TelnetServer : public std::enable_shared_from_this { const VEC_SP_TelnetSession &sessions() const { return m_sessions; } - bool interactivePrompt() const { return m_promptString.length() > 0; } + bool interactivePrompt() const { return !m_promptString.empty(); } void promptString(const std::string_view &prompt) { m_promptString = prompt; } const std::string &promptString() const { return m_promptString; } @@ -207,6 +207,12 @@ class TelnetServer : public std::enable_shared_from_this { std::shared_ptr m_checkFlag; /**< Runtime check flag */ }; +/** + * Print available commands to the session + * @param[in] session Handle to session + */ +void TelnetPrintAvailableCommands(const SP_TelnetSession &session); + /** * Telnet session connection start callback * @param[in] session Handle to session diff --git a/include/utils/BaseServerStats.hpp b/include/utils/BaseServerStats.hpp index a07ebddd5..8e5a279de 100644 --- a/include/utils/BaseServerStats.hpp +++ b/include/utils/BaseServerStats.hpp @@ -3,10 +3,7 @@ #include #define QUANTILE_DEFAULTS \ - prometheus::Summary::Quantiles \ - { \ - {0.5, 0.1}, {0.9, 0.1}, { 0.99, 0.1 } \ - } + prometheus::Summary::Quantiles { {0.5, 0.1}, {0.9, 0.1}, {0.99, 0.1} } /** * @class BaseServerStats diff --git a/include/utils/Tracer.hpp b/include/utils/Tracer.hpp index 60cadc8d9..e8c8ded64 100644 --- a/include/utils/Tracer.hpp +++ b/include/utils/Tracer.hpp @@ -92,7 +92,7 @@ class Tracer { * Check if the crashpad_handler process is running * @return true if the crashpad_handler is running, false otherwise */ - [[nodiscard]] bool isRunning() const; + [[nodiscard]] static bool isRunning(); /** * Check and restart the crashpad_handler process if it is not running diff --git a/src/logging/Sentry.cpp b/src/logging/Sentry.cpp index 46a283c1c..67626e3c4 100644 --- a/src/logging/Sentry.cpp +++ b/src/logging/Sentry.cpp @@ -17,10 +17,10 @@ // MAC address length for character string constexpr int MAC_LEN = 18; -namespace spdlog::sinks +namespace { /// Set version related information to the Sentry context - inline void setVersionContext() + void setVersionContext() { std::string versionBuffer; const sentry_value_t versionContext = sentry_value_new_object(); @@ -32,7 +32,7 @@ namespace spdlog::sinks } /// Set host related information to the Sentry context - inline void setHostContext() + void setHostContext() { std::array hostBuffer{}; gethostname(hostBuffer.data(), BUFSIZ); @@ -56,7 +56,7 @@ namespace spdlog::sinks } /// Set network related information to the Sentry context - inline void setNetworkContext() + void setNetworkContext() { ifaddrs *ifaddr = nullptr; if (getifaddrs(&ifaddr) < 0) @@ -111,7 +111,10 @@ namespace spdlog::sinks sentry_set_context("Network", networkContext); } +} // namespace +namespace spdlog::sinks +{ template sentry_api_sink::sentry_api_sink(const std::string &sentryAddress) { if (sentryAddress.empty()) @@ -173,12 +176,16 @@ namespace spdlog::sinks case spdlog::level::info: case spdlog::level::off: // For lower levels, do nothing for now. But you can easily handle them here. + break; default: break; } } - template void sentry_api_sink::flush_() {} + template void sentry_api_sink::flush_() + { + // Sentry library handles all operations, so no need to flush + } template class sentry_api_sink; template class sentry_api_sink; diff --git a/src/main.cpp b/src/main.cpp index 1629a9a34..91cdd4079 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,12 +15,13 @@ // SIGALRM interval in seconds constexpr uintmax_t alarmInterval = 30; -// Interruption flag -// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -volatile sig_atomic_t interruptFlag = 0; namespace { + // Interruption flag + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) + volatile sig_atomic_t interruptFlag = 0; + // Default SIGALRM function void alarmFunc(int /*unused*/) { diff --git a/src/metrics/Performance.cpp b/src/metrics/Performance.cpp index 47e3b3ed0..885246a79 100644 --- a/src/metrics/Performance.cpp +++ b/src/metrics/Performance.cpp @@ -4,10 +4,7 @@ #include #define QUANTILE_DEFAULTS \ - prometheus::Summary::Quantiles \ - { \ - {0.5, 0.1}, {0.9, 0.1}, { 0.99, 0.1 } \ - } + prometheus::Summary::Quantiles { {0.5, 0.1}, {0.9, 0.1}, {0.99, 0.1} } PerformanceTracker::PerformanceTracker(const std::shared_ptr ®, const std::string &name, uint64_t metricID) diff --git a/src/metrics/ProcessMetrics.cpp b/src/metrics/ProcessMetrics.cpp index 3357b208b..a7d3374c3 100644 --- a/src/metrics/ProcessMetrics.cpp +++ b/src/metrics/ProcessMetrics.cpp @@ -27,14 +27,14 @@ size_t ProcessMetrics::countDirectoryEntries(const std::string &path) long int ProcessMetrics::getMemoryUsage() { - struct rusage r_usage {}; + struct rusage r_usage{}; getrusage(RUSAGE_SELF, &r_usage); return r_usage.ru_maxrss; // NOLINT(cppcoreguidelines-pro-type-union-access) } long int ProcessMetrics::getPageFaults() { - struct rusage r_usage {}; + struct rusage r_usage{}; getrusage(RUSAGE_SELF, &r_usage); return r_usage.ru_majflt; // NOLINT(cppcoreguidelines-pro-type-union-access) } @@ -51,7 +51,7 @@ double ProcessMetrics::getCpuUsage() return 0.0; } - struct tms nowCpu {}; + struct tms nowCpu{}; auto nowCpuTime = times(&nowCpu); const double usage = 100.0 * static_cast(nowCpu.tms_utime - _oldCpu.tms_utime) / diff --git a/src/telnet/TelnetServer.cpp b/src/telnet/TelnetServer.cpp index c92bd8e83..103da4a2b 100644 --- a/src/telnet/TelnetServer.cpp +++ b/src/telnet/TelnetServer.cpp @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -129,7 +130,7 @@ void TelnetSession::sendPromptAndBuffer() } // Resend the buffer - if (m_buffer.length() > 0) + if (!m_buffer.empty()) { sendBytes = send(m_socket, m_buffer.c_str(), m_buffer.length(), 0); if (sendBytes > 0) @@ -160,7 +161,7 @@ void TelnetSession::eraseLine() void TelnetSession::sendLine(std::string data) { // If is something is on the prompt, wipe it off - if (m_telnetServer->interactivePrompt() || m_buffer.length() > 0) + if (m_telnetServer->interactivePrompt() || !m_buffer.empty()) { eraseLine(); } @@ -334,25 +335,26 @@ bool TelnetSession::processTab(std::string &buffer) do { found = buffer.find_first_of('\t'); - if (found != std::string::npos) + if (found == std::string::npos) { - // Remove single tab - if (buffer.length() > 0) - { - buffer.erase(found, 1); - } - foundTabs = true; + continue; + } + + // Remove single tab + if (!buffer.empty()) + { + buffer.erase(found, 1); + } + foundTabs = true; - // Process - if (m_telnetServer->tabCallback()) + // Process + if (m_telnetServer->tabCallback()) + { + const std::string retCommand = m_telnetServer->tabCallback()(shared_from_this(), buffer.substr(0, found)); + if (!retCommand.empty()) { - const std::string retCommand = - m_telnetServer->tabCallback()(shared_from_this(), buffer.substr(0, found)); - if (!retCommand.empty()) - { - buffer.erase(0, found); - buffer.insert(0, retCommand); - } + buffer.erase(0, found); + buffer.insert(0, retCommand); } } } while (found != std::string::npos); @@ -473,14 +475,8 @@ void TelnetSession::update() // we've got to be careful here. Telnet client might send null characters for New Lines mid-data block. We need // to swap these out. recv is not null terminated, so its cool - for (size_t i = 0; i < static_cast(readBytes); i++) - { - if (recvbuf[i] == ASCII_NULL) // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) - { - // New Line constant - recvbuf[i] = ASCII_LF; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) - } - } + std::replace_if( + recvbuf.begin(), recvbuf.begin() + readBytes, [](char chr) { return chr == ASCII_NULL; }, ASCII_LF); // Add it to the received buffer m_buffer.append(recvbuf.data(), static_cast(readBytes)); @@ -513,21 +509,16 @@ void TelnetSession::update() auto lines = getCompleteLines(m_buffer); for (const auto &line : lines) { - if (m_telnetServer->newLineCallBack()) + if (!m_telnetServer->newLineCallBack()) { - if (m_telnetServer->newLineCallBack()(shared_from_this(), line)) - { - ++stats.successCmdCtr; - } - else - { - ++stats.failCmdCtr; - } - addToHistory(line); + break; } + + m_telnetServer->newLineCallBack()(shared_from_this(), line) ? ++stats.successCmdCtr : ++stats.failCmdCtr; + addToHistory(line); } - if (m_telnetServer->interactivePrompt() && requirePromptReprint) + if (requirePromptReprint && m_telnetServer->interactivePrompt()) { eraseLine(); sendPromptAndBuffer(); diff --git a/src/utils/ConfigParser.cpp b/src/utils/ConfigParser.cpp index d1d0630e3..c3ec5e26e 100644 --- a/src/utils/ConfigParser.cpp +++ b/src/utils/ConfigParser.cpp @@ -7,13 +7,16 @@ #include -template std::string stringifyRapidjson(const T &obj) +namespace { - rapidjson::StringBuffer sbuffer; - rapidjson::Writer writer(sbuffer); - obj.Accept(writer); - return sbuffer.GetString(); -} + template std::string stringifyRapidjson(const T &obj) + { + rapidjson::StringBuffer sbuffer; + rapidjson::Writer writer(sbuffer); + obj.Accept(writer); + return sbuffer.GetString(); + } +} // namespace void ConfigParser::readJson() { diff --git a/src/utils/Tracer.cpp b/src/utils/Tracer.cpp index 57b595f97..e59ed14b0 100644 --- a/src/utils/Tracer.cpp +++ b/src/utils/Tracer.cpp @@ -61,6 +61,8 @@ void Tracer::threadFunc() noexcept { if (!isRunning()) { + // NOTICE: Worked before but currently restarting the handler raises an error + // due to changes in the crashpad library restart(); spdlog::warn("Crashpad handler restarted"); } @@ -99,12 +101,12 @@ std::string Tracer::getSelfExecutableDir() return (lastDelimPos == std::string::npos) ? "" : path.substr(0, lastDelimPos); } -bool Tracer::isRunning() const +bool Tracer::isRunning() { int sockId{-1}; pid_t processId{-1}; - if (!_clientHandler->GetHandlerSocket(&sockId, &processId)) + if (!crashpad::CrashpadClient::GetHandlerSocket(&sockId, &processId)) { return false; }