From e5743c1e0d822766cdd20756ec48ee781c6a2aee Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 29 May 2018 12:55:33 +0200 Subject: [PATCH] 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. --- src/basic/time-util.c | 24 ++++++++++++++++++++++++ src/basic/time-util.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/src/basic/time-util.c b/src/basic/time-util.c index dc8db13c2..71d6179ef 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -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); +} diff --git a/src/basic/time-util.h b/src/basic/time-util.h index fec051109..94534794a 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -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); -- 2.30.2