592caf
From 4c5fdbde7e745126f31542a70b45cc4faec094d2 Mon Sep 17 00:00:00 2001
592caf
From: Lennart Poettering <lennart@poettering.net>
592caf
Date: Wed, 30 Oct 2019 20:26:50 +0100
592caf
Subject: [PATCH] sd-event: refuse running default event loops in any other
592caf
 thread than the one they are default for
592caf
592caf
(cherry picked from commit e544601536ac13a288d7476f4400c7b0f22b7ea1)
592caf
592caf
Related: #1819868
592caf
---
592caf
 TODO                               | 1 -
592caf
 src/libsystemd/sd-event/sd-event.c | 5 +++++
592caf
 2 files changed, 5 insertions(+), 1 deletion(-)
592caf
592caf
diff --git a/TODO b/TODO
592caf
index 8f78000089..3100e067d6 100644
592caf
--- a/TODO
592caf
+++ b/TODO
592caf
@@ -581,7 +581,6 @@ Features:
592caf
   - allow multiple signal handlers per signal?
592caf
   - document chaining of signal handler for SIGCHLD and child handlers
592caf
   - define more intervals where we will shift wakeup intervals around in, 1h, 6h, 24h, ...
592caf
-  - generate a failure of a default event loop is executed out-of-thread
592caf
   - maybe add support for inotify events (which we can do safely now, with O_PATH)
592caf
 
592caf
 * investigate endianness issues of UUID vs. GUID
592caf
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
592caf
index 88641879cc..537a0b81d4 100644
592caf
--- a/src/libsystemd/sd-event/sd-event.c
592caf
+++ b/src/libsystemd/sd-event/sd-event.c
592caf
@@ -3360,6 +3360,11 @@ _public_ int sd_event_prepare(sd_event *e) {
592caf
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
592caf
         assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
592caf
 
592caf
+        /* Let's check that if we are a default event loop we are executed in the correct thread. We only do
592caf
+         * this check here once, since gettid() is typically not cached, and thus want to minimize
592caf
+         * syscalls */
592caf
+        assert_return(!e->default_event_ptr || e->tid == gettid(), -EREMOTEIO);
592caf
+
592caf
         if (e->exit_requested)
592caf
                 goto pending;
592caf