Skip to content

Commit

Permalink
Merge pull request #914 from pi-hole/new/DEBUG_HELPER
Browse files Browse the repository at this point in the history
Add DEBUG_HELPER
  • Loading branch information
DL6ER authored Nov 3, 2020
2 parents 86d5b8a + 5d74f1e commit 852c6a2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ void read_debuging_settings(FILE *fp)
// defaults to: false
setDebugOption(fp, "DEBUG_EVENTS", DEBUG_EVENTS);

// DEBUG_HELPER
// defaults to: false
setDebugOption(fp, "DEBUG_HELPER", DEBUG_HELPER);

if(config.debug)
{
logg("*****************************");
Expand All @@ -739,6 +743,7 @@ void read_debuging_settings(FILE *fp)
logg("* DEBUG_EDNS0 %s *", (config.debug & DEBUG_EDNS0)? "YES":"NO ");
logg("* DEBUG_CLIENTS %s *", (config.debug & DEBUG_CLIENTS)? "YES":"NO ");
logg("* DEBUG_EVENTS %s *", (config.debug & DEBUG_EVENTS)? "YES":"NO ");
logg("* DEBUG_HELPER %s *", (config.debug & DEBUG_HELPER)? "YES":"NO ");
logg("*****************************");
}

Expand Down
12 changes: 12 additions & 0 deletions src/dnsmasq/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd)
else
continue;

/************************** Pi-hole modification **************************/
FTL_log_helper(1, action_str);
/**************************************************************************/

/* stringify MAC into dhcp_buff */
p = daemon->dhcp_buff;
Expand Down Expand Up @@ -640,6 +643,12 @@ int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd)
fcntl(event_fd, F_SETFD, i | FD_CLOEXEC);
close(pipefd[0]);

/**************************** Pi-hole modification ****************************/
FTL_log_helper(5, daemon->lease_change_command, action_str,
(is6 && data.action != ACTION_ARP) ? daemon->packet : daemon->dhcp_buff,
daemon->addrbuff, hostname);
/******************************************************************************/

p = strrchr(daemon->lease_change_command, '/');
if (err == 0)
{
Expand All @@ -650,6 +659,9 @@ int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd)
err = errno;
}
/* failed, send event so the main process logs the problem */
/**************************** Pi-hole modification ****************************/
FTL_log_helper(2, daemon->lease_change_command, strerror(err));
/******************************************************************************/
send_event(event_fd, EVENT_EXEC_ERR, err, NULL);
_exit(0);
}
Expand Down
1 change: 1 addition & 0 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ enum debug_flags {
DEBUG_EDNS0 = (1 << 16), /* 00000000 00000001 00000000 00000000 */
DEBUG_CLIENTS = (1 << 17), /* 00000000 00000010 00000000 00000000 */
DEBUG_EVENTS = (1 << 18), /* 00000000 00000100 00000000 00000000 */
DEBUG_HELPER = (1 << 19), /* 00000000 00001000 00000000 00000000 */
} __attribute__ ((packed));

enum events {
Expand Down
38 changes: 38 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,44 @@ void _FTL_log(const bool newline, const char *format, ...)
pthread_mutex_unlock(&lock);
}

// Log helper activity (may be script or lua)
void FTL_log_helper(const unsigned char n, ...)
{
// Only log helper debug messages if enabled
if(!(config.debug & DEBUG_HELPER))
return;

// Extract all variable arguments
va_list args;
char *arg[n];
va_start(args, n);
for(unsigned char i = 0; i < n; i++)
arg[i] = strdup(va_arg(args, char*));
va_end(args);

// Select appropriate logging format
switch (n)
{
case 1:
logg("Script: Starting helper for action \"%s\"", arg[0]);
break;
case 2:
logg("Script: FAILED to execute \"%s\": %s", arg[0], arg[1]);
break;
case 5:
logg("Script: Executing \"%s\" with arguments: \"%s %s %s %s\"",
arg[0], arg[1], arg[2], arg[3], arg[4]);
break;
default:
logg("ERROR: Unsupported number of arguments passed to FTL_log_helper(): %u", n);
break;
}

// Free allocated memory
for(unsigned char i = 0; i < n; i++)
free(arg[i]);
}

void format_memory_size(char * const prefix, const unsigned long long int bytes,
double * const formated)
{
Expand Down
1 change: 1 addition & 0 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ const char *get_ordinal_suffix(unsigned int number) __attribute__ ((const));
#define logg_sameline(format, ...) _FTL_log(false, format, ## __VA_ARGS__)
void _FTL_log(const bool newline, const char* format, ...) __attribute__ ((format (gnu_printf, 2, 3)));
void log_ctrl(bool vlog, bool vstdout);
void FTL_log_helper(const unsigned char n, ...);

#endif //LOG_H

0 comments on commit 852c6a2

Please sign in to comment.