From: Michael Matirko (mmatirko) Date: Tue, 15 Oct 2024 14:59:09 +0000 (+0000) Subject: Pull request #4484: main: implement function to grab relative process id X-Git-Tag: 3.4.0.0 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60f37890b5c06db85b7679e6238a9d07085df4c9;p=thirdparty%2Fsnort3.git Pull request #4484: main: implement function to grab relative process id Merge in SNORT/snort3 from ~MMATIRKO/snort3:proc_id to master Squashed commit of the following: commit 52dac29650af818dd6d336100f3fb46b468fd854 Author: Michael Matirko Date: Fri Oct 11 12:45:56 2024 -0400 main: implement function to grab relative process id --- diff --git a/src/main/snort.cc b/src/main/snort.cc index 67590c2b7..ef12ace81 100644 --- a/src/main/snort.cc +++ b/src/main/snort.cc @@ -84,6 +84,10 @@ #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::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 + diff --git a/src/main/snort.h b/src/main/snort.h index 0ce5186d7..375c30fb5 100644 --- a/src/main/snort.h +++ b/src/main/snort.h @@ -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();