src/daemon/utils_time.h: Return structs from CDTIME_T_TO_TIME{VAL,SPEC}.
[collectd.git] / src / dns.c
index 15fa15a..c8e794e 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
 
 #include "common.h"
 #include "plugin.h"
-#include "configfile.h"
 
 #include "utils_dns.h"
 #include <poll.h>
 
 #include <pcap.h>
 
+#ifdef HAVE_SYS_CAPABILITY_H
+# include <sys/capability.h>
+#endif
+
 /*
  * Private data types
  */
@@ -275,30 +278,16 @@ static int dns_run_pcap_loop (void)
 
 static int dns_sleep_one_interval (void) /* {{{ */
 {
-       cdtime_t interval;
-       struct timespec ts = { 0, 0 };
-       int status = 0;
-
-       interval = plugin_get_interval ();
-       CDTIME_T_TO_TIMESPEC (interval, &ts);
-
-       while (42)
+       struct timespec ts = CDTIME_T_TO_TIMESPEC (plugin_get_interval ());
+       while (nanosleep (&ts, &ts) != 0)
        {
-               struct timespec rem = { 0, 0 };
-
-               status = nanosleep (&ts, &rem);
-               if (status == 0)
-                       break;
-               else if ((errno == EINTR) || (errno == EAGAIN))
-               {
-                       ts = rem;
+               if ((errno == EINTR) || (errno == EAGAIN))
                        continue;
-               }
-               else
-                       break;
+
+               return (errno);
        }
 
-       return (status);
+       return (0);
 } /* }}} int dns_sleep_one_interval */
 
 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
@@ -347,20 +336,30 @@ static int dns_init (void)
 
        listen_thread_init = 1;
 
+#if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
+       if (check_capability (CAP_NET_RAW) != 0)
+       {
+               if (getuid () == 0)
+                       WARNING ("dns 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 ("dns 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);
 } /* int dns_init */
 
 static void submit_derive (const char *type, const char *type_instance,
                derive_t value)
 {
-       value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
 
-       values[0].derive = value;
-
-       vl.values = values;
+       vl.values = &(value_t) { .derive = value };
        vl.values_len = 1;
-       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
        sstrncpy (vl.type, type, sizeof (vl.type));
        sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
@@ -370,15 +369,14 @@ static void submit_derive (const char *type, const char *type_instance,
 
 static void submit_octets (derive_t queries, derive_t responses)
 {
-       value_t values[2];
+       value_t values[] = {
+          { .derive = queries },
+          { .derive = responses },
+        };
        value_list_t vl = VALUE_LIST_INIT;
 
-       values[0].derive = queries;
-       values[1].derive = responses;
-
        vl.values = values;
-       vl.values_len = 2;
-       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       vl.values_len = STATIC_ARRAY_SIZE (values);
        sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
        sstrncpy (vl.type, "dns_octets", sizeof (vl.type));