ryantimwilson / rpms / systemd

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