summaryrefslogtreecommitdiffstats
path: root/modules/detect_time_jump
diff options
context:
space:
mode:
Diffstat (limited to 'modules/detect_time_jump')
-rw-r--r--modules/detect_time_jump/.packaging/test.config4
-rw-r--r--modules/detect_time_jump/README.rst22
-rw-r--r--modules/detect_time_jump/detect_time_jump.lua45
3 files changed, 71 insertions, 0 deletions
diff --git a/modules/detect_time_jump/.packaging/test.config b/modules/detect_time_jump/.packaging/test.config
new file mode 100644
index 0000000..7ed0e60
--- /dev/null
+++ b/modules/detect_time_jump/.packaging/test.config
@@ -0,0 +1,4 @@
+-- SPDX-License-Identifier: GPL-3.0-or-later
+modules.load('detect_time_jump')
+assert(detect_time_jump)
+quit()
diff --git a/modules/detect_time_jump/README.rst b/modules/detect_time_jump/README.rst
new file mode 100644
index 0000000..066f5a3
--- /dev/null
+++ b/modules/detect_time_jump/README.rst
@@ -0,0 +1,22 @@
+.. SPDX-License-Identifier: GPL-3.0-or-later
+
+.. _mod-detect_time_jump:
+
+Detect discontinuous jumps in the system time
+=============================================
+
+This module detect discontinuous jumps in the system time when resolver
+is running. It clears cache when a significant backward time jumps occurs.
+
+Time jumps are usually created by NTP time change or by admin intervention.
+These change can affect cache records as they store timestamp and TTL in real
+time.
+
+If you want to preserve cache during time travel you should disable
+this module by ``modules.unload('detect_time_jump')``.
+
+Due to the way monotonic system time works on typical systems,
+suspend-resume cycles will be perceived as forward time jumps,
+but this direction of shift does not have the risk of using records
+beyond their intended TTL, so forward jumps do not cause erasing the cache.
+
diff --git a/modules/detect_time_jump/detect_time_jump.lua b/modules/detect_time_jump/detect_time_jump.lua
new file mode 100644
index 0000000..ab099e8
--- /dev/null
+++ b/modules/detect_time_jump/detect_time_jump.lua
@@ -0,0 +1,45 @@
+-- SPDX-License-Identifier: GPL-3.0-or-later
+-- Module interface
+local ffi = require('ffi')
+
+local mod = {}
+mod.threshold = 10 * min
+local event_id = nil
+
+-- Get time of last cache clear. Compute difference between realtime
+-- and monotonic time. Compute difference of actual realtime and monotonic
+-- time. In ideal case these differences should be almost same.
+-- If they differ more than mod.threshold value then clear cache.
+local function check_time()
+ local checkpoint = cache.checkpoint()
+ local cache_timeshift = checkpoint.walltime.sec * 1000 - checkpoint.monotime
+ local actual_timeshift = os.time() * 1000 - tonumber(ffi.C.kr_now())
+ local jump_backward = cache_timeshift - actual_timeshift
+ if jump_backward > mod.threshold then
+ log("Detected backwards time jump, clearing cache.\n" ..
+ "But what does that mean? It means your future hasn't been written yet."
+ )
+ cache.clear()
+ elseif -jump_backward > mod.threshold then
+ -- On Linux 4.17+ this shouldn't happen anymore: https://lwn.net/Articles/751482/
+ log("Detected forward time jump. (Suspend-resume, possibly.)")
+ cache.checkpoint(true)
+ end
+end
+
+function mod.init()
+ if event_id then
+ error("Module is already loaded.")
+ else
+ event_id = event.recurrent(1 * min , check_time)
+ end
+end
+
+function mod.deinit()
+ if event_id then
+ event.cancel(event_id)
+ event_id = nil
+ end
+end
+
+return mod