diff options
Diffstat (limited to 'opentracker.c')
-rw-r--r-- | opentracker.c | 42 |
1 files changed, 12 insertions, 30 deletions
diff --git a/opentracker.c b/opentracker.c index f76c042..f8587c9 100644 --- a/opentracker.c +++ b/opentracker.c | |||
@@ -5,42 +5,36 @@ | |||
5 | $Id$ */ | 5 | $Id$ */ |
6 | 6 | ||
7 | /* System */ | 7 | /* System */ |
8 | #include <stdlib.h> | ||
8 | #include <string.h> | 9 | #include <string.h> |
9 | #include <sys/types.h> | ||
10 | #include <sys/socket.h> | ||
11 | #include <arpa/inet.h> | 10 | #include <arpa/inet.h> |
11 | #include <sys/socket.h> | ||
12 | #include <unistd.h> | 12 | #include <unistd.h> |
13 | #include <stdlib.h> | ||
14 | #include <errno.h> | 13 | #include <errno.h> |
15 | #include <signal.h> | 14 | #include <signal.h> |
16 | #include <stdio.h> | 15 | #include <stdio.h> |
17 | #include <pwd.h> | 16 | #include <pwd.h> |
18 | #include <ctype.h> | 17 | #include <ctype.h> |
19 | #include <arpa/inet.h> | ||
20 | 18 | ||
21 | /* Libowfat */ | 19 | /* Libowfat */ |
22 | #include "socket.h" | 20 | #include "socket.h" |
23 | #include "io.h" | 21 | #include "io.h" |
24 | #include "iob.h" | 22 | #include "iob.h" |
25 | #include "array.h" | ||
26 | #include "byte.h" | 23 | #include "byte.h" |
27 | #include "fmt.h" | ||
28 | #include "scan.h" | 24 | #include "scan.h" |
29 | #include "ip4.h" | 25 | #include "ip4.h" |
30 | 26 | ||
31 | /* Opentracker */ | 27 | /* Opentracker */ |
32 | #include "trackerlogic.h" | 28 | #include "trackerlogic.h" |
33 | #include "ot_iovec.h" | ||
34 | #include "ot_mutex.h" | 29 | #include "ot_mutex.h" |
35 | #include "ot_http.h" | 30 | #include "ot_http.h" |
36 | #include "ot_udp.h" | 31 | #include "ot_udp.h" |
37 | #include "ot_clean.h" | ||
38 | #include "ot_accesslist.h" | 32 | #include "ot_accesslist.h" |
39 | #include "ot_stats.h" | 33 | #include "ot_stats.h" |
40 | #include "ot_livesync.h" | 34 | #include "ot_livesync.h" |
41 | 35 | ||
42 | /* Globals */ | 36 | /* Globals */ |
43 | time_t g_now; | 37 | time_t g_now_seconds; |
44 | char * g_redirecturl = NULL; | 38 | char * g_redirecturl = NULL; |
45 | uint32_t g_tracker_id; | 39 | uint32_t g_tracker_id; |
46 | 40 | ||
@@ -61,7 +55,7 @@ static void signal_handler( int s ) { | |||
61 | trackerlogic_deinit(); | 55 | trackerlogic_deinit(); |
62 | exit( 0 ); | 56 | exit( 0 ); |
63 | } else if( s == SIGALRM ) { | 57 | } else if( s == SIGALRM ) { |
64 | g_now = time(NULL); | 58 | g_now_seconds = time(NULL); |
65 | alarm(5); | 59 | alarm(5); |
66 | } | 60 | } |
67 | } | 61 | } |
@@ -135,7 +129,7 @@ static ssize_t handle_read( const int64 clientsocket ) { | |||
135 | if( array_failed( &h->request ) ) | 129 | if( array_failed( &h->request ) ) |
136 | return http_issue_error( clientsocket, CODE_HTTPERROR_500 ); | 130 | return http_issue_error( clientsocket, CODE_HTTPERROR_500 ); |
137 | 131 | ||
138 | if( ( array_bytes( &h->request ) > 8192 ) && !accesslist_isblessed( (char*)&h->ip, OT_PERMISSION_MAY_SYNC ) ) | 132 | if( array_bytes( &h->request ) > 8192 ) |
139 | return http_issue_error( clientsocket, CODE_HTTPERROR_500 ); | 133 | return http_issue_error( clientsocket, CODE_HTTPERROR_500 ); |
140 | 134 | ||
141 | if( memchr( array_start( &h->request ), '\n', array_bytes( &h->request ) ) ) | 135 | if( memchr( array_start( &h->request ), '\n', array_bytes( &h->request ) ) ) |
@@ -178,7 +172,7 @@ static void handle_accept( const int64 serversocket ) { | |||
178 | /* That breaks taia encapsulation. But there is no way to take system | 172 | /* That breaks taia encapsulation. But there is no way to take system |
179 | time this often in FreeBSD and libowfat does not allow to set unix time */ | 173 | time this often in FreeBSD and libowfat does not allow to set unix time */ |
180 | taia_uint( &t, 0 ); /* Clear t */ | 174 | taia_uint( &t, 0 ); /* Clear t */ |
181 | tai_unix( &(t.sec), (g_now + OT_CLIENT_TIMEOUT) ); | 175 | tai_unix( &(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT) ); |
182 | io_timeout( i, t ); | 176 | io_timeout( i, t ); |
183 | } | 177 | } |
184 | 178 | ||
@@ -187,8 +181,7 @@ static void handle_accept( const int64 serversocket ) { | |||
187 | } | 181 | } |
188 | 182 | ||
189 | static void server_mainloop( ) { | 183 | static void server_mainloop( ) { |
190 | static time_t ot_last_clean_time; | 184 | time_t next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL; |
191 | time_t next_timeout_check = g_now + OT_CLIENT_TIMEOUT_CHECKINTERVAL; | ||
192 | struct iovec *iovector; | 185 | struct iovec *iovector; |
193 | int iovec_entries; | 186 | int iovec_entries; |
194 | 187 | ||
@@ -213,20 +206,14 @@ static void server_mainloop( ) { | |||
213 | while( ( i = io_canwrite( ) ) != -1 ) | 206 | while( ( i = io_canwrite( ) ) != -1 ) |
214 | handle_write( i ); | 207 | handle_write( i ); |
215 | 208 | ||
216 | if( g_now > next_timeout_check ) { | 209 | if( g_now_seconds > next_timeout_check ) { |
217 | while( ( i = io_timeouted() ) != -1 ) | 210 | while( ( i = io_timeouted() ) != -1 ) |
218 | handle_dead( i ); | 211 | handle_dead( i ); |
219 | next_timeout_check = g_now + OT_CLIENT_TIMEOUT_CHECKINTERVAL; | 212 | next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL; |
220 | } | 213 | } |
221 | 214 | ||
222 | livesync_ticker(); | 215 | livesync_ticker(); |
223 | 216 | ||
224 | /* See if we need to move our pools */ | ||
225 | if( NOW != ot_last_clean_time ) { | ||
226 | ot_last_clean_time = NOW; | ||
227 | clean_all_torrents(); | ||
228 | } | ||
229 | |||
230 | /* Enforce setting the clock */ | 217 | /* Enforce setting the clock */ |
231 | signal_handler( SIGALRM ); | 218 | signal_handler( SIGALRM ); |
232 | } | 219 | } |
@@ -266,7 +253,7 @@ char * set_config_option( char **option, char *value ) { | |||
266 | fprintf( stderr, "Setting config option: %s\n", value ); | 253 | fprintf( stderr, "Setting config option: %s\n", value ); |
267 | #endif | 254 | #endif |
268 | while( isspace(*value) ) ++value; | 255 | while( isspace(*value) ) ++value; |
269 | if( *option ) free( *option ); | 256 | free( *option ); |
270 | return *option = strdup( value ); | 257 | return *option = strdup( value ); |
271 | } | 258 | } |
272 | 259 | ||
@@ -342,11 +329,6 @@ int parse_configfile( char * config_filename ) { | |||
342 | #endif | 329 | #endif |
343 | } else if(!byte_diff(p, 20, "tracker.redirect_url" ) && isspace(p[20])) { | 330 | } else if(!byte_diff(p, 20, "tracker.redirect_url" ) && isspace(p[20])) { |
344 | set_config_option( &g_redirecturl, p+21 ); | 331 | set_config_option( &g_redirecturl, p+21 ); |
345 | #ifdef WANT_SYNC_BATCH | ||
346 | } else if(!byte_diff(p, 26, "batchsync.cluster.admin_ip" ) && isspace(p[26])) { | ||
347 | if(!scan_ip4( p+27, tmpip )) goto parse_error; | ||
348 | accesslist_blessip( tmpip, OT_PERMISSION_MAY_SYNC ); | ||
349 | #endif | ||
350 | #ifdef WANT_SYNC_LIVE | 332 | #ifdef WANT_SYNC_LIVE |
351 | } else if(!byte_diff(p, 24, "livesync.cluster.node_ip" ) && isspace(p[24])) { | 333 | } else if(!byte_diff(p, 24, "livesync.cluster.node_ip" ) && isspace(p[24])) { |
352 | if( !scan_ip4( p+25, tmpip )) goto parse_error; | 334 | if( !scan_ip4( p+25, tmpip )) goto parse_error; |
@@ -408,7 +390,7 @@ while( scanon ) { | |||
408 | break; | 390 | break; |
409 | case 'f': bound += parse_configfile( optarg ); break; | 391 | case 'f': bound += parse_configfile( optarg ); break; |
410 | case 'h': help( argv[0] ); exit( 0 ); | 392 | case 'h': help( argv[0] ); exit( 0 ); |
411 | case 'v': write( 2, static_inbuf, stats_return_tracker_version( static_inbuf )); exit( 0 ); | 393 | case 'v': stats_return_tracker_version( static_inbuf ); fputs( static_inbuf, stderr ); exit( 0 ); |
412 | default: | 394 | default: |
413 | case '?': usage( argv[0] ); exit( 1 ); | 395 | case '?': usage( argv[0] ); exit( 1 ); |
414 | } | 396 | } |
@@ -435,7 +417,7 @@ while( scanon ) { | |||
435 | signal( SIGINT, signal_handler ); | 417 | signal( SIGINT, signal_handler ); |
436 | signal( SIGALRM, signal_handler ); | 418 | signal( SIGALRM, signal_handler ); |
437 | 419 | ||
438 | g_now = time( NULL ); | 420 | g_now_seconds = time( NULL ); |
439 | 421 | ||
440 | if( trackerlogic_init( g_serverdir ? g_serverdir : "." ) == -1 ) | 422 | if( trackerlogic_init( g_serverdir ? g_serverdir : "." ) == -1 ) |
441 | panic( "Logic not started" ); | 423 | panic( "Logic not started" ); |