From 7e56a3aec7bca563418e2b5ce7d980ca1c600394 Mon Sep 17 00:00:00 2001
From: Franck Bui <fbui@suse.com>
Date: Mon, 8 Nov 2021 15:16:42 +0100
Subject: [PATCH 1/1] core: prevent bus_init_api() from being called
 recursively

Follow-up for 1898f668ddf3721d19c66e94b3e9a106093b8c96.

Commit 1898f668ddf3721d19c66e94b3e9a106093b8c96 alone wasn't enough to fix
bsc#1190325 completely as it introduced a regression when systemd was starting
up: when PID1 was detecting the activation of dbus.service, it tried to connect
to the buses (manager_recheck_dbus()) and then synchronized the bus names
(manager_sync_bus_names()). But a service (avahi-daemon.service in this case)
could have already registered its bus name because the dbus process was already
spawned at that point and the avahi-daemon.service was also started even if it
has After=dbus.service because dbus.service has Type=simple. In this case
manager_sync_bus_names() would update the state of the avahi socket and thus
calling a second time manager_recheck_dbus(). This indeed made
manager_recheck_dbus() (and hence bus_init_api()) called recursively and the
code wasn't prepared for that.

See the backtrace below for the details.

This was addressed by commit 5f109056d536849f6079df9321042cf4838a41d3 however
the commit message is rather cryptic, the code dealing with bus initializations
is rather messy (for example why connecting to the buses before coldplugging
the units when reexecuting ? and why not doing so when reloading ?).

So let's use a simple fix instead, which seems more appropriate for SUSE/v234
that is going to fully enter in LTSS mode soon.

 (gdb) bt
 #0  0x00007f5154023707 in kill () from /lib64/libc.so.6
 #1  0x000055991106d839 in crash (sig=6) at ../../../src/core/main.c:194
 #2  <signal handler called>
 #3  0x00007f5154023420 in raise () from /lib64/libc.so.6
 #4  0x00007f5154024a01 in abort () from /lib64/libc.so.6
 #5  0x000055991119bb7b in log_assert_failed_realm (realm=LOG_REALM_SYSTEMD, text=0x5599111e4d8f "false", file=0x5599111e40b0 "../../../src/core/dbus.c", line=872,
     func=0x5599111e5678 <__PRETTY_FUNCTION__.17253> "bus_init_api") at ../../../src/basic/log.c:817
 #6  0x00005599110d6203 in bus_init_api (m=0x559911e52420) at ../../../src/core/dbus.c:872
 #7  0x00005599110821c4 in manager_recheck_dbus (m=0x559911e52420) at ../../../src/core/manager.c:3256
 #8  0x00005599111173cb in unit_notify (u=avahi-daemon.socket, os=UNIT_ACTIVE, ns=UNIT_ACTIVE, reload_success=true) at ../../../src/core/unit.c:2172
 #9  0x00005599110b2244 in socket_set_state (s=avahi-daemon.socket, state=SOCKET_RUNNING) at ../../../src/core/socket.c:1775
 #10 0x00005599110b88f4 in socket_trigger_notify (u=avahi-daemon.socket, other=avahi-daemon.service) at ../../../src/core/socket.c:3169
 #11 0x0000559911116b91 in unit_trigger_notify (u=avahi-daemon.service) at ../../../src/core/unit.c:1980
 #12 0x0000559911124e0f in job_finish_and_invalidate (j=0x559911f47d80, result=JOB_DONE, recursive=true, already=false) at ../../../src/core/job.c:958
 #13 0x0000559911116f34 in unit_notify (u=avahi-daemon.service, os=UNIT_ACTIVATING, ns=UNIT_ACTIVE, reload_success=true) at ../../../src/core/unit.c:2058
 #14 0x000055991109f635 in service_set_state (s=avahi-daemon.service, state=SERVICE_RUNNING) at ../../../src/core/service.c:985
 #15 0x00005599110a1f11 in service_enter_running (s=avahi-daemon.service, f=SERVICE_SUCCESS) at ../../../src/core/service.c:1748
 #16 0x00005599110a2089 in service_enter_start_post (s=avahi-daemon.service) at ../../../src/core/service.c:1781
 #17 0x00005599110aacbd in service_bus_name_owner_change (u=avahi-daemon.service, name=0x559911ee9ea0 "org.freedesktop.Avahi", old_owner=0x0, new_owner=0x559911eeff70 ":1.0") at ../../../src/core/service.c:3504
 #18 0x00005599110d5cd3 in manager_sync_bus_names (m=0x559911e52420, bus=0x559911e54080) at ../../../src/core/dbus.c:782
 #19 0x00005599110d606f in bus_setup_api (m=0x559911e52420, bus=0x559911e54080) at ../../../src/core/dbus.c:846
 #20 0x00005599110d63ea in bus_init_api (m=0x559911e52420) at ../../../src/core/dbus.c:903
 #21 0x00005599110821c4 in manager_recheck_dbus (m=0x559911e52420) at ../../../src/core/manager.c:3256
 #22 0x00005599111173cb in unit_notify (u=dbus.socket, os=UNIT_ACTIVE, ns=UNIT_ACTIVE, reload_success=true) at ../../../src/core/unit.c:2172
 #23 0x00005599110b2244 in socket_set_state (s=dbus.socket, state=SOCKET_RUNNING) at ../../../src/core/socket.c:1775
 #24 0x00005599110b88f4 in socket_trigger_notify (u=dbus.socket, other=dbus.service) at ../../../src/core/socket.c:3169
 #25 0x0000559911116b91 in unit_trigger_notify (u=dbus.service) at ../../../src/core/unit.c:1980
 #26 0x0000559911124e0f in job_finish_and_invalidate (j=0x559911f470f0, result=JOB_DONE, recursive=true, already=false) at ../../../src/core/job.c:958
 #27 0x0000559911116f34 in unit_notify (u=dbus.service, os=UNIT_INACTIVE, ns=UNIT_ACTIVE, reload_success=true) at ../../../src/core/unit.c:2058
 #28 0x000055991109f635 in service_set_state (s=dbus.service, state=SERVICE_RUNNING) at ../../../src/core/service.c:985
 #29 0x00005599110a1f11 in service_enter_running (s=dbus.service, f=SERVICE_SUCCESS) at ../../../src/core/service.c:1748
 #30 0x00005599110a2089 in service_enter_start_post (s=dbus.service) at ../../../src/core/service.c:1781
 #31 0x00005599110a254a in service_enter_start (s=dbus.service) at ../../../src/core/service.c:1862
 #32 0x00005599110a27b0 in service_enter_start_pre (s=dbus.service) at ../../../src/core/service.c:1918
 #33 0x00005599110a363a in service_start (u=dbus.service) at ../../../src/core/service.c:2131
 #34 0x000055991111532a in unit_start (u=dbus.service) at ../../../src/core/unit.c:1676
 #35 0x0000559911123aeb in job_perform_on_unit (j=0x7ffe5b4c41a8) at ../../../src/core/job.c:555
 #36 0x0000559911123e60 in job_run_and_invalidate (j=0x559911f470f0) at ../../../src/core/job.c:620
 #37 0x000055991107bef4 in manager_dispatch_run_queue (source=0x559911e51280, userdata=0x559911e52420) at ../../../src/core/manager.c:1650
 #38 0x00007f51558f0ac5 in source_dispatch (s=0x559911e51280) at ../../../src/libsystemd/sd-event/sd-event.c:2304
 #39 0x00007f51558f1cb7 in sd_event_dispatch (e=0x559911e50fe0) at ../../../src/libsystemd/sd-event/sd-event.c:2631
 #40 0x00007f51558f20d2 in sd_event_run (e=0x559911e50fe0, timeout=18446744073709551615) at ../../../src/libsystemd/sd-event/sd-event.c:2690
 #41 0x000055991107e9cf in manager_loop (m=0x559911e52420) at ../../../src/core/manager.c:2316
 #42 0x0000559911074c73 in main (argc=5, argv=0x7ffe5b4c47d8) at ../../../src/core/main.c:2012
---
 src/core/unit.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/core/unit.c b/src/core/unit.c
index 8e0a34dd26..13248f6cd0 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -2169,7 +2169,8 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su
         }
 
         manager_recheck_journal(m);
-        manager_recheck_dbus(m);
+        if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
+                manager_recheck_dbus(m);
         unit_trigger_notify(u);
 
         if (!MANAGER_IS_RELOADING(u->manager)) {
-- 
2.31.1

