Handle program end more politely

dynamic-accesslists
erdgeist 16 years ago
parent ad8c9ee1ef
commit c6947b160f

@ -34,9 +34,10 @@
#include "ot_livesync.h" #include "ot_livesync.h"
/* Globals */ /* Globals */
time_t g_now_seconds; time_t g_now_seconds;
char * g_redirecturl = NULL; char * g_redirecturl = NULL;
uint32_t g_tracker_id; uint32_t g_tracker_id;
volatile int g_opentracker_running = 1;
static char * g_serverdir = NULL; static char * g_serverdir = NULL;
@ -51,6 +52,7 @@ static void panic( const char *routine ) {
static void signal_handler( int s ) { static void signal_handler( int s ) {
if( s == SIGINT ) { if( s == SIGINT ) {
signal( SIGINT, SIG_IGN); signal( SIGINT, SIG_IGN);
g_opentracker_running = 0;
trackerlogic_deinit(); trackerlogic_deinit();
exit( 0 ); exit( 0 );

@ -23,6 +23,10 @@
char *g_accesslist_filename = NULL; char *g_accesslist_filename = NULL;
static ot_vector accesslist; static ot_vector accesslist;
static void access_list_deinit( void ) {
accesslist_reset( );
}
static void accesslist_reset( void ) { static void accesslist_reset( void ) {
free( accesslist.data ); free( accesslist.data );
byte_zero( &accesslist, sizeof( accesslist ) ); byte_zero( &accesslist, sizeof( accesslist ) );

@ -13,12 +13,14 @@
#if defined ( WANT_ACCESSLIST_BLACK ) || defined (WANT_ACCESSLIST_WHITE ) #if defined ( WANT_ACCESSLIST_BLACK ) || defined (WANT_ACCESSLIST_WHITE )
#define WANT_ACCESSLIST #define WANT_ACCESSLIST
void accesslist_init( ); void accesslist_init( );
void accesslist_deinit( );
int accesslist_hashisvalid( ot_hash *hash ); int accesslist_hashisvalid( ot_hash *hash );
extern char *g_accesslist_filename; extern char *g_accesslist_filename;
#else #else
#define accesslist_init( accesslist_filename ) #define accesslist_init( accesslist_filename )
#define accesslist_deinit( )
#define accesslist_hashisvalid( hash ) 1 #define accesslist_hashisvalid( hash ) 1
#endif #endif

@ -114,6 +114,8 @@ static void * clean_worker( void * args ) {
} }
} }
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
if( !g_opentracker_running )
return NULL;
usleep( OT_CLEAN_SLEEP ); usleep( OT_CLEAN_SLEEP );
} }
} }

@ -65,6 +65,8 @@ static void * fullscrape_worker( void * args ) {
fullscrape_make( &iovec_entries, &iovector, tasktype ); fullscrape_make( &iovec_entries, &iovector, tasktype );
if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) ) if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) )
iovec_free( &iovec_entries, &iovector ); iovec_free( &iovec_entries, &iovector );
if( !g_opentracker_running )
return NULL;
} }
return NULL; return NULL;
} }
@ -150,7 +152,7 @@ static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tas
/* Get exclusive access to that bucket */ /* Get exclusive access to that bucket */
ot_vector *torrents_list = mutex_bucket_lock( bucket ); ot_vector *torrents_list = mutex_bucket_lock( bucket );
size_t tor_offset; size_t tor_offset;
/* For each torrent in this bucket.. */ /* For each torrent in this bucket.. */
for( tor_offset=0; tor_offset<torrents_list->size; ++tor_offset ) { for( tor_offset=0; tor_offset<torrents_list->size; ++tor_offset ) {
/* Address torrents members */ /* Address torrents members */
@ -202,8 +204,12 @@ static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tas
IF_COMPRESSION( r = compress_buffer; ) IF_COMPRESSION( r = compress_buffer; )
} }
/* All torrents done: release lock on currenct bucket */ /* All torrents done: release lock on current bucket */
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
/* Parent thread died? */
if( !g_opentracker_running )
return;
} }
if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE ) if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE )

@ -8,6 +8,7 @@
#include <sys/uio.h> #include <sys/uio.h>
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h>
/* Libowfat */ /* Libowfat */
#include "socket.h" #include "socket.h"
@ -57,6 +58,11 @@ void livesync_init( ) {
} }
void livesync_deinit() { void livesync_deinit() {
if( g_livesync_socket_in != -1 )
close( g_livesync_socket_in );
if( g_livesync_socket_out != -1 )
close( g_livesync_socket_out );
pthread_cancel( thread_id ); pthread_cancel( thread_id );
} }
@ -147,6 +153,9 @@ static void * livesync_worker( void * args ) {
ot_peer *peer = (ot_peer*)(livesync_inbuffer + off + sizeof(ot_hash)); ot_peer *peer = (ot_peer*)(livesync_inbuffer + off + sizeof(ot_hash));
ot_hash *hash = (ot_hash*)(livesync_inbuffer + off); ot_hash *hash = (ot_hash*)(livesync_inbuffer + off);
if( !g_opentracker_running )
return NULL;
if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED ) if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED )
remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA); remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA);
else else

@ -62,6 +62,7 @@ void handle_livesync( const int64 serversocket );
/* If no syncing is required, save calling code from #ifdef /* If no syncing is required, save calling code from #ifdef
constructions */ constructions */
#define livesync_deinit()
#define livesync_init() #define livesync_init()
#define livesync_ticker() #define livesync_ticker()
#define handle_livesync(a) #define handle_livesync(a)

@ -188,6 +188,8 @@ size_t stats_top10_txt( char * reply ) {
} }
} }
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
if( !g_opentracker_running )
return 0;
} }
r += sprintf( r, "Top 10 torrents by peers:\n" ); r += sprintf( r, "Top 10 torrents by peers:\n" );
@ -250,6 +252,8 @@ static size_t stats_slash24s_txt( char * reply, size_t amount, uint32_t thresh )
} }
} }
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
if( !g_opentracker_running )
goto bailout_cleanup;
} }
#endif #endif
@ -381,6 +385,8 @@ static size_t stats_peers_mrtg( char * reply ) {
peer_count += peer_list->peer_count; seed_count += peer_list->seed_count; peer_count += peer_list->peer_count; seed_count += peer_list->seed_count;
} }
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
if( !g_opentracker_running )
return 0;
} }
return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker",
peer_count, peer_count,
@ -399,6 +405,8 @@ static size_t stats_startstop_mrtg( char * reply )
ot_vector *torrents_list = mutex_bucket_lock( bucket ); ot_vector *torrents_list = mutex_bucket_lock( bucket );
torrent_count += torrents_list->size; torrent_count += torrents_list->size;
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
if( !g_opentracker_running )
return 0;
} }
return sprintf( reply, "%zd\n%zd\nopentracker handling %zd torrents\nopentracker", return sprintf( reply, "%zd\n%zd\nopentracker handling %zd torrents\nopentracker",
@ -422,6 +430,8 @@ static size_t stats_toraddrem_mrtg( char * reply )
peer_count += peer_list->peer_count; peer_count += peer_list->peer_count;
} }
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
if( !g_opentracker_running )
return 0;
} }
return sprintf( reply, "%zd\n%zd\nopentracker handling %zd peers\nopentracker", return sprintf( reply, "%zd\n%zd\nopentracker handling %zd peers\nopentracker",

@ -376,13 +376,6 @@ void trackerlogic_deinit( void ) {
int bucket; int bucket;
size_t j; size_t j;
/* Deinitialise background worker threads */
stats_deinit( );
livesync_init( );
accesslist_init( );
fullscrape_deinit( );
clean_deinit( );
/* Free all torrents... */ /* Free all torrents... */
for(bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) { for(bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
ot_vector *torrents_list = mutex_bucket_lock( bucket ); ot_vector *torrents_list = mutex_bucket_lock( bucket );
@ -396,6 +389,12 @@ void trackerlogic_deinit( void ) {
mutex_bucket_unlock( bucket ); mutex_bucket_unlock( bucket );
} }
/* Deinitialise background worker threads */
stats_deinit( );
livesync_deinit( );
accesslist_deinit( );
fullscrape_deinit( );
clean_deinit( );
/* Release mutexes */ /* Release mutexes */
mutex_deinit( ); mutex_deinit( );
} }

@ -50,6 +50,7 @@ typedef time_t ot_time;
/* From opentracker.c */ /* From opentracker.c */
extern time_t g_now_seconds; extern time_t g_now_seconds;
extern volatile int g_opentracker_running;
#define g_now_minutes (g_now_seconds/60) #define g_now_minutes (g_now_seconds/60)
extern uint32_t g_tracker_id; extern uint32_t g_tracker_id;

Loading…
Cancel
Save