2005-07-29 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-object-tree.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
3  *
4  * Copyright (C) 2003, 2005  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-object-tree.h"
24 #include "dbus-connection-internal.h"
25 #include "dbus-internals.h"
26 #include "dbus-hash.h"
27 #include "dbus-protocol.h"
28 #include "dbus-string.h"
29 #include <string.h>
30 #include <stdlib.h>
31
32 /**
33  * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
34  * @ingroup  DBusInternals
35  * @brief DBusObjectTree is used by DBusConnection to track the object tree
36  *
37  * Types and functions related to DBusObjectTree. These
38  * are all library-internal.
39  *
40  * @{
41  */
42
43 /** Subnode of the object hierarchy */
44 typedef struct DBusObjectSubtree DBusObjectSubtree;
45
46 static DBusObjectSubtree* _dbus_object_subtree_new   (const char                  *name,
47                                                       const DBusObjectPathVTable  *vtable,
48                                                       void                        *user_data);
49 static DBusObjectSubtree* _dbus_object_subtree_ref   (DBusObjectSubtree           *subtree);
50 static void               _dbus_object_subtree_unref (DBusObjectSubtree           *subtree);
51
52 /**
53  * Internals of DBusObjectTree
54  */
55 struct DBusObjectTree
56 {
57   int                 refcount;   /**< Reference count */
58   DBusConnection     *connection; /**< Connection this tree belongs to */
59
60   DBusObjectSubtree  *root;       /**< Root of the tree ("/" node) */
61 };
62
63 /**
64  * Struct representing a single registered subtree handler, or node
65  * that's a parent of a registered subtree handler. If
66  * message_function != NULL there's actually a handler at this node.
67  */
68 struct DBusObjectSubtree
69 {
70   DBusAtomic                         refcount;            /**< Reference count */
71   DBusObjectSubtree                 *parent;              /**< Parent node */
72   DBusObjectPathUnregisterFunction   unregister_function; /**< Function to call on unregister */
73   DBusObjectPathMessageFunction      message_function;    /**< Function to handle messages */
74   void                              *user_data;           /**< Data for functions */
75   DBusObjectSubtree                **subtrees;            /**< Child nodes */
76   int                                n_subtrees;          /**< Number of child nodes */
77   unsigned int                       subtrees_sorted : 1; /**< Whether children are sorted */
78   unsigned int                       invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
79   char                               name[1]; /**< Allocated as large as necessary */
80 };
81
82 /**
83  * Creates a new object tree, representing a mapping from paths
84  * to handler vtables.
85  *
86  * @param connection the connection this tree belongs to
87  * @returns the new tree or #NULL if no memory
88  */
89 DBusObjectTree*
90 _dbus_object_tree_new (DBusConnection *connection)
91 {
92   DBusObjectTree *tree;
93
94   /* the connection passed in here isn't fully constructed,
95    * so don't do anything more than store a pointer to
96    * it
97    */
98
99   tree = dbus_new0 (DBusObjectTree, 1);
100   if (tree == NULL)
101     goto oom;
102
103   tree->refcount = 1;
104   tree->connection = connection;
105   tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
106   if (tree->root == NULL)
107     goto oom;
108   tree->root->invoke_as_fallback = TRUE;
109   
110   return tree;
111
112  oom:
113   if (tree)
114     {
115       dbus_free (tree);
116     }
117
118   return NULL;
119 }
120
121 /**
122  * Increment the reference count
123  * @param tree the object tree
124  * @returns the object tree
125  */
126 DBusObjectTree *
127 _dbus_object_tree_ref (DBusObjectTree *tree)
128 {
129   _dbus_assert (tree->refcount > 0);
130
131   tree->refcount += 1;
132
133   return tree;
134 }
135
136 /**
137  * Decrement the reference count
138  * @param tree the object tree
139  */
140 void
141 _dbus_object_tree_unref (DBusObjectTree *tree)
142 {
143   _dbus_assert (tree->refcount > 0);
144
145   tree->refcount -= 1;
146
147   if (tree->refcount == 0)
148     {
149       _dbus_object_tree_free_all_unlocked (tree);
150
151       dbus_free (tree);
152     }
153 }
154
155 static int
156 subtree_cmp (DBusObjectSubtree *subtree_a,
157              DBusObjectSubtree *subtree_b)
158 {
159   return strcmp (subtree_a->name, subtree_b->name);
160 }
161
162 static int
163 subtree_qsort_cmp (const void *a,
164                    const void *b)
165 {
166   DBusObjectSubtree **subtree_a_p = (void*) a;
167   DBusObjectSubtree **subtree_b_p = (void*) b;
168
169   return subtree_cmp (*subtree_a_p, *subtree_b_p);
170 }
171
172 static void
173 ensure_sorted (DBusObjectSubtree *subtree)
174 {
175   if (subtree->subtrees && !subtree->subtrees_sorted)
176     {
177       qsort (subtree->subtrees,
178              subtree->n_subtrees,
179              sizeof (DBusObjectSubtree*),
180              subtree_qsort_cmp);
181       subtree->subtrees_sorted = TRUE;
182     }
183 }
184
185 /** Set to 1 to get a bunch of debug spew about finding the
186  * subtree nodes
187  */
188 #define VERBOSE_FIND 0
189
190 static DBusObjectSubtree*
191 find_subtree_recurse (DBusObjectSubtree  *subtree,
192                       const char        **path,
193                       dbus_bool_t         create_if_not_found,
194                       int                *index_in_parent,
195                       dbus_bool_t        *exact_match)
196 {
197   int i;
198   dbus_bool_t return_deepest_match;
199
200   return_deepest_match = exact_match != NULL;
201
202   _dbus_assert (!(return_deepest_match && create_if_not_found));
203
204   if (path[0] == NULL)
205     {
206 #if VERBOSE_FIND
207       _dbus_verbose ("  path exhausted, returning %s\n",
208                      subtree->name);
209 #endif
210       if (exact_match != NULL)
211         *exact_match = TRUE;
212       return subtree;
213     }
214
215 #if VERBOSE_FIND
216   _dbus_verbose ("  searching children of %s for %s\n",
217                  subtree->name, path[0]);
218 #endif
219   
220   ensure_sorted (subtree);
221
222   /* FIXME we should do a binary search here instead
223    * of O(n)
224    */
225
226   i = 0;
227   while (i < subtree->n_subtrees)
228     {
229       int v;
230
231       v = strcmp (path[0], subtree->subtrees[i]->name);
232
233 #if VERBOSE_FIND
234       _dbus_verbose ("  %s cmp %s = %d\n",
235                      path[0], subtree->subtrees[i]->name,
236                      v);
237 #endif
238       
239       if (v == 0)
240         {
241           if (index_in_parent)
242             {
243 #if VERBOSE_FIND
244               _dbus_verbose ("  storing parent index %d\n", i);
245 #endif
246               *index_in_parent = i;
247             }
248
249           if (return_deepest_match)
250             {
251               DBusObjectSubtree *next;
252
253               next = find_subtree_recurse (subtree->subtrees[i],
254                                            &path[1], create_if_not_found, 
255                                            index_in_parent, exact_match);
256               if (next == NULL &&
257                   subtree->invoke_as_fallback)
258                 {
259 #if VERBOSE_FIND
260                   _dbus_verbose ("  no deeper match found, returning %s\n",
261                                  subtree->name);
262 #endif
263                   if (exact_match != NULL)
264                     *exact_match = FALSE;
265                   return subtree;
266                 }
267               else
268                 return next;
269             }
270           else
271             return find_subtree_recurse (subtree->subtrees[i],
272                                          &path[1], create_if_not_found, 
273                                          index_in_parent, exact_match);
274         }
275       else if (v < 0)
276         {
277           goto not_found;
278         }
279
280       ++i;
281     }
282
283  not_found:
284 #if VERBOSE_FIND
285   _dbus_verbose ("  no match found, current tree %s, create_if_not_found = %d\n",
286                  subtree->name, create_if_not_found);
287 #endif
288   
289   if (create_if_not_found)
290     {
291       DBusObjectSubtree* child;
292       DBusObjectSubtree **new_subtrees;
293       int new_n_subtrees;
294
295 #if VERBOSE_FIND
296       _dbus_verbose ("  creating subtree %s\n",
297                      path[0]);
298 #endif
299       
300       child = _dbus_object_subtree_new (path[0],
301                                         NULL, NULL);
302       if (child == NULL)
303         return NULL;
304
305       /* FIXME we should do the "double alloc each time" standard thing */
306       new_n_subtrees = subtree->n_subtrees + 1;
307       new_subtrees = dbus_realloc (subtree->subtrees,
308                                    new_n_subtrees * sizeof (DBusObjectSubtree*));
309       if (new_subtrees == NULL)
310         {
311           child->unregister_function = NULL;
312           child->message_function = NULL;
313           _dbus_object_subtree_unref (child);
314           return NULL;
315         }
316
317       new_subtrees[subtree->n_subtrees] = child;
318       if (index_in_parent)
319         *index_in_parent = subtree->n_subtrees;
320       subtree->subtrees_sorted = FALSE;
321       subtree->n_subtrees = new_n_subtrees;
322       subtree->subtrees = new_subtrees;
323
324       child->parent = subtree;
325
326       return find_subtree_recurse (child,
327                                    &path[1], create_if_not_found, 
328                                    index_in_parent, exact_match);
329     }
330   else
331     {
332       if (exact_match != NULL)
333         *exact_match = FALSE;
334       return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
335     }
336 }
337
338 static DBusObjectSubtree*
339 find_subtree (DBusObjectTree *tree,
340               const char    **path,
341               int            *index_in_parent)
342 {
343   DBusObjectSubtree *subtree;
344
345 #if VERBOSE_FIND
346   _dbus_verbose ("Looking for exact registered subtree\n");
347 #endif
348   
349   subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
350
351   if (subtree && subtree->message_function == NULL)
352     return NULL;
353   else
354     return subtree;
355 }
356
357 static DBusObjectSubtree*
358 lookup_subtree (DBusObjectTree *tree,
359                 const char    **path)
360 {
361 #if VERBOSE_FIND
362   _dbus_verbose ("Looking for subtree\n");
363 #endif
364   return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
365 }
366
367 static DBusObjectSubtree*
368 find_handler (DBusObjectTree *tree,
369               const char    **path,
370               dbus_bool_t    *exact_match)
371 {
372 #if VERBOSE_FIND
373   _dbus_verbose ("Looking for deepest handler\n");
374 #endif
375   _dbus_assert (exact_match != NULL);
376
377   *exact_match = FALSE; /* ensure always initialized */
378   
379   return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
380 }
381
382 static DBusObjectSubtree*
383 ensure_subtree (DBusObjectTree *tree,
384                 const char    **path)
385 {
386 #if VERBOSE_FIND
387   _dbus_verbose ("Ensuring subtree\n");
388 #endif
389   return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
390 }
391
392 /**
393  * Registers a new subtree in the global object tree.
394  *
395  * @param tree the global object tree
396  * @param fallback #TRUE to handle messages to children of this path
397  * @param path NULL-terminated array of path elements giving path to subtree
398  * @param vtable the vtable used to traverse this subtree
399  * @param user_data user data to pass to methods in the vtable
400  * @returns #FALSE if not enough memory
401  */
402 dbus_bool_t
403 _dbus_object_tree_register (DBusObjectTree              *tree,
404                             dbus_bool_t                  fallback,
405                             const char                 **path,
406                             const DBusObjectPathVTable  *vtable,
407                             void                        *user_data)
408 {
409   DBusObjectSubtree  *subtree;
410
411   _dbus_assert (tree != NULL);
412   _dbus_assert (vtable->message_function != NULL);
413   _dbus_assert (path != NULL);
414
415   subtree = ensure_subtree (tree, path);
416   if (subtree == NULL)
417     return FALSE;
418
419 #ifndef DBUS_DISABLE_CHECKS
420   if (subtree->message_function != NULL)
421     {
422       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
423                   path[0] ? path[0] : "null");
424       return FALSE;
425     }
426 #else
427   _dbus_assert (subtree->message_function == NULL);
428 #endif
429
430   subtree->message_function = vtable->message_function;
431   subtree->unregister_function = vtable->unregister_function;
432   subtree->user_data = user_data;
433   subtree->invoke_as_fallback = fallback != FALSE;
434   
435   return TRUE;
436 }
437
438 /**
439  * Unregisters an object subtree that was registered with the
440  * same path.
441  *
442  * @param tree the global object tree
443  * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
444  */
445 void
446 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
447                                          const char             **path)
448 {
449   int i;
450   DBusObjectSubtree *subtree;
451   DBusObjectPathUnregisterFunction unregister_function;
452   void *user_data;
453   DBusConnection *connection;
454
455   _dbus_assert (path != NULL);
456
457   unregister_function = NULL;
458   user_data = NULL;
459
460   subtree = find_subtree (tree, path, &i);
461
462 #ifndef DBUS_DISABLE_CHECKS
463   if (subtree == NULL)
464     {
465       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
466                   path[0] ? path[0] : "null",
467                   path[1] ? path[1] : "null");
468       goto unlock;    
469     }
470 #else
471   _dbus_assert (subtree != NULL);
472 #endif
473
474   _dbus_assert (subtree->parent == NULL ||
475                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
476
477   subtree->message_function = NULL;
478
479   unregister_function = subtree->unregister_function;
480   user_data = subtree->user_data;
481
482   subtree->unregister_function = NULL;
483   subtree->user_data = NULL;
484
485   /* If we have no subtrees of our own, remove from
486    * our parent (FIXME could also be more aggressive
487    * and remove our parent if it becomes empty)
488    */
489   if (subtree->parent && subtree->n_subtrees == 0)
490     {
491       /* assumes a 0-byte memmove is OK */
492       memmove (&subtree->parent->subtrees[i],
493                &subtree->parent->subtrees[i+1],
494                (subtree->parent->n_subtrees - i - 1) *
495                sizeof (subtree->parent->subtrees[0]));
496       subtree->parent->n_subtrees -= 1;
497
498       subtree->parent = NULL;
499
500       _dbus_object_subtree_unref (subtree);
501     }
502   subtree = NULL;
503
504 unlock:
505   connection = tree->connection;
506
507   /* Unlock and call application code */
508 #ifdef DBUS_BUILD_TESTS
509   if (connection)
510 #endif
511     {
512       _dbus_connection_ref_unlocked (connection);
513       _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
514       _dbus_connection_unlock (connection);
515     }
516
517   if (unregister_function)
518     (* unregister_function) (connection, user_data);
519
520 #ifdef DBUS_BUILD_TESTS
521   if (connection)
522 #endif
523     dbus_connection_unref (connection);
524 }
525
526 static void
527 free_subtree_recurse (DBusConnection    *connection,
528                       DBusObjectSubtree *subtree)
529 {
530   /* Delete them from the end, for slightly
531    * more robustness against odd reentrancy.
532    */
533   while (subtree->n_subtrees > 0)
534     {
535       DBusObjectSubtree *child;
536
537       child = subtree->subtrees[subtree->n_subtrees - 1];
538       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
539       subtree->n_subtrees -= 1;
540       child->parent = NULL;
541
542       free_subtree_recurse (connection, child);
543     }
544
545   /* Call application code */
546   if (subtree->unregister_function)
547     (* subtree->unregister_function) (connection,
548                                       subtree->user_data);
549
550   subtree->message_function = NULL;
551   subtree->unregister_function = NULL;
552   subtree->user_data = NULL;
553
554   /* Now free ourselves */
555   _dbus_object_subtree_unref (subtree);
556 }
557
558 /**
559  * Free all the handlers in the tree. Lock on tree's connection
560  * must not be held.
561  *
562  * @param tree the object tree
563  */
564 void
565 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
566 {
567   if (tree->root)
568     free_subtree_recurse (tree->connection,
569                           tree->root);
570   tree->root = NULL;
571 }
572
573 static dbus_bool_t
574 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
575                                             const char    **parent_path,
576                                             char         ***child_entries)
577 {
578   DBusObjectSubtree *subtree;
579   char **retval;
580   
581   _dbus_assert (parent_path != NULL);
582   _dbus_assert (child_entries != NULL);
583
584   *child_entries = NULL;
585   
586   subtree = lookup_subtree (tree, parent_path);
587   if (subtree == NULL)
588     {
589       retval = dbus_new0 (char *, 1);
590     }
591   else
592     {
593       int i;
594       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
595       if (retval == NULL)
596         goto out;
597       i = 0;
598       while (i < subtree->n_subtrees)
599         {
600           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
601           if (retval[i] == NULL)
602             {
603               dbus_free_string_array (retval);
604               retval = NULL;
605               goto out;
606             }
607           ++i;
608         }
609     }
610
611  out:
612     
613   *child_entries = retval;
614   return retval != NULL;
615 }
616
617 static DBusHandlerResult
618 handle_default_introspect_and_unlock (DBusObjectTree          *tree,
619                                       DBusMessage             *message,
620                                       const char             **path)
621 {
622   DBusString xml;
623   DBusHandlerResult result;
624   char **children;
625   int i;
626   DBusMessage *reply;
627   DBusMessageIter iter;
628   const char *v_STRING;
629   dbus_bool_t already_unlocked;
630
631   /* We have the connection lock here */
632
633   already_unlocked = FALSE;
634   
635   _dbus_verbose (" considering default Introspect() handler...\n");
636
637   reply = NULL;
638   
639   if (!dbus_message_is_method_call (message,
640                                     DBUS_INTERFACE_INTROSPECTABLE,
641                                     "Introspect"))
642     {
643 #ifdef DBUS_BUILD_TESTS
644       if (tree->connection)
645 #endif
646         {
647           _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
648           _dbus_connection_unlock (tree->connection);
649         }
650       
651       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
652     }
653
654   _dbus_verbose (" using default Introspect() handler!\n");
655   
656   if (!_dbus_string_init (&xml))
657     {
658 #ifdef DBUS_BUILD_TESTS
659       if (tree->connection)
660 #endif
661         {
662           _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
663           _dbus_connection_unlock (tree->connection);
664         }
665
666       return DBUS_HANDLER_RESULT_NEED_MEMORY;
667     }
668
669   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
670
671   children = NULL;
672   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
673     goto out;
674
675   if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
676     goto out;
677   
678   if (!_dbus_string_append (&xml, "<node>\n"))
679     goto out;
680
681   i = 0;
682   while (children[i] != NULL)
683     {
684       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
685                                        children[i]))
686         goto out;
687
688       ++i;
689     }
690
691   if (!_dbus_string_append (&xml, "</node>\n"))
692     goto out;
693
694   reply = dbus_message_new_method_return (message);
695   if (reply == NULL)
696     goto out;
697
698   dbus_message_iter_init_append (reply, &iter);
699   v_STRING = _dbus_string_get_const_data (&xml);
700   if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
701     goto out;
702   
703 #ifdef DBUS_BUILD_TESTS
704   if (tree->connection)
705 #endif
706     {
707       already_unlocked = TRUE;
708       
709       if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
710         goto out;
711     }
712   
713   result = DBUS_HANDLER_RESULT_HANDLED;
714   
715  out:
716 #ifdef DBUS_BUILD_TESTS
717   if (tree->connection)
718 #endif
719     {
720       if (!already_unlocked)
721         {
722           _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
723           _dbus_connection_unlock (tree->connection);
724         }
725     }
726   
727   _dbus_string_free (&xml);
728   dbus_free_string_array (children);
729   if (reply)
730     dbus_message_unref (reply);
731   
732   return result;
733 }
734
735 /**
736  * Tries to dispatch a message by directing it to handler for the
737  * object path listed in the message header, if any. Messages are
738  * dispatched first to the registered handler that matches the largest
739  * number of path elements; that is, message to /foo/bar/baz would go
740  * to the handler for /foo/bar before the one for /foo.
741  *
742  * @todo thread problems
743  *
744  * @param tree the global object tree
745  * @param message the message to dispatch
746  * @returns whether message was handled successfully
747  */
748 DBusHandlerResult
749 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
750                                        DBusMessage             *message)
751 {
752   char **path;
753   dbus_bool_t exact_match;
754   DBusList *list;
755   DBusList *link;
756   DBusHandlerResult result;
757   DBusObjectSubtree *subtree;
758   
759 #if 0
760   _dbus_verbose ("Dispatch of message by object path\n");
761 #endif
762   
763   path = NULL;
764   if (!dbus_message_get_path_decomposed (message, &path))
765     {
766 #ifdef DBUS_BUILD_TESTS
767       if (tree->connection)
768 #endif
769         {
770           _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
771           _dbus_connection_unlock (tree->connection);
772         }
773       
774       _dbus_verbose ("No memory to get decomposed path\n");
775
776       return DBUS_HANDLER_RESULT_NEED_MEMORY;
777     }
778
779   if (path == NULL)
780     {
781 #ifdef DBUS_BUILD_TESTS
782       if (tree->connection)
783 #endif
784         {
785           _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
786           _dbus_connection_unlock (tree->connection);
787         }
788       
789       _dbus_verbose ("No path field in message\n");
790       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
791     }
792   
793   /* Find the deepest path that covers the path in the message */
794   subtree = find_handler (tree, (const char**) path, &exact_match);
795   
796   /* Build a list of all paths that cover the path in the message */
797
798   list = NULL;
799
800   while (subtree != NULL)
801     {
802       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
803         {
804           _dbus_object_subtree_ref (subtree);
805
806           /* run deepest paths first */
807           if (!_dbus_list_append (&list, subtree))
808             {
809               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
810               _dbus_object_subtree_unref (subtree);
811               goto free_and_return;
812             }
813         }
814
815       exact_match = FALSE;
816       subtree = subtree->parent;
817     }
818
819   _dbus_verbose ("%d handlers in the path tree for this message\n",
820                  _dbus_list_get_length (&list));
821
822   /* Invoke each handler in the list */
823
824   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
825
826   link = _dbus_list_get_first_link (&list);
827   while (link != NULL)
828     {
829       DBusList *next = _dbus_list_get_next_link (&list, link);
830       subtree = link->data;
831
832       /* message_function is NULL if we're unregistered
833        * due to reentrancy
834        */
835       if (subtree->message_function)
836         {
837           DBusObjectPathMessageFunction message_function;
838           void *user_data;
839
840           message_function = subtree->message_function;
841           user_data = subtree->user_data;
842
843 #if 0
844           _dbus_verbose ("  (invoking a handler)\n");
845 #endif
846           
847 #ifdef DBUS_BUILD_TESTS
848           if (tree->connection)
849 #endif
850             {
851               _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
852               _dbus_connection_unlock (tree->connection);
853             }
854
855           /* FIXME you could unregister the subtree in another thread
856            * before we invoke the callback, and I can't figure out a
857            * good way to solve this.
858            */
859
860           result = (* message_function) (tree->connection,
861                                          message,
862                                          user_data);
863
864 #ifdef DBUS_BUILD_TESTS
865           if (tree->connection)
866 #endif
867             _dbus_connection_lock (tree->connection);
868
869           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
870             goto free_and_return;
871         }
872
873       link = next;
874     }
875
876  free_and_return:
877
878   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
879     {
880       /* This hardcoded default handler does a minimal Introspect()
881        */
882       result = handle_default_introspect_and_unlock (tree, message,
883                                                      (const char**) path);
884     }
885   else
886     {
887 #ifdef DBUS_BUILD_TESTS
888       if (tree->connection)
889 #endif
890         {
891           _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
892           _dbus_connection_unlock (tree->connection);
893         }
894     }
895   
896   while (list != NULL)
897     {
898       link = _dbus_list_get_first_link (&list);
899       _dbus_object_subtree_unref (link->data);
900       _dbus_list_remove_link (&list, link);
901     }
902   
903   dbus_free_string_array (path);
904
905   return result;
906 }
907
908 /**
909  * Looks up the data passed to _dbus_object_tree_register() for a
910  * handler at the given path.
911  *
912  * @param tree the global object tree
913  * @param path NULL-terminated array of path elements giving path to subtree
914  * @returns the object's user_data or #NULL if none found
915  */
916 void*
917 _dbus_object_tree_get_user_data_unlocked (DBusObjectTree *tree,
918                                           const char    **path)
919 {
920   dbus_bool_t exact_match;
921   DBusObjectSubtree *subtree;
922
923   _dbus_assert (tree != NULL);
924   _dbus_assert (path != NULL);
925   
926   /* Find the deepest path that covers the path in the message */
927   subtree = find_handler (tree, (const char**) path, &exact_match);
928
929   if ((subtree == NULL) || !exact_match)
930     {
931       _dbus_verbose ("%s: No object at specified path found\n",
932                      _DBUS_FUNCTION_NAME);
933       return NULL;
934     }
935
936   return subtree->user_data;
937 }
938
939 /**
940  * Allocates a subtree object.
941  *
942  * @param name name to duplicate.
943  * @returns newly-allocated subtree
944  */
945 static DBusObjectSubtree*
946 allocate_subtree_object (const char *name)
947 {
948   int len;
949   DBusObjectSubtree *subtree;
950   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
951
952   _dbus_assert (name != NULL);
953
954   len = strlen (name);
955
956   subtree = dbus_malloc (front_padding + (len + 1));
957
958   if (subtree == NULL)
959     return NULL;
960
961   memcpy (subtree->name, name, len + 1);
962
963   return subtree;
964 }
965
966 static DBusObjectSubtree*
967 _dbus_object_subtree_new (const char                  *name,
968                           const DBusObjectPathVTable  *vtable,
969                           void                        *user_data)
970 {
971   DBusObjectSubtree *subtree;
972
973   subtree = allocate_subtree_object (name);
974   if (subtree == NULL)
975     goto oom;
976
977   _dbus_assert (name != NULL);
978
979   subtree->parent = NULL;
980
981   if (vtable)
982     {
983       subtree->message_function = vtable->message_function;
984       subtree->unregister_function = vtable->unregister_function;
985     }
986   else
987     {
988       subtree->message_function = NULL;
989       subtree->unregister_function = NULL;
990     }
991
992   subtree->user_data = user_data;
993   subtree->refcount.value = 1;
994   subtree->subtrees = NULL;
995   subtree->n_subtrees = 0;
996   subtree->subtrees_sorted = TRUE;
997   subtree->invoke_as_fallback = FALSE;
998
999   return subtree;
1000
1001  oom:
1002   if (subtree)
1003     {
1004       dbus_free (subtree);
1005     }
1006
1007   return NULL;
1008 }
1009
1010 static DBusObjectSubtree *
1011 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
1012 {
1013   _dbus_assert (subtree->refcount.value > 0);
1014   _dbus_atomic_inc (&subtree->refcount);
1015
1016   return subtree;
1017 }
1018
1019 static void
1020 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
1021 {
1022   _dbus_assert (subtree->refcount.value > 0);
1023
1024   if (_dbus_atomic_dec (&subtree->refcount) == 1)
1025     {
1026       _dbus_assert (subtree->unregister_function == NULL);
1027       _dbus_assert (subtree->message_function == NULL);
1028
1029       dbus_free (subtree->subtrees);
1030       dbus_free (subtree);
1031     }
1032 }
1033
1034 /**
1035  * Lists the registered fallback handlers and object path handlers at
1036  * the given parent_path. The returned array should be freed with
1037  * dbus_free_string_array().
1038  *
1039  * @param tree the object tree
1040  * @param parent_path the path to list the child handlers of
1041  * @param child_entries returns #NULL-terminated array of children
1042  * @returns #FALSE if no memory to allocate the child entries
1043  */
1044 dbus_bool_t
1045 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
1046                                               const char    **parent_path,
1047                                               char         ***child_entries)
1048 {
1049   dbus_bool_t result;
1050
1051   result = _dbus_object_tree_list_registered_unlocked (tree,
1052                                                        parent_path,
1053                                                        child_entries);
1054   
1055 #ifdef DBUS_BUILD_TESTS
1056   if (tree->connection)
1057 #endif
1058     {
1059       _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
1060       _dbus_connection_unlock (tree->connection);
1061     }
1062
1063   return result;
1064 }
1065
1066
1067 /** Set to 1 to get a bunch of spew about disassembling the path string */
1068 #define VERBOSE_DECOMPOSE 0
1069
1070 /**
1071  * Decompose an object path.  A path of just "/" is
1072  * represented as an empty vector of strings.
1073  * The path need not be nul terminated.
1074  * 
1075  * @param data the path data
1076  * @param len  the length of the path string
1077  * @param path address to store new object path
1078  * @param path_len length of stored path
1079  */
1080 dbus_bool_t
1081 _dbus_decompose_path (const char*     data,
1082                       int             len,
1083                       char         ***path,
1084                       int            *path_len)
1085 {
1086   char **retval;
1087   int n_components;
1088   int i, j, comp;
1089
1090   _dbus_assert (data != NULL);
1091   
1092 #if VERBOSE_DECOMPOSE
1093   _dbus_verbose ("Decomposing path \"%s\"\n",
1094                  data);
1095 #endif
1096   
1097   n_components = 0;
1098   if (len > 1) /* if path is not just "/" */
1099     {
1100       i = 0;
1101       while (i < len)
1102         {
1103           if (data[i] == '/')
1104             n_components += 1;
1105           ++i;
1106         }
1107     }
1108   
1109   retval = dbus_new0 (char*, n_components + 1);
1110
1111   if (retval == NULL)
1112     return FALSE;
1113
1114   comp = 0;
1115   if (n_components == 0)
1116     i = 1;
1117   else
1118     i = 0;
1119   while (comp < n_components)
1120     {
1121       _dbus_assert (i < len);
1122       
1123       if (data[i] == '/')
1124         ++i;
1125       j = i;
1126
1127       while (j < len && data[j] != '/')
1128         ++j;
1129
1130       /* Now [i, j) is the path component */
1131       _dbus_assert (i < j);
1132       _dbus_assert (data[i] != '/');
1133       _dbus_assert (j == len || data[j] == '/');
1134
1135 #if VERBOSE_DECOMPOSE
1136       _dbus_verbose ("  (component in [%d,%d))\n",
1137                      i, j);
1138 #endif
1139       
1140       retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1141       if (retval[comp] == NULL)
1142         {
1143           dbus_free_string_array (retval);
1144           return FALSE;
1145         }
1146       retval[comp][j-i] = '\0';
1147 #if VERBOSE_DECOMPOSE
1148       _dbus_verbose ("  (component %d = \"%s\")\n",
1149                      comp, retval[comp]);
1150 #endif
1151
1152       ++comp;
1153       i = j;
1154     }
1155   _dbus_assert (i == len);
1156   
1157   *path = retval;
1158   if (path_len)
1159     *path_len = n_components;
1160   
1161   return TRUE;
1162 }
1163
1164 /** @} */
1165
1166 #ifdef DBUS_BUILD_TESTS
1167 #include "dbus-test.h"
1168 #include <stdio.h>
1169
1170 static char*
1171 flatten_path (const char **path)
1172 {
1173   DBusString str;
1174   char *s;
1175
1176   if (!_dbus_string_init (&str))
1177     return NULL;
1178
1179   if (path[0] == NULL)
1180     {
1181       if (!_dbus_string_append_byte (&str, '/'))
1182         goto nomem;
1183     }
1184   else
1185     {
1186       int i;
1187       
1188       i = 0;
1189       while (path[i])
1190         {
1191           if (!_dbus_string_append_byte (&str, '/'))
1192             goto nomem;
1193           
1194           if (!_dbus_string_append (&str, path[i]))
1195             goto nomem;
1196           
1197           ++i;
1198         }
1199     }
1200
1201   if (!_dbus_string_steal_data (&str, &s))
1202     goto nomem;
1203
1204   _dbus_string_free (&str);
1205
1206   return s;
1207
1208  nomem:
1209   _dbus_string_free (&str);
1210   return NULL;
1211 }
1212
1213
1214 typedef enum 
1215 {
1216   STR_EQUAL,
1217   STR_PREFIX,
1218   STR_DIFFERENT
1219 } StrComparison;
1220
1221 /* Returns TRUE if container is a parent of child
1222  */
1223 static StrComparison
1224 path_contains (const char **container,
1225                const char **child)
1226 {
1227   int i;
1228
1229   i = 0;
1230   while (child[i] != NULL)
1231     {
1232       int v;
1233
1234       if (container[i] == NULL)
1235         return STR_PREFIX; /* container ran out, child continues;
1236                         * thus the container is a parent of the
1237                         * child.
1238                         */
1239
1240       _dbus_assert (container[i] != NULL);
1241       _dbus_assert (child[i] != NULL);
1242
1243       v = strcmp (container[i], child[i]);
1244
1245       if (v != 0)
1246         return STR_DIFFERENT; /* they overlap until here and then are different,
1247                            * not overlapping
1248                            */
1249
1250       ++i;
1251     }
1252
1253   /* Child ran out; if container also did, they are equal;
1254    * otherwise, the child is a parent of the container.
1255    */
1256   if (container[i] == NULL)
1257     return STR_EQUAL;
1258   else
1259     return STR_DIFFERENT;
1260 }
1261
1262 #if 0
1263 static void
1264 spew_subtree_recurse (DBusObjectSubtree *subtree,
1265                       int                indent)
1266 {
1267   int i;
1268
1269   i = 0;
1270   while (i < indent)
1271     {
1272       _dbus_verbose (" ");
1273       ++i;
1274     }
1275
1276   _dbus_verbose ("%s (%d children)\n",
1277                  subtree->name, subtree->n_subtrees);
1278
1279   i = 0;
1280   while (i < subtree->n_subtrees)
1281     {
1282       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1283
1284       ++i;
1285     }
1286 }
1287
1288 static void
1289 spew_tree (DBusObjectTree *tree)
1290 {
1291   spew_subtree_recurse (tree->root, 0);
1292 }
1293 #endif
1294
1295 /**
1296  * Callback data used in tests
1297  */
1298 typedef struct
1299 {
1300   const char **path; /**< Path */
1301   dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1302   dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1303   dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1304 } TreeTestData;
1305
1306
1307 static void
1308 test_unregister_function (DBusConnection  *connection,
1309                           void            *user_data)
1310 {
1311   TreeTestData *ttd = user_data;
1312
1313   ttd->handler_unregistered = TRUE;
1314 }
1315
1316 static DBusHandlerResult
1317 test_message_function (DBusConnection  *connection,
1318                        DBusMessage     *message,
1319                        void            *user_data)
1320 {
1321   TreeTestData *ttd = user_data;
1322
1323   ttd->message_handled = TRUE;
1324
1325   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1326 }
1327
1328 static dbus_bool_t
1329 do_register (DBusObjectTree *tree,
1330              const char    **path,
1331              dbus_bool_t     fallback,
1332              int             i,
1333              TreeTestData   *tree_test_data)
1334 {
1335   DBusObjectPathVTable vtable = { test_unregister_function,
1336                                   test_message_function, NULL };
1337   
1338   tree_test_data[i].message_handled = FALSE;
1339   tree_test_data[i].handler_unregistered = FALSE;
1340   tree_test_data[i].handler_fallback = fallback;
1341   tree_test_data[i].path = path;
1342
1343   if (!_dbus_object_tree_register (tree, fallback, path,
1344                                    &vtable,
1345                                    &tree_test_data[i]))
1346     return FALSE;
1347
1348   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path) ==
1349                 &tree_test_data[i]);
1350   
1351   return TRUE;
1352 }
1353
1354 static dbus_bool_t
1355 do_test_dispatch (DBusObjectTree *tree,
1356                   const char    **path,
1357                   int             i,
1358                   TreeTestData   *tree_test_data,
1359                   int             n_test_data)
1360 {
1361   DBusMessage *message;
1362   int j;
1363   DBusHandlerResult result;
1364   char *flat;
1365
1366   message = NULL;
1367   
1368   flat = flatten_path (path);
1369   if (flat == NULL)
1370     goto oom;
1371
1372   message = dbus_message_new_method_call (NULL,
1373                                           flat,
1374                                           "org.freedesktop.TestInterface",
1375                                           "Foo");
1376   dbus_free (flat);
1377   if (message == NULL)
1378     goto oom;
1379
1380   j = 0;
1381   while (j < n_test_data)
1382     {
1383       tree_test_data[j].message_handled = FALSE;
1384       ++j;
1385     }
1386
1387   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1388   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1389     goto oom;
1390
1391   _dbus_assert (tree_test_data[i].message_handled);
1392
1393   j = 0;
1394   while (j < n_test_data)
1395     {
1396       if (tree_test_data[j].message_handled)
1397         {
1398           if (tree_test_data[j].handler_fallback)
1399             _dbus_assert (path_contains (tree_test_data[j].path,
1400                                          path) != STR_DIFFERENT);
1401           else
1402             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1403         }
1404       else
1405         {
1406           if (tree_test_data[j].handler_fallback)
1407             _dbus_assert (path_contains (tree_test_data[j].path,
1408                                          path) == STR_DIFFERENT);
1409           else
1410             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1411         }
1412
1413       ++j;
1414     }
1415
1416   dbus_message_unref (message);
1417
1418   return TRUE;
1419
1420  oom:
1421   if (message)
1422     dbus_message_unref (message);
1423   return FALSE;
1424 }
1425
1426 static size_t
1427 string_array_length (const char **array)
1428 {
1429   size_t i;
1430   for (i = 0; array[i]; i++) ;
1431   return i;
1432 }
1433
1434 typedef struct
1435 {
1436   const char *path;
1437   const char *result[20];
1438 } DecomposePathTest;
1439
1440 static DecomposePathTest decompose_tests[] = {
1441   { "/foo", { "foo", NULL } },
1442   { "/foo/bar", { "foo", "bar", NULL } },
1443   { "/", { NULL } },
1444   { "/a/b", { "a", "b", NULL } },
1445   { "/a/b/c", { "a", "b", "c", NULL } },
1446   { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
1447   { "/foo/bar/q", { "foo", "bar", "q", NULL } },
1448   { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
1449 };
1450
1451 static dbus_bool_t
1452 run_decompose_tests (void)
1453 {
1454   int i;
1455
1456   i = 0;
1457   while (i < _DBUS_N_ELEMENTS (decompose_tests))
1458     {
1459       char **result;
1460       int    result_len;
1461       int    expected_len;
1462
1463       if (!_dbus_decompose_path (decompose_tests[i].path,
1464                                  strlen (decompose_tests[i].path),
1465                                  &result, &result_len))
1466         return FALSE;
1467
1468       expected_len = string_array_length (decompose_tests[i].result);
1469       
1470       if (result_len != (int) string_array_length ((const char**)result) ||
1471           expected_len != result_len ||
1472           path_contains (decompose_tests[i].result,
1473                          (const char**) result) != STR_EQUAL)
1474         {
1475           int real_len = string_array_length ((const char**)result);
1476           _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1477                       decompose_tests[i].path, expected_len, result_len,
1478                       real_len);
1479           _dbus_warn ("Decompose resulted in elements: { ");
1480           i = 0;
1481           while (i < real_len)
1482             {
1483               _dbus_warn ("\"%s\"%s", result[i],
1484                           (i + 1) == real_len ? "" : ", ");
1485               ++i;
1486             }
1487           _dbus_warn ("}\n");
1488           _dbus_assert_not_reached ("path decompose failed\n");
1489         }
1490
1491       dbus_free_string_array (result);
1492
1493       ++i;
1494     }
1495   
1496   return TRUE;
1497 }
1498
1499 static dbus_bool_t
1500 object_tree_test_iteration (void *data)
1501 {
1502   const char *path0[] = { NULL };
1503   const char *path1[] = { "foo", NULL };
1504   const char *path2[] = { "foo", "bar", NULL };
1505   const char *path3[] = { "foo", "bar", "baz", NULL };
1506   const char *path4[] = { "foo", "bar", "boo", NULL };
1507   const char *path5[] = { "blah", NULL };
1508   const char *path6[] = { "blah", "boof", NULL };
1509   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1510   const char *path8[] = { "childless", NULL };
1511   DBusObjectTree *tree;
1512   TreeTestData tree_test_data[9];
1513   int i;
1514   dbus_bool_t exact_match;
1515
1516   if (!run_decompose_tests ())
1517     return FALSE;
1518   
1519   tree = NULL;
1520
1521   tree = _dbus_object_tree_new (NULL);
1522   if (tree == NULL)
1523     goto out;
1524
1525   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1526     goto out;
1527
1528   _dbus_assert (find_subtree (tree, path0, NULL));
1529   _dbus_assert (!find_subtree (tree, path1, NULL));
1530   _dbus_assert (!find_subtree (tree, path2, NULL));
1531   _dbus_assert (!find_subtree (tree, path3, NULL));
1532   _dbus_assert (!find_subtree (tree, path4, NULL));
1533   _dbus_assert (!find_subtree (tree, path5, NULL));
1534   _dbus_assert (!find_subtree (tree, path6, NULL));
1535   _dbus_assert (!find_subtree (tree, path7, NULL));
1536   _dbus_assert (!find_subtree (tree, path8, NULL));
1537
1538   _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1539   _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
1540   _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
1541   _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
1542   _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
1543   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1544   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1545   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1546   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1547   
1548   if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1549     goto out;
1550
1551   _dbus_assert (find_subtree (tree, path0, NULL));
1552   _dbus_assert (find_subtree (tree, path1, NULL));
1553   _dbus_assert (!find_subtree (tree, path2, NULL));
1554   _dbus_assert (!find_subtree (tree, path3, NULL));
1555   _dbus_assert (!find_subtree (tree, path4, NULL));
1556   _dbus_assert (!find_subtree (tree, path5, NULL));
1557   _dbus_assert (!find_subtree (tree, path6, NULL));
1558   _dbus_assert (!find_subtree (tree, path7, NULL));
1559   _dbus_assert (!find_subtree (tree, path8, NULL));
1560
1561   _dbus_assert (find_handler (tree, path0, &exact_match) &&  exact_match);
1562   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
1563   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1564   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1565   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1566   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1567   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1568   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1569   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1570
1571   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1572     goto out;
1573
1574   _dbus_assert (find_subtree (tree, path1, NULL));
1575   _dbus_assert (find_subtree (tree, path2, NULL));
1576   _dbus_assert (!find_subtree (tree, path3, NULL));
1577   _dbus_assert (!find_subtree (tree, path4, NULL));
1578   _dbus_assert (!find_subtree (tree, path5, NULL));
1579   _dbus_assert (!find_subtree (tree, path6, NULL));
1580   _dbus_assert (!find_subtree (tree, path7, NULL));
1581   _dbus_assert (!find_subtree (tree, path8, NULL));
1582
1583   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1584     goto out;
1585
1586   _dbus_assert (find_subtree (tree, path0, NULL));
1587   _dbus_assert (find_subtree (tree, path1, NULL));
1588   _dbus_assert (find_subtree (tree, path2, NULL));
1589   _dbus_assert (find_subtree (tree, path3, NULL));
1590   _dbus_assert (!find_subtree (tree, path4, NULL));
1591   _dbus_assert (!find_subtree (tree, path5, NULL));
1592   _dbus_assert (!find_subtree (tree, path6, NULL));
1593   _dbus_assert (!find_subtree (tree, path7, NULL));
1594   _dbus_assert (!find_subtree (tree, path8, NULL));
1595   
1596   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1597     goto out;
1598
1599   _dbus_assert (find_subtree (tree, path0, NULL));
1600   _dbus_assert (find_subtree (tree, path1, NULL));
1601   _dbus_assert (find_subtree (tree, path2, NULL));
1602   _dbus_assert (find_subtree (tree, path3, NULL));  
1603   _dbus_assert (find_subtree (tree, path4, NULL));
1604   _dbus_assert (!find_subtree (tree, path5, NULL));
1605   _dbus_assert (!find_subtree (tree, path6, NULL));
1606   _dbus_assert (!find_subtree (tree, path7, NULL));
1607   _dbus_assert (!find_subtree (tree, path8, NULL));
1608   
1609   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1610     goto out;
1611
1612   _dbus_assert (find_subtree (tree, path0, NULL));
1613   _dbus_assert (find_subtree (tree, path1, NULL));
1614   _dbus_assert (find_subtree (tree, path2, NULL));
1615   _dbus_assert (find_subtree (tree, path3, NULL));
1616   _dbus_assert (find_subtree (tree, path4, NULL));
1617   _dbus_assert (find_subtree (tree, path5, NULL));
1618   _dbus_assert (!find_subtree (tree, path6, NULL));
1619   _dbus_assert (!find_subtree (tree, path7, NULL));
1620   _dbus_assert (!find_subtree (tree, path8, NULL));
1621
1622   _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root &&  exact_match);
1623   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
1624   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
1625   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
1626   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
1627   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
1628   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1629   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1630   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1631
1632   if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1633     goto out;
1634
1635   _dbus_assert (find_subtree (tree, path0, NULL));
1636   _dbus_assert (find_subtree (tree, path1, NULL));
1637   _dbus_assert (find_subtree (tree, path2, NULL));
1638   _dbus_assert (find_subtree (tree, path3, NULL));
1639   _dbus_assert (find_subtree (tree, path4, NULL));
1640   _dbus_assert (find_subtree (tree, path5, NULL));
1641   _dbus_assert (find_subtree (tree, path6, NULL));
1642   _dbus_assert (!find_subtree (tree, path7, NULL));
1643   _dbus_assert (!find_subtree (tree, path8, NULL));
1644
1645   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1646     goto out;
1647
1648   _dbus_assert (find_subtree (tree, path0, NULL));
1649   _dbus_assert (find_subtree (tree, path1, NULL));
1650   _dbus_assert (find_subtree (tree, path2, NULL));
1651   _dbus_assert (find_subtree (tree, path3, NULL));
1652   _dbus_assert (find_subtree (tree, path4, NULL));
1653   _dbus_assert (find_subtree (tree, path5, NULL));
1654   _dbus_assert (find_subtree (tree, path6, NULL));
1655   _dbus_assert (find_subtree (tree, path7, NULL));
1656   _dbus_assert (!find_subtree (tree, path8, NULL));
1657
1658   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1659     goto out;
1660
1661   _dbus_assert (find_subtree (tree, path0, NULL));
1662   _dbus_assert (find_subtree (tree, path1, NULL));
1663   _dbus_assert (find_subtree (tree, path2, NULL));
1664   _dbus_assert (find_subtree (tree, path3, NULL));
1665   _dbus_assert (find_subtree (tree, path4, NULL));
1666   _dbus_assert (find_subtree (tree, path5, NULL));
1667   _dbus_assert (find_subtree (tree, path6, NULL));
1668   _dbus_assert (find_subtree (tree, path7, NULL));
1669   _dbus_assert (find_subtree (tree, path8, NULL));
1670
1671   _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root &&  exact_match);
1672   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1673   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1674   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1675   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1676   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1677   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1678   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1679   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1680   
1681   /* test the list_registered function */
1682
1683   {
1684     const char *root[] = { NULL };
1685     char **child_entries;
1686     int nb;
1687
1688     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1689     if (child_entries != NULL)
1690       {
1691         nb = string_array_length ((const char**)child_entries);
1692         _dbus_assert (nb == 1);
1693         dbus_free_string_array (child_entries);
1694       }
1695
1696     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1697     if (child_entries != NULL)
1698       {
1699         nb = string_array_length ((const char**)child_entries);
1700         _dbus_assert (nb == 2);
1701         dbus_free_string_array (child_entries);
1702       }
1703
1704     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1705     if (child_entries != NULL)
1706       {
1707         nb = string_array_length ((const char**)child_entries);
1708         _dbus_assert (nb == 0);
1709         dbus_free_string_array (child_entries);
1710       }
1711
1712     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1713     if (child_entries != NULL)
1714       {
1715         nb = string_array_length ((const char**)child_entries);
1716         _dbus_assert (nb == 3);
1717         dbus_free_string_array (child_entries);
1718       }
1719   }
1720
1721   /* Check that destroying tree calls unregister funcs */
1722   _dbus_object_tree_unref (tree);
1723
1724   i = 0;
1725   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1726     {
1727       _dbus_assert (tree_test_data[i].handler_unregistered);
1728       _dbus_assert (!tree_test_data[i].message_handled);
1729       ++i;
1730     }
1731
1732   /* Now start again and try the individual unregister function */
1733   tree = _dbus_object_tree_new (NULL);
1734   if (tree == NULL)
1735     goto out;
1736
1737   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1738     goto out;
1739   if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1740     goto out;
1741   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1742     goto out;
1743   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1744     goto out;
1745   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1746     goto out;
1747   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1748     goto out;
1749   if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1750     goto out;
1751   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1752     goto out;
1753   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1754     goto out;
1755
1756   _dbus_object_tree_unregister_and_unlock (tree, path0);
1757   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path0) == NULL);
1758
1759   _dbus_assert (!find_subtree (tree, path0, NULL));
1760   _dbus_assert (find_subtree (tree, path1, NULL));
1761   _dbus_assert (find_subtree (tree, path2, NULL));
1762   _dbus_assert (find_subtree (tree, path3, NULL));
1763   _dbus_assert (find_subtree (tree, path4, NULL));
1764   _dbus_assert (find_subtree (tree, path5, NULL));
1765   _dbus_assert (find_subtree (tree, path6, NULL));
1766   _dbus_assert (find_subtree (tree, path7, NULL));
1767   _dbus_assert (find_subtree (tree, path8, NULL));
1768   
1769   _dbus_object_tree_unregister_and_unlock (tree, path1);
1770   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path1) == NULL);
1771
1772   _dbus_assert (!find_subtree (tree, path0, NULL));
1773   _dbus_assert (!find_subtree (tree, path1, NULL));
1774   _dbus_assert (find_subtree (tree, path2, NULL));
1775   _dbus_assert (find_subtree (tree, path3, NULL));
1776   _dbus_assert (find_subtree (tree, path4, NULL));
1777   _dbus_assert (find_subtree (tree, path5, NULL));
1778   _dbus_assert (find_subtree (tree, path6, NULL));
1779   _dbus_assert (find_subtree (tree, path7, NULL));
1780   _dbus_assert (find_subtree (tree, path8, NULL));
1781
1782   _dbus_object_tree_unregister_and_unlock (tree, path2);
1783   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path2) == NULL);
1784
1785   _dbus_assert (!find_subtree (tree, path0, NULL));
1786   _dbus_assert (!find_subtree (tree, path1, NULL));
1787   _dbus_assert (!find_subtree (tree, path2, NULL));
1788   _dbus_assert (find_subtree (tree, path3, NULL));
1789   _dbus_assert (find_subtree (tree, path4, NULL));
1790   _dbus_assert (find_subtree (tree, path5, NULL));
1791   _dbus_assert (find_subtree (tree, path6, NULL));
1792   _dbus_assert (find_subtree (tree, path7, NULL));
1793   _dbus_assert (find_subtree (tree, path8, NULL));
1794   
1795   _dbus_object_tree_unregister_and_unlock (tree, path3);
1796   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path3) == NULL);
1797
1798   _dbus_assert (!find_subtree (tree, path0, NULL));
1799   _dbus_assert (!find_subtree (tree, path1, NULL));
1800   _dbus_assert (!find_subtree (tree, path2, NULL));
1801   _dbus_assert (!find_subtree (tree, path3, NULL));
1802   _dbus_assert (find_subtree (tree, path4, NULL));
1803   _dbus_assert (find_subtree (tree, path5, NULL));
1804   _dbus_assert (find_subtree (tree, path6, NULL));
1805   _dbus_assert (find_subtree (tree, path7, NULL));
1806   _dbus_assert (find_subtree (tree, path8, NULL));
1807   
1808   _dbus_object_tree_unregister_and_unlock (tree, path4);
1809   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path4) == NULL);
1810
1811   _dbus_assert (!find_subtree (tree, path0, NULL));
1812   _dbus_assert (!find_subtree (tree, path1, NULL));
1813   _dbus_assert (!find_subtree (tree, path2, NULL));
1814   _dbus_assert (!find_subtree (tree, path3, NULL));
1815   _dbus_assert (!find_subtree (tree, path4, NULL));
1816   _dbus_assert (find_subtree (tree, path5, NULL));
1817   _dbus_assert (find_subtree (tree, path6, NULL));
1818   _dbus_assert (find_subtree (tree, path7, NULL));
1819   _dbus_assert (find_subtree (tree, path8, NULL));
1820   
1821   _dbus_object_tree_unregister_and_unlock (tree, path5);
1822   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path5) == NULL);
1823
1824   _dbus_assert (!find_subtree (tree, path0, NULL));
1825   _dbus_assert (!find_subtree (tree, path1, NULL));
1826   _dbus_assert (!find_subtree (tree, path2, NULL));
1827   _dbus_assert (!find_subtree (tree, path3, NULL));
1828   _dbus_assert (!find_subtree (tree, path4, NULL));
1829   _dbus_assert (!find_subtree (tree, path5, NULL));
1830   _dbus_assert (find_subtree (tree, path6, NULL));
1831   _dbus_assert (find_subtree (tree, path7, NULL));
1832   _dbus_assert (find_subtree (tree, path8, NULL));
1833   
1834   _dbus_object_tree_unregister_and_unlock (tree, path6);
1835   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path6) == NULL);
1836
1837   _dbus_assert (!find_subtree (tree, path0, NULL));
1838   _dbus_assert (!find_subtree (tree, path1, NULL));
1839   _dbus_assert (!find_subtree (tree, path2, NULL));
1840   _dbus_assert (!find_subtree (tree, path3, NULL));
1841   _dbus_assert (!find_subtree (tree, path4, NULL));
1842   _dbus_assert (!find_subtree (tree, path5, NULL));
1843   _dbus_assert (!find_subtree (tree, path6, NULL));
1844   _dbus_assert (find_subtree (tree, path7, NULL));
1845   _dbus_assert (find_subtree (tree, path8, NULL));
1846
1847   _dbus_object_tree_unregister_and_unlock (tree, path7);
1848   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path7) == NULL);
1849
1850   _dbus_assert (!find_subtree (tree, path0, NULL));
1851   _dbus_assert (!find_subtree (tree, path1, NULL));
1852   _dbus_assert (!find_subtree (tree, path2, NULL));
1853   _dbus_assert (!find_subtree (tree, path3, NULL));
1854   _dbus_assert (!find_subtree (tree, path4, NULL));
1855   _dbus_assert (!find_subtree (tree, path5, NULL));
1856   _dbus_assert (!find_subtree (tree, path6, NULL));
1857   _dbus_assert (!find_subtree (tree, path7, NULL));
1858   _dbus_assert (find_subtree (tree, path8, NULL));
1859
1860   _dbus_object_tree_unregister_and_unlock (tree, path8);
1861   _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path8) == NULL);
1862
1863   _dbus_assert (!find_subtree (tree, path0, NULL));
1864   _dbus_assert (!find_subtree (tree, path1, NULL));
1865   _dbus_assert (!find_subtree (tree, path2, NULL));
1866   _dbus_assert (!find_subtree (tree, path3, NULL));
1867   _dbus_assert (!find_subtree (tree, path4, NULL));
1868   _dbus_assert (!find_subtree (tree, path5, NULL));
1869   _dbus_assert (!find_subtree (tree, path6, NULL));
1870   _dbus_assert (!find_subtree (tree, path7, NULL));
1871   _dbus_assert (!find_subtree (tree, path8, NULL));
1872   
1873   i = 0;
1874   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1875     {
1876       _dbus_assert (tree_test_data[i].handler_unregistered);
1877       _dbus_assert (!tree_test_data[i].message_handled);
1878       ++i;
1879     }
1880
1881   /* Register it all again, and test dispatch */
1882   
1883   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1884     goto out;
1885   if (!do_register (tree, path1, FALSE, 1, tree_test_data))
1886     goto out;
1887   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1888     goto out;
1889   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1890     goto out;
1891   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1892     goto out;
1893   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1894     goto out;
1895   if (!do_register (tree, path6, FALSE, 6, tree_test_data))
1896     goto out;
1897   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1898     goto out;
1899   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1900     goto out;
1901
1902 #if 0
1903   spew_tree (tree);
1904 #endif
1905
1906   if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1907     goto out;
1908   if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1909     goto out;
1910   if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1911     goto out;
1912   if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1913     goto out;
1914   if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1915     goto out;
1916   if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1917     goto out;
1918   if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1919     goto out;
1920   if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1921     goto out;
1922   if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1923     goto out;
1924   
1925  out:
1926   if (tree)
1927     {
1928       /* test ref */
1929       _dbus_object_tree_ref (tree);
1930       _dbus_object_tree_unref (tree);
1931       _dbus_object_tree_unref (tree);
1932     }
1933
1934   return TRUE;
1935 }
1936
1937 /**
1938  * @ingroup DBusObjectTree
1939  * Unit test for DBusObjectTree
1940  * @returns #TRUE on success.
1941  */
1942 dbus_bool_t
1943 _dbus_object_tree_test (void)
1944 {
1945   _dbus_test_oom_handling ("object tree",
1946                            object_tree_test_iteration,
1947                            NULL);
1948
1949   return TRUE;
1950 }
1951
1952 #endif /* DBUS_BUILD_TESTS */