From e3fc53ecb90e5dc0dfed86878c846f67d725f31e Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 2 Jun 2010 17:55:01 +0200 Subject: [PATCH] Fix a corner case where the pcap receive modules could alloc packets at line rate until memory was depleted. --- src/source-pcap-file.c | 16 +++++++++------- src/source-pcap.c | 17 +++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/source-pcap-file.c b/src/source-pcap-file.c index ac777d8325..d04220fefa 100644 --- a/src/source-pcap-file.c +++ b/src/source-pcap-file.c @@ -121,7 +121,7 @@ void PcapFileCallback(char *user, struct pcap_pkthdr *h, u_char *pkt) { } if (p == NULL) { - return; + SCReturn; } p->ts.tv_sec = h->ts.tv_sec; @@ -159,13 +159,15 @@ TmEcode ReceivePcapFile(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, /* make sure we have at least one packet in the packet pool, to prevent * us from alloc'ing packets at line rate */ - SCMutexLock(&packet_q.mutex_q); - packet_q_len = packet_q.len; - if (packet_q.len == 0) { - SCondWait(&packet_q.cond_q, &packet_q.mutex_q); + while (packet_q_len == 0) { + SCMutexLock(&packet_q.mutex_q); + packet_q_len = packet_q.len; + if (packet_q.len == 0) { + SCondWait(&packet_q.cond_q, &packet_q.mutex_q); + } + packet_q_len = packet_q.len; + SCMutexUnlock(&packet_q.mutex_q); } - packet_q_len = packet_q.len; - SCMutexUnlock(&packet_q.mutex_q); if (postpq == NULL) pcap_max_read_packets = 1; diff --git a/src/source-pcap.c b/src/source-pcap.c index 643078db80..4ff7b52a8e 100644 --- a/src/source-pcap.c +++ b/src/source-pcap.c @@ -131,7 +131,6 @@ void TmModuleDecodePcapRegister (void) { void PcapCallback(char *user, struct pcap_pkthdr *h, u_char *pkt) { SCLogDebug("user %p, h %p, pkt %p", user, h, pkt); PcapThreadVars *ptv = (PcapThreadVars *)user; - //ThreadVars *tv = ptv->tv; Packet *p = NULL; if (ptv->array_idx == 0) { @@ -141,7 +140,7 @@ void PcapCallback(char *user, struct pcap_pkthdr *h, u_char *pkt) { } if (p == NULL) { - return; + SCReturn; } p->ts.tv_sec = h->ts.tv_sec; @@ -179,13 +178,15 @@ TmEcode ReceivePcap(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, Pack /* make sure we have at least one packet in the packet pool, to prevent * us from alloc'ing packets at line rate */ - SCMutexLock(&packet_q.mutex_q); - packet_q_len = packet_q.len; - if (packet_q.len == 0) { - SCondWait(&packet_q.cond_q, &packet_q.mutex_q); + while (packet_q_len == 0) { + SCMutexLock(&packet_q.mutex_q); + packet_q_len = packet_q.len; + if (packet_q.len == 0) { + SCondWait(&packet_q.cond_q, &packet_q.mutex_q); + } + packet_q_len = packet_q.len; + SCMutexUnlock(&packet_q.mutex_q); } - packet_q_len = packet_q.len; - SCMutexUnlock(&packet_q.mutex_q); if (postpq == NULL) pcap_max_read_packets = 1;