From 98b5e33678dcbb5785d960f6a26eb8bfc9fb1ec6 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Thu, 16 Jul 2020 11:02:32 -0700 Subject: [PATCH 1/3] Revert "Remove portfile not used by the current web interface any longer." This reverts commit af9107a338420a592877ae350aa37b77eafb1e59. --- src/api/socket.c | 42 +++++++++++++++++++++++++++++++++++++----- src/config.c | 4 ++++ src/config.h | 1 + 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/api/socket.c b/src/api/socket.c index d034db7d..975729ff 100644 --- a/src/api/socket.c +++ b/src/api/socket.c @@ -34,6 +34,21 @@ bool dualstack = false; bool ipv4telnet = false, ipv6telnet = false, sock_avail = false; bool istelnet[MAXCONNS]; +static void saveport(void) +{ + FILE *f; + if((f = fopen(FTLfiles.port, "w+")) == NULL) + { + logg("WARNING: Unable to write used port to file."); + logg(" Continuing anyway (API might not find the port)."); + } + else + { + fprintf(f, "%i", config.port); + fclose(f); + } +} + static bool bind_to_telnet_port_IPv4(int *socketdescriptor) { // IPv4 socket @@ -79,6 +94,7 @@ static bool bind_to_telnet_port_IPv4(int *socketdescriptor) return false; } + saveport(); logg("Listening on port %i for incoming IPv4 telnet connections", config.port); return true; } @@ -141,6 +157,7 @@ static bool bind_to_telnet_port_IPv6(int *socketdescriptor) return false; } + saveport(); logg("Listening on port %i for incoming IPv6 telnet connections", config.port); return true; } @@ -152,6 +169,7 @@ static bool bind_to_unix_socket(int *socketdescriptor) if(*socketdescriptor < 0) { logg("WARNING: Error opening Unix socket."); + logg(" Continuing anyway."); return false; } @@ -173,6 +191,7 @@ static bool bind_to_unix_socket(int *socketdescriptor) if(bind(*socketdescriptor, (struct sockaddr *) &address, sizeof (address)) != 0) { logg("WARNING: Cannot bind on Unix socket %s: %s (%i)", FTLfiles.socketfile, strerror(errno), errno); + logg(" Continuing anyway."); return false; } @@ -180,6 +199,7 @@ static bool bind_to_unix_socket(int *socketdescriptor) if(listen(*socketdescriptor, BACKLOG) == -1) { logg("WARNING: Cannot listen on Unix socket: %s (%i)", strerror(errno), errno); + logg(" Continuing anyway."); return false; } @@ -187,6 +207,18 @@ static bool bind_to_unix_socket(int *socketdescriptor) return true; } +// Called from main() at graceful shutdown +static void removeport(void) +{ + FILE *f; + if((f = fopen(FTLfiles.port, "w+")) == NULL) + { + logg("WARNING: Unable to empty port file"); + return; + } + fclose(f); +} + void seom(const int sock) { if(istelnet[sock]) @@ -264,6 +296,7 @@ static int listener(const int sockfd, const char type) void close_telnet_socket(void) { + removeport(); // Using global variable here if(telnetfd4) close(telnetfd4); @@ -426,7 +459,7 @@ void *telnet_listening_thread_IPv4(void *args) if(pthread_create( &telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) newsock ) != 0) { // Log the error code description - logg("WARNING: Unable to open telnet processing thread: %s", strerror(errno)); + logg("WARNING: Unable to open telnet processing thread, error: %s", strerror(errno)); } } return false; @@ -475,7 +508,7 @@ void *telnet_listening_thread_IPv6(void *args) if(pthread_create( &telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) newsock ) != 0) { // Log the error code description - logg("WARNING: Unable to open telnet processing thread: %s", strerror(errno)); + logg("WARNING: Unable to open telnet processing thread, error: %s", strerror(errno)); } } return false; @@ -504,8 +537,7 @@ void *socket_listening_thread(void *args) { // Look for new clients that want to connect const int csck = listener(socketfd, 0); - if(csck < 0) - continue; + if(csck < 0) continue; // Allocate memory used to transport client socket ID to client listening thread int *newsock; @@ -518,7 +550,7 @@ void *socket_listening_thread(void *args) if(pthread_create( &socket_connection_thread, &attr, socket_connection_handler_thread, (void*) newsock ) != 0) { // Log the error code description - logg("WARNING: Unable to open socket processing thread: %s", strerror(errno)); + logg("WARNING: Unable to open socket processing thread, error: %s", strerror(errno)); } } return false; diff --git a/src/config.c b/src/config.c index f0355d3c..3ccac98e 100644 --- a/src/config.c +++ b/src/config.c @@ -29,6 +29,7 @@ FTLFileNamesStruct FTLfiles = { NULL, NULL, NULL, + NULL, NULL }; @@ -300,6 +301,9 @@ void read_FTLconf(void) // PIDFILE getpath(fp, "PIDFILE", "/run/pihole-FTL.pid", &FTLfiles.pid); + // PORTFILE + getpath(fp, "PORTFILE", "/run/pihole-FTL.port", &FTLfiles.port); + // SOCKETFILE getpath(fp, "SOCKETFILE", "/run/pihole/FTL.sock", &FTLfiles.socketfile); diff --git a/src/config.h b/src/config.h index 503850f2..429d724b 100644 --- a/src/config.h +++ b/src/config.h @@ -50,6 +50,7 @@ typedef struct { const char* snapConf; char* log; char* pid; + char* port; char* socketfile; char* FTL_db; char* gravity_db; From dd927b586e06f67e7924811bb0d9f2caac432a16 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 17 Jul 2020 10:06:48 +0200 Subject: [PATCH 2/3] Tweak revert commit. We should add the port after configuring it and independtly from opening IPv4 and/or IPv6 sockets. Also, we cannot delete the port in close_telnet_port() as this function is called by TCP workers since v5.1 so we'd loose the port file when the first TCP query comes in. Signed-off-by: DL6ER --- .gitignore | 3 ++- src/api/socket.c | 45 ++++++++++++++++++--------------------------- src/api/socket.h | 1 + src/config.c | 3 +++ src/main.c | 3 +++ 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index a257dec6..c260c28b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,8 @@ pihole-FTL # Versioning files (generated by Makefile) version* -# CMake files +# CMake files generated during compilation +/cmake/ /cmake-build-debug/ /cmake-build-release/ diff --git a/src/api/socket.c b/src/api/socket.c index 975729ff..a5fb901d 100644 --- a/src/api/socket.c +++ b/src/api/socket.c @@ -34,17 +34,25 @@ bool dualstack = false; bool ipv4telnet = false, ipv6telnet = false, sock_avail = false; bool istelnet[MAXCONNS]; -static void saveport(void) +void saveport(int port) { FILE *f; - if((f = fopen(FTLfiles.port, "w+")) == NULL) + // Open "w" for truncation/creating file + if((f = fopen(FTLfiles.port, "w")) == NULL) { - logg("WARNING: Unable to write used port to file."); - logg(" Continuing anyway (API might not find the port)."); + // Opening failed (permissions, path does not exist, etc.) + logg("WARNING: Unable to write used port to file"); + logg(" (API might not find the port)"); + } + else if(port > 0) + { + // Save port to file + fprintf(f, "%i", port); + fclose(f); } else { - fprintf(f, "%i", config.port); + // FTL is terminating: Leave file truncated fclose(f); } } @@ -94,7 +102,6 @@ static bool bind_to_telnet_port_IPv4(int *socketdescriptor) return false; } - saveport(); logg("Listening on port %i for incoming IPv4 telnet connections", config.port); return true; } @@ -157,7 +164,6 @@ static bool bind_to_telnet_port_IPv6(int *socketdescriptor) return false; } - saveport(); logg("Listening on port %i for incoming IPv6 telnet connections", config.port); return true; } @@ -169,7 +175,6 @@ static bool bind_to_unix_socket(int *socketdescriptor) if(*socketdescriptor < 0) { logg("WARNING: Error opening Unix socket."); - logg(" Continuing anyway."); return false; } @@ -191,7 +196,6 @@ static bool bind_to_unix_socket(int *socketdescriptor) if(bind(*socketdescriptor, (struct sockaddr *) &address, sizeof (address)) != 0) { logg("WARNING: Cannot bind on Unix socket %s: %s (%i)", FTLfiles.socketfile, strerror(errno), errno); - logg(" Continuing anyway."); return false; } @@ -199,7 +203,6 @@ static bool bind_to_unix_socket(int *socketdescriptor) if(listen(*socketdescriptor, BACKLOG) == -1) { logg("WARNING: Cannot listen on Unix socket: %s (%i)", strerror(errno), errno); - logg(" Continuing anyway."); return false; } @@ -207,18 +210,6 @@ static bool bind_to_unix_socket(int *socketdescriptor) return true; } -// Called from main() at graceful shutdown -static void removeport(void) -{ - FILE *f; - if((f = fopen(FTLfiles.port, "w+")) == NULL) - { - logg("WARNING: Unable to empty port file"); - return; - } - fclose(f); -} - void seom(const int sock) { if(istelnet[sock]) @@ -296,7 +287,6 @@ static int listener(const int sockfd, const char type) void close_telnet_socket(void) { - removeport(); // Using global variable here if(telnetfd4) close(telnetfd4); @@ -459,7 +449,7 @@ void *telnet_listening_thread_IPv4(void *args) if(pthread_create( &telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) newsock ) != 0) { // Log the error code description - logg("WARNING: Unable to open telnet processing thread, error: %s", strerror(errno)); + logg("WARNING: Unable to open telnet processing thread: %s", strerror(errno)); } } return false; @@ -508,7 +498,7 @@ void *telnet_listening_thread_IPv6(void *args) if(pthread_create( &telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) newsock ) != 0) { // Log the error code description - logg("WARNING: Unable to open telnet processing thread, error: %s", strerror(errno)); + logg("WARNING: Unable to open telnet processing thread: %s", strerror(errno)); } } return false; @@ -537,7 +527,8 @@ void *socket_listening_thread(void *args) { // Look for new clients that want to connect const int csck = listener(socketfd, 0); - if(csck < 0) continue; + if(csck < 0) + continue; // Allocate memory used to transport client socket ID to client listening thread int *newsock; @@ -550,7 +541,7 @@ void *socket_listening_thread(void *args) if(pthread_create( &socket_connection_thread, &attr, socket_connection_handler_thread, (void*) newsock ) != 0) { // Log the error code description - logg("WARNING: Unable to open socket processing thread, error: %s", strerror(errno)); + logg("WARNING: Unable to open socket processing thread: %s", strerror(errno)); } } return false; diff --git a/src/api/socket.h b/src/api/socket.h index 15d54231..630ba960 100644 --- a/src/api/socket.h +++ b/src/api/socket.h @@ -10,6 +10,7 @@ #ifndef SOCKET_H #define SOCKET_H +void saveport(int port); void close_telnet_socket(void); void close_unix_socket(bool unlink_file); void seom(const int sock); diff --git a/src/config.c b/src/config.c index 3ccac98e..12a75d9b 100644 --- a/src/config.c +++ b/src/config.c @@ -15,6 +15,8 @@ #include "log.h" // nice() #include +// saveport() +#include "api/socket.h" ConfigStruct config; FTLFileNamesStruct FTLfiles = { @@ -303,6 +305,7 @@ void read_FTLconf(void) // PORTFILE getpath(fp, "PORTFILE", "/run/pihole-FTL.port", &FTLfiles.port); + saveport(config.port); // SOCKETFILE getpath(fp, "SOCKETFILE", "/run/pihole/FTL.sock", &FTLfiles.socketfile); diff --git a/src/main.c b/src/main.c index 2fa4ea95..98d5a243 100644 --- a/src/main.c +++ b/src/main.c @@ -112,6 +112,9 @@ int main (int argc, char* argv[]) close_telnet_socket(); close_unix_socket(true); + // Empty API port file, port 0 = truncate file + saveport(0); + // Close gravity database connection gravityDB_close(); From 41b49782c2cd62f284eef3d32eeacbd2d4938934 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 17 Jul 2020 10:16:45 +0200 Subject: [PATCH 3/3] Test: Check port file exists and contains the expected number (4711) Signed-off-by: DL6ER --- test/run.sh | 10 +++++++++- test/test_suite.bats | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test/run.sh b/test/run.sh index bf64b9f5..32322aa1 100755 --- a/test/run.sh +++ b/test/run.sh @@ -11,8 +11,16 @@ if ! id -u pihole &> /dev/null; then useradd -m -s /usr/sbin/nologin pihole fi +# Kill possibly running pihole-FTL process +while pidof -s pihole-FTL > /dev/null; do + pid="$(pidof -s pihole-FTL)" + echo "Terminating running pihole-FTL process with PID ${pid}" + kill $pid + sleep 1 +done + # Clean up possible old files from earlier test runs -rm -f /etc/pihole/gravity.db /etc/pihole/pihole-FTL.db /var/log/pihole.log /var/log/pihole-FTL.log +rm -f /etc/pihole/gravity.db /etc/pihole/pihole-FTL.db /var/log/pihole.log /var/log/pihole-FTL.log /dev/shm/FTL-* # Create necessary directories and files mkdir -p /etc/pihole /run/pihole /var/log diff --git a/test/test_suite.bats b/test/test_suite.bats index 82e1e15d..8fe60ab6 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -456,3 +456,9 @@ printf "%s\n" "${lines[@]}" [[ ${lines[0]} == "2" ]] } + +@test "Port file exists and contains expected API port" { + run bash -c 'cat /run/pihole-FTL.port' + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} == "4711" ]] +}