From 7902064611ec4cc68960b3ca68fb92a1fd0b5137 Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Jul 30 2020 15:20:10 +0000 Subject: Revert "Adapt to the new behavior of disconnect in dbus-broker" This reverts commit 39ce89ec821d02643681795d2149b20198f0fe42. systemd will kill certmonger anyway. Let it go ahead and die and we'll use PartOf to link the two services together instead. --- diff --git a/src/tdbus.c b/src/tdbus.c index 402022e..cb0a8ad 100644 --- a/src/tdbus.c +++ b/src/tdbus.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -523,24 +522,60 @@ cm_tdbus_timeout_cleanup(void *data) } static void -cm_tdbus_disconnected(struct tevent_context *ec, struct tevent_timer *timer, +cm_tdbus_reconnect(struct tevent_context *ec, struct tevent_timer *timer, struct timeval current_time, void *pvt) { + const char *bus_desc; struct tdbus_connection *tdb; - pid_t pid; + struct timeval later; + dbus_bool_t exit_on_disconnect = TRUE; tdb = pvt; talloc_free(timer); if ((tdb->conn == NULL) || !dbus_connection_get_is_connected(tdb->conn)) { - /* Close the current connection and exit. */ + /* Close the current connection and open a new one. */ if (tdb->conn != NULL) { dbus_connection_unref(tdb->conn); tdb->conn = NULL; } - pid = getpid(); - cm_log(0, "Disconnected from dbus, exiting with SIGTERM.\n"); - kill(pid, SIGTERM); + bus_desc = NULL; + switch (tdb->conn_type) { + case cm_tdbus_system: + cm_log(1, "Attempting to reconnect to system bus.\n"); + tdb->conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); + cm_set_conn_ptr(tdb->data, tdb->conn); + /* Don't exit if we get disconnected. */ + exit_on_disconnect = FALSE; + bus_desc = "system"; + break; + case cm_tdbus_session: + cm_log(1, "Attempting to reconnect to session bus.\n"); + tdb->conn = dbus_bus_get(DBUS_BUS_SESSION, NULL); + cm_set_conn_ptr(tdb->data, tdb->conn); + /* Exit if we get disconnected. */ + exit_on_disconnect = TRUE; + bus_desc = "session"; + break; + case cm_tdbus_private: + abort(); + break; + } + if ((tdb->conn != NULL) && + dbus_connection_get_is_connected(tdb->conn)) { + /* We're reconnected; reset our handlers. */ + cm_log(1, "Reconnected to %s bus.\n", bus_desc); + dbus_connection_set_exit_on_disconnect(tdb->conn, + exit_on_disconnect); + cm_tdbus_setup_public_connection(tdb, tdb->conn, + bus_desc, NULL); + } else { + /* Try reconnecting again later. */ + later = tevent_timeval_current_ofs(CM_DBUS_RECONNECT_TIMEOUT, 0), + tevent_add_timer(ec, tdb, later, + cm_tdbus_reconnect, + tdb); + } } } @@ -550,12 +585,12 @@ cm_tdbus_filter(DBusConnection *conn, DBusMessage *dmessage, void *data) struct tdbus_connection *tdb = data; const char *destination, *unique_name, *path, *interface, *member; - /* If we're disconnected, queue an exit. */ + /* If we're disconnected, queue a reconnect. */ if ((tdb->conn_type != cm_tdbus_private) && !dbus_connection_get_is_connected(conn)) { tevent_add_timer(talloc_parent(tdb), tdb, tevent_timeval_current(), - cm_tdbus_disconnected, + cm_tdbus_reconnect, tdb); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } @@ -710,6 +745,7 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type, DBusError err; const char *bus_desc; struct tdbus_connection *tdb; + dbus_bool_t exit_on_disconnect; /* Build our own context. */ tdb = talloc_ptrtype(ec, tdb); @@ -721,6 +757,7 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type, /* Connect to the right bus. */ bus_desc = NULL; conn = NULL; + exit_on_disconnect = TRUE; if (error != NULL) { dbus_error_init(error); } @@ -728,11 +765,15 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type, case cm_tdbus_system: conn = dbus_bus_get(DBUS_BUS_SYSTEM, error); cm_set_conn_ptr(data, conn); + /* Don't exit if we get disconnected. */ + exit_on_disconnect = FALSE; bus_desc = "system"; break; case cm_tdbus_session: conn = dbus_bus_get(DBUS_BUS_SESSION, error); cm_set_conn_ptr(data, conn); + /* Exit if we get disconnected. */ + exit_on_disconnect = TRUE; bus_desc = "session"; break; case cm_tdbus_private: @@ -744,8 +785,7 @@ cm_tdbus_setup_public(struct tevent_context *ec, enum cm_tdbus_type bus_type, talloc_free(tdb); return -1; } - /* Exit on disconnect is handled in cm_tdbus_disconnected(). */ - dbus_connection_set_exit_on_disconnect(conn, FALSE); + dbus_connection_set_exit_on_disconnect(conn, exit_on_disconnect); tdb->conn = conn; tdb->conn_type = bus_type; tdb->data = data;