From 650c36841827fc1ee5536edb60fc84acf83c1fce Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 31 Oct 2010 15:55:06 +0100 Subject: [PATCH] powerdns plugin: Make calculation of timeout value more precise. --- src/powerdns.c | 18 ++++++++++++------ src/utils_time.h | 17 +++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/powerdns.c b/src/powerdns.c index 29f6bca5..508ea506 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -321,6 +321,9 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */ struct sockaddr_un sa_unix; + struct timeval stv_timeout; + cdtime_t cdt_timeout; + sd = socket (PF_UNIX, item->socktype, 0); if (sd < 0) { @@ -361,12 +364,15 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */ break; } - struct timeval timeout; - timeout.tv_sec=2; - if (timeout.tv_sec < interval_g * 3 / 4) - timeout.tv_sec = interval_g * 3 / 4; - timeout.tv_usec=0; - status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout)); + /* TODO: Remove the macro once interval_g is of type cdtime_t. */ + cdt_timeout = TIME_T_TO_CDTIME_T (interval_g); + cdt_timeout = cdt_timeout * 3 / 4; + if (cdt_timeout < TIME_T_TO_CDTIME_T (2)) + cdt_timeout = TIME_T_TO_CDTIME_T (2); + + CDTIME_T_TO_TIMEVAL (cdt_timeout, &stv_timeout); + + status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, sizeof (stv_timeout)); if (status != 0) { FUNC_ERROR ("setsockopt"); diff --git a/src/utils_time.h b/src/utils_time.h index c3928d63..33384cf1 100644 --- a/src/utils_time.h +++ b/src/utils_time.h @@ -49,19 +49,20 @@ typedef uint64_t cdtime_t; #define CDTIME_T_TO_NS(t) ((long) (((double) (t)) / 1.073741824)) #define CDTIME_T_TO_TIMEVAL(cdt,tvp) do { \ - (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ - (tvp)->tv_used = CDTIME_T_TO_US ((cdt) % 1073741824) \ + (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ + (tvp)->tv_usec = CDTIME_T_TO_US ((cdt) % 1073741824); \ } while (0) -#define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv).tv_sec) \ - + US_TO_CDTIME_T ((tv).tv_usec)) +#define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv).tv_sec) \ + + US_TO_CDTIME_T ((tv).tv_usec)) #define CDTIME_T_TO_TIMESPEC(cdt,tsp) do { \ - (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ - (tsp)->tv_nsec = CDTIME_T_TO_NS ((cdt) % 1073741824); \ + (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ + (tsp)->tv_nsec = CDTIME_T_TO_NS ((cdt) % 1073741824); \ } while (0) -#define TIMESPEC_TO_CDTIME_T(ts) (TIME_T_TO_CDTIME_T ((ts).tv_sec) \ - + NS_TO_CDTIME_T ((ts).tv_nsec)) +#define TIMESPEC_TO_CDTIME_T(ts) (TIME_T_TO_CDTIME_T ((ts).tv_sec) \ + + NS_TO_CDTIME_T ((ts).tv_nsec)) cdtime_t cdtime (void); #endif /* UTILS_TIME_H */ +/* vim: set sw=2 sts=2 et : */ -- 2.30.2