From 45e631cf6134de4a5dd3bcf01794c9fdd539c412 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 21 Aug 2015 11:56:57 +0200 Subject: [PATCH] src/daemon/utils_time.h: Treat nanoseconds as 64bit integer. The assumed type was "long", because that is what struct timespec is using. However, struct timespec only stores the fraction of a second in the approrpiate field and therefore only cares about values up to 10^9. We, on the other hand, assume a UNIX epoch in ns precision, so we require the entire 64bits. This patch changes the [MUN]S_TO_CDTIME_T() macros to assume a uint64_t input and moves the casting to the appropriate data type for struct time{val,spec} to the CDTIME_T_TO_TIME{VAL,SPEC}() macros. Appropriate casts are added to the cURL based plugins which need to pass a "long" to cURL when specifying timeouts. It also fixes the unit test, which assigned large (> 32 bit) literals to a "long" field, which breaks on 32 bit architectures. --- src/apache.c | 3 +-- src/ascent.c | 3 +-- src/bind.c | 2 +- src/curl.c | 3 +-- src/curl_json.c | 6 ++---- src/curl_xml.c | 3 +-- src/daemon/utils_time.h | 16 ++++++++-------- src/daemon/utils_time_test.c | 2 +- src/nginx.c | 3 +-- 9 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/apache.c b/src/apache.c index e384d800..41b807af 100644 --- a/src/apache.c +++ b/src/apache.c @@ -383,8 +383,7 @@ static int init_host (apache_t *st) /* {{{ */ if (st->timeout >= 0) curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout); else - curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(plugin_get_interval())); + curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif return (0); diff --git a/src/ascent.c b/src/ascent.c index 11175af5..2ba3c772 100644 --- a/src/ascent.c +++ b/src/ascent.c @@ -594,8 +594,7 @@ static int ascent_init (void) /* {{{ */ if (timeout != NULL) curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout)); else - curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(plugin_get_interval())); + curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif return (0); diff --git a/src/bind.c b/src/bind.c index 32b0f16e..06b4ace0 100644 --- a/src/bind.c +++ b/src/bind.c @@ -1759,7 +1759,7 @@ static int bind_init (void) /* {{{ */ curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L); #ifdef HAVE_CURLOPT_TIMEOUT_MS curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (timeout >= 0) ? - (long) timeout : CDTIME_T_TO_MS(plugin_get_interval())); + (long) timeout : (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif diff --git a/src/curl.c b/src/curl.c index ac4cc512..16ae3aba 100644 --- a/src/curl.c +++ b/src/curl.c @@ -418,8 +418,7 @@ static int cc_page_init_curl (web_page_t *wp) /* {{{ */ if (wp->timeout >= 0) curl_easy_setopt (wp->curl, CURLOPT_TIMEOUT_MS, (long) wp->timeout); else - curl_easy_setopt (wp->curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(plugin_get_interval())); + curl_easy_setopt (wp->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif return (0); diff --git a/src/curl_json.c b/src/curl_json.c index 80632424..510d9b62 100644 --- a/src/curl_json.c +++ b/src/curl_json.c @@ -654,11 +654,9 @@ static int cj_init_curl (cj_t *db) /* {{{ */ if (db->timeout >= 0) curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout); else if (db->interval > 0) - curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(db->timeout)); + curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(db->timeout)); else - curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(plugin_get_interval())); + curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif return (0); diff --git a/src/curl_xml.c b/src/curl_xml.c index 8a466bae..5a1f2bab 100644 --- a/src/curl_xml.c +++ b/src/curl_xml.c @@ -897,8 +897,7 @@ static int cx_init_curl (cx_t *db) /* {{{ */ if (db->timeout >= 0) curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout); else - curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(plugin_get_interval())); + curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif return (0); diff --git a/src/daemon/utils_time.h b/src/daemon/utils_time.h index f2d474a0..6476780a 100644 --- a/src/daemon/utils_time.h +++ b/src/daemon/utils_time.h @@ -52,25 +52,25 @@ ((((((cdtime_t) (ns)) % 1000000000) << 30) + 500000000) / 1000000000)) #define CDTIME_T_TO_TIME_T(t) ((((time_t) (t)) + (1 << 29)) >> 30) -#define CDTIME_T_TO_MS(t) (((((long) (t)) >> 30) * 1000L) + \ - (((((long) (t)) & 0x3fffffff) * 1000L + (1 << 29)) >> 30)) -#define CDTIME_T_TO_US(t) (((((suseconds_t) (t)) >> 30) * 1000000L) + \ - (((((suseconds_t) (t)) & 0x3fffffff) * 1000000L + (1 << 29)) >> 30)) -#define CDTIME_T_TO_NS(t) (((((long) (t)) >> 30) * 1000000000L) + \ - (((((long) (t)) & 0x3fffffff) * 1000000000L + (1 << 29)) >> 30)) +#define CDTIME_T_TO_MS(t) (((((uint64_t) (t)) >> 30) * 1000L) + \ + (((((uint64_t) (t)) & 0x3fffffff) * 1000L + (1 << 29)) >> 30)) +#define CDTIME_T_TO_US(t) (((((uint64_t) (t)) >> 30) * 1000000L) + \ + (((((uint64_t) (t)) & 0x3fffffff) * 1000000L + (1 << 29)) >> 30)) +#define CDTIME_T_TO_NS(t) (((((uint64_t) (t)) >> 30) * 1000000000L) + \ + (((((uint64_t) (t)) & 0x3fffffff) * 1000000000L + (1 << 29)) >> 30)) #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0) #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0)) #define CDTIME_T_TO_TIMEVAL(cdt,tvp) do { \ (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ - (tvp)->tv_usec = CDTIME_T_TO_US ((cdt) & 0x3fffffff); \ + (tvp)->tv_usec = (suseconds_t) CDTIME_T_TO_US ((cdt) & 0x3fffffff); \ } while (0) #define TIMEVAL_TO_CDTIME_T(tv) US_TO_CDTIME_T(1000000 * (tv)->tv_sec + (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) & 0x3fffffff); \ + (tsp)->tv_nsec = (long) CDTIME_T_TO_NS ((cdt) & 0x3fffffff); \ } while (0) #define TIMESPEC_TO_CDTIME_T(ts) NS_TO_CDTIME_T(1000000000 * (ts)->tv_sec + (ts)->tv_nsec) diff --git a/src/daemon/utils_time_test.c b/src/daemon/utils_time_test.c index 1f135569..0c527290 100644 --- a/src/daemon/utils_time_test.c +++ b/src/daemon/utils_time_test.c @@ -93,7 +93,7 @@ DEF_TEST(conversion) DEF_TEST(ns_to_cdtime) { struct { - long ns; + uint64_t ns; cdtime_t want; } cases[] = { // 1439981652801860766 * 2^30 / 10^9 = 1546168526406004689.4 diff --git a/src/nginx.c b/src/nginx.c index 4e4ce3bb..69ec06dc 100644 --- a/src/nginx.c +++ b/src/nginx.c @@ -188,8 +188,7 @@ static int init (void) } else { - curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, - CDTIME_T_TO_MS(plugin_get_interval())); + curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); } #endif -- 2.11.0