Skip to content

Commit

Permalink
Merge branch 'development' into ftldns/remove_old_debug_options
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <dl6er@dl6er.de>

Conflicts:
	args.c
  • Loading branch information
DL6ER committed May 12, 2018
2 parents 24b9809 + d5e57f0 commit 5c38d32
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ extern unsigned char blockingstatus;
extern char ** wildcarddomains;

extern memoryStruct memory;
extern bool runtest;

extern char * username;
extern char timestamp[16];
extern bool flush;
extern bool needGC;
extern bool daemonmode;
extern bool database;
extern long int lastdbindex;
extern bool travis;
Expand Down
10 changes: 4 additions & 6 deletions args.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "version.h"

bool debug = false;
bool runtest = false;
bool daemonmode = true;
bool travis = false;
int argc_dnsmasq = 0;
char **argv_dnsmasq = NULL;
Expand Down Expand Up @@ -82,11 +82,10 @@ void parse_args(int argc, char* argv[])
}

// Don't go into background
// This is a no-op but we still support it
// for backwards compatibility
if(strcmp(argv[i], "-f") == 0 ||
strcmp(argv[i], "no-daemon") == 0)
{
daemonmode = false;
ok = true;
}

Expand Down Expand Up @@ -145,9 +144,8 @@ void parse_args(int argc, char* argv[])
printf("\t-v, version Return version\n");
printf("\t-t, tag Return git tag\n");
printf("\t-b, branch Return git branch\n");
printf("\t process is running and exit\n");
printf("\t even if not (instead of\n");
printf("\t starting a new one)\n");
printf("\t-f, no-daemon Don't go into daemon mode\n");
printf("\t-h, help Display this help and exit\n");
printf("\tdnsmasq-test Test syntax of dnsmasq's\n");
printf("\t config files and exit\n");
printf("\n\nOnline help: https://github.com/pi-hole/FTL\n");
Expand Down
69 changes: 68 additions & 1 deletion daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,77 @@
* Please see LICENSE file for your rights under this license. */

#include "FTL.h"
#include <dirent.h>

struct timeval t0[NUMTIMERS];

void go_daemon(void)
{
pid_t process_id = 0;
pid_t sid = 0;

// Create child process
process_id = fork();

// Indication of fork() failure
if (process_id < 0)
{
logg("fork failed!\n");
// Return failure in exit status
exit(EXIT_FAILURE);
}

// PARENT PROCESS. Need to kill it.
if (process_id > 0)
{
printf("FTL started!\n");
// return success in exit status
exit(EXIT_SUCCESS);
}

//unmask the file mode
umask(0);

//set new session
// creates a session and sets the process group ID
sid = setsid();
if(sid < 0)
{
// Return failure
logg("setsid failed!\n");
exit(EXIT_FAILURE);
}

// Create grandchild process
// Fork a second child and exit immediately to prevent zombies. This
// causes the second child process to be orphaned, making the init
// process responsible for its cleanup. And, since the first child is
// a session leader without a controlling terminal, it's possible for
// it to acquire one by opening a terminal in the future (System V-
// based systems). This second fork guarantees that the child is no
// longer a session leader, preventing the daemon from ever acquiring
// a controlling terminal.
process_id = fork();

// Indication of fork() failure
if (process_id < 0)
{
logg("fork failed!\n");
// Return failure in exit status
exit(EXIT_FAILURE);
}

// PARENT PROCESS. Need to kill it.
if (process_id > 0)
{
// return success in exit status
exit(EXIT_SUCCESS);
}

savepid();

// Closing stdin, stdout and stderr is handled by dnsmasq
}

void timer_start(int i)
{
if(i >= NUMTIMERS)
Expand Down
17 changes: 9 additions & 8 deletions datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,18 @@ int detectStatus(const char *domain)
// Note that this is a really expensive subroutine and trying to match
// blocked domains against all configured wildcards will take some time
int i;

// Return early if no wildcard domains are defined
if(counters.wildcarddomains < 1)
return QUERY_CACHE;

validate_access("wildcarddomains", counters.wildcarddomains-1, false, __LINE__, __FUNCTION__, __FILE__);
for(i=0; i < counters.wildcarddomains; i++)
{
if(strcasecmp(wildcarddomains[i], domain) == 0)
{
// Exact match with wildcard domain
// if(debug)
// printf("%s / %s (exact wildcard match)\n",wildcarddomains[i], domain);
return 4;
return QUERY_WILDCARD;
}
// Create copy of domain under investigation
char * part = strdup(domain);
Expand All @@ -274,13 +277,11 @@ int detectStatus(const char *domain)
// Test for a match
if(strcasecmp(wildcarddomains[i], partbuffer) == 0)
{
// Free allocated memory before return'ing
// Free allocated memory before returning
free(part);
free(partbuffer);
// Return match with wildcard domain
// if(debug)
// printf("%s / %s (wildcard match)\n",wildcarddomains[i], partbuffer);
return 4;
return QUERY_WILDCARD;
}
if(strlen(partbuffer) > 0)
{
Expand All @@ -299,5 +300,5 @@ int detectStatus(const char *domain)
// wildcard blocking, but from e.g. an
// address=// configuration
// Answer as "cached"
return 3;
return QUERY_CACHE;
}
2 changes: 1 addition & 1 deletion dnsmasq/dnsmasq.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ int main_dnsmasq (int argc, char **argv)
}
}

FTL_start_threads();
FTL_fork_and_bind_sockets();

log_err = log_start(ent_pw, err_pipe[1]);

Expand Down
8 changes: 5 additions & 3 deletions dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,12 @@ pthread_t socket_listenthread;
pthread_t DBthread;
pthread_t GCthread;

void FTL_start_threads(void)
void FTL_fork_and_bind_sockets(void)
{
// Save PID
savepid();
if(!debug && daemonmode)
go_daemon();
else
savepid();

// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
Expand Down
2 changes: 1 addition & 1 deletion dnsmasq_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void FTL_reply(unsigned short flags, char *name, struct all_addr *addr, int id);
void FTL_cache(unsigned int flags, char *name, struct all_addr *addr, char * arg, int id);
void FTL_dnssec(int status, int id);
void FTL_dnsmasq_reload(void);
void FTL_start_threads(void);
void FTL_fork_and_bind_sockets(void);

void FTL_forwarding_failed(struct server *server);
int FTL_listsfile(char* filename, unsigned int index, FILE *f, int cache_size, struct crec **rhash, int hashsz);
4 changes: 2 additions & 2 deletions socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ void *telnet_connection_handler_thread(void *socket_desc)
// Set connection type to telnet
istelnet[sock] = true;

int sockID = sock;
// Define buffer for client's message
char client_message[SOCKETBUFFERLEN] = "";

// Set thread name
char threadname[16];
sprintf(threadname,"telnet-%i",sockID);
sprintf(threadname,"telnet-%i",sock);
prctl(PR_SET_NAME,threadname,0,0,0);
//Receive from client
ssize_t n;
Expand Down

0 comments on commit 5c38d32

Please sign in to comment.