#165 Handle dbus restart better
Merged 3 years ago by rcritten. Opened 3 years ago by rcritten.
rcritten/certmonger restart  into  master

file modified
+51 -11
@@ -22,7 +22,6 @@ 

  #include <stdlib.h>

  #include <string.h>

  #include <unistd.h>

- #include <signal.h>

  

  #include <talloc.h>

  #include <tevent.h>
@@ -523,24 +522,60 @@ 

  }

  

  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 @@ 

  	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 @@ 

  	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 @@ 

  	/* 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 @@ 

  	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 @@ 

  		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;

@@ -1,6 +1,7 @@ 

  [Unit]

  Description=Certificate monitoring and PKI enrollment

  After=syslog.target network.target dbus.service

+ PartOf=dbus.service

  

  [Service]

  Type=dbus

Rather than trying to code around the new dbus provided by systemd rely on it instead.

This revokes the previous attempt at catching the restart and quitting gracefully ands uses PartOf in the systemd service file to have certmonger linked to dbus stops and restarts.

This will ensure that certmonger is running if dbus is restarted. No cores have been seen after reverting the original patch. This also restores reconnect if using a session bus.

Pull-Request has been merged by rcritten

3 years ago