X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fchrony.c;h=079a33570070083a5d453e672995c23f975bce48;hp=a5e85a8a84b1a42e2a2eff7384027c397c3c9fb0;hb=48efd3deb4c9139fd060ff3d289896e9031bcc7c;hpb=1035fba8812893e50d00a871e3399cc1ece3b384 diff --git a/src/chrony.c b/src/chrony.c index a5e85a8a..079a3357 100644 --- a/src/chrony.c +++ b/src/chrony.c @@ -28,8 +28,8 @@ #include "collectd.h" -#include "common.h" /* auxiliary functions */ -#include "plugin.h" /* plugin_register_*, plugin_dispatch_values */ +#include "plugin.h" /* plugin_register_*, plugin_dispatch_values */ +#include "utils/common/common.h" /* auxiliary functions */ #if HAVE_NETDB_H #include /* struct addrinfo */ @@ -38,6 +38,11 @@ #include /* 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" @@ -106,7 +111,9 @@ typedef enum { #define ATTRIB_PACKED #endif -typedef struct ATTRIB_PACKED { int32_t value; } tFloat; +typedef struct ATTRIB_PACKED { + int32_t value; +} tFloat; typedef struct ATTRIB_PACKED { uint32_t tv_sec_high; @@ -143,7 +150,9 @@ typedef struct ATTRIB_PACKED { Amplification) */ } tChrony_Req_Tracking; -typedef struct ATTRIB_PACKED { uint32_t f_n_sources; } tChrony_Req_N_Sources; +typedef struct ATTRIB_PACKED { + uint32_t f_n_sources; +} tChrony_Req_N_Sources; typedef struct ATTRIB_PACKED { int32_t f_index; @@ -178,7 +187,9 @@ typedef struct ATTRIB_PACKED { } tChrony_Request; /* Chrony daemon response packets */ -typedef struct ATTRIB_PACKED { uint32_t f_n_sources; } tChrony_Resp_N_Sources; +typedef struct ATTRIB_PACKED { + uint32_t f_n_sources; +} tChrony_Resp_N_Sources; typedef struct ATTRIB_PACKED { union { @@ -348,10 +359,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 +451,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 +984,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)