From 7fea06811d30824ef6461aa6776c0dc561697178 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 22 Aug 2024 16:49:27 +0200 Subject: [PATCH] Allow other FTL process inside docker Do not refuse to (re)start if there is another pihole-FTL process already running *inside* a docker container on the same host. Currently, (re)starting on the host is prevented when FTL detects during startup that another process is running on the machine (the one inside the container) even when they can obviously run alongside perfectly fine. This is caused by the host being able to see *all* processes, not only the one in its own cgroup. This actually hinders development/testing natively on the host. This commit fixes this by not considering pihole-FTL processes running in downstream (!) docker containers as duplicate instances. Signed-off-by: DL6ER --- src/procps.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/procps.c b/src/procps.c index 7362b98c6..d23ce7d7f 100644 --- a/src/procps.c +++ b/src/procps.c @@ -114,6 +114,30 @@ static bool get_process_creation_time(const pid_t pid, char timestr[TIMESTR_SIZE return true; } +// This function checks if a given PID is running inside a docker container +static bool is_in_docker(const pid_t pid) +{ + char filename[sizeof("/proc/%u/cgroup") + sizeof(int)*3]; + snprintf(filename, sizeof(filename), "/proc/%d/cgroup", pid); + + FILE *f = fopen(filename, "r"); + if(f == NULL) + return false; + + char buffer[128]; + while(fgets(buffer, sizeof(buffer), f) != NULL) + { + if(strstr(buffer, "/docker") != NULL) + { + fclose(f); + return true; + } + } + fclose(f); + + return false; +} + // This function prints an info message about if another FTL process is already // running. It returns true if another FTL process is already running, false // otherwise. @@ -219,7 +243,7 @@ bool another_FTL(void) if(pid == ourselves) continue; - // Only process this is this is our own process + // Only process this if this is our own process if(strcasecmp(name, PROCESS_NAME) != 0) continue; @@ -231,6 +255,10 @@ bool another_FTL(void) if(!get_process_name(ppid, ppid_name)) continue; + // Skip if this is an instance running inside a docker container + if(is_in_docker(pid)) + continue; + log_debug(DEBUG_SHMEM, " └ PPID: %d -> name: %s", ppid, ppid_name); char timestr[TIMESTR_SIZE] = { 0 };