86b31d
From 30f5836253f820086caa24fc9283344615b8fc00 Mon Sep 17 00:00:00 2001
86b31d
From: Lennart Poettering <lennart@poettering.net>
86b31d
Date: Tue, 28 Jul 2020 11:17:00 +0200
86b31d
Subject: [PATCH] sd-event: add relative timer calls
86b31d
86b31d
We frequently want to set a timer relative to the current time. Let's
86b31d
add an explicit API for this. This not only saves us a few lines of code
86b31d
everywhere and simplifies things, but also allows us to do correct
86b31d
overflow checking.
86b31d
86b31d
(cherry picked from commit d6a83dc48ad1981665ff427858ae8e59d4cfd6cb)
86b31d
86b31d
Related: #2122288
86b31d
---
86b31d
 src/libsystemd/libsystemd.sym      |  8 +++++-
86b31d
 src/libsystemd/sd-event/sd-event.c | 42 ++++++++++++++++++++++++++++++
86b31d
 src/systemd/sd-event.h             |  2 ++
86b31d
 3 files changed, 51 insertions(+), 1 deletion(-)
86b31d
86b31d
diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym
86b31d
index 3b55fc6473..449918093c 100644
86b31d
--- a/src/libsystemd/libsystemd.sym
86b31d
+++ b/src/libsystemd/libsystemd.sym
86b31d
@@ -578,12 +578,18 @@ LIBSYSTEMD_240 {
86b31d
         sd_bus_get_method_call_timeout;
86b31d
 } LIBSYSTEMD_239;
86b31d
 
86b31d
+LIBSYSTEMD_247 {
86b31d
+global:
86b31d
+        sd_event_add_time_relative;
86b31d
+        sd_event_source_set_time_relative;
86b31d
+} LIBSYSTEMD_240;
86b31d
+
86b31d
 LIBSYSTEMD_248 {
86b31d
 global:
86b31d
         sd_event_source_set_ratelimit;
86b31d
         sd_event_source_get_ratelimit;
86b31d
         sd_event_source_is_ratelimited;
86b31d
-} LIBSYSTEMD_240;
86b31d
+} LIBSYSTEMD_247;
86b31d
 
86b31d
 LIBSYSTEMD_250 {
86b31d
 global:
86b31d
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
86b31d
index 09d4584bf9..2c9d331bf2 100644
86b31d
--- a/src/libsystemd/sd-event/sd-event.c
86b31d
+++ b/src/libsystemd/sd-event/sd-event.c
86b31d
@@ -1415,6 +1415,31 @@ fail:
86b31d
         return r;
86b31d
 }
86b31d
 
86b31d
+_public_ int sd_event_add_time_relative(
86b31d
+                sd_event *e,
86b31d
+                sd_event_source **ret,
86b31d
+                clockid_t clock,
86b31d
+                uint64_t usec,
86b31d
+                uint64_t accuracy,
86b31d
+                sd_event_time_handler_t callback,
86b31d
+                void *userdata) {
86b31d
+
86b31d
+        usec_t t;
86b31d
+        int r;
86b31d
+
86b31d
+        /* Same as sd_event_add_time() but operates relative to the event loop's current point in time, and
86b31d
+         * checks for overflow. */
86b31d
+
86b31d
+        r = sd_event_now(e, clock, &t);
86b31d
+        if (r < 0)
86b31d
+                return r;
86b31d
+
86b31d
+        if (usec >= USEC_INFINITY - t)
86b31d
+                return -EOVERFLOW;
86b31d
+
86b31d
+        return sd_event_add_time(e, ret, clock, t + usec, accuracy, callback, userdata);
86b31d
+}
86b31d
+
86b31d
 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
86b31d
         assert(s);
86b31d
 
86b31d
@@ -2578,6 +2603,23 @@ _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
86b31d
         return 0;
86b31d
 }
86b31d
 
86b31d
+_public_ int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec) {
86b31d
+        usec_t t;
86b31d
+        int r;
86b31d
+
86b31d
+        assert_return(s, -EINVAL);
86b31d
+        assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
86b31d
+
86b31d
+        r = sd_event_now(s->event, event_source_type_to_clock(s->type), &t);
86b31d
+        if (r < 0)
86b31d
+                return r;
86b31d
+
86b31d
+        if (usec >= USEC_INFINITY - t)
86b31d
+                return -EOVERFLOW;
86b31d
+
86b31d
+        return sd_event_source_set_time(s, t + usec);
86b31d
+}
86b31d
+
86b31d
 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
86b31d
         assert_return(s, -EINVAL);
86b31d
         assert_return(usec, -EINVAL);
86b31d
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
86b31d
index c2e9c9614d..960bea1ac4 100644
86b31d
--- a/src/systemd/sd-event.h
86b31d
+++ b/src/systemd/sd-event.h
86b31d
@@ -87,6 +87,7 @@ sd_event* sd_event_unref(sd_event *e);
86b31d
 
86b31d
 int sd_event_add_io(sd_event *e, sd_event_source **s, int fd, uint32_t events, sd_event_io_handler_t callback, void *userdata);
86b31d
 int sd_event_add_time(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata);
86b31d
+int sd_event_add_time_relative(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata);
86b31d
 int sd_event_add_signal(sd_event *e, sd_event_source **s, int sig, sd_event_signal_handler_t callback, void *userdata);
86b31d
 int sd_event_add_child(sd_event *e, sd_event_source **s, pid_t pid, int options, sd_event_child_handler_t callback, void *userdata);
86b31d
 int sd_event_add_inotify(sd_event *e, sd_event_source **s, const char *path, uint32_t mask, sd_event_inotify_handler_t callback, void *userdata);
86b31d
@@ -136,6 +137,7 @@ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events);
86b31d
 int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
86b31d
 int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
86b31d
 int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
86b31d
+int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec);
86b31d
 int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
86b31d
 int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
86b31d
 int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock);