X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdns.c;h=fd75dc93a9437fb9bd0d7a580c1937e26f19b859;hb=ca316d91e178412604ea8462dc60a8bc32cbfc87;hp=f6c3504731cfe55bef6c810d3abe1d41a2e33624;hpb=aff80830f1154a5b6c4da16a0b1033aafde14e24;p=collectd.git diff --git a/src/dns.c b/src/dns.c index f6c35047..fd75dc93 100644 --- a/src/dns.c +++ b/src/dns.c @@ -1,6 +1,6 @@ /** * collectd - src/dns.c - * Copyright (C) 2006,2007 Florian octo Forster + * Copyright (C) 2006-2011 Florian octo Forster * Copyright (C) 2009 Mirko Buffoni * * This program is free software; you can redistribute it and/or modify it @@ -17,10 +17,11 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * Mirko Buffoni **/ +#define _DEFAULT_SOURCE #define _BSD_SOURCE #include "collectd.h" @@ -30,9 +31,11 @@ #include "utils_dns.h" #include -#include #include +#include +#include + /* * Private data types */ @@ -206,7 +209,7 @@ static void dns_child_callback (const rfc1035_header_t *dns) pthread_mutex_unlock (&opcode_mutex); } -static void *dns_child_loop (void __attribute__((unused)) *dummy) +static int dns_run_pcap_loop (void) { pcap_t *pcap_obj; char pcap_error[PCAP_ERRBUF_SIZE]; @@ -226,7 +229,7 @@ static void *dns_child_loop (void __attribute__((unused)) *dummy) pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any", PCAP_SNAPLEN, 0 /* Not promiscuous */, - (int) CDTIME_T_TO_MS (interval_g / 2), + (int) CDTIME_T_TO_MS (plugin_get_interval () / 2), pcap_error); if (pcap_obj == NULL) { @@ -234,19 +237,24 @@ static void *dns_child_loop (void __attribute__((unused)) *dummy) "failed: %s", (pcap_device != NULL) ? pcap_device : "any", pcap_error); - return (NULL); + return (PCAP_ERROR); } memset (&fp, 0, sizeof (fp)); - if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0) + status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0); + if (status < 0) { - ERROR ("dns plugin: pcap_compile failed"); - return (NULL); + ERROR ("dns plugin: pcap_compile failed: %s", + pcap_statustostr (status)); + return (status); } - if (pcap_setfilter (pcap_obj, &fp) < 0) + + status = pcap_setfilter (pcap_obj, &fp); + if (status < 0) { - ERROR ("dns plugin: pcap_setfilter failed"); - return (NULL); + ERROR ("dns plugin: pcap_setfilter failed: %s", + pcap_statustostr (status)); + return (status); } DEBUG ("dns plugin: PCAP object created."); @@ -257,19 +265,65 @@ static void *dns_child_loop (void __attribute__((unused)) *dummy) status = pcap_loop (pcap_obj, -1 /* loop forever */, handle_pcap /* callback */, - NULL /* Whatever this means.. */); - if (status < 0) - ERROR ("dns plugin: Listener thread is exiting " - "abnormally: %s", pcap_geterr (pcap_obj)); - - DEBUG ("dns plugin: Child is exiting."); + NULL /* user data */); + INFO ("dns plugin: pcap_loop exited with status %i.", status); + /* We need to handle "PCAP_ERROR" specially because libpcap currently + * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */ + if (status == PCAP_ERROR) + status = PCAP_ERROR_IFACE_NOT_UP; pcap_close (pcap_obj); - listen_thread_init = 0; - pthread_exit (NULL); + return (status); +} /* int dns_run_pcap_loop */ + +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 rem = { 0, 0 }; + + status = nanosleep (&ts, &rem); + if (status == 0) + break; + else if ((errno == EINTR) || (errno == EAGAIN)) + { + ts = rem; + continue; + } + else + break; + } + + return (status); +} /* }}} int dns_sleep_one_interval */ + +static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */ +{ + int status; + + while (42) + { + status = dns_run_pcap_loop (); + if (status != PCAP_ERROR_IFACE_NOT_UP) + break; + + dns_sleep_one_interval (); + } + + if (status != PCAP_ERROR_BREAK) + ERROR ("dns plugin: PCAP returned error %s.", + pcap_statustostr (status)); + + listen_thread_init = 0; return (NULL); -} /* static void dns_child_loop (void) */ +} /* }}} void *dns_child_loop */ static int dns_init (void) { @@ -284,7 +338,7 @@ static int dns_init (void) if (listen_thread_init != 0) return (-1); - status = pthread_create (&listen_thread, NULL, dns_child_loop, + status = plugin_thread_create (&listen_thread, NULL, dns_child_loop, (void *) 0); if (status != 0) {