X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fping.c;h=66a254d52d9d5ded6fd39dab3d0279523548b0b1;hb=9d9678b9e753ea0936612021f4f87f8092ab4e31;hp=88661e94ba5a5e60c99281a9fffc26d6c3265849;hpb=aef15b632b9f415e5ebfadd4e41fa8a3c19407ee;p=collectd.git diff --git a/src/ping.c b/src/ping.c index 88661e94..66a254d5 100644 --- a/src/ping.c +++ b/src/ping.c @@ -25,17 +25,20 @@ **/ #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 */ #endif +#ifdef HAVE_SYS_CAPABILITY_H +# include +#endif + #include #ifndef NI_MAXHOST @@ -152,11 +155,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)) { /* {{{ */ @@ -251,7 +253,6 @@ static void *ping_thread (void *arg) /* {{{ */ struct timespec ts_wait; struct timespec ts_int; - hostlist_t *hl; int count; c_complain_t complaint = C_COMPLAIN_INIT_STATIC; @@ -287,7 +288,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); @@ -385,13 +386,13 @@ static int start_thread (void) /* {{{ */ if (ping_thread_loop != 0) { pthread_mutex_unlock (&ping_lock); - return (-1); + return (0); } ping_thread_loop = 1; ping_thread_error = 0; status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL, - ping_thread, /* arg = */ (void *) 0); + ping_thread, /* arg = */ (void *) 0, "ping"); if (status != 0) { ping_thread_loop = 0; @@ -450,10 +451,21 @@ static int ping_init (void) /* {{{ */ "Will use a timeout of %gs.", ping_timeout); } - if (start_thread () != 0) - return (-1); +#if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW) + if (check_capability (CAP_NET_RAW) != 0) + { + if (getuid () == 0) + WARNING ("ping plugin: Running collectd as root, but the CAP_NET_RAW " + "capability is missing. The plugin's read function will probably " + "fail. Is your init system dropping capabilities?"); + else + WARNING ("ping plugin: collectd doesn't have the CAP_NET_RAW capability. " + "If you don't want to run collectd as root, try running \"setcap " + "cap_net_raw=ep\" on the collectd binary."); + } +#endif - return (0); + return (start_thread ()); } /* }}} int ping_init */ static int config_set_string (const char *name, /* {{{ */ @@ -550,8 +562,6 @@ static int ping_config (const char *key, const char *value) /* {{{ */ /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */ if (size <= 65487) { - size_t i; - sfree (ping_data); ping_data = malloc (size + 1); if (ping_data == NULL) @@ -567,7 +577,7 @@ 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. */ @@ -605,16 +615,11 @@ static int ping_config (const char *key, const char *value) /* {{{ */ static void submit (const char *host, const char *type, /* {{{ */ gauge_t value) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = value; - - vl.values = values; + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; - sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "ping", sizeof (vl.plugin)); - sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance)); sstrncpy (vl.type_instance, host, sizeof (vl.type_instance)); sstrncpy (vl.type, type, sizeof (vl.type)); @@ -623,15 +628,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; @@ -644,7 +647,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;