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