From: Volodymyr Shpyrka -X (vshpyrka - SOFTSERVE INC at Cisco) Date: Fri, 15 Aug 2025 14:23:20 +0000 (+0000) Subject: Pull request #4857: helpers: add syscall to flush new data written by SigSafePrinter... X-Git-Tag: 3.9.5.0~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=69dff3d9733ab3635592455f621ceb1b9fdcd9d4;p=thirdparty%2Fsnort3.git Pull request #4857: helpers: add syscall to flush new data written by SigSafePrinter to disk Merge in SNORT/snort3 from ~VSHPYRKA/snort3:unwind_bt_flush_enhance to master Squashed commit of the following: commit adef60447da7ee3f4d5b0a5becd14fb030907bf5 Author: Volodymyr Shpyrka Date: Tue Aug 12 08:37:01 2025 -0400 helpers: add syscall to flush new data written by SigSafePrinter to disk --- diff --git a/src/helpers/sigsafe.cc b/src/helpers/sigsafe.cc index be9c5ce55..a6c59902b 100644 --- a/src/helpers/sigsafe.cc +++ b/src/helpers/sigsafe.cc @@ -264,9 +264,17 @@ void SigSafePrinter::printf(const char *format, ...) write_string(fmt_buf); } +int SigSafePrinter::flush() +{ + if (fd >= 0) + return fsync(fd); + return 0; +} + #ifdef CATCH_TEST_BUILD #include +#include #include "catch/catch.hpp" @@ -446,6 +454,19 @@ TEST_CASE("sigsafe printer", "[SigsafePrinter]") SigSafePrinter(actual, sizeof(actual)).hex_dump(data, sizeof(data)); CHECK_THAT(expected, Equals(actual)); } + SECTION("flush buffer") + { + snprintf(expected, sizeof(expected), "%32s", "test"); + SigSafePrinter ssp(actual, sizeof(actual)); + ssp.printf("%32s", "test"); + ssp.flush(); + CHECK_THAT(expected, Equals(actual)); + } + SECTION("flush fd") + { + SigSafePrinter ssp(STDOUT_FILENO); + ssp.flush(); + } } #endif diff --git a/src/helpers/sigsafe.h b/src/helpers/sigsafe.h index d665b3b80..fb7f666e4 100644 --- a/src/helpers/sigsafe.h +++ b/src/helpers/sigsafe.h @@ -31,6 +31,7 @@ public: SigSafePrinter(char *buf, size_t size); SigSafePrinter(int fd) : fd(fd) { } + int flush(); void hex_dump(const uint8_t* data, unsigned len); void printf(const char* format, ...); diff --git a/src/main/process.cc b/src/main/process.cc index c0efd7495..87fa290e2 100644 --- a/src/main/process.cc +++ b/src/main/process.cc @@ -353,6 +353,9 @@ static void oops_handler(int signal) if ( !is_main_thread ) OopsHandler::handle_crash(STDERR_FILENO); + if (ssp.flush() != 0) + ssp.printf("Failed to flush output. Error: %s\n", strerror(errno)); + // Finally, raise the signal so that the original handler can handle it. raise(signal); }