summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
authorerdgeist <>2007-10-13 17:40:37 +0000
committererdgeist <>2007-10-13 17:40:37 +0000
commit6c1adb8fc8c135cf0c3017e5ca6454747b0153e4 (patch)
tree7e02a9b7ddb3b37ab443ebf65ddd879d59b75bff /opentracker.c
parent4c15dc7a70b189a7ae2cc66196c42f26881eb85e (diff)
since gettimeofday is rather expansive, we do only fetch time once in a 5 second period, when we are delivered a SIGALRM.
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c32
1 files changed, 16 insertions, 16 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