Don't initialize static numeric variables to 0
[collectd.git] / src / ping.c
index 03dea6d..ffb1691 100644 (file)
@@ -69,13 +69,14 @@ typedef struct hostlist_s hostlist_t;
 /*
  * Private variables
  */
-static hostlist_t *hostlist_head = NULL;
+static hostlist_t *hostlist_head;
 
-static char *ping_source = NULL;
+static int ping_af = PING_DEF_AF;
+static char *ping_source;
 #ifdef HAVE_OPING_1_3
-static char *ping_device = NULL;
+static char *ping_device;
 #endif
-static char *ping_data = NULL;
+static char *ping_data;
 static int ping_ttl = PING_DEF_TTL;
 static double ping_interval = 1.0;
 static double ping_timeout = 0.9;
@@ -83,11 +84,11 @@ static int ping_max_missed = -1;
 
 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
-static int ping_thread_loop = 0;
-static int ping_thread_error = 0;
+static int ping_thread_loop;
+static int ping_thread_error;
 static pthread_t ping_thread_id;
 
-static const char *config_keys[] = {"Host",    "SourceAddress",
+static const char *config_keys[] = {"Host",    "SourceAddress", "AddressFamily",
 #ifdef HAVE_OPING_1_3
                                     "Device",
 #endif
@@ -219,7 +220,7 @@ static int ping_dispatch_all(pingobj_t *pingobj) /* {{{ */
     } /* }}} ping_max_missed */
   }   /* }}} for (iter) */
 
-  return (0);
+  return 0;
 } /* }}} int ping_dispatch_all */
 
 static void *ping_thread(void *arg) /* {{{ */
@@ -239,7 +240,13 @@ static void *ping_thread(void *arg) /* {{{ */
     pthread_mutex_lock(&ping_lock);
     ping_thread_error = 1;
     pthread_mutex_unlock(&ping_lock);
-    return ((void *)-1);
+    return (void *)-1;
+  }
+
+  if (ping_af != PING_DEF_AF) {
+    if (ping_setopt(pingobj, PING_OPT_AF, &ping_af) != 0)
+      ERROR("ping plugin: Failed to set address family: %s",
+            ping_get_error(pingobj));
   }
 
   if (ping_source != NULL)
@@ -276,7 +283,7 @@ static void *ping_thread(void *arg) /* {{{ */
     pthread_mutex_lock(&ping_lock);
     ping_thread_error = 1;
     pthread_mutex_unlock(&ping_lock);
-    return ((void *)-1);
+    return (void *)-1;
   }
 
   /* Set up `ts_int' */
@@ -291,12 +298,10 @@ static void *ping_thread(void *arg) /* {{{ */
 
   pthread_mutex_lock(&ping_lock);
   while (ping_thread_loop > 0) {
-    _Bool send_successful = 0;
+    bool send_successful = false;
 
     if (gettimeofday(&tv_begin, NULL) < 0) {
-      char errbuf[1024];
-      ERROR("ping plugin: gettimeofday failed: %s",
-            sstrerror(errno, errbuf, sizeof(errbuf)));
+      ERROR("ping plugin: gettimeofday failed: %s", STRERRNO);
       ping_thread_error = 1;
       break;
     }
@@ -309,7 +314,7 @@ static void *ping_thread(void *arg) /* {{{ */
                  ping_get_error(pingobj));
     } else {
       c_release(LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
-      send_successful = 1;
+      send_successful = true;
     }
 
     pthread_mutex_lock(&ping_lock);
@@ -321,9 +326,7 @@ static void *ping_thread(void *arg) /* {{{ */
       (void)ping_dispatch_all(pingobj);
 
     if (gettimeofday(&tv_end, NULL) < 0) {
-      char errbuf[1024];
-      ERROR("ping plugin: gettimeofday failed: %s",
-            sstrerror(errno, errbuf, sizeof(errbuf)));
+      ERROR("ping plugin: gettimeofday failed: %s", STRERRNO);
       ping_thread_error = 1;
       break;
     }
@@ -340,7 +343,7 @@ static void *ping_thread(void *arg) /* {{{ */
   pthread_mutex_unlock(&ping_lock);
   ping_destroy(pingobj);
 
-  return ((void *)0);
+  return (void *)0;
 } /* }}} void *ping_thread */
 
 static int start_thread(void) /* {{{ */
@@ -351,7 +354,7 @@ static int start_thread(void) /* {{{ */
 
   if (ping_thread_loop != 0) {
     pthread_mutex_unlock(&ping_lock);
-    return (0);
+    return 0;
   }
 
   ping_thread_loop = 1;
@@ -362,11 +365,11 @@ static int start_thread(void) /* {{{ */
     ping_thread_loop = 0;
     ERROR("ping plugin: Starting thread failed.");
     pthread_mutex_unlock(&ping_lock);
-    return (-1);
+    return -1;
   }
 
   pthread_mutex_unlock(&ping_lock);
-  return (0);
+  return 0;
 } /* }}} int start_thread */
 
 static int stop_thread(void) /* {{{ */
@@ -377,7 +380,7 @@ static int stop_thread(void) /* {{{ */
 
   if (ping_thread_loop == 0) {
     pthread_mutex_unlock(&ping_lock);
-    return (-1);
+    return -1;
   }
 
   ping_thread_loop = 0;
@@ -395,14 +398,14 @@ static int stop_thread(void) /* {{{ */
   ping_thread_error = 0;
   pthread_mutex_unlock(&ping_lock);
 
-  return (status);
+  return status;
 } /* }}} int stop_thread */
 
 static int ping_init(void) /* {{{ */
 {
   if (hostlist_head == NULL) {
     NOTICE("ping plugin: No hosts have been configured.");
-    return (-1);
+    return -1;
   }
 
   if (ping_timeout > ping_interval) {
@@ -425,7 +428,7 @@ static int ping_init(void) /* {{{ */
   }
 #endif
 
-  return (start_thread());
+  return start_thread();
 } /* }}} int ping_init */
 
 static int config_set_string(const char *name, /* {{{ */
@@ -434,16 +437,15 @@ static int config_set_string(const char *name, /* {{{ */
 
   tmp = strdup(value);
   if (tmp == NULL) {
-    char errbuf[1024];
     ERROR("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s", name,
-          value, sstrerror(errno, errbuf, sizeof(errbuf)));
-    return (1);
+          value, STRERRNO);
+    return 1;
   }
 
   if (*var != NULL)
     free(*var);
   *var = tmp;
-  return (0);
+  return 0;
 } /* }}} int config_set_string */
 
 static int ping_config(const char *key, const char *value) /* {{{ */
@@ -454,19 +456,15 @@ static int ping_config(const char *key, const char *value) /* {{{ */
 
     hl = malloc(sizeof(*hl));
     if (hl == NULL) {
-      char errbuf[1024];
-      ERROR("ping plugin: malloc failed: %s",
-            sstrerror(errno, errbuf, sizeof(errbuf)));
-      return (1);
+      ERROR("ping plugin: malloc failed: %s", STRERRNO);
+      return 1;
     }
 
     host = strdup(value);
     if (host == NULL) {
-      char errbuf[1024];
       sfree(hl);
-      ERROR("ping plugin: strdup failed: %s",
-            sstrerror(errno, errbuf, sizeof(errbuf)));
-      return (1);
+      ERROR("ping plugin: strdup failed: %s", STRERRNO);
+      return 1;
     }
 
     hl->host = host;
@@ -477,16 +475,33 @@ static int ping_config(const char *key, const char *value) /* {{{ */
     hl->latency_squared = 0.0;
     hl->next = hostlist_head;
     hostlist_head = hl;
+  } else if (strcasecmp(key, "AddressFamily") == 0) {
+    char *af = NULL;
+    int status = config_set_string(key, &af, value);
+    if (status != 0)
+      return status;
+
+    if (strncmp(af, "any", 3) == 0) {
+      ping_af = AF_UNSPEC;
+    } else if (strncmp(af, "ipv4", 4) == 0) {
+      ping_af = AF_INET;
+    } else if (strncmp(af, "ipv6", 4) == 0) {
+      ping_af = AF_INET6;
+    } else {
+      WARNING("ping plugin: Ignoring invalid AddressFamily value %s", af);
+    }
+    free(af);
+
   } else if (strcasecmp(key, "SourceAddress") == 0) {
     int status = config_set_string(key, &ping_source, value);
     if (status != 0)
-      return (status);
+      return status;
   }
 #ifdef HAVE_OPING_1_3
   else if (strcasecmp(key, "Device") == 0) {
     int status = config_set_string(key, &ping_device, value);
     if (status != 0)
-      return (status);
+      return status;
   }
 #endif
   else if (strcasecmp(key, "TTL") == 0) {
@@ -512,7 +527,7 @@ static int ping_config(const char *key, const char *value) /* {{{ */
       ping_data = malloc(size + 1);
       if (ping_data == NULL) {
         ERROR("ping plugin: malloc failed.");
-        return (1);
+        return 1;
       }
 
       /* Note: By default oping is using constant string
@@ -530,7 +545,7 @@ static int ping_config(const char *key, const char *value) /* {{{ */
       } /* }}} for (i = 0; i < size; i++) */
       ping_data[size] = 0;
     } else
-      WARNING("ping plugin: Ignoring invalid Size %zu.", size);
+      WARNING("ping plugin: Ignoring invalid Size %" PRIsz ".", size);
   } else if (strcasecmp(key, "Timeout") == 0) {
     double tmp;
 
@@ -544,10 +559,10 @@ static int ping_config(const char *key, const char *value) /* {{{ */
     if (ping_max_missed < 0)
       INFO("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
   } else {
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 } /* }}} int ping_config */
 
 static void submit(const char *host, const char *type, /* {{{ */
@@ -579,7 +594,7 @@ static int ping_read(void) /* {{{ */
 
     start_thread();
 
-    return (-1);
+    return -1;
   } /* if (ping_thread_error != 0) */
 
   for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
@@ -640,7 +655,7 @@ static int ping_read(void) /* {{{ */
     submit(hl->host, "ping_droprate", droprate);
   } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
 
-  return (0);
+  return 0;
 } /* }}} int ping_read */
 
 static int ping_shutdown(void) /* {{{ */
@@ -649,7 +664,7 @@ static int ping_shutdown(void) /* {{{ */
 
   INFO("ping plugin: Shutting down thread.");
   if (stop_thread() < 0)
-    return (-1);
+    return -1;
 
   hl = hostlist_head;
   while (hl != NULL) {
@@ -668,7 +683,7 @@ static int ping_shutdown(void) /* {{{ */
     ping_data = NULL;
   }
 
-  return (0);
+  return 0;
 } /* }}} int ping_shutdown */
 
 void module_register(void) {
@@ -677,5 +692,3 @@ void module_register(void) {
   plugin_register_read("ping", ping_read);
   plugin_register_shutdown("ping", ping_shutdown);
 } /* void module_register */
-
-/* vim: set sw=2 sts=2 et fdm=marker : */