[daemon-fix] fixed querying about name information
[platform/upstream/dbus.git] / dbus / dbus-server.c
index 5383a66..ba993d1 100644 (file)
@@ -26,7 +26,7 @@
 #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"
  * @{
  */
 
+#ifndef _dbus_server_trace_ref
+void
+_dbus_server_trace_ref (DBusServer *server,
+    int old_refcount,
+    int new_refcount,
+    const char *why)
+{
+  static int enabled = -1;
+
+  _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
  */
@@ -128,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;
   
@@ -147,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)
     {
@@ -169,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.
@@ -194,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);
 
@@ -442,18 +505,15 @@ _dbus_server_toggle_timeout (DBusServer  *server,
 void
 _dbus_server_ref_unlocked (DBusServer *server)
 {
+  dbus_int32_t old_refcount;
+
   _dbus_assert (server != NULL);
   HAVE_LOCK_CHECK (server);
 
-#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
+  old_refcount = _dbus_atomic_inc (&server->refcount);
+  _dbus_assert (old_refcount > 0);
+  _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
+      "ref_unlocked");
 }
 
 /**
@@ -475,6 +535,9 @@ _dbus_server_unref_unlocked (DBusServer *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);
@@ -515,7 +578,7 @@ static const struct {
 } listen_funcs[] = {
   { _dbus_server_listen_socket }
   , { _dbus_server_listen_platform_specific }
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   , { _dbus_server_listen_debug_pipe }
 #endif
 };
@@ -681,29 +744,25 @@ dbus_server_listen (const char     *address,
 DBusServer *
 dbus_server_ref (DBusServer *server)
 {
-  _dbus_return_val_if_fail (server != NULL, NULL);
+  dbus_int32_t old_refcount;
 
-#ifdef DBUS_DISABLE_CHECKS
-  _dbus_atomic_inc (&server->refcount);
-#else
-    {
-      dbus_int32_t old_refcount;
+  _dbus_return_val_if_fail (server != NULL, NULL);
 
-      /* can't get the refcount without a side-effect */
-      old_refcount = _dbus_atomic_inc (&server->refcount);
+  old_refcount = _dbus_atomic_inc (&server->refcount);
 
-      if (_DBUS_UNLIKELY (old_refcount <= 0))
-        {
-          /* undo side-effect first */
-          _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;
-        }
+#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;
 }
 
@@ -724,13 +783,18 @@ dbus_server_unref (DBusServer *server)
 
   _dbus_return_if_fail (server != NULL);
 
-  /* can't get the refcount without a side-effect */
   old_refcount = _dbus_atomic_dec (&server->refcount);
 
 #ifndef DBUS_DISABLE_CHECKS
   if (_DBUS_UNLIKELY (old_refcount <= 0))
     {
-      /* undo side-effect first */
+      /* 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",
@@ -739,6 +803,8 @@ dbus_server_unref (DBusServer *server)
     }
 #endif
 
+  _dbus_server_trace_ref (server, old_refcount, old_refcount - 1, "unref");
+
   if (old_refcount == 1)
     {
       /* lock not held! */
@@ -763,16 +829,7 @@ dbus_server_disconnect (DBusServer *server)
 {
   _dbus_return_if_fail (server != NULL);
 
-#ifdef DBUS_DISABLE_CHECKS
-  _dbus_atomic_inc (&server->refcount);
-#else
-    {
-      dbus_int32_t old_refcount = _dbus_atomic_inc (&server->refcount);
-
-      _dbus_return_if_fail (old_refcount > 0);
-    }
-#endif
-
+  dbus_server_ref (server);
   SERVER_LOCK (server);
 
   _dbus_assert (server->vtable->disconnect != NULL);
@@ -1057,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
@@ -1079,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);
 }
 
@@ -1176,7 +1231,7 @@ dbus_server_get_data (DBusServer   *server,
 
 /** @} */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
 #include <string.h>
 
@@ -1232,4 +1287,4 @@ _dbus_server_test (void)
   return TRUE;
 }
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */