teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0693-sd-event-introduce-callback-invoked-when-event-sourc.patch

be0c12
From 51210a849ea7f163a1760de989756206c01dd758 Mon Sep 17 00:00:00 2001
be0c12
From: Michal Sekletar <msekleta@redhat.com>
be0c12
Date: Mon, 4 Oct 2021 19:44:06 +0200
be0c12
Subject: [PATCH] sd-event: introduce callback invoked when event source
be0c12
 ratelimit expires
be0c12
be0c12
(cherry picked from commit fd69f2247520b0be3190ded96d646a415edc97b7)
be0c12
be0c12
Related: #2036608
be0c12
---
be0c12
 src/libsystemd/libsystemd.sym        |  5 +++
be0c12
 src/libsystemd/sd-event/sd-event.c   | 61 +++++++++++++++++++++++-----
be0c12
 src/libsystemd/sd-event/test-event.c | 12 ++++++
be0c12
 src/systemd/sd-event.h               |  1 +
be0c12
 4 files changed, 68 insertions(+), 11 deletions(-)
be0c12
be0c12
diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym
be0c12
index 149d2e7b82..f4a1426248 100644
be0c12
--- a/src/libsystemd/libsystemd.sym
be0c12
+++ b/src/libsystemd/libsystemd.sym
be0c12
@@ -579,3 +579,8 @@ global:
be0c12
         sd_event_source_get_ratelimit;
be0c12
         sd_event_source_is_ratelimited;
be0c12
 } LIBSYSTEMD_239;
be0c12
+
be0c12
+LIBSYSTEMD_250 {
be0c12
+global:
be0c12
+        sd_event_source_set_ratelimit_expire_callback;
be0c12
+} LIBSYSTEMD_248;
be0c12
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
be0c12
index 47cf93b3f4..0adfdd9e1a 100644
be0c12
--- a/src/libsystemd/sd-event/sd-event.c
be0c12
+++ b/src/libsystemd/sd-event/sd-event.c
be0c12
@@ -125,6 +125,7 @@ struct sd_event_source {
be0c12
         uint64_t prepare_iteration;
be0c12
 
be0c12
         sd_event_destroy_t destroy_callback;
be0c12
+        sd_event_handler_t ratelimit_expire_callback;
be0c12
 
be0c12
         LIST_FIELDS(sd_event_source, sources);
be0c12
 
be0c12
@@ -2734,7 +2735,7 @@ fail:
be0c12
         return r;
be0c12
 }
be0c12
 
be0c12
-static int event_source_leave_ratelimit(sd_event_source *s) {
be0c12
+static int event_source_leave_ratelimit(sd_event_source *s, bool run_callback) {
be0c12
         int r;
be0c12
 
be0c12
         assert(s);
be0c12
@@ -2766,6 +2767,23 @@ static int event_source_leave_ratelimit(sd_event_source *s) {
be0c12
         ratelimit_reset(&s->rate_limit);
be0c12
 
be0c12
         log_debug("Event source %p (%s) left rate limit state.", s, strna(s->description));
be0c12
+
be0c12
+        if (run_callback && s->ratelimit_expire_callback) {
be0c12
+                s->dispatching = true;
be0c12
+                r = s->ratelimit_expire_callback(s, s->userdata);
be0c12
+                s->dispatching = false;
be0c12
+
be0c12
+                if (r < 0) {
be0c12
+                        log_debug_errno(r, "Ratelimit expiry callback of event source %s (type %s) returned error, disabling: %m",
be0c12
+                                        strna(s->description),
be0c12
+                                        event_source_type_to_string(s->type));
be0c12
+
be0c12
+                        sd_event_source_set_enabled(s, SD_EVENT_OFF);
be0c12
+                }
be0c12
+
be0c12
+                return 1;
be0c12
+        }
be0c12
+
be0c12
         return 0;
be0c12
 
be0c12
 fail:
be0c12
@@ -2966,6 +2984,7 @@ static int process_timer(
be0c12
                 struct clock_data *d) {
be0c12
 
be0c12
         sd_event_source *s;
be0c12
+        bool callback_invoked = false;
be0c12
         int r;
be0c12
 
be0c12
         assert(e);
be0c12
@@ -2981,9 +3000,11 @@ static int process_timer(
be0c12
                          * again. */
be0c12
                         assert(s->ratelimited);
be0c12
 
be0c12
-                        r = event_source_leave_ratelimit(s);
be0c12
+                        r = event_source_leave_ratelimit(s, /* run_callback */ true);
be0c12
                         if (r < 0)
be0c12
                                 return r;
be0c12
+                        else if (r == 1)
be0c12
+                                callback_invoked = true;
be0c12
 
be0c12
                         continue;
be0c12
                 }
be0c12
@@ -2998,7 +3019,7 @@ static int process_timer(
be0c12
                 event_source_time_prioq_reshuffle(s);
be0c12
         }
be0c12
 
be0c12
-        return 0;
be0c12
+        return callback_invoked;
be0c12
 }
be0c12
 
be0c12
 static int process_child(sd_event *e) {
be0c12
@@ -3698,15 +3719,15 @@ _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
be0c12
         if (r < 0)
be0c12
                 goto finish;
be0c12
 
be0c12
-        r = process_timer(e, e->timestamp.realtime, &e->realtime);
be0c12
+        r = process_inotify(e);
be0c12
         if (r < 0)
be0c12
                 goto finish;
be0c12
 
be0c12
-        r = process_timer(e, e->timestamp.boottime, &e->boottime);
be0c12
+        r = process_timer(e, e->timestamp.realtime, &e->realtime);
be0c12
         if (r < 0)
be0c12
                 goto finish;
be0c12
 
be0c12
-        r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
be0c12
+        r = process_timer(e, e->timestamp.boottime, &e->boottime);
be0c12
         if (r < 0)
be0c12
                 goto finish;
be0c12
 
be0c12
@@ -3718,16 +3739,27 @@ _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
be0c12
         if (r < 0)
be0c12
                 goto finish;
be0c12
 
be0c12
+        r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
be0c12
+        if (r < 0)
be0c12
+                goto finish;
be0c12
+        else if (r == 1) {
be0c12
+                /* Ratelimit expiry callback was called. Let's postpone processing pending sources and
be0c12
+                 * put loop in the initial state in order to evaluate (in the next iteration) also sources
be0c12
+                 * there were potentially re-enabled by the callback.
be0c12
+                 *
be0c12
+                 * Wondering why we treat only this invocation of process_timer() differently? Once event
be0c12
+                 * source is ratelimited we essentially transform it into CLOCK_MONOTONIC timer hence
be0c12
+                 * ratelimit expiry callback is never called for any other timer type. */
be0c12
+                r = 0;
be0c12
+                goto finish;
be0c12
+        }
be0c12
+
be0c12
         if (e->need_process_child) {
be0c12
                 r = process_child(e);
be0c12
                 if (r < 0)
be0c12
                         goto finish;
be0c12
         }
be0c12
 
be0c12
-        r = process_inotify(e);
be0c12
-        if (r < 0)
be0c12
-                goto finish;
be0c12
-
be0c12
         if (event_next_pending(e)) {
be0c12
                 e->state = SD_EVENT_PENDING;
be0c12
 
be0c12
@@ -4054,7 +4086,7 @@ _public_ int sd_event_source_set_ratelimit(sd_event_source *s, uint64_t interval
be0c12
 
be0c12
         /* When ratelimiting is configured we'll always reset the rate limit state first and start fresh,
be0c12
          * non-ratelimited. */
be0c12
-        r = event_source_leave_ratelimit(s);
be0c12
+        r = event_source_leave_ratelimit(s, /* run_callback */ false);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
 
be0c12
@@ -4062,6 +4094,13 @@ _public_ int sd_event_source_set_ratelimit(sd_event_source *s, uint64_t interval
be0c12
         return 0;
be0c12
 }
be0c12
 
be0c12
+_public_ int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, sd_event_handler_t callback) {
be0c12
+        assert_return(s, -EINVAL);
be0c12
+
be0c12
+        s->ratelimit_expire_callback = callback;
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 _public_ int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_interval, unsigned *ret_burst) {
be0c12
         assert_return(s, -EINVAL);
be0c12
 
be0c12
diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c
be0c12
index e3ee4cd5c3..9135b22839 100644
be0c12
--- a/src/libsystemd/sd-event/test-event.c
be0c12
+++ b/src/libsystemd/sd-event/test-event.c
be0c12
@@ -506,6 +506,11 @@ static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userd
be0c12
         return 0;
be0c12
 }
be0c12
 
be0c12
+static int expired = -1;
be0c12
+static int ratelimit_expired(sd_event_source *s, void *userdata) {
be0c12
+        return ++expired;
be0c12
+}
be0c12
+
be0c12
 static void test_ratelimit(void) {
be0c12
         _cleanup_close_pair_ int p[2] = {-1, -1};
be0c12
         _cleanup_(sd_event_unrefp) sd_event *e = NULL;
be0c12
@@ -568,12 +573,19 @@ static void test_ratelimit(void) {
be0c12
 
be0c12
         assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) >= 0);
be0c12
 
be0c12
+        /* Set callback that will be invoked when we leave rate limited state. */
be0c12
+        assert_se(sd_event_source_set_ratelimit_expire_callback(s, ratelimit_expired) >= 0);
be0c12
+
be0c12
         do {
be0c12
                 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
be0c12
         } while (!sd_event_source_is_ratelimited(s));
be0c12
 
be0c12
         log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited");
be0c12
         assert_se(count == 20);
be0c12
+
be0c12
+        /* Dispatch the event loop once more and check that ratelimit expiration callback got called */
be0c12
+        assert_se(sd_event_run(e, UINT64_MAX) >= 0);
be0c12
+        assert_se(expired == 0);
be0c12
 }
be0c12
 
be0c12
 int main(int argc, char *argv[]) {
be0c12
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
be0c12
index a17a9b3488..c2e9c9614d 100644
be0c12
--- a/src/systemd/sd-event.h
be0c12
+++ b/src/systemd/sd-event.h
be0c12
@@ -147,6 +147,7 @@ int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t
be0c12
 int sd_event_source_set_ratelimit(sd_event_source *s, uint64_t interval_usec, unsigned burst);
be0c12
 int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_interval_usec, unsigned *ret_burst);
be0c12
 int sd_event_source_is_ratelimited(sd_event_source *s);
be0c12
+int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, sd_event_handler_t callback);
be0c12
 
be0c12
 /* Define helpers so that __attribute__((cleanup(sd_event_unrefp))) and similar may be used. */
be0c12
 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event, sd_event_unref);