X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fchrony.c;h=77ba8cc460564d42ed6b8794080a36234e3c0199;hb=c5ffa797883f238776e6294054f260df3cd0aea6;hp=4554f4990e36183e6e45eab3ce2b6cf462dc7672;hpb=ba6141349270c4ca127e9e0e6e83fc3c6460cf5c;p=collectd.git diff --git a/src/chrony.c b/src/chrony.c index 4554f499..77ba8cc4 100644 --- a/src/chrony.c +++ b/src/chrony.c @@ -26,6 +26,7 @@ */ #include "collectd.h" + #include "common.h" /* auxiliary functions */ #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */ @@ -105,8 +106,8 @@ typedef enum } eDaemonReplies; -#if defined(__GNUC__) -# /* GNU gcc extension to enforce struct packing. */ +#if defined(__GNUC__) || defined (__SUNPRO_C) || defined(lint) +# /* extension to enforce struct packing. */ # define ATTRIB_PACKED __attribute__((packed)) #else # error Not defining packed attribute (unknown compiler) @@ -306,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) { @@ -389,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) */ @@ -409,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) */ @@ -451,8 +452,8 @@ chrony_connect() if (chrony_set_timeout()) { - ERROR(PLUGIN_NAME ": Error setting timeout to %lds. Errno = %d", - g_chrony_timeout, errno); + ERROR(PLUGIN_NAME ": Error setting timeout to %llds. Errno = %d", + (long long)g_chrony_timeout, errno); return CHRONY_RC_FAIL; } return CHRONY_RC_OK; @@ -674,20 +675,14 @@ ntohf(tFloat p_float) 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; - values[0].gauge = p_value; /* TODO: Check type??? (counter, gauge, derive, absolute) */ - - vl.values = values; + vl.values = &(value_t) { .gauge = p_value }; vl.values_len = 1; /* XXX: Shall g_chrony_host/g_chrony_port be reflected in the plugin's output? */ - /* hostname_g is set in daemon/collectd.c (from config, via gethostname or by resolving localhost) */ - /* defined as: char hostname_g[DATA_MAX_NAME_LEN]; (never NULL) */ - sstrncpy(vl.host, hostname_g, sizeof(vl.host)); sstrncpy(vl.plugin, PLUGIN_NAME_SHORT, sizeof(vl.plugin)); if (g_chrony_plugin_instance != NULL) { @@ -705,7 +700,7 @@ chrony_push_data(char *p_type, char *p_type_inst, double p_value) 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) */ @@ -717,13 +712,15 @@ chrony_push_data_valid(char *p_type, char *p_type_inst, const int p_is_valid, 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)); @@ -742,7 +739,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)); @@ -831,7 +828,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; @@ -850,8 +847,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), @@ -935,8 +931,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); @@ -995,8 +990,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) { @@ -1049,11 +1043,11 @@ 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) { @@ -1075,7 +1069,7 @@ chrony_read() 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); @@ -1092,7 +1086,7 @@ chrony_read() static int -chrony_shutdown() +chrony_shutdown(void) { /* Collectd shutdown callback: Free mem */ if (g_chrony_is_connected != 0)