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