diff --git a/FTL.h b/FTL.h index 90a13bc9..3be2f0cf 100644 --- a/FTL.h +++ b/FTL.h @@ -188,3 +188,4 @@ bool threadreadlock; char ** wildcarddomains; memoryStruct memory; +bool runtest; diff --git a/args.c b/args.c index 76c135a9..be3528c7 100644 --- a/args.c +++ b/args.c @@ -15,6 +15,7 @@ bool debug = false; bool debugthreads = false; bool debugclients = false; bool debugGC = false; +bool runtest = false; void parse_args(int argc, char* argv[]) { int i; @@ -55,6 +56,14 @@ void parse_args(int argc, char* argv[]) exit(0); } + // pihole-FTL running + // will test if another pihole-FTL process is running + // and exits even if not (instead of starting a new one) + if(strcmp(argv[i], "running") == 0) + { + runtest = true; + } + // Other arguments are ignored } } diff --git a/daemon.c b/daemon.c index db645e11..c86840b6 100644 --- a/daemon.c +++ b/daemon.c @@ -15,9 +15,17 @@ bool test_singularity(void) FILE *f; if((f = fopen(FTLfiles.pid, "r")) == NULL) { - logg("WARNING: Unable to read PID from file (cannot open file)."); - logg(" Cannot test if another FTL process is running!"); - return true; + if(!runtest) + { + logg("WARNING: Unable to read PID from file (cannot open file)."); + logg(" Cannot test if another FTL process is running!"); + return true; + } + else + { + printf("Unknown: Unable to open PID file\n"); + exit(1); + } } // Test if any process has the given PID // We use getpgid() since we are allowed to inspect the @@ -25,20 +33,45 @@ bool test_singularity(void) int pid; if(fscanf(f,"%d",&pid) != 1) { - logg("WARNING: Unable to read PID from file (cannot read PID from file)."); - logg(" Cannot test if another FTL process is running!"); - fclose(f); - return true; + if(!runtest) + { + logg("WARNING: Unable to read PID from file (cannot read PID from file)."); + logg(" Cannot test if another FTL process is running!"); + fclose(f); + return true; + } + else + { + printf("Unknown: Unable to read PID from file\n"); + exit(1); + } } fclose(f); + + // Test if another process is running if (getpgid(pid) >= 0) { - // Other process is running - printf("FATAL: Another FTL process is already running! Exiting...\n"); - logg("FATAL: Another FTL process is already running! Exiting..."); - return false; + if(!runtest) + { + printf("FATAL: Another FTL process is already running! Exiting...\n"); + logg("FATAL: Another FTL process is already running! Exiting..."); + return false; + } + else + { + printf("Yes: Found a running FTL process\n"); + exit(1); + } } // No other process found - return true; + if(!runtest) + { + return true; + } + else + { + printf("No: Did not find a running FTL process\n"); + exit(0); + } } void go_daemon(void)