summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/opentracker.c2
-rw-r--r--src/ot_livesync.c12
-rw-r--r--src/ot_worker.c2
-rw-r--r--src/trackerlogic.c6
-rw-r--r--src/trackerlogic.h2
-rw-r--r--tests/integration/test_livesync.py11
-rw-r--r--tests/t_trackerlogic.c2
7 files changed, 27 insertions, 10 deletions
diff --git a/src/opentracker.c b/src/opentracker.c
index 14b817e..60a6d8c 100644
--- a/src/opentracker.c
+++ b/src/opentracker.c
@@ -109,7 +109,7 @@ static void *server_mainloop(void *args) {
109 109
110 (void)args; 110 (void)args;
111 111
112 if (workstruct_init(&ws) == -1) 112 if (workstruct_init(&ws, G_INBUF_SIZE) == -1)
113 panic("Initializing worker failed"); 113 panic("Initializing worker failed");
114 114
115 while (g_opentracker_running) { 115 while (g_opentracker_running) {
diff --git a/src/ot_livesync.c b/src/ot_livesync.c
index 083fe1a..18dae0d 100644
--- a/src/ot_livesync.c
+++ b/src/ot_livesync.c
@@ -4,6 +4,7 @@
4/* System */ 4/* System */
5#include <arpa/inet.h> 5#include <arpa/inet.h>
6#include <pthread.h> 6#include <pthread.h>
7#include <stdio.h>
7#include <stdlib.h> 8#include <stdlib.h>
8#include <string.h> 9#include <string.h>
9#include <sys/socket.h> 10#include <sys/socket.h>
@@ -200,9 +201,12 @@ static void *livesync_worker(void *args) {
200 201
201 (void)args; 202 (void)args;
202 203
203 /* Initialize our "thread local storage" */ 204 if (workstruct_init(&ws, LIVESYNC_INCOMING_BUFFSIZE) == -1) {
204 ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); 205 fprintf(stderr, "Warning: live sync could not get its buffers and will not run.\n");
205 ws.outbuf = ws.reply = 0; 206 atomic_store(&thread_left, 1);
207 return NULL;
208 }
209 ws.request = ws.inbuf;
206 210
207 while (g_opentracker_running) { 211 while (g_opentracker_running) {
208 uint32_t packet_type; 212 uint32_t packet_type;
@@ -233,7 +237,7 @@ static void *livesync_worker(void *args) {
233 } 237 }
234 } 238 }
235 239
236 free(ws.inbuf); 240 workstruct_deinit(&ws);
237 atomic_store(&thread_left, 1); 241 atomic_store(&thread_left, 1);
238 return NULL; 242 return NULL;
239} 243}
diff --git a/src/ot_worker.c b/src/ot_worker.c
index 432d272..ef50945 100644
--- a/src/ot_worker.c
+++ b/src/ot_worker.c
@@ -20,7 +20,7 @@ static size_t g_worker_count;
20static void *worker_trampoline(void *args) { 20static void *worker_trampoline(void *args) {
21 struct ot_worker *me = args; 21 struct ot_worker *me = args;
22 22
23 if (workstruct_init(&me->ws) == -1) 23 if (workstruct_init(&me->ws, G_INBUF_SIZE) == -1)
24 fprintf(stderr, "Warning: a worker could not get its buffers and will not run.\n"); 24 fprintf(stderr, "Warning: a worker could not get its buffers and will not run.\n");
25 else { 25 else {
26 me->run(me); 26 me->run(me);
diff --git a/src/trackerlogic.c b/src/trackerlogic.c
index 9565090..8e30a48 100644
--- a/src/trackerlogic.c
+++ b/src/trackerlogic.c
@@ -567,9 +567,11 @@ void trackerlogic_init(void) {
567 stats_init(); 567 stats_init();
568} 568}
569 569
570int workstruct_init(struct ot_workstruct *ws) { 570/* Live sync reads whole packets rather than requests and wants a larger
571 inbuf than the request path does, hence the size. */
572int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size) {
571 memset(ws, 0, sizeof(*ws)); 573 memset(ws, 0, sizeof(*ws));
572 ws->inbuf = malloc(G_INBUF_SIZE); 574 ws->inbuf = malloc(inbuf_size);
573 ws->outbuf = malloc(G_OUTBUF_SIZE); 575 ws->outbuf = malloc(G_OUTBUF_SIZE);
574#ifdef _DEBUG_HTTPERROR 576#ifdef _DEBUG_HTTPERROR
575 ws->debugbuf = malloc(G_DEBUGBUF_SIZE); 577 ws->debugbuf = malloc(G_DEBUGBUF_SIZE);
diff --git a/src/trackerlogic.h b/src/trackerlogic.h
index 7292d36..692b6af 100644
--- a/src/trackerlogic.h
+++ b/src/trackerlogic.h
@@ -242,7 +242,7 @@ struct ot_workstruct {
242/* Everything a thread that serves requests needs for itself. Set up and torn 242/* Everything a thread that serves requests needs for itself. Set up and torn
243 down in one place so that a new kind of worker cannot miss a piece. 243 down in one place so that a new kind of worker cannot miss a piece.
244 Returns -1 if the buffers could not be had. */ 244 Returns -1 if the buffers could not be had. */
245int workstruct_init(struct ot_workstruct *ws); 245int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size);
246void workstruct_deinit(struct ot_workstruct *ws); 246void workstruct_deinit(struct ot_workstruct *ws);
247 247
248/* 248/*
diff --git a/tests/integration/test_livesync.py b/tests/integration/test_livesync.py
index 8dd754b..1659a41 100644
--- a/tests/integration/test_livesync.py
+++ b/tests/integration/test_livesync.py
@@ -102,6 +102,17 @@ class LiveSyncIn(TrackerTestCase):
102 self.tracker.livesync_send(packet(99, [(info_hash, peer4("10.1.2.11", 6881, FLAG_SEEDING))])) 102 self.tracker.livesync_send(packet(99, [(info_hash, peer4("10.1.2.11", 6881, FLAG_SEEDING))]))
103 self.assertFalse(self.tracker.wait_until(lambda: self.counts(info_hash) is not None, timeout=1)) 103 self.assertFalse(self.tracker.wait_until(lambda: self.counts(info_hash) is not None, timeout=1))
104 104
105 def test_synced_peers_are_counted(self):
106 """The live sync worker keeps its own tally like every other worker,
107 and ?mode=syncs is the sum of them."""
108 before = int(self.tracker.stats("syncs").split()[0])
109 hashes = [self.hash_for("sync-count-%d" % i) for i in range(3)]
110 self.tracker.livesync_send(packet(SYNC_PEER4, [(h, peer4("10.1.2.%d" % (20 + i), 6881, FLAG_SEEDING))
111 for i, h in enumerate(hashes)]))
112 self.assertTrue(self.tracker.wait_until(
113 lambda: int(self.tracker.stats("syncs").split()[0]) == before + 3),
114 "syncs says %s" % self.tracker.stats("syncs").split()[0])
115
105 def test_a_packet_with_nothing_but_a_header_is_ignored(self): 116 def test_a_packet_with_nothing_but_a_header_is_ignored(self):
106 before = self.tracker.torrent_count() 117 before = self.tracker.torrent_count()
107 self.tracker.livesync_send(struct.pack("!I", 0xDEADBEEF) + struct.pack("!I", SYNC_PEER4)) 118 self.tracker.livesync_send(struct.pack("!I", 0xDEADBEEF) + struct.pack("!I", SYNC_PEER4))
diff --git a/tests/t_trackerlogic.c b/tests/t_trackerlogic.c
index f64977f..1cf9141 100644
--- a/tests/t_trackerlogic.c
+++ b/tests/t_trackerlogic.c
@@ -224,7 +224,7 @@ void test_trackerlogic(void) {
224 pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); 224 pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
225 225
226 trackerlogic_init(); 226 trackerlogic_init();
227 CHECK_INT(workstruct_init(&ws), 0); 227 CHECK_INT(workstruct_init(&ws, G_INBUF_SIZE), 0);
228 228
229 case_an_announce_makes_a_torrent(); 229 case_an_announce_makes_a_torrent();
230 case_the_same_peer_twice_is_one_peer(); 230 case_the_same_peer_twice_is_one_peer();