summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2015-03-22 13:48:50 +0200
committerTobias Klauser <tklauser@distanz.ch>2015-03-30 14:08:50 +0200
commitdd5d906c40db5264d8d33c37565b39540f0258c8 (patch)
tree8f4205351a4f104c6ebf0e883f58546738fb9e6f
parent895fdbb7d6b01e207103c8924c1c853f0fafee15 (diff)
mz: Make access to pcap init functions thread safe
mz fails to start in cli mode and prints each time different pcap errors: $ mz -x -V fatal flex scanner internal error--end of buffer missed rx_arp: [ERROR] Error calling pcap_compile or simply shuts down. Sometimes it successfully gets up. Seems some initialization pcap functions are not thread safer. Fixed by using mutex locking before entering pcap_loop() Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--locking.h2
-rw-r--r--staging/mopsrx_arp.c21
2 files changed, 18 insertions, 5 deletions
diff --git a/locking.h b/locking.h
index cb57a9d..2cb93d1 100644
--- a/locking.h
+++ b/locking.h
@@ -11,6 +11,8 @@ struct mutexlock {
pthread_mutex_t lock;
};
+#define MUTEXLOCK_INITIALIZER { .lock = PTHREAD_MUTEX_INITIALIZER }
+
struct rwlock {
pthread_rwlock_t lock;
};
diff --git a/staging/mopsrx_arp.c b/staging/mopsrx_arp.c
index baf8133..132c7ef 100644
--- a/staging/mopsrx_arp.c
+++ b/staging/mopsrx_arp.c
@@ -19,6 +19,9 @@
#include "mz.h"
#include "mops.h"
#include "cli.h"
+#include "locking.h"
+
+static struct mutexlock pcap_init_lock = MUTEXLOCK_INITIALIZER;
// Starts an ARP RX thread for *every* device in the device_list.
// (Except for the loopback interface)
@@ -67,6 +70,8 @@ void *rx_arp (void *arg)
// FYI, possible filter string is also:
// "eth.dst==00:05:4e:51:01:b5 and arp and arp.opcode==2";
+ mutexlock_lock(&pcap_init_lock);
+
p_arp = pcap_open_live (dev->dev,
100, // max num of bytes to read
1, // 1 if promiscuous mode
@@ -75,7 +80,7 @@ void *rx_arp (void *arg)
if (p_arp == NULL) {
fprintf(stderr," rx_arp: [ERROR] %s\n",errbuf);
- return NULL; // TODO: Should return pointer to error message or something similar
+ goto exit_unlock;
}
dev->p_arp = p_arp; // also assign pointer to a global which is needed for clean_up
@@ -87,20 +92,22 @@ void *rx_arp (void *arg)
0) // netmask
== -1) {
fprintf(stderr," rx_arp: [ERROR] Error calling pcap_compile\n");
- return NULL;
+ goto exit_unlock;
}
if ( pcap_setfilter(p_arp, &filter) == -1) {
fprintf(stderr," rx_arp: [ERROR] Error setting pcap filter\n");
pcap_perror(p_arp, " rx_arp: ");
- return NULL;
+ goto exit_unlock;
}
if (pcap_setdirection(p_arp, PCAP_D_IN) == -1) {
pcap_perror(p_arp, " rx_arp: ");
- return NULL;
+ goto exit_unlock;
}
+ mutexlock_unlock(&pcap_init_lock);
+
again:
pcap_loop (p_arp,
1, // number of packets to wait
@@ -109,7 +116,11 @@ void *rx_arp (void *arg)
goto again;
pthread_exit(NULL); // destroy thread
- return NULL;
+ return NULL;
+
+exit_unlock:
+ mutexlock_unlock(&pcap_init_lock);
+ return NULL;
}