forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.c
182 lines (151 loc) · 5.52 KB
/
gc.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* 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
* Garbage collection routines
*
* 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 "shmem.h"
bool doGC = false;
time_t lastGCrun = 0;
void *GC_thread(void *val)
{
// Set thread name
prctl(PR_SET_NAME,"housekeeper",0,0,0);
// Save timestamp as we do not want to store immediately
// to the database
lastGCrun = time(NULL) - time(NULL)%GCinterval;
while(!killed)
{
if(time(NULL) - GCdelay - lastGCrun >= GCinterval || doGC)
{
doGC = false;
// Update lastGCrun timer
lastGCrun = time(NULL) - GCdelay - (time(NULL) - GCdelay)%GCinterval;
// Lock FTL's data structure, since it is likely that it will be changed here
// Requests should not be processed/answered when data is about to change
lock_shm();
// Get minimum time stamp to keep
time_t mintime = (time(NULL) - GCdelay) - MAXLOGAGE*3600;
// Align to the start of the next hour. This will also align with
// the oldest overTime interval after GC is done.
mintime -= mintime % 3600;
mintime += 3600;
if(config.debug & DEBUG_GC) timer_start(GC_TIMER);
long int i;
int removed = 0;
if(config.debug & DEBUG_GC) logg("GC starting, mintime: %lu %s", mintime, ctime(&mintime));
// Process all queries
for(i=0; i < counters->queries; i++)
{
validate_access("queries", i, true, __LINE__, __FUNCTION__, __FILE__);
// Test if this query is too new
if(queries[i].timestamp > mintime)
break;
// Adjust client counter
int clientID = queries[i].clientID;
validate_access("clients", clientID, true, __LINE__, __FUNCTION__, __FILE__);
clients[clientID].count--;
// Adjust total counters and total over time data
int timeidx = queries[i].timeidx;
overTime[timeidx].total--;
// Adjust corresponding overTime counters
clients[clientID].overTime[timeidx]--;
// Adjust domain counter (no overTime information)
int domainID = queries[i].domainID;
validate_access("domains", domainID, true, __LINE__, __FUNCTION__, __FILE__);
domains[domainID].count--;
// Change other counters according to status of this query
switch(queries[i].status)
{
case QUERY_UNKNOWN:
// Unknown (?)
counters->unknown--;
break;
case QUERY_FORWARDED:
// Forwarded to an upstream DNS server
counters->forwardedqueries--;
validate_access("forwarded", queries[i].forwardID, true, __LINE__, __FUNCTION__, __FILE__);
forwarded[queries[i].forwardID].count--;
overTime[timeidx].forwarded--;
break;
case QUERY_CACHE:
// Answered from local cache _or_ local config
counters->cached--;
overTime[timeidx].cached--;
break;
case QUERY_GRAVITY: // Blocked by Pi-hole's blocking lists (fall through)
case QUERY_BLACKLIST: // Exact blocked (fall through)
case QUERY_WILDCARD: // Regex blocked (fall through)
case QUERY_EXTERNAL_BLOCKED_IP: // Blocked by upstream provider (fall through)
case QUERY_EXTERNAL_BLOCKED_NXRA: // Blocked by upstream provider (fall through)
case QUERY_EXTERNAL_BLOCKED_NULL: // Blocked by upstream provider (fall through)
counters->blocked--;
overTime[timeidx].blocked--;
domains[domainID].blockedcount--;
clients[clientID].blockedcount--;
break;
default:
/* That cannot happen */
break;
}
// Update reply counters
switch(queries[i].reply)
{
case REPLY_NODATA: // NODATA(-IPv6)
counters->reply_NODATA--;
break;
case REPLY_NXDOMAIN: // NXDOMAIN
counters->reply_NXDOMAIN--;
break;
case REPLY_CNAME: // <CNAME>
counters->reply_CNAME--;
break;
case REPLY_IP: // valid IP
counters->reply_IP--;
break;
case REPLY_DOMAIN: // reverse lookup
counters->reply_domain--;
break;
default: // Incomplete query or TXT, do nothing
break;
}
// Update type counters
if(queries[i].type >= TYPE_A && queries[i].type < TYPE_MAX)
{
counters->querytype[queries[i].type-1]--;
overTime[timeidx].querytypedata[queries[i].type-1]--;
}
// Count removed queries
removed++;
}
// Move memory forward to keep only what we want
// Note: for overlapping memory blocks, memmove() is a safer approach than memcpy()
// Example: (I = now invalid, X = still valid queries, F = free space)
// Before: IIIIIIXXXXFF
// After: XXXXFFFFFFFF
memmove(&queries[0], &queries[removed], (counters->queries - removed)*sizeof(*queries));
// Update queries counter
counters->queries -= removed;
// Update DB index as total number of queries reduced
lastdbindex -= removed;
// Zero out remaining memory (marked as "F" in the above example)
memset(&queries[counters->queries], 0, (counters->queries_MAX - counters->queries)*sizeof(*queries));
// Determine if overTime memory needs to get moved
moveOverTimeMemory(mintime);
if(config.debug & DEBUG_GC) logg("Notice: GC removed %i queries (took %.2f ms)", removed, timer_elapsed_msec(GC_TIMER));
// Release thread lock
unlock_shm();
// After storing data in the database for the next time,
// we should scan for old entries, which will then be deleted
// to free up pages in the database and prevent it from growing
// ever larger and larger
DBdeleteoldqueries = true;
}
sleepms(100);
}
return NULL;
}