summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c32
-rw-r--r--trackerlogic.h5
2 files changed, 20 insertions, 17 deletions
diff --git a/opentracker.c b/opentracker.c
index ad6dad4..ce33eb6 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -38,6 +38,7 @@ static time_t ot_start_time;
38static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; 38static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80;
39static const size_t SUCCESS_HTTP_SIZE_OFF = 17; 39static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
40static char g_adminip[4] = {0,0,0,0}; 40static char g_adminip[4] = {0,0,0,0};
41time_t g_now;
41 42
42#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER ) 43#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER )
43 #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive. 44 #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive.
@@ -93,7 +94,7 @@ static void help( char *name );
93 94
94static void carp( const char *routine ); 95static void carp( const char *routine );
95static void panic( const char *routine ); 96static void panic( const char *routine );
96static void graceful( int s ); 97static void signal_handler( int s );
97 98
98#define HTTPERROR_400 return httperror( s, "400 Invalid Request", "This server only understands GET." ) 99#define HTTPERROR_400 return httperror( s, "400 Invalid Request", "This server only understands GET." )
99#define HTTPERROR_400_PARAM return httperror( s, "400 Invalid Request", "Invalid parameter" ) 100#define HTTPERROR_400_PARAM return httperror( s, "400 Invalid Request", "Invalid parameter" )
@@ -503,11 +504,14 @@ ANNOUNCE_WORKAROUND:
503 senddata( s, static_outbuf + reply_off, reply_size ); 504 senddata( s, static_outbuf + reply_off, reply_size );
504} 505}
505 506
506static void graceful( int s ) { 507static void signal_handler( int s ) {
507 if( s == SIGINT ) { 508 if( s == SIGINT ) {
508 signal( SIGINT, SIG_IGN); 509 signal( SIGINT, SIG_IGN);
509 deinit_logic(); 510 deinit_logic();
510 exit( 0 ); 511 exit( 0 );
512 } else if( s == SIGALRM ) {
513 g_now = time(NULL);
514 alarm(5);
511 } 515 }
512} 516}
513 517
@@ -612,8 +616,7 @@ static void handle_accept( const int64 serversocket ) {
612 616
613 ++ot_overall_tcp_connections; 617 ++ot_overall_tcp_connections;
614 618
615 taia_now( &t ); 619 taia_uint( &t, (unsigned int)(g_now + OT_CLIENT_TIMEOUT) );
616 taia_addsec( &t, &t, OT_CLIENT_TIMEOUT );
617 io_timeout( i, t ); 620 io_timeout( i, t );
618 } 621 }
619 622
@@ -722,15 +725,13 @@ static void handle_udp4( int64 serversocket ) {
722} 725}
723 726
724static void server_mainloop( ) { 727static void server_mainloop( ) {
725 tai6464 t, next_timeout_check; 728 time_t next_timeout_check = g_now + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
726
727 taia_now( &next_timeout_check );
728 729
729 for( ; ; ) { 730 for( ; ; ) {
731 tai6464 t;
730 int64 i; 732 int64 i;
731 733
732 taia_now( &t ); 734 taia_uint( &t, (unsigned int)(g_now + OT_CLIENT_TIMEOUT_CHECKINTERVAL) );
733 taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_CHECKINTERVAL );
734 io_waituntil( t ); 735 io_waituntil( t );
735 736
736 while( ( i = io_canread( ) ) != -1 ) { 737 while( ( i = io_canread( ) ) != -1 ) {
@@ -746,11 +747,9 @@ static void server_mainloop( ) {
746 while( ( i = io_canwrite( ) ) != -1 ) 747 while( ( i = io_canwrite( ) ) != -1 )
747 handle_write( i ); 748 handle_write( i );
748 749
749 taia_now( &t ); 750 if( g_now > next_timeout_check ) {
750 if( taia_less( &next_timeout_check, &t ) ) {
751 handle_timeouted( ); 751 handle_timeouted( );
752 taia_now( &next_timeout_check ); 752 next_timeout_check = g_now + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
753 taia_addsec( &next_timeout_check, &next_timeout_check, OT_CLIENT_TIMEOUT_CHECKINTERVAL);
754 } 753 }
755 754
756 /* See if we need to move our pools */ 755 /* See if we need to move our pools */
@@ -874,13 +873,14 @@ int main( int argc, char **argv ) {
874#endif 873#endif
875 874
876 signal( SIGPIPE, SIG_IGN ); 875 signal( SIGPIPE, SIG_IGN );
877 signal( SIGINT, graceful ); 876 signal( SIGINT, signal_handler );
878 signal( SIGALRM, SIG_IGN ); 877 signal( SIGALRM, signal_handler );
879 878
880 if( init_logic( serverdir ) == -1 ) 879 if( init_logic( serverdir ) == -1 )
881 panic( "Logic not started" ); 880 panic( "Logic not started" );
882 881
883 ot_start_time = time( NULL ); 882 g_now = ot_start_time = time( NULL );
883 alarm(5);
884 884
885 server_mainloop( ); 885 server_mainloop( );
886 886
diff --git a/trackerlogic.h b/trackerlogic.h
index 37e2451..596324e 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -7,6 +7,7 @@
7#include <sys/types.h> 7#include <sys/types.h>
8#include <sys/time.h> 8#include <sys/time.h>
9#include <time.h> 9#include <time.h>
10#include <stdint.h>
10 11
11/* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */ 12/* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */
12/* They mark memory used as data instead of integer or human readable string - 13/* They mark memory used as data instead of integer or human readable string -
@@ -39,7 +40,9 @@ typedef time_t ot_time;
39 40
40#define OT_POOLS_COUNT 9 41#define OT_POOLS_COUNT 9
41#define OT_POOLS_TIMEOUT (60*5) 42#define OT_POOLS_TIMEOUT (60*5)
42#define NOW (time(NULL)/OT_POOLS_TIMEOUT) 43
44extern time_t g_now;
45#define NOW (g_now/OT_POOLS_TIMEOUT)
43 46
44#define OT_VECTOR_MIN_MEMBERS 16 47#define OT_VECTOR_MIN_MEMBERS 16
45#define OT_VECTOR_GROW_RATIO 4 48#define OT_VECTOR_GROW_RATIO 4