Skip to content

Commit

Permalink
Guarantee FTL's thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
DL6ER committed Mar 8, 2017
1 parent fa99b5b commit 6ccfeea
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
2 changes: 2 additions & 0 deletions FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,7 @@ int setupVarsElements;

bool initialscan;
bool debug;
bool debugthreads;
bool threadlock;

char ** wildcarddomains;
6 changes: 6 additions & 0 deletions args.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 20 additions & 9 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion request.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
9 changes: 9 additions & 0 deletions socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 6ccfeea

Please sign in to comment.