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