]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4484: main: implement function to grab relative process id 3.4.0.0
authorMichael Matirko (mmatirko) <mmatirko@cisco.com>
Tue, 15 Oct 2024 14:59:09 +0000 (14:59 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Tue, 15 Oct 2024 14:59:09 +0000 (14:59 +0000)
Merge in SNORT/snort3 from ~MMATIRKO/snort3:proc_id to master

Squashed commit of the following:

commit 52dac29650af818dd6d336100f3fb46b468fd854
Author: Michael Matirko <mmatirko@cisco.com>
Date:   Fri Oct 11 12:45:56 2024 -0400

    main: implement function to grab relative process id

src/main/snort.cc
src/main/snort.h

index 67590c2b7527d16d70e5ae38e522b7f7bf5dd1a7..ef12ace815ef278254cefe08280c083d267eea2f 100644 (file)
 #include "ac_shell_cmd.h"
 #endif
 
+#ifdef UNIT_TEST
+#include "catch/snort_catch.h"
+#endif
+
 #include "snort_config.h"
 #include "thread_config.h"
 
@@ -386,6 +390,15 @@ bool Snort::is_reloading()
 bool Snort::has_dropped_privileges()
 { return privileges_dropped; }
 
+unsigned Snort::get_process_id()
+{
+    const SnortConfig* sc = SnortConfig::get_conf();
+    if (!sc->id_offset)
+        return 1;
+    else
+        return sc->id_offset / ThreadConfig::get_instance_max() + 1;
+}
+
 void Snort::setup(int argc, char* argv[])
 {
     set_main_thread();
@@ -606,3 +619,40 @@ SnortConfig* Snort::get_updated_policy(
     return sc;
 }
 
+// -----------------------------------------------------------------------------
+// unit tests
+// -----------------------------------------------------------------------------
+
+#ifdef UNIT_TEST
+
+TEST_CASE("Check process ID handling", "[snort_process_id]")
+{
+    // Mock first process
+    snort::SnortConfig* sc = const_cast<snort::SnortConfig*>(snort::SnortConfig::get_conf());
+    snort::ThreadConfig::set_instance_max(4);
+    sc->id_offset = 0;
+    unsigned pid1 = Snort::get_process_id();
+    CHECK(pid1 == 1);
+
+    // Mock second process
+    sc->id_offset = 5;
+    unsigned pid2 = Snort::get_process_id();
+    CHECK(pid2 == 2);
+
+    // Mock third process
+    sc->id_offset = 9;
+    unsigned pid3 = Snort::get_process_id();
+    CHECK(pid3 == 3);
+
+    // Mock fourth process
+    sc->id_offset = 13;
+    unsigned pid4 = Snort::get_process_id();
+    CHECK(pid4 == 4);
+
+    // Restore prior configs
+    snort::ThreadConfig::set_instance_max(1);
+    sc->id_offset = 0;
+}
+
+#endif
+
index 0ce5186d759c123dffecaf3990f8724c48217990..375c30fb5762aa5d969c951a0ebb31e7bc8c82e9 100644 (file)
@@ -48,6 +48,8 @@ public:
     static bool is_exiting() { return already_exiting; }
     static bool is_reloading();
 
+    static unsigned get_process_id();
+
 private:
     static void init(int, char**);
     static void term();