2004-06-02 Kristian Høgsberg <krh@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  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.0
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   return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
377 }
378
379 static DBusObjectSubtree*
380 ensure_subtree (DBusObjectTree *tree,
381                 const char    **path)
382 {
383 #if VERBOSE_FIND
384   _dbus_verbose ("Ensuring subtree\n");
385 #endif
386   return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
387 }
388
389 /**
390  * Registers a new subtree in the global object tree.
391  *
392  * @param tree the global object tree
393  * @param fallback #TRUE to handle messages to children of this path
394  * @param path NULL-terminated array of path elements giving path to subtree
395  * @param vtable the vtable used to traverse this subtree
396  * @param user_data user data to pass to methods in the vtable
397  * @returns #FALSE if not enough memory
398  */
399 dbus_bool_t
400 _dbus_object_tree_register (DBusObjectTree              *tree,
401                             dbus_bool_t                  fallback,
402                             const char                 **path,
403                             const DBusObjectPathVTable  *vtable,
404                             void                        *user_data)
405 {
406   DBusObjectSubtree  *subtree;
407
408   _dbus_assert (tree != NULL);
409   _dbus_assert (vtable->message_function != NULL);
410   _dbus_assert (path != NULL);
411
412   subtree = ensure_subtree (tree, path);
413   if (subtree == NULL)
414     return FALSE;
415
416 #ifndef DBUS_DISABLE_CHECKS
417   if (subtree->message_function != NULL)
418     {
419       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
420                   path[0] ? path[0] : "null");
421       return FALSE;
422     }
423 #else
424   _dbus_assert (subtree->message_function == NULL);
425 #endif
426
427   subtree->message_function = vtable->message_function;
428   subtree->unregister_function = vtable->unregister_function;
429   subtree->user_data = user_data;
430   subtree->invoke_as_fallback = fallback != FALSE;
431   
432   return TRUE;
433 }
434
435 /**
436  * Unregisters an object subtree that was registered with the
437  * same path.
438  *
439  * @param tree the global object tree
440  * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
441  */
442 void
443 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
444                                          const char             **path)
445 {
446   int i;
447   DBusObjectSubtree *subtree;
448   DBusObjectPathUnregisterFunction unregister_function;
449   void *user_data;
450   DBusConnection *connection;
451
452   _dbus_assert (path != NULL);
453
454   subtree = find_subtree (tree, path, &i);
455
456 #ifndef DBUS_DISABLE_CHECKS
457   if (subtree == NULL)
458     {
459       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
460                   path[0] ? path[0] : "null",
461                   path[1] ? path[1] : "null");
462       return;
463     }
464 #else
465   _dbus_assert (subtree != NULL);
466 #endif
467
468   _dbus_assert (subtree->parent == NULL ||
469                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
470
471   subtree->message_function = NULL;
472
473   unregister_function = subtree->unregister_function;
474   user_data = subtree->user_data;
475
476   subtree->unregister_function = NULL;
477   subtree->user_data = NULL;
478
479   /* If we have no subtrees of our own, remove from
480    * our parent (FIXME could also be more aggressive
481    * and remove our parent if it becomes empty)
482    */
483   if (subtree->parent && subtree->n_subtrees == 0)
484     {
485       /* assumes a 0-byte memmove is OK */
486       memmove (&subtree->parent->subtrees[i],
487                &subtree->parent->subtrees[i+1],
488                (subtree->parent->n_subtrees - i - 1) *
489                sizeof (subtree->parent->subtrees[0]));
490       subtree->parent->n_subtrees -= 1;
491
492       subtree->parent = NULL;
493
494       _dbus_object_subtree_unref (subtree);
495     }
496   subtree = NULL;
497
498   connection = tree->connection;
499
500   /* Unlock and call application code */
501 #ifdef DBUS_BUILD_TESTS
502   if (connection)
503 #endif
504     {
505       _dbus_connection_ref_unlocked (connection);
506       _dbus_connection_unlock (connection);
507     }
508
509   if (unregister_function)
510     (* unregister_function) (connection, user_data);
511
512 #ifdef DBUS_BUILD_TESTS
513   if (connection)
514 #endif
515     dbus_connection_unref (connection);
516 }
517
518 static void
519 free_subtree_recurse (DBusConnection    *connection,
520                       DBusObjectSubtree *subtree)
521 {
522   /* Delete them from the end, for slightly
523    * more robustness against odd reentrancy.
524    */
525   while (subtree->n_subtrees > 0)
526     {
527       DBusObjectSubtree *child;
528
529       child = subtree->subtrees[subtree->n_subtrees - 1];
530       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
531       subtree->n_subtrees -= 1;
532       child->parent = NULL;
533
534       free_subtree_recurse (connection, child);
535     }
536
537   /* Call application code */
538   if (subtree->unregister_function)
539     {
540       (* subtree->unregister_function) (connection,
541                                         subtree->user_data);
542       subtree->message_function = NULL;
543       subtree->unregister_function = NULL;
544       subtree->user_data = NULL;
545     }
546
547   /* Now free ourselves */
548   _dbus_object_subtree_unref (subtree);
549 }
550
551 /**
552  * Free all the handlers in the tree. Lock on tree's connection
553  * must not be held.
554  *
555  * @param tree the object tree
556  */
557 void
558 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
559 {
560   if (tree->root)
561     free_subtree_recurse (tree->connection,
562                           tree->root);
563   tree->root = NULL;
564 }
565
566 static dbus_bool_t
567 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
568                                             const char    **parent_path,
569                                             char         ***child_entries)
570 {
571   DBusObjectSubtree *subtree;
572   char **retval;
573   
574   _dbus_assert (parent_path != NULL);
575   _dbus_assert (child_entries != NULL);
576
577   *child_entries = NULL;
578   
579   subtree = lookup_subtree (tree, parent_path);
580   if (subtree == NULL)
581     {
582       retval = dbus_new0 (char *, 1);
583     }
584   else
585     {
586       int i;
587       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
588       if (retval == NULL)
589         goto out;
590       i = 0;
591       while (i < subtree->n_subtrees)
592         {
593           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
594           if (retval[i] == NULL)
595             {
596               dbus_free_string_array (retval);
597               retval = NULL;
598               goto out;
599             }
600           ++i;
601         }
602     }
603
604  out:
605     
606   *child_entries = retval;
607   return retval != NULL;
608 }
609
610 static DBusHandlerResult
611 handle_default_introspect_unlocked (DBusObjectTree          *tree,
612                                     DBusMessage             *message,
613                                     const char             **path)
614 {
615   DBusString xml;
616   DBusHandlerResult result;
617   char **children;
618   int i;
619
620   if (!dbus_message_is_method_call (message,
621                                     DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
622                                     "Introspect"))
623     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
624   
625   if (!_dbus_string_init (&xml))
626     return DBUS_HANDLER_RESULT_NEED_MEMORY;
627
628   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
629
630   children = NULL;
631   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
632     goto out;
633
634   if (!_dbus_string_append (&xml, "<node>\n"))
635     goto out;
636
637   i = 0;
638   while (children[i] != NULL)
639     {
640       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
641                                        children[i]))
642         goto out;
643
644       ++i;
645     }
646
647   if (!_dbus_string_append (&xml, "</node>\n"))
648     goto out;
649   
650   result = DBUS_HANDLER_RESULT_HANDLED;
651   
652  out:
653   _dbus_string_free (&xml);
654   dbus_free_string_array (children);
655   
656   return result;
657 }
658
659 /**
660  * Tries to dispatch a message by directing it to handler for the
661  * object path listed in the message header, if any. Messages are
662  * dispatched first to the registered handler that matches the largest
663  * number of path elements; that is, message to /foo/bar/baz would go
664  * to the handler for /foo/bar before the one for /foo.
665  *
666  * @todo thread problems
667  *
668  * @param tree the global object tree
669  * @param message the message to dispatch
670  * @returns whether message was handled successfully
671  */
672 DBusHandlerResult
673 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
674                                        DBusMessage             *message)
675 {
676   char **path;
677   dbus_bool_t exact_match;
678   DBusList *list;
679   DBusList *link;
680   DBusHandlerResult result;
681   DBusObjectSubtree *subtree;
682   
683 #if 0
684   _dbus_verbose ("Dispatch of message by object path\n");
685 #endif
686   
687   path = NULL;
688   if (!dbus_message_get_path_decomposed (message, &path))
689     {
690 #ifdef DBUS_BUILD_TESTS
691       if (tree->connection)
692 #endif
693         _dbus_connection_unlock (tree->connection);
694       
695       _dbus_verbose ("No memory to get decomposed path\n");
696
697       return DBUS_HANDLER_RESULT_NEED_MEMORY;
698     }
699
700   if (path == NULL)
701     {
702 #ifdef DBUS_BUILD_TESTS
703       if (tree->connection)
704 #endif
705         _dbus_connection_unlock (tree->connection);
706       
707       _dbus_verbose ("No path field in message\n");
708       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
709     }
710   
711   /* Find the deepest path that covers the path in the message */
712   subtree = find_handler (tree, (const char**) path, &exact_match);
713   
714   /* Build a list of all paths that cover the path in the message */
715
716   list = NULL;
717
718   while (subtree != NULL)
719     {
720       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
721         {
722           _dbus_object_subtree_ref (subtree);
723
724           /* run deepest paths first */
725           if (!_dbus_list_append (&list, subtree))
726             {
727               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
728               _dbus_object_subtree_unref (subtree);
729               goto free_and_return;
730             }
731         }
732
733       exact_match = FALSE;
734       subtree = subtree->parent;
735     }
736
737   _dbus_verbose ("%d handlers in the path tree for this message\n",
738                  _dbus_list_get_length (&list));
739
740   /* Invoke each handler in the list */
741
742   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
743
744   link = _dbus_list_get_first_link (&list);
745   while (link != NULL)
746     {
747       DBusList *next = _dbus_list_get_next_link (&list, link);
748       subtree = link->data;
749
750       /* message_function is NULL if we're unregistered
751        * due to reentrancy
752        */
753       if (subtree->message_function)
754         {
755           DBusObjectPathMessageFunction message_function;
756           void *user_data;
757
758           message_function = subtree->message_function;
759           user_data = subtree->user_data;
760
761 #if 0
762           _dbus_verbose ("  (invoking a handler)\n");
763 #endif
764           
765 #ifdef DBUS_BUILD_TESTS
766           if (tree->connection)
767 #endif
768             _dbus_connection_unlock (tree->connection);
769
770           /* FIXME you could unregister the subtree in another thread
771            * before we invoke the callback, and I can't figure out a
772            * good way to solve this.
773            */
774
775           result = (* message_function) (tree->connection,
776                                          message,
777                                          user_data);
778
779 #ifdef DBUS_BUILD_TESTS
780           if (tree->connection)
781 #endif
782             _dbus_connection_lock (tree->connection);
783
784           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
785             goto free_and_return;
786         }
787
788       link = next;
789     }
790
791  free_and_return:
792
793   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
794     {
795       /* This hardcoded default handler does a minimal Introspect()
796        */
797       result = handle_default_introspect_unlocked (tree, message,
798                                                    (const char**) path);
799     }
800
801 #ifdef DBUS_BUILD_TESTS
802   if (tree->connection)
803 #endif
804     _dbus_connection_unlock (tree->connection);
805   
806   while (list != NULL)
807     {
808       link = _dbus_list_get_first_link (&list);
809       _dbus_object_subtree_unref (link->data);
810       _dbus_list_remove_link (&list, link);
811     }
812   
813   dbus_free_string_array (path);
814
815   return result;
816 }
817
818 /**
819  * Allocates a subtree object.
820  *
821  * @param name name to duplicate.
822  * @returns newly-allocated subtree
823  */
824 static DBusObjectSubtree*
825 allocate_subtree_object (const char *name)
826 {
827   int len;
828   DBusObjectSubtree *subtree;
829   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
830
831   _dbus_assert (name != NULL);
832
833   len = strlen (name);
834
835   subtree = dbus_malloc (front_padding + (len + 1));
836
837   if (subtree == NULL)
838     return NULL;
839
840   memcpy (subtree->name, name, len + 1);
841
842   return subtree;
843 }
844
845 static DBusObjectSubtree*
846 _dbus_object_subtree_new (const char                  *name,
847                           const DBusObjectPathVTable  *vtable,
848                           void                        *user_data)
849 {
850   DBusObjectSubtree *subtree;
851
852   subtree = allocate_subtree_object (name);
853   if (subtree == NULL)
854     goto oom;
855
856   _dbus_assert (name != NULL);
857
858   subtree->parent = NULL;
859
860   if (vtable)
861     {
862       subtree->message_function = vtable->message_function;
863       subtree->unregister_function = vtable->unregister_function;
864     }
865   else
866     {
867       subtree->message_function = NULL;
868       subtree->unregister_function = NULL;
869     }
870
871   subtree->user_data = user_data;
872   subtree->refcount.value = 1;
873   subtree->subtrees = NULL;
874   subtree->n_subtrees = 0;
875   subtree->subtrees_sorted = TRUE;
876   subtree->invoke_as_fallback = FALSE;
877
878   return subtree;
879
880  oom:
881   if (subtree)
882     {
883       dbus_free (subtree);
884     }
885
886   return NULL;
887 }
888
889 static DBusObjectSubtree *
890 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
891 {
892   _dbus_assert (subtree->refcount.value > 0);
893   _dbus_atomic_inc (&subtree->refcount);
894
895   return subtree;
896 }
897
898 static void
899 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
900 {
901   _dbus_assert (subtree->refcount.value > 0);
902
903   if (_dbus_atomic_dec (&subtree->refcount) == 1)
904     {
905       _dbus_assert (subtree->unregister_function == NULL);
906       _dbus_assert (subtree->message_function == NULL);
907
908       dbus_free (subtree->subtrees);
909       dbus_free (subtree);
910     }
911 }
912
913 /**
914  * Lists the registered fallback handlers and object path handlers at
915  * the given parent_path. The returned array should be freed with
916  * dbus_free_string_array().
917  *
918  * @param tree the object tree
919  * @param parent_path the path to list the child handlers of
920  * @param child_entries returns #NULL-terminated array of children
921  * @returns #FALSE if no memory to allocate the child entries
922  */
923 dbus_bool_t
924 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
925                                               const char    **parent_path,
926                                               char         ***child_entries)
927 {
928   dbus_bool_t result;
929
930   result = _dbus_object_tree_list_registered_unlocked (tree,
931                                                        parent_path,
932                                                        child_entries);
933   
934 #ifdef DBUS_BUILD_TESTS
935   if (tree->connection)
936 #endif
937     _dbus_connection_unlock (tree->connection);
938
939   return result;
940 }
941      
942 /** @} */
943
944 #ifdef DBUS_BUILD_TESTS
945 #include "dbus-test.h"
946 #include <stdio.h>
947
948 static char*
949 flatten_path (const char **path)
950 {
951   DBusString str;
952   int i;
953   char *s;
954
955   if (!_dbus_string_init (&str))
956     return NULL;
957
958   i = 0;
959   while (path[i])
960     {
961       if (!_dbus_string_append_byte (&str, '/'))
962         goto nomem;
963
964       if (!_dbus_string_append (&str, path[i]))
965         goto nomem;
966
967       ++i;
968     }
969
970   if (!_dbus_string_steal_data (&str, &s))
971     goto nomem;
972
973   _dbus_string_free (&str);
974
975   return s;
976
977  nomem:
978   _dbus_string_free (&str);
979   return NULL;
980 }
981
982
983 typedef enum 
984 {
985   STR_EQUAL,
986   STR_PREFIX,
987   STR_DIFFERENT
988 } StrComparison;
989
990 /* Returns TRUE if container is a parent of child
991  */
992 static StrComparison
993 path_contains (const char **container,
994                const char **child)
995 {
996   int i;
997
998   i = 0;
999   while (child[i] != NULL)
1000     {
1001       int v;
1002
1003       if (container[i] == NULL)
1004         return STR_PREFIX; /* container ran out, child continues;
1005                         * thus the container is a parent of the
1006                         * child.
1007                         */
1008
1009       _dbus_assert (container[i] != NULL);
1010       _dbus_assert (child[i] != NULL);
1011
1012       v = strcmp (container[i], child[i]);
1013
1014       if (v != 0)
1015         return STR_DIFFERENT; /* they overlap until here and then are different,
1016                            * not overlapping
1017                            */
1018
1019       ++i;
1020     }
1021
1022   /* Child ran out; if container also did, they are equal;
1023    * otherwise, the child is a parent of the container.
1024    */
1025   if (container[i] == NULL)
1026     return STR_EQUAL;
1027   else
1028     return STR_DIFFERENT;
1029 }
1030
1031 #if 0
1032 static void
1033 spew_subtree_recurse (DBusObjectSubtree *subtree,
1034                       int                indent)
1035 {
1036   int i;
1037
1038   i = 0;
1039   while (i < indent)
1040     {
1041       _dbus_verbose (" ");
1042       ++i;
1043     }
1044
1045   _dbus_verbose ("%s (%d children)\n",
1046                  subtree->name, subtree->n_subtrees);
1047
1048   i = 0;
1049   while (i < subtree->n_subtrees)
1050     {
1051       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1052
1053       ++i;
1054     }
1055 }
1056
1057 static void
1058 spew_tree (DBusObjectTree *tree)
1059 {
1060   spew_subtree_recurse (tree->root, 0);
1061 }
1062 #endif
1063
1064 /**
1065  * Callback data used in tests
1066  */
1067 typedef struct
1068 {
1069   const char **path; /**< Path */
1070   dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1071   dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1072   dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1073 } TreeTestData;
1074
1075
1076 static void
1077 test_unregister_function (DBusConnection  *connection,
1078                           void            *user_data)
1079 {
1080   TreeTestData *ttd = user_data;
1081
1082   ttd->handler_unregistered = TRUE;
1083 }
1084
1085 static DBusHandlerResult
1086 test_message_function (DBusConnection  *connection,
1087                        DBusMessage     *message,
1088                        void            *user_data)
1089 {
1090   TreeTestData *ttd = user_data;
1091
1092   ttd->message_handled = TRUE;
1093
1094   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1095 }
1096
1097 static dbus_bool_t
1098 do_register (DBusObjectTree *tree,
1099              const char    **path,
1100              dbus_bool_t     fallback,
1101              int             i,
1102              TreeTestData   *tree_test_data)
1103 {
1104   DBusObjectPathVTable vtable = { test_unregister_function,
1105                                   test_message_function, NULL };
1106
1107   tree_test_data[i].message_handled = FALSE;
1108   tree_test_data[i].handler_unregistered = FALSE;
1109   tree_test_data[i].handler_fallback = fallback;
1110   tree_test_data[i].path = path;
1111
1112   if (!_dbus_object_tree_register (tree, fallback, path,
1113                                    &vtable,
1114                                    &tree_test_data[i]))
1115     return FALSE;
1116
1117   return TRUE;
1118 }
1119
1120 static dbus_bool_t
1121 do_test_dispatch (DBusObjectTree *tree,
1122                   const char    **path,
1123                   int             i,
1124                   TreeTestData   *tree_test_data,
1125                   int             n_test_data)
1126 {
1127   DBusMessage *message;
1128   int j;
1129   DBusHandlerResult result;
1130   char *flat;
1131
1132   message = NULL;
1133   
1134   flat = flatten_path (path);
1135   if (flat == NULL)
1136     goto oom;
1137
1138   message = dbus_message_new_method_call (NULL,
1139                                           flat,
1140                                           "org.freedesktop.TestInterface",
1141                                           "Foo");
1142   dbus_free (flat);
1143   if (message == NULL)
1144     goto oom;
1145
1146   j = 0;
1147   while (j < n_test_data)
1148     {
1149       tree_test_data[j].message_handled = FALSE;
1150       ++j;
1151     }
1152
1153   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1154   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1155     goto oom;
1156
1157   _dbus_assert (tree_test_data[i].message_handled);
1158
1159   j = 0;
1160   while (j < n_test_data)
1161     {
1162       if (tree_test_data[j].message_handled)
1163         {
1164           if (tree_test_data[j].handler_fallback)
1165             _dbus_assert (path_contains (tree_test_data[j].path,
1166                                          path) != STR_DIFFERENT);
1167           else
1168             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1169         }
1170       else
1171         {
1172           if (tree_test_data[j].handler_fallback)
1173             _dbus_assert (path_contains (tree_test_data[j].path,
1174                                          path) == STR_DIFFERENT);
1175           else
1176             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1177         }
1178
1179       ++j;
1180     }
1181
1182   dbus_message_unref (message);
1183
1184   return TRUE;
1185
1186  oom:
1187   if (message)
1188     dbus_message_unref (message);
1189   return FALSE;
1190 }
1191
1192 static size_t
1193 string_array_length (char **array)
1194 {
1195   size_t i;
1196   for (i = 0; array[i]; i++) ;
1197   return i;
1198 }
1199
1200
1201 static dbus_bool_t
1202 object_tree_test_iteration (void *data)
1203 {
1204   const char *path1[] = { "foo", NULL };
1205   const char *path2[] = { "foo", "bar", NULL };
1206   const char *path3[] = { "foo", "bar", "baz", NULL };
1207   const char *path4[] = { "foo", "bar", "boo", NULL };
1208   const char *path5[] = { "blah", NULL };
1209   const char *path6[] = { "blah", "boof", NULL };
1210   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1211   const char *path8[] = { "childless", NULL };
1212   DBusObjectTree *tree;
1213   TreeTestData tree_test_data[8];
1214   int i;
1215   dbus_bool_t exact_match;
1216
1217   tree = NULL;
1218
1219   tree = _dbus_object_tree_new (NULL);
1220   if (tree == NULL)
1221     goto out;
1222
1223   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
1224     goto out;
1225
1226   _dbus_assert (find_subtree (tree, path1, NULL));
1227   _dbus_assert (!find_subtree (tree, path2, NULL));
1228   _dbus_assert (!find_subtree (tree, path3, NULL));
1229   _dbus_assert (!find_subtree (tree, path4, NULL));
1230   _dbus_assert (!find_subtree (tree, path5, NULL));
1231   _dbus_assert (!find_subtree (tree, path6, NULL));
1232   _dbus_assert (!find_subtree (tree, path7, NULL));
1233   _dbus_assert (!find_subtree (tree, path8, NULL));
1234
1235   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
1236   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1237   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1238   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1239   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1240   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1241   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1242   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1243
1244   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
1245     goto out;
1246
1247   _dbus_assert (find_subtree (tree, path1, NULL));
1248   _dbus_assert (find_subtree (tree, path2, NULL));
1249   _dbus_assert (!find_subtree (tree, path3, NULL));
1250   _dbus_assert (!find_subtree (tree, path4, NULL));
1251   _dbus_assert (!find_subtree (tree, path5, NULL));
1252   _dbus_assert (!find_subtree (tree, path6, NULL));
1253   _dbus_assert (!find_subtree (tree, path7, NULL));
1254   _dbus_assert (!find_subtree (tree, path8, NULL));
1255
1256   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
1257     goto out;
1258
1259   _dbus_assert (find_subtree (tree, path1, NULL));
1260   _dbus_assert (find_subtree (tree, path2, NULL));
1261   _dbus_assert (find_subtree (tree, path3, NULL));
1262   _dbus_assert (!find_subtree (tree, path4, NULL));
1263   _dbus_assert (!find_subtree (tree, path5, NULL));
1264   _dbus_assert (!find_subtree (tree, path6, NULL));
1265   _dbus_assert (!find_subtree (tree, path7, NULL));
1266   _dbus_assert (!find_subtree (tree, path8, NULL));
1267   
1268   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
1269     goto out;
1270
1271   _dbus_assert (find_subtree (tree, path1, NULL));
1272   _dbus_assert (find_subtree (tree, path2, NULL));
1273   _dbus_assert (find_subtree (tree, path3, NULL));  
1274   _dbus_assert (find_subtree (tree, path4, NULL));
1275   _dbus_assert (!find_subtree (tree, path5, NULL));
1276   _dbus_assert (!find_subtree (tree, path6, NULL));
1277   _dbus_assert (!find_subtree (tree, path7, NULL));
1278   _dbus_assert (!find_subtree (tree, path8, NULL));
1279   
1280   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
1281     goto out;
1282
1283   _dbus_assert (find_subtree (tree, path1, NULL));
1284   _dbus_assert (find_subtree (tree, path2, NULL));
1285   _dbus_assert (find_subtree (tree, path3, NULL));
1286   _dbus_assert (find_subtree (tree, path4, NULL));
1287   _dbus_assert (find_subtree (tree, path5, NULL));
1288   _dbus_assert (!find_subtree (tree, path6, NULL));
1289   _dbus_assert (!find_subtree (tree, path7, NULL));
1290   _dbus_assert (!find_subtree (tree, path8, NULL));
1291   
1292   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
1293   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
1294   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
1295   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
1296   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
1297   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1298   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1299   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1300
1301   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
1302     goto out;
1303
1304   _dbus_assert (find_subtree (tree, path1, NULL));
1305   _dbus_assert (find_subtree (tree, path2, NULL));
1306   _dbus_assert (find_subtree (tree, path3, NULL));
1307   _dbus_assert (find_subtree (tree, path4, NULL));
1308   _dbus_assert (find_subtree (tree, path5, NULL));
1309   _dbus_assert (find_subtree (tree, path6, NULL));
1310   _dbus_assert (!find_subtree (tree, path7, NULL));
1311   _dbus_assert (!find_subtree (tree, path8, NULL));
1312
1313   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
1314     goto out;
1315
1316   _dbus_assert (find_subtree (tree, path1, NULL));
1317   _dbus_assert (find_subtree (tree, path2, NULL));
1318   _dbus_assert (find_subtree (tree, path3, NULL));
1319   _dbus_assert (find_subtree (tree, path4, NULL));
1320   _dbus_assert (find_subtree (tree, path5, NULL));
1321   _dbus_assert (find_subtree (tree, path6, NULL));
1322   _dbus_assert (find_subtree (tree, path7, NULL));
1323   _dbus_assert (!find_subtree (tree, path8, NULL));
1324
1325   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
1326     goto out;
1327
1328   _dbus_assert (find_subtree (tree, path1, NULL));
1329   _dbus_assert (find_subtree (tree, path2, NULL));
1330   _dbus_assert (find_subtree (tree, path3, NULL));
1331   _dbus_assert (find_subtree (tree, path4, NULL));
1332   _dbus_assert (find_subtree (tree, path5, NULL));
1333   _dbus_assert (find_subtree (tree, path6, NULL));
1334   _dbus_assert (find_subtree (tree, path7, NULL));
1335   _dbus_assert (find_subtree (tree, path8, NULL));
1336   
1337   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1338   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1339   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1340   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1341   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1342   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1343   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1344   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1345   
1346   /* test the list_registered function */
1347
1348   {
1349     const char *root[] = { NULL };
1350     char **child_entries;
1351     int nb;
1352
1353     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1354     if (child_entries != NULL)
1355       {
1356         nb = string_array_length (child_entries);
1357         _dbus_assert (nb == 1);
1358         dbus_free_string_array (child_entries);
1359       }
1360
1361     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1362     if (child_entries != NULL)
1363       {
1364         nb = string_array_length (child_entries);
1365         _dbus_assert (nb == 2);
1366         dbus_free_string_array (child_entries);
1367       }
1368
1369     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1370     if (child_entries != NULL)
1371       {
1372         nb = string_array_length (child_entries);
1373         _dbus_assert (nb == 0);
1374         dbus_free_string_array (child_entries);
1375       }
1376
1377     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1378     if (child_entries != NULL)
1379       {
1380         nb = string_array_length (child_entries);
1381         _dbus_assert (nb == 3);
1382         dbus_free_string_array (child_entries);
1383       }
1384   }
1385
1386   /* Check that destroying tree calls unregister funcs */
1387   _dbus_object_tree_unref (tree);
1388
1389   i = 0;
1390   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1391     {
1392       _dbus_assert (tree_test_data[i].handler_unregistered);
1393       _dbus_assert (!tree_test_data[i].message_handled);
1394       ++i;
1395     }
1396
1397   /* Now start again and try the individual unregister function */
1398   tree = _dbus_object_tree_new (NULL);
1399   if (tree == NULL)
1400     goto out;
1401
1402   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
1403     goto out;
1404   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
1405     goto out;
1406   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
1407     goto out;
1408   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
1409     goto out;
1410   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
1411     goto out;
1412   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
1413     goto out;
1414   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
1415     goto out;
1416   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
1417     goto out;
1418   
1419   _dbus_object_tree_unregister_and_unlock (tree, path1);
1420
1421   _dbus_assert (!find_subtree (tree, path1, NULL));
1422   _dbus_assert (find_subtree (tree, path2, NULL));
1423   _dbus_assert (find_subtree (tree, path3, NULL));
1424   _dbus_assert (find_subtree (tree, path4, NULL));
1425   _dbus_assert (find_subtree (tree, path5, NULL));
1426   _dbus_assert (find_subtree (tree, path6, NULL));
1427   _dbus_assert (find_subtree (tree, path7, NULL));
1428   _dbus_assert (find_subtree (tree, path8, NULL));
1429
1430   _dbus_object_tree_unregister_and_unlock (tree, path2);
1431
1432   _dbus_assert (!find_subtree (tree, path1, NULL));
1433   _dbus_assert (!find_subtree (tree, path2, NULL));
1434   _dbus_assert (find_subtree (tree, path3, NULL));
1435   _dbus_assert (find_subtree (tree, path4, NULL));
1436   _dbus_assert (find_subtree (tree, path5, NULL));
1437   _dbus_assert (find_subtree (tree, path6, NULL));
1438   _dbus_assert (find_subtree (tree, path7, NULL));
1439   _dbus_assert (find_subtree (tree, path8, NULL));
1440   
1441   _dbus_object_tree_unregister_and_unlock (tree, path3);
1442
1443   _dbus_assert (!find_subtree (tree, path1, NULL));
1444   _dbus_assert (!find_subtree (tree, path2, NULL));
1445   _dbus_assert (!find_subtree (tree, path3, NULL));
1446   _dbus_assert (find_subtree (tree, path4, NULL));
1447   _dbus_assert (find_subtree (tree, path5, NULL));
1448   _dbus_assert (find_subtree (tree, path6, NULL));
1449   _dbus_assert (find_subtree (tree, path7, NULL));
1450   _dbus_assert (find_subtree (tree, path8, NULL));
1451   
1452   _dbus_object_tree_unregister_and_unlock (tree, path4);
1453
1454   _dbus_assert (!find_subtree (tree, path1, NULL));
1455   _dbus_assert (!find_subtree (tree, path2, NULL));
1456   _dbus_assert (!find_subtree (tree, path3, NULL));
1457   _dbus_assert (!find_subtree (tree, path4, NULL));
1458   _dbus_assert (find_subtree (tree, path5, NULL));
1459   _dbus_assert (find_subtree (tree, path6, NULL));
1460   _dbus_assert (find_subtree (tree, path7, NULL));
1461   _dbus_assert (find_subtree (tree, path8, NULL));
1462   
1463   _dbus_object_tree_unregister_and_unlock (tree, path5);
1464
1465   _dbus_assert (!find_subtree (tree, path1, NULL));
1466   _dbus_assert (!find_subtree (tree, path2, NULL));
1467   _dbus_assert (!find_subtree (tree, path3, NULL));
1468   _dbus_assert (!find_subtree (tree, path4, NULL));
1469   _dbus_assert (!find_subtree (tree, path5, NULL));
1470   _dbus_assert (find_subtree (tree, path6, NULL));
1471   _dbus_assert (find_subtree (tree, path7, NULL));
1472   _dbus_assert (find_subtree (tree, path8, NULL));
1473   
1474   _dbus_object_tree_unregister_and_unlock (tree, path6);
1475
1476   _dbus_assert (!find_subtree (tree, path1, NULL));
1477   _dbus_assert (!find_subtree (tree, path2, NULL));
1478   _dbus_assert (!find_subtree (tree, path3, NULL));
1479   _dbus_assert (!find_subtree (tree, path4, NULL));
1480   _dbus_assert (!find_subtree (tree, path5, NULL));
1481   _dbus_assert (!find_subtree (tree, path6, NULL));
1482   _dbus_assert (find_subtree (tree, path7, NULL));
1483   _dbus_assert (find_subtree (tree, path8, NULL));
1484
1485   _dbus_object_tree_unregister_and_unlock (tree, path7);
1486
1487   _dbus_assert (!find_subtree (tree, path1, NULL));
1488   _dbus_assert (!find_subtree (tree, path2, NULL));
1489   _dbus_assert (!find_subtree (tree, path3, NULL));
1490   _dbus_assert (!find_subtree (tree, path4, NULL));
1491   _dbus_assert (!find_subtree (tree, path5, NULL));
1492   _dbus_assert (!find_subtree (tree, path6, NULL));
1493   _dbus_assert (!find_subtree (tree, path7, NULL));
1494   _dbus_assert (find_subtree (tree, path8, NULL));
1495
1496   _dbus_object_tree_unregister_and_unlock (tree, path8);
1497
1498   _dbus_assert (!find_subtree (tree, path1, NULL));
1499   _dbus_assert (!find_subtree (tree, path2, NULL));
1500   _dbus_assert (!find_subtree (tree, path3, NULL));
1501   _dbus_assert (!find_subtree (tree, path4, NULL));
1502   _dbus_assert (!find_subtree (tree, path5, NULL));
1503   _dbus_assert (!find_subtree (tree, path6, NULL));
1504   _dbus_assert (!find_subtree (tree, path7, NULL));
1505   _dbus_assert (!find_subtree (tree, path8, NULL));
1506   
1507   i = 0;
1508   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1509     {
1510       _dbus_assert (tree_test_data[i].handler_unregistered);
1511       _dbus_assert (!tree_test_data[i].message_handled);
1512       ++i;
1513     }
1514
1515   /* Register it all again, and test dispatch */
1516
1517   if (!do_register (tree, path1, FALSE, 0, tree_test_data))
1518     goto out;
1519   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
1520     goto out;
1521   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
1522     goto out;
1523   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
1524     goto out;
1525   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
1526     goto out;
1527   if (!do_register (tree, path6, FALSE, 5, tree_test_data))
1528     goto out;
1529   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
1530     goto out;
1531   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
1532     goto out;
1533
1534 #if 0
1535   spew_tree (tree);
1536 #endif
1537   
1538   if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1539     goto out;
1540   if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1541     goto out;
1542   if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1543     goto out;
1544   if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1545     goto out;
1546   if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1547     goto out;
1548   if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1549     goto out;
1550   if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1551     goto out;
1552   if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1553     goto out;
1554   
1555  out:
1556   if (tree)
1557     {
1558       /* test ref */
1559       _dbus_object_tree_ref (tree);
1560       _dbus_object_tree_unref (tree);
1561       _dbus_object_tree_unref (tree);
1562     }
1563
1564   return TRUE;
1565 }
1566
1567 /**
1568  * @ingroup DBusObjectTree
1569  * Unit test for DBusObjectTree
1570  * @returns #TRUE on success.
1571  */
1572 dbus_bool_t
1573 _dbus_object_tree_test (void)
1574 {
1575   _dbus_test_oom_handling ("object tree",
1576                            object_tree_test_iteration,
1577                            NULL);
1578
1579   return TRUE;
1580 }
1581
1582 #endif /* DBUS_BUILD_TESTS */