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