synchronize reads of std::map to fix crash due to memory corruption.

Apparently containers aren't thread-safe even for reads.
This commit is contained in:
ehouse 2009-07-03 14:54:21 +00:00
parent a954286960
commit f4db39f490
2 changed files with 13 additions and 8 deletions

View file

@ -25,6 +25,7 @@
#include <assert.h> #include <assert.h>
#include "configs.h" #include "configs.h"
#include "mlock.h"
#define MAX_LINE 128 #define MAX_LINE 128
@ -46,6 +47,8 @@ RelayConfigs::InitConfigs( const char* cfile )
RelayConfigs::RelayConfigs( const char* cfile ) RelayConfigs::RelayConfigs( const char* cfile )
{ {
pthread_mutex_init( &m_values_mutex, NULL );
/* There's an order here. Open multiple files, if present. File in /etc /* There's an order here. Open multiple files, if present. File in /etc
is first, but overridden by local file which is in turn overridden by is first, but overridden by local file which is in turn overridden by
file passed in. */ file passed in. */
@ -57,10 +60,10 @@ RelayConfigs::RelayConfigs( const char* cfile )
bool bool
RelayConfigs::GetValueFor( const char* key, int* value ) RelayConfigs::GetValueFor( const char* key, int* value )
{ {
map<string,string>::const_iterator iter = m_values.find(key); char buf[32];
bool found = iter != m_values.end(); bool found = GetValueFor( key, buf, sizeof(buf) );
if ( found ) { if ( found ) {
*value = atoi( iter->second.c_str() ); *value = atoi( buf );
} }
return found; return found;
} }
@ -79,6 +82,7 @@ RelayConfigs::GetValueFor( const char* key, time_t* value )
bool bool
RelayConfigs::GetValueFor( const char* key, char* buf, int len ) RelayConfigs::GetValueFor( const char* key, char* buf, int len )
{ {
MutexLock ml( &m_values_mutex );
map<string,string>::const_iterator iter = m_values.find(key); map<string,string>::const_iterator iter = m_values.find(key);
bool found = iter != m_values.end(); bool found = iter != m_values.end();
if ( found ) { if ( found ) {
@ -90,9 +94,8 @@ RelayConfigs::GetValueFor( const char* key, char* buf, int len )
bool bool
RelayConfigs::GetValueFor( const char* key, vector<int>& ints ) RelayConfigs::GetValueFor( const char* key, vector<int>& ints )
{ {
map<string,string>::const_iterator iter = m_values.find(key); char str[256];
bool found = iter != m_values.end(); bool found = GetValueFor( key, str, sizeof(str) );
const char* str = iter->second.c_str();
int len = strlen(str); int len = strlen(str);
char buf[len+1]; char buf[len+1];
strcpy( buf, str ); strcpy( buf, str );
@ -104,7 +107,7 @@ RelayConfigs::GetValueFor( const char* key, vector<int>& ints )
*end = '\0'; *end = '\0';
} }
logf( XW_LOGINFO, "adding %s to ports", port ); fprintf( stderr, "adding %s to ports\n", port );
ints.push_back( atoi(port) ); ints.push_back( atoi(port) );
if ( !end ) { if ( !end ) {
@ -120,6 +123,7 @@ RelayConfigs::GetValueFor( const char* key, vector<int>& ints )
void void
RelayConfigs::SetValueFor( const char* key, const char* value ) RelayConfigs::SetValueFor( const char* key, const char* value )
{ {
MutexLock ml( &m_values_mutex );
/* Does this leak in the case where we're replacing a value? */ /* Does this leak in the case where we're replacing a value? */
m_values.insert( pair<string,string>(string(key),string(value) ) ); m_values.insert( pair<string,string>(string(key),string(value) ) );
} }
@ -135,7 +139,7 @@ RelayConfigs::parse( const char* fname, ino_t prev )
if ( inode != prev ) { if ( inode != prev ) {
FILE* f = fopen( fname, "r" ); FILE* f = fopen( fname, "r" );
if ( f != NULL ) { if ( f != NULL ) {
logf( XW_LOGINFO, "config: reading from %s", fname ); fprintf( stderr, "config: reading from %s\n", fname );
char line[MAX_LINE]; char line[MAX_LINE];
for ( ; ; ) { for ( ; ; ) {

View file

@ -55,6 +55,7 @@ class RelayConfigs {
time_t m_allConnInterval; time_t m_allConnInterval;
map<string,string> m_values; map<string,string> m_values;
pthread_mutex_t m_values_mutex;
static RelayConfigs* instance; static RelayConfigs* instance;
}; };