summaryrefslogtreecommitdiffstats
path: root/src/basic/time-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-05-29 12:55:33 +0200
committerLennart Poettering <lennart@poettering.net>2018-06-06 10:55:45 +0200
commit4f811d27d6d9324e65ce628fa20ac1761ef98de0 (patch)
treeb2673f25c2e521455ec92a2440cf1e86370ca1df /src/basic/time-util.c
parentcore: subscribe to /etc/localtime timezone changes and update timer elapsatio... (diff)
downloadsystemd-4f811d27d6d9324e65ce628fa20ac1761ef98de0.tar.xz
systemd-4f811d27d6d9324e65ce628fa20ac1761ef98de0.zip
time-util: introduce common implementation of TFD_TIMER_CANCEL_ON_SET client code
We now use pretty much the same code at three places, let's unify that.
Diffstat (limited to 'src/basic/time-util.c')
-rw-r--r--src/basic/time-util.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 671311e885..031b871640 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -1463,3 +1463,27 @@ bool in_utc_timezone(void) {
return timezone == 0 && daylight == 0;
}
+
+int time_change_fd(void) {
+
+ /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
+ static const struct itimerspec its = {
+ .it_value.tv_sec = TIME_T_MAX,
+ };
+
+ _cleanup_close_ int fd;
+
+ assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
+
+ /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
+ * CLOCK_MONOTONIC. */
+
+ fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+
+ if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0)
+ return -errno;
+
+ return TAKE_FD(fd);
+}