From 6ccfeea2da631f7901e56d655f45210a4e72cb40 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 8 Mar 2017 11:47:58 +0100 Subject: [PATCH] Guarantee FTL's thread safety --- FTL.h | 2 ++ args.c | 6 ++++++ parser.c | 29 ++++++++++++++++++++--------- request.c | 7 ++++++- socket.c | 9 +++++++++ 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/FTL.h b/FTL.h index 21ec14a1..3d9340bf 100644 --- a/FTL.h +++ b/FTL.h @@ -145,5 +145,7 @@ int setupVarsElements; bool initialscan; bool debug; +bool debugthreads; +bool threadlock; char ** wildcarddomains; diff --git a/args.c b/args.c index 2a609a9a..1d7d1a5b 100644 --- a/args.c +++ b/args.c @@ -12,12 +12,18 @@ #include "version.h" bool debug = false; +bool threaddebug = false; void parse_args(int argc, char* argv[]) { int i; for(i=0; i < argc; i++) { if(strcmp(argv[i], "debug") == 0) debug = true; + if(strcmp(argv[i], "threaddebug") == 0) + { + debug = true; + threaddebug = true; + } if(strcmp(argv[i], "test") == 0) killed = 1; if(strcmp(argv[i], "version") == 0) diff --git a/parser.c b/parser.c index 9f8a9883..4a421884 100644 --- a/parser.c +++ b/parser.c @@ -43,16 +43,27 @@ void *pihole_log_thread(void *val) { int newdata = checkLogForChanges(); - // Process new data if found - if(newdata > 0) + if(newdata != 0) { - process_pihole_log(); - } - - // Process flushed log - else if(newdata < 0) - { - pihole_log_flushed(); + // Lock FTL data structure, since it is likely that it will be changed here + // Requests should not be processed/answered when data is about to change + while(threadlock) sleepms(1); + if(debugthreads) + logg("Thread lock enabled (pihole_log_thread)"); + threadlock = true; + // Process new data if found + if(newdata > 0) + { + process_pihole_log(); + } + // Process flushed log + else if(newdata < 0) + { + pihole_log_flushed(); + } + threadlock = false; + if(debugthreads) + logg("Thread lock disabled (pihole_log_thread)"); } sleepms(50); diff --git a/request.c b/request.c index b2ce994c..dd0c4a39 100644 --- a/request.c +++ b/request.c @@ -140,7 +140,12 @@ void process_request(char *client_message, int *sock) if(excludedomains != NULL) clearSetupVarsArray(); if(debug) - logg_int("Sent top lists data to client, ID: ", *sock); + { + if(blocked) + logg_int("Sent top ads list data to client, ID: ", *sock); + else + logg_int("Sent top domains list data to client, ID: ", *sock); + } } else if(command(client_message, ">top-clients")) { diff --git a/socket.c b/socket.c index 758429e5..6aae4e45 100644 --- a/socket.c +++ b/socket.c @@ -157,7 +157,16 @@ void *connection_handler_thread(void *socket_desc) { char *message = calloc(strlen(client_message)+1,sizeof(char)); strcpy(message, client_message); + // Lock FTL data structure, since it is likely that it will be changed here + // Requests should not be processed/answered when data is about to change + while(threadlock) sleepms(1); + threadlock = true; + if(debugthreads) + logg("Thread lock enabled (process_request)"); process_request(message, &sock); + threadlock = false; + if(debugthreads) + logg("Thread lock disabled (process_request)"); free(message); if(sock == 0) {