chiark / gitweb /
time-util: introduce common implementation of TFD_TIMER_CANCEL_ON_SET client code
authorLennart Poettering <lennart@poettering.net>
Tue, 29 May 2018 10:55:33 +0000 (12:55 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
We now use pretty much the same code at three places, let's unify that.

src/basic/time-util.c
src/basic/time-util.h

index dc8db13c261a5327451ec4f27833ffd5a449c8e6..71d6179efacd8156f397b29138452a59125b30ec 100644 (file)
@@ -1496,3 +1496,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);
+}
index fec0511091cefffda15b11f7d53e0e0ac0471d57..94534794a2c3287bfd017c9d38a04d927d0ab220 100644 (file)
@@ -209,3 +209,5 @@ static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
 #else
 #error "Yuck, time_t is neither 4 nor 8 bytes wide?"
 #endif
+
+int time_change_fd(void);