From: Jason Ish Date: Wed, 24 Apr 2024 22:47:11 +0000 (-0600) Subject: lib: remove global worker id variable X-Git-Tag: suricata-8.0.0-beta1~145 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60860e43ac89c2dd5f382f66caa927447f1a5c06;p=thirdparty%2Fsuricata.git lib: remove global worker id variable Update ThreadVars creation in lib mode to have the worker_id provided by the user. Ticket: #7240 --- diff --git a/examples/lib/custom/main.c b/examples/lib/custom/main.c index 1508915490..11c10ab6ff 100644 --- a/examples/lib/custom/main.c +++ b/examples/lib/custom/main.c @@ -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); } diff --git a/src/runmode-lib.c b/src/runmode-lib.c index 97f8d9d78e..b5643d99c8 100644 --- a/src/runmode-lib.c +++ b/src/runmode-lib.c @@ -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); } diff --git a/src/runmode-lib.h b/src/runmode-lib.h index 86a1b812d0..44c8c3a474 100644 --- a/src/runmode-lib.h +++ b/src/runmode-lib.h @@ -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. *