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