X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fping.c;h=c97571b85c066c695fd66369269b6035bba00097;hb=958f7776a2daaa1a8664aded0a2c1d717c2f5909;hp=cac8a3fde43540c08249877e54138cefdc6e590f;hpb=ef7cc595639328626c90f4b4bf984f101b389166;p=collectd.git diff --git a/src/ping.c b/src/ping.c index cac8a3fd..978cb490 100644 --- a/src/ping.c +++ b/src/ping.c @@ -25,12 +25,12 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" #include "configfile.h" #include "utils_complain.h" -#include #include #if HAVE_NETDB_H # include /* NI_MAXHOST */ @@ -152,11 +152,10 @@ static void time_calc (struct timespec *ts_dest, /* {{{ */ static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */ { - pingobj_iter_t *iter; hostlist_t *hl; int status; - for (iter = ping_iterator_get (pingobj); + for (pingobj_iter_t *iter = ping_iterator_get (pingobj); iter != NULL; iter = ping_iterator_next (iter)) { /* {{{ */ @@ -244,14 +243,13 @@ static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */ static void *ping_thread (void *arg) /* {{{ */ { - static pingobj_t *pingobj = NULL; + pingobj_t *pingobj = NULL; struct timeval tv_begin; struct timeval tv_end; struct timespec ts_wait; struct timespec ts_int; - hostlist_t *hl; int count; c_complain_t complaint = C_COMPLAIN_INIT_STATIC; @@ -287,7 +285,7 @@ static void *ping_thread (void *arg) /* {{{ */ /* Add all the hosts to the ping object. */ count = 0; - for (hl = hostlist_head; hl != NULL; hl = hl->next) + for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) { int tmp_status; tmp_status = ping_host_add (pingobj, hl->host); @@ -399,7 +397,7 @@ static int start_thread (void) /* {{{ */ pthread_mutex_unlock (&ping_lock); return (-1); } - + pthread_mutex_unlock (&ping_lock); return (0); } /* }}} int start_thread */ @@ -427,8 +425,10 @@ static int stop_thread (void) /* {{{ */ status = -1; } + pthread_mutex_lock (&ping_lock); memset (&ping_thread_id, 0, sizeof (ping_thread_id)); ping_thread_error = 0; + pthread_mutex_unlock (&ping_lock); return (status); } /* }}} int stop_thread */ @@ -481,7 +481,7 @@ static int ping_config (const char *key, const char *value) /* {{{ */ hostlist_t *hl; char *host; - hl = (hostlist_t *) malloc (sizeof (hostlist_t)); + hl = malloc (sizeof (*hl)); if (hl == NULL) { char errbuf[1024]; @@ -543,26 +543,19 @@ static int ping_config (const char *key, const char *value) /* {{{ */ tmp, value); } else if (strcasecmp (key, "Size") == 0) { - int size; - - if (ping_data != NULL) - { - free (ping_data); - ping_data = NULL; - } + size_t size = (size_t) atoi (value); - size = atoi (value); - if ((size >= 0) && (size <= 65536)) + /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */ + if (size <= 65487) { - int i; - ping_data = (char *) malloc(size + 1); + sfree (ping_data); + ping_data = malloc (size + 1); if (ping_data == NULL) { - char errbuf[1024]; - ERROR ("ping plugin: malloc failed: %s", - sstrerror (errno, errbuf, sizeof (errbuf))); + ERROR ("ping plugin: malloc failed."); return (1); } + /* Note: By default oping is using constant string * "liboping -- ICMP ping library " * which is exactly 56 bytes. @@ -570,15 +563,15 @@ static int ping_config (const char *key, const char *value) /* {{{ */ * Optimally we would follow the ping(1) behaviour, but we * cannot use byte 00 or start data payload at exactly same * location, due to oping library limitations. */ - for (i = 0; i < size; i++) /* {{{ */ + for (size_t i = 0; i < size; i++) /* {{{ */ { /* This restricts data pattern to be only composed of easily * printable characters, and not NUL character. */ ping_data[i] = ('0' + i % 64); } /* }}} for (i = 0; i < size; i++) */ - ping_data[size] = '\0'; + ping_data[size] = 0; } else - WARNING ("ping plugin: Ignoring invalid Size %i.", size); + WARNING ("ping plugin: Ignoring invalid Size %zu.", size); } else if (strcasecmp (key, "Timeout") == 0) { @@ -626,15 +619,13 @@ static void submit (const char *host, const char *type, /* {{{ */ static int ping_read (void) /* {{{ */ { - hostlist_t *hl; - if (ping_thread_error != 0) { ERROR ("ping plugin: The ping thread had a problem. Restarting it."); stop_thread (); - for (hl = hostlist_head; hl != NULL; hl = hl->next) + for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) { hl->pkg_sent = 0; hl->pkg_recv = 0; @@ -647,7 +638,7 @@ static int ping_read (void) /* {{{ */ return (-1); } /* if (ping_thread_error != 0) */ - for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */ + for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */ { uint32_t pkg_sent; uint32_t pkg_recv;