-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
database-thread.c
100 lines (86 loc) · 2.3 KB
/
database-thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* 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"
// reimport_aliasclients()
#include "aliasclients.h"
// Eventqueue routines
#include "../events.h"
void *DB_thread(void *val)
{
// Set thread name
prctl(PR_SET_NAME,"database",0,0,0);
// Save timestamp as we do not want to store immediately
// to the database
time_t lastDBsave = time(NULL) - time(NULL)%config.DBinterval;
while(!killed)
{
if(FTL_DB_avail())
{
time_t now = time(NULL);
if(now - lastDBsave >= config.DBinterval)
{
// Update lastDBsave timer
lastDBsave = time(NULL) - time(NULL)%config.DBinterval;
// Save data to database (if enabled)
if(config.DBexport)
{
lock_shm();
DB_save_queries();
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)
set_event(PARSE_NEIGHBOR_CACHE);
}
// Update MAC vendor strings once a month (the MAC vendor
// database is not updated very often)
if(now % 2592000L == 0)
updateMACVendorRecords();
if(get_and_clear_event(PARSE_NEIGHBOR_CACHE))
parse_neighbor_cache();
}
// Process database related event queue elements
if(get_and_clear_event(RELOAD_GRAVITY))
FTL_reload_all_domainlists();
// Reload privacy level from pihole-FTL.conf
if(get_and_clear_event(RELOAD_PRIVACY_LEVEL))
get_privacy_level(NULL);
// Import alias-clients
if(get_and_clear_event(REIMPORT_ALIASCLIENTS))
{
lock_shm();
reimport_aliasclients();
unlock_shm();
}
// Sleep 0.1 seconds
sleepms(100);
}
return NULL;
}