Used faster access() to check for file existence and more secure snprintf() instead of sprintf().
Change-Id: I8421bef12e3dcf57b23afeaf387bd568196cb85c
diff --git a/src/forward.c b/src/forward.c
index 8004340..829f9d2 100644
--- a/src/forward.c
+++ b/src/forward.c
@@ -551,14 +551,7 @@
else bucket = TIMING_BUCKET_500_MILLIS;
daemon->forward_timings[bucket]++;
- int DNS_timing_log_active = 0;
- FILE *fp = fopen("/config/experiments/DNSTimingLog.active", "r");
- if (fp != NULL)
- {
- DNS_timing_log_active = 1;
- fclose(fp);
- }
- if (DNS_timing_log_active)
+ if (access("/config/experiments/DNSTimingLog.active", R_OK) == 0)
{
prettyprint_addr(&server->addr, daemon->namebuff);
my_syslog(LOG_INFO, _("upstream server %s handled request in %.2f ms"), daemon->namebuff, milliseconds);
@@ -1318,31 +1311,21 @@
log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
(struct all_addr *)&source_addr.in6.sin6_addr, types);
#endif
- int cache_warming_active = 0;
- FILE *fp = fopen("/config/experiments/CacheWarming.active", "r");
- if (fp != NULL)
- {
- cache_warming_active = 1;
- fclose(fp);
- }
- if (strcmp("127.0.0.1", daemon->addrbuff) != 0 && cache_warming_active)
+ if (strcmp("127.0.0.1", daemon->addrbuff) != 0 &&
+ access("/config/experiments/CacheWarming.active", R_OK) == 0)
{
int sock;
if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
{
- my_syslog(LOG_ERR, "failed to create socket: %s", strerror(errno));
+ my_syslog(LOG_ERR, "failed to create query log socket: %s", strerror(errno));
}
struct sockaddr_un server_address = {AF_UNIX, "/tmp/dns_query_log_socket"};
char msg[128];
- sprintf(msg, "%lu %s", now, daemon->namebuff);
+ snprintf(msg, sizeof(msg), "%lu %s", now, daemon->namebuff);
if (sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)&server_address,
sizeof(server_address)) < 0)
{
- my_syslog(LOG_ERR, "failed to send message on socket: %s", strerror(errno));
- }
- if (close(sock) < 0)
- {
- my_syslog(LOG_ERR, "failed to close socket: %s", strerror(errno));
+ my_syslog(LOG_ERR, "failed to send message on query log socket: %s", strerror(errno));
}
}
#ifdef HAVE_AUTH
@@ -1388,14 +1371,7 @@
(char *)header, m, &source_addr, &dst_addr, if_index);
daemon->local_answer++;
- int DNS_timing_log_active = 0;
- FILE *fp = fopen("/config/experiments/DNSTimingLog.active", "r");
- if (fp != NULL)
- {
- DNS_timing_log_active = 1;
- fclose(fp);
- }
- if (DNS_timing_log_active)
+ if (access("/config/experiments/DNSTimingLog.active", R_OK) == 0)
{
double milliseconds = ((double)(dnsmasq_time64() - now64))/1000000;
my_syslog(LOG_INFO, _("cache response handled request in %.2f ms"), milliseconds);