Skip to content

Commit

Permalink
Merge pull request #1243 from pi-hole/tweak/warning_messages
Browse files Browse the repository at this point in the history
Add dnsmasq warnings to message table
  • Loading branch information
DL6ER authored Nov 14, 2021
2 parents 6a53765 + bd373c5 commit 827b55a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/database/message-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "../gc.h"

static const char *message_types[MAX_MESSAGE] =
{ "REGEX", "SUBNET", "HOSTNAME", "DNSMASQ_CONFIG", "RATE_LIMIT" };
{ "REGEX", "SUBNET", "HOSTNAME", "DNSMASQ_CONFIG", "RATE_LIMIT" , "DNSMASQ_WARN" };

static unsigned char message_blob_types[MAX_MESSAGE][5] =
{
Expand Down Expand Up @@ -66,6 +66,13 @@ static unsigned char message_blob_types[MAX_MESSAGE][5] =
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
{ // DNSMASQ_WARN_MESSAGE: The message column contains the full message itself
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL, // Not used
SQLITE_NULL // Not used
},
};
// Create message table in the database
bool create_message_table(sqlite3 *db)
Expand Down Expand Up @@ -337,3 +344,12 @@ void logg_rate_limit_message(const char *clientIP, const unsigned int rate_limit
// Log to database
add_message(RATE_LIMIT_MESSAGE, clientIP, 2, config.rate_limit.count, config.rate_limit.interval);
}

void logg_warn_dnsmasq_message(char *message)
{
// Log to pihole-FTL.log
logg("WARNING in dnsmasq core: %s", message);

// Log to database
add_message(DNSMASQ_WARN_MESSAGE, message, 0);
}
1 change: 1 addition & 0 deletions src/database/message-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ void logg_subnet_warning(const char *ip, const int matching_count, const char *m
void logg_hostname_warning(const char *ip, const char *name, const unsigned int pos);
void logg_fatal_dnsmasq_message(const char *message);
void logg_rate_limit_message(const char *clientIP, const unsigned int rate_limit_count);
void logg_warn_dnsmasq_message(char *message);

#endif //MESSAGETABLE_H
5 changes: 0 additions & 5 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include "regex_r.h"
// reload_per_client_regex()
#include "database/gravity-db.h"
// flush_message_table()
#include "database/message-table.h"
// bool startup
#include "main.h"
// reset_aliasclient()
Expand Down Expand Up @@ -477,9 +475,6 @@ void FTL_reload_all_domainlists(void)
{
lock_shm();

// Flush messages stored in the long-term database
flush_message_table();

// (Re-)open gravity database connection
gravityDB_reopen();

Expand Down
13 changes: 13 additions & 0 deletions src/dnsmasq/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ void my_syslog(int priority, const char *format, ...)
fputc('\n', stderr);
}

/* Pi-hole diagnosis system */
if(priority == LOG_WARNING)
{
char *message;
va_start(ap, format);
if(vasprintf(&message, format, ap))
{
dnsmasq_diagnosis_warning(message);
free(message);
}
va_end(ap);
}

if (log_fd == -1)
{
#ifdef __ANDROID__
Expand Down
1 change: 1 addition & 0 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ enum message_type {
HOSTNAME_MESSAGE,
DNSMASQ_CONFIG_MESSAGE,
RATE_LIMIT_MESSAGE,
DNSMASQ_WARN_MESSAGE,
MAX_MESSAGE
} __attribute__ ((packed));

Expand Down
16 changes: 16 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,19 @@ void print_FTL_version(void)
{
printf("Pi-hole FTL %s\n", get_FTL_version());
}

// Skip leading string if found
static char *skipStr(const char *startstr, char *message)
{
const size_t startlen = strlen(startstr);
if(strncmp(startstr, message, startlen) == 0)
return message + startlen;
else
return message;
}

void dnsmasq_diagnosis_warning(char *message)
{
// Crop away any existing initial "warning: "
logg_warn_dnsmasq_message(skipStr("warning: ", message));
}
1 change: 1 addition & 0 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void log_FTL_version(bool crashreport);
void get_timestr(char * const timestring, const time_t timein, const bool millis);
const char *get_ordinal_suffix(unsigned int number) __attribute__ ((const));
void print_FTL_version(void);
void dnsmasq_diagnosis_warning(char *message);

// The actual logging routine can take extra options for specialized logging
// The more general interfaces can be defined here as appropriate shortcuts
Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "procps.h"
// init_overtime()
#include "overTime.h"
// flush_message_table()
#include "database/message-table.h"

char * username;
bool needGC = false;
Expand Down Expand Up @@ -86,6 +88,9 @@ int main (int argc, char* argv[])
// Initialize query database (pihole-FTL.db)
db_init();

// Flush messages stored in the long-term database
flush_message_table();

// Try to import queries from long-term database if available
if(config.DBimport)
DB_read_queries();
Expand Down

0 comments on commit 827b55a

Please sign in to comment.