src/daemon/common.c: avoid leaking cap_header in error condition
[collectd.git] / src / chrony.c
index a8f6f62..0485036 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 #include "collectd.h"
+
 #include "common.h"             /* auxiliary functions */
 #include "plugin.h"             /* plugin_register_*, plugin_dispatch_values */
 
@@ -105,10 +106,11 @@ typedef enum
 } eDaemonReplies;
 
 
-#if defined(__GNUC__) || defined(__clang__)
+#if defined(__GNUC__) || defined (__SUNPRO_C) || defined(lint)
+#  /* extension to enforce struct packing. */
 #  define ATTRIB_PACKED __attribute__((packed))
 #else
-#  warning Not defining packed attribute (unknown compiler)
+#  error Not defining packed attribute (unknown compiler)
 #  define ATTRIB_PACKED 
 #endif
 
@@ -305,15 +307,15 @@ static int
 connect_client(const char *p_hostname,
                const char *p_service, int p_family, int p_socktype)
 {
-  struct addrinfo hints, *res = NULL, *ressave = NULL;
+  struct addrinfo *res, *ressave;
   int n, sockfd;
 
-  memset(&hints, 0, sizeof(struct addrinfo));
-
-  hints.ai_family = p_family;
-  hints.ai_socktype = p_socktype;
+  struct addrinfo ai_hints = {
+    .ai_family = p_family,
+    .ai_socktype = p_socktype
+  };
 
-  n = getaddrinfo(p_hostname, p_service, &hints, &res);
+  n = getaddrinfo(p_hostname, p_service, &ai_hints, &res);
 
   if (n < 0)
   {
@@ -355,7 +357,6 @@ niptoha(const tChrony_IPAddr * addr, char *p_buf, size_t p_buf_size)
 {
   int rc = 1;
   unsigned long a, b, c, d, ip;
-  const uint8_t *ip6;
 
   switch (ntohs(addr->f_family))
   {
@@ -372,33 +373,12 @@ niptoha(const tChrony_IPAddr * addr, char *p_buf, size_t p_buf_size)
     break;
   case IPADDR_INET6:
   {
-    ip6 = addr->addr.ip6;
-
-#if 1
-    /* XXX: Use feature test macro? */
-    const char *rp = inet_ntop(AF_INET6, ip6, p_buf, p_buf_size);
+    const char *rp = inet_ntop(AF_INET6, addr->addr.ip6, p_buf, p_buf_size);
     if (rp == NULL)
     {
       ERROR(PLUGIN_NAME ": Error converting ipv6 address to string. Errno = %d", errno);
       rc = snprintf(p_buf, p_buf_size, "[UNKNOWN]");
     }
-#else
-#  if defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
-    rc =
-      snprintf(p_buf, p_buf_size,
-               "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
-               ip6[15], ip6[14], ip6[13], ip6[12], ip6[11], ip6[10], ip6[9],
-               ip6[8], ip6[7], ip6[6], ip6[5], ip6[4], ip6[3], ip6[2],
-               ip6[1], ip6[0]);
-#  else
-    rc =
-      snprintf(p_buf, p_buf_size,
-               "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
-               ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6],
-               ip6[7], ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13],
-               ip6[14], ip6[15]);
-#  endif
-#endif
     break;
   }
   default:
@@ -410,7 +390,7 @@ niptoha(const tChrony_IPAddr * addr, char *p_buf, size_t p_buf_size)
 
 
 static int
-chrony_set_timeout()
+chrony_set_timeout(void)
 {
   /* Set the socket's  timeout to g_chrony_timeout; a value of 0 signals infinite timeout */
   /* Returns 0 on success, !0 on error (check errno) */
@@ -430,7 +410,7 @@ chrony_set_timeout()
 
 
 static int
-chrony_connect()
+chrony_connect(void)
 {
   /* Connects to the chrony daemon */
   /* Returns 0 on success, !0 on error (check errno) */
@@ -571,9 +551,7 @@ chrony_query(const int p_command, tChrony_Request * p_req,
     }
 
     if (valid_command == 0)
-    {
       break;
-    }
 
     uint32_t seq_nr = rand_r(&g_chrony_rand);
     p_req->header.f_cmd = htons(p_command);
@@ -583,15 +561,12 @@ chrony_query(const int p_command, tChrony_Request * p_req,
     DEBUG(PLUGIN_NAME ": Sending request (.cmd = %d, .seq = %d)", p_command,
           seq_nr);
     if (chrony_send_request(p_req, req_size) != 0)
-    {
       break;
-    }
 
     DEBUG(PLUGIN_NAME ": Waiting for response");
     if (chrony_recv_response(p_resp, resp_size, p_resp_size) != 0)
-    {
       break;
-    }
+
     DEBUG(PLUGIN_NAME
           ": Received response: .version = %u, .type = %u, .cmd = %u, .reply = %u, .status = %u, .seq = %u",
           p_resp->header.f_version, p_resp->header.f_type,
@@ -688,22 +663,19 @@ ntohf(tFloat p_float)
   uval = ntohl(p_float.value);
   exp = (uval >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
   if (exp >= 1 << (FLOAT_EXP_BITS - 1))
-  {
     exp -= 1 << FLOAT_EXP_BITS;
-  }
 
   /* coef = (x << FLOAT_EXP_BITS) >> FLOAT_EXP_BITS; */
   coef = uval % (1U << FLOAT_COEF_BITS);
   if (coef >= 1 << (FLOAT_COEF_BITS - 1))
-  {
     coef -= 1 << FLOAT_COEF_BITS;
-  }
+
   return coef * pow(2.0, exp);
 }
 
 
 static void
-chrony_push_data(char *p_type, char *p_type_inst, double p_value)
+chrony_push_data(const char *p_type, const char *p_type_inst, double p_value)
 {
   value_t values[1];
   value_list_t vl = VALUE_LIST_INIT;
@@ -724,39 +696,37 @@ chrony_push_data(char *p_type, char *p_type_inst, double p_value)
              sizeof(vl.plugin_instance));
   }
   if (p_type != NULL)
-  {
     sstrncpy(vl.type, p_type, sizeof(vl.type));
-  }
+
   if (p_type_inst != NULL)
-  {
     sstrncpy(vl.type_instance, p_type_inst, sizeof(vl.type_instance));
-  }
 
   plugin_dispatch_values(&vl);
 }
 
 
 static void
-chrony_push_data_valid(char *p_type, char *p_type_inst, const int p_is_valid,
+chrony_push_data_valid(const char *p_type, const char *p_type_inst, const int p_is_valid,
                        double p_value)
 {
   /* Push real value if p_is_valid is true, push NAN if p_is_valid is not true (idea from ntp plugin) */
   if (p_is_valid == 0)
-  {
     p_value = NAN;
-  }
+
   chrony_push_data(p_type, p_type_inst, p_value);
 }
 
 
 static int
-chrony_init_seq()
+chrony_init_seq(void)
 {
   /* Initialize the sequence number generator from /dev/urandom */
   /* Fallbacks: /dev/random and time(NULL) */
 
+  int fh;
+
   /* Try urandom */
-  int fh = open(URAND_DEVICE_PATH, O_RDONLY);
+  fh = open(URAND_DEVICE_PATH, O_RDONLY);
   if (fh >= 0)
   {
     ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
@@ -775,7 +745,7 @@ chrony_init_seq()
     if (errno == ENOENT)
     {
       /* URAND_DEVICE_PATH device not found. Try RAND_DEVICE_PATH as fall-back */
-      int fh = open(RAND_DEVICE_PATH, O_RDONLY);
+      fh = open(RAND_DEVICE_PATH, O_RDONLY);
       if (fh >= 0)
       {
         ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
@@ -822,9 +792,8 @@ chrony_config(const char *p_key, const char *p_value)
   if (strcasecmp(p_key, CONFIG_KEY_HOST) == 0)
   {
     if (g_chrony_host != NULL)
-    {
       free(g_chrony_host);
-    }
+
     if ((g_chrony_host = strdup(p_value)) == NULL)
     {
       ERROR(PLUGIN_NAME ": Error duplicating host name");
@@ -836,9 +805,8 @@ chrony_config(const char *p_key, const char *p_value)
     if (strcasecmp(p_key, CONFIG_KEY_PORT) == 0)
     {
       if (g_chrony_port != NULL)
-      {
         free(g_chrony_port);
-      }
+
       if ((g_chrony_port = strdup(p_value)) == NULL)
       {
         ERROR(PLUGIN_NAME ": Error duplicating port name");
@@ -866,7 +834,7 @@ chrony_config(const char *p_key, const char *p_value)
 
 
 static int
-chrony_request_daemon_stats()
+chrony_request_daemon_stats(void)
 {
   /* Perform Tracking request */
   int rc;
@@ -885,8 +853,7 @@ chrony_request_daemon_stats()
   }
 #if COLLECT_DEBUG
   {
-    char src_addr[IPV6_STR_MAX_SIZE];
-    memset(src_addr, 0, sizeof(src_addr));
+    char src_addr[IPV6_STR_MAX_SIZE] = { 0 };
     niptoha(&chrony_resp.body.tracking.addr, src_addr, sizeof(src_addr));
     DEBUG(PLUGIN_NAME ": Daemon stat: .addr = %s, .ref_id= %u, .stratum = %u, .leap_status = %u, .ref_time = %u:%u:%u, .current_correction = %f, .last_offset = %f, .rms_offset = %f, .freq_ppm = %f, .skew_ppm = %f, .root_delay = %f, .root_dispersion = %f, .last_update_interval = %f", src_addr, ntohs(chrony_resp.body.tracking.f_ref_id),  
           ntohs(chrony_resp.body.tracking.f_stratum),
@@ -970,8 +937,7 @@ chrony_request_source_data(int p_src_idx, int *p_is_reachable)
   tChrony_Request chrony_req;
   tChrony_Response chrony_resp;
 
-  char src_addr[IPV6_STR_MAX_SIZE];
-  memset(src_addr, 0, sizeof(src_addr));
+  char src_addr[IPV6_STR_MAX_SIZE] = { 0 };
 
   chrony_init_req(&chrony_req);
   chrony_req.body.source_data.f_index = htonl(p_src_idx);
@@ -1030,8 +996,7 @@ chrony_request_source_stats(int p_src_idx, const int *p_is_reachable)
   tChrony_Response chrony_resp;
   double skew_ppm, frequency_error, time_offset;
 
-  char src_addr[IPV6_STR_MAX_SIZE];
-  memset(src_addr, 0, sizeof(src_addr));
+  char src_addr[IPV6_STR_MAX_SIZE] = { 0 };
 
   if (*p_is_reachable == 0)
   {
@@ -1084,58 +1049,50 @@ chrony_request_source_stats(int p_src_idx, const int *p_is_reachable)
 
 
 static int
-chrony_read()
+chrony_read(void)
 {
   /* collectd read callback: Perform data acquisition */
   int rc;
-  unsigned int now_src, n_sources;
+  unsigned int n_sources;
 
   if (g_chrony_seq_is_initialized == 0)
   {
     /* Seed RNG for sequence number generation */
     rc = chrony_init_seq();
     if (rc != CHRONY_RC_OK)
-    {
       return rc;
-    }
+
     g_chrony_seq_is_initialized = 1;
   }
 
   /* Get daemon stats */
   rc = chrony_request_daemon_stats();
   if (rc != CHRONY_RC_OK)
-  {
     return rc;
-  }
 
   /* Get number of time sources, then check every source for status */
   rc = chrony_request_sources_count(&n_sources);
   if (rc != CHRONY_RC_OK)
-  {
     return rc;
-  }
 
-  for (now_src = 0; now_src < n_sources; ++now_src)
+  for (unsigned int now_src = 0; now_src < n_sources; ++now_src)
   {
     int is_reachable;
     rc = chrony_request_source_data(now_src, &is_reachable);
     if (rc != CHRONY_RC_OK)
-    {
       return rc;
-    }
 
     rc = chrony_request_source_stats(now_src, &is_reachable);
     if (rc != CHRONY_RC_OK)
-    {
       return rc;
-    }
+
   }
   return CHRONY_RC_OK;
 }
 
 
 static int
-chrony_shutdown()
+chrony_shutdown(void)
 {
   /* Collectd shutdown callback: Free mem */
   if (g_chrony_is_connected != 0)
@@ -1144,17 +1101,14 @@ chrony_shutdown()
     g_chrony_is_connected = 0;
   }
   if (g_chrony_host != NULL)
-  {
     sfree(g_chrony_host);
-  }
+
   if (g_chrony_port != NULL)
-  {
     sfree(g_chrony_port);
-  }
+  
   if (g_chrony_plugin_instance != NULL)
-  {
     sfree(g_chrony_plugin_instance);
-  }
+  
   return CHRONY_RC_OK;
 }