Adding support for Mac OS X, FreeBSD, centrailizing mutex/spins/conditions in a macro API, and some unittests

remotes/origin/master-1.0.x
Pablo Rincon 16 years ago committed by Victor Julien
parent 4cae27522d
commit 769022f4be

@ -1,7 +1,7 @@
#!/bin/sh
# the list of commands that need to run before we do a compile
libtoolize --force --automake --copy
aclocal
libtoolize --force --automake --copy
autoheader
automake --add-missing --copy
autoconf

@ -7,7 +7,7 @@ AC_INIT(configure.in)
AC_LANG_C
AC_PROG_CC_C99
AM_PROG_LIBTOOL
AC_PROG_LIBTOOL
AC_DEFUN([FAIL_MESSAGE],[
echo
@ -64,6 +64,35 @@ AC_INIT(configure.in)
[ enable_gccprotect=yes
])
#check for os
AC_MSG_CHECKING([host os])
# If no host os was detected, try with uname
if test -z "$host" ; then
host="`uname`"
fi
echo -n "installation for $host OS... \c"
case "$host" in
*-*-*freebsd*)
CFLAGS="${CFLAGS} -DOS_FREEBSD"
CPPFLAGS="${CPPFLAGS} -I/usr/local/include -I/usr/local/include/libnet11"
LDFLAGS="${LDFLAGS} -L/usr/local/lib -L/usr/local/lib/libnet11"
;;
*darwin*|*Darwin*)
CFLAGS="${CFLAGS} -DOS_DARWIN"
CPPFLAGS="${CPPFLAGS} -I/opt/local/include"
LDFLAGS="${LDFLAGS} -L/opt/local/lib"
;;
*-*-linux*)
#for now do nothing
;;
*)
AC_MSG_WARN([unsupported OS this may or may not work])
;;
esac
AC_MSG_RESULT(ok)
if test "$enable_gccprotect" = "yes"; then
#buffer overflow protection
AC_MSG_CHECKING(for -fstack-protector)
@ -294,7 +323,7 @@ AC_INIT(configure.in)
LIBNET_INC_DIR=""
AC_MSG_CHECKING("for libnet.h version 1.1.x")
libnet_dir="/usr/include /usr/local/include"
libnet_dir="/usr/include /usr/local/include /usr/local/include/libnet11 /opt/local/include"
for i in $libnet_dir; do
if test -r "$i/libnet.h"; then
LIBNET_INC_DIR="$i"

@ -86,6 +86,26 @@ For CentOS5 Users
./eidps
For Mac OS X Users
==================
# The following instructions has been tested with Snow Leopard, Mac OS X 10.6.1.
# First of all you need an essential developmnet environment like gcc/make. You can also download and install a set basic set of development tools Xcode from http://developer.apple.com/technology/xcode.html . You need macports to fetch the depends
# By default macports place the libraries at /opt/local/lib and /opt/local/include. The configuration should take care of this.
port install autoconf automake gcc44 make libnet11 libpcap pcre libyaml libtool
cd your_local_git_branch
export AC_PROG_LIBTOOL=$( which libtool )
bash autojunk.sh
sudo mkdir /var/log/eidps/
./configure --enable-unittests
cd src/
make
./eidps
#If autojunk, or ./configure fail, re export AC_PROG_LIBTOOL and try one more time.
*****************

@ -73,7 +73,7 @@ TmEcode AlertDebuglogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq
CreateTimeString(&p->ts, timebuf, sizeof(timebuf));
mutex_lock(&aft->file_ctx->fp_mutex);
sc_mutex_lock(&aft->file_ctx->fp_mutex);
fprintf(aft->file_ctx->fp, "+================\n");
fprintf(aft->file_ctx->fp, "TIME: %s\n", timebuf);
@ -137,7 +137,7 @@ TmEcode AlertDebuglogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq
PrintRawDataFp(aft->file_ctx->fp, p->pkt, p->pktlen);
fflush(aft->file_ctx->fp);
mutex_unlock(&aft->file_ctx->fp_mutex);
sc_mutex_unlock(&aft->file_ctx->fp_mutex);
return TM_ECODE_OK;
}
@ -155,7 +155,7 @@ TmEcode AlertDebuglogIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq
CreateTimeString(&p->ts, timebuf, sizeof(timebuf));
mutex_lock(&aft->file_ctx->fp_mutex);
sc_mutex_lock(&aft->file_ctx->fp_mutex);
for (i = 0; i < p->alerts.cnt; i++) {
PacketAlert *pa = &p->alerts.alerts[i];
char srcip[46], dstip[46];
@ -167,7 +167,7 @@ TmEcode AlertDebuglogIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq
timebuf, pa->gid, pa->sid, pa->rev, pa->msg, pa->prio, IPV6_GET_L4PROTO(p), srcip, p->sp, dstip, p->dp);
}
fflush(aft->file_ctx->fp);
mutex_unlock(&aft->file_ctx->fp_mutex);
sc_mutex_unlock(&aft->file_ctx->fp_mutex);
return TM_ECODE_OK;
}

@ -91,7 +91,7 @@ TmEcode AlertFastlogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
CreateTimeString(&p->ts, timebuf, sizeof(timebuf));
mutex_lock(&aft->file_ctx->fp_mutex);
sc_mutex_lock(&aft->file_ctx->fp_mutex);
for (i = 0; i < p->alerts.cnt; i++) {
PacketAlert *pa = &p->alerts.alerts[i];
@ -104,7 +104,7 @@ TmEcode AlertFastlogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
timebuf, pa->gid, pa->sid, pa->rev, pa->msg, pa->prio, IPV4_GET_IPPROTO(p), srcip, p->sp, dstip, p->dp);
}
fflush(aft->file_ctx->fp);
mutex_unlock(&aft->file_ctx->fp_mutex);
sc_mutex_unlock(&aft->file_ctx->fp_mutex);
return TM_ECODE_OK;
}
@ -122,7 +122,7 @@ TmEcode AlertFastlogIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
CreateTimeString(&p->ts, timebuf, sizeof(timebuf));
mutex_lock(&aft->file_ctx->fp_mutex);
sc_mutex_lock(&aft->file_ctx->fp_mutex);
for (i = 0; i < p->alerts.cnt; i++) {
PacketAlert *pa = &p->alerts.alerts[i];
@ -136,7 +136,7 @@ TmEcode AlertFastlogIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
fflush(aft->file_ctx->fp);
}
mutex_unlock(&aft->file_ctx->fp_mutex);
sc_mutex_unlock(&aft->file_ctx->fp_mutex);
return TM_ECODE_OK;
}

@ -150,15 +150,15 @@ TmEcode AlertUnifiedAlert (ThreadVars *tv, Packet *p, void *data, PacketQueue *p
}
/** check and enforce the filesize limit, thread safe */
mutex_lock(&aun->file_ctx->fp_mutex);
sc_mutex_lock(&aun->file_ctx->fp_mutex);
if ((aun->size_current + sizeof(hdr)) > aun->size_limit) {
if (AlertUnifiedAlertRotateFile(tv,aun) < 0)
{
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
return TM_ECODE_FAILED;
}
}
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
/* XXX which one to add to this alert? Lets see how Snort solves this.
* For now just take last alert. */

@ -156,15 +156,15 @@ TmEcode AlertUnifiedLog (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
/* check and enforce the filesize limit */
/** Wait for the mutex. We dont want all the threads rotating the file
* at the same time :) */
mutex_lock(&aun->file_ctx->fp_mutex);
sc_mutex_lock(&aun->file_ctx->fp_mutex);
if ((aun->size_current + sizeof(hdr) + p->pktlen + ethh_offset) > aun->size_limit) {
if (AlertUnifiedLogRotateFile(tv,aun) < 0)
{
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
return TM_ECODE_FAILED;
}
}
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
/* XXX which one to add to this alert? Lets see how Snort solves this.
* For now just take last alert. */

@ -20,6 +20,9 @@
#include "util-debug.h"
#include "util-time.h"
#ifndef IPPROTO_SCTP
#define IPPROTO_SCTP 132
#endif
/*prototypes*/
TmEcode Unified2Alert (ThreadVars *, Packet *, void *, PacketQueue *);
@ -214,15 +217,15 @@ int Unified2PacketTypeAlert (ThreadVars *t, Packet *p, void *data)
memcpy(write_buffer,&hdr,sizeof(Unified2AlertFileHeader));
mutex_lock(&aun->file_ctx->fp_mutex);
sc_mutex_lock(&aun->file_ctx->fp_mutex);
if ((aun->size_current + (sizeof(hdr) + sizeof(phdr))) > aun->size_limit) {
if (Unified2AlertRotateFile(t,aun) < 0)
{
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
return -1;
}
}
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
phdr.sensor_id = 0;
phdr.linktype = htonl(p->datalink);
@ -289,15 +292,15 @@ int Unified2IPv6TypeAlert (ThreadVars *t, Packet *p, void *data, PacketQueue *pq
}
/* check and enforce the filesize limit */
mutex_lock(&aun->file_ctx->fp_mutex);
sc_mutex_lock(&aun->file_ctx->fp_mutex);
if ((aun->size_current +(sizeof(hdr) + sizeof(phdr))) > aun->size_limit) {
if (Unified2AlertRotateFile(t,aun) < 0)
{
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
return -1;
}
}
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
/* XXX which one to add to this alert? Lets see how Snort solves this.
* For now just take last alert. */
@ -400,15 +403,15 @@ int Unified2IPv4TypeAlert (ThreadVars *tv, Packet *p, void *data, PacketQueue *p
}
/* check and enforce the filesize limit */
mutex_lock(&aun->file_ctx->fp_mutex);
sc_mutex_lock(&aun->file_ctx->fp_mutex);
if ((aun->size_current +(sizeof(hdr) + sizeof(phdr))) > aun->size_limit) {
if (Unified2AlertRotateFile(tv,aun) < 0)
{
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
return -1;
}
}
mutex_unlock(&aun->file_ctx->fp_mutex);
sc_mutex_unlock(&aun->file_ctx->fp_mutex);
/* XXX which one to add to this alert? Lets see how Snort solves this.
* For now just take last alert. */

@ -307,12 +307,12 @@ int AppLayerHandleMsg(StreamMsg *smsg, char need_lock)
uint16_t alproto = ALPROTO_UNKNOWN;
int r = 0;
if (need_lock == TRUE) mutex_lock(&smsg->flow->m);
if (need_lock == TRUE) sc_mutex_lock(&smsg->flow->m);
TcpSession *ssn = smsg->flow->protoctx;
if (ssn != NULL) {
alproto = ssn->alproto;
}
if (need_lock == TRUE) mutex_unlock(&smsg->flow->m);
if (need_lock == TRUE) sc_mutex_unlock(&smsg->flow->m);
if (ssn != NULL) {
if (smsg->flags & STREAM_START) {
@ -327,10 +327,10 @@ int AppLayerHandleMsg(StreamMsg *smsg, char need_lock)
smsg->data.data, smsg->data.data_len, smsg->flags);
if (alproto != ALPROTO_UNKNOWN) {
/* store the proto and setup the L7 data array */
if (need_lock == TRUE) mutex_lock(&smsg->flow->m);
if (need_lock == TRUE) sc_mutex_lock(&smsg->flow->m);
StreamL7DataPtrInit(ssn,StreamL7GetStorageSize());
ssn->alproto = alproto;
if (need_lock == TRUE) mutex_unlock(&smsg->flow->m);
if (need_lock == TRUE) sc_mutex_unlock(&smsg->flow->m);
r = AppLayerParse(smsg->flow, alproto, smsg->flags,
smsg->data.data, smsg->data.data_len, need_lock);
@ -354,9 +354,9 @@ int AppLayerHandleMsg(StreamMsg *smsg, char need_lock)
}
}
if (need_lock == TRUE) mutex_lock(&smsg->flow->m);
if (need_lock == TRUE) sc_mutex_lock(&smsg->flow->m);
smsg->flow->use_cnt--;
if (need_lock == TRUE) mutex_unlock(&smsg->flow->m);
if (need_lock == TRUE) sc_mutex_unlock(&smsg->flow->m);
/* return the used message to the queue */
StreamMsgReturnToPool(smsg);

@ -376,7 +376,7 @@ static int HTTPParseResponse(void *http_state, AppLayerParserState *pstate, uint
}
#ifdef DEBUG
static pthread_mutex_t http_state_mem_lock = PTHREAD_MUTEX_INITIALIZER;
static sc_mutex_t http_state_mem_lock = PTHREAD_MUTEX_INITIALIZER;
static uint64_t http_state_memuse = 0;
static uint64_t http_state_memcnt = 0;
#endif
@ -389,10 +389,10 @@ static void *HTTPStateAlloc(void) {
memset(s, 0, sizeof(HttpState));
#ifdef DEBUG
mutex_lock(&http_state_mem_lock);
sc_mutex_lock(&http_state_mem_lock);
http_state_memcnt++;
http_state_memuse+=sizeof(HttpState);
mutex_unlock(&http_state_mem_lock);
sc_mutex_unlock(&http_state_mem_lock);
#endif
return s;
}
@ -400,10 +400,10 @@ static void *HTTPStateAlloc(void) {
static void HTTPStateFree(void *s) {
free(s);
#ifdef DEBUG
mutex_lock(&http_state_mem_lock);
sc_mutex_lock(&http_state_mem_lock);
http_state_memcnt--;
http_state_memuse-=sizeof(HttpState);
mutex_unlock(&http_state_mem_lock);
sc_mutex_unlock(&http_state_mem_lock);
#endif
}
@ -422,9 +422,9 @@ void RegisterHTTPParsers(void) {
void HTTPAtExitPrintStats(void) {
#ifdef DEBUG
mutex_lock(&http_state_mem_lock);
sc_mutex_lock(&http_state_mem_lock);
SCLogDebug("http_state_memcnt %"PRIu64", http_state_memuse %"PRIu64"", http_state_memcnt, http_state_memuse);
mutex_unlock(&http_state_mem_lock);
sc_mutex_unlock(&http_state_mem_lock);
#endif
}

@ -681,9 +681,9 @@ int AppLayerParse(Flow *f, uint8_t proto, uint8_t flags, uint8_t *input,
if (parser_state_store == NULL)
goto error;
if (need_lock == TRUE) mutex_lock(&f->m);
if (need_lock == TRUE) sc_mutex_lock(&f->m);
ssn->aldata[app_layer_sid] = (void *)parser_state_store;
if (need_lock == TRUE) mutex_unlock(&f->m);
if (need_lock == TRUE) sc_mutex_unlock(&f->m);
}
} else {
SCLogDebug("No App Layer Data");
@ -725,18 +725,18 @@ int AppLayerParse(Flow *f, uint8_t proto, uint8_t flags, uint8_t *input,
/* See if we already have a 'app layer' state */
void *app_layer_state = NULL;
if (need_lock == TRUE) mutex_lock(&f->m);
if (need_lock == TRUE) sc_mutex_lock(&f->m);
app_layer_state = ssn->aldata[p->storage_id];
if (need_lock == TRUE) mutex_unlock(&f->m);
if (need_lock == TRUE) sc_mutex_unlock(&f->m);
if (app_layer_state == NULL) {
app_layer_state = p->StateAlloc();
if (app_layer_state == NULL)
goto error;
if (need_lock == TRUE) mutex_lock(&f->m);
if (need_lock == TRUE) sc_mutex_lock(&f->m);
ssn->aldata[p->storage_id] = app_layer_state;
if (need_lock == TRUE) mutex_unlock(&f->m);
if (need_lock == TRUE) sc_mutex_unlock(&f->m);
}
/* invoke the recursive parser */
@ -748,9 +748,9 @@ int AppLayerParse(Flow *f, uint8_t proto, uint8_t flags, uint8_t *input,
/* set the packets to no inspection and reassembly for the TLS sessions */
if (parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) {
if (need_lock == TRUE) mutex_lock(&f->m);
if (need_lock == TRUE) sc_mutex_lock(&f->m);
FlowSetNoPayloadInspectionFlag(f);
if (need_lock == TRUE) mutex_unlock(&f->m);
if (need_lock == TRUE) sc_mutex_unlock(&f->m);
/* Set the no reassembly flag for both the stream in this TcpSession */
if (parser_state->flags & APP_LAYER_PARSER_NO_REASSEMBLY) {

@ -92,7 +92,7 @@ static void SCPerfInitOPCtx(void)
sc_perf_op_ctx->club_tm = 1;
/* init the lock used by SCPerfClubTMInst */
if (pthread_mutex_init(&sc_perf_op_ctx->pctmi_lock, NULL) != 0) {
if (sc_mutex_init(&sc_perf_op_ctx->pctmi_lock, NULL) != 0) {
SCLogError(SC_INITIALIZATION_ERROR, "error initializing pctmi mutex");
exit(EXIT_FAILURE);
}
@ -156,9 +156,9 @@ static void *SCPerfMgmtThread(void *arg)
cond_time.tv_sec = time(NULL) + SC_PERF_MGMTT_TTS;
cond_time.tv_nsec = 0;
pthread_mutex_lock(tv_local->m);
pthread_cond_timedwait(tv_local->cond, tv_local->m, &cond_time);
pthread_mutex_unlock(tv_local->m);
sc_mutex_lock(tv_local->m);
sc_cond_timedwait(tv_local->cond, tv_local->m, &cond_time);
sc_mutex_unlock(tv_local->m);
SCPerfOutputCounters();
@ -200,9 +200,9 @@ static void *SCPerfWakeupThread(void *arg)
cond_time.tv_sec = time(NULL) + SC_PERF_WUT_TTS;
cond_time.tv_nsec = 0;
pthread_mutex_lock(tv_local->m);
pthread_cond_timedwait(tv_local->cond, tv_local->m, &cond_time);
pthread_mutex_unlock(tv_local->m);
sc_mutex_lock(tv_local->m);
sc_cond_timedwait(tv_local->cond, tv_local->m, &cond_time);
sc_mutex_unlock(tv_local->m);
tv = tv_root[TVT_PPT];
while (tv != NULL) {
@ -217,7 +217,7 @@ static void *SCPerfWakeupThread(void *arg)
* not, it should be okay */
tv->sc_perf_pctx.perf_flag = 1;
pthread_cond_signal(&q->cond_q);
sc_cond_signal(&q->cond_q);
tv = tv->next;
}
@ -701,7 +701,7 @@ static int SCPerfOutputCounterFileIface()
tv = tv_root[i];
while (tv != NULL) {
pthread_mutex_lock(&tv->sc_perf_pctx.m);
sc_mutex_lock(&tv->sc_perf_pctx.m);
pc = tv->sc_perf_pctx.head;
while (pc != NULL) {
@ -728,7 +728,7 @@ static int SCPerfOutputCounterFileIface()
pc = pc->next;
}
pthread_mutex_unlock(&tv->sc_perf_pctx.m);
sc_mutex_unlock(&tv->sc_perf_pctx.m);
tv = tv->next;
}
fflush(sc_perf_op_ctx->fp);
@ -748,7 +748,7 @@ static int SCPerfOutputCounterFileIface()
for (i = 0; i < pctmi->size; i++) {
pc_heads[i] = pctmi->head[i]->head;
pthread_mutex_lock(&pctmi->head[i]->m);
sc_mutex_lock(&pctmi->head[i]->m);
while(strcmp(pctmi->tm_name, pc_heads[i]->name->tm_name))
pc_heads[i] = pc_heads[i]->next;
@ -799,7 +799,7 @@ static int SCPerfOutputCounterFileIface()
}
for (i = 0; i < pctmi->size; i++)
pthread_mutex_unlock(&pctmi->head[i]->m);
sc_mutex_unlock(&pctmi->head[i]->m);
pctmi = pctmi->next;
@ -1088,7 +1088,7 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx)
return 0;
}
pthread_mutex_lock(&sc_perf_op_ctx->pctmi_lock);
sc_mutex_lock(&sc_perf_op_ctx->pctmi_lock);
pctmi = sc_perf_op_ctx->pctmi;
prev = pctmi;
@ -1120,7 +1120,7 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx)
else
prev->next = temp;
pthread_mutex_unlock(&sc_perf_op_ctx->pctmi_lock);
sc_mutex_unlock(&sc_perf_op_ctx->pctmi_lock);
return 1;
}
@ -1129,7 +1129,7 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx)
if (hpctx[i] != pctx)
continue;
pthread_mutex_unlock(&sc_perf_op_ctx->pctmi_lock);
sc_mutex_unlock(&sc_perf_op_ctx->pctmi_lock);
return 1;
}
@ -1148,7 +1148,7 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx)
}
pctmi->size++;
pthread_mutex_unlock(&sc_perf_op_ctx->pctmi_lock);
sc_mutex_unlock(&sc_perf_op_ctx->pctmi_lock);
return 1;
}
@ -1504,7 +1504,7 @@ int SCPerfUpdateCounterArray(SCPerfCounterArray *pca, SCPerfContext *pctx,
pcae = pca->head;
pthread_mutex_lock(&pctx->m);
sc_mutex_lock(&pctx->m);
pc = pctx->head;
for (i = 1; i <= pca->size; i++) {
@ -1523,7 +1523,7 @@ int SCPerfUpdateCounterArray(SCPerfCounterArray *pca, SCPerfContext *pctx,
}
}
pthread_mutex_unlock(&pctx->m);
sc_mutex_unlock(&pctx->m);
pctx->perf_flag = 0;

@ -119,7 +119,7 @@ typedef struct SCPerfContext_ {
uint16_t curr_id;
/* mutex to prevent simultaneous access during update_counter/output_stat */
pthread_mutex_t m;
sc_mutex_t m;
} SCPerfContext;
/**
@ -187,7 +187,7 @@ typedef struct SCPerfOPIfaceContext_ {
uint32_t club_tm;
SCPerfClubTMInst *pctmi;
pthread_mutex_t pctmi_lock;
sc_mutex_t pctmi_lock;
} SCPerfOPIfaceContext;

@ -201,7 +201,7 @@ typedef struct Packet_
uint8_t rtv_cnt;
/* tunnel packet ref count */
uint8_t tpr_cnt;
pthread_mutex_t mutex_rtv_cnt;
sc_mutex_t mutex_rtv_cnt;
/* tunnel stuff */
uint8_t tunnel_proto;
/* tunnel XXX convert to bitfield*/
@ -297,8 +297,8 @@ typedef struct PacketQueue_ {
Packet *top;
Packet *bot;
uint16_t len;
pthread_mutex_t mutex_q;
pthread_cond_t cond_q;
sc_mutex_t mutex_q;
sc_cond_t cond_q;
#ifdef DBG_PERF
uint16_t dbg_maxlen;
#endif /* DBG_PERF */
@ -382,22 +382,22 @@ typedef struct DecodeThreadVars_
#define TUNNEL_INCR_PKT_RTV(p) \
{ \
mutex_lock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
sc_mutex_lock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
((p)->root ? (p)->root->rtv_cnt++ : (p)->rtv_cnt++); \
mutex_unlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
sc_mutex_unlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
}
#define TUNNEL_INCR_PKT_TPR(p) \
{ \
mutex_lock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
sc_mutex_lock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
((p)->root ? (p)->root->tpr_cnt++ : (p)->tpr_cnt++); \
mutex_unlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
sc_mutex_unlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
}
#define TUNNEL_DECR_PKT_TPR(p) \
{ \
mutex_lock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
sc_mutex_lock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
((p)->root ? (p)->root->tpr_cnt-- : (p)->tpr_cnt--); \
mutex_unlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
sc_mutex_unlock((p)->root ? &(p)->root->mutex_rtv_cnt : &(p)->mutex_rtv_cnt); \
}
#define TUNNEL_DECR_PKT_TPR_NOLOCK(p) \
{ \

@ -75,13 +75,13 @@ typedef struct _DefragContext {
uint64_t ip6_frags; /**< Number of IPv6 fragments seen. */
HashListTable *frag_table; /**< Hash (list) table of fragment trackers. */
pthread_mutex_t frag_table_lock;
sc_mutex_t frag_table_lock;
Pool *tracker_pool; /**< Pool of trackers. */
pthread_mutex_t tracker_pool_lock;
sc_mutex_t tracker_pool_lock;
Pool *frag_pool; /**< Pool of fragments. */
pthread_mutex_t frag_pool_lock;
sc_mutex_t frag_pool_lock;
time_t timeout; /**< Default timeout. */
@ -133,7 +133,7 @@ typedef struct _DefragTracker {
uint8_t seen_last; /**< Has this tracker seen the last fragment? */
pthread_mutex_t lock; /**< Mutex for locking list operations on
sc_mutex_t lock; /**< Mutex for locking list operations on
* this tracker. */
TAILQ_HEAD(frag_tailq, _frag) frags; /**< Head of list of fragments. */
@ -285,7 +285,7 @@ DefragTrackerFreeFrags(DefragTracker *tracker)
Frag *frag;
/* Lock the frag pool as we'll be return items to it. */
mutex_lock(&tracker->dc->frag_pool_lock);
sc_mutex_lock(&tracker->dc->frag_pool_lock);
while ((frag = TAILQ_FIRST(&tracker->frags)) != NULL) {
TAILQ_REMOVE(&tracker->frags, frag, next);
@ -295,7 +295,7 @@ DefragTrackerFreeFrags(DefragTracker *tracker)
PoolReturn(frag->dc->frag_pool, frag);
}
mutex_unlock(&tracker->dc->frag_pool_lock);
sc_mutex_unlock(&tracker->dc->frag_pool_lock);
}
/**
@ -305,7 +305,7 @@ static void
DefragTrackerReset(DefragTracker *tracker)
{
DefragContext *saved_dc = tracker->dc;
pthread_mutex_t saved_lock = tracker->lock;
sc_mutex_t saved_lock = tracker->lock;
DefragTrackerFreeFrags(tracker);
memset(tracker, 0, sizeof(*tracker));
@ -332,7 +332,7 @@ DefragTrackerNew(void *arg)
tracker = calloc(1, sizeof(*tracker));
if (tracker == NULL)
return NULL;
if (pthread_mutex_init(&tracker->lock, NULL) != 0)
if (sc_mutex_init(&tracker->lock, NULL) != 0)
return NULL;
tracker->dc = dc;
TAILQ_INIT(&tracker->frags);
@ -349,7 +349,7 @@ DefragTrackerFree(void *arg)
{
DefragTracker *tracker = arg;
pthread_mutex_destroy(&tracker->lock);
sc_mutex_destroy(&tracker->lock);
DefragTrackerFreeFrags(tracker);
free(tracker);
}
@ -377,7 +377,7 @@ DefragContextNew(void)
"Defrag: Failed to initialize hash table.");
exit(EXIT_FAILURE);
}
if (pthread_mutex_init(&dc->frag_table_lock, NULL) != 0) {
if (sc_mutex_init(&dc->frag_table_lock, NULL) != 0) {
SCLogError(SC_ERR_MEM_ALLOC,
"Defrag: Failed to initialize hash table mutex.");
exit(EXIT_FAILURE);
@ -395,7 +395,7 @@ DefragContextNew(void)
"Defrag: Failed to initialize tracker pool.");
exit(EXIT_FAILURE);
}
if (pthread_mutex_init(&dc->tracker_pool_lock, NULL) != 0) {
if (sc_mutex_init(&dc->tracker_pool_lock, NULL) != 0) {
SCLogError(SC_ERR_MEM_ALLOC,
"Defrag: Failed to initialize tracker pool mutex.");
exit(EXIT_FAILURE);
@ -411,7 +411,7 @@ DefragContextNew(void)
"Defrag: Failed to initialize fragment pool.");
exit(EXIT_FAILURE);
}
if (pthread_mutex_init(&dc->frag_pool_lock, NULL) != 0) {
if (sc_mutex_init(&dc->frag_pool_lock, NULL) != 0) {
SCLogError(SC_ERR_MEM_ALLOC,
"Defrag: Failed to initialize frag pool mutex.");
exit(EXIT_FAILURE);
@ -479,7 +479,7 @@ Defrag4InsertFrag(DefragContext *dc, DefragTracker *tracker, Packet *p)
* instead of after. */
/* Lock this tracker as we'll be doing list operations on it. */
mutex_lock(&tracker->lock);
sc_mutex_lock(&tracker->lock);
/* Update timeout. */
tracker->timeout = p->ts;
@ -618,16 +618,16 @@ insert:
}
/* Allocate frag and insert. */
mutex_lock(&dc->frag_pool_lock);
sc_mutex_lock(&dc->frag_pool_lock);
new = PoolGet(dc->frag_pool);
mutex_unlock(&dc->frag_pool_lock);
sc_mutex_unlock(&dc->frag_pool_lock);
if (new == NULL)
goto done;
new->pkt = malloc(len);
if (new->pkt == NULL) {
mutex_lock(&dc->frag_pool_lock);
sc_mutex_lock(&dc->frag_pool_lock);
PoolReturn(dc->frag_pool, new);
mutex_unlock(&dc->frag_pool_lock);
sc_mutex_unlock(&dc->frag_pool_lock);
goto done;
}
BUG_ON(ltrim > len);
@ -651,13 +651,13 @@ insert:
if (remove) {
TAILQ_REMOVE(&tracker->frags, prev, next);
DefragFragReset(prev);
mutex_lock(&dc->frag_pool_lock);
sc_mutex_lock(&dc->frag_pool_lock);
PoolReturn(dc->frag_pool, prev);
mutex_unlock(&dc->frag_pool_lock);
sc_mutex_unlock(&dc->frag_pool_lock);
}
done:
mutex_unlock(&tracker->lock);
sc_mutex_unlock(&tracker->lock);
}
/**
@ -676,7 +676,7 @@ Defrag4Reassemble(ThreadVars *tv, DefragContext *dc, DefragTracker *tracker,
int len = 0;
/* Lock the tracker. */
mutex_lock(&tracker->lock);
sc_mutex_lock(&tracker->lock);
/* Should not be here unless we have seen the last fragment. */
if (!tracker->seen_last)
@ -719,13 +719,13 @@ Defrag4Reassemble(ThreadVars *tv, DefragContext *dc, DefragTracker *tracker,
if (rp == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate packet for fragmentation re-assembly, dumping fragments.");
mutex_lock(&dc->frag_table_lock);
sc_mutex_lock(&dc->frag_table_lock);
HashListTableRemove(dc->frag_table, tracker, sizeof(tracker));
mutex_unlock(&dc->frag_table_lock);
sc_mutex_unlock(&dc->frag_table_lock);
DefragTrackerReset(tracker);
mutex_lock(&dc->tracker_pool_lock);
sc_mutex_lock(&dc->tracker_pool_lock);
PoolReturn(dc->tracker_pool, tracker);
mutex_unlock(&dc->tracker_pool_lock);
sc_mutex_unlock(&dc->tracker_pool_lock);
goto done;
}
@ -786,12 +786,12 @@ Defrag4Reassemble(ThreadVars *tv, DefragContext *dc, DefragTracker *tracker,
/* Remove the frag tracker. */
HashListTableRemove(dc->frag_table, tracker, sizeof(tracker));
DefragTrackerReset(tracker);
mutex_lock(&dc->tracker_pool_lock);
sc_mutex_lock(&dc->tracker_pool_lock);
PoolReturn(dc->tracker_pool, tracker);
mutex_unlock(&dc->tracker_pool_lock);
sc_mutex_unlock(&dc->tracker_pool_lock);
done:
mutex_unlock(&tracker->lock);
sc_mutex_unlock(&tracker->lock);
return rp;
}
@ -864,18 +864,18 @@ Defrag4(ThreadVars *tv, DefragContext *dc, Packet *p)
lookup.id = IPV4_GET_IPID(p);
lookup.src_addr = p->src;
lookup.dst_addr = p->dst;
mutex_lock(&dc->frag_table_lock);
sc_mutex_lock(&dc->frag_table_lock);
tracker = HashListTableLookup(dc->frag_table, &lookup, sizeof(lookup));
mutex_unlock(&dc->frag_table_lock);
sc_mutex_unlock(&dc->frag_table_lock);
if (tracker == NULL) {
mutex_lock(&dc->tracker_pool_lock);
sc_mutex_lock(&dc->tracker_pool_lock);
tracker = PoolGet(dc->tracker_pool);
if (tracker == NULL) {
/* Timeout trackers and try again. */
DefragTimeoutTracker(dc, p);
tracker = PoolGet(dc->tracker_pool);
}
mutex_unlock(&dc->tracker_pool_lock);
sc_mutex_unlock(&dc->tracker_pool_lock);
if (tracker == NULL) {
/* Report memory error - actually a pool allocation error. */
SCLogError(SC_ERR_MEM_ALLOC, "Defrag: Failed to allocate tracker.");
@ -890,15 +890,15 @@ Defrag4(ThreadVars *tv, DefragContext *dc, Packet *p)
/* XXX Do policy lookup. */
tracker->policy = dc->default_policy;
mutex_lock(&dc->frag_table_lock);
sc_mutex_lock(&dc->frag_table_lock);
if (HashListTableAdd(dc->frag_table, tracker, sizeof(*tracker)) != 0) {
/* Failed to add new tracker. */
mutex_unlock(&dc->frag_table_lock);
sc_mutex_unlock(&dc->frag_table_lock);
SCLogError(SC_ERR_MEM_ALLOC,
"Defrag: Failed to add new tracker to hash table.");
return NULL;
}
mutex_unlock(&dc->frag_table_lock);
sc_mutex_unlock(&dc->frag_table_lock);
}
if (!more_frags) {

@ -63,7 +63,7 @@ int DetectFlowvarMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Packet *p
DetectFlowvarData *fd = (DetectFlowvarData *)m->ctx;
/* we need a lock */
mutex_lock(&p->flow->m);
sc_mutex_lock(&p->flow->m);
FlowVar *fv = FlowVarGet(p->flow, fd->idx);
if (fv != NULL) {
@ -71,7 +71,7 @@ int DetectFlowvarMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Packet *p
if (ptr != NULL)
ret = 1;
}
mutex_unlock(&p->flow->m);
sc_mutex_unlock(&p->flow->m);
return ret;
}

@ -98,7 +98,7 @@ int DetectTlsVersionMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Flow *
}
int ret = 0;
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
SCLogDebug("looking for tls_data->ver 0x%02X (flags 0x%02X)", tls_data->ver, flags);
if (flags & STREAM_TOCLIENT) {
@ -110,7 +110,7 @@ int DetectTlsVersionMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Flow *
if (tls_data->ver == tls_state->client_version)
ret = 1;
}
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
SCReturnInt(ret);
}

@ -27,7 +27,7 @@
#include <string.h>
#include <pcre.h>
#include <pthread.h>
#include "threads.h"
#if HAVE_CONFIG_H
#include <config.h>

@ -3,6 +3,8 @@
#include "eidps-common.h"
#include <getopt.h>
#include <signal.h>
#include <pthread.h>
#include "eidps.h"
#include "decode.h"
@ -126,10 +128,11 @@ SignalHandlerSetup(int sig, void (*handler)())
Packet *SetupPktWait (void)
{
Packet *p = NULL;
int r = 0;
do {
mutex_lock(&packet_q.mutex_q);
r = sc_mutex_lock(&packet_q.mutex_q);
p = PacketDequeue(&packet_q);
mutex_unlock(&packet_q.mutex_q);
sc_mutex_unlock(&packet_q.mutex_q);
if (p == NULL) {
//TmqDebugList();
@ -148,10 +151,11 @@ Packet *SetupPktWait (void)
Packet *SetupPkt (void)
{
Packet *p = NULL;
int r = 0;
mutex_lock(&packet_q.mutex_q);
r = sc_mutex_lock(&packet_q.mutex_q);
p = PacketDequeue(&packet_q);
mutex_unlock(&packet_q.mutex_q);
r = sc_mutex_unlock(&packet_q.mutex_q);
if (p == NULL) {
TmqDebugList();
@ -164,7 +168,7 @@ Packet *SetupPkt (void)
memset(p, 0, sizeof(Packet));
pthread_mutex_init(&p->mutex_rtv_cnt, NULL);
r = sc_mutex_init(&p->mutex_rtv_cnt, NULL);
SCLogDebug("allocated a new packet...");
}
@ -175,6 +179,33 @@ Packet *SetupPkt (void)
return p;
}
void GlobalInits()
{
memset(&trans_q, 0,sizeof(trans_q));
/* Initialize the trans_q mutex */
int blah;
int r = 0;
for(blah=0;blah<256;blah++) {
r |= sc_mutex_init(&trans_q[blah].mutex_q, NULL);
r |= sc_cond_init(&trans_q[blah].cond_q, NULL);
}
if (r != 0) {
SCLogInfo("Trans_Q Mutex not initialized correctly");
exit(EXIT_FAILURE);
}
sc_mutex_init(&mutex_pending, NULL);
sc_cond_init(&cond_pending, NULL);
/* initialize packet queues Here! */
memset(&packet_q,0,sizeof(packet_q));
sc_mutex_init(&packet_q.mutex_q, NULL);
sc_cond_init(&packet_q.cond_q, NULL);
}
/* \todo dtv not used. */
Packet *TunnelPktSetup(ThreadVars *t, DecodeThreadVars *dtv, Packet *parent, uint8_t *pkt, uint16_t len, uint8_t proto)
{
@ -182,11 +213,12 @@ Packet *TunnelPktSetup(ThreadVars *t, DecodeThreadVars *dtv, Packet *parent, uin
/* get us a packet */
Packet *p = SetupPkt();
int r = 0;
#if 0
do {
mutex_lock(&packet_q.mutex_q);
r = sc_mutex_lock(&packet_q.mutex_q);
p = PacketDequeue(&packet_q);
mutex_unlock(&packet_q.mutex_q);
sc_mutex_unlock(&packet_q.mutex_q);
if (p == NULL) {
//TmqDebugList();
@ -197,13 +229,13 @@ Packet *TunnelPktSetup(ThreadVars *t, DecodeThreadVars *dtv, Packet *parent, uin
}
} while (p == NULL);
#endif
mutex_lock(&mutex_pending);
r = sc_mutex_lock(&mutex_pending);
pending++;
#ifdef DBG_PERF
if (pending > dbg_maxpending)
dbg_maxpending = pending;
#endif /* DBG_PERF */
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
/* set the root ptr to the lowest layer */
if (parent->root != NULL)
@ -362,6 +394,9 @@ int main(int argc, char **argv)
}
}
/* Initializations for global vars, queues, etc (memsets, mutex init..) */
GlobalInits();
/* Load yaml configuration file if provided. */
if (conf_filename != NULL) {
ConfYamlLoadFile(conf_filename);
@ -475,6 +510,8 @@ int main(int argc, char **argv)
SCHInfoRegisterTests();
SCRuleVarsRegisterTests();
AppLayerParserRegisterTests();
ThreadMacrosRegisterTests();
if (list_unittests) {
UtListTests(regex_arg);
}
@ -495,10 +532,6 @@ int main(int argc, char **argv)
SignalHandlerSetup(SIGTERM, SignalHandlerSigterm);
SignalHandlerSetup(SIGHUP, SignalHandlerSighup);
/* initialize packet queues */
memset(&packet_q,0,sizeof(packet_q));
memset(&trans_q, 0,sizeof(trans_q));
/* pre allocate packets */
SCLogInfo("preallocating packets... packet size %" PRIuMAX "", (uintmax_t)sizeof(Packet));
int i = 0;
@ -510,7 +543,7 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
memset(p, 0, sizeof(Packet));
pthread_mutex_init(&p->mutex_rtv_cnt, NULL);
sc_mutex_init(&p->mutex_rtv_cnt, NULL);
PacketEnqueue(&packet_q,p);
}
@ -600,10 +633,10 @@ int main(int argc, char **argv)
if (sigflags & EIDPS_SIGTERM || sigflags & EIDPS_KILL)
break;
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
if (pending == 0)
done = 1;
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
if (done == 0) {
usleep(100);

@ -28,8 +28,8 @@ uint32_t pending;
#ifdef DBG_PERF
uint32_t dbg_maxpending;
#endif /* DBG_PERF */
pthread_mutex_t mutex_pending;
pthread_cond_t cond_pending;
sc_mutex_t mutex_pending;
sc_cond_t cond_pending;
/* preallocated packet structures here
* XXX move to the packetpool queue handler code
@ -39,6 +39,8 @@ PacketQueue packet_q;
* XXX move to the TmQueue structure later
*/
PacketQueue trans_q[256];
/* memset to zeros, and mutex init! */
void GlobalInits();
/* uppercase to lowercase conversion lookup table */
uint8_t g_u8_lowercasetable[256];

@ -48,12 +48,12 @@ static void FlowBitAdd(Flow *f, uint16_t idx) {
//printf("FlowBitAdd: adding flowbit with idx %" PRIu32 "\n", idx);
#ifdef FLOWBITS_STATS
mutex_lock(&flowbits_mutex);
sc_mutex_lock(&flowbits_mutex);
flowbits_added++;
flowbits_memuse += sizeof(FlowBit);
if (flowbits_memuse > flowbits_memuse_max)
flowbits_memuse_max = flowbits_memuse;
mutex_unlock(&flowbits_mutex);
sc_mutex_unlock(&flowbits_mutex);
#endif /* FLOWBITS_STATS */
}
}
@ -67,7 +67,7 @@ static void FlowBitRemove(Flow *f, uint16_t idx) {
//printf("FlowBitRemove: remove flowbit with idx %" PRIu32 "\n", idx);
#ifdef FLOWBITS_STATS
mutex_lock(&flowbits_mutex);
sc_mutex_lock(&flowbits_mutex);
flowbits_removed++;
if (flowbits_memuse >= sizeof(FlowBit))
flowbits_memuse -= sizeof(FlowBit);
@ -75,34 +75,34 @@ static void FlowBitRemove(Flow *f, uint16_t idx) {
printf("ERROR: flowbits memory usage going below 0!\n");
flowbits_memuse = 0;
}
mutex_unlock(&flowbits_mutex);
sc_mutex_unlock(&flowbits_mutex);
#endif /* FLOWBITS_STATS */
}
void FlowBitSet(Flow *f, uint16_t idx) {
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowBit *fb = FlowBitGet(f, idx);
if (fb == NULL) {
FlowBitAdd(f, idx);
}
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
void FlowBitUnset(Flow *f, uint16_t idx) {
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowBit *fb = FlowBitGet(f, idx);
if (fb != NULL) {
FlowBitRemove(f, idx);
}
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
void FlowBitToggle(Flow *f, uint16_t idx) {
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowBit *fb = FlowBitGet(f, idx);
if (fb != NULL) {
@ -111,32 +111,32 @@ void FlowBitToggle(Flow *f, uint16_t idx) {
FlowBitAdd(f, idx);
}
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
int FlowBitIsset(Flow *f, uint16_t idx) {
int r = 0;
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowBit *fb = FlowBitGet(f, idx);
if (fb != NULL) {
r = 1;
}
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
return r;
}
int FlowBitIsnotset(Flow *f, uint16_t idx) {
int r = 0;
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowBit *fb = FlowBitGet(f, idx);
if (fb == NULL) {
r = 1;
}
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
return r;
}
@ -147,7 +147,7 @@ void FlowBitFree(FlowBit *fb) {
free(fb);
#ifdef FLOWBITS_STATS
mutex_lock(&flowbits_mutex);
sc_mutex_lock(&flowbits_mutex);
flowbits_removed++;
if (flowbits_memuse >= sizeof(FlowBit))
flowbits_memuse -= sizeof(FlowBit);
@ -155,7 +155,7 @@ void FlowBitFree(FlowBit *fb) {
printf("ERROR: flowbits memory usage going below 0!\n");
flowbits_memuse = 0;
}
mutex_unlock(&flowbits_mutex);
sc_mutex_unlock(&flowbits_mutex);
#endif /* FLOWBITS_STATS */
}

@ -77,7 +77,7 @@ Flow *FlowGetFlowFromHash (Packet *p)
uint32_t key = FlowGetKey(p);
/* get our hash bucket and lock it */
FlowBucket *fb = &flow_hash[key];
mutex_lock(&fb->m);
sc_mutex_lock(&fb->m);
SCLogDebug("FlowGetFlowFromHash: fb %p fb->f %p", fb, fb->f);
@ -90,7 +90,7 @@ Flow *FlowGetFlowFromHash (Packet *p)
f = fb->f = FlowAlloc();
if (f == NULL) {
mutex_unlock(&fb->m);
sc_mutex_unlock(&fb->m);
return NULL;
}
}
@ -99,25 +99,25 @@ Flow *FlowGetFlowFromHash (Packet *p)
f->hprev = NULL;
/* got one, now lock, initialize and return */
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowInit(f,p);
FlowRequeue(f, NULL, &flow_new_q[f->protomap]);
f->flags |= FLOW_NEW_LIST;
f->fb = fb;
mutex_unlock(&fb->m);
sc_mutex_unlock(&fb->m);
return f;
}
/* ok, we have a flow in the bucket. Let's find out if it is our flow */
f = fb->f;
/* lock the 'root' flow */
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
/* see if this is the flow we are looking for */
if (CMP_FLOW(f, p) == 0) {
Flow *pf = NULL; /* previous flow */
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
while (f) {
pf = f; /* pf is not locked at this point */
@ -131,7 +131,7 @@ Flow *FlowGetFlowFromHash (Packet *p)
f = fb->f = FlowAlloc();
if (f == NULL) {
mutex_unlock(&fb->m);
sc_mutex_unlock(&fb->m);
return NULL;
}
}
@ -140,18 +140,18 @@ Flow *FlowGetFlowFromHash (Packet *p)
f->hprev = pf;
/* lock, initialize and return */
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowInit(f,p);
FlowRequeue(f, NULL, &flow_new_q[f->protomap]);
f->flags |= FLOW_NEW_LIST;
f->fb = fb;
mutex_unlock(&fb->m);
sc_mutex_unlock(&fb->m);
return f;
}
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
if (CMP_FLOW(f, p) != 0) {
/* we found our flow, lets put it on top of the
@ -165,18 +165,18 @@ Flow *FlowGetFlowFromHash (Packet *p)
fb->f = f;
/* found our flow */
mutex_unlock(&fb->m);
sc_mutex_unlock(&fb->m);
return f;
}
/* not found, try the next... */
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
}
/* The 'root' flow was our flow, return it.
* It's already locked. */
mutex_unlock(&fb->m);
sc_mutex_unlock(&fb->m);
return f;
}

@ -9,7 +9,7 @@
* to the list, the entire bucket is locked. */
typedef struct FlowBucket_ {
Flow *f;
pthread_mutex_t m;
sc_mutex_t m;
} FlowBucket;
/* prototypes */

@ -85,7 +85,7 @@ FlowConfig flow_config;
uint8_t flow_flags;
uint32_t flow_memuse;
pthread_mutex_t flow_memuse_mutex;
sc_mutex_t flow_memuse_mutex;
#define FLOWBITS_STATS
#ifdef FLOWBITS_STATS
@ -93,7 +93,7 @@ uint32_t flowbits_memuse;
uint32_t flowbits_memuse_max;
uint32_t flowbits_added;
uint32_t flowbits_removed;
pthread_mutex_t flowbits_mutex;
sc_mutex_t flowbits_mutex;
#endif /* FLOWBITS_STATS */
#endif /* __FLOW_PRIVATE_H__ */

@ -5,6 +5,29 @@
#include "debug.h"
#include "flow-queue.h"
#include "flow-util.h"
#include "util-error.h"
#include "util-debug.h"
#include "util-print.h"
#include <string.h>
FlowQueue *FlowQueueNew() {
FlowQueue *q = (FlowQueue *)malloc(sizeof(FlowQueue));
if (q == NULL) {
SCLogError(SC_ERR_MEM_ALLOC,"Error allocating flow queue");
exit(EXIT_SUCCESS);
}
q = FlowQueueInit(q);
return q;
}
FlowQueue *FlowQueueInit (FlowQueue *q) {
if (q != NULL) {
memset(q, 0, sizeof(FlowQueue));
sc_mutex_init(&q->mutex_q, NULL);
sc_cond_init(&q->cond_q, NULL);
}
return q;
}
void FlowEnqueue (FlowQueue *q, Flow *f) {
/* more packets in queue */
@ -25,11 +48,11 @@ void FlowEnqueue (FlowQueue *q, Flow *f) {
}
Flow *FlowDequeue (FlowQueue *q) {
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
Flow *f = q->bot;
if (f == NULL) {
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return NULL;
}
@ -48,7 +71,7 @@ Flow *FlowDequeue (FlowQueue *q) {
f->lnext = NULL;
f->lprev = NULL;
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return f;
}
@ -56,7 +79,7 @@ void FlowRequeue(Flow *f, FlowQueue *srcq, FlowQueue *dstq)
{
if (srcq != NULL)
{
mutex_lock(&srcq->mutex_q);
sc_mutex_lock(&srcq->mutex_q);
/* remove from old queue */
if (srcq->top == f)
@ -74,11 +97,11 @@ void FlowRequeue(Flow *f, FlowQueue *srcq, FlowQueue *dstq)
f->lprev = NULL;
/* don't unlock if src and dst are the same */
if (srcq != dstq) mutex_unlock(&srcq->mutex_q);
if (srcq != dstq) sc_mutex_unlock(&srcq->mutex_q);
}
/* now put it in dst */
if (srcq != dstq) mutex_lock(&dstq->mutex_q);
if (srcq != dstq) sc_mutex_lock(&dstq->mutex_q);
/* add to new queue (append) */
f->lprev = dstq->bot;
@ -95,6 +118,6 @@ void FlowRequeue(Flow *f, FlowQueue *srcq, FlowQueue *dstq)
dstq->dbg_maxlen = dstq->len;
#endif /* DBG_PERF */
mutex_unlock(&dstq->mutex_q);
sc_mutex_unlock(&dstq->mutex_q);
}

@ -4,6 +4,7 @@
#define __FLOW_QUEUE_H__
#include "flow.h"
#include "eidps-common.h"
/* Define a queue for storing flows */
typedef struct FlowQueue_
@ -11,14 +12,17 @@ typedef struct FlowQueue_
Flow *top;
Flow *bot;
uint32_t len;
pthread_mutex_t mutex_q;
pthread_cond_t cond_q;
sc_mutex_t mutex_q;
sc_cond_t cond_q;
#ifdef DBG_PERF
uint32_t dbg_maxlen;
#endif /* DBG_PERF */
} FlowQueue;
/* prototypes */
FlowQueue *FlowQueueNew();
FlowQueue *FlowQueueInit(FlowQueue *);
void FlowEnqueue (FlowQueue *, Flow *);
Flow *FlowDequeue (FlowQueue *);
void FlowRequeue(Flow *, FlowQueue *, FlowQueue *);

@ -15,20 +15,20 @@ Flow *FlowAlloc(void)
{
Flow *f;
mutex_lock(&flow_memuse_mutex);
sc_mutex_lock(&flow_memuse_mutex);
if (flow_memuse + sizeof(Flow) > flow_config.memcap) {
mutex_unlock(&flow_memuse_mutex);
sc_mutex_unlock(&flow_memuse_mutex);
return NULL;
}
f = malloc(sizeof(Flow));
if (f == NULL) {
mutex_unlock(&flow_memuse_mutex);
sc_mutex_unlock(&flow_memuse_mutex);
return NULL;
}
flow_memuse += sizeof(Flow);
mutex_unlock(&flow_memuse_mutex);
sc_mutex_unlock(&flow_memuse_mutex);
pthread_mutex_init(&f->m, NULL);
sc_mutex_init(&f->m, NULL);
f->lnext = NULL;
f->lprev = NULL;
f->hnext = NULL;
@ -42,9 +42,9 @@ Flow *FlowAlloc(void)
/** free the memory of a flow */
void FlowFree(Flow *f)
{
mutex_lock(&flow_memuse_mutex);
sc_mutex_lock(&flow_memuse_mutex);
flow_memuse -= sizeof(Flow);
mutex_unlock(&flow_memuse_mutex);
sc_mutex_unlock(&flow_memuse_mutex);
free(f);
}

@ -39,7 +39,7 @@ FlowVar *FlowVarGet(Flow *f, uint8_t idx) {
void FlowVarAdd(Flow *f, uint8_t idx, uint8_t *value, uint16_t size) {
//printf("Adding flow var \"%s\" with value(%" PRId32 ") \"%s\"\n", name, size, value);
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
FlowVar *fv = FlowVarGet(f, idx);
if (fv == NULL) {
@ -59,7 +59,7 @@ void FlowVarAdd(Flow *f, uint8_t idx, uint8_t *value, uint16_t size) {
}
out:
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
void FlowVarFree(FlowVar *fv) {

@ -10,9 +10,8 @@
#include "eidps-common.h"
#include "decode.h"
#include "threads.h"
#include "tm-modules.h"
#include "threadvars.h"
#include "tm-modules.h"
#include "tm-threads.h"
#include "util-time.h"
@ -105,25 +104,34 @@ void FlowUpdateQueue(Flow *f)
*/
static int FlowPrune (FlowQueue *q, struct timeval *ts)
{
if (mutex_trylock(&q->mutex_q) != 0) {
int mr = sc_mutex_trylock(&q->mutex_q);
if (mr != 0) {
SCLogDebug("Trylock failed!\n");
if (mr == EBUSY)
SCLogDebug("Was locked!\n");
if (mr == EINVAL)
SCLogDebug("Bad mutex value!\n");
return 0;
}
Flow *f = q->top;
if (f == NULL) {
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
SCLogDebug("top was null!\n");
return 0;
}
if (mutex_trylock(&f->m) != 0) {
mutex_unlock(&q->mutex_q);
if (sc_mutex_trylock(&f->m) != 0) {
SCLogDebug("cant lock 1!\n");
sc_mutex_unlock(&q->mutex_q);
return 0;
}
/* unlock list */
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
if (mutex_trylock(&f->fb->m) != 0) {
mutex_unlock(&f->m);
if (sc_mutex_trylock(&f->fb->m) != 0) {
sc_mutex_unlock(&f->m);
SCLogDebug("cant lock 2!\n");
return 0;
}
@ -176,8 +184,9 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts)
/* do the timeout check */
if ((f->lastts.tv_sec + timeout) >= ts->tv_sec) {
mutex_unlock(&f->fb->m);
mutex_unlock(&f->m);
sc_mutex_unlock(&f->fb->m);
sc_mutex_unlock(&f->m);
SCLogDebug("timeout check failed!\n");
return 0;
}
@ -185,8 +194,9 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts)
* we are currently processing in one of the threads */
if (f->use_cnt > 0) {
SCLogDebug("timed out but use_cnt > 0: %"PRIu16", %p, proto %"PRIu8"", f->use_cnt, f, f->proto);
mutex_unlock(&f->fb->m);
mutex_unlock(&f->m);
sc_mutex_unlock(&f->fb->m);
sc_mutex_unlock(&f->m);
SCLogDebug("it is in one of the threads!\n");
return 0;
}
@ -201,7 +211,7 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts)
f->hnext = NULL;
f->hprev = NULL;
mutex_unlock(&f->fb->m);
sc_mutex_unlock(&f->fb->m);
f->fb = NULL;
FlowClearMemory (f, f->protomap);
@ -209,7 +219,7 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts)
/* move to spare list */
FlowRequeue(f, q, &flow_spare_q);
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
return 1;
}
@ -237,9 +247,11 @@ static uint32_t FlowPruneFlows(FlowQueue *q, struct timeval *ts)
static int FlowUpdateSpareFlows(void) {
uint32_t toalloc = 0, tofree = 0, len;
mutex_lock(&flow_spare_q.mutex_q);
sc_mutex_lock(&flow_spare_q.mutex_q);
len = flow_spare_q.len;
mutex_unlock(&flow_spare_q.mutex_q);
sc_mutex_unlock(&flow_spare_q.mutex_q);
if (len < flow_config.prealloc) {
toalloc = flow_config.prealloc - len;
@ -250,9 +262,9 @@ static int FlowUpdateSpareFlows(void) {
if (f == NULL)
return 0;
mutex_lock(&flow_spare_q.mutex_q);
sc_mutex_lock(&flow_spare_q.mutex_q);
FlowEnqueue(&flow_spare_q,f);
mutex_unlock(&flow_spare_q.mutex_q);
sc_mutex_unlock(&flow_spare_q.mutex_q);
}
} else if (len > flow_config.prealloc) {
tofree = len - flow_config.prealloc;
@ -276,9 +288,9 @@ static int FlowUpdateSpareFlows(void) {
* \param direction direction to set the flag in
*/
void FlowSetIPOnlyFlag(Flow *f, char direction) {
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
direction ? (f->flags |= FLOW_TOSERVER_IPONLY_SET) : (f->flags |= FLOW_TOCLIENT_IPONLY_SET);
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
/** \brief decrease the use cnt of a flow
@ -289,10 +301,10 @@ void FlowDecrUsecnt(ThreadVars *tv, Packet *p) {
if (p == NULL || p->flow == NULL)
return;
mutex_lock(&p->flow->m);
sc_mutex_lock(&p->flow->m);
if (p->flow->use_cnt > 0)
p->flow->use_cnt--;
mutex_unlock(&p->flow->m);
sc_mutex_unlock(&p->flow->m);
}
/** \brief Entry point for packet flow handling
@ -350,7 +362,7 @@ void FlowHandlePacket (ThreadVars *tv, Packet *p)
/* set the flow in the packet */
p->flow = f;
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
/** \brief initialize the configuration
@ -361,11 +373,16 @@ void FlowInitConfig (char quiet)
SCLogInfo("initializing flow engine...");
memset(&flow_config, 0, sizeof(flow_config));
memset(&flow_spare_q, 0, sizeof(flow_spare_q));
memset(&flow_new_q, 0, sizeof(flow_new_q));
memset(&flow_est_q, 0, sizeof(flow_est_q));
flow_memuse = 0;
pthread_mutex_init(&flow_memuse_mutex, NULL);
int ifq = 0;
FlowQueueInit(&flow_spare_q);
for (ifq = 0; ifq < FLOW_PROTO_MAX; ifq++) {
FlowQueueInit(&flow_new_q[ifq]);
FlowQueueInit(&flow_est_q[ifq]);
FlowQueueInit(&flow_close_q[ifq]);
}
sc_mutex_init(&flow_memuse_mutex, NULL);
/* set defaults */
flow_config.hash_rand = rand(); /* XXX seed rand */
@ -384,7 +401,11 @@ void FlowInitConfig (char quiet)
printf("calloc failed %s\n", strerror(errno));
exit(1);
}
uint32_t i = 0;
memset(flow_hash, 0, flow_config.hash_size * sizeof(FlowBucket));
for (i = 0; i < flow_config.hash_size; i++)
sc_mutex_init(&flow_hash[i].m, NULL);
flow_config.memuse += (flow_config.hash_size * sizeof(FlowBucket));
if (quiet == FALSE)
@ -392,7 +413,6 @@ void FlowInitConfig (char quiet)
flow_config.memuse, flow_config.hash_size, (uintmax_t)sizeof(FlowBucket));
/* pre allocate flows */
uint32_t i = 0;
for (i = 0; i < flow_config.prealloc; i++) {
Flow *f = FlowAlloc();
if (f == NULL) {
@ -491,7 +511,7 @@ void FlowShutdown(void) {
free(flow_hash);
flow_memuse -= flow_config.hash_size * sizeof(FlowBucket);
pthread_mutex_destroy(&flow_memuse_mutex);
sc_mutex_destroy(&flow_memuse_mutex);
}
/** \brief Thread that manages the various queue's and removes timed out flows.
@ -751,9 +771,9 @@ int FlowSetProtoEmergencyTimeout(uint8_t proto, uint32_t emerg_new_timeout, uint
* \param f Flow to set the flag in
*/
void FlowLockSetNoPacketInspectionFlag(Flow *f) {
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
f->flags |= FLOW_NOPACKET_INSPECTION;
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
/** \brief Set the No Packet Inspection Flag without locking the flow.
@ -769,9 +789,9 @@ void FlowSetNoPacketInspectionFlag(Flow *f) {
* \param f Flow to set the flag in
*/
void FlowLockSetNoPayloadInspectionFlag(Flow *f) {
mutex_lock(&f->m);
sc_mutex_lock(&f->m);
f->flags |= FLOW_NOPAYLOAD_INSPECTION;
mutex_unlock(&f->m);
sc_mutex_unlock(&f->m);
}
/** \brief Set the No payload inspection Flag without locking the flow.
@ -784,6 +804,7 @@ void FlowSetNoPayloadInspectionFlag(Flow *f) {
#ifdef UNITTESTS
#include "stream-tcp-private.h"
#include "threads.h"
/**
* \test Test the setting of the per protocol timeouts.
@ -876,20 +897,25 @@ static int FlowTest02 (void) {
static int FlowTestPrune(Flow *f, struct timeval *ts) {
FlowQueue q;
FlowQueue *q = FlowQueueNew();
int r = sc_mutex_init(&q->mutex_q, NULL);
memset(&q, 0, sizeof(FlowQueue));
if (r != 0) {
SCLogDebug("Error initializing mutex!");
return 0;
}
q.top = NULL;
q->top = NULL;
FlowEnqueue(&q, f);
if (q.len != 1) {
FlowEnqueue(q, f);
if (q->len != 1) {
printf("Failed in enqueue the flow in flowqueue\n");
return 0;
}
FlowPrune(&q, ts);
if (q.len != 0) {
FlowPrune(q, ts);
if (q->len != 0) {
printf("Failed in prunning the flow\n");
return 0;
}
@ -919,11 +945,14 @@ static int FlowTest03 (void) {
memset(&f, 0, sizeof(Flow));
memset(&ts, 0, sizeof(ts));
memset(&fb, 0, sizeof(FlowBucket));
sc_mutex_init(&f.m, NULL);
TimeGet(&ts);
f.lastts.tv_sec = ts.tv_sec - 5000;
f.protoctx = &ssn;
sc_mutex_init(&fb.m, NULL);
f.fb = &fb;
f.proto = IPPROTO_TCP;
if (FlowTestPrune(&f, &ts) != 1)
@ -956,6 +985,9 @@ static int FlowTest04 (void) {
memset(&seg, 0, sizeof(TcpSegment));
memset(&client, 0, sizeof(TcpSegment));
sc_mutex_init(&f.m, NULL);
sc_mutex_init(&fb.m, NULL);
TimeGet(&ts);
seg.payload = payload;
seg.payload_len = 3;
@ -996,6 +1028,9 @@ static int FlowTest05 (void) {
memset(&ts, 0, sizeof(ts));
memset(&fb, 0, sizeof(FlowBucket));
sc_mutex_init(&f.m, NULL);
sc_mutex_init(&fb.m, NULL);
TimeGet(&ts);
ssn.state = TCP_SYN_SENT;
f.lastts.tv_sec = ts.tv_sec - 300;
@ -1034,6 +1069,9 @@ static int FlowTest06 (void) {
memset(&seg, 0, sizeof(TcpSegment));
memset(&client, 0, sizeof(TcpSegment));
sc_mutex_init(&fb.m, NULL);
sc_mutex_init(&f.m, NULL);
TimeGet(&ts);
seg.payload = payload;
seg.payload_len = 3;

@ -77,7 +77,7 @@ typedef struct Flow_
/** how many pkts and stream msgs are using the flow *right now* */
uint16_t use_cnt;
pthread_mutex_t m;
sc_mutex_t m;
/* list flow ptrs
* NOTE!!! These are NOT protected by the

@ -6,7 +6,7 @@
#include "util-bloomfilter-counting.h"
typedef struct HostTable_ {
pthread_mutex_t m;
sc_mutex_t m;
/* storage & lookup */
HashTable *hash;
@ -16,7 +16,7 @@ typedef struct HostTable_ {
} HostTable;
typedef struct Host_ {
pthread_mutex_t m;
sc_mutex_t m;
Address addr;
uint8_t os;

@ -93,7 +93,7 @@ TmEcode LogHttplogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
inet_ntop(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), srcip, sizeof(srcip));
inet_ntop(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), dstip, sizeof(dstip));
mutex_lock(&aft->file_ctx->fp_mutex);
sc_mutex_lock(&aft->file_ctx->fp_mutex);
for (i = 0; i < p->http_uri.cnt; i++) {
/* time */
fprintf(aft->file_ctx->fp, "%s ", timebuf);
@ -111,7 +111,7 @@ TmEcode LogHttplogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
fprintf(aft->file_ctx->fp, " [**] %s:%" PRIu32 " -> %s:%" PRIu32 "\n", srcip, p->sp, dstip, p->dp);
}
fflush(aft->file_ctx->fp);
mutex_unlock(&aft->file_ctx->fp_mutex);
sc_mutex_unlock(&aft->file_ctx->fp_mutex);
aft->uri_cnt += p->http_uri.cnt;
return TM_ECODE_OK;
@ -136,7 +136,7 @@ TmEcode LogHttplogIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
inet_ntop(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), srcip, sizeof(srcip));
inet_ntop(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), dstip, sizeof(dstip));
mutex_lock(&aft->file_ctx->fp_mutex);
sc_mutex_lock(&aft->file_ctx->fp_mutex);
for (i = 0; i < p->http_uri.cnt; i++) {
/* time */
fprintf(aft->file_ctx->fp, "%s ", timebuf);
@ -154,7 +154,7 @@ TmEcode LogHttplogIPv6(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
fprintf(aft->file_ctx->fp, " [**] %s:%" PRIu32 " -> %s:%" PRIu32 "\n", srcip, p->sp, dstip, p->dp);
}
fflush(aft->file_ctx->fp);
mutex_unlock(&aft->file_ctx->fp_mutex);
sc_mutex_unlock(&aft->file_ctx->fp_mutex);
aft->uri_cnt += p->http_uri.cnt;
return TM_ECODE_OK;

@ -34,9 +34,25 @@ Packet *PacketDequeue (PacketQueue *q) {
return NULL;
}
/* If we are going to get the last packet, set len to 0
* before doing anything else (to make the threads to follow
* the sc_cond_wait as soon as possible) */
q->len--;
/* pull the bottom packet from the queue */
Packet *p = q->bot;
#ifdef OS_DARWIN
/* Weird issue in OS_DARWIN
* Sometimes it looks that two thread arrive here at the same time
* so the bot ptr is NULL
*/
if (p == NULL) {
printf("No packet to dequeue!\n");
return NULL;
}
#endif
/* more packets in queue */
if (q->bot->prev != NULL) {
q->bot = q->bot->prev;
@ -46,7 +62,6 @@ Packet *PacketDequeue (PacketQueue *q) {
q->top = NULL;
q->bot = NULL;
}
q->len--;
p->next = NULL;
p->prev = NULL;

@ -3,7 +3,7 @@
#ifndef __PACKET_QUEUE_H__
#define __PACKET_QUEUE_H__
#include <pthread.h>
#include "threads.h"
#include "decode.h"
/* XXX: moved to decode.h */
@ -12,8 +12,8 @@ typedef struct PacketQueue_ {
Packet *top;
Packet *bot;
uint16_t len;
pthread_mutex_t mutex_q;
pthread_cond_t cond_q;
sc_mutex_t mutex_q;
sc_cond_t cond_q;
#ifdef DBG_PERF
uint16_t dbg_maxlen;
#endif /* DBG_PERF */

@ -68,7 +68,7 @@ static NFQGlobalVars nfq_g;
static NFQThreadVars nfq_t[NFQ_MAX_QUEUE];
static uint16_t receive_queue_num = 0;
static uint16_t verdict_queue_num = 0;
static pthread_mutex_t nfq_init_lock;
static sc_mutex_t nfq_init_lock;
TmEcode ReceiveNFQ(ThreadVars *, Packet *, void *, PacketQueue *);
TmEcode ReceiveNFQThreadInit(ThreadVars *, void *, void **);
@ -86,7 +86,7 @@ void TmModuleReceiveNFQRegister (void) {
/* XXX create a general NFQ setup function */
memset(&nfq_g, 0, sizeof(nfq_g));
memset(&nfq_t, 0, sizeof(nfq_t));
pthread_mutex_init(&nfq_init_lock, NULL);
sc_mutex_init(&nfq_init_lock, NULL);
tmm_modules[TMM_RECEIVENFQ].name = "ReceiveNFQ";
tmm_modules[TMM_RECEIVENFQ].ThreadInit = ReceiveNFQThreadInit;
@ -264,7 +264,7 @@ TmEcode NFQInitThread(NFQThreadVars *nfq_t, uint16_t queue_num, uint32_t queue_m
}
TmEcode ReceiveNFQThreadInit(ThreadVars *tv, void *initdata, void **data) {
mutex_lock(&nfq_init_lock);
sc_mutex_lock(&nfq_init_lock);
printf("ReceiveNFQThreadInit: starting... will bind to queuenum %" PRIu32 "\n", receive_queue_num);
sigset_t sigs;
@ -281,18 +281,18 @@ TmEcode ReceiveNFQThreadInit(ThreadVars *tv, void *initdata, void **data) {
if (r < 0) {
printf("NFQInitThread failed\n");
//return -1;
mutex_unlock(&nfq_init_lock);
sc_mutex_unlock(&nfq_init_lock);
exit(EXIT_FAILURE);
}
*data = (void *)ntv;
receive_queue_num++;
mutex_unlock(&nfq_init_lock);
sc_mutex_unlock(&nfq_init_lock);
return TM_ECODE_OK;
}
TmEcode VerdictNFQThreadInit(ThreadVars *tv, void *initdata, void **data) {
mutex_lock(&nfq_init_lock);
sc_mutex_lock(&nfq_init_lock);
printf("VerdictNFQThreadInit: starting... will bind to queuenum %" PRIu32 "\n", verdict_queue_num);
/* no initialization, ReceiveNFQ takes care of that */
@ -301,7 +301,7 @@ TmEcode VerdictNFQThreadInit(ThreadVars *tv, void *initdata, void **data) {
*data = (void *)ntv;
verdict_queue_num++;
mutex_unlock(&nfq_init_lock);
sc_mutex_unlock(&nfq_init_lock);
return TM_ECODE_OK;
}
@ -338,9 +338,9 @@ void NFQRecvPkt(NFQThreadVars *t) {
//printf("NFQRecvPkt: t %p, rv = %" PRId32 "\n", t, rv);
mutex_lock(&t->mutex_qh);
sc_mutex_lock(&t->mutex_qh);
ret = nfq_handle_packet(t->h, buf, rv);
mutex_unlock(&t->mutex_qh);
sc_mutex_unlock(&t->mutex_qh);
if (ret != 0)
printf("NFQRecvPkt: nfq_handle_packet error %" PRId32 "\n", ret);
@ -357,11 +357,11 @@ TmEcode ReceiveNFQ(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) {
/* check if we have too many packets in the system
* so we will wait for some to free up */
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
if (pending > MAX_PENDING) {
pthread_cond_wait(&cond_pending, &mutex_pending);
sc_cond_wait(&cond_pending, &mutex_pending);
}
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
return TM_ECODE_OK;
}
@ -405,9 +405,9 @@ void NFQSetVerdict(NFQThreadVars *t, Packet *p) {
if (verdict == NF_DROP) t->dropped++;
#endif /* COUNTERS */
mutex_lock(&t->mutex_qh);
sc_mutex_lock(&t->mutex_qh);
ret = nfq_set_verdict(t->qh, p->nfq_v.id, verdict, 0, NULL);
mutex_unlock(&t->mutex_qh);
sc_mutex_unlock(&t->mutex_qh);
if (ret < 0)
printf("NFQSetVerdict: nfq_set_verdict of %p failed %" PRId32 "\n", p, ret);
@ -422,15 +422,15 @@ TmEcode VerdictNFQ(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) {
char verdict = 1;
//printf("VerdictNFQ: tunnel pkt: %p %s\n", p, p->root ? "upper layer" : "root");
pthread_mutex_t *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt;
mutex_lock(m);
sc_mutex_t *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt;
sc_mutex_lock(m);
/* if there are more tunnel packets than ready to verdict packets,
* we won't verdict this one */
if (TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p)) {
//printf("VerdictNFQ: not ready to verdict yet: TUNNEL_PKT_TPR(p) > TUNNEL_PKT_RTV(p) = %" PRId32 " > %" PRId32 "\n", TUNNEL_PKT_TPR(p), TUNNEL_PKT_RTV(p));
verdict = 0;
}
mutex_unlock(m);
sc_mutex_unlock(m);
/* don't verdict if we are not ready */
if (verdict == 1) {

@ -5,7 +5,7 @@
#ifdef NFQ
#include <pthread.h>
#include "threads.h"
#include <linux/netfilter.h> /* for NF_ACCEPT */
#include <libnetfilter_queue/libnetfilter_queue.h>
@ -30,7 +30,7 @@ typedef struct NFQThreadVars_
struct nfnl_handle *nh;
/* 2 threads deal with the queue handle, so add a mutex */
struct nfq_q_handle *qh;
pthread_mutex_t mutex_qh;
sc_mutex_t mutex_qh;
/* this one should be not changing after init */
uint16_t queue_num;
int fd;

@ -74,11 +74,11 @@ void PcapFileCallback(char *user, struct pcap_pkthdr *h, u_char *pkt) {
PcapFileThreadVars *ptv = (PcapFileThreadVars *)user;
//ThreadVars *tv = ptv->tv;
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
if (pending > MAX_PENDING) {
pthread_cond_wait(&cond_pending, &mutex_pending);
sc_cond_wait(&cond_pending, &mutex_pending);
}
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
Packet *p = ptv->in_p;

@ -86,11 +86,11 @@ void PcapCallback(char *user, struct pcap_pkthdr *h, u_char *pkt) {
PcapThreadVars *ptv = (PcapThreadVars *)user;
ThreadVars *tv = ptv->tv;
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
if (pending > MAX_PENDING) {
pthread_cond_wait(&cond_pending, &mutex_pending);
sc_cond_wait(&cond_pending, &mutex_pending);
}
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
Packet *p = tv->tmqh_in(tv);
p->ts.tv_sec = h->ts.tv_sec;

@ -37,7 +37,7 @@
//#define DEBUG
#ifdef DEBUG
static pthread_mutex_t segment_pool_memuse_mutex;
static sc_mutex_t segment_pool_memuse_mutex;
static uint64_t segment_pool_memuse = 0;
static uint64_t segment_pool_memcnt = 0;
#endif
@ -70,10 +70,10 @@ void *TcpSegmentPoolAlloc(void *payload_len) {
}
#ifdef DEBUG
mutex_lock(&segment_pool_memuse_mutex);
sc_mutex_lock(&segment_pool_memuse_mutex);
segment_pool_memuse += seg->payload_len;
segment_pool_memcnt ++;
mutex_unlock(&segment_pool_memuse_mutex);
sc_mutex_unlock(&segment_pool_memuse_mutex);
#endif
return seg;
}
@ -97,9 +97,9 @@ void TcpSegmentPoolFree(void *ptr) {
static uint16_t segment_pool_pktsizes[segment_pool_num] = {4, 16, 112, 248, 512, 768, 1448, 0xffff};
static uint16_t segment_pool_poolsizes[segment_pool_num] = {2048, 3072, 3072, 3072, 3072, 4096, 8192, 512};
static Pool *segment_pool[segment_pool_num];
static pthread_mutex_t segment_pool_mutex[segment_pool_num];
static sc_mutex_t segment_pool_mutex[segment_pool_num];
#ifdef DEBUG
static pthread_mutex_t segment_pool_cnt_mutex;
static sc_mutex_t segment_pool_cnt_mutex;
static uint64_t segment_pool_cnt = 0;
#endif
/* index to the right pool for all packet sizes. */
@ -108,12 +108,12 @@ static uint16_t segment_pool_idx[65536]; /* O(1) lookups of the pool */
int StreamTcpReassembleInit(char quiet) {
StreamMsgQueuesInit();
#ifdef DEBUG
pthread_mutex_init(&segment_pool_memuse_mutex, NULL);
sc_mutex_init(&segment_pool_memuse_mutex, NULL);
#endif
uint16_t u16 = 0;
for (u16 = 0; u16 < segment_pool_num; u16++) {
segment_pool[u16] = PoolInit(segment_pool_poolsizes[u16], segment_pool_poolsizes[u16] / 8, TcpSegmentPoolAlloc, (void *) & segment_pool_pktsizes[u16], TcpSegmentPoolFree);
pthread_mutex_init(&segment_pool_mutex[u16], NULL);
sc_mutex_init(&segment_pool_mutex[u16], NULL);
}
uint16_t idx = 0;
@ -131,7 +131,7 @@ int StreamTcpReassembleInit(char quiet) {
idx++;
}
#ifdef DEBUG
pthread_mutex_init(&segment_pool_cnt_mutex, NULL);
sc_mutex_init(&segment_pool_cnt_mutex, NULL);
#endif
return 0;
}
@ -145,7 +145,7 @@ void StreamTcpReassembleFree(char quiet) {
}
PoolFree(segment_pool[u16]);
pthread_mutex_destroy(&segment_pool_mutex[u16]);
sc_mutex_destroy(&segment_pool_mutex[u16]);
}
#ifdef DEBUG
@ -164,7 +164,6 @@ TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(void) {
}
memset(ra_ctx, 0x00, sizeof(TcpReassemblyThreadCtx));
ra_ctx->stream_q = StreamMsgQueueGetNew();
return ra_ctx;
@ -1353,21 +1352,21 @@ void StreamTcpSegmentDataCopy(TcpSegment *dst_seg, TcpSegment *src_seg) {
TcpSegment* StreamTcpGetSegment(uint16_t len) {
uint16_t idx = segment_pool_idx[len];
SCLogDebug("%" PRIu32 " for payload_len %" PRIu32 "", idx, len);
mutex_lock(&segment_pool_mutex[idx]);
sc_mutex_lock(&segment_pool_mutex[idx]);
TcpSegment *seg = (TcpSegment *) PoolGet(segment_pool[idx]);
SCLogDebug("segment_pool[%u]->empty_list_size %u, segment_pool[%u]->alloc_list_size %u, alloc %u",
idx, segment_pool[idx]->empty_list_size, idx, segment_pool[idx]->alloc_list_size, segment_pool[idx]->allocated);
//PoolPrintSaturation(segment_pool[idx]);
mutex_unlock(&segment_pool_mutex[idx]);
sc_mutex_unlock(&segment_pool_mutex[idx]);
SCLogDebug("StreamTcpGetSegment: seg we return is %p", seg);
if (seg == NULL) {
SCLogDebug("segment_pool[%u]->empty_list_size %u, alloc %u", idx, segment_pool[idx]->empty_list_size, segment_pool[idx]->allocated);
} else {
#ifdef DEBUG
mutex_lock(&segment_pool_cnt_mutex);
sc_mutex_lock(&segment_pool_cnt_mutex);
segment_pool_cnt++;
mutex_unlock(&segment_pool_cnt_mutex);
sc_mutex_unlock(&segment_pool_cnt_mutex);
#endif
}
return seg;
@ -1384,15 +1383,15 @@ void StreamTcpSegmentReturntoPool(TcpSegment *seg) {
seg->prev = NULL;
uint16_t idx = segment_pool_idx[seg->pool_size];
mutex_lock(&segment_pool_mutex[idx]);
sc_mutex_lock(&segment_pool_mutex[idx]);
PoolReturn(segment_pool[idx], (void *) seg);
SCLogDebug("segment_pool[%"PRIu16"]->empty_list_size %"PRIu32"", idx,segment_pool[idx]->empty_list_size);
mutex_unlock(&segment_pool_mutex[idx]);
sc_mutex_unlock(&segment_pool_mutex[idx]);
#ifdef DEBUG
mutex_lock(&segment_pool_cnt_mutex);
sc_mutex_lock(&segment_pool_cnt_mutex);
segment_pool_cnt--;
mutex_unlock(&segment_pool_cnt_mutex);
sc_mutex_unlock(&segment_pool_cnt_mutex);
#endif
}

@ -68,11 +68,11 @@ static int ValidTimestamp(TcpSession * , Packet *);
#define STREAMTCP_EMERG_CLOSED_TIMEOUT 20
static Pool *ssn_pool = NULL;
static pthread_mutex_t ssn_pool_mutex;
static sc_mutex_t ssn_pool_mutex;
#ifdef DEBUG
static uint64_t ssn_pool_cnt;
static pthread_mutex_t ssn_pool_cnt_mutex;
static sc_mutex_t ssn_pool_cnt_mutex;
#endif
void TmModuleStreamTcpRegister (void) {
@ -116,14 +116,14 @@ void StreamTcpSessionClear(void *ssnptr) {
AppLayerParserCleanupState(ssn);
memset(ssn, 0, sizeof(TcpSession));
mutex_lock(&ssn_pool_mutex);
sc_mutex_lock(&ssn_pool_mutex);
PoolReturn(ssn_pool, ssn);
mutex_unlock(&ssn_pool_mutex);
sc_mutex_unlock(&ssn_pool_mutex);
#ifdef DEBUG
mutex_lock(&ssn_pool_cnt_mutex);
sc_mutex_lock(&ssn_pool_cnt_mutex);
ssn_pool_cnt--;
mutex_unlock(&ssn_pool_cnt_mutex);
sc_mutex_unlock(&ssn_pool_cnt_mutex);
#endif
}
@ -143,16 +143,16 @@ static void StreamTcpSessionPktFree (Packet *p) {
AppLayerParserCleanupState(ssn);
memset(ssn, 0, sizeof(TcpSession));
mutex_lock(&ssn_pool_mutex);
sc_mutex_lock(&ssn_pool_mutex);
PoolReturn(ssn_pool, p->flow->protoctx);
mutex_unlock(&ssn_pool_mutex);
sc_mutex_unlock(&ssn_pool_mutex);
p->flow->protoctx = NULL;
#ifdef DEBUG
mutex_lock(&ssn_pool_cnt_mutex);
sc_mutex_lock(&ssn_pool_cnt_mutex);
ssn_pool_cnt--;
mutex_unlock(&ssn_pool_cnt_mutex);
sc_mutex_unlock(&ssn_pool_cnt_mutex);
#endif
}
@ -210,7 +210,7 @@ void StreamTcpInitConfig(char quiet) {
exit(1);
}
pthread_mutex_init(&ssn_pool_mutex, NULL);
sc_mutex_init(&ssn_pool_mutex, NULL);
StreamTcpReassembleInit(quiet);
@ -235,7 +235,7 @@ void StreamTcpFreeConfig(char quiet) {
#ifdef DEBUG
SCLogDebug("ssn_pool_cnt %"PRIu64"", ssn_pool_cnt);
#endif
pthread_mutex_destroy(&ssn_pool_mutex);
sc_mutex_destroy(&ssn_pool_mutex);
}
/** \brief The function is used to to fetch a TCP session from the
@ -249,9 +249,9 @@ TcpSession *StreamTcpNewSession (Packet *p) {
TcpSession *ssn = (TcpSession *)p->flow->protoctx;
if (ssn == NULL) {
mutex_lock(&ssn_pool_mutex);
sc_mutex_lock(&ssn_pool_mutex);
p->flow->protoctx = PoolGet(ssn_pool);
mutex_unlock(&ssn_pool_mutex);
sc_mutex_unlock(&ssn_pool_mutex);
ssn = (TcpSession *)p->flow->protoctx;
if (ssn == NULL)
@ -261,9 +261,9 @@ TcpSession *StreamTcpNewSession (Packet *p) {
ssn->aldata = NULL;
#ifdef DEBUG
mutex_lock(&ssn_pool_cnt_mutex);
sc_mutex_lock(&ssn_pool_cnt_mutex);
ssn_pool_cnt++;
mutex_unlock(&ssn_pool_cnt_mutex);
sc_mutex_unlock(&ssn_pool_cnt_mutex);
#endif
}
@ -1955,9 +1955,9 @@ TmEcode StreamTcp (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq)
if (p->flow == NULL)
return TM_ECODE_OK;
mutex_lock(&p->flow->m);
sc_mutex_lock(&p->flow->m);
ret = StreamTcpPacket(tv, p, stt);
mutex_unlock(&p->flow->m);
sc_mutex_unlock(&p->flow->m);
//if (ret)
// return TM_ECODE_FAILED;

@ -7,7 +7,7 @@
#include "util-pool.h"
#include "util-debug.h"
static pthread_mutex_t stream_pool_memuse_mutex;
static sc_mutex_t stream_pool_memuse_mutex;
static uint64_t stream_pool_memuse = 0;
static uint64_t stream_pool_memcnt = 0;
@ -20,7 +20,7 @@ static uint16_t toclient_min_init_chunk_len = 0;
static uint16_t toclient_min_chunk_len = 0;
static Pool *stream_msg_pool = NULL;
static pthread_mutex_t stream_msg_pool_mutex = PTHREAD_MUTEX_INITIALIZER;
static sc_mutex_t stream_msg_pool_mutex = PTHREAD_MUTEX_INITIALIZER;
void *StreamMsgAlloc(void *null) {
StreamMsg *s = malloc(sizeof(StreamMsg));
@ -29,10 +29,10 @@ void *StreamMsgAlloc(void *null) {
memset(s, 0, sizeof(StreamMsg));
mutex_lock(&stream_pool_memuse_mutex);
sc_mutex_lock(&stream_pool_memuse_mutex);
stream_pool_memuse += sizeof(StreamMsg);
stream_pool_memcnt ++;
mutex_unlock(&stream_pool_memuse_mutex);
sc_mutex_unlock(&stream_pool_memuse_mutex);
return s;
}
@ -92,38 +92,38 @@ static StreamMsg *StreamMsgDequeue (StreamMsgQueue *q) {
/* Used by stream reassembler to get msgs */
StreamMsg *StreamMsgGetFromPool(void)
{
mutex_lock(&stream_msg_pool_mutex);
sc_mutex_lock(&stream_msg_pool_mutex);
StreamMsg *s = (StreamMsg *)PoolGet(stream_msg_pool);
mutex_unlock(&stream_msg_pool_mutex);
sc_mutex_unlock(&stream_msg_pool_mutex);
return s;
}
/* Used by l7inspection to return msgs to pool */
void StreamMsgReturnToPool(StreamMsg *s) {
mutex_lock(&stream_msg_pool_mutex);
sc_mutex_lock(&stream_msg_pool_mutex);
PoolReturn(stream_msg_pool, (void *)s);
mutex_unlock(&stream_msg_pool_mutex);
sc_mutex_unlock(&stream_msg_pool_mutex);
}
/* Used by l7inspection to get msgs with data */
StreamMsg *StreamMsgGetFromQueue(StreamMsgQueue *q)
{
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
if (q->len == 0) {
struct timespec cond_time;
cond_time.tv_sec = time(NULL) + 5;
cond_time.tv_nsec = 0;
/* if we have no stream msgs in queue, wait... for 5 seconds */
pthread_cond_timedwait(&q->cond_q, &q->mutex_q, &cond_time);
sc_cond_timedwait(&q->cond_q, &q->mutex_q, &cond_time);
}
if (q->len > 0) {
StreamMsg *s = StreamMsgDequeue(q);
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return s;
} else {
/* return NULL if we have no stream msg. Should only happen on signals. */
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return NULL;
}
}
@ -131,15 +131,15 @@ StreamMsg *StreamMsgGetFromQueue(StreamMsgQueue *q)
/* Used by stream reassembler to fill the queue for l7inspect reading */
void StreamMsgPutInQueue(StreamMsgQueue *q, StreamMsg *s)
{
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
StreamMsgEnqueue(q, s);
SCLogDebug("q->len %" PRIu32 "", q->len);
pthread_cond_signal(&q->cond_q);
mutex_unlock(&q->mutex_q);
sc_cond_signal(&q->cond_q);
sc_mutex_unlock(&q->mutex_q);
}
void StreamMsgQueuesInit(void) {
pthread_mutex_init(&stream_pool_memuse_mutex, NULL);
sc_mutex_init(&stream_pool_memuse_mutex, NULL);
//memset(&stream_q, 0, sizeof(stream_q));
stream_msg_pool = PoolInit(5000,250,StreamMsgAlloc,NULL,StreamMsgFree);
@ -149,7 +149,7 @@ void StreamMsgQueuesInit(void) {
void StreamMsgQueuesDeinit(char quiet) {
PoolFree(stream_msg_pool);
pthread_mutex_destroy(&stream_pool_memuse_mutex);
sc_mutex_destroy(&stream_pool_memuse_mutex);
if (quiet == FALSE)
SCLogDebug("stream_pool_memuse %"PRIu64", stream_pool_memcnt %"PRIu64"", stream_pool_memuse, stream_pool_memcnt);
@ -164,6 +164,8 @@ StreamMsgQueue *StreamMsgQueueGetNew(void) {
}
memset(smq, 0x00, sizeof(StreamMsgQueue));
sc_mutex_init(&smq->mutex_q, NULL);
sc_cond_init(&smq->cond_q, NULL);
return smq;
}
@ -182,7 +184,7 @@ StreamMsgQueue *StreamMsgQueueGetByPort(uint16_t port) {
/* XXX hack */
void StreamMsgSignalQueueHack(void) {
//pthread_cond_signal(&stream_q.cond_q);
//sc_cond_signal(&stream_q.cond_q);
}
void StreamMsgQueueSetMinInitChunkLen(uint8_t dir, uint16_t len) {

@ -40,8 +40,8 @@ typedef struct StreamMsgQueue_ {
StreamMsg *top;
StreamMsg *bot;
uint16_t len;
pthread_mutex_t mutex_q;
pthread_cond_t cond_q;
sc_mutex_t mutex_q;
sc_cond_t cond_q;
#ifdef DBG_PERF
uint16_t dbg_maxlen;
#endif /* DBG_PERF */

@ -1,36 +1,58 @@
/* Copyright (c) 2008 Victor Julien <victor@inliniac.net> */
/**
* Copyright (c) 2009 Open Information Security Foundation
*
* \author Victor Julien <victor@inliniac.net>
* \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
*/
#include "eidps-common.h"
#include "decode.h"
#ifdef DBG_THREADS
int mutex_lock_dbg (pthread_mutex_t *m) {
int ret;
printf("%16s: (%"PRIuMAX") locking mutex %p\n", __FUNCTION__, (uintmax_t)pthread_self(), m);
ret = pthread_mutex_lock(m);
printf("%16s: (%"PRIuMAX") locked mutex %p ret %" PRId32 "\n", __FUNCTION__, (uintmax_t)pthread_self(), m, ret);
return(ret);
#include "util-unittest.h"
#include "debug.h"
#include "util-debug.h"
#include "util-unittest.h"
#include "threads.h"
#ifdef UNITTESTS /* UNIT TESTS */
/**
* \brief Test Mutex macros
*/
int ThreadMacrosTest01Mutex(void) {
sc_mutex_t mut;
int r = 0;
r |= sc_mutex_init(&mut, NULL);
r |= sc_mutex_lock(&mut);
r |= (sc_mutex_trylock(&mut) == EBUSY)? 0 : 1;
r |= sc_mutex_unlock(&mut);
r |= sc_mutex_destroy(&mut);
return (r == 0)? 1 : 0;
}
int mutex_trylock_dbg (pthread_mutex_t *m) {
int ret;
printf("%16s: (%"PRIuMAX") trylocking mutex %p\n", __FUNCTION__, (uintmax_t)pthread_self(), m);
ret = pthread_mutex_trylock(m);
printf("%16s: (%"PRIuMAX") trylocked mutex %p ret %" PRId32 "\n", __FUNCTION__, (uintmax_t)pthread_self(), m, ret);
return(ret);
/**
* \brief Test Spin Macros
*/
int ThreadMacrosTest02Spinlocks(void) {
sc_spin_t mut;
int r = 0;
r |= sc_spin_init(&mut, 0);
r |= sc_spin_lock(&mut);
r |= (sc_spin_trylock(&mut) == EBUSY)? 0 : 1;
r |= sc_spin_unlock(&mut);
r |= sc_spin_destroy(&mut);
return (r == 0)? 1 : 0;
}
int mutex_unlock_dbg (pthread_mutex_t *m) {
int ret;
printf("%16s: (%"PRIuMAX") unlocking mutex %p\n", __FUNCTION__, (uintmax_t)pthread_self(), m);
ret = pthread_mutex_unlock(m);
printf("%16s: (%"PRIuMAX") unlocked mutex %p ret %" PRId32 "\n", __FUNCTION__, (uintmax_t)pthread_self(), m, ret);
return(ret);
#endif /* UNIT TESTS */
/**
* \brief this function registers unit tests for DetectId
*/
void ThreadMacrosRegisterTests(void)
{
#ifdef UNITTESTS /* UNIT TESTS */
UtRegisterTest("ThreadMacrosTest01Mutex", ThreadMacrosTest01Mutex, 1);
UtRegisterTest("ThreadMacrossTest02Spinlocks", ThreadMacrosTest02Spinlocks, 1);
#endif /* UNIT TESTS */
}
#endif /* DBG_THREADS */

@ -1,30 +1,264 @@
/* Copyright (c) 2008 Victor Julien <victor@inliniac.net> */
/**
* Copyright (c) 2009 Open Information Security Foundation
*
* \author Victor Julien <victor@inliniac.net>
* \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
*/
#ifndef __THREADS_H__
#define __THREADS_H__
#include <pthread.h>
/** The mutex/spinlock/condition definitions and functions are used
* in the same way as the POSIX definitionsr; Anyway we are centralizing
* them here to make an easier portability process and debugging process;
* Please, make sure you initialize mutex and spinlocks before using them
* because, some OS doesn't initialize them for you :)
*/
//#define DBG_THREADS
/** Suricata Mutex */
#define sc_mutex_t pthread_mutex_t
#define sc_mutexattr_t pthread_mutexattr_t
#define sc_mutex_destroy pthread_mutex_destroy
/** Mutex Functions */
#ifdef DBG_THREADS
/** When dbg threads is defined, if a mutex fail to lock, it's
* initialized, logged, and does a second try; This is to prevent the system to freeze;
* It is for Mac OS X users;
* If you see a mutex, spinlock or condiion not initialized, report it please!
*/
#define sc_mutex_lock_dbg(mut) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") locking mutex %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut); \
int retl = pthread_mutex_lock(mut); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") locked mutex %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, retl); \
if (retl != 0) { \
switch (retl) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
retl = pthread_mutex_init(mut, NULL); \
if (retl != 0) \
exit(EXIT_FAILURE); \
retl = pthread_mutex_lock(mut); \
break; \
case EDEADLK: \
printf("A deadlock would occur if the thread blocked waiting for mutex\n"); \
break; \
} \
} \
retl; \
})
#include <pthread.h>
int mutex_lock_dbg (pthread_mutex_t *);
int mutex_trylock_dbg (pthread_mutex_t *);
int mutex_unlock_dbg (pthread_mutex_t *);
#define sc_mutex_trylock_dbg(mut) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocking mutex %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut); \
int rett = pthread_mutex_trylock(mut); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocked mutex %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, rett); \
if (rett != 0) { \
switch (rett) { \
case EINVAL: \
printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
break; \
case EBUSY: \
printf("Mutex is already locked\n"); \
break; \
} \
} \
rett; \
})
#define sc_mutex_init_dbg(mut, mutattr) ({ \
int ret; \
ret = pthread_mutex_init(mut, mutattr); \
if (ret != 0) { \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") mutex %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
break; \
case EAGAIN: \
printf("The system temporarily lacks the resources to create another mutex\n"); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") mutex %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
break; \
case ENOMEM: \
printf("The process cannot allocate enough memory to create another mutex\n"); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") mutex %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
break; \
} \
} \
ret; \
})
#define sc_mutex_unlock_dbg(mut) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocking mutex %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut); \
int retu = pthread_mutex_unlock(mut); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocked mutex %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, retu); \
if (retu != 0) { \
switch (retu) { \
case EINVAL: \
printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
break; \
case EPERM: \
printf("The current thread does not hold a lock on mutex\n"); \
break; \
} \
} \
retu; \
})
#define mutex_lock mutex_lock_dbg
#define mutex_trylock mutex_trylock_dbg
#define mutex_unlock mutex_unlock_dbg
#define sc_mutex_init(mut, mutattrs) sc_mutex_init_dbg(mut, mutattrs)
#define sc_mutex_lock(mut) sc_mutex_lock_dbg(mut)
#define sc_mutex_trylock(mut) sc_mutex_trylock_dbg(mut)
#define sc_mutex_unlock(mut) sc_mutex_unlock_dbg(mut)
#else
#define sc_mutex_init(mut, mutattr ) pthread_mutex_init(mut, mutattr)
#define sc_mutex_lock(mut) pthread_mutex_lock(mut)
#define sc_mutex_trylock(mut) pthread_mutex_trylock(mut)
#define sc_mutex_unlock(mut) pthread_mutex_unlock(mut)
#endif
#else /* DBG_THREADS */
/** Conditions/Signals */
/* Here we don't need to do nothing atm */
#define sc_cond_t pthread_cond_t
#define sc_cond_init pthread_cond_init
#define sc_cond_signal pthread_cond_signal
#define sc_cond_timedwait pthread_cond_timedwait
#define mutex_lock pthread_mutex_lock
#define mutex_trylock pthread_mutex_trylock
#define mutex_unlock pthread_mutex_unlock
#ifdef DBG_THREAD
#define sc_cond_wait_dbg(cond, mut) ({ \
int ret = pthread_cond_wait(cond, mut); \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid (or a sc_cond_t not initialized!)\n"); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") failed sc_cond_wait %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, retu); \
break; \
} \
ret; \
})
#define sc_cond_wait sc_cond_wait_dbg
#else
#define sc_cond_wait(cond, mut) pthread_cond_wait(cond, mut)
#endif
#define spin_lock pthread_spin_lock
#define spin_trylock pthread_spin_trylock
#define spin_unlock pthread_spin_unlock
/** Spinlocks */
#define sc_spin_t pthread_spinlock_t
/** If posix spin not supported, use mutex */
#if ((_POSIX_SPIN_LOCKS - 200112L) < 0L)
#define pthread_spinlock_t pthread_mutex_t
#define pthread_spin_init(target,arg) sc_mutex_init(target, NULL)
#define pthread_spin_lock(spin) sc_mutex_lock(spin)
#define pthread_spin_trylock(spin) sc_mutex_trylock(spin)
#define pthread_spin_unlock(spin) sc_mutex_unlock(spin)
#define pthread_spin_destroy(spin) sc_mutex_destroy(spin)
#endif /* End Spin not supported */
#ifdef DBG_THREADS
#define sc_spin_lock_dbg(spin) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") locking spin %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
int ret = pthread_spin_lock(spin); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocked spin %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
break; \
case EDEADLK: \
printf("A deadlock would occur if the thread blocked waiting for spin\n"); \
break; \
} \
ret; \
})
#define sc_spin_trylock_dbg(spin) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocking spin %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
int ret = pthread_spin_trylock(spin); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocked spin %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
break; \
case EDEADLK: \
printf("A deadlock would occur if the thread blocked waiting for spin\n"); \
break; \
case EBUSY: \
printf("A thread currently holds the lock\n"); \
break; \
} \
ret; \
})
#define sc_spin_unlock_dbg(spin) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocking spin %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
int ret = pthread_spin_unlock(spin); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") unlockedspin %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
break; \
case EPERM: \
printf("The calling thread does not hold the lock\n"); \
break; \
} \
ret; \
})
#define sc_spin_init_dbg(spin, spin_attr) ({ \
int ret = pthread_spin_init(spin, spin_attr); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") spinlock %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
break; \
case EBUSY: \
printf("A thread currently holds the lock\n"); \
break; \
case ENOMEM: \
printf("The process cannot allocate enough memory to create another spin\n"); \
break; \
case EAGAIN: \
printf("The system temporarily lacks the resources to create another spin\n"); \
break; \
} \
ret; \
})
#define sc_spin_destroy_dbg(spin) ({ \
printf("%16s(%s:%d): (thread:%"PRIuMAX") condition %p waiting\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
int ret = pthread_spin_destroy(spin); \
printf("%16s(%s:%d): (thread:%"PRIuMAX") condition %p passed %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
switch (ret) { \
case EINVAL: \
printf("The value specified by attr is invalid\n"); \
break; \
case EBUSY: \
printf("A thread currently holds the lock\n"); \
break; \
case ENOMEM: \
printf("The process cannot allocate enough memory to create another spin\n"); \
break; \
case EAGAIN: \
printf("The system temporarily lacks the resources to create another spin\n"); \
break; \
} \
ret; \
})
#define sc_spin_lock sc_spin_lock_dbg
#define sc_spin_trylock sc_spin_trylock_dbg
#define sc_spin_unlock sc_spin_unlock_dbg
#define sc_spin_init sc_spin_init_dbg
#define sc_spin_destroy sc_spin_destroy_dbg
#else /* if no dbg threads defined... */
#define sc_spin_lock(spin) pthread_spin_lock(spin)
#define sc_spin_trylock(spin) pthread_spin_trylock(spin)
#define sc_spin_unlock(spin) pthread_spin_unlock(spin)
#define sc_spin_init(spin, spin_attr) pthread_spin_init(spin, spin_attr)
#define sc_spin_destroy(spin) pthread_spin_destroy(spin)
#endif /* DBG_THREADS */
void ThreadMacrosRegisterTests(void);
#endif /* __THREADS_H__ */

@ -6,6 +6,7 @@
#include "util-mpm.h"
#include "tm-queues.h"
#include "counters.h"
#include "threads.h"
/** Thread flags set and read by threads to control the threads */
#define THV_USE 0x01 /** thread is in use */
@ -29,7 +30,7 @@ typedef struct ThreadVars_ {
char *name;
uint8_t flags;
pthread_spinlock_t flags_spinlock;
sc_spin_t flags_spinlock;
/** aof(action on failure) determines what should be done with the thread
when it encounters certain conditions like failures */
@ -61,8 +62,8 @@ typedef struct ThreadVars_ {
SCPerfContext sc_perf_pctx;
SCPerfCounterArray *sc_perf_pca;
pthread_mutex_t *m;
pthread_cond_t *cond;
sc_mutex_t *m;
sc_cond_t *cond;
struct ThreadVars_ *next;
struct ThreadVars_ *prev;

@ -55,8 +55,8 @@ LogFileCtx *LogFileNewCtx()
}
memset(lf_ctx, 0, sizeof(LogFileCtx));
/** Ensure that it is unlocked */
pthread_mutex_init(&lf_ctx->fp_mutex,NULL);
mutex_unlock(&lf_ctx->fp_mutex);
sc_mutex_init(&lf_ctx->fp_mutex,NULL);
sc_mutex_unlock(&lf_ctx->fp_mutex);
return lf_ctx;
}
@ -73,9 +73,9 @@ int LogFileFreeCtx(LogFileCtx *lf_ctx)
{
if (lf_ctx->fp != NULL)
{
mutex_lock(&lf_ctx->fp_mutex);
sc_mutex_lock(&lf_ctx->fp_mutex);
fclose(lf_ctx->fp);
mutex_unlock(&lf_ctx->fp_mutex);
sc_mutex_unlock(&lf_ctx->fp_mutex);
}
if (lf_ctx->config_file != NULL);
free(lf_ctx->config_file);

@ -56,7 +56,7 @@ typedef struct LogFileCtx_ {
FILE *fp;
/** It will be locked if the log/alert
* record cannot be written to the file in one call */
pthread_mutex_t fp_mutex;
sc_mutex_t fp_mutex;
/** To know where did we read this config */
char *config_file;

@ -50,9 +50,9 @@ void TmqDebugList(void) {
uint16_t i = 0;
for (i = 0; i < tmq_id; i++) {
/* get a lock accessing the len */
mutex_lock(&trans_q[tmqs[i].id].mutex_q);
sc_mutex_lock(&trans_q[tmqs[i].id].mutex_q);
printf("TmqDebugList: id %" PRIu32 ", name \'%s\', len %" PRIu32 "\n", tmqs[i].id, tmqs[i].name, trans_q[tmqs[i].id].len);
mutex_unlock(&trans_q[tmqs[i].id].mutex_q);
sc_mutex_unlock(&trans_q[tmqs[i].id].mutex_q);
}
}
@ -71,7 +71,7 @@ void TmValidateQueueState(void)
char err = FALSE;
for (i = 0; i < tmq_id; i++) {
mutex_lock(&trans_q[tmqs[i].id].mutex_q);
sc_mutex_lock(&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);
err = TRUE;
@ -79,7 +79,7 @@ void TmValidateQueueState(void)
printf("Error: Queue \"%s\" doesn't have a writer\n", tmqs[i].name);
err = TRUE;
}
mutex_unlock(&trans_q[tmqs[i].id].mutex_q);
sc_mutex_unlock(&trans_q[tmqs[i].id].mutex_q);
if (err == TRUE)
goto error;

@ -15,6 +15,21 @@
#include "threads.h"
#include "util-debug.h"
#ifdef OS_FREEBSD
#include <sched.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/cpuset.h>
#define cpu_set_t cpuset_t
#elif OS_DARWIN
#include <mach/mach.h>
#include <mach/mach_init.h>
#include <mach/thread_policy.h>
#define cpu_set_t thread_affinity_policy_data_t
#define CPU_SET(cpu_id, new_mask) (*(new_mask)).affinity_tag = (cpu_id + 1)
#define CPU_ZERO(new_mask) (*(new_mask)).affinity_tag = THREAD_AFFINITY_TAG_NULL
#endif /* OS_FREEBSD */
/* prototypes */
static int SetCPUAffinity(int cpu);
@ -22,7 +37,7 @@ static int SetCPUAffinity(int cpu);
ThreadVars *tv_root[TVT_MAX] = { NULL };
/* lock to protect tv_root */
pthread_mutex_t tv_root_lock = PTHREAD_MUTEX_INITIALIZER;
sc_mutex_t tv_root_lock = PTHREAD_MUTEX_INITIALIZER;
/* Action On Failure(AOF). Determines how the engine should behave when a
thread encounters a failure. Defaults to restart the failed thread */
@ -60,34 +75,34 @@ typedef struct TmVarSlot_ {
*/
inline int TmThreadsCheckFlag(ThreadVars *tv, uint8_t flag) {
int r;
if (spin_lock(&tv->flags_spinlock) != 0) {
if (sc_spin_lock(&tv->flags_spinlock) != 0) {
SCLogError(SC_SPINLOCK_ERROR,"spin lock errno=%d",errno);
return 0;
}
r = (tv->flags & flag);
spin_unlock(&tv->flags_spinlock);
sc_spin_unlock(&tv->flags_spinlock);
return r;
}
inline void TmThreadsSetFlag(ThreadVars *tv, uint8_t flag) {
if (spin_lock(&tv->flags_spinlock) != 0) {
if (sc_spin_lock(&tv->flags_spinlock) != 0) {
SCLogError(SC_SPINLOCK_ERROR,"spin lock errno=%d",errno);
return;
}
tv->flags |= flag;
spin_unlock(&tv->flags_spinlock);
sc_spin_unlock(&tv->flags_spinlock);
}
inline void TmThreadsUnsetFlag(ThreadVars *tv, uint8_t flag) {
if (spin_lock(&tv->flags_spinlock) != 0) {
if (sc_spin_lock(&tv->flags_spinlock) != 0) {
SCLogError(SC_SPINLOCK_ERROR,"spin lock errno=%d",errno);
return;
}
tv->flags &= ~flag;
spin_unlock(&tv->flags_spinlock);
sc_spin_unlock(&tv->flags_spinlock);
}
/* 1 slot functions */
@ -570,7 +585,14 @@ static int SetCPUAffinity(int cpu) {
CPU_ZERO(&cs);
CPU_SET(cpu,&cs);
#ifdef OS_FREEBSD
int r = cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_TID,tid,sizeof(cpu_set_t),&cs);
#elif OS_DARWIN
int r = thread_policy_set(mach_thread_self(), THREAD_AFFINITY_POLICY, (void*)&cs, THREAD_AFFINITY_POLICY_COUNT);
#else
int r = sched_setaffinity(tid,sizeof(cpu_set_t),&cs);
#endif /* OS_FREEBSD */
if (r != 0) {
printf("Warning: sched_setaffinity failed (%" PRId32 "): %s\n", r, strerror(errno));
}
@ -614,7 +636,8 @@ ThreadVars *TmThreadCreate(char *name, char *inq_name, char *inqh_name,
if (tv == NULL) goto error;
memset(tv, 0, sizeof(ThreadVars));
pthread_spin_init(&tv->flags_spinlock, PTHREAD_PROCESS_PRIVATE);
sc_spin_init(&tv->flags_spinlock, PTHREAD_PROCESS_PRIVATE);
sc_mutex_init(&tv->sc_perf_pctx.m, NULL);
tv->name = name;
/* default state for every newly created thread */
@ -746,7 +769,7 @@ ThreadVars *TmThreadCreateMgmtThread(char *name, void *(fn_p)(void *),
*/
void TmThreadAppend(ThreadVars *tv, int type)
{
pthread_mutex_lock(&tv_root_lock);
sc_mutex_lock(&tv_root_lock);
if (tv_root[type] == NULL) {
tv_root[type] = tv;
@ -754,7 +777,7 @@ void TmThreadAppend(ThreadVars *tv, int type)
tv->prev = NULL;
//printf("TmThreadAppend: thread \'%s\' is the first thread in the list.\n", tv->name);
pthread_mutex_unlock(&tv_root_lock);
sc_mutex_unlock(&tv_root_lock);
return;
}
@ -771,7 +794,7 @@ void TmThreadAppend(ThreadVars *tv, int type)
t = t->next;
}
pthread_mutex_unlock(&tv_root_lock);
sc_mutex_unlock(&tv_root_lock);
//printf("TmThreadAppend: thread \'%s\' is added to the list.\n", tv->name);
}
@ -796,12 +819,12 @@ void TmThreadKillThreads(void) {
//printf("TmThreadKillThreads: (t->inq->reader_cnt + t->inq->writer_cnt) %" PRIu32 "\n", (t->inq->reader_cnt + t->inq->writer_cnt));
/* make sure our packet pending counter doesn't block */
pthread_cond_signal(&cond_pending);
sc_cond_signal(&cond_pending);
/* signal the queue for the number of users */
for (i = 0; i < (tv->inq->reader_cnt + tv->inq->writer_cnt); i++)
pthread_cond_signal(&trans_q[tv->inq->id].cond_q);
sc_cond_signal(&trans_q[tv->inq->id].cond_q);
/* to be sure, signal more */
int cnt = 0;
@ -814,7 +837,7 @@ void TmThreadKillThreads(void) {
cnt++;
for (i = 0; i < (tv->inq->reader_cnt + tv->inq->writer_cnt); i++)
pthread_cond_signal(&trans_q[tv->inq->id].cond_q);
sc_cond_signal(&trans_q[tv->inq->id].cond_q);
usleep(100);
}
@ -912,22 +935,22 @@ void TmThreadSetAOF(ThreadVars *tv, uint8_t aof)
*/
void TmThreadInitMC(ThreadVars *tv)
{
if ( (tv->m = malloc(sizeof(pthread_mutex_t))) == NULL) {
if ( (tv->m = malloc(sizeof(sc_mutex_t))) == NULL) {
printf("Error allocating memory\n");
exit(0);
}
if (pthread_mutex_init(tv->m, NULL) != 0) {
if (sc_mutex_init(tv->m, NULL) != 0) {
printf("Error initializing the tv->m mutex\n");
exit(0);
}
if ( (tv->cond = malloc(sizeof(pthread_cond_t))) == NULL) {
if ( (tv->cond = malloc(sizeof(sc_cond_t))) == NULL) {
printf("Error allocating memory\n");
exit(0);
}
if (pthread_cond_init(tv->cond, NULL) != 0) {
if (sc_cond_init(tv->cond, NULL) != 0) {
printf("Error initializing the tv->cond condition variable\n");
exit(0);
}
@ -1131,20 +1154,20 @@ ThreadVars *TmThreadsGetCallingThread(void)
ThreadVars *tv = NULL;
int i = 0;
mutex_lock(&tv_root_lock);
sc_mutex_lock(&tv_root_lock);
for (i = 0; i < TVT_MAX; i++) {
tv = tv_root[i];
while (tv) {
if (pthread_equal(self, tv->t)) {
mutex_unlock(&tv_root_lock);
sc_mutex_unlock(&tv_root_lock);
return tv;
}
tv = tv->next;
}
}
mutex_unlock(&tv_root_lock);
sc_mutex_unlock(&tv_root_lock);
return NULL;
}

@ -12,7 +12,7 @@ enum {
extern ThreadVars *tv_root[TVT_MAX];
extern pthread_mutex_t tv_root_lock;
extern sc_mutex_t tv_root_lock;
void Tm1SlotSetFunc(ThreadVars *, TmModule *, void *);

@ -49,10 +49,10 @@ Packet *TmqhInputFlow(ThreadVars *tv)
{
PacketQueue *q = &trans_q[tv->inq->id];
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
if (q->len == 0) {
/* if we have no packets in queue, wait... */
pthread_cond_wait(&q->cond_q, &q->mutex_q);
sc_cond_wait(&q->cond_q, &q->mutex_q);
}
if (tv->sc_perf_pctx.perf_flag == 1)
@ -60,11 +60,11 @@ Packet *TmqhInputFlow(ThreadVars *tv)
if (q->len > 0) {
Packet *p = PacketDequeue(q);
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return p;
} else {
/* return NULL if we have no pkt. Should only happen on signals. */
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return NULL;
}
}
@ -177,10 +177,10 @@ void TmqhOutputFlow(ThreadVars *tv, Packet *p)
}
PacketQueue *q = &trans_q[qid];
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
PacketEnqueue(q, p);
pthread_cond_signal(&q->cond_q);
mutex_unlock(&q->mutex_q);
sc_cond_signal(&q->cond_q);
sc_mutex_unlock(&q->mutex_q);
}
#ifdef UNITTESTS

@ -22,10 +22,10 @@ void TmqhOutputVerdictNfq(ThreadVars *t, Packet *p)
#if 0
PacketQueue *q = &trans_q[p->verdict_q_id];
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
PacketEnqueue(q, p);
pthread_cond_signal(&q->cond_q);
mutex_unlock(&q->mutex_q);
sc_cond_signal(&q->cond_q);
sc_mutex_unlock(&q->mutex_q);
#endif
}

@ -26,14 +26,14 @@ Packet *TmqhInputPacketpool(ThreadVars *t)
/* XXX */
Packet *p = SetupPkt();
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
pending++;
//printf("PcapFileCallback: pending %" PRIu32 "\n", pending);
#ifdef DBG_PERF
if (pending > dbg_maxpending)
dbg_maxpending = pending;
#endif /* DBG_PERF */
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
/*
* Disabled because it can enter a 'wait' state, while
@ -41,11 +41,11 @@ Packet *TmqhInputPacketpool(ThreadVars *t)
* to free packets, the exact condition we are waiting
* for. VJ 09-01-16
*
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
if (pending > MAX_PENDING) {
pthread_cond_wait(&cond_pending, &mutex_pending);
sc_cond_wait(&cond_pending, &mutex_pending);
}
mutex_unlock(&mutex_pending);
sc_mutex_unlock(&mutex_pending);
*/
return p;
}
@ -62,8 +62,8 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
//printf("TmqhOutputPacketpool: tunnel packet: %p %s\n", p,p->root ? "upper layer":"root");
/* get a lock */
pthread_mutex_t *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt;
mutex_lock(m);
sc_mutex_t *m = p->root ? &p->root->mutex_rtv_cnt : &p->mutex_rtv_cnt;
sc_mutex_lock(m);
if (IS_TUNNEL_ROOT_PKT(p)) {
//printf("TmqhOutputPacketpool: IS_TUNNEL_ROOT_PKT\n");
@ -80,7 +80,7 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
* by the tunnel packets, and we will enqueue it
* when we handle them */
p->tunnel_verdicted = 1;
mutex_unlock(m);
sc_mutex_unlock(m);
return;
}
} else {
@ -103,7 +103,7 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
/* fall through */
}
}
mutex_unlock(m);
sc_mutex_unlock(m);
//printf("TmqhOutputPacketpool: tunnel stuff done, move on\n");
}
@ -112,18 +112,18 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
if (proot && p->root != NULL) {
CLEAR_PACKET(p->root);
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
PacketEnqueue(q, p->root);
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
}
CLEAR_PACKET(p);
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
PacketEnqueue(q, p);
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
mutex_lock(&mutex_pending);
sc_mutex_lock(&mutex_pending);
//printf("TmqhOutputPacketpool: pending %" PRIu32 "\n", pending);
if (pending > 0) {
pending--;
@ -138,8 +138,8 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
printf("TmqhOutputPacketpool: warning, trying to subtract from 0 pending counter.\n");
}
if (pending <= MAX_PENDING)
pthread_cond_signal(&cond_pending);
mutex_unlock(&mutex_pending);
sc_cond_signal(&cond_pending);
sc_mutex_unlock(&mutex_pending);
}
/**
@ -157,12 +157,12 @@ void TmqhReleasePacketsToPacketPool(PacketQueue *pq)
if (pq == NULL)
return;
mutex_lock(&pq->mutex_q);
sc_mutex_lock(&pq->mutex_q);
while ( (p = PacketDequeue(pq)) != NULL)
TmqhOutputPacketpool(NULL, p);
mutex_unlock(&pq->mutex_q);
sc_mutex_unlock(&pq->mutex_q);
return;
}

@ -20,10 +20,10 @@ Packet *TmqhInputSimple(ThreadVars *t)
{
PacketQueue *q = &trans_q[t->inq->id];
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
if (q->len == 0) {
/* if we have no packets in queue, wait... */
pthread_cond_wait(&q->cond_q, &q->mutex_q);
sc_cond_wait(&q->cond_q, &q->mutex_q);
}
if (t->sc_perf_pctx.perf_flag == 1)
@ -31,11 +31,11 @@ Packet *TmqhInputSimple(ThreadVars *t)
if (q->len > 0) {
Packet *p = PacketDequeue(q);
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return p;
} else {
/* return NULL if we have no pkt. Should only happen on signals. */
mutex_unlock(&q->mutex_q);
sc_mutex_unlock(&q->mutex_q);
return NULL;
}
}
@ -44,9 +44,9 @@ void TmqhOutputSimple(ThreadVars *t, Packet *p)
{
PacketQueue *q = &trans_q[t->outq->id];
mutex_lock(&q->mutex_q);
sc_mutex_lock(&q->mutex_q);
PacketEnqueue(q, p);
pthread_cond_signal(&q->cond_q);
mutex_unlock(&q->mutex_q);
sc_cond_signal(&q->cond_q);
sc_mutex_unlock(&q->mutex_q);
}

@ -1,18 +1,19 @@
/** Copyright (c) 2009 Open Information Security Foundation.
* \author Anoop Saldanha <poonaatsoc@gmail.com>
*/
#include "eidps-common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "threads.h"
#include "util-debug.h"
#include "util-debug-filters.h"
#include "util-error.h"
#include "util-enum.h"
#include "threads.h"
#include <pthread.h>
/* both of these are defined in util-debug.c */
extern int sc_log_module_initialized;
@ -32,7 +33,7 @@ static SCLogFGFilterFile *sc_log_fg_filters[SC_LOG_FILTER_MAX] = { NULL, NULL };
/**
* \brief Mutex for accessing the fine-grained fiters sc_log_fg_filters
*/
static pthread_mutex_t sc_log_fg_filters_m[SC_LOG_FILTER_MAX] = { PTHREAD_MUTEX_INITIALIZER,
static sc_mutex_t sc_log_fg_filters_m[SC_LOG_FILTER_MAX] = { PTHREAD_MUTEX_INITIALIZER,
PTHREAD_MUTEX_INITIALIZER };
/**
@ -43,7 +44,7 @@ static SCLogFDFilter *sc_log_fd_filters = NULL;
/**
* \brief Mutex for accessing the function-dependent filters sc_log_fd_filters
*/
static pthread_mutex_t sc_log_fd_filters_m = PTHREAD_MUTEX_INITIALIZER;
static sc_mutex_t sc_log_fd_filters_m = PTHREAD_MUTEX_INITIALIZER;
/**
* \brief Holds the thread_list required by function-dependent filters
@ -53,7 +54,7 @@ static SCLogFDFilterThreadList *sc_log_fd_filters_tl = NULL;
/**
* \brief Mutex for accessing the FD thread_list sc_log_fd_filters_tl
*/
static pthread_mutex_t sc_log_fd_filters_tl_m = PTHREAD_MUTEX_INITIALIZER;
static sc_mutex_t sc_log_fd_filters_tl_m = PTHREAD_MUTEX_INITIALIZER;
/**
@ -245,7 +246,10 @@ static inline int SCLogAddFGFilter(const char *file, const char *function,
return -1;
}
mutex_lock(&sc_log_fg_filters_m[listtype]);
sc_mutex_t *m = &sc_log_fg_filters_m[listtype];
sc_mutex_lock(m);
fgf_file = sc_log_fg_filters[listtype];
prev_fgf_file = fgf_file;
@ -311,7 +315,7 @@ static inline int SCLogAddFGFilter(const char *file, const char *function,
}
done:
mutex_unlock(&sc_log_fg_filters_m[listtype]);
sc_mutex_unlock(&sc_log_fg_filters_m[listtype]);
sc_log_fg_filters_present = 1;
return 0;
@ -347,12 +351,12 @@ static int SCLogMatchFGFilter(const char *file, const char *function, int line,
return -1;
}
mutex_lock(&sc_log_fg_filters_m[listtype]);
sc_mutex_lock(&sc_log_fg_filters_m[listtype]);
fgf_file = sc_log_fg_filters[listtype];
if (fgf_file == NULL) {
mutex_unlock(&sc_log_fg_filters_m[listtype]);
sc_mutex_unlock(&sc_log_fg_filters_m[listtype]);
return 1;
}
@ -396,7 +400,7 @@ static int SCLogMatchFGFilter(const char *file, const char *function, int line,
}
if (match == 1) {
mutex_unlock(&sc_log_fg_filters_m[listtype]);
sc_mutex_unlock(&sc_log_fg_filters_m[listtype]);
if (listtype == SC_LOG_FILTER_WL)
return 1;
else
@ -406,7 +410,7 @@ static int SCLogMatchFGFilter(const char *file, const char *function, int line,
fgf_file = fgf_file->next;
}
mutex_unlock(&sc_log_fg_filters_m[listtype]);
sc_mutex_unlock(&sc_log_fg_filters_m[listtype]);
if (listtype == SC_LOG_FILTER_WL)
return 0;
@ -503,7 +507,7 @@ void SCLogReleaseFGFilters(void)
int i = 0;
for (i = 0; i < SC_LOG_FILTER_MAX; i++) {
mutex_lock(&sc_log_fg_filters_m[i]);
sc_mutex_lock(&sc_log_fg_filters_m[i]);
fgf_file = sc_log_fg_filters[i];
while (fgf_file != NULL) {
@ -532,7 +536,7 @@ void SCLogReleaseFGFilters(void)
free(temp);
}
mutex_unlock(&sc_log_fg_filters_m[i]);
sc_mutex_unlock(&sc_log_fg_filters_m[i]);
sc_log_fg_filters[i] = NULL;
}
@ -564,7 +568,7 @@ int SCLogPrintFGFilters()
#endif
for (i = 0; i < SC_LOG_FILTER_MAX; i++) {
mutex_lock(&sc_log_fg_filters_m[i]);
sc_mutex_lock(&sc_log_fg_filters_m[i]);
fgf_file = sc_log_fg_filters[i];
while (fgf_file != NULL) {
@ -590,7 +594,7 @@ int SCLogPrintFGFilters()
fgf_file = fgf_file->next;
}
mutex_unlock(&sc_log_fg_filters_m[i]);
sc_mutex_unlock(&sc_log_fg_filters_m[i]);
}
return count;
@ -632,7 +636,7 @@ int SCLogMatchFDFilter(const char *function)
{
SCLogFDFilterThreadList *thread_list = NULL;
pthread_t self = syscall(SYS_gettid);
pid_t self = syscall(SYS_gettid);
#ifndef DEBUG
return 1;
@ -644,10 +648,10 @@ int SCLogMatchFDFilter(const char *function)
return 0;
}
mutex_lock(&sc_log_fd_filters_tl_m);
sc_mutex_lock(&sc_log_fd_filters_tl_m);
if (sc_log_fd_filters_tl == NULL) {
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
if (sc_log_fd_filters != NULL)
return 0;
return 1;
@ -657,17 +661,17 @@ int SCLogMatchFDFilter(const char *function)
while (thread_list != NULL) {
if (self == thread_list->t) {
if (thread_list->entered > 0) {
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
return 1;
}
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
return 0;
}
thread_list = thread_list->next;
}
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
return 0;
}
@ -689,7 +693,7 @@ int SCLogCheckFDFilterEntry(const char *function)
SCLogFDFilterThreadList *thread_list_prev = NULL;
SCLogFDFilterThreadList *thread_list_temp = NULL;
pthread_t self = syscall(SYS_gettid);
pid_t self = syscall(SYS_gettid);
if (sc_log_module_initialized != 1) {
printf("Logging module not initialized. Call SCLogInitLogModule() "
@ -697,7 +701,7 @@ int SCLogCheckFDFilterEntry(const char *function)
return 0;
}
mutex_lock(&sc_log_fd_filters_m);
sc_mutex_lock(&sc_log_fd_filters_m);
curr = sc_log_fd_filters;
@ -709,13 +713,13 @@ int SCLogCheckFDFilterEntry(const char *function)
}
if (curr == NULL) {
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
return 1;
}
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
mutex_lock(&sc_log_fd_filters_tl_m);
sc_mutex_lock(&sc_log_fd_filters_tl_m);
thread_list = sc_log_fd_filters_tl;
thread_list_temp = thread_list;
@ -730,7 +734,7 @@ int SCLogCheckFDFilterEntry(const char *function)
if (thread_list != NULL) {
thread_list->entered++;
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
return 1;
}
@ -748,7 +752,7 @@ int SCLogCheckFDFilterEntry(const char *function)
else
thread_list_prev->next = thread_list_temp;
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
return 1;
}
@ -767,7 +771,7 @@ void SCLogCheckFDFilterExit(const char *function)
SCLogFDFilterThreadList *thread_list = NULL;
pthread_t self = syscall(SYS_gettid);
pid_t self = syscall(SYS_gettid);
if (sc_log_module_initialized != 1) {
printf("Logging module not initialized. Call SCLogInitLogModule() "
@ -775,7 +779,7 @@ void SCLogCheckFDFilterExit(const char *function)
return;
}
mutex_lock(&sc_log_fd_filters_m);
sc_mutex_lock(&sc_log_fd_filters_m);
curr = sc_log_fd_filters;
@ -787,13 +791,13 @@ void SCLogCheckFDFilterExit(const char *function)
}
if (curr == NULL) {
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
return;
}
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
mutex_lock(&sc_log_fd_filters_tl_m);
sc_mutex_lock(&sc_log_fd_filters_tl_m);
thread_list = sc_log_fd_filters_tl;
while (thread_list != NULL) {
@ -803,7 +807,7 @@ void SCLogCheckFDFilterExit(const char *function)
thread_list = thread_list->next;
}
mutex_unlock(&sc_log_fd_filters_tl_m);
sc_mutex_unlock(&sc_log_fd_filters_tl_m);
thread_list->entered--;
@ -835,14 +839,16 @@ int SCLogAddFDFilter(const char *function)
return -1;
}
mutex_lock(&sc_log_fd_filters_m);
sc_mutex_lock(&sc_log_fd_filters_m);
curr = sc_log_fd_filters;
while (curr != NULL) {
prev = curr;
if (strcmp(function, curr->func) == 0) {
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
return 0;
}
@ -867,7 +873,7 @@ int SCLogAddFDFilter(const char *function)
prev->next = temp;
}
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
sc_log_fd_filters_present = 1;
return 0;
@ -881,7 +887,7 @@ void SCLogReleaseFDFilters(void)
SCLogFDFilter *fdf = NULL;
SCLogFDFilter *temp = NULL;
mutex_lock(&sc_log_fd_filters_m);
sc_mutex_lock(&sc_log_fd_filters_m);
fdf = sc_log_fd_filters;
while (fdf != NULL) {
@ -892,7 +898,7 @@ void SCLogReleaseFDFilters(void)
sc_log_fd_filters = NULL;
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock( &sc_log_fd_filters_m );
return;
}
@ -921,10 +927,10 @@ int SCLogRemoveFDFilter(const char *function)
return -1;
}
mutex_lock(&sc_log_fd_filters_m);
sc_mutex_lock(&sc_log_fd_filters_m);
if (sc_log_fd_filters == NULL) {
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
return 0;
}
@ -939,7 +945,9 @@ int SCLogRemoveFDFilter(const char *function)
}
if (curr == NULL) {
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
return 0;
}
@ -950,7 +958,7 @@ int SCLogRemoveFDFilter(const char *function)
SCLogReleaseFDFilter(curr);
mutex_unlock(&sc_log_fd_filters_m);
sc_mutex_unlock(&sc_log_fd_filters_m);
if (sc_log_fd_filters == NULL)
sc_log_fd_filters_present = 0;
@ -967,6 +975,7 @@ int SCLogPrintFDFilters(void)
{
SCLogFDFilter *fdf = NULL;
int count = 0;
int r = 0;
if (sc_log_module_initialized != 1) {
printf("Logging module not initialized. Call SCLogInitLogModule() "
@ -978,7 +987,7 @@ int SCLogPrintFDFilters(void)
printf("FD filters:\n");
#endif
mutex_lock(&sc_log_fd_filters_m);
r = sc_mutex_lock(&sc_log_fd_filters_m);
fdf = sc_log_fd_filters;
while (fdf != NULL) {
@ -989,7 +998,7 @@ int SCLogPrintFDFilters(void)
count++;
}
mutex_unlock(&sc_log_fd_filters_m);
r = sc_mutex_unlock(&sc_log_fd_filters_m);
return count;
}

@ -2,11 +2,20 @@
* \author Anoop Saldanha <poonaatsoc@gmail.com>
*/
#include <pthread.h>
#ifndef __DEBUG_FILTERS_H__
#define __DEBUG_FILTERS_H__
#include <pthread.h>
#include "threads.h"
/**
* \brief extra defines needed for FreeBSD
*/
#ifdef OS_FREEBSD
#ifndef SYS_gettid
#define SYS_gettid 1105
#endif
#endif /* OS_FREEBSD */
/**
* \brief Enum that holds the different kinds of filters available

@ -21,7 +21,6 @@
#include "decode.h"
#include "detect.h"
#include "packet-queue.h"
#include "threads.h"
#include "threadvars.h"
#include "tm-queuehandlers.h"

@ -3,7 +3,7 @@
*/
#include <stdio.h>
#include <pthread.h>
#include "threads.h"
#include <stdint.h>
#include <syslog.h>

@ -10,6 +10,13 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#ifdef OS_FREEBSD
#include <netinet/in.h>
#endif /* OS_FREEBSD */
#ifdef OS_DARWIN
#include <netinet/in.h>
#endif /* OS_FREEBSD */
#include "util-radix-tree.h"
#include "util-debug.h"
#include "util-error.h"

@ -6,7 +6,7 @@
#include "util-debug.h"
static struct timeval current_time = { 0, 0 };
static pthread_mutex_t current_time_mutex = PTHREAD_MUTEX_INITIALIZER;
static sc_mutex_t current_time_mutex = PTHREAD_MUTEX_INITIALIZER;
static char live = TRUE;
void TimeModeSetLive(void)
@ -29,14 +29,14 @@ void TimeSet(struct timeval *tv)
if (tv == NULL)
return;
mutex_lock(&current_time_mutex);
sc_mutex_lock(&current_time_mutex);
current_time.tv_sec = tv->tv_sec;
current_time.tv_usec = tv->tv_usec;
SCLogDebug("time set to %" PRIuMAX " sec, %" PRIuMAX " usec",
(uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec);
mutex_unlock(&current_time_mutex);
sc_mutex_unlock(&current_time_mutex);
}
/** \brief set the time to "gettimeofday" meant for testing */
@ -57,10 +57,10 @@ void TimeGet(struct timeval *tv)
if (live == TRUE) {
gettimeofday(tv, NULL);
} else {
mutex_lock(&current_time_mutex);
sc_mutex_lock(&current_time_mutex);
tv->tv_sec = current_time.tv_sec;
tv->tv_usec = current_time.tv_usec;
mutex_unlock(&current_time_mutex);
sc_mutex_unlock(&current_time_mutex);
}
SCLogDebug("time we got is %" PRIuMAX " sec, %" PRIuMAX " usec",

Loading…
Cancel
Save