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