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