]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
main: cleanup thread names and exit without throwing core on FatalError
authorMichael Matirko <mmatirko@cisco.com>
Tue, 7 Apr 2026 16:04:37 +0000 (12:04 -0400)
committerGitHub <noreply@github.com>
Tue, 7 Apr 2026 16:04:37 +0000 (12:04 -0400)
* main: name unnamed threads, rename snort threads to snort3

* log: on FatalError, _exit instead of exit() since the latter can cause a crash when cleaning up

12 files changed:
src/connectors/std_connector/std_connector_buffer.cc
src/connectors/tcp_connector/tcp_connector.cc
src/connectors/unixdomain_connector/unixdomain_connector.cc
src/detection/fp_utils.cc
src/detection/regex_offload.cc
src/file_api/file_capture.cc
src/framework/mp_data_bus.cc
src/log/batched_logger.cc
src/log/messages.cc
src/main/snort_config.cc
src/main/thread_config.cc
src/mp_transport/mp_unix_transport/mp_unix_transport.cc

index fc1afadd74e2ab618ac080a10a751ef9550b83ea..9357689b1945663c71a06f17b4dc0c2e316675cd 100644 (file)
 using namespace snort;
 using namespace std;
 
-#define FLUSHER_THREAD_NAME "std_connector.flusher"
+#define FLUSHER_AFFINITY_NAME "std_connector.flusher"
+#define FLUSHER_THREAD_NAME "snort3.stdc_fl"
 
 static void flusher(const SnortConfig* initial_config, const char* output,
     mutex& rings_mutex, list<Ring2>& rings, atomic_flag& latest, atomic_flag& run)
 {
     ThreadConfig *thread_config = initial_config->thread_config;
-    thread_config->implement_named_thread_affinity(FLUSHER_THREAD_NAME);
+    thread_config->implement_named_thread_affinity(FLUSHER_AFFINITY_NAME);
     SET_THREAD_NAME(pthread_self(), FLUSHER_THREAD_NAME);
 
     SnortConfig local_config;
index 7bdb9d433bb2d93299e86e81eda38cf1d0bf246c..6d867e753a9e0248cf8bb6a4327e8dc85c7cea1a 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "log/messages.h"
 #include "profiler/profiler_defs.h"
+#include "utils/util.h"
 
 #include "tcp_connector_module.h"
 
@@ -177,6 +178,7 @@ void TcpConnector::start_receive_thread()
 {
     run_thread.store(true, std::memory_order_relaxed);
     receive_thread = new std::thread(&TcpConnector::receive_processing_thread, this);
+    SET_THREAD_NAME(receive_thread->native_handle(), "snort3.tcp_rx");
 }
 
 void TcpConnector::stop_receive_thread()
index 2e45e42cf69157992249267db7db1266ed9b00ff..cd95b9810908f14736284105b343d84ef4c483fd 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "log/messages.h"
 #include "profiler/profiler_defs.h"
+#include "utils/util.h"
 
 #include "unixdomain_connector_module.h"
 
@@ -201,6 +202,7 @@ static void connection_retry_handler(const UnixDomainConnectorConfig& cfg, size_
 
 static void start_retry_thread(const UnixDomainConnectorConfig& cfg, size_t idx, UnixDomainConnectorUpdateHandler update_handler = nullptr) {
     std::thread retry_thread(connection_retry_handler, cfg, idx, update_handler, nullptr);
+    SET_THREAD_NAME(retry_thread.native_handle(), "snort3.ud_retry");
     retry_thread.detach();
 }
 
@@ -387,6 +389,7 @@ void UnixDomainConnector::receive_processing_thread() {
 void UnixDomainConnector::start_receive_thread() {
     run_thread.store(true, std::memory_order_relaxed);
     receive_thread = new std::thread(&UnixDomainConnector::receive_processing_thread, this);
+    SET_THREAD_NAME(receive_thread->native_handle(), "snort3.ud_recv");
 }
 
 void UnixDomainConnector::stop_receive_thread() {
@@ -637,6 +640,7 @@ void UnixDomainConnectorListener::start_accepting_connections(UnixDomainConnecto
     should_accept = true;
     accept_thread = new std::thread([this, handler = std::move(handler), config]()
     {
+        SET_THREAD_NAME(pthread_self(), "snort3.ud_acpt");
         sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
         if (sock_fd == -1) {
             ErrorMessage("UnixDomainC: socket error: %s \n", strerror(errno));
@@ -727,6 +731,7 @@ void UnixDomainConnectorReconnectHelper::connect(const char* path, size_t idx)
         if (cfg.conn_retries) {
             
             connection_thread = new std::thread(connection_retry_handler, cfg, idx, update_handler, this);
+            SET_THREAD_NAME(connection_thread->native_handle(), "snort3.ud_conn");
             return; 
         } else {
             close(sfd);
@@ -762,6 +767,7 @@ void UnixDomainConnectorReconnectHelper::reconnect(size_t idx)
     }
     
     connection_thread = new std::thread(connection_retry_handler, cfg, idx, update_handler, this);
+    SET_THREAD_NAME(connection_thread->native_handle(), "snort3.ud_conn");
 }
 
 void UnixDomainConnectorReconnectHelper::set_reconnect_enabled(bool enabled)
index 6fed82db7cffeed7355b5868c676324bad4cc9f3..34cd13a2af81a37f5c2b74bfa2da2a883ff8b142 100644 (file)
@@ -727,7 +727,11 @@ unsigned compile_mpses(struct SnortConfig* sc, bool parallel)
     }
 
     for ( unsigned i = 0; i < max; ++i )
-        workers.push_back(new std::thread(compile_mpse, sc, i, &count));
+    {
+        auto* w = new std::thread(compile_mpse, sc, i, &count);
+        SET_THREAD_NAME(w->native_handle(), "snort3.compile");
+        workers.push_back(w);
+    }
 
     for ( auto* w : workers )
     {
index acaeec2f59ff13bcbc5b7e341d8417b63e90beba..793b64141976d6e542da17b5fc253b5ecf7d87e5 100644 (file)
@@ -41,6 +41,7 @@
 #include "main/thread_config.h"
 #include "managers/module_manager.h"
 #include "utils/stats.h"
+#include "utils/util.h"
 
 using namespace snort;
 
@@ -185,6 +186,7 @@ ThreadRegexOffload::ThreadRegexOffload(unsigned max) : RegexOffload(max)
         ModuleManager::add_thread_stats_entry("search_engine");
         ModuleManager::add_thread_stats_entry("detection");
         req->thread = new std::thread(worker, req, sc, i++);
+        SET_THREAD_NAME(req->thread->native_handle(), "snort3.regex");
     }
 }
 
index b0f4459b47fd981b9dffca7ce0ee2e883b90a742..23e7d62134d7b22f10cdad87ee14e507e1a4edfa 100644 (file)
@@ -67,7 +67,7 @@ FileCaptureState FileCapture::error_capture(FileCaptureState state)
 // Only one writer thread supported
 void FileCapture::writer_thread()
 {
-    SET_THREAD_NAME(pthread_self(), "snort.filecap");
+    SET_THREAD_NAME(pthread_self(), "snort3.filecap");
 
     while (true)
     {
index d4f5b964839de72a29216901d4cbebe7d74b4807..9bc03e6f03d9f39e6e0e045190b06b2d524b1ec5 100644 (file)
@@ -308,6 +308,7 @@ void MPDataBus::start_worker_thread()
 {
     run_thread.store(true);
     worker_thread = std::make_unique<std::thread>(&MPDataBus::worker_thread_func, this);
+    SET_THREAD_NAME(worker_thread->native_handle(), "snort3.mp_dbus");
 }
 
 void MPDataBus::stop_worker_thread()
index cf94796d66bfe4c42d3732a966f9a0ab0a2d45d1..70a63b2ca0282103d40a5e3b2124313255f0c192 100644 (file)
@@ -392,7 +392,7 @@ void BatchedLogManager::init()
     snort::ThreadConfig* thread_config = snort::SnortConfig::get_conf()->thread_config;
     thread_config->implement_named_thread_affinity("BatchedLoggerWriter");
     writer_thread = std::thread(writer_thread_func);
-    SET_THREAD_NAME(writer_thread.native_handle(), "snort.logger");
+    SET_THREAD_NAME(writer_thread.native_handle(), "snort3.logger");
     thread_config->implement_thread_affinity(STHREAD_TYPE_MAIN, snort::ThreadConfig::DEFAULT_THREAD_ID);
 
     std::atexit(BatchedLogManager::shutdown);
index 98f3bfcd2ec8c1d712027d7a96d911e440a42c12..bd18c3891beadb3bd584c8d4fa831b19aab48702 100644 (file)
@@ -25,6 +25,7 @@
 #include "log_errors.h"
 
 #include <syslog.h>
+#include <unistd.h>
 
 #include <atomic>
 #include <cassert>
@@ -298,7 +299,10 @@ void ErrorMessage(const char* format,...)
     // -----------------------------
     // bail now if we are reentering
     if ( already_fatal )
-        exit(1);
+    {
+        fflush(NULL);
+        _exit(1);
+    }
     else
         already_fatal = 1;
     // -----------------------------
@@ -334,8 +338,8 @@ void ErrorMessage(const char* format,...)
     else
 #endif
     {
-        // FIXIT-M this makes no sense from main thread
-        exit(EXIT_FAILURE);
+        fflush(NULL);
+        _exit(EXIT_FAILURE);
     }
 }
 
index 90be74bb6d53ffedbdfa061f033eb0795d951e53..ff9f4642717fb51ff26bf1344310cc733fd59151 100644 (file)
@@ -1061,6 +1061,7 @@ void SnortConfig::generate_dump(std::list<ConfigData*> *config_data_to_dump)
     {
         config_dumper = new std::thread(generate_config_dump, config_data_to_dump,
             time(nullptr), SnortConfig::get_reload_id(), dump_config_file);
+        SET_THREAD_NAME(config_dumper->native_handle(), "snort3.cfgdump");
     }
     else
     {
index 0b701baedbb6eecd8ec86dbc23db0e2b07779541..b66feeae570d282e18cd6755b9e3ab4ce22c9f5f 100644 (file)
@@ -407,25 +407,25 @@ void ThreadConfig::implement_thread_affinity(SThreadType type, unsigned id)
     if (!hwloc_bitmap_isequal(current_cpuset, desired_cpuset))
     {
         LogMessage("Binding %s to CPU %s.\n", stringify_thread(type, id).c_str(), s);
-        thread_name_suffix = ".core-";
+        // "cX" = thread bound to core X
+        thread_name_suffix = "-c";
         thread_name_suffix.append(s);
     }
     else
     {
-        thread_name_suffix = ".ins-";
+        // "iX" = unbound instance number X
+        thread_name_suffix = "-i";
         thread_name_suffix.append(std::to_string(id));
     }
 
-    // Thread name is snort.ins-X for unpinned threads, and snort.core-X
-    // for threads pinned to CPU x
     if (type == STHREAD_TYPE_MAIN)
     {
-        thread_name = "snort3";
+        thread_name = "snort3.main";
         thread_name_suffix = "";
     }
     else
     {
-        thread_name = "snort";
+        thread_name = "s3.pkt";
     }
 
     thread_name.append(thread_name_suffix);
@@ -445,7 +445,8 @@ void ThreadConfig::implement_thread_affinity(SThreadType type, unsigned id)
         {
             char* fallback_s = nullptr;
             hwloc_bitmap_list_asprintf(&fallback_s, process_cpuset);
-            thread_name = "snort.core-";
+            // "cX" = thread bound to core X (fallback cpuset)
+            thread_name = "s3.pkt-c";
             thread_name.append(fallback_s ? fallback_s : "?");
             SET_THREAD_NAME(pthread_self(), thread_name.c_str());
             if (fallback_s) free(fallback_s);
index 6e869b635dc97b0928de177a4dc828b328f4c1e4..051add0267d61ff7df69c3b2549c4acff5ac3fd7 100644 (file)
@@ -37,6 +37,7 @@
 #include "log/messages.h"
 #include "main/snort.h"
 #include "main/snort_config.h"
+#include "utils/util.h"
 
 static std::mutex _receive_mutex;
 static std::shared_mutex _connection_update_mutex;
@@ -466,6 +467,7 @@ void MPUnixDomainTransport::init_side_channels()
 
     assert(!this->consume_thread);
     this->consume_thread = new std::thread(&MPUnixDomainTransport::process_messages_from_side_channels, this);
+    SET_THREAD_NAME(this->consume_thread->native_handle(), "snort3.mp_recv");
 }
 
 void MPUnixDomainTransport::cleanup_side_channels()