From c6947b160f99278057df0770b849b46264b72229 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Sun, 7 Dec 2008 03:50:51 +0000 Subject: [PATCH] Handle program end more politely --- opentracker.c | 8 +++++--- ot_accesslist.c | 4 ++++ ot_accesslist.h | 2 ++ ot_clean.c | 2 ++ ot_fullscrape.c | 10 ++++++++-- ot_livesync.c | 9 +++++++++ ot_livesync.h | 1 + ot_stats.c | 10 ++++++++++ trackerlogic.c | 13 ++++++------- trackerlogic.h | 1 + 10 files changed, 48 insertions(+), 12 deletions(-) diff --git a/opentracker.c b/opentracker.c index 022f561..1e996d9 100644 --- a/opentracker.c +++ b/opentracker.c @@ -34,9 +34,10 @@ #include "ot_livesync.h" /* Globals */ -time_t g_now_seconds; -char * g_redirecturl = NULL; -uint32_t g_tracker_id; +time_t g_now_seconds; +char * g_redirecturl = NULL; +uint32_t g_tracker_id; +volatile int g_opentracker_running = 1; static char * g_serverdir = NULL; @@ -51,6 +52,7 @@ static void panic( const char *routine ) { static void signal_handler( int s ) { if( s == SIGINT ) { signal( SIGINT, SIG_IGN); + g_opentracker_running = 0; trackerlogic_deinit(); exit( 0 ); diff --git a/ot_accesslist.c b/ot_accesslist.c index f4c21c5..3b8e8a9 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -23,6 +23,10 @@ char *g_accesslist_filename = NULL; static ot_vector accesslist; +static void access_list_deinit( void ) { + accesslist_reset( ); +} + static void accesslist_reset( void ) { free( accesslist.data ); byte_zero( &accesslist, sizeof( accesslist ) ); diff --git a/ot_accesslist.h b/ot_accesslist.h index 9c93187..15a21f2 100644 --- a/ot_accesslist.h +++ b/ot_accesslist.h @@ -13,12 +13,14 @@ #if defined ( WANT_ACCESSLIST_BLACK ) || defined (WANT_ACCESSLIST_WHITE ) #define WANT_ACCESSLIST void accesslist_init( ); +void accesslist_deinit( ); int accesslist_hashisvalid( ot_hash *hash ); extern char *g_accesslist_filename; #else #define accesslist_init( accesslist_filename ) +#define accesslist_deinit( ) #define accesslist_hashisvalid( hash ) 1 #endif diff --git a/ot_clean.c b/ot_clean.c index 2173c27..4adeee6 100644 --- a/ot_clean.c +++ b/ot_clean.c @@ -114,6 +114,8 @@ static void * clean_worker( void * args ) { } } mutex_bucket_unlock( bucket ); + if( !g_opentracker_running ) + return NULL; usleep( OT_CLEAN_SLEEP ); } } diff --git a/ot_fullscrape.c b/ot_fullscrape.c index 5f61afd..9a6effb 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c @@ -65,6 +65,8 @@ static void * fullscrape_worker( void * args ) { fullscrape_make( &iovec_entries, &iovector, tasktype ); if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) ) iovec_free( &iovec_entries, &iovector ); + if( !g_opentracker_running ) + 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 */ ot_vector *torrents_list = mutex_bucket_lock( bucket ); size_t tor_offset; - + /* For each torrent in this bucket.. */ for( tor_offset=0; tor_offsetsize; ++tor_offset ) { /* Address torrents members */ @@ -202,8 +204,12 @@ static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tas 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 ); + + /* Parent thread died? */ + if( !g_opentracker_running ) + return; } if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE ) diff --git a/ot_livesync.c b/ot_livesync.c index b447e83..f61f0ec 100644 --- a/ot_livesync.c +++ b/ot_livesync.c @@ -8,6 +8,7 @@ #include #include #include +#include /* Libowfat */ #include "socket.h" @@ -57,6 +58,11 @@ void livesync_init( ) { } 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 ); } @@ -147,6 +153,9 @@ static void * livesync_worker( void * args ) { ot_peer *peer = (ot_peer*)(livesync_inbuffer + off + sizeof(ot_hash)); ot_hash *hash = (ot_hash*)(livesync_inbuffer + off); + if( !g_opentracker_running ) + return NULL; + if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED ) remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA); else diff --git a/ot_livesync.h b/ot_livesync.h index c534f59..153dbac 100644 --- a/ot_livesync.h +++ b/ot_livesync.h @@ -62,6 +62,7 @@ void handle_livesync( const int64 serversocket ); /* If no syncing is required, save calling code from #ifdef constructions */ +#define livesync_deinit() #define livesync_init() #define livesync_ticker() #define handle_livesync(a) diff --git a/ot_stats.c b/ot_stats.c index f456d46..a4596c6 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -188,6 +188,8 @@ size_t stats_top10_txt( char * reply ) { } } mutex_bucket_unlock( bucket ); + if( !g_opentracker_running ) + return 0; } 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 ); + if( !g_opentracker_running ) + goto bailout_cleanup; } #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; } mutex_bucket_unlock( bucket ); + if( !g_opentracker_running ) + return 0; } return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", peer_count, @@ -399,6 +405,8 @@ static size_t stats_startstop_mrtg( char * reply ) ot_vector *torrents_list = mutex_bucket_lock( bucket ); torrent_count += torrents_list->size; mutex_bucket_unlock( bucket ); + if( !g_opentracker_running ) + return 0; } 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; } mutex_bucket_unlock( bucket ); + if( !g_opentracker_running ) + return 0; } return sprintf( reply, "%zd\n%zd\nopentracker handling %zd peers\nopentracker", diff --git a/trackerlogic.c b/trackerlogic.c index d7fca69..9366cd1 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -376,13 +376,6 @@ void trackerlogic_deinit( void ) { int bucket; size_t j; - /* Deinitialise background worker threads */ - stats_deinit( ); - livesync_init( ); - accesslist_init( ); - fullscrape_deinit( ); - clean_deinit( ); - /* Free all torrents... */ for(bucket=0; bucket