]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Add error checking for pthread_setspecific() and pthread_key_create().
authorKen Steele <ken@tilera.com>
Wed, 2 Jul 2014 14:42:05 +0000 (10:42 -0400)
committerKen Steele <ken@tilera.com>
Mon, 21 Jul 2014 18:48:31 +0000 (14:48 -0400)
14 files changed:
src/counters.c
src/flow-manager.c
src/source-af-packet.c
src/source-erf-dag.c
src/source-erf-file.c
src/source-ipfw.c
src/source-mpipe.c
src/source-napatech.c
src/source-nfq.c
src/source-pcap-file.c
src/source-pcap.c
src/source-pfring.c
src/tm-threads.c
src/tmqh-packetpool.c

index fce106908087772575283c66abc6018044d5e193..9aae1fe52d9e25026572ab355b22c6c6e2d471b0 100644 (file)
@@ -340,7 +340,7 @@ static void *SCPerfMgmtThread(void *arg)
     tv_local->cap_flags = 0;
 
     SCDropCaps(tv_local);
-
+    PacketPoolInit();
 
     if (sc_perf_op_ctx == NULL) {
         SCLogError(SC_ERR_PERF_STATS_NOT_INIT, "Perf Counter API not init"
@@ -409,6 +409,7 @@ static void *SCPerfWakeupThread(void *arg)
     tv_local->cap_flags = 0;
 
     SCDropCaps(tv_local);
+    PacketPoolInit();
 
     if (sc_perf_op_ctx == NULL) {
         SCLogError(SC_ERR_PERF_STATS_NOT_INIT, "Perf Counter API not init"
index 90388882191ee8d302cae3ffa75cbc31120a00d1..71bf2e0a49369d3ecd31384a87360a28a93b9711 100644 (file)
@@ -441,6 +441,7 @@ void *FlowManagerThread(void *td)
     /* Set the threads capability */
     th_v->cap_flags = 0;
     SCDropCaps(th_v);
+    PacketPoolInit();
 
     FlowHashDebugInit();
 
index 99acb86801ef380240a60542141652bc77940542..dd817468aeadcb4cd121fa686ebb20b0b48265f5 100644 (file)
@@ -1781,8 +1781,6 @@ TmEcode ReceiveAFPThreadInit(ThreadVars *tv, void *initdata, void **data) {
         ptv->vlan_disabled = 1;
     }
 
-    PacketPoolInit();
-
     SCReturnInt(TM_ECODE_OK);
 }
 
index 8eef943d43d463ca4e7ccc4fffc9f484a93b09d8..e4202b41e03db05eaf154386573e09328ce7c880 100644 (file)
@@ -316,8 +316,6 @@ ReceiveErfDagThreadInit(ThreadVars *tv, void *initdata, void **data)
     SCLogInfo("Starting processing packets from stream: %d on DAG: %s",
         ewtn->dagstream, ewtn->dagname);
 
-    PacketPoolInit();
-    
     SCReturnInt(TM_ECODE_OK);
 }
 
index 81305b8a0a52f07bd32cd204a00873674668e82c..1a35f4d650cee79b0516baeb8f28576f3dbdf862 100644 (file)
@@ -234,8 +234,6 @@ ReceiveErfFileThreadInit(ThreadVars *tv, void *initdata, void **data)
     etv->tv = tv;
     *data = (void *)etv;
 
-    PacketPoolInit();
-
     SCLogInfo("Processing ERF file %s", (char *)initdata);
 
     SCReturnInt(TM_ECODE_OK);
index 60192361b57a63dabc13b7229c990d2cadc2acfb..f3b64bd35f9c9ad65f7f67dac73ffe071afdda9c 100644 (file)
@@ -374,8 +374,6 @@ TmEcode ReceiveIPFWThreadInit(ThreadVars *tv, void *initdata, void **data)
 
     *data = (void *)ntv;
 
-    PacketPoolInit();
-
     SCReturnInt(TM_ECODE_OK);
 }
 
index 382ed63f99786276896b549159d36d8897216c40..061cb1e1ff3e323e5cc155a071704e85f6a69191 100644 (file)
@@ -898,8 +898,6 @@ TmEcode ReceiveMpipeThreadInit(ThreadVars *tv, void *initdata, void **data)
 
     *data = (void *)ptv;
 
-    PacketPoolInit();
-
     /* Only rank 0 does initialization of mpipe */
     if (rank != 0)
         SCReturnInt(TM_ECODE_OK);
index 60f9f246ce971f849f30481a17b5b8678369bcab..1e4a4068e5e8006ab46c34b4c31d7f3abd505791 100644 (file)
@@ -171,8 +171,6 @@ TmEcode NapatechStreamThreadInit(ThreadVars *tv, void *initdata, void **data)
 
     *data = (void *)ntv;
 
-    PacketPoolInit();
-
     SCReturnInt(TM_ECODE_OK);
 }
 
index 7c6359a31eef867423de98eaca9c930492ad2c51..8a6c9f5b2dff6140ee151f387e62056dad33fa38 100644 (file)
@@ -718,8 +718,6 @@ TmEcode ReceiveNFQThreadInit(ThreadVars *tv, void *initdata, void **data) {
 
     *data = (void *)ntv;
 
-    PacketPoolInit();
-
     SCMutexUnlock(&nfq_init_lock);
     return TM_ECODE_OK;
 }
index ee7418d1c668a983fcf5d41535c3c90513620924..0d6f9a74ed87594827e0446b26ec89bb383685b3 100644 (file)
@@ -341,8 +341,6 @@ TmEcode ReceivePcapFileThreadInit(ThreadVars *tv, void *initdata, void **data) {
     ptv->tv = tv;
     *data = (void *)ptv;
 
-    PacketPoolInit();
-
     SCReturnInt(TM_ECODE_OK);
 }
 
index 2b13d21dd3be482c3b40a14c486619043e7107aa..f116da3fbefdfc1849b1c3c4e91154c3adbbf1e1 100644 (file)
@@ -638,8 +638,6 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) {
 
     *data = (void *)ptv;
 
-    PacketPoolInit();
-
     /* Dereference config */
     pcapconfig->DerefFunc(pcapconfig);
     SCReturnInt(TM_ECODE_OK);
index 5735d3ec3afc37081363aa13db29b509a53e6819..4a17a92ca712556571fb4176c5f8c0357a3077dc 100644 (file)
@@ -540,8 +540,6 @@ TmEcode ReceivePfringThreadInit(ThreadVars *tv, void *initdata, void **data) {
     *data = (void *)ptv;
     pfconf->DerefFunc(pfconf);
 
-    PacketPoolInit();
-
     return TM_ECODE_OK;
 }
 
index 18ec35e5787dd582078095bc28afa4a99acf5a93..ec51ba21d7ed4f8b6d618dd6a0de416b81957eac 100644 (file)
@@ -142,6 +142,8 @@ void *TmThreadsSlot1NoIn(void *td)
     /* Drop the capabilities for this thread */
     SCDropCaps(tv);
 
+    PacketPoolInit();
+
     if (s->SlotThreadInit != NULL) {
         void *slot_data = NULL;
         r = s->SlotThreadInit(tv, s->slot_initdata, &slot_data);
@@ -252,6 +254,8 @@ void *TmThreadsSlot1NoOut(void *td)
     /* Drop the capabilities for this thread */
     SCDropCaps(tv);
 
+    PacketPoolInit();
+
     if (s->SlotThreadInit != NULL) {
         void *slot_data = NULL;
         r = s->SlotThreadInit(tv, s->slot_initdata, &slot_data);
@@ -343,6 +347,8 @@ void *TmThreadsSlot1NoInOut(void *td)
     /* Drop the capabilities for this thread */
     SCDropCaps(tv);
 
+    PacketPoolInit();
+
     SCLogDebug("%s starting", tv->name);
 
     if (s->SlotThreadInit != NULL) {
@@ -431,6 +437,8 @@ void *TmThreadsSlot1(void *td)
     /* Drop the capabilities for this thread */
     SCDropCaps(tv);
 
+    PacketPoolInit();
+
     SCLogDebug("%s starting", tv->name);
 
     if (s->SlotThreadInit != NULL) {
@@ -650,6 +658,8 @@ void *TmThreadsSlotPktAcqLoop(void *td) {
     /* Drop the capabilities for this thread */
     SCDropCaps(tv);
 
+    PacketPoolInit();
+
     /* check if we are setup properly */
     if (s == NULL || s->PktAcqLoop == NULL || tv->tmqh_in == NULL || tv->tmqh_out == NULL) {
         SCLogError(SC_ERR_FATAL, "TmSlot or ThreadVars badly setup: s=%p,"
index 9acd490dd27467016d47869f3f059706979faf9a..b816f4a97415db9d01dd9308dc5de70714c53fad 100644 (file)
@@ -48,6 +48,9 @@
 #include "util-profiling.h"
 #include "util-device.h"
 
+/* Number of freed packet to save for one pool before freeing them. */
+#define MAX_PENDING_RETURN_PACKETS 32
+
 #ifdef TLS
 __thread PktPool thread_pkt_pool;
 
@@ -58,22 +61,51 @@ static inline PktPool *GetThreadPacketPool(void)
 
 static inline PktPool *ThreadPacketPoolCreate(void)
 {
-    /* Nothing to do since __thread allocates the memory. */ 
+    /* Nothing to do since __thread statically allocates the memory. */
     return GetThreadPacketPool();
 }
-
-
 #else
 /* __thread not supported. */
 static pthread_key_t pkt_pool_thread_key;
+static SCMutex pkt_pool_thread_key_mutex = SCMUTEX_INITIALIZER;
+static int pkt_pool_thread_key_initialized = 0;
 
 static inline PktPool *GetThreadPacketPool(void)
 {
     return (PktPool*)pthread_getspecific(pkt_pool_thread_key);
 }
 
+static void PktPoolThreadDestroy(void * buf)
+{
+    free(buf);
+}
+
+static void TmqhPacketpoolInit(void)
+{
+    SCMutexLock(&pkt_pool_thread_key_mutex);
+    if (pkt_pool_thread_key_initialized) {
+        /* Key has already been created. */
+        SCMutexUnlock(&pkt_pool_thread_key_mutex);
+        return;
+    }
+
+    /* Create the pthread Key that is used to look up thread specific
+     * data buffer. Needs to be created only once.
+     */
+    int r = pthread_key_create(&pkt_pool_thread_key, PktPoolThreadDestroy);
+    if (r != 0) {
+        SCLogError(SC_ERR_MEM_ALLOC, "pthread_key_create failed with %d", r);
+        exit(EXIT_FAILURE);
+    }
+
+    pkt_pool_thread_key_initialized = 1;
+    SCMutexUnlock(&pkt_pool_thread_key_mutex);
+}
+
 static inline PktPool *ThreadPacketPoolCreate(void)
 {
+    TmqhPacketpoolInit();
+
     /* Check that the pool is not already created */
     PktPool *pool = GetThreadPacketPool();
     if (pool)
@@ -85,30 +117,15 @@ static inline PktPool *ThreadPacketPoolCreate(void)
         SCLogError(SC_ERR_MEM_ALLOC, "malloc failed");
         exit(EXIT_FAILURE);
     }
-    pthread_setspecific(pkt_pool_thread_key, pool);
+    int r = pthread_setspecific(pkt_pool_thread_key, pool);
+    if (r != 0) {
+        SCLogError(SC_ERR_MEM_ALLOC, "pthread_setspecific failed with %d", r);
+        exit(EXIT_FAILURE);
+    }
 
     return pool;
 }
-
-static void PktPoolThreadDestroy(void * buf)
-{
-    free(buf);
-}
-
-#endif
-
-/* Number of freed packet to save for one pool before freeing them. */
-#define MAX_PENDING_RETURN_PACKETS 32
-
-void TmqhPacketpoolInit(void)
-{
-#ifndef TLS
-    /* Create the pthread Key that is used to look up thread specific
-     * data buffer. Needs to be created only once.
-     */
-    pthread_key_create(&pkt_pool_thread_key, PktPoolThreadDestroy);
 #endif
-}
 
 /**
  * \brief TmqhPacketpoolRegister
@@ -118,8 +135,6 @@ void TmqhPacketpoolRegister (void) {
     tmqh_table[TMQH_PACKETPOOL].name = "packetpool";
     tmqh_table[TMQH_PACKETPOOL].InHandler = TmqhInputPacketpool;
     tmqh_table[TMQH_PACKETPOOL].OutHandler = TmqhOutputPacketpool;
-
-    TmqhPacketpoolInit();
 }
 
 static int PacketPoolIsEmpty(PktPool *pool)