From cdea931a51b0c5bebb37007da965635cbae74b40 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 26 Apr 2013 14:16:38 +0200 Subject: [PATCH] dns plugin: Sleep for an interval before re-trying to read from interface. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Otherwise we'd just have the CPU hogging behavior shifted from libpcap to collectd … Github: #308 --- src/dns.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/dns.c b/src/dns.c index aea41c43..00fcff19 100644 --- a/src/dns.c +++ b/src/dns.c @@ -275,12 +275,47 @@ static int dns_run_pcap_loop (void) 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 = PCAP_ERROR_IFACE_NOT_UP; + int status; - while (status == PCAP_ERROR_IFACE_NOT_UP) + 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)); -- 2.30.2