xref: /openbmc/phosphor-objmgr/src/main.cpp (revision 11401e2e)
1 #include "associations.hpp"
2 #include "processing.hpp"
3 #include "src/argument.hpp"
4 #include "types.hpp"
5 
6 #include <tinyxml2.h>
7 
8 #include <atomic>
9 #include <boost/algorithm/string/case_conv.hpp>
10 #include <boost/algorithm/string/predicate.hpp>
11 #include <boost/container/flat_map.hpp>
12 #include <chrono>
13 #include <iomanip>
14 #include <iostream>
15 #include <sdbusplus/asio/connection.hpp>
16 #include <sdbusplus/asio/object_server.hpp>
17 
18 constexpr const char* OBJECT_MAPPER_DBUS_NAME =
19     "xyz.openbmc_project.ObjectMapper";
20 
21 AssociationMaps associationMaps;
22 
23 static WhiteBlackList service_whitelist;
24 static WhiteBlackList service_blacklist;
25 
26 /** Exception thrown when a path is not found in the object list. */
27 struct NotFoundException final : public sdbusplus::exception_t
28 {
29     const char* name() const noexcept override
30     {
31         return "org.freedesktop.DBus.Error.FileNotFound";
32     };
33     const char* description() const noexcept override
34     {
35         return "path or object not found";
36     };
37     const char* what() const noexcept override
38     {
39         return "org.freedesktop.DBus.Error.FileNotFound: "
40                "The requested object was not found";
41     };
42 };
43 
44 void update_owners(sdbusplus::asio::connection* conn,
45                    boost::container::flat_map<std::string, std::string>& owners,
46                    const std::string& new_object)
47 {
48     if (boost::starts_with(new_object, ":"))
49     {
50         return;
51     }
52     conn->async_method_call(
53         [&, new_object](const boost::system::error_code ec,
54                         const std::string& nameOwner) {
55             if (ec)
56             {
57                 std::cerr << "Error getting owner of " << new_object << " : "
58                           << ec << "\n";
59                 return;
60             }
61             owners[nameOwner] = new_object;
62         },
63         "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
64         new_object);
65 }
66 
67 void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
68                                         const std::string& process_name)
69 {
70     // TODO(ed) This signal doesn't get exposed properly in the
71     // introspect right now.  Find out how to register signals in
72     // sdbusplus
73     sdbusplus::message::message m = system_bus->new_signal(
74         "/xyz/openbmc_project/object_mapper",
75         "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
76     m.append(process_name);
77     m.signal_send();
78 }
79 
80 struct InProgressIntrospect
81 {
82     InProgressIntrospect(
83         sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
84         const std::string& process_name, AssociationMaps& am
85 #ifdef DEBUG
86         ,
87         std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
88             global_start_time
89 #endif
90         ) :
91         system_bus(system_bus),
92         io(io), process_name(process_name), assocMaps(am)
93 #ifdef DEBUG
94         ,
95         global_start_time(global_start_time),
96         process_start_time(std::chrono::steady_clock::now())
97 #endif
98     {
99     }
100     ~InProgressIntrospect()
101     {
102         send_introspection_complete_signal(system_bus, process_name);
103 
104 #ifdef DEBUG
105         std::chrono::duration<float> diff =
106             std::chrono::steady_clock::now() - process_start_time;
107         std::cout << std::setw(50) << process_name << " scan took "
108                   << diff.count() << " seconds\n";
109 
110         // If we're the last outstanding caller globally, calculate the
111         // time it took
112         if (global_start_time != nullptr && global_start_time.use_count() == 1)
113         {
114             diff = std::chrono::steady_clock::now() - *global_start_time;
115             std::cout << "Total scan took " << diff.count()
116                       << " seconds to complete\n";
117         }
118 #endif
119     }
120     sdbusplus::asio::connection* system_bus;
121     boost::asio::io_service& io;
122     std::string process_name;
123     AssociationMaps& assocMaps;
124 #ifdef DEBUG
125     std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
126         global_start_time;
127     std::chrono::time_point<std::chrono::steady_clock> process_start_time;
128 #endif
129 };
130 
131 void do_associations(sdbusplus::asio::connection* system_bus,
132                      interface_map_type& interfaceMap,
133                      sdbusplus::asio::object_server& objectServer,
134                      const std::string& processName, const std::string& path,
135                      const std::string& assocDefIface)
136 {
137     system_bus->async_method_call(
138         [&objectServer, path, processName, &interfaceMap](
139             const boost::system::error_code ec,
140             const sdbusplus::message::variant<std::vector<Association>>&
141                 variantAssociations) {
142             if (ec)
143             {
144                 std::cerr << "Error getting associations from " << path << "\n";
145             }
146             std::vector<Association> associations =
147                 sdbusplus::message::variant_ns::get<std::vector<Association>>(
148                     variantAssociations);
149             associationChanged(objectServer, associations, path, processName,
150                                interfaceMap, associationMaps);
151         },
152         processName, path, "org.freedesktop.DBus.Properties", "Get",
153         assocDefIface, getAssocDefPropName(assocDefIface));
154 }
155 
156 void do_introspect(sdbusplus::asio::connection* system_bus,
157                    std::shared_ptr<InProgressIntrospect> transaction,
158                    interface_map_type& interface_map,
159                    sdbusplus::asio::object_server& objectServer,
160                    std::string path)
161 {
162     system_bus->async_method_call(
163         [&interface_map, &objectServer, transaction, path,
164          system_bus](const boost::system::error_code ec,
165                      const std::string& introspect_xml) {
166             if (ec)
167             {
168                 std::cerr << "Introspect call failed with error: " << ec << ", "
169                           << ec.message()
170                           << " on process: " << transaction->process_name
171                           << " path: " << path << "\n";
172                 return;
173             }
174 
175             tinyxml2::XMLDocument doc;
176 
177             tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
178             if (e != tinyxml2::XMLError::XML_SUCCESS)
179             {
180                 std::cerr << "XML parsing failed\n";
181                 return;
182             }
183 
184             tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
185             if (pRoot == nullptr)
186             {
187                 std::cerr << "XML document did not contain any data\n";
188                 return;
189             }
190             auto& thisPathMap = interface_map[path];
191             tinyxml2::XMLElement* pElement =
192                 pRoot->FirstChildElement("interface");
193             while (pElement != nullptr)
194             {
195                 const char* iface_name = pElement->Attribute("name");
196                 if (iface_name == nullptr)
197                 {
198                     continue;
199                 }
200 
201                 thisPathMap[transaction->process_name].emplace(iface_name);
202 
203                 if (isAssocDefIface(iface_name))
204                 {
205                     do_associations(system_bus, interface_map, objectServer,
206                                     transaction->process_name, path,
207                                     iface_name);
208                 }
209 
210                 pElement = pElement->NextSiblingElement("interface");
211             }
212 
213             // Check if this new path has a pending association that can
214             // now be completed.
215             checkIfPendingAssociation(path, interface_map,
216                                       transaction->assocMaps, objectServer);
217 
218             pElement = pRoot->FirstChildElement("node");
219             while (pElement != nullptr)
220             {
221                 const char* child_path = pElement->Attribute("name");
222                 if (child_path != nullptr)
223                 {
224                     std::string parent_path(path);
225                     if (parent_path == "/")
226                     {
227                         parent_path.clear();
228                     }
229 
230                     do_introspect(system_bus, transaction, interface_map,
231                                   objectServer, parent_path + "/" + child_path);
232                 }
233                 pElement = pElement->NextSiblingElement("node");
234             }
235         },
236         transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
237         "Introspect");
238 }
239 
240 void start_new_introspect(
241     sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
242     interface_map_type& interface_map, const std::string& process_name,
243     AssociationMaps& assocMaps,
244 #ifdef DEBUG
245     std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
246         global_start_time,
247 #endif
248     sdbusplus::asio::object_server& objectServer)
249 {
250     if (needToIntrospect(process_name, service_whitelist, service_blacklist))
251     {
252         std::shared_ptr<InProgressIntrospect> transaction =
253             std::make_shared<InProgressIntrospect>(system_bus, io, process_name,
254                                                    assocMaps
255 #ifdef DEBUG
256                                                    ,
257                                                    global_start_time
258 #endif
259             );
260 
261         do_introspect(system_bus, transaction, interface_map, objectServer,
262                       "/");
263     }
264 }
265 
266 // TODO(ed) replace with std::set_intersection once c++17 is available
267 template <class InputIt1, class InputIt2>
268 bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
269 {
270     while (first1 != last1 && first2 != last2)
271     {
272         if (*first1 < *first2)
273         {
274             ++first1;
275             continue;
276         }
277         if (*first2 < *first1)
278         {
279             ++first2;
280             continue;
281         }
282         return true;
283     }
284     return false;
285 }
286 
287 void doListNames(
288     boost::asio::io_service& io, interface_map_type& interface_map,
289     sdbusplus::asio::connection* system_bus,
290     boost::container::flat_map<std::string, std::string>& name_owners,
291     AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer)
292 {
293     system_bus->async_method_call(
294         [&io, &interface_map, &name_owners, &objectServer, system_bus,
295          &assocMaps](const boost::system::error_code ec,
296                      std::vector<std::string> process_names) {
297             if (ec)
298             {
299                 std::cerr << "Error getting names: " << ec << "\n";
300                 std::exit(EXIT_FAILURE);
301                 return;
302             }
303             // Try to make startup consistent
304             std::sort(process_names.begin(), process_names.end());
305 #ifdef DEBUG
306             std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
307                 global_start_time = std::make_shared<
308                     std::chrono::time_point<std::chrono::steady_clock>>(
309                     std::chrono::steady_clock::now());
310 #endif
311             for (const std::string& process_name : process_names)
312             {
313                 if (needToIntrospect(process_name, service_whitelist,
314                                      service_blacklist))
315                 {
316                     start_new_introspect(system_bus, io, interface_map,
317                                          process_name, assocMaps,
318 #ifdef DEBUG
319                                          global_start_time,
320 #endif
321                                          objectServer);
322                     update_owners(system_bus, name_owners, process_name);
323                 }
324             }
325         },
326         "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
327         "ListNames");
328 }
329 
330 void splitArgs(const std::string& stringArgs,
331                boost::container::flat_set<std::string>& listArgs)
332 {
333     std::istringstream args;
334     std::string arg;
335 
336     args.str(stringArgs);
337 
338     while (!args.eof())
339     {
340         args >> arg;
341         if (!arg.empty())
342         {
343             listArgs.insert(arg);
344         }
345     }
346 }
347 
348 void addObjectMapResult(
349     std::vector<interface_map_type::value_type>& objectMap,
350     const std::string& objectPath,
351     const std::pair<std::string, boost::container::flat_set<std::string>>&
352         interfaceMap)
353 {
354     // Adds an object path/service name/interface list entry to
355     // the results of GetSubTree and GetAncestors.
356     // If an entry for the object path already exists, just add the
357     // service name and interfaces to that entry, otherwise create
358     // a new entry.
359     auto entry = std::find_if(
360         objectMap.begin(), objectMap.end(),
361         [&objectPath](const auto& i) { return objectPath == i.first; });
362 
363     if (entry != objectMap.end())
364     {
365         entry->second.emplace(interfaceMap);
366     }
367     else
368     {
369         interface_map_type::value_type object;
370         object.first = objectPath;
371         object.second.emplace(interfaceMap);
372         objectMap.push_back(object);
373     }
374 }
375 
376 // Remove parents of the passed in path that:
377 // 1) Only have the 3 default interfaces on them
378 //    - Means D-Bus created these, not application code,
379 //      with the Properties, Introspectable, and Peer ifaces
380 // 2) Have no other child for this owner
381 void removeUnneededParents(const std::string& objectPath,
382                            const std::string& owner,
383                            interface_map_type& interface_map)
384 {
385     auto parent = objectPath;
386 
387     while (true)
388     {
389         auto pos = parent.find_last_of('/');
390         if ((pos == std::string::npos) || (pos == 0))
391         {
392             break;
393         }
394         parent = parent.substr(0, pos);
395 
396         auto parent_it = interface_map.find(parent);
397         if (parent_it == interface_map.end())
398         {
399             break;
400         }
401 
402         auto ifaces_it = parent_it->second.find(owner);
403         if (ifaces_it == parent_it->second.end())
404         {
405             break;
406         }
407 
408         if (ifaces_it->second.size() != 3)
409         {
410             break;
411         }
412 
413         auto child_path = parent + '/';
414 
415         // Remove this parent if there isn't a remaining child on this owner
416         auto child = std::find_if(
417             interface_map.begin(), interface_map.end(),
418             [&owner, &child_path](const auto& entry) {
419                 return boost::starts_with(entry.first, child_path) &&
420                        (entry.second.find(owner) != entry.second.end());
421             });
422 
423         if (child == interface_map.end())
424         {
425             parent_it->second.erase(ifaces_it);
426             if (parent_it->second.empty())
427             {
428                 interface_map.erase(parent_it);
429             }
430         }
431         else
432         {
433             break;
434         }
435     }
436 }
437 
438 int main(int argc, char** argv)
439 {
440     auto options = ArgumentParser(argc, argv);
441     boost::asio::io_service io;
442     std::shared_ptr<sdbusplus::asio::connection> system_bus =
443         std::make_shared<sdbusplus::asio::connection>(io);
444 
445     splitArgs(options["service-namespaces"], service_whitelist);
446     splitArgs(options["service-blacklists"], service_blacklist);
447 
448     // TODO(Ed) Remove this once all service files are updated to not use this.
449     // For now, simply squash the input, and ignore it.
450     boost::container::flat_set<std::string> iface_whitelist;
451     splitArgs(options["interface-namespaces"], iface_whitelist);
452 
453     system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
454     sdbusplus::asio::object_server server(system_bus);
455 
456     // Construct a signal set registered for process termination.
457     boost::asio::signal_set signals(io, SIGINT, SIGTERM);
458     signals.async_wait([&io](const boost::system::error_code& error,
459                              int signal_number) { io.stop(); });
460 
461     interface_map_type interface_map;
462     boost::container::flat_map<std::string, std::string> name_owners;
463 
464     std::function<void(sdbusplus::message::message & message)>
465         nameChangeHandler = [&interface_map, &io, &name_owners, &server,
466                              system_bus](sdbusplus::message::message& message) {
467             std::string name;      // well-known
468             std::string old_owner; // unique-name
469             std::string new_owner; // unique-name
470 
471             message.read(name, old_owner, new_owner);
472 
473             if (!old_owner.empty())
474             {
475                 processNameChangeDelete(name_owners, name, old_owner,
476                                         interface_map, associationMaps, server);
477             }
478 
479             if (!new_owner.empty())
480             {
481 #ifdef DEBUG
482                 auto transaction = std::make_shared<
483                     std::chrono::time_point<std::chrono::steady_clock>>(
484                     std::chrono::steady_clock::now());
485 #endif
486                 // New daemon added
487                 if (needToIntrospect(name, service_whitelist,
488                                      service_blacklist))
489                 {
490                     name_owners[new_owner] = name;
491                     start_new_introspect(system_bus.get(), io, interface_map,
492                                          name, associationMaps,
493 #ifdef DEBUG
494                                          transaction,
495 #endif
496                                          server);
497                 }
498             }
499         };
500 
501     sdbusplus::bus::match::match nameOwnerChanged(
502         static_cast<sdbusplus::bus::bus&>(*system_bus),
503         sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
504 
505     std::function<void(sdbusplus::message::message & message)>
506         interfacesAddedHandler = [&interface_map, &name_owners, &server](
507                                      sdbusplus::message::message& message) {
508             sdbusplus::message::object_path obj_path;
509             InterfacesAdded interfaces_added;
510             message.read(obj_path, interfaces_added);
511             std::string well_known;
512             if (!getWellKnown(name_owners, message.get_sender(), well_known))
513             {
514                 return; // only introspect well-known
515             }
516             if (needToIntrospect(well_known, service_whitelist,
517                                  service_blacklist))
518             {
519                 processInterfaceAdded(interface_map, obj_path, interfaces_added,
520                                       well_known, associationMaps, server);
521             }
522         };
523 
524     sdbusplus::bus::match::match interfacesAdded(
525         static_cast<sdbusplus::bus::bus&>(*system_bus),
526         sdbusplus::bus::match::rules::interfacesAdded(),
527         interfacesAddedHandler);
528 
529     std::function<void(sdbusplus::message::message & message)>
530         interfacesRemovedHandler = [&interface_map, &name_owners, &server](
531                                        sdbusplus::message::message& message) {
532             sdbusplus::message::object_path obj_path;
533             std::vector<std::string> interfaces_removed;
534             message.read(obj_path, interfaces_removed);
535             auto connection_map = interface_map.find(obj_path.str);
536             if (connection_map == interface_map.end())
537             {
538                 return;
539             }
540 
541             std::string sender;
542             if (!getWellKnown(name_owners, message.get_sender(), sender))
543             {
544                 return;
545             }
546             for (const std::string& interface : interfaces_removed)
547             {
548                 auto interface_set = connection_map->second.find(sender);
549                 if (interface_set == connection_map->second.end())
550                 {
551                     continue;
552                 }
553 
554                 if (isAssocDefIface(interface))
555                 {
556                     removeAssociation(obj_path.str, sender, server,
557                                       associationMaps);
558                 }
559 
560                 interface_set->second.erase(interface);
561                 // If this was the last interface on this connection,
562                 // erase the connection
563                 if (interface_set->second.empty())
564                 {
565                     connection_map->second.erase(interface_set);
566                 }
567             }
568             // If this was the last connection on this object path,
569             // erase the object path
570             if (connection_map->second.empty())
571             {
572                 interface_map.erase(connection_map);
573             }
574 
575             removeUnneededParents(obj_path.str, sender, interface_map);
576         };
577 
578     sdbusplus::bus::match::match interfacesRemoved(
579         static_cast<sdbusplus::bus::bus&>(*system_bus),
580         sdbusplus::bus::match::rules::interfacesRemoved(),
581         interfacesRemovedHandler);
582 
583     std::function<void(sdbusplus::message::message & message)>
584         associationChangedHandler = [&server, &name_owners, &interface_map](
585                                         sdbusplus::message::message& message) {
586             std::string objectName;
587             boost::container::flat_map<
588                 std::string,
589                 sdbusplus::message::variant<std::vector<Association>>>
590                 values;
591             message.read(objectName, values);
592 
593             auto prop =
594                 std::find_if(values.begin(), values.end(), [](const auto& v) {
595                     using namespace boost::algorithm;
596                     return to_lower_copy(v.first) == "associations";
597                 });
598 
599             if (prop != values.end())
600             {
601                 std::vector<Association> associations =
602                     sdbusplus::message::variant_ns::get<
603                         std::vector<Association>>(prop->second);
604 
605                 std::string well_known;
606                 if (!getWellKnown(name_owners, message.get_sender(),
607                                   well_known))
608                 {
609                     return;
610                 }
611                 associationChanged(server, associations, message.get_path(),
612                                    well_known, interface_map, associationMaps);
613             }
614         };
615     sdbusplus::bus::match::match assocChangedMatch(
616         static_cast<sdbusplus::bus::bus&>(*system_bus),
617         sdbusplus::bus::match::rules::interface(
618             "org.freedesktop.DBus.Properties") +
619             sdbusplus::bus::match::rules::member("PropertiesChanged") +
620             sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
621         associationChangedHandler);
622 
623     sdbusplus::bus::match::match orgOpenbmcAssocChangedMatch(
624         static_cast<sdbusplus::bus::bus&>(*system_bus),
625         sdbusplus::bus::match::rules::interface(
626             "org.freedesktop.DBus.Properties") +
627             sdbusplus::bus::match::rules::member("PropertiesChanged") +
628             sdbusplus::bus::match::rules::argN(0, orgOpenBMCAssocDefsInterface),
629         associationChangedHandler);
630 
631     std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
632         server.add_interface("/xyz/openbmc_project/object_mapper",
633                              "xyz.openbmc_project.ObjectMapper");
634 
635     iface->register_method(
636         "GetAncestors", [&interface_map](std::string& req_path,
637                                          std::vector<std::string>& interfaces) {
638             // Interfaces need to be sorted for intersect to function
639             std::sort(interfaces.begin(), interfaces.end());
640 
641             if (boost::ends_with(req_path, "/"))
642             {
643                 req_path.pop_back();
644             }
645             if (req_path.size() &&
646                 interface_map.find(req_path) == interface_map.end())
647             {
648                 throw NotFoundException();
649             }
650 
651             std::vector<interface_map_type::value_type> ret;
652             for (auto& object_path : interface_map)
653             {
654                 auto& this_path = object_path.first;
655                 if (boost::starts_with(req_path, this_path) &&
656                     (req_path != this_path))
657                 {
658                     if (interfaces.empty())
659                     {
660                         ret.emplace_back(object_path);
661                     }
662                     else
663                     {
664                         for (auto& interface_map : object_path.second)
665                         {
666 
667                             if (intersect(interfaces.begin(), interfaces.end(),
668                                           interface_map.second.begin(),
669                                           interface_map.second.end()))
670                             {
671                                 addObjectMapResult(ret, this_path,
672                                                    interface_map);
673                             }
674                         }
675                     }
676                 }
677             }
678 
679             return ret;
680         });
681 
682     iface->register_method(
683         "GetObject", [&interface_map](const std::string& path,
684                                       std::vector<std::string>& interfaces) {
685             boost::container::flat_map<std::string,
686                                        boost::container::flat_set<std::string>>
687                 results;
688 
689             // Interfaces need to be sorted for intersect to function
690             std::sort(interfaces.begin(), interfaces.end());
691             auto path_ref = interface_map.find(path);
692             if (path_ref == interface_map.end())
693             {
694                 throw NotFoundException();
695             }
696             if (interfaces.empty())
697             {
698                 return path_ref->second;
699             }
700             for (auto& interface_map : path_ref->second)
701             {
702                 if (intersect(interfaces.begin(), interfaces.end(),
703                               interface_map.second.begin(),
704                               interface_map.second.end()))
705                 {
706                     results.emplace(interface_map.first, interface_map.second);
707                 }
708             }
709 
710             if (results.empty())
711             {
712                 throw NotFoundException();
713             }
714 
715             return results;
716         });
717 
718     iface->register_method(
719         "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
720                                        std::vector<std::string>& interfaces) {
721             if (depth <= 0)
722             {
723                 depth = std::numeric_limits<int32_t>::max();
724             }
725             // Interfaces need to be sorted for intersect to function
726             std::sort(interfaces.begin(), interfaces.end());
727             std::vector<interface_map_type::value_type> ret;
728 
729             if (boost::ends_with(req_path, "/"))
730             {
731                 req_path.pop_back();
732             }
733             if (req_path.size() &&
734                 interface_map.find(req_path) == interface_map.end())
735             {
736                 throw NotFoundException();
737             }
738 
739             for (auto& object_path : interface_map)
740             {
741                 auto& this_path = object_path.first;
742 
743                 if (this_path == req_path)
744                 {
745                     continue;
746                 }
747 
748                 if (boost::starts_with(this_path, req_path))
749                 {
750                     // count the number of slashes past the search term
751                     int32_t this_depth =
752                         std::count(this_path.begin() + req_path.size(),
753                                    this_path.end(), '/');
754                     if (this_depth <= depth)
755                     {
756                         for (auto& interface_map : object_path.second)
757                         {
758                             if (intersect(interfaces.begin(), interfaces.end(),
759                                           interface_map.second.begin(),
760                                           interface_map.second.end()) ||
761                                 interfaces.empty())
762                             {
763                                 addObjectMapResult(ret, this_path,
764                                                    interface_map);
765                             }
766                         }
767                     }
768                 }
769             }
770 
771             return ret;
772         });
773 
774     iface->register_method(
775         "GetSubTreePaths",
776         [&interface_map](std::string& req_path, int32_t depth,
777                          std::vector<std::string>& interfaces) {
778             if (depth <= 0)
779             {
780                 depth = std::numeric_limits<int32_t>::max();
781             }
782             // Interfaces need to be sorted for intersect to function
783             std::sort(interfaces.begin(), interfaces.end());
784             std::vector<std::string> ret;
785 
786             if (boost::ends_with(req_path, "/"))
787             {
788                 req_path.pop_back();
789             }
790             if (req_path.size() &&
791                 interface_map.find(req_path) == interface_map.end())
792             {
793                 throw NotFoundException();
794             }
795 
796             for (auto& object_path : interface_map)
797             {
798                 auto& this_path = object_path.first;
799 
800                 if (this_path == req_path)
801                 {
802                     continue;
803                 }
804 
805                 if (boost::starts_with(this_path, req_path))
806                 {
807                     // count the number of slashes past the search term
808                     int this_depth =
809                         std::count(this_path.begin() + req_path.size(),
810                                    this_path.end(), '/');
811                     if (this_depth <= depth)
812                     {
813                         bool add = interfaces.empty();
814                         for (auto& interface_map : object_path.second)
815                         {
816                             if (intersect(interfaces.begin(), interfaces.end(),
817                                           interface_map.second.begin(),
818                                           interface_map.second.end()))
819                             {
820                                 add = true;
821                                 break;
822                             }
823                         }
824                         if (add)
825                         {
826                             // TODO(ed) this is a copy
827                             ret.emplace_back(this_path);
828                         }
829                     }
830                 }
831             }
832 
833             return ret;
834         });
835 
836     iface->initialize();
837 
838     io.post([&]() {
839         doListNames(io, interface_map, system_bus.get(), name_owners,
840                     associationMaps, server);
841     });
842 
843     io.run();
844 }
845