Merge branch 'collectd-5.8'
[collectd.git] / src / chrony.c
index a5e85a8..913aab9 100644 (file)
 #include <arpa/inet.h> /* ntohs/ntohl */
 #endif
 
+/* AIX doesn't have MSG_DONTWAIT */
+#ifndef MSG_DONTWAIT
+#define MSG_DONTWAIT MSG_NONBLOCK
+#endif
+
 #define CONFIG_KEY_HOST "Host"
 #define CONFIG_KEY_PORT "Port"
 #define CONFIG_KEY_TIMEOUT "Timeout"
@@ -348,10 +353,10 @@ static char *niptoha(const tChrony_IPAddr *addr, char *p_buf,
 }
 
 static void nreftostr(uint32_t nrefid, char *p_buf, size_t p_buf_size) {
-  int i, j, c;
+  size_t j = 0;
 
-  for (i = j = 0; i < 4; i++) {
-    c = ntohl(nrefid) << i * 8 >> 24;
+  for (int i = 0; i < 4; i++) {
+    int c = ntohl(nrefid) << i * 8 >> 24;
     if (!isalnum(c) || j + 1 >= p_buf_size)
       continue;
     p_buf[j++] = c;
@@ -440,6 +445,15 @@ static int chrony_recv_response(tChrony_Response *p_resp,
   }
 }
 
+static void chrony_flush_recv_queue(void) {
+  char buf[1];
+
+  if (g_chrony_is_connected) {
+    while (recv(g_chrony_socket, buf, sizeof(buf), MSG_DONTWAIT) > 0)
+      ;
+  }
+}
+
 static int chrony_query(const int p_command, tChrony_Request *p_req,
                         tChrony_Response *p_resp, size_t *p_resp_size) {
   /* Check connection. We simply perform one try as collectd already handles
@@ -964,6 +978,9 @@ static int chrony_read(void) {
     g_chrony_seq_is_initialized = 1;
   }
 
+  /* Ignore late responses that may have been received */
+  chrony_flush_recv_queue();
+
   /* Get daemon stats */
   rc = chrony_request_daemon_stats();
   if (rc != CHRONY_RC_OK)