forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
94 lines (74 loc) · 2.3 KB
/
main.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
/* 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
* Core routine
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
char * username;
bool needGC = false;
bool needDBGC = false;
// Prototype
int main_dnsmasq(int argc, char **argv);
int main (int argc, char* argv[])
{
// Get user pihole-FTL is running as
// We store this in a global variable
// such that the log routine can access
// it if needed
username = getUserName();
// Parse arguments
// We run this also for no direct arguments
// to have arg{c,v}_dnsmasq initialized
parse_args(argc, argv);
// Try to open FTL log
open_FTL_log(true);
timer_start(EXIT_TIMER);
logg("########## FTL started! ##########");
log_FTL_version();
init_thread_lock();
// pihole-FTL should really be run as user "pihole" to not mess up with file permissions
// print warning otherwise
if(strcmp(username, "pihole") != 0)
logg("WARNING: Starting pihole-FTL as user %s is not recommended", username);
// Process pihole-FTL.conf
read_FTLconf();
// Read and compile possible regex filters
read_regex_from_file();
// Catch signals like SIGTERM and SIGINT
// Other signals like SIGHUP, SIGUSR1 are handled by the resolver part
handle_signals();
// Initialize database
if(config.maxDBdays != 0)
db_init();
// Try to import queries from long-term database if available
if(database && config.DBimport)
read_data_from_DB();
log_counter_info();
check_setupVarsconf();
// Preparations done - start the resolver
main_dnsmasq(argc_dnsmasq, argv_dnsmasq);
logg("Shutting down...");
// 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)
{
save_to_DB();
logg("Finished final database update");
}
// Close sockets
close_telnet_socket();
close_unix_socket();
// Invalidate blocking regex if compiled
free_regex();
//Remove PID file
removepid();
logg("########## FTL terminated after %.1f ms! ##########", timer_elapsed_msec(EXIT_TIMER));
return 1;
}