]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/dbus/DbusHandler.java
Shut down when dbus daemon connection goes away unexpectedly
[signal-cli] / src / main / java / org / asamk / signal / dbus / DbusHandler.java
index 8b1dd61e0161a496a83d9c7caca133ae9c102a59..2ed6d11fdf3d307bec4fdba5b6c4a725991783c8 100644 (file)
@@ -1,17 +1,21 @@
 package org.asamk.signal.dbus;
 
 import org.asamk.signal.DbusConfig;
+import org.asamk.signal.Shutdown;
 import org.asamk.signal.commands.exceptions.CommandException;
+import org.asamk.signal.commands.exceptions.IOErrorException;
 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
 import org.asamk.signal.commands.exceptions.UserErrorException;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.manager.MultiAccountManager;
+import org.freedesktop.dbus.connections.IDisconnectCallback;
 import org.freedesktop.dbus.connections.impl.DBusConnection;
 import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
 import org.freedesktop.dbus.exceptions.DBusException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -21,12 +25,18 @@ public class DbusHandler implements AutoCloseable {
 
     private final boolean isDbusSystem;
     private DBusConnection dBusConnection;
+    private final String busname;
 
     private final List<AutoCloseable> closeables = new ArrayList<>();
     private final DbusRunner dbusRunner;
     private final boolean noReceiveOnStart;
 
-    public DbusHandler(final boolean isDbusSystem, final Manager m, final boolean noReceiveOnStart) {
+    public DbusHandler(
+            final boolean isDbusSystem,
+            final String busname,
+            final Manager m,
+            final boolean noReceiveOnStart
+    ) {
         this.isDbusSystem = isDbusSystem;
         this.dbusRunner = (connection) -> {
             try {
@@ -35,9 +45,15 @@ public class DbusHandler implements AutoCloseable {
             }
         };
         this.noReceiveOnStart = noReceiveOnStart;
+        this.busname = busname;
     }
 
-    public DbusHandler(final boolean isDbusSystem, final MultiAccountManager c, final boolean noReceiveOnStart) {
+    public DbusHandler(
+            final boolean isDbusSystem,
+            final String busname,
+            final MultiAccountManager c,
+            final boolean noReceiveOnStart
+    ) {
         this.isDbusSystem = isDbusSystem;
         this.dbusRunner = (connection) -> {
             final var signalControl = new DbusSignalControlImpl(c, DbusConfig.getObjectPath());
@@ -72,6 +88,7 @@ public class DbusHandler implements AutoCloseable {
             }
         };
         this.noReceiveOnStart = noReceiveOnStart;
+        this.busname = busname;
     }
 
     public void init() throws CommandException {
@@ -79,9 +96,11 @@ public class DbusHandler implements AutoCloseable {
             throw new AssertionError("DbusHandler already initialized");
         }
         final var busType = isDbusSystem ? DBusConnection.DBusBusType.SYSTEM : DBusConnection.DBusBusType.SESSION;
-        logger.debug("Starting DBus server on {} bus: {}", busType, DbusConfig.getBusname());
+        logger.debug("Starting DBus server on {} bus: {}", busType, busname);
         try {
-            dBusConnection = DBusConnectionBuilder.forType(busType).build();
+            dBusConnection = DBusConnectionBuilder.forType(busType)
+                    .withDisconnectCallback(new DisconnectCallback())
+                    .build();
             dbusRunner.run(dBusConnection);
         } catch (DBusException e) {
             throw new UnexpectedErrorException("Dbus command failed: " + e.getMessage(), e);
@@ -90,13 +109,13 @@ public class DbusHandler implements AutoCloseable {
         }
 
         try {
-            dBusConnection.requestBusName(DbusConfig.getBusname());
+            dBusConnection.requestBusName(busname);
         } catch (DBusException e) {
             throw new UnexpectedErrorException("Dbus command failed, maybe signal-cli dbus daemon is already running: "
                     + e.getMessage(), e);
         }
 
-        logger.info("Started DBus server on {} bus: {}", busType, DbusConfig.getBusname());
+        logger.info("Started DBus server on {} bus: {}", busType, busname);
     }
 
     @Override
@@ -128,4 +147,13 @@ public class DbusHandler implements AutoCloseable {
 
         void run(DBusConnection connection) throws DBusException;
     }
+
+    private static final class DisconnectCallback implements IDisconnectCallback {
+
+        @Override
+        public void disconnectOnError(IOException ex) {
+            logger.debug("DBus daemon disconnected unexpectedly, shutting down");
+            Shutdown.triggerShutdown(new IOErrorException("Unexpected dbus daemon disconnect", ex));
+        }
+    }
 }