Remove Unix socket support

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2019-10-25 17:09:56 +02:00
parent a5a35018f3
commit f5824a2aab
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
7 changed files with 10 additions and 183 deletions

View File

@ -17,8 +17,6 @@
#include "memory.h"
// global variable killed
#include "signals.h"
// http_init()
#include "http.h"
// The backlog argument defines the maximum length
// to which the queue of pending connections for
@ -31,7 +29,7 @@
#define BACKLOG 5
// File descriptors
int socketfd, telnetfd4 = 0, telnetfd6 = 0;
int telnetfd4 = 0, telnetfd6 = 0;
bool dualstack = false;
bool ipv4telnet = false, ipv6telnet = false;
bool sock_avail = false;
@ -163,51 +161,6 @@ static bool bind_to_telnet_port_IPv6(int *socketdescriptor)
return true;
}
static void bind_to_unix_socket(int *socketdescriptor)
{
*socketdescriptor = socket(AF_LOCAL, SOCK_STREAM, 0);
if(*socketdescriptor < 0)
{
logg("WARNING: Error opening Unix socket.");
logg(" Continuing anyway.");
return;
}
// Make sure unix socket file handle does not exist, if it exists, remove it
unlink(FTLfiles.socketfile);
struct sockaddr_un address;
address.sun_family = AF_LOCAL;
// The sockaddr_un.sum_path may be shorter than the size of the FTLfiles.socketfile
// buffer. Ensure that the string is null-terminated even when the string is too large.
// In case strlen(FTLfiles.socketfile) < sizeof(address.sun_path) [this will virtually
// always be the case], the explicit setting of the last byte to zero is a no-op as
// strncpy() writes additional null bytes to ensure that a total of n bytes are written.
strncpy(address.sun_path, FTLfiles.socketfile, sizeof(address.sun_path));
address.sun_path[sizeof(address.sun_path)-1] = '\0';
// Bind to Unix socket handle
errno = 0;
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;
}
// The listen system call allows the process to listen on the Unix socket for connections
if(listen(*socketdescriptor, BACKLOG) == -1)
{
logg("WARNING: Cannot listen on Unix socket: %s (%i)", strerror(errno), errno);
logg(" Continuing anyway.");
return;
}
logg("Listening on Unix socket");
sock_avail = true;
}
// Called from main() at graceful shutdown
static void removeport(void)
{
@ -272,11 +225,6 @@ static int listener(const int sockfd, const char type)
switch(type)
{
case 0: // Unix socket
memset(&un_addr, 0, sizeof(un_addr));
socklen = sizeof(un_addr);
return accept(sockfd, (struct sockaddr *) &un_addr, &socklen);
case 4: // Internet socket (IPv4)
memset(&in4_addr, 0, sizeof(in4_addr));
socklen = sizeof(un_addr);
@ -305,14 +253,6 @@ void close_telnet_socket(void)
close(telnetfd6);
}
void close_unix_socket(void)
{
// The process has to take care of unlinking the socket file description on exit
unlink(FTLfiles.socketfile);
// Using global variable here
close(socketfd);
}
static void *telnet_connection_handler_thread(void *socket_desc)
{
//Get the socket descriptor
@ -363,7 +303,6 @@ static void *telnet_connection_handler_thread(void *socket_desc)
return false;
}
static void *socket_connection_handler_thread(void *socket_desc)
{
//Get the socket descriptor
@ -410,65 +349,6 @@ static void *socket_connection_handler_thread(void *socket_desc)
//Free the socket pointer
if(sock != 0)
close(sock);
free(socket_desc);
return false;
}
void bind_sockets(void)
{
// Initialize IPv4 telnet socket
if(bind_to_telnet_port_IPv4(&telnetfd4))
ipv4telnet = true;
// Initialize IPv6 telnet socket
// only if IPv6 interfaces are available
if(ipv6_available())
if(bind_to_telnet_port_IPv6(&telnetfd6))
ipv6telnet = true;
saveport();
// Initialize Unix socket
bind_to_unix_socket(&socketfd);
// Initialize HTTP server
http_init();
}
void *telnet_listening_thread_IPv4(void *args)
{
// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
// Initialize thread attributes object with default attribute values
pthread_attr_init(&attr);
// When a detached thread terminates, its resources are automatically released back to
// the system without the need for another thread to join with the terminated thread
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Set thread name
prctl(PR_SET_NAME,"telnet-IPv4",0,0,0);
// Listen as long as FTL is not killed
while(!killed)
{
// Look for new clients that want to connect
const int csck = listener(telnetfd4, 4);
if(csck == -1)
{
logg("IPv4 telnet error: %s (%i)", strerror(errno), errno);
continue;
}
// Allocate memory used to transport client socket ID to client listening thread
int *newsock;
newsock = calloc(1,sizeof(int));
if(newsock == NULL) break;
*newsock = csck;
pthread_t telnet_connection_thread;
// Create a new thread
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));
@ -518,47 +398,6 @@ void *telnet_listening_thread_IPv6(void *args)
return false;
}
void *socket_listening_thread(void *args)
{
// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
// Initialize thread attributes object with default attribute values
pthread_attr_init(&attr);
// When a detached thread terminates, its resources are automatically released back to
// the system without the need for another thread to join with the terminated thread
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Set thread name
prctl(PR_SET_NAME,"socket listener",0,0,0);
// Return early to avoid CPU spinning if Unix socket is not available
if(!sock_avail)
return NULL;
// Listen as long as FTL is not killed
while(!killed)
{
// Look for new clients that want to connect
const int csck = listener(socketfd, 0);
if(csck < 0) continue;
// Allocate memory used to transport client socket ID to client listening thread
int *newsock;
newsock = calloc(1,sizeof(int));
if(newsock == NULL) break;
*newsock = csck;
pthread_t socket_connection_thread;
// Create a new thread
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));
}
}
return false;
}
bool ipv6_available(void)
{
struct ifaddrs *allInterfaces;

View File

@ -11,13 +11,11 @@
#define SOCKET_H
void close_telnet_socket(void);
void close_unix_socket(void);
void seom(const int sock);
void ssend(const int sock, const char *format, ...) __attribute__ ((format (gnu_printf, 2, 3)));
void swrite(const int sock, const void* value, const size_t size);
void *telnet_listening_thread_IPv4(void *args);
void *telnet_listening_thread_IPv6(void *args);
void *socket_listening_thread(void *args);
bool ipv6_available(void);
void bind_sockets(void);

View File

@ -27,7 +27,6 @@ FTLFileNamesStruct FTLfiles = {
NULL,
NULL,
NULL,
NULL,
NULL
};
@ -310,9 +309,6 @@ void read_FTLconf(void)
// PORTFILE
getpath(fp, "PORTFILE", "/run/pihole-FTL.port", &FTLfiles.port);
// SOCKETFILE
getpath(fp, "SOCKETFILE", "/run/pihole/FTL.sock", &FTLfiles.socketfile);
// SETUPVARSFILE
getpath(fp, "SETUPVARSFILE", "/etc/pihole/setupVars.conf", &FTLfiles.setupVars);

View File

@ -47,7 +47,6 @@ typedef struct {
char* log;
char* pid;
char* port;
char* socketfile;
char* FTL_db;
char* gravity_db;
char* macvendor_db;

View File

@ -35,6 +35,8 @@
#include "api/api.h"
// global variable daemonmode
#include "args.h"
// http_init()
#include "api/http.h"
static void print_flags(const unsigned int flags);
static void save_reply_type(const unsigned int flags, const union all_addr *addr,
@ -1613,7 +1615,6 @@ static void save_reply_type(const unsigned int flags, const union all_addr *addr
pthread_t telnet_listenthreadv4;
pthread_t telnet_listenthreadv6;
pthread_t socket_listenthread;
pthread_t DBthread;
pthread_t GCthread;
pthread_t DNSclientthread;
@ -1656,13 +1657,6 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw)
exit(EXIT_FAILURE);
}
// Start SOCKET thread
if(pthread_create( &socket_listenthread, &attr, socket_listening_thread, NULL ) != 0)
{
logg("Unable to open Unix socket listening thread. Exiting...");
exit(EXIT_FAILURE);
}
// Start database thread if database is used
if(database && pthread_create( &DBthread, &attr, DB_thread, NULL ) != 0)
{
@ -1701,6 +1695,9 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw)
// Obtain DNS port from dnsmasq daemon
config.dns_port = daemon->port;
// Initialize FTL HTTP server
http_init();
}
// int cache_inserted, cache_live_freed are defined in dnsmasq/cache.c

View File

@ -10,10 +10,7 @@
#ifndef DNSMASQ_INTERFACE_H
#define DNSMASQ_INTERFACE_H
// Including stdbool.h here as it is required for defining the boolean prototype of FTL_new_query
#include <stdbool.h>
extern int socketfd, telnetfd4, telnetfd6;
extern int telnetfd4, telnetfd6;
extern unsigned char* pihole_privacylevel;
enum { TCP, UDP };

View File

@ -98,7 +98,6 @@ int main (int argc, char* argv[])
// Cancel active threads as we don't need them any more
if(ipv4telnet) pthread_cancel(telnet_listenthreadv4);
if(ipv6telnet) pthread_cancel(telnet_listenthreadv6);
pthread_cancel(socket_listenthread);
// Save new queries to database
if(database)
@ -109,7 +108,6 @@ int main (int argc, char* argv[])
// Close sockets
close_telnet_socket();
close_unix_socket();
// Close gravity database connection
gravityDB_close();
@ -122,6 +120,9 @@ int main (int argc, char* argv[])
// Terminate HTTP server
http_terminate();
// Remove shared memory objects
destroy_shmem();
//Remove PID file
removepid();
logg("########## FTL terminated after %e s! ##########", 1e-3*timer_elapsed_msec(EXIT_TIMER));