X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fntpd.c;h=baa1988b26596665b0a5936042de5ecd9a113f80;hp=bc81cdd8ad47f059748d4b3e239e69e28e66450a;hb=d486225f89ea52d8ed2b4242eba2ad94c409f837;hpb=35a6c9c5fcd87cb78451a974c4d5b5707926845c diff --git a/src/ntpd.c b/src/ntpd.c index bc81cdd8..baa1988b 100644 --- a/src/ntpd.c +++ b/src/ntpd.c @@ -32,9 +32,6 @@ #include "common.h" #include "plugin.h" -#if HAVE_STDINT_H -#include -#endif #if HAVE_NETDB_H #include #endif @@ -59,17 +56,17 @@ static const char *config_keys[] = {"Host", "Port", "ReverseLookups", "IncludeUnitID"}; static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); -static _Bool do_reverse_lookups = 1; +static bool do_reverse_lookups = true; /* This option only exists for backward compatibility. If it is false and two * ntpd peers use the same refclock driver, the plugin will try to write * simultaneous measurements from both to the same type instance. */ -static _Bool include_unit_id = 0; +static bool include_unit_id; #define NTPD_DEFAULT_HOST "localhost" #define NTPD_DEFAULT_PORT "123" static int sock_descr = -1; -static char *ntpd_host = NULL; +static char *ntpd_host; static char ntpd_port[16]; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -266,28 +263,28 @@ static int ntpd_config(const char *key, const char *value) { if (ntpd_host != NULL) free(ntpd_host); if ((ntpd_host = strdup(value)) == NULL) - return (1); + return 1; } else if (strcasecmp(key, "Port") == 0) { int port = (int)(atof(value)); if ((port > 0) && (port <= 65535)) - ssnprintf(ntpd_port, sizeof(ntpd_port), "%i", port); + snprintf(ntpd_port, sizeof(ntpd_port), "%i", port); else sstrncpy(ntpd_port, value, sizeof(ntpd_port)); } else if (strcasecmp(key, "ReverseLookups") == 0) { if (IS_TRUE(value)) - do_reverse_lookups = 1; + do_reverse_lookups = true; else - do_reverse_lookups = 0; + do_reverse_lookups = false; } else if (strcasecmp(key, "IncludeUnitID") == 0) { if (IS_TRUE(value)) - include_unit_id = 1; + include_unit_id = true; else - include_unit_id = 0; + include_unit_id = false; } else { - return (-1); + return -1; } - return (0); + return 0; } static void ntpd_submit(const char *type, const char *type_inst, @@ -322,7 +319,7 @@ static int ntpd_connect(void) { int status; if (sock_descr >= 0) - return (sock_descr); + return sock_descr; DEBUG("Opening a new socket"); @@ -340,11 +337,9 @@ static int ntpd_connect(void) { .ai_socktype = SOCK_DGRAM}; if ((status = getaddrinfo(host, port, &ai_hints, &ai_list)) != 0) { - char errbuf[1024]; ERROR("ntpd plugin: getaddrinfo (%s, %s): %s", host, port, - (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf)) - : gai_strerror(status)); - return (-1); + (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status)); + return -1; } for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; @@ -370,7 +365,7 @@ static int ntpd_connect(void) { ERROR("ntpd plugin: Unable to connect to server."); } - return (sock_descr); + return sock_descr; } /* For a description of the arguments see `ntpd_do_query' below. */ @@ -399,7 +394,7 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, ssize_t pkt_padding; /* Padding in this packet */ if ((sd = ntpd_connect()) < 0) - return (-1); + return -1; items = NULL; items_num = 0; @@ -412,10 +407,8 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, *res_data = NULL; if (gettimeofday(&time_end, NULL) < 0) { - char errbuf[1024]; - ERROR("ntpd plugin: gettimeofday failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + ERROR("ntpd plugin: gettimeofday failed: %s", STRERRNO); + return -1; } time_end.tv_sec++; /* wait for a most one second */ @@ -424,10 +417,8 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, struct timeval time_left; if (gettimeofday(&time_now, NULL) < 0) { - char errbuf[1024]; - ERROR("ntpd plugin: gettimeofday failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + ERROR("ntpd plugin: gettimeofday failed: %s", STRERRNO); + return -1; } if (timeval_cmp(time_end, time_now, &time_left) <= 0) @@ -450,10 +441,8 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, continue; if (status < 0) { - char errbuf[1024]; - ERROR("ntpd plugin: poll failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (-1); + ERROR("ntpd plugin: poll failed: %s", STRERRNO); + return -1; } if (status == 0) /* timeout */ @@ -469,12 +458,11 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, continue; if (status < 0) { - char errbuf[1024]; - INFO("recv(2) failed: %s", sstrerror(errno, errbuf, sizeof(errbuf))); + INFO("recv(2) failed: %s", STRERRNO); DEBUG("Closing socket #%i", sd); close(sd); sock_descr = sd = -1; - return (-1); + return -1; } DEBUG("recv'd %i bytes", status); @@ -516,7 +504,7 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, if (INFO_ERR(res.err_nitems) != 0) { ERROR("ntpd plugin: Received error code %i", (int)INFO_ERR(res.err_nitems)); - return ((int)INFO_ERR(res.err_nitems)); + return (int)INFO_ERR(res.err_nitems); } /* extract number of items in this packet and the size of these items */ @@ -596,7 +584,7 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, * Enough with the checks. Copy the data now. * We start by allocating some more memory. */ - DEBUG("realloc (%p, %zu)", (void *)*res_data, + DEBUG("realloc (%p, %" PRIsz ")", (void *)*res_data, (items_num + pkt_item_num) * res_item_size); items = realloc(*res_data, (items_num + pkt_item_num) * res_item_size); if (items == NULL) { @@ -635,7 +623,7 @@ static int ntpd_receive_response(int *res_items, int *res_size, char **res_data, done = 1; } /* while (done == 0) */ - return (0); + return 0; } /* int ntpd_receive_response */ /* For a description of the arguments see `ntpd_do_query' below. */ @@ -650,7 +638,7 @@ static int ntpd_send_request(int req_code, int req_items, int req_size, assert(req_size >= 0); if ((sd = ntpd_connect()) < 0) - return (-1); + return -1; req.rm_vn_mode = RM_VN_MODE(0, 0, 0); req.auth_seq = AUTH_SEQ(0, 0); @@ -672,14 +660,14 @@ static int ntpd_send_request(int req_code, int req_items, int req_size, (void *)req_data); status = swrite(sd, (const char *)&req, REQ_LEN_NOMAC); - if (status < 0) { + if (status != 0) { DEBUG("`swrite' failed. Closing socket #%i", sd); close(sd); sock_descr = sd = -1; - return (status); + return status; } - return (0); + return 0; } /* @@ -704,10 +692,10 @@ static int ntpd_do_query(int req_code, int req_items, int req_size, status = ntpd_send_request(req_code, req_items, req_size, req_data); if (status != 0) - return (status); + return status; status = ntpd_receive_response(res_items, res_size, res_data, res_item_size); - return (status); + return status; } static double ntpd_read_fp(int32_t val_int) { @@ -716,7 +704,7 @@ static double ntpd_read_fp(int32_t val_int) { val_int = ntohl(val_int); val_double = ((double)val_int) / FP_FRAC; - return (val_double); + return val_double; } static uint32_t @@ -724,12 +712,12 @@ ntpd_get_refclock_id(struct info_peer_summary const *peer_info) { uint32_t addr = ntohl(peer_info->srcadr); uint32_t refclock_id = (addr >> 8) & 0x00FF; - return (refclock_id); + return refclock_id; } static int ntpd_get_name_from_address(char *buffer, size_t buffer_size, struct info_peer_summary const *peer_info, - _Bool do_reverse_lookup) { + bool do_reverse_lookup) { struct sockaddr_storage sa = {0}; socklen_t sa_len; int flags = 0; @@ -766,14 +754,12 @@ static int ntpd_get_name_from_address(char *buffer, size_t buffer_size, buffer_size, NULL, 0, /* No port name */ flags); if (status != 0) { - char errbuf[1024]; ERROR("ntpd plugin: getnameinfo failed: %s", - (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf)) - : gai_strerror(status)); - return (-1); + (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status)); + return -1; } - return (0); + return 0; } /* ntpd_get_name_from_address */ static int ntpd_get_name_refclock(char *buffer, size_t buffer_size, @@ -782,16 +768,15 @@ static int ntpd_get_name_refclock(char *buffer, size_t buffer_size, uint32_t unit_id = ntohl(peer_info->srcadr) & 0x00FF; if (((size_t)refclock_id) >= refclock_names_num) - return (ntpd_get_name_from_address(buffer, buffer_size, peer_info, - /* do_reverse_lookup = */ 0)); + return ntpd_get_name_from_address(buffer, buffer_size, peer_info, 0); if (include_unit_id) - ssnprintf(buffer, buffer_size, "%s-%" PRIu32, refclock_names[refclock_id], - unit_id); + snprintf(buffer, buffer_size, "%s-%" PRIu32, refclock_names[refclock_id], + unit_id); else sstrncpy(buffer, refclock_names[refclock_id], buffer_size); - return (0); + return 0; } /* int ntpd_get_name_refclock */ static int ntpd_get_name(char *buffer, size_t buffer_size, @@ -799,10 +784,10 @@ static int ntpd_get_name(char *buffer, size_t buffer_size, uint32_t addr = ntohl(peer_info->srcadr); if (!peer_info->v6_flag && ((addr & REFCLOCK_MASK) == REFCLOCK_ADDR)) - return (ntpd_get_name_refclock(buffer, buffer_size, peer_info)); + return ntpd_get_name_refclock(buffer, buffer_size, peer_info); else - return (ntpd_get_name_from_address(buffer, buffer_size, peer_info, - do_reverse_lookups)); + return ntpd_get_name_from_address(buffer, buffer_size, peer_info, + do_reverse_lookups); } /* int ntpd_addr_to_name */ static int ntpd_read(void) { @@ -836,12 +821,12 @@ static int ntpd_read(void) { if (status != 0) { ERROR("ntpd plugin: ntpd_do_query (REQ_GET_KERNEL) failed with status %i", status); - return (status); + return status; } else if ((ik == NULL) || (ik_num == 0) || (ik_size == 0)) { ERROR("ntpd plugin: ntpd_do_query returned unexpected data. " "(ik = %p; ik_num = %i; ik_size = %i)", (void *)ik, ik_num, ik_size); - return (-1); + return -1; } if (ntohs(ik->status) & STA_NANO) { @@ -875,12 +860,12 @@ static int ntpd_read(void) { ERROR( "ntpd plugin: ntpd_do_query (REQ_PEER_LIST_SUM) failed with status %i", status); - return (status); + return status; } else if ((ps == NULL) || (ps_num == 0) || (ps_size == 0)) { ERROR("ntpd plugin: ntpd_do_query returned unexpected data. " "(ps = %p; ps_num = %i; ps_size = %i)", (void *)ps, ps_num, ps_size); - return (-1); + return -1; } for (int i = 0; i < ps_num; i++) { @@ -898,6 +883,12 @@ static int ntpd_read(void) { continue; } + // `0.0.0.0` hosts are caused by POOL servers + // see https://github.com/collectd/collectd/issues/2358 + if (strcmp(peername, "0.0.0.0") == 0) { + continue; + } + refclock_id = ntpd_get_refclock_id(ptr); /* Convert the `long floating point' offset value to double */ @@ -929,7 +920,7 @@ static int ntpd_read(void) { free(ps); ps = NULL; - return (0); + return 0; } /* int ntpd_read */ void module_register(void) {