From: Jason Ish Date: Mon, 30 Sep 2024 22:34:04 +0000 (-0600) Subject: examples/lib: create threadvars from main thread X-Git-Tag: suricata-8.0.0-beta1~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04161155ecf6d22abb713c6a6ce42a75c8a6c46b;p=thirdparty%2Fsuricata.git examples/lib: create threadvars from main thread This also allows us to remove the sleep, as the ThreadVars are now guaranteed to be created before PostInit. Ticket: #7240 --- diff --git a/examples/lib/custom/main.c b/examples/lib/custom/main.c index 34d65d74cb..b910b32887 100644 --- a/examples/lib/custom/main.c +++ b/examples/lib/custom/main.c @@ -27,6 +27,14 @@ static int worker_id = 1; +/** + * Struct to pass arguments into a worker thread. + */ +struct WorkerArgs { + ThreadVars *tv; + char *pcap_filename; +}; + /** * Release packet callback. * @@ -57,13 +65,8 @@ static void ReleasePacket(Packet *p) */ static void *SimpleWorker(void *arg) { - char *pcap_file = (char *)arg; - - /* Create worker. */ - ThreadVars *tv = SCRunModeLibCreateThreadVars(worker_id++); - if (!tv) { - pthread_exit(NULL); - } + struct WorkerArgs *args = arg; + ThreadVars *tv = args->tv; /* Start worker. */ if (SCRunModeLibSpawnWorker(tv) != 0) { @@ -71,7 +74,7 @@ static void *SimpleWorker(void *arg) } /* Replay pcap. */ - pcap_t *fp = pcap_open_offline(pcap_file, NULL); + pcap_t *fp = pcap_open_offline(args->pcap_filename, NULL); if (fp == NULL) { pthread_exit(NULL); } @@ -194,16 +197,18 @@ int main(int argc, char **argv) * file as argument. This needs to be done in between SuricataInit * and SuricataPostInit. */ pthread_t worker; - if (pthread_create(&worker, NULL, SimpleWorker, argv[argc - 1]) != 0) { + ThreadVars *tv = SCRunModeLibCreateThreadVars(worker_id++); + if (!tv) { + FatalError("Failed to create ThreadVars"); + } + struct WorkerArgs args = { + .tv = tv, + .pcap_filename = argv[argc - 1], + }; + if (pthread_create(&worker, NULL, SimpleWorker, &args) != 0) { exit(EXIT_FAILURE); } - /* Need to introduce a little sleep to allow the worker thread to - * initialize before SuricataPostInit invokes - * TmThreadContinueThreads(). This should be handle at the API - * level. */ - usleep(100); - SuricataPostInit(); /* Shutdown engine. */