Browse Source

Reloading accesslists left a wide window for race conditions.

dynamic-accesslists
erdgeist 16 years ago
parent
commit
a9c25b9fed
  1. 22
      ot_accesslist.c

22
ot_accesslist.c

@ -34,9 +34,9 @@ void accesslist_deinit( void ) {
accesslist_reset( ); accesslist_reset( );
} }
static int accesslist_addentry( ot_hash infohash ) { static int accesslist_addentry( ot_vector *al, ot_hash infohash ) {
int eger; int eger;
void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &eger ); void *insert = vector_find_or_insert( al, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &eger );
if( !insert ) if( !insert )
return -1; return -1;
@ -50,20 +50,22 @@ static int accesslist_addentry( ot_hash infohash ) {
static void accesslist_readfile( int sig ) { static void accesslist_readfile( int sig ) {
FILE * accesslist_filehandle; FILE * accesslist_filehandle;
ot_hash infohash; ot_hash infohash;
ot_vector accesslist_tmp;
void *olddata = accesslist.data;
char inbuf[512]; char inbuf[512];
if( sig != SIGHUP ) return; if( sig != SIGHUP ) return;
accesslist_filehandle = fopen( g_accesslist_filename, "r" ); accesslist_filehandle = fopen( g_accesslist_filename, "r" );
/* Free accesslist vector in trackerlogic.c*/
accesslist_reset();
if( accesslist_filehandle == NULL ) { if( accesslist_filehandle == NULL ) {
fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", g_accesslist_filename ); fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", g_accesslist_filename );
return; return;
} }
/* Initialise an empty accesslist vector */
memset( &accesslist_tmp, 0, sizeof(accesslist_tmp));
/* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
while( fgets( inbuf, sizeof(inbuf), accesslist_filehandle ) ) { while( fgets( inbuf, sizeof(inbuf), accesslist_filehandle ) ) {
int i; int i;
@ -77,10 +79,18 @@ static void accesslist_readfile( int sig ) {
continue; continue;
/* Append accesslist to accesslist vector */ /* Append accesslist to accesslist vector */
accesslist_addentry( infohash ); accesslist_addentry( &accesslist_tmp, infohash );
} }
#ifdef _DEBUG
fprintf( stderr, "Added %zd info_hashes to accesslist\n", accesslist_tmp.size );
#endif
fclose( accesslist_filehandle ); fclose( accesslist_filehandle );
/* Now exchange the accesslist vector in the least race condition prone way */
accesslist.size = 0;
memcpy( &accesslist, &accesslist_tmp, sizeof( &accesslist_tmp ));
free( olddata );
} }
int accesslist_hashisvalid( ot_hash hash ) { int accesslist_hashisvalid( ot_hash hash ) {

Loading…
Cancel
Save