]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4857: helpers: add syscall to flush new data written by SigSafePrinter...
authorVolodymyr Shpyrka -X (vshpyrka - SOFTSERVE INC at Cisco) <vshpyrka@cisco.com>
Fri, 15 Aug 2025 14:23:20 +0000 (14:23 +0000)
committerOleksii Shumeiko -X (oshumeik - SOFTSERVE INC at Cisco) <oshumeik@cisco.com>
Fri, 15 Aug 2025 14:23:20 +0000 (14:23 +0000)
Merge in SNORT/snort3 from ~VSHPYRKA/snort3:unwind_bt_flush_enhance to master

Squashed commit of the following:

commit adef60447da7ee3f4d5b0a5becd14fb030907bf5
Author: Volodymyr Shpyrka <vshpyrka@cisco.com>
Date:   Tue Aug 12 08:37:01 2025 -0400

    helpers: add syscall to flush new data written by SigSafePrinter to disk

src/helpers/sigsafe.cc
src/helpers/sigsafe.h
src/main/process.cc

index be9c5ce55513d5b6abc818c4cba0af02ebdd93e3..a6c59902bdd4fd313e614b02dbfd231368b9830c 100644 (file)
@@ -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 <cinttypes>
+#include <iostream>
 
 #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
index d665b3b8057c6f359536084bf8b800d9a8c8f9ce..fb7f666e4717e2a5e4847d3d4e35791bf8b81364 100644 (file)
@@ -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, ...);
 
index c0efd749592cab93c94b050892fbae282f67cec9..87fa290e206a36cf6a6e58ad0e80e5182647306d 100644 (file)
@@ -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);
 }