xref: /openbmc/phosphor-objmgr/src/main.cpp (revision b15df6b2)
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 #include <xyz/openbmc_project/Common/error.hpp>
19 
20 AssociationMaps associationMaps;
21 
22 static WhiteBlackList service_whitelist;
23 static WhiteBlackList service_blacklist;
24 
25 void update_owners(sdbusplus::asio::connection* conn,
26                    boost::container::flat_map<std::string, std::string>& owners,
27                    const std::string& new_object)
28 {
29     if (boost::starts_with(new_object, ":"))
30     {
31         return;
32     }
33     conn->async_method_call(
34         [&, new_object](const boost::system::error_code ec,
35                         const std::string& nameOwner) {
36             if (ec)
37             {
38                 std::cerr << "Error getting owner of " << new_object << " : "
39                           << ec << "\n";
40                 return;
41             }
42             owners[nameOwner] = new_object;
43         },
44         "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
45         new_object);
46 }
47 
48 void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
49                                         const std::string& process_name)
50 {
51     // TODO(ed) This signal doesn't get exposed properly in the
52     // introspect right now.  Find out how to register signals in
53     // sdbusplus
54     sdbusplus::message::message m = system_bus->new_signal(
55         "/xyz/openbmc_project/object_mapper",
56         "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
57     m.append(process_name);
58     m.signal_send();
59 }
60 
61 struct InProgressIntrospect
62 {
63     InProgressIntrospect(
64         sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
65         const std::string& process_name, AssociationMaps& am
66 #ifdef DEBUG
67         ,
68         std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
69             global_start_time
70 #endif
71         ) :
72         system_bus(system_bus),
73         io(io), process_name(process_name), assocMaps(am)
74 #ifdef DEBUG
75         ,
76         global_start_time(global_start_time),
77         process_start_time(std::chrono::steady_clock::now())
78 #endif
79     {
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 
465                     if (intersect(interfaces.begin(), interfaces.end(),
466                                   interface_map.second.begin(),
467                                   interface_map.second.end()))
468                     {
469                         addObjectMapResult(ret, this_path, interface_map);
470                     }
471                 }
472             }
473         }
474     }
475 
476     return ret;
477 }
478 
479 boost::container::flat_map<std::string, boost::container::flat_set<std::string>>
480     getObject(const interface_map_type& interface_map, const std::string& path,
481               std::vector<std::string>& interfaces)
482 {
483     boost::container::flat_map<std::string,
484                                boost::container::flat_set<std::string>>
485         results;
486 
487     // Interfaces need to be sorted for intersect to function
488     std::sort(interfaces.begin(), interfaces.end());
489     auto path_ref = interface_map.find(path);
490     if (path_ref == interface_map.end())
491     {
492         throw sdbusplus::xyz::openbmc_project::Common::Error::
493             ResourceNotFound();
494     }
495     if (interfaces.empty())
496     {
497         return path_ref->second;
498     }
499     for (auto& interface_map : path_ref->second)
500     {
501         if (intersect(interfaces.begin(), interfaces.end(),
502                       interface_map.second.begin(), interface_map.second.end()))
503         {
504             results.emplace(interface_map.first, interface_map.second);
505         }
506     }
507 
508     if (results.empty())
509     {
510         throw sdbusplus::xyz::openbmc_project::Common::Error::
511             ResourceNotFound();
512     }
513 
514     return results;
515 }
516 
517 std::vector<interface_map_type::value_type>
518     getSubTree(const interface_map_type& interface_map, std::string req_path,
519                int32_t depth, std::vector<std::string>& interfaces)
520 {
521     if (depth <= 0)
522     {
523         depth = std::numeric_limits<int32_t>::max();
524     }
525     // Interfaces need to be sorted for intersect to function
526     std::sort(interfaces.begin(), interfaces.end());
527     std::vector<interface_map_type::value_type> ret;
528 
529     if (boost::ends_with(req_path, "/"))
530     {
531         req_path.pop_back();
532     }
533     if (req_path.size() && interface_map.find(req_path) == interface_map.end())
534     {
535         throw sdbusplus::xyz::openbmc_project::Common::Error::
536             ResourceNotFound();
537     }
538 
539     for (auto& object_path : interface_map)
540     {
541         auto& this_path = object_path.first;
542 
543         if (this_path == req_path)
544         {
545             continue;
546         }
547 
548         if (boost::starts_with(this_path, req_path))
549         {
550             // count the number of slashes past the search term
551             int32_t this_depth = std::count(this_path.begin() + req_path.size(),
552                                             this_path.end(), '/');
553             if (this_depth <= depth)
554             {
555                 for (auto& interface_map : object_path.second)
556                 {
557                     if (intersect(interfaces.begin(), interfaces.end(),
558                                   interface_map.second.begin(),
559                                   interface_map.second.end()) ||
560                         interfaces.empty())
561                     {
562                         addObjectMapResult(ret, this_path, interface_map);
563                     }
564                 }
565             }
566         }
567     }
568 
569     return ret;
570 }
571 
572 std::vector<std::string>
573     getSubTreePaths(const interface_map_type& interface_map,
574                     std::string req_path, int32_t depth,
575                     std::vector<std::string>& interfaces)
576 {
577     if (depth <= 0)
578     {
579         depth = std::numeric_limits<int32_t>::max();
580     }
581     // Interfaces need to be sorted for intersect to function
582     std::sort(interfaces.begin(), interfaces.end());
583     std::vector<std::string> ret;
584 
585     if (boost::ends_with(req_path, "/"))
586     {
587         req_path.pop_back();
588     }
589     if (req_path.size() && interface_map.find(req_path) == interface_map.end())
590     {
591         throw sdbusplus::xyz::openbmc_project::Common::Error::
592             ResourceNotFound();
593     }
594 
595     for (auto& object_path : interface_map)
596     {
597         auto& this_path = object_path.first;
598 
599         if (this_path == req_path)
600         {
601             continue;
602         }
603 
604         if (boost::starts_with(this_path, req_path))
605         {
606             // count the number of slashes past the search term
607             int this_depth = std::count(this_path.begin() + req_path.size(),
608                                         this_path.end(), '/');
609             if (this_depth <= depth)
610             {
611                 bool add = interfaces.empty();
612                 for (auto& interface_map : object_path.second)
613                 {
614                     if (intersect(interfaces.begin(), interfaces.end(),
615                                   interface_map.second.begin(),
616                                   interface_map.second.end()))
617                     {
618                         add = true;
619                         break;
620                     }
621                 }
622                 if (add)
623                 {
624                     // TODO(ed) this is a copy
625                     ret.emplace_back(this_path);
626                 }
627             }
628         }
629     }
630 
631     return ret;
632 }
633 
634 int main(int argc, char** argv)
635 {
636     auto options = ArgumentParser(argc, argv);
637     boost::asio::io_context io;
638     std::shared_ptr<sdbusplus::asio::connection> system_bus =
639         std::make_shared<sdbusplus::asio::connection>(io);
640 
641     splitArgs(options["service-namespaces"], service_whitelist);
642     splitArgs(options["service-blacklists"], service_blacklist);
643 
644     // TODO(Ed) Remove this once all service files are updated to not use this.
645     // For now, simply squash the input, and ignore it.
646     boost::container::flat_set<std::string> iface_whitelist;
647     splitArgs(options["interface-namespaces"], iface_whitelist);
648 
649     sdbusplus::asio::object_server server(system_bus);
650 
651     // Construct a signal set registered for process termination.
652     boost::asio::signal_set signals(io, SIGINT, SIGTERM);
653     signals.async_wait(
654         [&io](const boost::system::error_code&, int) { io.stop(); });
655 
656     interface_map_type interface_map;
657     boost::container::flat_map<std::string, std::string> name_owners;
658 
659     std::function<void(sdbusplus::message::message & message)>
660         nameChangeHandler = [&interface_map, &io, &name_owners, &server,
661                              system_bus](sdbusplus::message::message& message) {
662             std::string name;      // well-known
663             std::string old_owner; // unique-name
664             std::string new_owner; // unique-name
665 
666             message.read(name, old_owner, new_owner);
667 
668             if (!old_owner.empty())
669             {
670                 processNameChangeDelete(name_owners, name, old_owner,
671                                         interface_map, associationMaps, server);
672             }
673 
674             if (!new_owner.empty())
675             {
676 #ifdef DEBUG
677                 auto transaction = std::make_shared<
678                     std::chrono::time_point<std::chrono::steady_clock>>(
679                     std::chrono::steady_clock::now());
680 #endif
681                 // New daemon added
682                 if (needToIntrospect(name, service_whitelist,
683                                      service_blacklist))
684                 {
685                     name_owners[new_owner] = name;
686                     start_new_introspect(system_bus.get(), io, interface_map,
687                                          name, associationMaps,
688 #ifdef DEBUG
689                                          transaction,
690 #endif
691                                          server);
692                 }
693             }
694         };
695 
696     sdbusplus::bus::match::match nameOwnerChanged(
697         static_cast<sdbusplus::bus::bus&>(*system_bus),
698         sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
699 
700     std::function<void(sdbusplus::message::message & message)>
701         interfacesAddedHandler = [&interface_map, &name_owners, &server](
702                                      sdbusplus::message::message& message) {
703             sdbusplus::message::object_path obj_path;
704             InterfacesAdded interfaces_added;
705             message.read(obj_path, interfaces_added);
706             std::string well_known;
707             if (!getWellKnown(name_owners, message.get_sender(), well_known))
708             {
709                 return; // only introspect well-known
710             }
711             if (needToIntrospect(well_known, service_whitelist,
712                                  service_blacklist))
713             {
714                 processInterfaceAdded(interface_map, obj_path, interfaces_added,
715                                       well_known, associationMaps, server);
716             }
717         };
718 
719     sdbusplus::bus::match::match interfacesAdded(
720         static_cast<sdbusplus::bus::bus&>(*system_bus),
721         sdbusplus::bus::match::rules::interfacesAdded(),
722         interfacesAddedHandler);
723 
724     std::function<void(sdbusplus::message::message & message)>
725         interfacesRemovedHandler = [&interface_map, &name_owners, &server](
726                                        sdbusplus::message::message& message) {
727             sdbusplus::message::object_path obj_path;
728             std::vector<std::string> interfaces_removed;
729             message.read(obj_path, interfaces_removed);
730             auto connection_map = interface_map.find(obj_path.str);
731             if (connection_map == interface_map.end())
732             {
733                 return;
734             }
735 
736             std::string sender;
737             if (!getWellKnown(name_owners, message.get_sender(), sender))
738             {
739                 return;
740             }
741             for (const std::string& interface : interfaces_removed)
742             {
743                 auto interface_set = connection_map->second.find(sender);
744                 if (interface_set == connection_map->second.end())
745                 {
746                     continue;
747                 }
748 
749                 if (interface == assocDefsInterface)
750                 {
751                     removeAssociation(obj_path.str, sender, server,
752                                       associationMaps);
753                 }
754 
755                 interface_set->second.erase(interface);
756 
757                 if (interface_set->second.empty())
758                 {
759                     // If this was the last interface on this connection,
760                     // erase the connection
761                     connection_map->second.erase(interface_set);
762 
763                     // Instead of checking if every single path is the endpoint
764                     // of an association that needs to be moved to pending,
765                     // only check when the only remaining owner of this path is
766                     // ourself, which would be because we still own the
767                     // association path.
768                     if ((connection_map->second.size() == 1) &&
769                         (connection_map->second.begin()->first ==
770                          "xyz.openbmc_project.ObjectMapper"))
771                     {
772                         // Remove the 2 association D-Bus paths and move the
773                         // association to pending.
774                         moveAssociationToPending(obj_path.str, associationMaps,
775                                                  server);
776                     }
777                 }
778             }
779             // If this was the last connection on this object path,
780             // erase the object path
781             if (connection_map->second.empty())
782             {
783                 interface_map.erase(connection_map);
784             }
785 
786             removeUnneededParents(obj_path.str, sender, interface_map);
787         };
788 
789     sdbusplus::bus::match::match interfacesRemoved(
790         static_cast<sdbusplus::bus::bus&>(*system_bus),
791         sdbusplus::bus::match::rules::interfacesRemoved(),
792         interfacesRemovedHandler);
793 
794     std::function<void(sdbusplus::message::message & message)>
795         associationChangedHandler = [&server, &name_owners, &interface_map](
796                                         sdbusplus::message::message& message) {
797             std::string objectName;
798             boost::container::flat_map<std::string,
799                                        std::variant<std::vector<Association>>>
800                 values;
801             message.read(objectName, values);
802             auto prop = values.find(assocDefsProperty);
803             if (prop != values.end())
804             {
805                 std::vector<Association> associations =
806                     std::get<std::vector<Association>>(prop->second);
807 
808                 std::string well_known;
809                 if (!getWellKnown(name_owners, message.get_sender(),
810                                   well_known))
811                 {
812                     return;
813                 }
814                 associationChanged(server, associations, message.get_path(),
815                                    well_known, interface_map, associationMaps);
816             }
817         };
818     sdbusplus::bus::match::match assocChangedMatch(
819         static_cast<sdbusplus::bus::bus&>(*system_bus),
820         sdbusplus::bus::match::rules::interface(
821             "org.freedesktop.DBus.Properties") +
822             sdbusplus::bus::match::rules::member("PropertiesChanged") +
823             sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
824         associationChangedHandler);
825 
826     std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
827         server.add_interface("/xyz/openbmc_project/object_mapper",
828                              "xyz.openbmc_project.ObjectMapper");
829 
830     iface->register_method(
831         "GetAncestors", [&interface_map](std::string& req_path,
832                                          std::vector<std::string>& interfaces) {
833             return getAncestors(interface_map, req_path, interfaces);
834         });
835 
836     iface->register_method(
837         "GetObject", [&interface_map](const std::string& path,
838                                       std::vector<std::string>& interfaces) {
839             return getObject(interface_map, path, interfaces);
840         });
841 
842     iface->register_method(
843         "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
844                                        std::vector<std::string>& interfaces) {
845             return getSubTree(interface_map, req_path, depth, interfaces);
846         });
847 
848     iface->register_method(
849         "GetSubTreePaths",
850         [&interface_map](std::string& req_path, int32_t depth,
851                          std::vector<std::string>& interfaces) {
852             return getSubTreePaths(interface_map, req_path, depth, interfaces);
853         });
854 
855     iface->initialize();
856 
857     io.post([&]() {
858         doListNames(io, interface_map, system_bus.get(), name_owners,
859                     associationMaps, server);
860     });
861 
862     system_bus->request_name("xyz.openbmc_project.ObjectMapper");
863 
864     io.run();
865 }
866