-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
main.c
136 lines (113 loc) · 3.41 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
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
/* 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"
#include "daemon.h"
#include "log.h"
#include "api/socket.h"
#include "setupVars.h"
#include "args.h"
#include "config.h"
#include "database/common.h"
#include "database/query-table.h"
#include "main.h"
#include "signals.h"
#include "regex_r.h"
#include "shmem.h"
#include "capabilities.h"
#include "database/gravity-db.h"
#include "timers.h"
#include "procps.h"
char * username;
bool needGC = false;
bool needDBGC = false;
bool startup = true;
volatile int exit_code = EXIT_SUCCESS;
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(false);
// Catch SIGSEGV (generate a crash report)
// Other signals are handled by dnsmasq
// We handle real-time signals later (after dnsmasq has forked)
handle_SIGSEGV();
// Initialize shared memory
if(!init_shmem(true))
{
logg("Initialization of shared memory failed.");
check_running_FTL();
return EXIT_FAILURE;
}
// Process pihole-FTL.conf
read_FTLconf();
// 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);
// Initialize query database (pihole-FTL.db)
db_init();
// Try to import queries from long-term database if available
if(config.DBimport)
DB_read_queries();
log_counter_info();
check_setupVarsconf();
// Check for availability of advanced capabilities
// immediately before starting the resolver.
check_capabilities();
// Start the resolver, delay startup if requested
delay_startup();
startup = false;
if(config.debug != 0)
{
for(int i = 0; i < argc_dnsmasq; i++)
logg("DEBUG: argv[%i] = \"%s\"", i, argv_dnsmasq[i]);
}
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 is used)
if(config.DBexport)
{
DB_save_queries();
logg("Finished final database update");
}
// Close sockets and delete Unix socket file handle
close_telnet_socket();
close_unix_socket(true);
// Empty API port file, port 0 = truncate file
saveport(0);
// Close gravity database connection
gravityDB_close();
// Remove shared memory objects
// Important: This invalidated all objects such as
// counters-> ... Do this last when
// terminating in main.c !
destroy_shmem();
//Remove PID file
removepid();
char buffer[42] = { 0 };
format_time(buffer, 0, timer_elapsed_msec(EXIT_TIMER));
logg("########## FTL terminated after%s! ##########", buffer);
return exit_code;
}