X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdns.c;h=04e5a1e6459671d04aab853256616514a3ad5a32;hb=ec43e8a33bdcb116e75310c7bd9daae3bc912834;hp=3421c475d7e6fc49c2419332dbcd8e5d3dd09f06;hpb=1eb3c043cb162ef198f86034cf7604be4a0a0d9c;p=collectd.git diff --git a/src/dns.c b/src/dns.c index 3421c475..04e5a1e6 100644 --- a/src/dns.c +++ b/src/dns.c @@ -25,16 +25,19 @@ #define _BSD_SOURCE #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "configfile.h" #include "utils_dns.h" -#include #include #include +#ifdef HAVE_SYS_CAPABILITY_H +# include +#endif + /* * Private data types */ @@ -94,11 +97,10 @@ static counter_list_t *counter_list_create (counter_list_t **list, { counter_list_t *entry; - entry = (counter_list_t *) malloc (sizeof (counter_list_t)); + entry = calloc (1, sizeof (*entry)); if (entry == NULL) return (NULL); - memset (entry, 0, sizeof (counter_list_t)); entry->key = key; entry->value = value; @@ -212,7 +214,7 @@ static int dns_run_pcap_loop (void) { pcap_t *pcap_obj; char pcap_error[PCAP_ERRBUF_SIZE]; - struct bpf_program fp; + struct bpf_program fp = { 0 }; int status; @@ -239,7 +241,6 @@ static int dns_run_pcap_loop (void) return (PCAP_ERROR); } - memset (&fp, 0, sizeof (fp)); status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0); if (status < 0) { @@ -349,20 +350,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)); @@ -372,15 +383,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)); @@ -392,7 +402,6 @@ static int dns_read (void) unsigned int keys[T_MAX]; unsigned int values[T_MAX]; int len; - int i; counter_list_t *ptr; @@ -414,7 +423,7 @@ static int dns_read (void) } pthread_mutex_unlock (&qtype_mutex); - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) { DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]); submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]); @@ -430,7 +439,7 @@ static int dns_read (void) } pthread_mutex_unlock (&opcode_mutex); - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) { DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]); submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]); @@ -446,7 +455,7 @@ static int dns_read (void) } pthread_mutex_unlock (&rcode_mutex); - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) { DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]); submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);