[Xfce4-commits] <thunar:master> Don't allow starting multiple daemons (bug #3814).

Nick Schermer noreply at xfce.org
Sun Sep 30 12:30:01 CEST 2012


Updating branch refs/heads/master
         to a05cae53be734fcb9c7ec7fa292ab4054d8c3ac0 (commit)
       from 4393f889f081f285c4cfd855bfd1bea730f28cf7 (commit)

commit a05cae53be734fcb9c7ec7fa292ab4054d8c3ac0
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Sep 30 12:26:29 2012 +0200

    Don't allow starting multiple daemons (bug #3814).
    
    Quit when the org.xfce.Thunar is already acquired. This is
    saver then quiting the running instance and take over, because
    the daemon could own windows or work on a transfer.
    
    In case things hang, the user can always run thunar -q and
    afterwards thunar --daemon.

 thunar/main.c                |   13 ++++++++---
 thunar/thunar-dbus-service.c |   43 +++++++++++++++++++++++++++++++++++++----
 thunar/thunar-dbus-service.h |    4 ++-
 3 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/thunar/main.c b/thunar/main.c
index 9de4bd3..b65c8e5 100644
--- a/thunar/main.c
+++ b/thunar/main.c
@@ -149,12 +149,12 @@ main (int argc, char **argv)
         {
           /* no error message, the GUI initialization failed */
           const gchar *display_name = gdk_get_display_arg_name ();
-          g_fprintf (stderr, _("Thunar: Failed to open display: %s\n"), (display_name != NULL) ? display_name : " ");
+          g_printerr (_("Thunar: Failed to open display: %s\n"), (display_name != NULL) ? display_name : " ");
         }
       else
         {
           /* yep, there's an error, so print it */
-          g_fprintf (stderr, _("Thunar: %s\n"), error->message);
+          g_printerr (_("Thunar: %s\n"), error->message);
           g_error_free (error);
         }
       return EXIT_FAILURE;
@@ -187,7 +187,7 @@ main (int argc, char **argv)
       /* try to terminate whatever is running */
       if (!thunar_dbus_client_terminate (&error))
         {
-          g_fprintf (stderr, "Thunar: Failed to terminate running instance: %s\n", error->message);
+          g_printerr ("Thunar: Failed to terminate running instance: %s\n", error->message);
           g_error_free (error);
           return EXIT_FAILURE;
         }
@@ -259,7 +259,7 @@ main (int argc, char **argv)
     {
       /* we failed to process the filenames or the bulk rename failed */
 error0:
-      g_fprintf (stderr, "Thunar: %s\n", error->message);
+      g_printerr ("Thunar: %s\n", error->message);
       g_object_unref (G_OBJECT (application));
       g_error_free (error);
       return EXIT_FAILURE;
@@ -278,6 +278,10 @@ error0:
 #ifdef HAVE_DBUS
       /* attach the D-Bus service */
       dbus_service = g_object_new (THUNAR_TYPE_DBUS_SERVICE, NULL);
+
+      /* check if the name was requested successfully */
+      if (!thunar_dbus_service_has_connection (dbus_service))
+        goto dbus_name_failed;
 #endif
     }
   else
@@ -294,6 +298,7 @@ error0:
   gtk_main ();
 
 #ifdef HAVE_DBUS
+  dbus_name_failed:
   if (dbus_service != NULL)
     g_object_unref (G_OBJECT (dbus_service));
 #endif
diff --git a/thunar/thunar-dbus-service.c b/thunar/thunar-dbus-service.c
index 33b9709..2e81061 100644
--- a/thunar/thunar-dbus-service.c
+++ b/thunar/thunar-dbus-service.c
@@ -258,7 +258,9 @@ thunar_dbus_service_class_init (ThunarDBusServiceClass *klass)
 static void
 thunar_dbus_service_init (ThunarDBusService *dbus_service)
 {
-  GError *error = NULL;
+  GError         *error = NULL;
+  DBusConnection *dbus_connection;
+  gint            result;
 
   /* try to connect to the session bus */
   dbus_service->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
@@ -268,10 +270,24 @@ thunar_dbus_service_init (ThunarDBusService *dbus_service)
       dbus_g_connection_register_g_object (dbus_service->connection, "/org/xfce/FileManager", G_OBJECT (dbus_service));
 
       /* request the org.xfce.Thunar name for Thunar */
-      dbus_bus_request_name (dbus_g_connection_get_connection (dbus_service->connection), "org.xfce.Thunar", DBUS_NAME_FLAG_REPLACE_EXISTING, NULL);
+      dbus_connection = dbus_g_connection_get_connection (dbus_service->connection);
+      result = dbus_bus_request_name (dbus_connection, "org.xfce.Thunar",
+                                      DBUS_NAME_FLAG_ALLOW_REPLACEMENT | DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
+
+      /* check if we successfully acquired the name */
+      if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+        {
+          g_printerr ("Thunar: D-BUS name org.xfce.Thunar already registered.\n");
+
+          /* unset connection */
+          dbus_g_connection_unref (dbus_service->connection);
+          dbus_service->connection = NULL;
+
+          return;
+        }
 
       /* request the org.xfce.FileManager name for Thunar */
-      dbus_bus_request_name (dbus_g_connection_get_connection (dbus_service->connection), "org.xfce.FileManager", DBUS_NAME_FLAG_REPLACE_EXISTING, NULL);
+      dbus_bus_request_name (dbus_connection, "org.xfce.FileManager", DBUS_NAME_FLAG_REPLACE_EXISTING, NULL);
 
       /* once we registered, unset dbus variables (bug #8800) */
       g_unsetenv ("DBUS_STARTER_ADDRESS");
@@ -280,7 +296,7 @@ thunar_dbus_service_init (ThunarDBusService *dbus_service)
   else
     {
       /* notify the user that D-BUS service won't be available */
-      g_fprintf (stderr, "Thunar: Failed to connect to the D-BUS session bus: %s\n", error->message);
+      g_printerr ("Thunar: Failed to connect to the D-BUS session bus: %s\n", error->message);
       g_error_free (error);
     }
 }
@@ -291,10 +307,18 @@ static void
 thunar_dbus_service_finalize (GObject *object)
 {
   ThunarDBusService *dbus_service = THUNAR_DBUS_SERVICE (object);
+  DBusConnection    *dbus_connection;
 
   /* release the D-BUS connection object */
   if (G_LIKELY (dbus_service->connection != NULL))
-    dbus_g_connection_unref (dbus_service->connection);
+    {
+      /* release the names */
+      dbus_connection = dbus_g_connection_get_connection (dbus_service->connection);
+      dbus_bus_release_name (dbus_connection, "org.xfce.Thunar", NULL);
+      dbus_bus_release_name (dbus_connection, "org.xfce.FileManager", NULL);
+
+      dbus_g_connection_unref (dbus_service->connection);
+    }
 
   /* check if we are connected to the trash bin */
   if (G_LIKELY (dbus_service->trash_bin != NULL))
@@ -1298,3 +1322,12 @@ thunar_dbus_service_terminate (ThunarDBusService *dbus_service,
   /* we cannot fail */
   return TRUE;
 }
+
+
+
+gboolean
+thunar_dbus_service_has_connection (ThunarDBusService *dbus_service)
+{
+  _thunar_return_val_if_fail (THUNAR_IS_DBUS_SERVICE (dbus_service), FALSE);
+  return dbus_service->connection != NULL;
+}
diff --git a/thunar/thunar-dbus-service.h b/thunar/thunar-dbus-service.h
index 4ec75e5..c2ef4f1 100644
--- a/thunar/thunar-dbus-service.h
+++ b/thunar/thunar-dbus-service.h
@@ -34,7 +34,9 @@ typedef struct _ThunarDBusService      ThunarDBusService;
 #define THUNAR_IS_DBUS_SERVICE_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_TYPE_DBUS_BRIGDE))
 #define THUNAR_DBUS_SERVICE_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_TYPE_DBUS_SERVICE, ThunarDBusServicetClass))
 
-GType thunar_dbus_service_get_type (void) G_GNUC_CONST;
+GType    thunar_dbus_service_get_type       (void) G_GNUC_CONST;
+
+gboolean thunar_dbus_service_has_connection (ThunarDBusService *dbus_service);
 
 G_END_DECLS;
 


More information about the Xfce4-commits mailing list