803fb7
From a5626cd14a7f27538033e1e110f9a827c0189526 Mon Sep 17 00:00:00 2001
803fb7
From: Krzysztof Kotlenga <k.kotlenga@sims.pl>
803fb7
Date: Thu, 24 Sep 2015 00:34:51 +0200
803fb7
Subject: [PATCH] sd-event: fix prepare priority queue comparison function
803fb7
803fb7
Otherwise a disabled event source can get swapped with an enabled one
803fb7
and cause a severe sd-event malfunction.
803fb7
803fb7
http://lists.freedesktop.org/archives/systemd-devel/2015-September/034356.html
803fb7
803fb7
Cherry-picked from: 8046c4576a68977a1089d2585866bfab8152661b
803fb7
Resolves: #1266479
803fb7
---
803fb7
 src/libsystemd/sd-event/sd-event.c | 12 ++++++------
803fb7
 1 file changed, 6 insertions(+), 6 deletions(-)
803fb7
803fb7
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
803fb7
index c6350be9f..1f1e6fe91 100644
803fb7
--- a/src/libsystemd/sd-event/sd-event.c
803fb7
+++ b/src/libsystemd/sd-event/sd-event.c
803fb7
@@ -231,6 +231,12 @@ static int prepare_prioq_compare(const void *a, const void *b) {
803fb7
         assert(x->prepare);
803fb7
         assert(y->prepare);
803fb7
 
803fb7
+        /* Enabled ones first */
803fb7
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
803fb7
+                return -1;
803fb7
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
803fb7
+                return 1;
803fb7
+
803fb7
         /* Move most recently prepared ones last, so that we can stop
803fb7
          * preparing as soon as we hit one that has already been
803fb7
          * prepared in the current iteration */
803fb7
@@ -239,12 +245,6 @@ static int prepare_prioq_compare(const void *a, const void *b) {
803fb7
         if (x->prepare_iteration > y->prepare_iteration)
803fb7
                 return 1;
803fb7
 
803fb7
-        /* Enabled ones first */
803fb7
-        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
803fb7
-                return -1;
803fb7
-        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
803fb7
-                return 1;
803fb7
-
803fb7
         /* Lower priority values first */
803fb7
         if (x->priority < y->priority)
803fb7
                 return -1;