Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-bus.c
index 4a4314b..f05ddea 100644 (file)
@@ -22,6 +22,7 @@
  *
  */
 
+#include <config.h>
 #include "dbus-bus.h"
 #include "dbus-protocol.h"
 #include "dbus-internals.h"
@@ -29,7 +30,7 @@
 #include "dbus-marshal-validate.h"
 #include "dbus-threads-internal.h"
 #include "dbus-connection-internal.h"
-#include <string.h>
+#include "dbus-string.h"
 
 /**
  * @defgroup DBusBus Message bus APIs
@@ -147,6 +148,63 @@ get_from_env (char           **connection_p,
 }
 
 static dbus_bool_t
+init_session_address (void)
+{
+  dbus_bool_t retval;
+  retval = FALSE;
+
+  /* First, look in the environment.  This is the normal case on 
+   * freedesktop.org/Unix systems. */
+  get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
+                     "DBUS_SESSION_BUS_ADDRESS");
+  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+    {
+      dbus_bool_t supported;
+      DBusString addr;
+      DBusError error = DBUS_ERROR_INIT;
+
+      if (!_dbus_string_init (&addr))
+        return FALSE;
+
+      supported = FALSE;
+      /* So it's not in the environment - let's try a platform-specific method.
+       * On MacOS, this involves asking launchd.  On Windows (not specified yet)
+       * we might do a COM lookup.
+       * Ignore errors - if we failed, fall back to autolaunch. */
+      retval = _dbus_lookup_session_address (&supported, &addr, &error);
+      if (supported && retval)
+        {
+          retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
+        }
+      else if (supported && !retval)
+        {
+          if (dbus_error_is_set(&error))
+            _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
+          else
+            _dbus_warn ("Dynamic session lookup supported but failed silently\n");
+        }
+      _dbus_string_free (&addr);
+    }
+  else
+    retval = TRUE;
+
+  if (!retval)
+    return FALSE;
+
+  /* The DBUS_SESSION_BUS_DEFAULT_ADDRESS should have really been named
+   * DBUS_SESSION_BUS_FALLBACK_ADDRESS. 
+   */
+  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+    bus_connection_addresses[DBUS_BUS_SESSION] =
+      _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
+  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
+    return FALSE;
+
+  return TRUE;
+}
+
+static dbus_bool_t
 init_connections_unlocked (void)
 {
   if (!initialized)
@@ -198,17 +256,9 @@ init_connections_unlocked (void)
         {
           _dbus_verbose ("Filling in session bus address...\n");
           
-          if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
-                             "DBUS_SESSION_BUS_ADDRESS"))
+          if (!init_session_address ())
             return FALSE;
 
-         if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
-           bus_connection_addresses[DBUS_BUS_SESSION] =
-             _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
-          
-          if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
-             return FALSE;
-
           _dbus_verbose ("  \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
                          bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
         }
@@ -611,6 +661,8 @@ dbus_bus_register (DBusConnection *connection,
   _dbus_return_val_if_error_is_set (error, FALSE);
 
   retval = FALSE;
+  message = NULL;
+  reply = NULL;
 
   _DBUS_LOCK (bus_datas);
 
@@ -618,18 +670,16 @@ dbus_bus_register (DBusConnection *connection,
   if (bd == NULL)
     {
       _DBUS_SET_OOM (error);
-      _DBUS_UNLOCK (bus_datas);
-      return FALSE;
+      goto out;
     }
 
   if (bd->unique_name != NULL)
     {
       _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
                      bd->unique_name);
-      _DBUS_UNLOCK (bus_datas);
-
       /* Success! */
-      return TRUE;
+      retval = TRUE;
+      goto out;
     }
   
   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
@@ -640,15 +690,11 @@ dbus_bus_register (DBusConnection *connection,
   if (!message)
     {
       _DBUS_SET_OOM (error);
-
-      _DBUS_UNLOCK (bus_datas);
-      return FALSE;
+      goto out;
     }
   
   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
 
-  dbus_message_unref (message);
-  
   if (reply == NULL)
     goto out;
   else if (dbus_set_error_from_message (error, reply))
@@ -668,14 +714,17 @@ dbus_bus_register (DBusConnection *connection,
   retval = TRUE;
   
  out:
+  _DBUS_UNLOCK (bus_datas);
+
+  if (message)
+    dbus_message_unref (message);
+
   if (reply)
     dbus_message_unref (reply);
 
   if (!retval)
     _DBUS_ASSERT_ERROR_IS_SET (error);
 
-  _DBUS_UNLOCK (bus_datas);
-  
   return retval;
 }
 
@@ -719,7 +768,7 @@ dbus_bus_set_unique_name (DBusConnection *connection,
                           const char     *unique_name)
 {
   BusData *bd;
-  dbus_bool_t success;
+  dbus_bool_t success = FALSE;
 
   _dbus_return_val_if_fail (connection != NULL, FALSE);
   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
@@ -728,13 +777,14 @@ dbus_bus_set_unique_name (DBusConnection *connection,
   
   bd = ensure_bus_data (connection);
   if (bd == NULL)
-    return FALSE;
+    goto out;
 
   _dbus_assert (bd->unique_name == NULL);
   
   bd->unique_name = _dbus_strdup (unique_name);
   success = bd->unique_name != NULL;
-  
+
+out:
   _DBUS_UNLOCK (bus_datas);
   
   return success;
@@ -762,7 +812,7 @@ const char*
 dbus_bus_get_unique_name (DBusConnection *connection)
 {
   BusData *bd;
-  const char *unique_name;
+  const char *unique_name = NULL;
 
   _dbus_return_val_if_fail (connection != NULL, NULL);
 
@@ -770,12 +820,13 @@ dbus_bus_get_unique_name (DBusConnection *connection)
   
   bd = ensure_bus_data (connection);
   if (bd == NULL)
-    return NULL;
+    goto out;
 
   unique_name = bd->unique_name;
 
+out:
   _DBUS_UNLOCK (bus_datas);
-  
+
   return unique_name;
 }
 
@@ -1382,11 +1433,17 @@ send_no_return_values (DBusConnection *connection,
  * If you pass #NULL for the error, this function will not
  * block; the match thus won't be added until you flush the
  * connection, and if there's an error adding the match
- * (only possible error is lack of resources in the bus),
- * you won't find out about it.
+ * you won't find out about it. This is generally acceptable, since the
+ * possible errors (including a lack of resources in the bus, the connection
+ * having exceeded its quota of active match rules, or the match rule being
+ * unparseable) are generally unrecoverable.
  *
  * If you pass non-#NULL for the error this function will
- * block until it gets a reply.
+ * block until it gets a reply. This may be useful when using match rule keys
+ * introduced in recent versions of D-Bus, like 'arg0namespace', to allow the
+ * application to fall back to less efficient match rules supported by older
+ * versions of the daemon if the running version is not new enough; or when
+ * using user-supplied rules rather than rules hard-coded at compile time.
  *
  * Normal API conventions would have the function return
  * a boolean value indicating whether the error was set,