From c71cb022f71ec4aa3ab478094c58937702ae2845 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 11 May 2018 16:31:45 +0200 Subject: [PATCH 1/3] Return early if no wildcard domains are defined to avoid warning to be printed although for-loop will anyhow be skipped Signed-off-by: DL6ER --- datastructure.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/datastructure.c b/datastructure.c index 1ffc6bcf..0d19fc61 100644 --- a/datastructure.c +++ b/datastructure.c @@ -239,15 +239,18 @@ int detectStatus(const char *domain) // Note that this is a really expensive subroutine and trying to match // blocked domains against all configured wildcards will take some time int i; + + // Return early if no wildcard domains are defined + if(counters.wildcarddomains < 1) + return QUERY_CACHE; + validate_access("wildcarddomains", counters.wildcarddomains-1, false, __LINE__, __FUNCTION__, __FILE__); for(i=0; i < counters.wildcarddomains; i++) { if(strcasecmp(wildcarddomains[i], domain) == 0) { // Exact match with wildcard domain - // if(debug) - // printf("%s / %s (exact wildcard match)\n",wildcarddomains[i], domain); - return 4; + return QUERY_WILDCARD; } // Create copy of domain under investigation char * part = strdup(domain); @@ -272,13 +275,11 @@ int detectStatus(const char *domain) // Test for a match if(strcasecmp(wildcarddomains[i], partbuffer) == 0) { - // Free allocated memory before return'ing + // Free allocated memory before returning free(part); free(partbuffer); // Return match with wildcard domain - // if(debug) - // printf("%s / %s (wildcard match)\n",wildcarddomains[i], partbuffer); - return 4; + return QUERY_WILDCARD; } if(strlen(partbuffer) > 0) { @@ -297,5 +298,5 @@ int detectStatus(const char *domain) // wildcard blocking, but from e.g. an // address=// configuration // Answer as "cached" - return 3; + return QUERY_CACHE; } From 599e27b324f6e04fff244d92137ed91796b50e3e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 12 May 2018 14:39:44 +0200 Subject: [PATCH 2/3] Revert "No forking" --- FTL.h | 1 + args.c | 20 ++++++ daemon.c | 158 ++++++++++++++++++++++++++++++++++++++++++++ dnsmasq/dnsmasq.c | 2 +- dnsmasq_interface.c | 8 ++- dnsmasq_interface.h | 2 +- 6 files changed, 186 insertions(+), 5 deletions(-) diff --git a/FTL.h b/FTL.h index ed08f89b..b4058b4e 100644 --- a/FTL.h +++ b/FTL.h @@ -258,6 +258,7 @@ extern char * username; extern char timestamp[16]; extern bool flush; extern bool needGC; +extern bool daemonmode; extern bool database; extern long int lastdbindex; extern bool travis; diff --git a/args.c b/args.c index 09e07003..83ab7106 100644 --- a/args.c +++ b/args.c @@ -12,6 +12,7 @@ #include "version.h" bool debug = false; +bool daemonmode = true; bool debugthreads = false; bool debugclients = false; bool debugGC = false; @@ -113,6 +114,23 @@ void parse_args(int argc, char* argv[]) exit(EXIT_SUCCESS); } + // 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; + ok = true; + } + + // Don't go into background + if(strcmp(argv[i], "-f") == 0 || + strcmp(argv[i], "no-daemon") == 0) + { + daemonmode = false; + ok = true; + } + // Use files in local places for Travis-CI tests if(strcmp(argv[i], "travis-ci") == 0) { @@ -168,10 +186,12 @@ void parse_args(int argc, char* argv[]) printf("\t-v, version Return version\n"); printf("\t-t, tag Return git tag\n"); printf("\t-b, branch Return git branch\n"); + printf("\t running Test if another pihole-FTL\n"); printf("\t process is running and exit\n"); printf("\t even if not (instead of\n"); printf("\t starting a new one)\n"); printf("\t-f, no-daemon Don't go into daemon mode\n"); + printf("\t-h, help Display this help and exit\n"); printf("\tdnsmasq-test Test syntax of dnsmasq's\n"); printf("\t config files and exit\n"); printf("\n\nOnline help: https://github.com/pi-hole/FTL\n"); diff --git a/daemon.c b/daemon.c index 92ff614e..8a740660 100644 --- a/daemon.c +++ b/daemon.c @@ -13,6 +13,164 @@ struct timeval t0[NUMTIMERS]; +int detect_FTL_process(void) +{ + DIR* dir = opendir("/proc"); + + if(dir) + { + struct dirent* de = 0; + while((de = readdir(dir)) != 0) + { + // Skip "." and ".." + if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; + + int pid = -1; + if(sscanf(de->d_name, "%d", &pid) == 1) + { + // Test if that is our own process + if(pid == getpid()) + continue; + + char buffer[512] = { 0 }; + sprintf(buffer, "/proc/%d/cmdline", pid); + + FILE* fp; + if((fp = fopen(buffer, "r")) != NULL) + { + char *linebuffer = NULL; + size_t size = 0; + + errno = 0; + if (getline(&linebuffer, &size, fp) != -1) + { + if (strstr(linebuffer, "pihole-FTL") != 0) + { + fclose(fp); + logg("%i - %s", pid, linebuffer); + return pid; + } + } + + if(errno == ENOMEM) + logg("WARN: process_pihole_log failed: could not allocate memory for getline"); + + if(linebuffer != NULL) + { + free(linebuffer); + linebuffer = NULL; + } + fclose(fp); + } + } + } + closedir(dir); + } + return -1; +} + +void test_singularity(void) +{ + if(runtest) + { + if(detect_FTL_process() > -1) + { + printf("Yes: Found a running FTL process\n"); + exit(EXIT_FAILURE); + } + else + { + printf("No: Did not find a running FTL process\n"); + exit(EXIT_SUCCESS); + } + } + + int pid; + while((pid = detect_FTL_process()) > -1) + { + printf("Found pihole-FTL process with PID %i (my PID %i) - killing it ...\n", pid, getpid()); + logg("Found pihole-FTL process with PID %i (my PID %i) - killing it ...", pid, getpid()); + if(kill(pid, SIGTERM) != 0) + { + printf("Killing failed (%s) ... Exiting now ...\n", strerror(errno)); + logg("Killing failed (%s) ... Exiting now ...", strerror(errno)); + exit(EXIT_FAILURE); + } + } + logg("Found no other running pihole-FTL process"); +} + +void go_daemon(void) +{ + pid_t process_id = 0; + pid_t sid = 0; + + test_singularity(); + + // Create child process + process_id = fork(); + + // Indication of fork() failure + if (process_id < 0) + { + logg("fork failed!\n"); + // Return failure in exit status + exit(EXIT_FAILURE); + } + + // PARENT PROCESS. Need to kill it. + if (process_id > 0) + { + printf("FTL started!\n"); + // return success in exit status + exit(EXIT_SUCCESS); + } + + //unmask the file mode + umask(0); + + //set new session + // creates a session and sets the process group ID + sid = setsid(); + if(sid < 0) + { + // Return failure + logg("setsid failed!\n"); + exit(EXIT_FAILURE); + } + + // Create grandchild process + // Fork a second child and exit immediately to prevent zombies. This + // causes the second child process to be orphaned, making the init + // process responsible for its cleanup. And, since the first child is + // a session leader without a controlling terminal, it's possible for + // it to acquire one by opening a terminal in the future (System V- + // based systems). This second fork guarantees that the child is no + // longer a session leader, preventing the daemon from ever acquiring + // a controlling terminal. + process_id = fork(); + + // Indication of fork() failure + if (process_id < 0) + { + logg("fork failed!\n"); + // Return failure in exit status + exit(EXIT_FAILURE); + } + + // PARENT PROCESS. Need to kill it. + if (process_id > 0) + { + // return success in exit status + exit(EXIT_SUCCESS); + } + + savepid(); + + // Closing stdin, stdout and stderr is handled by dnsmasq +} + void timer_start(int i) { if(i >= NUMTIMERS) diff --git a/dnsmasq/dnsmasq.c b/dnsmasq/dnsmasq.c index c5e768b0..54a47da8 100644 --- a/dnsmasq/dnsmasq.c +++ b/dnsmasq/dnsmasq.c @@ -570,7 +570,7 @@ int main_dnsmasq (int argc, char **argv) } } - FTL_start_threads(); + FTL_fork_and_bind_sockets(); log_err = log_start(ent_pw, err_pipe[1]); diff --git a/dnsmasq_interface.c b/dnsmasq_interface.c index 2f035010..7baa500a 100644 --- a/dnsmasq_interface.c +++ b/dnsmasq_interface.c @@ -735,10 +735,12 @@ pthread_t socket_listenthread; pthread_t DBthread; pthread_t GCthread; -void FTL_start_threads(void) +void FTL_fork_and_bind_sockets(void) { - // Save PID - savepid(); + if(!debug && daemonmode) + go_daemon(); + else + savepid(); // We will use the attributes object later to start all threads in detached mode pthread_attr_t attr; diff --git a/dnsmasq_interface.h b/dnsmasq_interface.h index e7e7a20b..7872be16 100644 --- a/dnsmasq_interface.h +++ b/dnsmasq_interface.h @@ -16,7 +16,7 @@ void FTL_reply(unsigned short flags, char *name, struct all_addr *addr, int id); void FTL_cache(unsigned int flags, char *name, struct all_addr *addr, char * arg, int id); void FTL_dnssec(int status, int id); void FTL_dnsmasq_reload(void); -void FTL_start_threads(void); +void FTL_fork_and_bind_sockets(void); void FTL_forwarding_failed(struct server *server); int FTL_listsfile(char* filename, unsigned int index, FILE *f, int cache_size, struct crec **rhash, int hashsz); From f28cc2b8fb97ee1bcab6ad819ab329dc375e0bb2 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 12 May 2018 14:46:02 +0200 Subject: [PATCH 3/3] Remove obsolete test singularity function Signed-off-by: DL6ER --- FTL.h | 1 - args.c | 14 --------- daemon.c | 91 -------------------------------------------------------- 3 files changed, 106 deletions(-) diff --git a/FTL.h b/FTL.h index b4058b4e..6b155e6b 100644 --- a/FTL.h +++ b/FTL.h @@ -252,7 +252,6 @@ extern unsigned char blockingstatus; extern char ** wildcarddomains; extern memoryStruct memory; -extern bool runtest; extern char * username; extern char timestamp[16]; diff --git a/args.c b/args.c index 83ab7106..923f3133 100644 --- a/args.c +++ b/args.c @@ -16,7 +16,6 @@ bool daemonmode = true; bool debugthreads = false; bool debugclients = false; bool debugGC = false; -bool runtest = false; bool debugDB = false; bool travis = false; int argc_dnsmasq = 0; @@ -114,15 +113,6 @@ void parse_args(int argc, char* argv[]) exit(EXIT_SUCCESS); } - // 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; - ok = true; - } - // Don't go into background if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "no-daemon") == 0) @@ -186,10 +176,6 @@ void parse_args(int argc, char* argv[]) printf("\t-v, version Return version\n"); printf("\t-t, tag Return git tag\n"); printf("\t-b, branch Return git branch\n"); - printf("\t running Test if another pihole-FTL\n"); - printf("\t process is running and exit\n"); - printf("\t even if not (instead of\n"); - printf("\t starting a new one)\n"); printf("\t-f, no-daemon Don't go into daemon mode\n"); printf("\t-h, help Display this help and exit\n"); printf("\tdnsmasq-test Test syntax of dnsmasq's\n"); diff --git a/daemon.c b/daemon.c index 8a740660..c535f857 100644 --- a/daemon.c +++ b/daemon.c @@ -9,105 +9,14 @@ * Please see LICENSE file for your rights under this license. */ #include "FTL.h" -#include struct timeval t0[NUMTIMERS]; -int detect_FTL_process(void) -{ - DIR* dir = opendir("/proc"); - - if(dir) - { - struct dirent* de = 0; - while((de = readdir(dir)) != 0) - { - // Skip "." and ".." - if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) - continue; - - int pid = -1; - if(sscanf(de->d_name, "%d", &pid) == 1) - { - // Test if that is our own process - if(pid == getpid()) - continue; - - char buffer[512] = { 0 }; - sprintf(buffer, "/proc/%d/cmdline", pid); - - FILE* fp; - if((fp = fopen(buffer, "r")) != NULL) - { - char *linebuffer = NULL; - size_t size = 0; - - errno = 0; - if (getline(&linebuffer, &size, fp) != -1) - { - if (strstr(linebuffer, "pihole-FTL") != 0) - { - fclose(fp); - logg("%i - %s", pid, linebuffer); - return pid; - } - } - - if(errno == ENOMEM) - logg("WARN: process_pihole_log failed: could not allocate memory for getline"); - - if(linebuffer != NULL) - { - free(linebuffer); - linebuffer = NULL; - } - fclose(fp); - } - } - } - closedir(dir); - } - return -1; -} - -void test_singularity(void) -{ - if(runtest) - { - if(detect_FTL_process() > -1) - { - printf("Yes: Found a running FTL process\n"); - exit(EXIT_FAILURE); - } - else - { - printf("No: Did not find a running FTL process\n"); - exit(EXIT_SUCCESS); - } - } - - int pid; - while((pid = detect_FTL_process()) > -1) - { - printf("Found pihole-FTL process with PID %i (my PID %i) - killing it ...\n", pid, getpid()); - logg("Found pihole-FTL process with PID %i (my PID %i) - killing it ...", pid, getpid()); - if(kill(pid, SIGTERM) != 0) - { - printf("Killing failed (%s) ... Exiting now ...\n", strerror(errno)); - logg("Killing failed (%s) ... Exiting now ...", strerror(errno)); - exit(EXIT_FAILURE); - } - } - logg("Found no other running pihole-FTL process"); -} - void go_daemon(void) { pid_t process_id = 0; pid_t sid = 0; - test_singularity(); - // Create child process process_id = fork();