summaryrefslogtreecommitdiff
path: root/src/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/opentracker.c')
-rw-r--r--src/opentracker.c50
1 files changed, 40 insertions, 10 deletions
diff --git a/src/opentracker.c b/src/opentracker.c
index 60a6d8c..81b4df6 100644
--- a/src/opentracker.c
+++ b/src/opentracker.c
@@ -7,6 +7,7 @@
7#include <errno.h> 7#include <errno.h>
8#include <limits.h> 8#include <limits.h>
9#include <pthread.h> 9#include <pthread.h>
10#include <grp.h>
10#include <pwd.h> 11#include <pwd.h>
11#include <signal.h> 12#include <signal.h>
12#include <stdint.h> 13#include <stdint.h>
@@ -24,6 +25,7 @@
24#include "ot_config.h" 25#include "ot_config.h"
25#include "ot_hex.h" 26#include "ot_hex.h"
26#include "ot_http.h" 27#include "ot_http.h"
28#include "ot_iovec.h"
27#include "ot_ip.h" 29#include "ot_ip.h"
28#include "ot_livesync.h" 30#include "ot_livesync.h"
29#include "ot_mutex.h" 31#include "ot_mutex.h"
@@ -106,6 +108,7 @@ static void *server_mainloop(void *args) {
106 struct iovec *iovector; 108 struct iovec *iovector;
107 size_t iovec_entries; 109 size_t iovec_entries;
108 int is_partial; 110 int is_partial;
111 uint32_t generation;
109 112
110 (void)args; 113 (void)args;
111 114
@@ -148,10 +151,15 @@ static void *server_mainloop(void *args) {
148 } 151 }
149 } 152 }
150 153
151 while ((sock = workqueue_popresult(&iovec_entries, &iovector, &is_partial)) != -1) 154 while ((sock = workqueue_popresult(&iovec_entries, &iovector, &is_partial, &generation)) != -1) {
155 if (fdstate_generation((int)sock) != generation) {
156 iovec_free(&iovec_entries, &iovector);
157 continue;
158 }
152 http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial); 159 http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial);
160 }
153 161
154 if (time_now_seconds() > next_timeout_check) { 162 if (time_is_past(next_timeout_check)) {
155 int cursor = 0; 163 int cursor = 0;
156 164
157 while ((sock = fdstate_next_expired(time_now_seconds(), &cursor)) != -1) 165 while ((sock = fdstate_next_expired(time_now_seconds(), &cursor)) != -1)
@@ -219,6 +227,16 @@ static void listen_bind_one(ot_listen *entry) {
219#endif 227#endif
220} 228}
221 229
230static void listen_warn_if_taken(const ot_listen *entry) {
231 char address[64];
232
233 if (entry->proto != FLAG_UDP || !socket_udp_is_taken(entry->ip, entry->port))
234 return;
235
236 address[ip_format(address, entry->ip)] = 0;
237 fprintf(stderr, "Warning: udp address [%s]:%d is already in use, by another tracker instance perhaps.\n", address, entry->port);
238}
239
222/* Bound in the order the entries were given. 240/* Bound in the order the entries were given.
223 Note: On macOS SO_REUSEPORT delivers everything to the socket that bound last. */ 241 Note: On macOS SO_REUSEPORT delivers everything to the socket that bound last. */
224static void listen_bind_all(void) { 242static void listen_bind_all(void) {
@@ -226,6 +244,9 @@ static void listen_bind_all(void) {
226 ot_listen *entries = config_listen_entries(&count); 244 ot_listen *entries = config_listen_entries(&count);
227 245
228 for (i = 0; i < count; ++i) 246 for (i = 0; i < count; ++i)
247 listen_warn_if_taken(entries + i);
248
249 for (i = 0; i < count; ++i)
229 listen_bind_one(entries + i); 250 listen_bind_one(entries + i);
230} 251}
231 252
@@ -300,9 +321,13 @@ void load_state(const char *const state_filename) {
300 FILE *state_filehandle; 321 FILE *state_filehandle;
301 char inbuf[512]; 322 char inbuf[512];
302 ot_hash infohash; 323 ot_hash infohash;
303 uint64_t base, downcount; 324 uint64_t touched, downcount;
304 int consumed; 325 int consumed;
305 326
327 /* The file records the minute a torrent was last touched, by the clock that
328 survives a restart */
329 uint64_t now = (uint64_t)time_wall_minutes();
330
306 state_filehandle = fopen(state_filename, "r"); 331 state_filehandle = fopen(state_filename, "r");
307 332
308 if (state_filehandle == NULL) { 333 if (state_filehandle == NULL) {
@@ -318,12 +343,12 @@ void load_state(const char *const state_filename) {
318 continue; 343 continue;
319 i = 2 * sizeof(ot_hash); 344 i = 2 * sizeof(ot_hash);
320 345
321 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &base, UINT64_MAX))) 346 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &touched, now)))
322 continue; 347 continue;
323 i += consumed; 348 i += consumed;
324 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &downcount, UINT64_MAX))) 349 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &downcount, SIZE_MAX)))
325 continue; 350 continue;
326 add_torrent_from_saved_state(infohash, base, downcount); 351 add_torrent_from_saved_state(infohash, (ot_time)(now - touched), (size_t)downcount);
327 } 352 }
328 353
329 fclose(state_filehandle); 354 fclose(state_filehandle);
@@ -356,10 +381,10 @@ int drop_privileges(const char *const serveruser, const char *const serverdir) {
356 /* If we can't find server user, revert to nobody's default uid */ 381 /* If we can't find server user, revert to nobody's default uid */
357 if (!pws) { 382 if (!pws) {
358 fprintf(stderr, "Warning: Could not get password entry for %s. Reverting to uid -2.\n", serveruser); 383 fprintf(stderr, "Warning: Could not get password entry for %s. Reverting to uid -2.\n", serveruser);
359 if (setegid((gid_t)-2) || setgid((gid_t)-2) || setuid((uid_t)-2) || seteuid((uid_t)-2)) 384 if (setgroups(0, NULL) || setegid((gid_t)-2) || setgid((gid_t)-2) || setuid((uid_t)-2) || seteuid((uid_t)-2))
360 panic("Could not set uid to value -2"); 385 panic("Could not set uid to value -2");
361 } else { 386 } else {
362 if (setegid(pws->pw_gid) || setgid(pws->pw_gid) || setuid(pws->pw_uid) || seteuid(pws->pw_uid)) 387 if (setgroups(0, NULL) || setegid(pws->pw_gid) || setgid(pws->pw_gid) || setuid(pws->pw_uid) || seteuid(pws->pw_uid))
363 panic("Could not set uid to specified value"); 388 panic("Could not set uid to specified value");
364 } 389 }
365 390
@@ -410,6 +435,10 @@ int main(int argc, char **argv) {
410 if (event_add(g_self_pipe[0], EVENT_IN, fdstate_open(g_self_pipe[0], (void *)FLAG_SELFPIPE, 0)) == -1) 435 if (event_add(g_self_pipe[0], EVENT_IN, fdstate_open(g_self_pipe[0], (void *)FLAG_SELFPIPE, 0)) == -1)
411 panic("selfpipe event_add"); 436 panic("selfpipe event_add");
412 437
438 /* Before any thread exists, so none of them runs with a signal still at its
439 default disposition */
440 install_signal_handlers();
441
413 /* Init all sub systems. This call may fail with an exit() */ 442 /* Init all sub systems. This call may fail with an exit() */
414 trackerlogic_init(); 443 trackerlogic_init();
415 444
@@ -422,13 +451,14 @@ int main(int argc, char **argv) {
422 if (g_statefile) 451 if (g_statefile)
423 load_state(g_statefile); 452 load_state(g_statefile);
424 453
425 install_signal_handlers(); 454 /* Nothing that adds a peer runs before the state file has been read */
426 455 livesync_init();
427 listen_start_tcp_workers(); 456 listen_start_tcp_workers();
428 listen_start_udp_workers(); 457 listen_start_udp_workers();
429 458
430 server_mainloop(0); 459 server_mainloop(0);
431 worker_stop_all(); 460 worker_stop_all();
461 livesync_deinit();
432 462
433 /* No thread is serving any more, so the teardown can run where a debugger 463 /* No thread is serving any more, so the teardown can run where a debugger
434 and the sanitizers can follow it */ 464 and the sanitizers can follow it */