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