[Foxp][Test] simple echo test with custom payload size
[platform/upstream/dbus.git] / dbus / dbus-object-tree.c
index e5ff773..f6ae19d 100644 (file)
@@ -1,4 +1,4 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
  *
  * Copyright (C) 2003, 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-object-tree.h"
 #include "dbus-connection-internal.h"
 #include "dbus-internals.h"
@@ -371,6 +373,8 @@ ensure_subtree (DBusObjectTree *tree,
   return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
 }
 
+static char *flatten_path (const char **path);
+
 /**
  * Registers a new subtree in the global object tree.
  *
@@ -379,14 +383,17 @@ ensure_subtree (DBusObjectTree *tree,
  * @param path NULL-terminated array of path elements giving path to subtree
  * @param vtable the vtable used to traverse this subtree
  * @param user_data user data to pass to methods in the vtable
- * @returns #FALSE if not enough memory
+ * @param error address where an error can be returned
+ * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
+ *    #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
  */
 dbus_bool_t
 _dbus_object_tree_register (DBusObjectTree              *tree,
                             dbus_bool_t                  fallback,
                             const char                 **path,
                             const DBusObjectPathVTable  *vtable,
-                            void                        *user_data)
+                            void                        *user_data,
+                            DBusError                   *error)
 {
   DBusObjectSubtree  *subtree;
 
@@ -396,24 +403,33 @@ _dbus_object_tree_register (DBusObjectTree              *tree,
 
   subtree = ensure_subtree (tree, path);
   if (subtree == NULL)
-    return FALSE;
+    {
+      _DBUS_SET_OOM (error);
+      return FALSE;
+    }
 
-#ifndef DBUS_DISABLE_CHECKS
   if (subtree->message_function != NULL)
     {
-      _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
-                  path[0] ? path[0] : "null");
+      if (error != NULL)
+        {
+          char *complete_path = flatten_path (path);
+
+          dbus_set_error (error, DBUS_ERROR_OBJECT_PATH_IN_USE,
+                          "A handler is already registered for %s",
+                          complete_path ? complete_path
+                                        : "(cannot represent path: out of memory!)");
+
+          dbus_free (complete_path);
+        }
+
       return FALSE;
     }
-#else
-  _dbus_assert (subtree->message_function == NULL);
-#endif
 
   subtree->message_function = vtable->message_function;
   subtree->unregister_function = vtable->unregister_function;
   subtree->user_data = user_data;
   subtree->invoke_as_fallback = fallback != FALSE;
-  
+
   return TRUE;
 }
 
@@ -487,19 +503,19 @@ unlock:
   connection = tree->connection;
 
   /* Unlock and call application code */
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (connection)
 #endif
     {
       _dbus_connection_ref_unlocked (connection);
-      _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+      _dbus_verbose ("unlock\n");
       _dbus_connection_unlock (connection);
     }
 
   if (unregister_function)
     (* unregister_function) (connection, user_data);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (connection)
 #endif
     dbus_connection_unref (connection);
@@ -622,11 +638,11 @@ handle_default_introspect_and_unlock (DBusObjectTree          *tree,
                                     DBUS_INTERFACE_INTROSPECTABLE,
                                     "Introspect"))
     {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (tree->connection)
 #endif
         {
-          _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
+          _dbus_verbose ("unlock\n");
           _dbus_connection_unlock (tree->connection);
         }
       
@@ -637,11 +653,11 @@ handle_default_introspect_and_unlock (DBusObjectTree          *tree,
   
   if (!_dbus_string_init (&xml))
     {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (tree->connection)
 #endif
         {
-          _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
+          _dbus_verbose ("unlock\n");
           _dbus_connection_unlock (tree->connection);
         }
 
@@ -682,7 +698,7 @@ handle_default_introspect_and_unlock (DBusObjectTree          *tree,
   if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
     goto out;
   
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (tree->connection)
 #endif
     {
@@ -695,13 +711,13 @@ handle_default_introspect_and_unlock (DBusObjectTree          *tree,
   result = DBUS_HANDLER_RESULT_HANDLED;
   
  out:
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (tree->connection)
 #endif
     {
       if (!already_unlocked)
         {
-          _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
+          _dbus_verbose ("unlock\n");
           _dbus_connection_unlock (tree->connection);
         }
     }
@@ -725,11 +741,13 @@ handle_default_introspect_and_unlock (DBusObjectTree          *tree,
  *
  * @param tree the global object tree
  * @param message the message to dispatch
+ * @param found_object return location for the object
  * @returns whether message was handled successfully
  */
 DBusHandlerResult
 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
-                                       DBusMessage             *message)
+                                       DBusMessage             *message,
+                                       dbus_bool_t             *found_object)
 {
   char **path;
   dbus_bool_t exact_match;
@@ -745,11 +763,11 @@ _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
   path = NULL;
   if (!dbus_message_get_path_decomposed (message, &path))
     {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (tree->connection)
 #endif
         {
-          _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+          _dbus_verbose ("unlock\n");
           _dbus_connection_unlock (tree->connection);
         }
       
@@ -760,11 +778,11 @@ _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
 
   if (path == NULL)
     {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (tree->connection)
 #endif
         {
-          _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+          _dbus_verbose ("unlock\n");
           _dbus_connection_unlock (tree->connection);
         }
       
@@ -775,6 +793,9 @@ _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
   /* Find the deepest path that covers the path in the message */
   subtree = find_handler (tree, (const char**) path, &exact_match);
   
+  if (found_object)
+    *found_object = !!subtree;
+
   /* Build a list of all paths that cover the path in the message */
 
   list = NULL;
@@ -826,11 +847,11 @@ _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
           _dbus_verbose ("  (invoking a handler)\n");
 #endif
           
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
           if (tree->connection)
 #endif
             {
-              _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+              _dbus_verbose ("unlock\n");
               _dbus_connection_unlock (tree->connection);
             }
 
@@ -843,7 +864,7 @@ _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
                                          message,
                                          user_data);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
           if (tree->connection)
 #endif
             _dbus_connection_lock (tree->connection);
@@ -866,11 +887,11 @@ _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
     }
   else
     {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (tree->connection)
 #endif
         {
-          _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+          _dbus_verbose ("unlock\n");
           _dbus_connection_unlock (tree->connection);
         }
     }
@@ -910,8 +931,7 @@ _dbus_object_tree_get_user_data_unlocked (DBusObjectTree *tree,
 
   if ((subtree == NULL) || !exact_match)
     {
-      _dbus_verbose ("%s: No object at specified path found\n",
-                     _DBUS_FUNCTION_NAME);
+      _dbus_verbose ("No object at specified path found\n");
       return NULL;
     }
 
@@ -935,7 +955,7 @@ allocate_subtree_object (const char *name)
 
   len = strlen (name);
 
-  subtree = dbus_malloc (MAX (front_padding + (len + 1), sizeof (DBusObjectSubtree)));
+  subtree = dbus_malloc0 (MAX (front_padding + (len + 1), sizeof (DBusObjectSubtree)));
 
   if (subtree == NULL)
     return NULL;
@@ -972,7 +992,7 @@ _dbus_object_subtree_new (const char                  *name,
     }
 
   subtree->user_data = user_data;
-  subtree->refcount.value = 1;
+  _dbus_atomic_inc (&subtree->refcount);
   subtree->subtrees = NULL;
   subtree->n_subtrees = 0;
   subtree->max_subtrees = 0;
@@ -987,8 +1007,14 @@ _dbus_object_subtree_new (const char                  *name,
 static DBusObjectSubtree *
 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
 {
-  _dbus_assert (subtree->refcount.value > 0);
+#ifdef DBUS_DISABLE_ASSERT
   _dbus_atomic_inc (&subtree->refcount);
+#else
+  dbus_int32_t old_value;
+
+  old_value = _dbus_atomic_inc (&subtree->refcount);
+  _dbus_assert (old_value > 0);
+#endif
 
   return subtree;
 }
@@ -996,9 +1022,12 @@ _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
 static void
 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
 {
-  _dbus_assert (subtree->refcount.value > 0);
+  dbus_int32_t old_value;
+
+  old_value = _dbus_atomic_dec (&subtree->refcount);
+  _dbus_assert (old_value > 0);
 
-  if (_dbus_atomic_dec (&subtree->refcount) == 1)
+  if (old_value == 1)
     {
       _dbus_assert (subtree->unregister_function == NULL);
       _dbus_assert (subtree->message_function == NULL);
@@ -1029,11 +1058,11 @@ _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
                                                        parent_path,
                                                        child_entries);
   
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (tree->connection)
 #endif
     {
-      _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
+      _dbus_verbose ("unlock\n");
       _dbus_connection_unlock (tree->connection);
     }
 
@@ -1065,6 +1094,7 @@ _dbus_decompose_path (const char*     data,
   int i, j, comp;
 
   _dbus_assert (data != NULL);
+  _dbus_assert (path != NULL);
   
 #if VERBOSE_DECOMPOSE
   _dbus_verbose ("Decomposing path \"%s\"\n",
@@ -1077,6 +1107,7 @@ _dbus_decompose_path (const char*     data,
       i = 0;
       while (i < len)
         {
+          _dbus_assert (data[i] != '\0');
           if (data[i] == '/')
             n_components += 1;
           ++i;
@@ -1140,13 +1171,6 @@ _dbus_decompose_path (const char*     data,
 
 /** @} */
 
-#ifdef DBUS_BUILD_TESTS
-
-#ifndef DOXYGEN_SHOULD_SKIP_THIS
-
-#include "dbus-test.h"
-#include <stdio.h>
-
 static char*
 flatten_path (const char **path)
 {
@@ -1191,6 +1215,13 @@ flatten_path (const char **path)
 }
 
 
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+
+#include "dbus-test.h"
+#include <stdio.h>
+
 typedef enum 
 {
   STR_EQUAL,
@@ -1314,7 +1345,7 @@ do_register (DBusObjectTree *tree,
 {
   DBusObjectPathVTable vtable = { test_unregister_function,
                                   test_message_function, NULL };
-  
+
   tree_test_data[i].message_handled = FALSE;
   tree_test_data[i].handler_unregistered = FALSE;
   tree_test_data[i].handler_fallback = fallback;
@@ -1322,7 +1353,8 @@ do_register (DBusObjectTree *tree,
 
   if (!_dbus_object_tree_register (tree, fallback, path,
                                    &vtable,
-                                   &tree_test_data[i]))
+                                   &tree_test_data[i],
+                                   NULL))
     return FALSE;
 
   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path) ==
@@ -1364,7 +1396,7 @@ do_test_dispatch (DBusObjectTree *tree,
       ++j;
     }
 
-  result = _dbus_object_tree_dispatch_and_unlock (tree, message);
+  result = _dbus_object_tree_dispatch_and_unlock (tree, message, NULL);
   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
     goto oom;
 
@@ -1931,4 +1963,4 @@ _dbus_object_tree_test (void)
 
 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */