Fixed concurrency issues with POSIX mutexes

This commit is contained in:
Simone Margaritelli 2010-09-15 03:35:34 +02:00
parent 37c3482c74
commit 2e86de2000
4 changed files with 42 additions and 24 deletions

View File

@ -27,9 +27,9 @@
#include <pthread.h>
PRIVATE AI_snort_alert *alerts = NULL;
PRIVATE FILE *alert_fp = NULL;
PRIVATE BOOL lock_flag = false;
PRIVATE AI_snort_alert *alerts = NULL;
PRIVATE FILE *alert_fp = NULL;
PRIVATE pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
/** \defgroup alert_parser Parse the alert log into binary structures
* @{ */
@ -106,8 +106,8 @@ AI_file_alertparser_thread ( void* arg )
inotify_rm_watch ( ifd, wd );
close ( ifd );
/* Set the lock flag to true until it's done with alert parsing */
lock_flag = true;
/* Acquire the mutex until it's done with alert parsing */
pthread_mutex_lock(&file_lock)
while ( !feof ( alert_fp ))
{
@ -305,7 +305,8 @@ AI_file_alertparser_thread ( void* arg )
}
}
lock_flag = false;
/* Release the file lock. */
pthread_mutex_unlock(&file_lock);
}
pthread_exit ((void*) 0 );
@ -351,8 +352,15 @@ _AI_copy_alerts ( AI_snort_alert *node )
AI_snort_alert*
AI_get_alerts ()
{
while ( lock_flag );
return _AI_copy_alerts ( alerts );
/* Acquire the mutex or block until the thread releases it. */
pthread_mutex_lock(&file_lock);
AI_snort_alert* alerts = _AI_copy_alerts ( alerts );
/* We've done, we can release the mutex again and let the thread read the file. */
pthread_mutex_unlock(&file_lock);
return alerts;
} /* ----- end of function AI_get_alerts ----- */

View File

@ -45,7 +45,7 @@ typedef struct {
PRIVATE hierarchy_node *h_root[CLUSTER_TYPES] = { NULL };
PRIVATE AI_config *_config = NULL;
PRIVATE AI_snort_alert *alert_log = NULL;
PRIVATE BOOL lock_flag = false;
PRIVATE pthread_mutex_t cluster_lock = PTHREAD_MUTEX_INITIALIZER;
/**
@ -375,7 +375,7 @@ _AI_cluster_thread ( void* arg )
sleep ( _config->alertClusteringInterval );
/* Set the lock over the alert log until it's done with the clustering operation */
lock_flag = true;
pthread_mutex_lock(&cluster_lock);
/* Free the current alert log and get the latest one */
if ( alert_log )
@ -386,7 +386,7 @@ _AI_cluster_thread ( void* arg )
if ( !( alert_log = get_alerts() ))
{
lock_flag = false;
pthread_mutex_unlock(&cluster_lock);
continue;
}
@ -490,7 +490,7 @@ _AI_cluster_thread ( void* arg )
/* break; */
} while ( old_alert_count != alert_count );
lock_flag = false;
pthread_mutex_unlock(&cluster_lock);
if ( !( cluster_fp = fopen ( _config->clusterfile, "w" )) )
{
@ -661,8 +661,13 @@ _AI_copy_clustered_alerts ( AI_snort_alert *node )
AI_snort_alert*
AI_get_clustered_alerts ()
{
while ( lock_flag );
return _AI_copy_clustered_alerts ( alert_log );
pthread_mutex_lock(&cluster_lock);
AI_snort_alert *alerts = _AI_copy_clustered_alerts ( alert_log );
pthread_mutex_unlock(&cluster_lock);
return alerts;
} /* ----- end of function AI_get_clustered_alerts ----- */
/** @} */

View File

@ -70,7 +70,7 @@ PRIVATE AI_hyperalert_info *hyperalerts = NULL;
PRIVATE AI_config *conf = NULL;
PRIVATE AI_snort_alert *alerts = NULL;
PRIVATE AI_alert_correlation *correlation_table = NULL;
PRIVATE BOOL lock_flag = false;
PRIVATE pthread_mutex_t correlation_lock = PTHREAD_MUTEX_INITIALIZER;
/**
* \brief Clean up the correlation hash table
@ -756,7 +756,7 @@ AI_alert_correlation_thread ( void *arg )
}
/* Set the lock flag to true, and keep it this way until I've done with generating the new hyperalerts */
lock_flag = true;
pthread_mutex_lock(&correlation_lock);
if ( alerts )
{
@ -766,7 +766,7 @@ AI_alert_correlation_thread ( void *arg )
if ( !( alerts = AI_get_clustered_alerts() ))
{
lock_flag = false;
pthread_mutex_unlock(&correlation_lock);
continue;
}
@ -917,7 +917,7 @@ AI_alert_correlation_thread ( void *arg )
#endif
}
lock_flag = false;
pthread_mutex_unlock(&correlation_lock);
}
pthread_exit (( void* ) 0 );

17
db.c
View File

@ -32,7 +32,7 @@
PRIVATE AI_config *config;
PRIVATE AI_snort_alert *alerts = NULL;
PRIVATE BOOL lock_flag = false;
PRIVATE pthread_mutex_t db_lock = PTHREAD_MUTEX_INITIALIZER;
/** pthread mutex for accessing database data */
PRIVATE pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
@ -80,7 +80,7 @@ AI_db_alertparser_thread ( void *arg )
sleep ( config->databaseParsingInterval );
/* Set the lock flag to true until it's done with alert parsing */
lock_flag = true;
pthread_mutex_lock(&db_lock);
memset ( query, 0, sizeof ( query ));
snprintf ( query, sizeof (query), "select cid, unix_timestamp(timestamp), signature from event where cid > %d "
@ -98,7 +98,7 @@ AI_db_alertparser_thread ( void *arg )
DB_close();
_dpd.fatalMsg ( "AIPreproc: Could not store the query result at %s:%d\n", __FILE__, __LINE__ );
} else if ( rows == 0 ) {
lock_flag = false;
pthread_mutex_unlock(&db_lock);
continue;
}
@ -224,7 +224,7 @@ AI_db_alertparser_thread ( void *arg )
}
}
lock_flag = false;
pthread_mutex_unlock(&db_lock);
DB_free_result ( res );
latest_time = time ( NULL );
}
@ -272,8 +272,13 @@ _AI_db_copy_alerts ( AI_snort_alert *node )
AI_snort_alert*
AI_db_get_alerts ()
{
while ( lock_flag );
return _AI_db_copy_alerts ( alerts );
pthread_mutex_lock(&db_lock);
AI_snort_alert *alerts = _AI_db_copy_alerts ( alerts );
pthread_mutex_unlock(&db_lock);
return alerts;
} /* ----- end of function AI_db_get_alerts ----- */
/** @} */