From: Michael Matirko Date: Tue, 7 Apr 2026 16:04:37 +0000 (-0400) Subject: main: cleanup thread names and exit without throwing core on FatalError X-Git-Tag: 3.12.2.0~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ffc0ff565b5fd3b9d0d84c5e86d6ba3fc9d66dc2;p=thirdparty%2Fsnort3.git main: cleanup thread names and exit without throwing core on FatalError * 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 --- diff --git a/src/connectors/std_connector/std_connector_buffer.cc b/src/connectors/std_connector/std_connector_buffer.cc index fc1afadd7..9357689b1 100644 --- a/src/connectors/std_connector/std_connector_buffer.cc +++ b/src/connectors/std_connector/std_connector_buffer.cc @@ -36,13 +36,14 @@ 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& 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; diff --git a/src/connectors/tcp_connector/tcp_connector.cc b/src/connectors/tcp_connector/tcp_connector.cc index 7bdb9d433..6d867e753 100644 --- a/src/connectors/tcp_connector/tcp_connector.cc +++ b/src/connectors/tcp_connector/tcp_connector.cc @@ -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() diff --git a/src/connectors/unixdomain_connector/unixdomain_connector.cc b/src/connectors/unixdomain_connector/unixdomain_connector.cc index 2e45e42cf..cd95b9810 100644 --- a/src/connectors/unixdomain_connector/unixdomain_connector.cc +++ b/src/connectors/unixdomain_connector/unixdomain_connector.cc @@ -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) diff --git a/src/detection/fp_utils.cc b/src/detection/fp_utils.cc index 6fed82db7..34cd13a2a 100644 --- a/src/detection/fp_utils.cc +++ b/src/detection/fp_utils.cc @@ -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 ) { diff --git a/src/detection/regex_offload.cc b/src/detection/regex_offload.cc index acaeec2f5..793b64141 100644 --- a/src/detection/regex_offload.cc +++ b/src/detection/regex_offload.cc @@ -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"); } } diff --git a/src/file_api/file_capture.cc b/src/file_api/file_capture.cc index b0f4459b4..23e7d6213 100644 --- a/src/file_api/file_capture.cc +++ b/src/file_api/file_capture.cc @@ -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) { diff --git a/src/framework/mp_data_bus.cc b/src/framework/mp_data_bus.cc index d4f5b9648..9bc03e6f0 100644 --- a/src/framework/mp_data_bus.cc +++ b/src/framework/mp_data_bus.cc @@ -308,6 +308,7 @@ void MPDataBus::start_worker_thread() { run_thread.store(true); worker_thread = std::make_unique(&MPDataBus::worker_thread_func, this); + SET_THREAD_NAME(worker_thread->native_handle(), "snort3.mp_dbus"); } void MPDataBus::stop_worker_thread() diff --git a/src/log/batched_logger.cc b/src/log/batched_logger.cc index cf94796d6..70a63b2ca 100644 --- a/src/log/batched_logger.cc +++ b/src/log/batched_logger.cc @@ -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); diff --git a/src/log/messages.cc b/src/log/messages.cc index 98f3bfcd2..bd18c3891 100644 --- a/src/log/messages.cc +++ b/src/log/messages.cc @@ -25,6 +25,7 @@ #include "log_errors.h" #include +#include #include #include @@ -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); } } diff --git a/src/main/snort_config.cc b/src/main/snort_config.cc index 90be74bb6..ff9f46427 100644 --- a/src/main/snort_config.cc +++ b/src/main/snort_config.cc @@ -1061,6 +1061,7 @@ void SnortConfig::generate_dump(std::list *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 { diff --git a/src/main/thread_config.cc b/src/main/thread_config.cc index 0b701baed..b66feeae5 100644 --- a/src/main/thread_config.cc +++ b/src/main/thread_config.cc @@ -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); diff --git a/src/mp_transport/mp_unix_transport/mp_unix_transport.cc b/src/mp_transport/mp_unix_transport/mp_unix_transport.cc index 6e869b635..051add026 100644 --- a/src/mp_transport/mp_unix_transport/mp_unix_transport.cc +++ b/src/mp_transport/mp_unix_transport/mp_unix_transport.cc @@ -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()