Blame SOURCES/0044-Adapt-to-the-new-behavior-of-disconnect-in-dbus-brok.patch

f0b236
From bf409dbab5f5910de6c2691f978bb8644532728a Mon Sep 17 00:00:00 2001
f0b236
From: Rob Crittenden <rcritten@redhat.com>
f0b236
Date: Mon, 18 May 2020 15:01:02 -0400
f0b236
Subject: [PATCH] Adapt to the new behavior of disconnect in dbus-broker
f0b236
f0b236
The dbus server was replaced in Fedora-29 to use dbus-broker.
f0b236
f0b236
This server does not allow reconnects on a disconnect. certmonger
f0b236
was crashing as a result.
f0b236
f0b236
The only way to directly pass a message between the dbus queue and
f0b236
the main tevent handle is via a signal. So when a disconnect is
f0b236
detected send a SIGTERM to getpid() in order to force a graceful
f0b236
shutdown.
f0b236
f0b236
https://bugzilla.redhat.com/show_bug.cgi?id=1687698
f0b236
---
f0b236
 src/tdbus.c | 61 ++++++++++-------------------------------------------
f0b236
 1 file changed, 11 insertions(+), 50 deletions(-)
f0b236
f0b236
diff --git a/src/tdbus.c b/src/tdbus.c
f0b236
index a81b534..402022e 100644
f0b236
--- a/src/tdbus.c
f0b236
+++ b/src/tdbus.c
f0b236
@@ -22,6 +22,7 @@
f0b236
 #include <stdlib.h>
f0b236
 #include <string.h>
f0b236
 #include <unistd.h>
f0b236
+#include <signal.h>
f0b236
 
f0b236
 #include <talloc.h>
f0b236
 #include <tevent.h>
f0b236
@@ -522,60 +523,24 @@ cm_tdbus_timeout_cleanup(void *data)
f0b236
 }
f0b236
 
f0b236
 static void
f0b236
-cm_tdbus_reconnect(struct tevent_context *ec, struct tevent_timer *timer,
f0b236
+cm_tdbus_disconnected(struct tevent_context *ec, struct tevent_timer *timer,
f0b236
 		   struct timeval current_time, void *pvt)
f0b236
 {
f0b236
-	const char *bus_desc;
f0b236
 	struct tdbus_connection *tdb;
f0b236
-	struct timeval later;
f0b236
-	dbus_bool_t exit_on_disconnect = TRUE;
f0b236
+	pid_t pid;
f0b236
 
f0b236
 	tdb = pvt;
f0b236
 	talloc_free(timer);
f0b236
 	if ((tdb->conn == NULL) ||
f0b236
 	    !dbus_connection_get_is_connected(tdb->conn)) {
f0b236
-		/* Close the current connection and open a new one. */
f0b236
+		/* Close the current connection and exit. */
f0b236
 		if (tdb->conn != NULL) {
f0b236
 			dbus_connection_unref(tdb->conn);
f0b236
 			tdb->conn = NULL;
f0b236
 		}
f0b236
-		bus_desc = NULL;
f0b236
-		switch (tdb->conn_type) {
f0b236
-		case cm_tdbus_system:
f0b236
-			cm_log(1, "Attempting to reconnect to system bus.\n");
f0b236
-			tdb->conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
f0b236
-			cm_set_conn_ptr(tdb->data, tdb->conn);
f0b236
-			/* Don't exit if we get disconnected. */
f0b236
-			exit_on_disconnect = FALSE;
f0b236
-			bus_desc = "system";
f0b236
-			break;
f0b236
-		case cm_tdbus_session:
f0b236
-			cm_log(1, "Attempting to reconnect to session bus.\n");
f0b236
-			tdb->conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
f0b236
-			cm_set_conn_ptr(tdb->data, tdb->conn);
f0b236
-			/* Exit if we get disconnected. */
f0b236
-			exit_on_disconnect = TRUE;
f0b236
-			bus_desc = "session";
f0b236
-			break;
f0b236
-		case cm_tdbus_private:
f0b236
-			abort();
f0b236
-			break;
f0b236
-		}
f0b236
-		if ((tdb->conn != NULL) &&
f0b236
-		    dbus_connection_get_is_connected(tdb->conn)) {
f0b236
-			/* We're reconnected; reset our handlers. */
f0b236
-			cm_log(1, "Reconnected to %s bus.\n", bus_desc);
f0b236
-			dbus_connection_set_exit_on_disconnect(tdb->conn,
f0b236
-							       exit_on_disconnect);
f0b236
-			cm_tdbus_setup_public_connection(tdb, tdb->conn,
f0b236
-							 bus_desc, NULL);
f0b236
-		} else {
f0b236
-			/* Try reconnecting again later. */
f0b236
-			later = tevent_timeval_current_ofs(CM_DBUS_RECONNECT_TIMEOUT, 0),
f0b236
-			tevent_add_timer(ec, tdb, later,
f0b236
-					 cm_tdbus_reconnect,
f0b236
-					 tdb);
f0b236
-		}
f0b236
+		pid = getpid();
f0b236
+		cm_log(0, "Disconnected from dbus, exiting with SIGTERM.\n");
f0b236
+		kill(pid, SIGTERM);
f0b236
 	}
f0b236
 }
f0b236
 
f0b236
@@ -585,12 +550,12 @@ cm_tdbus_filter(DBusConnection *conn, DBusMessage *dmessage, void *data)
f0b236
 	struct tdbus_connection *tdb = data;
f0b236
 	const char *destination, *unique_name, *path, *interface, *member;
f0b236
 
f0b236
-	/* If we're disconnected, queue a reconnect. */
f0b236
+	/* If we're disconnected, queue an exit. */
f0b236
 	if ((tdb->conn_type != cm_tdbus_private) &&
f0b236
 	    !dbus_connection_get_is_connected(conn)) {
f0b236
 		tevent_add_timer(talloc_parent(tdb), tdb,
f0b236
 				 tevent_timeval_current(),
f0b236
-				 cm_tdbus_reconnect,
f0b236
+				 cm_tdbus_disconnected,
f0b236
 				 tdb);
f0b236
 		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
f0b236
 	}
f0b236
@@ -745,7 +710,6 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type,
f0b236
 	DBusError err;
f0b236
 	const char *bus_desc;
f0b236
 	struct tdbus_connection *tdb;
f0b236
-	dbus_bool_t exit_on_disconnect;
f0b236
 
f0b236
 	/* Build our own context. */
f0b236
 	tdb = talloc_ptrtype(ec, tdb);
f0b236
@@ -764,15 +728,11 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type,
f0b236
 	case cm_tdbus_system:
f0b236
 		conn = dbus_bus_get(DBUS_BUS_SYSTEM, error);
f0b236
 		cm_set_conn_ptr(data, conn);
f0b236
-		/* Don't exit if we get disconnected. */
f0b236
-		exit_on_disconnect = FALSE;
f0b236
 		bus_desc = "system";
f0b236
 		break;
f0b236
 	case cm_tdbus_session:
f0b236
 		conn = dbus_bus_get(DBUS_BUS_SESSION, error);
f0b236
 		cm_set_conn_ptr(data, conn);
f0b236
-		/* Exit if we get disconnected. */
f0b236
-		exit_on_disconnect = TRUE;
f0b236
 		bus_desc = "session";
f0b236
 		break;
f0b236
 	case cm_tdbus_private:
f0b236
@@ -784,7 +744,8 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type,
f0b236
 		talloc_free(tdb);
f0b236
 		return -1;
f0b236
 	}
f0b236
-	dbus_connection_set_exit_on_disconnect(conn, exit_on_disconnect);
f0b236
+	/* Exit on disconnect is handled in cm_tdbus_disconnected(). */
f0b236
+	dbus_connection_set_exit_on_disconnect(conn, FALSE);
f0b236
 	tdb->conn = conn;
f0b236
 	tdb->conn_type = bus_type;
f0b236
 	tdb->data = data;
f0b236
-- 
f0b236
2.21.1
f0b236