[daemon-fix] fixed querying about name information
[platform/upstream/dbus.git] / dbus / dbus-server.c
index d9c7f02..ba993d1 100644 (file)
@@ -1,4 +1,4 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-server.c DBusServer object
  *
  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */ 
 
+#include <config.h>
 #include "dbus-server.h"
 #include "dbus-server-unix.h"
+#include "dbus-server-socket.h"
 #include "dbus-string.h"
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-server-debug-pipe.h"
 #endif
 #include "dbus-address.h"
  * @ingroup  DBus
  * @brief Server that listens for new connections.
  *
- * Types and functions related to DBusServer.
  * A DBusServer represents a server that other applications
  * can connect to. Each connection from another application
- * is represented by a DBusConnection.
+ * is represented by a #DBusConnection.
  *
- * @todo Thread safety hasn't been looked at for #DBusServer
+ * @todo Thread safety hasn't been tested much for #DBusServer
  * @todo Need notification to apps of disconnection, may matter for some transports
  */
 
  * @{
  */
 
-static void
-init_guid (DBusGUID *guid)
+#ifndef _dbus_server_trace_ref
+void
+_dbus_server_trace_ref (DBusServer *server,
+    int old_refcount,
+    int new_refcount,
+    const char *why)
 {
-  long now;
-  char *p;
-  int ts_size;
-
-  _dbus_get_current_time (&now, NULL);
+  static int enabled = -1;
 
-  guid->as_uint32s[0] = now;
-
-  ts_size = sizeof (guid->as_uint32s[0]);
-  p = ((char*)guid->as_bytes) + ts_size;
-  
-  _dbus_generate_random_bytes_buffer (p,
-                                      sizeof (guid->as_bytes) - ts_size);
+  _dbus_trace_ref ("DBusServer", server, old_refcount, new_refcount, why,
+      "DBUS_SERVER_TRACE", &enabled);
 }
+#endif
 
 /* this is a little fragile since it assumes the address doesn't
  * already have a guid, but it shouldn't
@@ -115,25 +112,29 @@ _dbus_server_init_base (DBusServer             *server,
                         const DBusServerVTable *vtable,
                         const DBusString       *address)
 {
-  DBusString guid_raw;
-  
   server->vtable = vtable;
-  server->refcount.value = 1;
+
+#ifdef DBUS_DISABLE_ASSERT
+  _dbus_atomic_inc (&server->refcount);
+#else
+    {
+      dbus_int32_t old_refcount = _dbus_atomic_inc (&server->refcount);
+
+      _dbus_assert (old_refcount == 0);
+    }
+#endif
 
   server->address = NULL;
   server->watches = NULL;
   server->timeouts = NULL;
+  server->published_address = FALSE;
 
   if (!_dbus_string_init (&server->guid_hex))
     return FALSE;
 
-  init_guid (&server->guid);
+  _dbus_generate_uuid (&server->guid);
 
-  _dbus_string_init_const_len (&guid_raw, (signed char*) server->guid.as_bytes,
-                               sizeof (server->guid.as_bytes));
-  if (!_dbus_string_hex_encode (&guid_raw, 0,
-                                &server->guid_hex,
-                                _dbus_string_get_length (&server->guid_hex)))
+  if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
     goto failed;
   
   server->address = copy_address_with_guid_appended (address,
@@ -141,7 +142,7 @@ _dbus_server_init_base (DBusServer             *server,
   if (server->address == NULL)
     goto failed;
   
-  _dbus_mutex_new_at_location (&server->mutex);
+  _dbus_rmutex_new_at_location (&server->mutex);
   if (server->mutex == NULL)
     goto failed;
   
@@ -160,7 +161,7 @@ _dbus_server_init_base (DBusServer             *server,
   return TRUE;
 
  failed:
-  _dbus_mutex_free_at_location (&server->mutex);
+  _dbus_rmutex_free_at_location (&server->mutex);
   server->mutex = NULL;
   if (server->watches)
     {
@@ -182,6 +183,55 @@ _dbus_server_init_base (DBusServer             *server,
   return FALSE;
 }
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+static void mini_vtable_dummy_func(DBusServer *server)
+{
+    // Used to prevent assert errors. Pointer to function is passed to
+    // DBusServerVTable which is passed to server->vtable in
+    // dbus_server_init_mini function.
+}
+
+DBusServer*
+dbus_server_init_mini (char* address)
+{
+       DBusServer *server;
+
+    static const DBusServerVTable dbus_server_init_mini_vtable = {
+        mini_vtable_dummy_func,
+        mini_vtable_dummy_func
+    };
+
+       server = dbus_new0(struct DBusServer, 1);
+       if(server == NULL)
+               return NULL;
+
+       memset(server, 0, sizeof(struct DBusServer));
+       _dbus_rmutex_new_at_location (&server->mutex);
+       if (server->mutex == NULL)
+           goto failed;
+       server->address = address;
+
+    server->vtable = &dbus_server_init_mini_vtable;
+
+    _dbus_atomic_inc (&server->refcount);
+
+    server->watches = _dbus_watch_list_new ();
+    if (server->watches == NULL)
+        goto failed;
+
+    server->timeouts = _dbus_timeout_list_new();
+    if (server->timeouts == NULL)
+        goto failed;
+
+       return server;
+
+failed:
+       dbus_free(server);
+       return NULL;
+}
+#endif
+
+
 /**
  * Finalizes the members of the DBusServer base class.
  * Chained up to by subclass finalizers.
@@ -207,7 +257,7 @@ _dbus_server_finalize_base (DBusServer *server)
   _dbus_watch_list_free (server->watches);
   _dbus_timeout_list_free (server->timeouts);
 
-  _dbus_mutex_free_at_location (&server->mutex);
+  _dbus_rmutex_free_at_location (&server->mutex);
   
   dbus_free (server->address);
 
@@ -217,10 +267,13 @@ _dbus_server_finalize_base (DBusServer *server)
 }
 
 
+/** Function to be called in protected_change_watch() with refcount held */
 typedef dbus_bool_t (* DBusWatchAddFunction)     (DBusWatchList *list,
                                                   DBusWatch     *watch);
+/** Function to be called in protected_change_watch() with refcount held */
 typedef void        (* DBusWatchRemoveFunction)  (DBusWatchList *list,
                                                   DBusWatch     *watch);
+/** Function to be called in protected_change_watch() with refcount held */
 typedef void        (* DBusWatchToggleFunction)  (DBusWatchList *list,
                                                   DBusWatch     *watch,
                                                   dbus_bool_t    enabled);
@@ -330,11 +383,13 @@ _dbus_server_toggle_watch (DBusServer  *server,
                           enabled);
 }
 
-
+/** Function to be called in protected_change_timeout() with refcount held */
 typedef dbus_bool_t (* DBusTimeoutAddFunction)    (DBusTimeoutList *list,
                                                    DBusTimeout     *timeout);
+/** Function to be called in protected_change_timeout() with refcount held */
 typedef void        (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
                                                    DBusTimeout     *timeout);
+/** Function to be called in protected_change_timeout() with refcount held */
 typedef void        (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
                                                    DBusTimeout     *timeout,
                                                    dbus_bool_t      enabled);
@@ -442,6 +497,59 @@ _dbus_server_toggle_timeout (DBusServer  *server,
 }
 
 
+/**
+ * Like dbus_server_ref() but does not acquire the lock (must already be held)
+ *
+ * @param server the server.
+ */
+void
+_dbus_server_ref_unlocked (DBusServer *server)
+{
+  dbus_int32_t old_refcount;
+
+  _dbus_assert (server != NULL);
+  HAVE_LOCK_CHECK (server);
+
+  old_refcount = _dbus_atomic_inc (&server->refcount);
+  _dbus_assert (old_refcount > 0);
+  _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
+      "ref_unlocked");
+}
+
+/**
+ * Like dbus_server_unref() but does not acquire the lock (must already be held)
+ *
+ * @param server the server.
+ */
+void
+_dbus_server_unref_unlocked (DBusServer *server)
+{
+  dbus_int32_t old_refcount;
+
+  /* Keep this in sync with dbus_server_unref */
+
+  _dbus_assert (server != NULL);
+
+  HAVE_LOCK_CHECK (server);
+
+  old_refcount = _dbus_atomic_dec (&server->refcount);
+  _dbus_assert (old_refcount > 0);
+
+  _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
+      "unref_unlocked");
+
+  if (old_refcount == 1)
+    {
+      _dbus_assert (server->disconnected);
+      
+      SERVER_UNLOCK (server);
+      
+      _dbus_assert (server->vtable->finalize != NULL);
+      
+      (* server->vtable->finalize) (server);
+    }
+}
+
 /** @} */
 
 /**
@@ -463,20 +571,36 @@ _dbus_server_toggle_timeout (DBusServer  *server,
  * 
  */
 
+static const struct {
+  DBusServerListenResult (* func) (DBusAddressEntry *entry,
+                                   DBusServer      **server_p,
+                                   DBusError        *error);
+} listen_funcs[] = {
+  { _dbus_server_listen_socket }
+  , { _dbus_server_listen_platform_specific }
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+  , { _dbus_server_listen_debug_pipe }
+#endif
+};
+
 /**
- * Listens for new connections on the given address.
- * Returns #NULL if listening fails for any reason.
+ * Listens for new connections on the given address.  If there are
+ * multiple semicolon-separated address entries in the address, tries
+ * each one and listens on the first one that works.
+ * 
+ * Returns #NULL and sets error if listening fails for any reason.
  * Otherwise returns a new #DBusServer.
- * dbus_server_set_new_connection_function() and
- * dbus_server_set_watch_functions() should be called
- * immediately to render the server fully functional.
- *
- * @todo 1.0? error messages on bad address could really be better.
- * DBusResultCode is a bit limiting here.
+ * dbus_server_set_new_connection_function(),
+ * dbus_server_set_watch_functions(), and
+ * dbus_server_set_timeout_functions() should be called immediately to
+ * render the server fully functional.
  *
+ * To free the server, applications must call first
+ * dbus_server_disconnect() and then dbus_server_unref().
+ * 
  * @param address the address of this server.
- * @param error location to store rationale for failure.
- * @returns a new DBusServer, or #NULL on failure.
+ * @param error location to store reason for failure.
+ * @returns a new #DBusServer, or #NULL on failure.
  * 
  */
 DBusServer*
@@ -486,10 +610,9 @@ dbus_server_listen (const char     *address,
   DBusServer *server;
   DBusAddressEntry **entries;
   int len, i;
-  const char *address_problem_type;
-  const char *address_problem_field;
-  const char *address_problem_other;
-
+  DBusError first_connect_error = DBUS_ERROR_INIT;
+  dbus_bool_t handled_once;
+  
   _dbus_return_val_if_fail (address != NULL, NULL);
   _dbus_return_val_if_error_is_set (error, NULL);
   
@@ -497,161 +620,119 @@ dbus_server_listen (const char     *address,
     return NULL;
 
   server = NULL;
-  address_problem_type = NULL;
-  address_problem_field = NULL;
-  address_problem_other = NULL;
-  
+  handled_once = FALSE;
+
   for (i = 0; i < len; i++)
     {
-      const char *method;
+      int j;
 
-      method = dbus_address_entry_get_method (entries[i]);
+      for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
+        {
+          DBusServerListenResult result;
+          DBusError tmp_error = DBUS_ERROR_INIT;
 
-      if (strcmp (method, "unix") == 0)
-       {
-         const char *path = dbus_address_entry_get_value (entries[i], "path");
-          const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
-          const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
-          
-         if (path == NULL && tmpdir == NULL && abstract == NULL)
-            {
-              address_problem_type = "unix";
-              address_problem_field = "path or tmpdir or abstract";
-              goto bad_address;
-            }
+          result = (* listen_funcs[j].func) (entries[i],
+                                             &server,
+                                             &tmp_error);
 
-          if ((path && tmpdir) ||
-              (path && abstract) ||
-              (tmpdir && abstract))
+          if (result == DBUS_SERVER_LISTEN_OK)
             {
-              address_problem_other = "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time";
-              goto bad_address;
+              _dbus_assert (server != NULL);
+              _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+              handled_once = TRUE;
+              goto out;
             }
-
-          if (tmpdir != NULL)
+          else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
             {
-              DBusString full_path;
-              DBusString filename;
-              
-              if (!_dbus_string_init (&full_path))
-                {
-                  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-                  goto out;
-                }
-                  
-              if (!_dbus_string_init (&filename))
-                {
-                  _dbus_string_free (&full_path);
-                  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-                  goto out;
-                }
-              
-              if (!_dbus_string_append (&filename,
-                                        "dbus-") ||
-                  !_dbus_generate_random_ascii (&filename, 10) ||
-                  !_dbus_string_append (&full_path, tmpdir) ||
-                  !_dbus_concat_dir_and_file (&full_path, &filename))
-                {
-                  _dbus_string_free (&full_path);
-                  _dbus_string_free (&filename);
-                  dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-                  goto out;
-                }
-              
-              /* Always use abstract namespace if possible with tmpdir */
-              
-              server =
-                _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
-#ifdef HAVE_ABSTRACT_SOCKETS
-                                                    TRUE,
-#else
-                                                    FALSE,
-#endif
-                                                    error);
-
-              _dbus_string_free (&full_path);
-              _dbus_string_free (&filename);
+              _dbus_assert (server == NULL);
+              dbus_set_error (error,
+                       DBUS_ERROR_ADDRESS_IN_USE,
+                       "Address '%s' already used",
+                       dbus_address_entry_get_method (entries[0]));
+              handled_once = TRUE;
+              goto out;
             }
-          else
+          else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
             {
-              if (path)
-                server = _dbus_server_new_for_domain_socket (path, FALSE, error);
-              else
-                server = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
+              _dbus_assert (server == NULL);
+              _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+              dbus_move_error (&tmp_error, error);
+              handled_once = TRUE;
+              goto out;
             }
-       }
-      else if (strcmp (method, "tcp") == 0)
-       {
-         const char *host = dbus_address_entry_get_value (entries[i], "host");
-          const char *port = dbus_address_entry_get_value (entries[i], "port");
-          DBusString  str;
-          long lport;
-          dbus_bool_t sresult;
-          
-         if (port == NULL)
+          else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
             {
-              address_problem_type = "tcp";
-              address_problem_field = "port";
-              goto bad_address;
-            }
+              _dbus_assert (server == NULL);
+              _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
 
-          _dbus_string_init_const (&str, port);
-          sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
-          _dbus_string_free (&str);
-          
-          if (sresult == FALSE || lport <= 0 || lport > 65535)
-            {
-              address_problem_other = "Port is not an integer between 0 and 65535";
-              goto bad_address;
+              /* keep trying addresses */
             }
-          
-         server = _dbus_server_new_for_tcp_socket (host, lport, error);
-
-         if (server)
-           break;
-       }
-#ifdef DBUS_BUILD_TESTS
-      else if (strcmp (method, "debug-pipe") == 0)
-       {
-         const char *name = dbus_address_entry_get_value (entries[i], "name");
-
-         if (name == NULL)
+          else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
             {
-              address_problem_type = "debug-pipe";
-              address_problem_field = "name";
-              goto bad_address;
-            }
+              _dbus_assert (server == NULL);
+              _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+              if (!dbus_error_is_set (&first_connect_error))
+                dbus_move_error (&tmp_error, &first_connect_error);
+              else
+                dbus_error_free (&tmp_error);
 
-         server = _dbus_server_debug_pipe_new (name, error);
-       }
-#endif
-      else
-        {
-          address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
-          goto bad_address;
+              handled_once = TRUE;
+              
+              /* keep trying addresses */
+            }
         }
-      
-      if (server)
-        break;
+
+      _dbus_assert (server == NULL);
+      _DBUS_ASSERT_ERROR_IS_CLEAR (error);
     }
 
  out:
+
+  if (!handled_once)
+    {
+      _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+      if (len > 0)
+        dbus_set_error (error,
+                       DBUS_ERROR_BAD_ADDRESS,
+                       "Unknown address type '%s'",
+                       dbus_address_entry_get_method (entries[0]));
+      else
+        dbus_set_error (error,
+                        DBUS_ERROR_BAD_ADDRESS,
+                        "Empty address '%s'",
+                        address);
+    }
   
   dbus_address_entries_free (entries);
-  return server;
 
- bad_address:
-  dbus_address_entries_free (entries);
-  if (address_problem_type != NULL)
-    dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
-                    "Server address of type %s was missing argument %s",
-                    address_problem_type, address_problem_field);
-  else
-    dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
-                    "Could not parse server address: %s",
-                    address_problem_other);
+  if (server == NULL)
+    {
+      _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
+                   dbus_error_is_set (error));
+      
+      if (error && dbus_error_is_set (error))
+        {
+          /* already set the error */
+        }
+      else
+        {
+          /* didn't set the error but either error should be
+           * NULL or first_connect_error should be set.
+           */
+          _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
+          dbus_move_error (&first_connect_error, error);
+        }
+
+      _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
+      _DBUS_ASSERT_ERROR_IS_SET (error);
 
-  return NULL;
+      return NULL;
+    }
+  else
+    {
+      _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+      return server;
+    }
 }
 
 /**
@@ -663,19 +744,25 @@ dbus_server_listen (const char     *address,
 DBusServer *
 dbus_server_ref (DBusServer *server)
 {
+  dbus_int32_t old_refcount;
+
   _dbus_return_val_if_fail (server != NULL, NULL);
-  _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
 
-#ifdef DBUS_HAVE_ATOMIC_INT
-  _dbus_atomic_inc (&server->refcount);
-#else
-  SERVER_LOCK (server);
-  _dbus_assert (server->refcount.value > 0);
+  old_refcount = _dbus_atomic_inc (&server->refcount);
 
-  server->refcount.value += 1;
-  SERVER_UNLOCK (server);
+#ifndef DBUS_DISABLE_CHECKS
+  if (_DBUS_UNLIKELY (old_refcount <= 0))
+    {
+      _dbus_atomic_dec (&server->refcount);
+      _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,
+                               _DBUS_FUNCTION_NAME, "old_refcount > 0",
+                               __FILE__, __LINE__);
+      return NULL;
+    }
 #endif
 
+  _dbus_server_trace_ref (server, old_refcount, old_refcount + 1, "ref");
+
   return server;
 }
 
@@ -690,87 +777,39 @@ dbus_server_ref (DBusServer *server)
 void
 dbus_server_unref (DBusServer *server)
 {
-  dbus_bool_t last_unref;
-  
+  dbus_int32_t old_refcount;
+
+  /* keep this in sync with unref_unlocked */
+
   _dbus_return_if_fail (server != NULL);
-  _dbus_return_if_fail (server->refcount.value > 0);
 
-#ifdef DBUS_HAVE_ATOMIC_INT
-  last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
-#else
-  SERVER_LOCK (server);
-  
-  _dbus_assert (server->refcount.value > 0);
+  old_refcount = _dbus_atomic_dec (&server->refcount);
 
-  server->refcount.value -= 1;
-  last_unref = (server->refcount.value == 0);
-  
-  SERVER_UNLOCK (server);
-#endif
-  
-  if (last_unref)
+#ifndef DBUS_DISABLE_CHECKS
+  if (_DBUS_UNLIKELY (old_refcount <= 0))
     {
-      /* lock not held! */
-      _dbus_assert (server->disconnected);
-      
-      _dbus_assert (server->vtable->finalize != NULL);
-      
-      (* server->vtable->finalize) (server);
+      /* undo side-effect first
+       * please do not try to simplify the code here by using
+       * _dbus_atomic_get(), why we don't use it is
+       * because it issues another atomic operation even though
+       * DBUS_DISABLE_CHECKS defined.
+       * Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68303
+       */
+      _dbus_atomic_inc (&server->refcount);
+      _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,
+                               _DBUS_FUNCTION_NAME, "old_refcount > 0",
+                               __FILE__, __LINE__);
+      return;
     }
-}
-
-/**
- * Like dbus_server_ref() but does not acquire the lock (must already be held)
- *
- * @param server the server.
- */
-void
-_dbus_server_ref_unlocked (DBusServer *server)
-{
-  _dbus_assert (server != NULL);
-  _dbus_assert (server->refcount.value > 0);
-  
-  HAVE_LOCK_CHECK (server);
-
-#ifdef DBUS_HAVE_ATOMIC_INT
-  _dbus_atomic_inc (&server->refcount);
-#else
-  _dbus_assert (server->refcount.value > 0);
-
-  server->refcount.value += 1;
 #endif
-}
-
-/**
- * Like dbus_server_unref() but does not acquire the lock (must already be held)
- *
- * @param server the server.
- */
-void
-_dbus_server_unref_unlocked (DBusServer *server)
-{
-  dbus_bool_t last_unref;
-  
-  _dbus_assert (server != NULL);
-  _dbus_assert (server->refcount.value > 0);
 
-  HAVE_LOCK_CHECK (server);
-  
-#ifdef DBUS_HAVE_ATOMIC_INT
-  last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
-#else
-  _dbus_assert (server->refcount.value > 0);
+  _dbus_server_trace_ref (server, old_refcount, old_refcount - 1, "unref");
 
-  server->refcount.value -= 1;
-  last_unref = (server->refcount.value == 0);
-#endif
-  
-  if (last_unref)
+  if (old_refcount == 1)
     {
+      /* lock not held! */
       _dbus_assert (server->disconnected);
       
-      SERVER_UNLOCK (server);
-      
       _dbus_assert (server->vtable->finalize != NULL);
       
       (* server->vtable->finalize) (server);
@@ -789,11 +828,10 @@ void
 dbus_server_disconnect (DBusServer *server)
 {
   _dbus_return_if_fail (server != NULL);
-  _dbus_return_if_fail (server->refcount.value > 0);
 
+  dbus_server_ref (server);
   SERVER_LOCK (server);
-  _dbus_server_ref_unlocked (server);
-  
+
   _dbus_assert (server->vtable->disconnect != NULL);
 
   if (!server->disconnected)
@@ -849,11 +887,56 @@ dbus_server_get_address (DBusServer *server)
 }
 
 /**
+ * Returns the unique ID of the server, as a newly-allocated
+ * string which must be freed by the caller. This ID is
+ * normally used by clients to tell when two #DBusConnection
+ * would be equivalent (because the server address passed
+ * to dbus_connection_open() will have the same guid in the
+ * two cases). dbus_connection_open() can re-use an existing
+ * connection with the same ID instead of opening a new
+ * connection.
+ *
+ * This is an ID unique to each #DBusServer. Remember that
+ * a #DBusServer represents only one mode of connecting,
+ * so e.g. a bus daemon can listen on multiple addresses
+ * which will mean it has multiple #DBusServer each with
+ * their own ID.
+ *
+ * The ID is not a UUID in the sense of RFC4122; the details
+ * are explained in the D-Bus specification.
+ *
+ * @param server the server
+ * @returns the id of the server or #NULL if no memory
+ */
+char*
+dbus_server_get_id (DBusServer *server)
+{
+  char *retval;
+  
+  _dbus_return_val_if_fail (server != NULL, NULL);
+
+  SERVER_LOCK (server);
+  retval = NULL;
+  _dbus_string_copy_data (&server->guid_hex, &retval);
+  SERVER_UNLOCK (server);
+
+  return retval;
+}
+
+/**
  * Sets a function to be used for handling new connections.  The given
  * function is passed each new connection as the connection is
  * created. If the new connection function increments the connection's
  * reference count, the connection will stay alive. Otherwise, the
- * connection will be unreferenced and closed.
+ * connection will be unreferenced and closed. The new connection
+ * function may also close the connection itself, which is considered
+ * good form if the connection is not wanted.
+ *
+ * The connection here is private in the sense of
+ * dbus_connection_open_private(), so if the new connection function
+ * keeps a reference it must arrange for the connection to be closed.
+ * i.e. libdbus does not own this connection once the new connection
+ * function takes a reference.
  *
  * @param server the server.
  * @param function a function to handle new connections.
@@ -885,7 +968,7 @@ dbus_server_set_new_connection_function (DBusServer                *server,
 }
 
 /**
- * Sets the watch functions for the connection. These functions are
+ * Sets the watch functions for the server. These functions are
  * responsible for making the application's main loop aware of file
  * descriptors that need to be monitored for events.
  *
@@ -929,7 +1012,7 @@ dbus_server_set_watch_functions (DBusServer              *server,
     }
   else
     {
-      _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
+      _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
       result = FALSE;
     }
   server->watches = watches;
@@ -939,7 +1022,7 @@ dbus_server_set_watch_functions (DBusServer              *server,
 }
 
 /**
- * Sets the timeout functions for the connection. These functions are
+ * Sets the timeout functions for the server. These functions are
  * responsible for making the application's main loop aware of timeouts.
  *
  * This function behaves exactly like dbus_connection_set_timeout_functions();
@@ -982,7 +1065,7 @@ dbus_server_set_timeout_functions (DBusServer                *server,
     }
   else
     {
-      _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
+      _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
       result = FALSE;
     }
   server->timeouts = timeouts;
@@ -992,10 +1075,13 @@ dbus_server_set_timeout_functions (DBusServer                *server,
 }
 
 /**
- * Sets the authentication mechanisms that this server offers
- * to clients, as a list of SASL mechanisms. This function
- * only affects connections created *after* it is called.
- * Pass #NULL instead of an array to use all available mechanisms.
+ * Sets the authentication mechanisms that this server offers to
+ * clients, as a #NULL-terminated array of mechanism names. This
+ * function only affects connections created <em>after</em> it is
+ * called.  Pass #NULL instead of an array to use all available
+ * mechanisms (this is the default behavior).
+ *
+ * The D-Bus specification describes some of the supported mechanisms.
  *
  * @param server the server
  * @param mechanisms #NULL-terminated array of mechanisms
@@ -1028,9 +1114,8 @@ dbus_server_set_auth_mechanisms (DBusServer  *server,
   return TRUE;
 }
 
-
-static DBusDataSlotAllocator slot_allocator;
-_DBUS_DEFINE_GLOBAL_LOCK (server_slots);
+static DBusDataSlotAllocator slot_allocator =
+  _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (server_slots));
 
 /**
  * Allocates an integer ID to be used for storing application-specific
@@ -1050,7 +1135,6 @@ dbus_bool_t
 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
 {
   return _dbus_data_slot_allocator_alloc (&slot_allocator,
-                                          (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
                                           slot_p);
 }
 
@@ -1147,18 +1231,21 @@ dbus_server_get_data (DBusServer   *server,
 
 /** @} */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
+#include <string.h>
 
 dbus_bool_t
 _dbus_server_test (void)
 {
   const char *valid_addresses[] = {
     "tcp:port=1234",
-    "unix:path=./boogie",
     "tcp:host=localhost,port=1234",
     "tcp:host=localhost,port=1234;tcp:port=5678",
+#ifdef DBUS_UNIX
+    "unix:path=./boogie",
     "tcp:port=1234;unix:path=./boogie",
+#endif
   };
 
   DBusServer *server;
@@ -1166,18 +1253,33 @@ _dbus_server_test (void)
   
   for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
     {
-      server = dbus_server_listen (valid_addresses[i], NULL);
+      DBusError error = DBUS_ERROR_INIT;
+      char *address;
+      char *id;
+
+      server = dbus_server_listen (valid_addresses[i], &error);
       if (server == NULL)
-       _dbus_assert_not_reached ("Failed to listen for valid address.");
+        {
+          _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
+          dbus_error_free (&error);
+          _dbus_assert_not_reached ("Failed to listen for valid address.");
+        }
 
-      dbus_server_disconnect (server);
-      dbus_server_unref (server);
+      id = dbus_server_get_id (server);
+      _dbus_assert (id != NULL);
+      address = dbus_server_get_address (server);
+      _dbus_assert (address != NULL);
 
-      /* Try disconnecting before unreffing */
-      server = dbus_server_listen (valid_addresses[i], NULL);
-      if (server == NULL)
-       _dbus_assert_not_reached ("Failed to listen for valid address.");
+      if (strstr (address, id) == NULL)
+        {
+          _dbus_warn ("server id '%s' is not in the server address '%s'\n",
+                      id, address);
+          _dbus_assert_not_reached ("bad server id or address");
+        }
 
+      dbus_free (id);
+      dbus_free (address);
+      
       dbus_server_disconnect (server);
       dbus_server_unref (server);
     }
@@ -1185,4 +1287,4 @@ _dbus_server_test (void)
   return TRUE;
 }
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */