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