Do not re-open gravity database when not forking for TCP workers (debug mode) and simplify network table routines (remove code duplication and prevent possible dead-locks when trying to resolve host names)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-08-18 23:28:03 +02:00
parent 1dde00299b
commit 2d1ec8099f
4 changed files with 45 additions and 58 deletions
+8 -8
View File
@@ -21,7 +21,7 @@
// init_shmem()
#include "shmem.h"
static bool debug = false;
bool dnsmasq_debug = false;
bool daemonmode = true, cli_mode = false;
int argc_dnsmasq = 0;
const char** argv_dnsmasq = NULL;
@@ -84,12 +84,12 @@ void parse_args(int argc, char* argv[])
argv_dnsmasq = calloc(argc_dnsmasq, sizeof(const char*));
argv_dnsmasq[0] = "";
if(debug)
if(dnsmasq_debug)
argv_dnsmasq[1] = "-d";
else
argv_dnsmasq[1] = "-k";
if(debug)
if(dnsmasq_debug)
{
printf("dnsmasq options: [0]: %s\n", argv_dnsmasq[0]);
printf("dnsmasq options: [1]: %s\n", argv_dnsmasq[1]);
@@ -99,7 +99,7 @@ void parse_args(int argc, char* argv[])
while(i < argc)
{
argv_dnsmasq[j++] = strdup(argv[i++]);
if(debug)
if(dnsmasq_debug)
printf("dnsmasq options: [%i]: %s\n", j-1, argv_dnsmasq[j-1]);
}
@@ -112,11 +112,11 @@ void parse_args(int argc, char* argv[])
if(strcmp(argv[i], "d") == 0 ||
strcmp(argv[i], "debug") == 0)
{
debug = true;
dnsmasq_debug = true;
daemonmode = false;
ok = true;
// Replace "-k" by "-d" (debug mode implies nofork)
// Replace "-k" by "-d" (dnsmasq_debug mode implies nofork)
argv_dnsmasq[1] = "-d";
}
@@ -169,9 +169,9 @@ void parse_args(int argc, char* argv[])
// Enable stdout printing
cli_mode = true;
if(argc == i + 2)
exit(regex_test(debug, quiet, argv[i + 1], NULL));
exit(regex_test(dnsmasq_debug, quiet, argv[i + 1], NULL));
else if(argc == i + 3)
exit(regex_test(debug, quiet, argv[i + 1], argv[i + 2]));
exit(regex_test(dnsmasq_debug, quiet, argv[i + 1], argv[i + 2]));
else
{
printf("pihole-FTL: invalid option -- '%s' need either one or two parameters\nTry '%s --help' for more information\n", argv[i], argv[0]);
+1 -1
View File
@@ -12,7 +12,7 @@
void parse_args(int argc, char* argv[]);
extern bool daemonmode, cli_mode;
extern bool daemonmode, cli_mode, dnsmasq_debug;
extern int argc_dnsmasq;
extern const char ** argv_dnsmasq;
+24 -49
View File
@@ -861,23 +861,15 @@ void parse_neighbor_cache(void)
// Obtain ID which was given to this new entry
dbID = get_lastID();
// Add unique IP address / mock-MAC pair to network_addresses table
rc = add_netDB_network_address(dbID, ip);
if(rc != SQLITE_OK)
break;
// Try to determine host names if this is a new device we don't know a hostname for...
unlock_shm();
if(strlen(hostname) == 0)
hostname = resolveHostname(ip);
lock_shm();
// ... and store it in the appropriate network_address record
rc = update_netDB_name(ip, hostname);
if(rc != SQLITE_OK)
break;
// Store interface if available
rc = update_netDB_interface(dbID, iface);
if(rc != SQLITE_OK)
break;
}
else
{
@@ -891,11 +883,6 @@ void parse_neighbor_cache(void)
// Update/replace important device properties
unmock_netDB_device(hwaddr, macVendor, dbID);
// Store interface if available
rc = update_netDB_interface(dbID, iface);
if(rc != SQLITE_OK)
break;
// Host name, count and last query timestamp will be set in the next
// loop interation for the sake of simplicity
}
@@ -921,8 +908,12 @@ void parse_neighbor_cache(void)
if(rc != SQLITE_OK)
break;
}
// else:
// Device in database but not known to Pi-hole: No action required
// else: Device in database but not known to Pi-hole
// Store interface if available
rc = update_netDB_interface(dbID, iface);
if(rc != SQLITE_OK)
break;
// Add unique IP address / mock-MAC pair to network_addresses table
rc = add_netDB_network_address(dbID, ip);
@@ -1065,24 +1056,8 @@ void parse_neighbor_cache(void)
// Obtain ID which was given to this new entry
dbID = get_lastID();
// Add unique IP address / mock-MAC pair to network_addresses table
rc = add_netDB_network_address(dbID, ipaddr);
if(rc != SQLITE_OK)
break;
// Create new name record
rc = update_netDB_name(ipaddr, hostname);
if(rc != SQLITE_OK)
break;
// Update interface if available
rc = update_netDB_interface(dbID, interface);
if(rc != SQLITE_OK)
break;
}
// Device already in database
else
else // Device already in database
{
// Update timestamp of last query if applicable
rc = update_netDB_lastQuery(dbID, client);
@@ -1093,23 +1068,23 @@ void parse_neighbor_cache(void)
rc = update_netDB_numQueries(dbID, client);
if(rc != SQLITE_OK)
break;
// Add unique IP address / mock-MAC pair to network_addresses table
rc = add_netDB_network_address(dbID, ipaddr);
if(rc != SQLITE_OK)
break;
// Update hostname if available
rc = update_netDB_name(ipaddr, hostname);
if(rc != SQLITE_OK)
break;
// Update interface if available
rc = update_netDB_interface(dbID, interface);
if(rc != SQLITE_OK)
break;
}
// Add unique IP address / mock-MAC pair to network_addresses table
rc = add_netDB_network_address(dbID, ipaddr);
if(rc != SQLITE_OK)
break;
// Update hostname if available
rc = update_netDB_name(ipaddr, hostname);
if(rc != SQLITE_OK)
break;
// Update interface if available
rc = update_netDB_interface(dbID, interface);
if(rc != SQLITE_OK)
break;
// Add to number of processed ARP cache entries
additional_entries++;
}
+12
View File
@@ -1863,6 +1863,12 @@ static void prepare_blocking_metadata(void)
volatile atomic_flag worker_already_terminating = ATOMIC_FLAG_INIT;
void FTL_TCP_worker_terminating(bool finished)
{
if(dnsmasq_debug)
{
// Nothing to be done here, forking does not happen in debug mode
return;
}
if(atomic_flag_test_and_set(&worker_already_terminating))
{
logg("TCP worker already terminating!");
@@ -1937,6 +1943,12 @@ void FTL_TCP_worker_created(const int confd, const char *iface_name)
return;
}
if(dnsmasq_debug)
{
// Nothing to be done here, forking does not happen in debug mode
return;
}
// Reopen gravity database handle in this fork as the main process's
// handle isn't valid here
gravityDB_forked();