Bug 14259 - Make session address lookup system-dependent
authorColin Walters <walters@verbum.org>
Mon, 13 Jul 2009 16:47:19 +0000 (12:47 -0400)
committerColin Walters <walters@verbum.org>
Mon, 13 Jul 2009 16:47:19 +0000 (12:47 -0400)
On some platforms such as MacOS X and Windows, we can't depend
on an environment variable to determine the address of the
session bus.  Create a sysdep function dbus_lookup_session_address
which can be filled in with platform-specific code.

dbus/dbus-bus.c
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps-win.c
dbus/dbus-sysdeps.h

index 4a4314b..92ec20e 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");
         }
index ad85204..d10d48b 100644 (file)
@@ -3117,6 +3117,37 @@ _dbus_read_local_machine_uuid (DBusGUID   *machine_id,
 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
 
+/**
+ * Determines the address of the session bus by querying a
+ * platform-specific method.  
+ *
+ * The first parameter will be a boolean specifying whether
+ * or not a dynamic session lookup is supported on this platform.
+ * 
+ * If supported is TRUE and the return value is #TRUE, the
+ * address will be  appended to @p address.
+ * If a failure happens, returns #FALSE and sets an error in 
+ * @p error.
+ *
+ * If supported is FALSE, ignore the return value.
+ *
+ * @param supported returns whether this method is supported
+ * @param address a DBusString where the address can be stored
+ * @param error a DBusError to store the error in case of failure
+ * @returns #TRUE on success, #FALSE if an error happened
+ */
+dbus_bool_t
+_dbus_lookup_session_address (dbus_bool_t *supported,
+                              DBusString  *address,
+                              DBusError   *error)
+{
+  /* On non-Mac Unix platforms, if the session address isn't already
+   * set in DBUS_SESSION_BUS_ADDRESS environment variable, we punt and
+   * fall back to the autolaunch: global default; see 
+   * init_session_address in dbus/dbus-bus.c. */
+  *supported = FALSE;
+  return TRUE;
+}
 
 /**
  * Returns the standard directories for a session bus to look for service 
index c62e145..02f3123 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright (C) 2003 CodeFactory AB
  * Copyright (C) 2005 Novell, Inc.
  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
- * Copyright (C) 2006 Peter Kümmel  <syntheticpp@gmx.net>
+ * Copyright (C) 2006 Peter Kümmel  <syntheticpp@gmx.net>
  * Copyright (C) 2006 Christian Ehrlicher <ch.ehrlicher@gmx.de>
  *
  * Licensed under the Academic Free License version 2.1
@@ -3281,6 +3281,17 @@ _dbus_append_session_config_file (DBusString *str)
   return _dbus_get_config_file_name(str, "session.conf");
 }
 
+/* See comment in dbus-sysdeps-unix.c */
+dbus_bool_t
+_dbus_lookup_session_address (dbus_bool_t *supported,
+                              DBusString  *address,
+                              DBusError   *error)
+{
+  /* Probably fill this in with something based on COM? */
+  *supported = FALSE;
+  return TRUE;
+}
+
 /**
  * Appends the directory in which a keyring for the given credentials
  * should be stored.  The credentials should have either a Windows or
index 7af4287..2fd5421 100644 (file)
@@ -468,6 +468,10 @@ void _dbus_log_security (const char *msg, va_list args);
 dbus_bool_t _dbus_get_autolaunch_address (DBusString *address, 
                                          DBusError *error);
 
+dbus_bool_t _dbus_lookup_session_address (dbus_bool_t *supported,
+                                          DBusString  *address,
+                                          DBusError   *error);
+
 /** Type representing a universally unique ID
  * @todo rename to UUID instead of GUID
  */