diff --git a/src/runmode-erf-file.c b/src/runmode-erf-file.c index c8a1f95de6..e9e15f5510 100644 --- a/src/runmode-erf-file.c +++ b/src/runmode-erf-file.c @@ -34,6 +34,8 @@ #include "util-cpu.h" #include "util-affinity.h" +#include "util-runmodes.h" + static const char *default_mode; const char *RunModeErfFileGetDefaultMode(void) @@ -126,13 +128,23 @@ int RunModeErfFileSingle(DetectEngineCtx *de_ctx) int RunModeErfFileAutoFp(DetectEngineCtx *de_ctx) { SCEnter(); - char tname[12]; - char qname[12]; + char tname[TM_THREAD_NAME_MAX]; + char qname[TM_QUEUE_NAME_MAX]; uint16_t cpu = 0; - char queues[2048] = ""; + char *queues = NULL; + int thread; RunModeInitialize(); + char *file = NULL; + if (ConfGet("erf-file.file", &file) == 0) { + SCLogError(SC_ERR_RUNMODE, + "Failed retrieving erf-file.file from config"); + exit(EXIT_FAILURE); + } + + TimeModeSetOffline(); + /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); @@ -148,31 +160,20 @@ int RunModeErfFileAutoFp(DetectEngineCtx *de_ctx) if (thread_max < 1) thread_max = 1; - int thread; - for (thread = 0; thread < thread_max; thread++) { - if (strlen(queues) > 0) - strlcat(queues, ",", sizeof(queues)); - - snprintf(qname, sizeof(qname), "pickup%"PRIu16, thread+1); - strlcat(queues, qname, sizeof(queues)); - } - SCLogDebug("queues %s", queues); - - char *file = NULL; - if (ConfGet("erf-file.file", &file) == 0) { - SCLogError(SC_ERR_RUNMODE, - "Failed retrieving erf-file.file from config"); + queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); + if (queues == NULL) { + SCLogError(SC_ERR_RUNMODE, "RunmodeAutoFpCreatePickupQueuesString failed"); exit(EXIT_FAILURE); } - TimeModeSetOffline(); - /* create the threads */ ThreadVars *tv = TmThreadCreatePacketHandler("ReceiveErfFile", "packetpool", "packetpool", queues, "flow", "pktacqloop"); + SCFree(queues); + if (tv == NULL) { printf("ERROR: TmThreadsCreate failed\n"); exit(EXIT_FAILURE); diff --git a/src/runmode-pcap-file.c b/src/runmode-pcap-file.c index d68618e04d..f6ccd41a5c 100644 --- a/src/runmode-pcap-file.c +++ b/src/runmode-pcap-file.c @@ -35,6 +35,8 @@ #include "util-cpu.h" #include "util-affinity.h" +#include "util-runmodes.h" + static const char *default_mode = NULL; const char *RunModeFilePcapGetDefaultMode(void) @@ -82,34 +84,34 @@ int RunModeFilePcapSingle(DetectEngineCtx *de_ctx) "packetpool", "packetpool", "pktacqloop"); if (tv == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "threading setup failed"); exit(EXIT_FAILURE); } TmModule *tm_module = TmModuleGetByName("ReceivePcapFile"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName failed for ReceivePcap\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName failed for ReceivePcap"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv, tm_module, file); tm_module = TmModuleGetByName("DecodePcapFile"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName DecodePcap failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName DecodePcap failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv, tm_module, NULL); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName StreamTcp failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv, tm_module, NULL); tm_module = TmModuleGetByName("Detect"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName Detect failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv, tm_module, (void *)de_ctx); @@ -119,7 +121,7 @@ int RunModeFilePcapSingle(DetectEngineCtx *de_ctx) TmThreadSetCPU(tv, DETECT_CPU_SET); if (TmThreadSpawn(tv) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -146,7 +148,7 @@ int RunModeFilePcapSingle(DetectEngineCtx *de_ctx) int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) { SCEnter(); - char tname[16]; + char tname[TM_THREAD_NAME_MAX]; uint16_t cpu = 0; TmModule *tm_module; RunModeInitialize(); @@ -170,26 +172,26 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) "detect-queue1", "simple", "pktacqloop"); if (tv_receivepcap == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_FATAL, "threading setup failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("ReceivePcapFile"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName failed for ReceivePcap\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName failed for ReceivePcap"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, file); tm_module = TmModuleGetByName("DecodePcapFile"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName DecodePcap failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName DecodePcap failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, NULL); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName StreamTcp failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, (void *)de_ctx); @@ -197,7 +199,7 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) TmThreadSetCPU(tv_receivepcap, RECEIVE_CPU_SET); if (TmThreadSpawn(tv_receivepcap) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -219,7 +221,7 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { - printf("ERROR: Can not strdup thread name\n"); + SCLogError(SC_ERR_RUNMODE, "failed to strdup thread name"); exit(EXIT_FAILURE); } SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); @@ -230,19 +232,19 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) "alert-queue1", "simple", "1slot"); if (tv_detect_ncpu == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("Detect"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName Detect failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx); char *thread_group_name = SCStrdup("Detect"); if (unlikely(thread_group_name == NULL)) { - printf("Error allocating memory\n"); + SCLogError(SC_ERR_RUNMODE, "error allocating memory"); exit(EXIT_FAILURE); } tv_detect_ncpu->thread_group_name = thread_group_name; @@ -250,7 +252,7 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET); if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -266,7 +268,7 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) "packetpool", "packetpool", "varslot"); if (tv_outputs == NULL) { - printf("ERROR: TmThreadCreatePacketHandler for Outputs failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadCreatePacketHandler for Outputs failed"); exit(EXIT_FAILURE); } @@ -275,7 +277,7 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) TmThreadSetCPU(tv_outputs, OUTPUT_CPU_SET); if (TmThreadSpawn(tv_outputs) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -302,13 +304,23 @@ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) { SCEnter(); - char tname[12]; - char qname[12]; + char tname[TM_THREAD_NAME_MAX]; + char qname[TM_QUEUE_NAME_MAX]; uint16_t cpu = 0; - char queues[2048] = ""; + char *queues = NULL; + int thread; RunModeInitialize(); + char *file = NULL; + if (ConfGet("pcap-file.file", &file) == 0) { + SCLogError(SC_ERR_RUNMODE, "Failed retrieving pcap-file from Conf"); + exit(EXIT_FAILURE); + } + SCLogDebug("file %s", file); + + TimeModeSetOffline(); + /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); @@ -324,24 +336,11 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) if (thread_max < 1) thread_max = 1; - int thread; - for (thread = 0; thread < thread_max; thread++) { - if (strlen(queues) > 0) - strlcat(queues, ",", sizeof(queues)); - - snprintf(qname, sizeof(qname), "pickup%"PRIu16, thread+1); - strlcat(queues, qname, sizeof(queues)); - } - SCLogDebug("queues %s", queues); - - char *file = NULL; - if (ConfGet("pcap-file.file", &file) == 0) { - SCLogError(SC_ERR_RUNMODE, "Failed retrieving pcap-file from Conf"); + queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); + if (queues == NULL) { + SCLogError(SC_ERR_RUNMODE, "RunmodeAutoFpCreatePickupQueuesString failed"); exit(EXIT_FAILURE); } - SCLogDebug("file %s", file); - - TimeModeSetOffline(); /* create the threads */ ThreadVars *tv_receivepcap = @@ -349,20 +348,22 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) "packetpool", "packetpool", queues, "flow", "pktacqloop"); + SCFree(queues); + if (tv_receivepcap == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_FATAL, "threading setup failed"); exit(EXIT_FAILURE); } TmModule *tm_module = TmModuleGetByName("ReceivePcapFile"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName failed for ReceivePcap\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName failed for ReceivePcap"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, file); tm_module = TmModuleGetByName("DecodePcapFile"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName DecodePcap failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName DecodePcap failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, NULL); @@ -370,7 +371,7 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) TmThreadSetCPU(tv_receivepcap, RECEIVE_CPU_SET); if (TmThreadSpawn(tv_receivepcap) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -382,7 +383,7 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { - printf("ERROR: Can not strdup thread name\n"); + SCLogError(SC_ERR_RUNMODE, "failed to strdup thread name"); exit(EXIT_FAILURE); } SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); @@ -393,19 +394,19 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) "packetpool", "packetpool", "varslot"); if (tv_detect_ncpu == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName StreamTcp failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL); tm_module = TmModuleGetByName("Detect"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName Detect failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx); @@ -413,7 +414,7 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) char *thread_group_name = SCStrdup("Detect"); if (unlikely(thread_group_name == NULL)) { - printf("Error allocating memory\n"); + SCLogError(SC_ERR_RUNMODE, "error allocating memory"); exit(EXIT_FAILURE); } tv_detect_ncpu->thread_group_name = thread_group_name; @@ -424,7 +425,7 @@ int RunModeFilePcapAutoFp(DetectEngineCtx *de_ctx) TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET); if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } diff --git a/src/runmode-tile.c b/src/runmode-tile.c index bb5815536b..99c2275475 100644 --- a/src/runmode-tile.c +++ b/src/runmode-tile.c @@ -148,7 +148,7 @@ void *ParseMpipeConfig(const char *iface) int RunModeTileMpipeWorkers(DetectEngineCtx *de_ctx) { SCEnter(); - char tname[32]; + char tname[TM_THREAD_NAME_MAX]; char *thread_name; TmModule *tm_module; int pipe; diff --git a/src/tm-queues.c b/src/tm-queues.c index e7afcb53c5..673e8363e9 100644 --- a/src/tm-queues.c +++ b/src/tm-queues.c @@ -59,6 +59,7 @@ Tmq* TmqCreateQueue(char *name) { return q; error: + SCLogError(SC_ERR_THREAD_QUEUE, "too many thread queues %u, max is %u", tmq_id+1, TMQ_MAX_QUEUES); return NULL; } @@ -100,10 +101,10 @@ void TmValidateQueueState(void) for (i = 0; i < tmq_id; i++) { SCMutexLock(&trans_q[tmqs[i].id].mutex_q); if (tmqs[i].reader_cnt == 0) { - printf("Error: Queue \"%s\" doesn't have a reader\n", tmqs[i].name); + SCLogError(SC_ERR_THREAD_QUEUE, "queue \"%s\" doesn't have a reader (id %d, max %u)", tmqs[i].name, i, tmq_id); err = TRUE; } else if (tmqs[i].writer_cnt == 0) { - printf("Error: Queue \"%s\" doesn't have a writer\n", tmqs[i].name); + SCLogError(SC_ERR_THREAD_QUEUE, "queue \"%s\" doesn't have a writer (id %d, max %u)", tmqs[i].name, i, tmq_id); err = TRUE; } SCMutexUnlock(&trans_q[tmqs[i].id].mutex_q); @@ -115,5 +116,6 @@ void TmValidateQueueState(void) return; error: + SCLogError(SC_ERR_FATAL, "fatal error during threading setup"); exit(EXIT_FAILURE); } diff --git a/src/tm-threads.c b/src/tm-threads.c index 79efa6d398..cc783e056d 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -1405,6 +1405,8 @@ ThreadVars *TmThreadCreate(char *name, char *inq_name, char *inqh_name, if (tmqh->OutHandlerCtxSetup != NULL) { tv->outctx = tmqh->OutHandlerCtxSetup(outq_name); + if (tv->outctx == NULL) + goto error; tv->outq = NULL; } else { tmq = TmqGetQueueByName(outq_name); diff --git a/src/tm-threads.h b/src/tm-threads.h index 2653dbb84c..eed425ea8f 100644 --- a/src/tm-threads.h +++ b/src/tm-threads.h @@ -29,6 +29,9 @@ #include "tm-threads-common.h" #include "tm-modules.h" +#define TM_QUEUE_NAME_MAX 16 +#define TM_THREAD_NAME_MAX 16 + typedef TmEcode (*TmSlotFunc)(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *); diff --git a/src/tmqh-flow.c b/src/tmqh-flow.c index 6382ac238e..207ddc71ab 100644 --- a/src/tmqh-flow.c +++ b/src/tmqh-flow.c @@ -153,6 +153,8 @@ void *TmqhOutputFlowSetupCtx(char *queue_str) if (queue_str == NULL || strlen(queue_str) == 0) return NULL; + SCLogDebug("queue_str %s", queue_str); + TmqhFlowCtx *ctx = SCMalloc(sizeof(TmqhFlowCtx)); if (unlikely(ctx == NULL)) return NULL; diff --git a/src/util-error.c b/src/util-error.c index 778d7e7970..8c4e77699c 100644 --- a/src/util-error.c +++ b/src/util-error.c @@ -275,6 +275,7 @@ const char * SCErrorToString(SCError err) CASE_CODE (SC_ERR_DNS_LOG_GENERIC); CASE_CODE (SC_WARN_OPTION_OBSOLETE); CASE_CODE (SC_WARN_NO_UNITTESTS); + CASE_CODE (SC_ERR_THREAD_QUEUE); } return "UNKNOWN_ERROR"; diff --git a/src/util-error.h b/src/util-error.h index 706604476b..fa9442987f 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -264,6 +264,7 @@ typedef enum { SC_ERR_DNS_LOG_GENERIC, SC_WARN_OPTION_OBSOLETE, SC_WARN_NO_UNITTESTS, + SC_ERR_THREAD_QUEUE, } SCError; const char *SCErrorToString(SCError); diff --git a/src/util-runmodes.c b/src/util-runmodes.c index 21dacbeeb7..860e30a11d 100644 --- a/src/util-runmodes.c +++ b/src/util-runmodes.c @@ -58,7 +58,7 @@ int RunModeSetLiveCaptureAuto(DetectEngineCtx *de_ctx, uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); int nlive = LiveGetDeviceCount(); TmModule *tm_module; - char tname[16]; + char tname[TM_THREAD_NAME_MAX]; int thread; if ((nlive <= 1) && (live_dev != NULL)) { @@ -286,6 +286,37 @@ int RunModeSetLiveCaptureAuto(DetectEngineCtx *de_ctx, return 0; } +/** \brief create a queue string for autofp to pass to + * the flow queue handler. + * + * The string will be "pickup1,pickup2,pickup3\0" + */ +char *RunmodeAutoFpCreatePickupQueuesString(int n) { + char *queues = NULL; + /* 13 because pickup12345, = 12 + \0 */ + size_t queues_size = n * 13; + int thread; + char qname[TM_QUEUE_NAME_MAX]; + + queues = SCMalloc(queues_size); + if (queues == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "failed to alloc queues buffer: %s", strerror(errno)); + return NULL; + } + memset(queues, 0x00, queues_size); + + for (thread = 0; thread < n; thread++) { + if (strlen(queues) > 0) + strlcat(queues, ",", queues_size); + + snprintf(qname, sizeof(qname), "pickup%"PRIu16, thread+1); + strlcat(queues, qname, queues_size); + } + + SCLogDebug("%d %"PRIuMAX", queues %s", n, (uintmax_t)queues_size, queues); + return queues; +} + int RunModeSetLiveCaptureAutoFp(DetectEngineCtx *de_ctx, ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, @@ -293,10 +324,10 @@ int RunModeSetLiveCaptureAutoFp(DetectEngineCtx *de_ctx, char *decode_mod_name, char *thread_name, const char *live_dev) { - char tname[12]; - char qname[12]; - char queues[2048] = ""; - int thread; + char tname[TM_THREAD_NAME_MAX]; + char qname[TM_QUEUE_NAME_MAX]; + char *queues = NULL; + int thread = 0; /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); int nlive = LiveGetDeviceCount(); @@ -307,14 +338,11 @@ int RunModeSetLiveCaptureAutoFp(DetectEngineCtx *de_ctx, if (thread_max < 1) thread_max = 1; - for (thread = 0; thread < thread_max; thread++) { - if (strlen(queues) > 0) - strlcat(queues, ",", sizeof(queues)); - - snprintf(qname, sizeof(qname),"pickup%"PRIu16, thread+1); - strlcat(queues, qname, sizeof(queues)); + queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); + if (queues == NULL) { + SCLogError(SC_ERR_RUNMODE, "RunmodeAutoFpCreatePickupQueuesString failed"); + exit(EXIT_FAILURE); } - SCLogDebug("queues %s", queues); if ((nlive <= 1) && (live_dev != NULL)) { void *aconf; @@ -496,6 +524,7 @@ int RunModeSetLiveCaptureAutoFp(DetectEngineCtx *de_ctx, } } + SCFree(queues); return 0; } @@ -518,7 +547,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(DetectEngineCtx *de_ctx, /* create the threads */ for (thread = 0; thread < threads_count; thread++) { - char tname[20]; + char tname[TM_THREAD_NAME_MAX]; char *n_thread_name = NULL; ThreadVars *tv = NULL; TmModule *tm_module = NULL; @@ -671,7 +700,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, char *decode_mod_name) { SCEnter(); - char tname[16]; + char tname[TM_THREAD_NAME_MAX]; TmModule *tm_module ; char *cur_queue = NULL; @@ -683,7 +712,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, /* create the threads */ cur_queue = LiveGetDeviceName(i); if (cur_queue == NULL) { - printf("ERROR: Invalid queue number\n"); + SCLogError(SC_ERR_RUNMODE, "invalid queue number"); exit(EXIT_FAILURE); } memset(tname, 0, sizeof(tname)); @@ -691,7 +720,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { - printf("ERROR: Can't create thread name failed\n"); + SCLogError(SC_ERR_RUNMODE, "failed to create thread name"); exit(EXIT_FAILURE); } ThreadVars *tv_receivenfq = @@ -700,12 +729,12 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, "pickup-queue", "simple", "1slot_noinout"); if (tv_receivenfq == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(recv_mod_name); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName failed for %s\n", recv_mod_name); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName failed for %s", recv_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivenfq, tm_module, (void *) ConfigParser(i)); @@ -713,7 +742,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, TmThreadSetCPU(tv_receivenfq, RECEIVE_CPU_SET); if (TmThreadSpawn(tv_receivenfq) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } @@ -725,20 +754,20 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, "decode-queue", "simple", "varslot"); if (tv_decode == NULL) { - printf("ERROR: TmThreadsCreate failed for Decode1\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed for Decode1"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(decode_mod_name); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName %s failed\n", decode_mod_name); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName %s failed", decode_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode,tm_module,NULL); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName StreamTcp failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode, tm_module, NULL); @@ -746,7 +775,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, TmThreadSetCPU(tv_decode, DECODE_CPU_SET); if (TmThreadSpawn(tv_decode) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -764,7 +793,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { - printf("ERROR: thead name creation failed\n"); + SCLogError(SC_ERR_RUNMODE, "thread name creation failed"); exit(EXIT_FAILURE); } SCLogDebug("Assigning %s affinity", thread_name); @@ -775,12 +804,12 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, "verdict-queue", "simple", "1slot"); if (tv_detect_ncpu == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("Detect"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName Detect failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppendDelayed(tv_detect_ncpu, tm_module, @@ -790,13 +819,13 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, char *thread_group_name = SCStrdup("Detect"); if (unlikely(thread_group_name == NULL)) { - printf("Error allocating memory\n"); + SCLogError(SC_ERR_RUNMODE, "error allocating memory"); exit(EXIT_FAILURE); } tv_detect_ncpu->thread_group_name = thread_group_name; if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } @@ -808,7 +837,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { - printf("ERROR: thead name creation failed\n"); + SCLogError(SC_ERR_RUNMODE, "thread name creation failed"); exit(EXIT_FAILURE); } ThreadVars *tv_verdict = @@ -817,19 +846,19 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, "alert-queue", "simple", "varslot"); if (tv_verdict == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(verdict_mod_name); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName %s failed\n", verdict_mod_name); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName %s failed", verdict_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_verdict, tm_module, (void *)ConfigParser(i)); tm_module = TmModuleGetByName("RespondReject"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName for RespondReject failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName for RespondReject failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_verdict, tm_module, NULL); @@ -837,7 +866,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, TmThreadSetCPU(tv_verdict, VERDICT_CPU_SET); if (TmThreadSpawn(tv_verdict) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } }; @@ -849,7 +878,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, "varslot"); if (tv_outputs == NULL) { - printf("ERROR: TmThreadCreatePacketHandler for Outputs failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadCreatePacketHandler for Outputs failed"); exit(EXIT_FAILURE); } @@ -857,7 +886,7 @@ int RunModeSetIPSAuto(DetectEngineCtx *de_ctx, SetupOutputs(tv_outputs); if (TmThreadSpawn(tv_outputs) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } @@ -872,11 +901,11 @@ int RunModeSetIPSAutoFp(DetectEngineCtx *de_ctx, char *decode_mod_name) { SCEnter(); - char tname[16]; - char qname[16]; + char tname[TM_THREAD_NAME_MAX]; + char qname[TM_QUEUE_NAME_MAX]; TmModule *tm_module ; char *cur_queue = NULL; - char queues[2048] = ""; + char *queues = NULL; int thread; /* Available cpus */ @@ -890,20 +919,17 @@ int RunModeSetIPSAutoFp(DetectEngineCtx *de_ctx, if (thread_max < 1) thread_max = 1; - for (thread = 0; thread < thread_max; thread++) { - if (strlen(queues) > 0) - strlcat(queues, ",", sizeof(queues)); - - snprintf(qname, sizeof(qname),"pickup%"PRIu16, thread+1); - strlcat(queues, qname, sizeof(queues)); + queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); + if (queues == NULL) { + SCLogError(SC_ERR_RUNMODE, "RunmodeAutoFpCreatePickupQueuesString failed"); + exit(EXIT_FAILURE); } - SCLogDebug("queues %s", queues); for (int i = 0; i < nqueue; i++) { /* create the threads */ cur_queue = LiveGetDeviceName(i); if (cur_queue == NULL) { - printf("ERROR: Invalid queue number\n"); + SCLogError(SC_ERR_RUNMODE, "invalid queue number"); exit(EXIT_FAILURE); } memset(tname, 0, sizeof(tname)); @@ -911,7 +937,7 @@ int RunModeSetIPSAutoFp(DetectEngineCtx *de_ctx, char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { - printf("ERROR: thead name creation failed\n"); + SCLogError(SC_ERR_RUNMODE, "thread name creation failed"); exit(EXIT_FAILURE); } ThreadVars *tv_receive = @@ -1012,19 +1038,19 @@ int RunModeSetIPSAutoFp(DetectEngineCtx *de_ctx, "packetpool", "packetpool", "varslot"); if (tv_verdict == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(verdict_mod_name); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName %s failed\n", verdict_mod_name); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName %s failed", verdict_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_verdict, tm_module, (void *)ConfigParser(i)); tm_module = TmModuleGetByName("RespondReject"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName for RespondReject failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName for RespondReject failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_verdict, tm_module, NULL); @@ -1032,10 +1058,12 @@ int RunModeSetIPSAutoFp(DetectEngineCtx *de_ctx, TmThreadSetCPU(tv_verdict, VERDICT_CPU_SET); if (TmThreadSpawn(tv_verdict) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } - }; + } + + SCFree(queues); return 0; } @@ -1045,7 +1073,7 @@ int RunModeSetIPSWorker(DetectEngineCtx *de_ctx, char *verdict_mod_name, char *decode_mod_name) { - char tname[16]; + char tname[TM_THREAD_NAME_MAX]; ThreadVars *tv = NULL; TmModule *tm_module = NULL; char *cur_queue = NULL; @@ -1056,7 +1084,7 @@ int RunModeSetIPSWorker(DetectEngineCtx *de_ctx, /* create the threads */ cur_queue = LiveGetDeviceName(i); if (cur_queue == NULL) { - printf("ERROR: Invalid queue number\n"); + SCLogError(SC_ERR_RUNMODE, "invalid queue number"); exit(EXIT_FAILURE); } memset(tname, 0, sizeof(tname)); @@ -1115,7 +1143,7 @@ int RunModeSetIPSWorker(DetectEngineCtx *de_ctx, tm_module = TmModuleGetByName("RespondReject"); if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName for RespondReject failed\n"); + SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName for RespondReject failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv, tm_module, NULL); diff --git a/src/util-runmodes.h b/src/util-runmodes.h index 958ceae121..2d562c3472 100644 --- a/src/util-runmodes.h +++ b/src/util-runmodes.h @@ -73,4 +73,7 @@ int RunModeSetIPSWorker(DetectEngineCtx *de_ctx, char *recv_mod_name, char *verdict_mod_name, char *decode_mod_name); + +char *RunmodeAutoFpCreatePickupQueuesString(int n); + #endif /* __UTIL_RUNMODES_H__ */