From 58f76d9e91c8c29404d7990a1a876a8d61ce9448 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Wed, 28 Oct 2026 23:32:51 +0100 Subject: Livesync now also gets proper deinit code --- ot_livesync.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'ot_livesync.c') diff --git a/ot_livesync.c b/ot_livesync.c index 45271bc..e6db720 100644 --- a/ot_livesync.c +++ b/ot_livesync.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,9 @@ ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5}; #define LIVESYNC_MAXDELAY 15 /* seconds */ +/* How long the worker may sit in a read before it looks at the running flag */ +#define LIVESYNC_WORKER_PATIENCE 1 /* seconds */ + enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; /* Forward declaration */ @@ -53,6 +57,7 @@ static sync_buffer g_v6_buf; static sync_buffer g_v4_buf; static pthread_t thread_id; +static int thread_started; void livesync_init(void) { uint32_t packet_type6 = htonl(OT_SYNC_PEER6); uint32_t packet_type4 = htonl(OT_SYNC_PEER4); @@ -73,16 +78,21 @@ void livesync_init(void) { g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); - pthread_create(&thread_id, NULL, livesync_worker, NULL); + socket_set_timeout(g_socket_in, LIVESYNC_WORKER_PATIENCE); + thread_started = !pthread_create(&thread_id, NULL, livesync_worker, NULL); } void livesync_deinit(void) { + if (thread_started) { + shutdown(g_socket_in, SHUT_RD); + pthread_join(thread_id, NULL); + thread_started = 0; + } + if (g_socket_in != -1) close(g_socket_in); if (g_socket_out != -1) close(g_socket_out); - - pthread_cancel(thread_id); } void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { @@ -195,7 +205,7 @@ static void *livesync_worker(void *args) { ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); ws.outbuf = ws.reply = 0; - while (1) { + while (g_opentracker_running) { uint32_t packet_type; ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); @@ -224,7 +234,7 @@ static void *livesync_worker(void *args) { } } - /* Never returns. */ + free(ws.inbuf); return NULL; } -- cgit v1.3