]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
lib: remove global worker id variable
authorJason Ish <jason.ish@oisf.net>
Wed, 24 Apr 2024 22:47:11 +0000 (16:47 -0600)
committerVictor Julien <victor@inliniac.net>
Tue, 1 Apr 2025 08:17:05 +0000 (10:17 +0200)
Update ThreadVars creation in lib mode to have the worker_id provided
by the user.

Ticket: #7240

examples/lib/custom/main.c
src/runmode-lib.c
src/runmode-lib.h

index 150891549013e1567ab417e4b3efd27bdb45b4f9..11c10ab6ff86fbcb630691ea9d17cd6c2496ed50 100644 (file)
@@ -22,6 +22,8 @@
 #include "source-lib.h"
 #include "threadvars.h"
 
+static int worker_id = 1;
+
 /**
  * Suricata worker thread in library mode.
  * The functions should be wrapped in an API layer.
@@ -31,7 +33,7 @@ static void *SimpleWorker(void *arg)
     char *pcap_file = (char *)arg;
 
     /* Create worker. */
-    ThreadVars *tv = SCRunModeLibCreateThreadVars();
+    ThreadVars *tv = SCRunModeLibCreateThreadVars(worker_id++);
     if (!tv) {
         pthread_exit(NULL);
     }
index 97f8d9d78efbac8da41235aaa8ae7084995ad55a..b5643d99c8337a0f4d155d8370058619bfca490d 100644 (file)
@@ -25,9 +25,6 @@
 #include "runmode-lib.h"
 #include "runmodes.h"
 #include "tm-threads.h"
-#include "util-device.h"
-
-static int g_thread_id = 0;
 
 /** \brief register runmodes for suricata as a library */
 void RunModeIdsLibRegister(void)
@@ -59,11 +56,11 @@ const char *RunModeLibGetDefaultMode(void)
     return "live";
 }
 
-ThreadVars *SCRunModeLibCreateThreadVars(void)
+ThreadVars *SCRunModeLibCreateThreadVars(int worker_id)
 {
     char tname[TM_THREAD_NAME_MAX];
     TmModule *tm_module = NULL;
-    snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, ++g_thread_id);
+    snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, worker_id);
 
     ThreadVars *tv = TmThreadCreatePacketHandler(
             tname, "packetpool", "packetpool", "packetpool", "packetpool", "lib");
@@ -142,7 +139,6 @@ void RunModeDestroyWorker(void *td)
     }
 
     tv->stream_pq = NULL;
-    --g_thread_id;
     SCLogDebug("%s ending", tv->name);
     TmThreadsSetFlag(tv, THV_CLOSED);
 }
index 86a1b812d05e31671669643c5f133d7398dad576..44c8c3a4748c9c5cb80c33f71cf63fac12cbd200 100644 (file)
@@ -45,9 +45,11 @@ const char *RunModeLibGetDefaultMode(void);
  * Unlike other runmodes, this does not spawn a thread, as the threads
  * are controlled by the application using Suricata as a library.
  *
+ * \param worker_id an ID to give this ThreadVars instance
+ *
  * \return Pointer to allocated ThreadVars or NULL on failure
  */
-ThreadVars *SCRunModeLibCreateThreadVars(void);
+ThreadVars *SCRunModeLibCreateThreadVars(int worker_id);
 
 /** \brief start the "fake" worker.
  *