diff --git a/Makefile b/Makefile index 4442a72d..c3abc322 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ DNSMASQOPTS = -DHAVE_DNSSEC -DHAVE_DNSSEC_STATIC # Flags for compiling with libidn2: -DHAVE_LIBIDN2 -DIDN2_VERSION_NUMBER=0x02000003 FTLDEPS = *.h version.h -FTLDBOBJ = database/common.o database/query-table.o database/network-table.o database/gravity-db.o +FTLDBOBJ = database/common.o database/query-table.o database/network-table.o database/gravity-db.o database/database-thread.o FTLAPIOBJ = api/socket.o api/request.o api/msgpack.o api/api.o FTLOBJ = $(FTLDBOBJ) $(FTLAPIOBJ) main.o memory.o log.o daemon.o datastructure.o signals.o files.o setupVars.o args.o gc.o config.o dnsmasq_interface.o resolve.o regex.o shmem.o capabilities.o overTime.o timers.o diff --git a/src/database/common.c b/src/database/common.c index 3affe3a3..d0008ea0 100644 --- a/src/database/common.c +++ b/src/database/common.c @@ -10,19 +10,13 @@ #include "FTL.h" #include "common.h" -#include "sqlite3.h" #include "shmem.h" -#include "overTime.h" #include "network-table.h" -#include "query-table.h" -#include "datastructure.h" #include "memory.h" #include "config.h" #include "log.h" #include "timers.h" #include "files.h" -// global variable killed -#include "signals.h" sqlite3 *FTL_db; bool database = false; @@ -424,52 +418,7 @@ long get_lastID(void) return id; } -void *DB_thread(void *val) -{ - int lastDBsave = 0; - - // Set thread name - prctl(PR_SET_NAME,"database",0,0,0); - - // Save timestamp as we do not want to store immediately - // to the database - lastDBsave = time(NULL) - time(NULL)%config.DBinterval; - - while(!killed && database) - { - if(time(NULL) - lastDBsave >= config.DBinterval) - { - // Update lastDBsave timer - lastDBsave = time(NULL) - time(NULL)%config.DBinterval; - - // Lock FTL's data structures, since it is - // likely that they will be changed here - lock_shm(); - - // Save data to database - DB_save_queries(); - - // Release data lock - unlock_shm(); - - // Check if GC should be done on the database - if(DBdeleteoldqueries) - { - // No thread locks needed - delete_old_queries_in_DB(); - DBdeleteoldqueries = false; - } - - // Parse neighbor cache (fill network table) if enabled - if (config.parse_arp_cache) - parse_neighbor_cache(); - } - sleepms(100); - } - - return NULL; -} - +// Return SQLite3 engine version string const char *get_sqlite3_version(void) { return sqlite3_libversion(); diff --git a/src/database/common.h b/src/database/common.h index 77e684c0..6debe210 100644 --- a/src/database/common.h +++ b/src/database/common.h @@ -14,7 +14,6 @@ bool check_database(int rc); void db_init(void); -void *DB_thread(void *val); int db_get_FTL_property(const unsigned int ID); bool db_set_FTL_property(const unsigned int ID, const int value); bool dbquery(const char *format, ...); diff --git a/src/database/database-thread.c b/src/database/database-thread.c new file mode 100644 index 00000000..2b2ae945 --- /dev/null +++ b/src/database/database-thread.c @@ -0,0 +1,70 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2017 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Database thread +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "FTL.h" +#include "database-thread.h" +#include "common.h" +// [un]lock_shm(); +#include "shmem.h" +// parse_neighbor_cache() +#include "network-table.h" +// DB_save_queries() +#include "query-table.h" +#include "config.h" +#include "log.h" +#include "timers.h" +// global variable killed +#include "signals.h" + +void *DB_thread(void *val) +{ + int lastDBsave = 0; + + // Set thread name + prctl(PR_SET_NAME,"database",0,0,0); + + // Save timestamp as we do not want to store immediately + // to the database + lastDBsave = time(NULL) - time(NULL)%config.DBinterval; + + while(!killed && database) + { + if(time(NULL) - lastDBsave >= config.DBinterval) + { + // Update lastDBsave timer + lastDBsave = time(NULL) - time(NULL)%config.DBinterval; + + // Lock FTL's data structures, since it is + // likely that they will be changed here + lock_shm(); + + // Save data to database + DB_save_queries(); + + // Release data lock + unlock_shm(); + + // Check if GC should be done on the database + if(DBdeleteoldqueries) + { + // No thread locks needed + delete_old_queries_in_DB(); + DBdeleteoldqueries = false; + } + + // Parse neighbor cache (fill network table) if enabled + if (config.parse_arp_cache) + parse_neighbor_cache(); + } + sleepms(100); + } + + return NULL; +} diff --git a/src/database/database-thread.h b/src/database/database-thread.h new file mode 100644 index 00000000..dffb44d2 --- /dev/null +++ b/src/database/database-thread.h @@ -0,0 +1,15 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2019 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Database thread prototype +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ +#ifndef DATABASE_THREAD_H +#define DATABASE_THREAD_H + +void *DB_thread(void *val); + +#endif //DATABASE_THREAD_H diff --git a/src/database/network-table.c b/src/database/network-table.c index 8943ba15..0f662684 100644 --- a/src/database/network-table.c +++ b/src/database/network-table.c @@ -12,7 +12,6 @@ #include "network-table.h" #include "common.h" #include "shmem.h" -#include "sqlite3.h" #include "memory.h" #include "log.h" #include "timers.h" diff --git a/src/database/query-table.c b/src/database/query-table.c index 9a90034a..1e73ddd6 100644 --- a/src/database/query-table.c +++ b/src/database/query-table.c @@ -11,14 +11,17 @@ #include "FTL.h" #include "query-table.h" #include "common.h" -#include "sqlite3.h" +// get[Domain,ClientIP,Forward]String(), etc. #include "datastructure.h" +// getOverTimeID() #include "overTime.h" +// get_FTL_db_filesize() #include "files.h" #include "memory.h" #include "timers.h" #include "log.h" #include "config.h" +// getstr() #include "shmem.h" int get_number_of_queries_in_DB(void) @@ -111,18 +114,18 @@ void DB_save_queries(void) // DOMAIN const char *domain = getDomainString(queryID); - sqlite3_bind_text(stmt, 4, domain, -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 4, domain, -1, SQLITE_STATIC); // CLIENT const char *client = getClientIPString(queryID); - sqlite3_bind_text(stmt, 5, client, -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 5, client, -1, SQLITE_STATIC); // FORWARD if(query->status == QUERY_FORWARDED && query->forwardID > -1) { // Get forward pointer const forwardedData* forward = getForward(query->forwardID, true); - sqlite3_bind_text(stmt, 6, getstr(forward->ippos), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 6, getstr(forward->ippos), -1, SQLITE_STATIC); } else { diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 5ce686e6..ad6c2a2c 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -17,6 +17,7 @@ #include "overTime.h" #include "memory.h" #include "database/common.h" +#include "database/database-thread.h" #include "database/gravity-db.h" #include "setupVars.h" #include "daemon.h"