From 923c6328785f2f507999b52584655df854d83b4b Mon Sep 17 00:00:00 2001 From: emeric Date: Sun, 16 Feb 2025 13:16:06 +0100 Subject: [PATCH] Setting the size of the ipc pipe buffers is not mandatory, ref #614 --- src/libs/core/impl/ArchiveZipper.cpp | 6 ++-- src/libs/core/impl/ChildProcess.cpp | 41 ++++++++++++++++++--------- src/libs/core/impl/String.cpp | 9 ++++++ src/libs/core/include/core/String.hpp | 3 +- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/libs/core/impl/ArchiveZipper.cpp b/src/libs/core/impl/ArchiveZipper.cpp index 909a5b31e..15042e877 100644 --- a/src/libs/core/impl/ArchiveZipper.cpp +++ b/src/libs/core/impl/ArchiveZipper.cpp @@ -21,13 +21,13 @@ #include #include -#include // strerror #include #include #include #include "core/ILogger.hpp" +#include "core/String.hpp" namespace lms::zip { @@ -45,7 +45,7 @@ namespace lms::zip } FileException(const std::filesystem::path& p, std::string_view message, int err) - : Exception{ "File '" + p.string() + "': " + std::string{ message } + ": " + ::strerror(err) } + : Exception{ "File '" + p.string() + "': " + std::string{ message } + ": " + core::stringUtils::systemErrorToString(err) } { } }; @@ -75,7 +75,7 @@ namespace lms::zip { const int res{ ::archive_write_free(arch) }; if (res != ARCHIVE_OK) - LMS_LOG(UTILS, ERROR, "Failure while freeing archive control struct: " << std::string{ ::strerror(res) }); + LMS_LOG(UTILS, ERROR, "Failure while freeing archive control struct: error code = " << res); } void ArchiveZipper::ArchiveEntryDeleter::operator()(struct ::archive_entry* archEntry) diff --git a/src/libs/core/impl/ChildProcess.cpp b/src/libs/core/impl/ChildProcess.cpp index 599bd16b6..0f7b2ef94 100644 --- a/src/libs/core/impl/ChildProcess.cpp +++ b/src/libs/core/impl/ChildProcess.cpp @@ -20,10 +20,9 @@ #include "ChildProcess.hpp" #include -#include +#include #include #include -#include #include #include #include @@ -35,8 +34,8 @@ #include #include -#include "core/Exception.hpp" #include "core/ILogger.hpp" +#include "core/String.hpp" namespace lms::core { @@ -46,7 +45,7 @@ namespace lms::core { public: SystemException(int err, const std::string& errMsg) - : ChildProcessException{ errMsg + ": " + ::strerror(err) } + : ChildProcessException{ errMsg + ": " + stringUtils::systemErrorToString(err) } { } @@ -86,14 +85,30 @@ namespace lms::core throw SystemException{ errno, "fcntl failed to set FD_CLOEXEC!" }; #if defined(__linux__) && defined(F_SETPIPE_SZ) + for (const int fd : { pipefd[0], pipefd[1] }) { - // Just a hint here to prevent the writer from writing too many bytes ahead of the reader - constexpr std::size_t pipeSize{ 65536 * 4 }; - - if (fcntl(pipefd[0], F_SETPIPE_SZ, pipeSize) == -1) - throw SystemException{ errno, "fcntl failed!" }; - if (fcntl(pipefd[1], F_SETPIPE_SZ, pipeSize) == -1) - throw SystemException{ errno, "fcntl failed!" }; + constexpr std::size_t targetPipeSize{ static_cast(65'536) * 4 }; + std::size_t currentPipeSize{ 65'536 }; // common default value + #if defined(F_GETPIPE_SZ) + const int pipeSizeRes{ fcntl(fd, F_GETPIPE_SZ) }; + if (pipeSizeRes == -1) + { + const int err{ errno }; + LMS_LOG(CHILDPROCESS, DEBUG, "F_GETPIPE_SZ failed: " << stringUtils::systemErrorToString(err)); + } + else + { + currentPipeSize = pipeSizeRes; + } + #endif + if (currentPipeSize < targetPipeSize) + { + if (fcntl(fd, F_SETPIPE_SZ, targetPipeSize) == -1) + { + const int err{ errno }; + LMS_LOG(CHILDPROCESS, DEBUG, "F_SETPIPE_SZ failed: " << stringUtils::systemErrorToString(err)); + } + } } #endif @@ -153,7 +168,7 @@ namespace lms::core // process may already have finished LMS_LOG(CHILDPROCESS, DEBUG, "Killing child process..."); if (::kill(_childPID, SIGKILL) == -1) - LMS_LOG(CHILDPROCESS, DEBUG, "Kill failed: " << ::strerror(errno)); + LMS_LOG(CHILDPROCESS, DEBUG, "Kill failed: " << stringUtils::systemErrorToString(errno)); } bool ChildProcess::wait(bool block) @@ -165,7 +180,7 @@ namespace lms::core if (pid == -1) throw SystemException{ errno, "waitpid failed!" }; - else if (pid == 0) + if (pid == 0) return false; if (WIFEXITED(wstatus)) diff --git a/src/libs/core/impl/String.cpp b/src/libs/core/impl/String.cpp index fd437c021..418a8d33a 100644 --- a/src/libs/core/impl/String.cpp +++ b/src/libs/core/impl/String.cpp @@ -20,6 +20,8 @@ #include "core/String.hpp" #include +#include +#include #include #include @@ -504,4 +506,11 @@ namespace lms::core::stringUtils return "[" + std::to_string(mins) + ":" + (secs < 10 ? "0" : "") + std::to_string(secs) + "." + (millis < 100 ? (millis < 10 ? "00" : "0") : "") + std::to_string(millis) + "]"; } + + std::string systemErrorToString(int err) + { + std::array buffer{}; + ::strerror_r(err, buffer.data(), buffer.size()); + return std::string{ buffer.data() }; + } } // namespace lms::core::stringUtils diff --git a/src/libs/core/include/core/String.hpp b/src/libs/core/include/core/String.hpp index 31542808b..7c99a7ccd 100644 --- a/src/libs/core/include/core/String.hpp +++ b/src/libs/core/include/core/String.hpp @@ -20,7 +20,6 @@ #pragma once #include -#include #include #include #include @@ -120,4 +119,6 @@ namespace lms::core::stringUtils // to "[minutes:seconds.milliseconds]" std::string formatTimestamp(std::chrono::milliseconds timestamp); + + std::string systemErrorToString(int err); } // namespace lms::core::stringUtils \ No newline at end of file