X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fntpd.c;h=39bbeeb5f5413cc7764f7273fc55d809d36e43e7;hb=0b7cd83a5e6bac068ea83a88a5ddcfb07c09fbec;hp=21b65f5888b3fb30df46c7f7d936422e9c7c7f69;hpb=64b8150edec2271bb110f36e70915fdd87d9cac2;p=collectd.git diff --git a/src/ntpd.c b/src/ntpd.c index 21b65f58..39bbeeb5 100644 --- a/src/ntpd.c +++ b/src/ntpd.c @@ -263,11 +263,11 @@ 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) { @@ -281,10 +281,10 @@ static int ntpd_config(const char *key, const char *value) { else include_unit_id = 0; } else { - return (-1); + return -1; } - return (0); + return 0; } static void ntpd_submit(const char *type, const char *type_inst, @@ -319,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"); @@ -337,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; @@ -367,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. */ @@ -396,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; @@ -409,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 */ @@ -421,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) @@ -447,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 */ @@ -466,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); @@ -513,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 */ @@ -632,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. */ @@ -647,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); @@ -669,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; } /* @@ -701,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) { @@ -713,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 @@ -721,7 +712,7 @@ 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, @@ -763,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, @@ -779,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, @@ -796,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) { @@ -833,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) { @@ -847,9 +835,9 @@ static int ntpd_read(void) { } /* kerninfo -> estimated error */ - offset_loop = scale_loop * ((gauge_t)ntohl(ik->offset)); + offset_loop = (gauge_t)((int32_t)ntohl(ik->offset) * scale_loop); freq_loop = ntpd_read_fp(ik->freq); - offset_error = scale_error * ((gauge_t)ntohl(ik->esterror)); + offset_error = (gauge_t)((int32_t)ntohl(ik->esterror) * scale_error); DEBUG("info_kernel:\n" " pll offset = %.8g\n" @@ -872,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++) { @@ -926,7 +914,7 @@ static int ntpd_read(void) { free(ps); ps = NULL; - return (0); + return 0; } /* int ntpd_read */ void module_register(void) {